Home | History | Annotate | Line # | Download | only in quotacheck
quotacheck.c revision 1.23
      1 /*	$NetBSD: quotacheck.c,v 1.23 2002/12/10 22:42:00 bouyer 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. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #ifndef lint
     41 __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\n\
     42 	The Regents of the University of California.  All rights reserved.\n");
     43 #endif /* not lint */
     44 
     45 #ifndef lint
     46 #if 0
     47 static char sccsid[] = "@(#)quotacheck.c	8.6 (Berkeley) 4/28/95";
     48 #else
     49 __RCSID("$NetBSD: quotacheck.c,v 1.23 2002/12/10 22:42:00 bouyer Exp $");
     50 #endif
     51 #endif /* not lint */
     52 
     53 /*
     54  * Fix up / report on disk quotas & usage
     55  */
     56 #include <sys/param.h>
     57 #include <sys/stat.h>
     58 #include <sys/queue.h>
     59 
     60 #include <ufs/ufs/dinode.h>
     61 #include <ufs/ufs/quota.h>
     62 #include <ufs/ufs/ufs_bswap.h>
     63 #include <ufs/ffs/fs.h>
     64 #include <ufs/ffs/ffs_extern.h>
     65 
     66 #include <err.h>
     67 #include <fcntl.h>
     68 #include <fstab.h>
     69 #include <pwd.h>
     70 #include <grp.h>
     71 #include <errno.h>
     72 #include <unistd.h>
     73 #include <stdio.h>
     74 #include <stdlib.h>
     75 #include <string.h>
     76 
     77 #include "fsutil.h"
     78 
     79 static char *qfname = QUOTAFILENAME;
     80 static char *qfextension[] = INITQFNAMES;
     81 static char *quotagroup = QUOTAGROUP;
     82 
     83 static union {
     84 	struct	fs	sblk;
     85 	char	dummy[MAXBSIZE];
     86 } un;
     87 #define	sblock	un.sblk
     88 static long dev_bsize;
     89 static long maxino;
     90 
     91 struct quotaname {
     92 	long	flags;
     93 	char	grpqfname[MAXPATHLEN + 1];
     94 	char	usrqfname[MAXPATHLEN + 1];
     95 };
     96 #define	HASUSR	1
     97 #define	HASGRP	2
     98 
     99 struct fileusage {
    100 	struct	fileusage *fu_next;
    101 	u_long	fu_curinodes;
    102 	u_long	fu_curblocks;
    103 	u_long	fu_id;
    104 	char	fu_name[1];
    105 	/* actually bigger */
    106 };
    107 #define FUHASH 1024	/* must be power of two */
    108 static struct fileusage *fuhead[MAXQUOTAS][FUHASH];
    109 
    110 static int	aflag;		/* all file systems */
    111 static int	gflag;		/* check group quotas */
    112 static int	uflag;		/* check user quotas */
    113 static int	vflag;		/* verbose */
    114 static int	fi;		/* open disk file descriptor */
    115 static u_long	highid[MAXQUOTAS];/* highest addid()'ed identifier per type */
    116 static int needswap;	/* FS is in swapped order */
    117 static int got_siginfo = 0; /* got a siginfo signal */
    118 
    119 
    120 int main __P((int, char *[]));
    121 static void usage __P((void));
    122 static void *needchk __P((struct fstab *));
    123 static int chkquota __P((const char *, const char *, const char *, void *,
    124     pid_t *));
    125 static int update __P((const char *, const char *, int));
    126 static int oneof __P((const char *, char *[], int));
    127 static int getquotagid __P((void));
    128 static int hasquota __P((struct fstab *, int, char **));
    129 static struct fileusage *lookup __P((u_long, int));
    130 static struct fileusage *addid __P((u_long, int, const char *));
    131 static struct dinode *getnextinode __P((ino_t));
    132 static void resetinodebuf __P((void));
    133 static void freeinodebuf __P((void));
    134 static void bread __P((daddr_t, char *, long));
    135 static void infohandler __P((int sig));
    136 
    137 int
    138 main(argc, argv)
    139 	int argc;
    140 	char *argv[];
    141 {
    142 	struct fstab *fs;
    143 	struct passwd *pw;
    144 	struct group *gr;
    145 	struct quotaname *auxdata;
    146 	int i, argnum, maxrun, errs;
    147 	long done = 0;
    148 	int flags = CHECK_PREEN;
    149 	const char *name;
    150 	int ch;
    151 
    152 	errs = maxrun = 0;
    153 	while ((ch = getopt(argc, argv, "aguvdl:")) != -1) {
    154 		switch(ch) {
    155 		case 'a':
    156 			aflag++;
    157 			break;
    158 		case 'd':
    159 			flags |= CHECK_DEBUG;
    160 			break;
    161 		case 'g':
    162 			gflag++;
    163 			break;
    164 		case 'u':
    165 			uflag++;
    166 			break;
    167 		case 'v':
    168 			vflag++;
    169 			break;
    170 		case 'l':
    171 			maxrun = atoi(optarg);
    172 			break;
    173 		default:
    174 			usage();
    175 		}
    176 	}
    177 	argc -= optind;
    178 	argv += optind;
    179 	if ((argc == 0 && !aflag) || (argc > 0 && aflag))
    180 		usage();
    181 	if (!gflag && !uflag) {
    182 		gflag++;
    183 		uflag++;
    184 	}
    185 
    186 	/* If -a, we do not want to pay the cost of processing every
    187 	 * group and password entry if there are no filesystems with quotas
    188 	 */
    189 	if (aflag) {
    190 		i = 0;
    191 		while ((fs = getfsent()) != NULL) {
    192 			if (needchk(fs))
    193 				i=1;
    194 		}
    195 		endfsent();
    196 		if (!i)	/* No filesystems with quotas */
    197 			exit(0);
    198 	}
    199 
    200 	if (gflag) {
    201 		setgrent();
    202 		while ((gr = getgrent()) != 0)
    203 			(void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
    204 		endgrent();
    205 	}
    206 	if (uflag) {
    207 		setpwent();
    208 		while ((pw = getpwent()) != 0)
    209 			(void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
    210 		endpwent();
    211 	}
    212 	if (aflag)
    213 		exit(checkfstab(flags, maxrun, needchk, chkquota));
    214 	if (setfsent() == 0)
    215 		err(1, "%s: can't open", FSTAB);
    216 	while ((fs = getfsent()) != NULL) {
    217 		if (((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
    218 		    (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) &&
    219 		    (auxdata = needchk(fs)) &&
    220 		    (name = blockcheck(fs->fs_spec))) {
    221 			done |= 1 << argnum;
    222 			errs += chkquota(fs->fs_type, name, fs->fs_file,
    223 			    auxdata, NULL);
    224 		}
    225 	}
    226 	endfsent();
    227 	for (i = 0; i < argc; i++)
    228 		if ((done & (1 << i)) == 0)
    229 			fprintf(stderr, "%s not found in %s\n",
    230 				argv[i], FSTAB);
    231 	exit(errs);
    232 }
    233 
    234 static void
    235 usage()
    236 {
    237 
    238 	(void)fprintf(stderr,
    239 	    "Usage:\t%s -a [-guv]\n\t%s [-guv] filesys ...\n", getprogname(),
    240 	    getprogname());
    241 	exit(1);
    242 }
    243 
    244 static void *
    245 needchk(fs)
    246 	struct fstab *fs;
    247 {
    248 	struct quotaname *qnp;
    249 	char *qfnp;
    250 
    251 	if (strcmp(fs->fs_vfstype, "ffs") ||
    252 	    strcmp(fs->fs_type, FSTAB_RW))
    253 		return (NULL);
    254 	if ((qnp = malloc(sizeof(*qnp))) == NULL)
    255 		err(1, "%s", strerror(errno));
    256 	qnp->flags = 0;
    257 	if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) {
    258 		strcpy(qnp->grpqfname, qfnp);
    259 		qnp->flags |= HASGRP;
    260 	}
    261 	if (uflag && hasquota(fs, USRQUOTA, &qfnp)) {
    262 		strcpy(qnp->usrqfname, qfnp);
    263 		qnp->flags |= HASUSR;
    264 	}
    265 	if (qnp->flags)
    266 		return (qnp);
    267 	free(qnp);
    268 	return (NULL);
    269 }
    270 
    271 /*
    272  * Scan the specified filesystem to check quota(s) present on it.
    273  */
    274 static int
    275 chkquota(type, fsname, mntpt, v, pid)
    276 	const char *type, *fsname, *mntpt;
    277 	void *v;
    278 	pid_t *pid;
    279 {
    280 	struct quotaname *qnp = v;
    281 	struct fileusage *fup;
    282 	struct dinode *dp;
    283 	int cg, i, mode, errs = 0;
    284 	ino_t ino;
    285 
    286 	if (pid != NULL) {
    287 		switch ((*pid = fork())) {
    288 		default:
    289 			break;
    290 		case 0:
    291 			return 0;
    292 		case -1:
    293 			err(1, "Cannot fork");
    294 		}
    295 	}
    296 
    297 	if ((fi = open(fsname, O_RDONLY, 0)) < 0) {
    298 		warn("Cannot open %s", fsname);
    299 		return (1);
    300 	}
    301 	if (vflag) {
    302 		(void)printf("*** Checking ");
    303 		if (qnp->flags & HASUSR)
    304 			(void)printf("%s%s", qfextension[USRQUOTA],
    305 			    (qnp->flags & HASGRP) ? " and " : "");
    306 		if (qnp->flags & HASGRP)
    307 			(void)printf("%s", qfextension[GRPQUOTA]);
    308 		(void)printf(" quotas for %s (%s)\n", fsname, mntpt);
    309 	}
    310 	signal(SIGINFO, infohandler);
    311 	sync();
    312 	dev_bsize = 1;
    313 	bread(SBOFF, (char *)&sblock, (long)SBSIZE);
    314 	if (sblock.fs_magic != FS_MAGIC) {
    315 		if (sblock.fs_magic== bswap32(FS_MAGIC)) {
    316 			needswap = 1;
    317 			ffs_sb_swap(&sblock, &sblock);
    318 		} else
    319 			errx(1, "%s: superblock magic number 0x%x, not 0x%x",
    320 				fsname, sblock.fs_magic, FS_MAGIC);
    321 	}
    322 	dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
    323 	maxino = sblock.fs_ncg * sblock.fs_ipg;
    324 	resetinodebuf();
    325 	for (ino = 0, cg = 0; cg < sblock.fs_ncg; cg++) {
    326 		for (i = 0; i < sblock.fs_ipg; i++, ino++) {
    327 			if (got_siginfo) {
    328 				fprintf(stderr,
    329 				    "%s: cyl group %d of %d (%d%%)\n",
    330 				    fsname, cg, sblock.fs_ncg,
    331 				    cg * 100 / sblock.fs_ncg);
    332 				got_siginfo = 0;
    333 			}
    334 			if (ino < ROOTINO)
    335 				continue;
    336 			if ((dp = getnextinode(ino)) == NULL)
    337 				continue;
    338 			if ((mode = dp->di_mode & IFMT) == 0)
    339 				continue;
    340 			if (qnp->flags & HASGRP) {
    341 				fup = addid((u_long)dp->di_gid, GRPQUOTA,
    342 				    (char *)0);
    343 				fup->fu_curinodes++;
    344 				if (mode == IFREG || mode == IFDIR ||
    345 				    mode == IFLNK)
    346 					fup->fu_curblocks += dp->di_blocks;
    347 			}
    348 			if (qnp->flags & HASUSR) {
    349 				fup = addid((u_long)dp->di_uid, USRQUOTA,
    350 				    (char *)0);
    351 				fup->fu_curinodes++;
    352 				if (mode == IFREG || mode == IFDIR ||
    353 				    mode == IFLNK)
    354 					fup->fu_curblocks += dp->di_blocks;
    355 			}
    356 		}
    357 	}
    358 	freeinodebuf();
    359 	if (qnp->flags & HASUSR)
    360 		errs += update(mntpt, qnp->usrqfname, USRQUOTA);
    361 	if (qnp->flags & HASGRP)
    362 		errs += update(mntpt, qnp->grpqfname, GRPQUOTA);
    363 	close(fi);
    364 	return (errs);
    365 }
    366 
    367 /*
    368  * Update a specified quota file.
    369  */
    370 static int
    371 update(fsname, quotafile, type)
    372 	const char *fsname, *quotafile;
    373 	int type;
    374 {
    375 	struct fileusage *fup;
    376 	FILE *qfi, *qfo;
    377 	u_long id, lastid;
    378 	struct dqblk dqbuf;
    379 	static int warned = 0;
    380 	static struct dqblk zerodqbuf;
    381 	static struct fileusage zerofileusage;
    382 
    383 	if ((qfo = fopen(quotafile, "r+")) == NULL) {
    384 		if (errno == ENOENT)
    385 			qfo = fopen(quotafile, "w+");
    386 		if (qfo) {
    387 			(void) fprintf(stderr,
    388 			    "quotacheck: creating quota file %s\n", quotafile);
    389 #define	MODE	(S_IRUSR|S_IWUSR|S_IRGRP)
    390 			(void) fchown(fileno(qfo), getuid(), getquotagid());
    391 			(void) fchmod(fileno(qfo), MODE);
    392 		} else {
    393 			(void) fprintf(stderr,
    394 			    "quotacheck: %s: %s\n", quotafile, strerror(errno));
    395 			return (1);
    396 		}
    397 	}
    398 	if ((qfi = fopen(quotafile, "r")) == NULL) {
    399 		(void) fprintf(stderr,
    400 		    "quotacheck: %s: %s\n", quotafile, strerror(errno));
    401 		(void) fclose(qfo);
    402 		return (1);
    403 	}
    404 	if (quotactl(fsname, QCMD(Q_SYNC, type), (u_long)0, (caddr_t)0) < 0 &&
    405 	    errno == EOPNOTSUPP && !warned && vflag) {
    406 		warned++;
    407 		(void)printf("*** Warning: %s\n",
    408 		    "Quotas are not compiled into this kernel");
    409 	}
    410 	for (lastid = highid[type], id = 0; id <= lastid; id++) {
    411 		if (fread((char *)&dqbuf, sizeof(struct dqblk), 1, qfi) == 0)
    412 			dqbuf = zerodqbuf;
    413 		if ((fup = lookup(id, type)) == 0)
    414 			fup = &zerofileusage;
    415 		if (dqbuf.dqb_curinodes == fup->fu_curinodes &&
    416 		    dqbuf.dqb_curblocks == fup->fu_curblocks) {
    417 			fup->fu_curinodes = 0;
    418 			fup->fu_curblocks = 0;
    419 			(void) fseek(qfo, (long)sizeof(struct dqblk), 1);
    420 			continue;
    421 		}
    422 		if (vflag) {
    423 			if (aflag)
    424 				printf("%s: ", fsname);
    425 			printf("%-8s fixed:", fup->fu_name);
    426 			if (dqbuf.dqb_curinodes != fup->fu_curinodes)
    427 				(void)printf("\tinodes %d -> %ld",
    428 					dqbuf.dqb_curinodes, fup->fu_curinodes);
    429 			if (dqbuf.dqb_curblocks != fup->fu_curblocks)
    430 				(void)printf("\tblocks %d -> %ld",
    431 					dqbuf.dqb_curblocks, fup->fu_curblocks);
    432 			(void)printf("\n");
    433 		}
    434 		/*
    435 		 * Reset time limit if have a soft limit and were
    436 		 * previously under it, but are now over it.
    437 		 */
    438 		if (dqbuf.dqb_bsoftlimit &&
    439 		    dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
    440 		    fup->fu_curblocks >= dqbuf.dqb_bsoftlimit)
    441 			dqbuf.dqb_btime = 0;
    442 		if (dqbuf.dqb_isoftlimit &&
    443 		    dqbuf.dqb_curblocks < dqbuf.dqb_isoftlimit &&
    444 		    fup->fu_curblocks >= dqbuf.dqb_isoftlimit)
    445 			dqbuf.dqb_itime = 0;
    446 		dqbuf.dqb_curinodes = fup->fu_curinodes;
    447 		dqbuf.dqb_curblocks = fup->fu_curblocks;
    448 		(void) fwrite((char *)&dqbuf, sizeof(struct dqblk), 1, qfo);
    449 		(void) quotactl(fsname, QCMD(Q_SETUSE, type), id,
    450 		    (caddr_t)&dqbuf);
    451 		fup->fu_curinodes = 0;
    452 		fup->fu_curblocks = 0;
    453 	}
    454 	(void) fclose(qfi);
    455 	(void) fflush(qfo);
    456 	(void) ftruncate(fileno(qfo),
    457 	    (off_t)((highid[type] + 1) * sizeof(struct dqblk)));
    458 	(void) fclose(qfo);
    459 	return (0);
    460 }
    461 
    462 /*
    463  * Check to see if target appears in list of size cnt.
    464  */
    465 static int
    466 oneof(target, list, cnt)
    467 	const char *target;
    468 	char *list[];
    469 	int cnt;
    470 {
    471 	int i;
    472 
    473 	for (i = 0; i < cnt; i++)
    474 		if (strcmp(target, list[i]) == 0)
    475 			return (i);
    476 	return (-1);
    477 }
    478 
    479 /*
    480  * Determine the group identifier for quota files.
    481  */
    482 static int
    483 getquotagid()
    484 {
    485 	struct group *gr;
    486 
    487 	if ((gr = getgrnam(quotagroup)) != NULL)
    488 		return (gr->gr_gid);
    489 	return (-1);
    490 }
    491 
    492 /*
    493  * Check to see if a particular quota is to be enabled.
    494  */
    495 static int
    496 hasquota(fs, type, qfnamep)
    497 	struct fstab *fs;
    498 	int type;
    499 	char **qfnamep;
    500 {
    501 	char *opt;
    502 	char *cp = NULL;
    503 	static char initname, usrname[100], grpname[100];
    504 	static char buf[BUFSIZ];
    505 
    506 	if (!initname) {
    507 		(void)snprintf(usrname, sizeof(usrname),
    508 		    "%s%s", qfextension[USRQUOTA], qfname);
    509 		(void)snprintf(grpname, sizeof(grpname),
    510 		    "%s%s", qfextension[GRPQUOTA], qfname);
    511 		initname = 1;
    512 	}
    513 	(void) strcpy(buf, fs->fs_mntops);
    514 	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
    515 		if ((cp = strchr(opt, '=')) != NULL)
    516 			*cp++ = '\0';
    517 		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
    518 			break;
    519 		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
    520 			break;
    521 	}
    522 	if (!opt)
    523 		return (0);
    524 	if (cp)
    525 		*qfnamep = cp;
    526 	else {
    527 		(void)snprintf(buf, sizeof(buf),
    528 		    "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
    529 		*qfnamep = buf;
    530 	}
    531 	return (1);
    532 }
    533 
    534 /*
    535  * Routines to manage the file usage table.
    536  *
    537  * Lookup an id of a specific type.
    538  */
    539 static struct fileusage *
    540 lookup(id, type)
    541 	u_long id;
    542 	int type;
    543 {
    544 	struct fileusage *fup;
    545 
    546 	for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
    547 		if (fup->fu_id == id)
    548 			return (fup);
    549 	return (NULL);
    550 }
    551 
    552 /*
    553  * Add a new file usage id if it does not already exist.
    554  */
    555 static struct fileusage *
    556 addid(id, type, name)
    557 	u_long id;
    558 	int type;
    559 	const char *name;
    560 {
    561 	struct fileusage *fup, **fhp;
    562 	int len;
    563 
    564 	if ((fup = lookup(id, type)) != NULL)
    565 		return (fup);
    566 	if (name)
    567 		len = strlen(name);
    568 	else
    569 		len = 10;
    570 	if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
    571 		err(1, "%s", strerror(errno));
    572 	fhp = &fuhead[type][id & (FUHASH - 1)];
    573 	fup->fu_next = *fhp;
    574 	*fhp = fup;
    575 	fup->fu_id = id;
    576 	if (id > highid[type])
    577 		highid[type] = id;
    578 	if (name)
    579 		memmove(fup->fu_name, name, len + 1);
    580 	else
    581 		(void)sprintf(fup->fu_name, "%lu", id);
    582 	return (fup);
    583 }
    584 
    585 /*
    586  * Special purpose version of ginode used to optimize pass
    587  * over all the inodes in numerical order.
    588  */
    589 static ino_t nextino, lastinum;
    590 static long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
    591 static struct dinode *inodebuf;
    592 #define	INOBUFSIZE	56*1024	/* size of buffer to read inodes */
    593 
    594 static struct dinode *
    595 getnextinode(inumber)
    596 	ino_t inumber;
    597 {
    598 	long size;
    599 	daddr_t dblk;
    600 	static struct dinode *dp;
    601 
    602 	if (inumber != nextino++ || inumber > maxino)
    603 		err(1, "bad inode number %d to nextinode", inumber);
    604 	if (inumber >= lastinum) {
    605 		readcnt++;
    606 		dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
    607 		if (readcnt % readpercg == 0) {
    608 			size = partialsize;
    609 			lastinum += partialcnt;
    610 		} else {
    611 			size = inobufsize;
    612 			lastinum += fullcnt;
    613 		}
    614 		bread(dblk, (char *)inodebuf, size);
    615 		dp = inodebuf;
    616 	}
    617 	if (needswap)
    618 		ffs_dinode_swap(dp, dp);
    619 	return (dp++);
    620 }
    621 
    622 /*
    623  * Prepare to scan a set of inodes.
    624  */
    625 static void
    626 resetinodebuf()
    627 {
    628 
    629 	nextino = 0;
    630 	lastinum = 0;
    631 	readcnt = 0;
    632 	inobufsize = blkroundup(&sblock, INOBUFSIZE);
    633 	fullcnt = inobufsize / sizeof(struct dinode);
    634 	readpercg = sblock.fs_ipg / fullcnt;
    635 	partialcnt = sblock.fs_ipg % fullcnt;
    636 	partialsize = partialcnt * sizeof(struct dinode);
    637 	if (partialcnt != 0) {
    638 		readpercg++;
    639 	} else {
    640 		partialcnt = fullcnt;
    641 		partialsize = inobufsize;
    642 	}
    643 	if (inodebuf == NULL &&
    644 	   (inodebuf = malloc((u_int)inobufsize)) == NULL)
    645 		err(1, "%s", strerror(errno));
    646 	while (nextino < ROOTINO)
    647 		getnextinode(nextino);
    648 }
    649 
    650 /*
    651  * Free up data structures used to scan inodes.
    652  */
    653 static void
    654 freeinodebuf()
    655 {
    656 
    657 	if (inodebuf != NULL)
    658 		free(inodebuf);
    659 	inodebuf = NULL;
    660 }
    661 
    662 /*
    663  * Read specified disk blocks.
    664  */
    665 static void
    666 bread(bno, buf, cnt)
    667 	daddr_t bno;
    668 	char *buf;
    669 	long cnt;
    670 {
    671 
    672 	if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0 ||
    673 	    read(fi, buf, cnt) != cnt)
    674 		err(1, "block %d", bno);
    675 }
    676 
    677 void
    678 infohandler(int sig)
    679 {
    680 	got_siginfo = 1;
    681 }
    682 
    683