Home | History | Annotate | Line # | Download | only in quotacheck
quotacheck.c revision 1.28
      1 /*	$NetBSD: quotacheck.c,v 1.28 2003/08/07 11:25:39 agc 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.28 2003/08/07 11:25:39 agc 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	fi;		/* open disk file descriptor */
    120 static u_long	highid[MAXQUOTAS];/* highest addid()'ed identifier per type */
    121 static int needswap;	/* FS is in swapped order */
    122 static int got_siginfo = 0; /* got a siginfo signal */
    123 static int is_ufs2;
    124 
    125 
    126 int main __P((int, char *[]));
    127 static void usage __P((void));
    128 static void *needchk __P((struct fstab *));
    129 static int chkquota __P((const char *, const char *, const char *, void *,
    130     pid_t *));
    131 static int update __P((const char *, const char *, int));
    132 static int oneof __P((const char *, char *[], int));
    133 static int getquotagid __P((void));
    134 static int hasquota __P((struct fstab *, int, char **));
    135 static struct fileusage *lookup __P((u_long, int));
    136 static struct fileusage *addid __P((u_long, int, const char *));
    137 static union dinode *getnextinode __P((ino_t));
    138 static void setinodebuf __P((ino_t));
    139 static void freeinodebuf __P((void));
    140 static void bread __P((daddr_t, char *, long));
    141 static void infohandler __P((int sig));
    142 static void swap_dinode1(union dinode *, int);
    143 static void swap_dinode2(union dinode *, int);
    144 
    145 int
    146 main(argc, argv)
    147 	int argc;
    148 	char *argv[];
    149 {
    150 	struct fstab *fs;
    151 	struct passwd *pw;
    152 	struct group *gr;
    153 	struct quotaname *auxdata;
    154 	int i, argnum, maxrun, errs;
    155 	long done = 0;
    156 	int flags = CHECK_PREEN;
    157 	const char *name;
    158 	int ch;
    159 
    160 	errs = maxrun = 0;
    161 	while ((ch = getopt(argc, argv, "aguvdl:")) != -1) {
    162 		switch(ch) {
    163 		case 'a':
    164 			aflag++;
    165 			break;
    166 		case 'd':
    167 			flags |= CHECK_DEBUG;
    168 			break;
    169 		case 'g':
    170 			gflag++;
    171 			break;
    172 		case 'u':
    173 			uflag++;
    174 			break;
    175 		case 'v':
    176 			vflag++;
    177 			break;
    178 		case 'l':
    179 			maxrun = atoi(optarg);
    180 			break;
    181 		default:
    182 			usage();
    183 		}
    184 	}
    185 	argc -= optind;
    186 	argv += optind;
    187 	if ((argc == 0 && !aflag) || (argc > 0 && aflag))
    188 		usage();
    189 	if (!gflag && !uflag) {
    190 		gflag++;
    191 		uflag++;
    192 	}
    193 
    194 	/* If -a, we do not want to pay the cost of processing every
    195 	 * group and password entry if there are no filesystems with quotas
    196 	 */
    197 	if (aflag) {
    198 		i = 0;
    199 		while ((fs = getfsent()) != NULL) {
    200 			if (needchk(fs))
    201 				i=1;
    202 		}
    203 		endfsent();
    204 		if (!i)	/* No filesystems with quotas */
    205 			exit(0);
    206 	}
    207 
    208 	if (gflag) {
    209 		setgrent();
    210 		while ((gr = getgrent()) != 0)
    211 			(void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
    212 		endgrent();
    213 	}
    214 	if (uflag) {
    215 		setpwent();
    216 		while ((pw = getpwent()) != 0)
    217 			(void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
    218 		endpwent();
    219 	}
    220 	if (aflag)
    221 		exit(checkfstab(flags, maxrun, needchk, chkquota));
    222 	if (setfsent() == 0)
    223 		err(1, "%s: can't open", FSTAB);
    224 	while ((fs = getfsent()) != NULL) {
    225 		if (((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
    226 		    (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) &&
    227 		    (auxdata = needchk(fs)) &&
    228 		    (name = blockcheck(fs->fs_spec))) {
    229 			done |= 1 << argnum;
    230 			errs += chkquota(fs->fs_type, name, fs->fs_file,
    231 			    auxdata, NULL);
    232 		}
    233 	}
    234 	endfsent();
    235 	for (i = 0; i < argc; i++)
    236 		if ((done & (1 << i)) == 0)
    237 			fprintf(stderr, "%s not found in %s\n",
    238 				argv[i], FSTAB);
    239 	exit(errs);
    240 }
    241 
    242 static void
    243 usage()
    244 {
    245 
    246 	(void)fprintf(stderr,
    247 	    "Usage:\t%s -a [-guv]\n\t%s [-guv] filesys ...\n", getprogname(),
    248 	    getprogname());
    249 	exit(1);
    250 }
    251 
    252 static void *
    253 needchk(fs)
    254 	struct fstab *fs;
    255 {
    256 	struct quotaname *qnp;
    257 	char *qfnp;
    258 
    259 	if (strcmp(fs->fs_vfstype, "ffs") ||
    260 	    strcmp(fs->fs_type, FSTAB_RW))
    261 		return (NULL);
    262 	if ((qnp = malloc(sizeof(*qnp))) == NULL)
    263 		err(1, "%s", strerror(errno));
    264 	qnp->flags = 0;
    265 	if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) {
    266 		strlcpy(qnp->grpqfname, qfnp, sizeof(qnp->grpqfname));
    267 		qnp->flags |= HASGRP;
    268 	}
    269 	if (uflag && hasquota(fs, USRQUOTA, &qfnp)) {
    270 		strlcpy(qnp->usrqfname, qfnp, sizeof(qnp->usrqfname));
    271 		qnp->flags |= HASUSR;
    272 	}
    273 	if (qnp->flags)
    274 		return (qnp);
    275 	free(qnp);
    276 	return (NULL);
    277 }
    278 
    279 off_t sblock_try[] = SBLOCKSEARCH;
    280 
    281 /*
    282  * Scan the specified filesystem to check quota(s) present on it.
    283  */
    284 static int
    285 chkquota(type, fsname, mntpt, v, pid)
    286 	const char *type, *fsname, *mntpt;
    287 	void *v;
    288 	pid_t *pid;
    289 {
    290 	struct quotaname *qnp = v;
    291 	struct fileusage *fup;
    292 	union dinode *dp;
    293 	int cg, i, mode, errs = 0, inosused;
    294 	ino_t ino;
    295 	struct cg *cgp;
    296 
    297 	if (pid != NULL) {
    298 		switch ((*pid = fork())) {
    299 		default:
    300 			break;
    301 		case 0:
    302 			return 0;
    303 		case -1:
    304 			err(1, "Cannot fork");
    305 		}
    306 	}
    307 
    308 	if ((fi = open(fsname, O_RDONLY, 0)) < 0) {
    309 		warn("Cannot open %s", fsname);
    310 		return 1;
    311 	}
    312 	if (vflag) {
    313 		(void)printf("*** Checking ");
    314 		if (qnp->flags & HASUSR)
    315 			(void)printf("%s%s", qfextension[USRQUOTA],
    316 			    (qnp->flags & HASGRP) ? " and " : "");
    317 		if (qnp->flags & HASGRP)
    318 			(void)printf("%s", qfextension[GRPQUOTA]);
    319 		(void)printf(" quotas for %s (%s)\n", fsname, mntpt);
    320 	}
    321 	signal(SIGINFO, infohandler);
    322 	sync();
    323 	dev_bsize = 1;
    324 
    325 	cgp = malloc(sblock.fs_cgsize);
    326 	if (cgp == NULL) {
    327 		warn("%s: can't allocate %d bytes of cg space", fsname,
    328 		    sblock.fs_cgsize);
    329 		return 1;
    330 	}
    331 
    332 	for (i = 0; sblock_try[i] != -1; i++) {
    333 		bread(sblock_try[i], (char *)&sblock, SBLOCKSIZE);
    334 		switch (sblock.fs_magic) {
    335 		case FS_UFS2_MAGIC:
    336 			is_ufs2 = 1;
    337 			/*FALLTHROUGH*/
    338 		case FS_UFS1_MAGIC:
    339 			goto found;
    340 		case FS_UFS2_MAGIC_SWAPPED:
    341 			is_ufs2 = 1;
    342 			/*FALLTHROUGH*/
    343 		case FS_UFS1_MAGIC_SWAPPED:
    344 			needswap = 1;
    345 			goto found;
    346 		default:
    347 			continue;
    348 		}
    349 	}
    350 	warnx("%s: superblock not found", fsname);
    351 	free(cgp);
    352 	return 1;
    353 found:
    354 	if (needswap)
    355 		ffs_sb_swap(&sblock, &sblock);
    356 	dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
    357 	maxino = sblock.fs_ncg * sblock.fs_ipg;
    358 	for (ino = 0, cg = 0; cg < sblock.fs_ncg; cg++) {
    359 		setinodebuf(cg * sblock.fs_ipg);
    360 		if (sblock.fs_magic == FS_UFS2_MAGIC) {
    361 			bread(fsbtodb(&sblock, cgtod(&sblock, cg)), (char *)cgp,
    362 			    sblock.fs_cgsize);
    363 			if (needswap)
    364 				ffs_cg_swap(cgp, cgp, &sblock);
    365 			inosused = cgp->cg_initediblk;
    366 		} else
    367 			inosused = sblock.fs_ipg;
    368 		for (i = 0; i < inosused; i++, ino++) {
    369 			if (got_siginfo) {
    370 				fprintf(stderr,
    371 				    "%s: cyl group %d of %d (%d%%)\n",
    372 				    fsname, cg, sblock.fs_ncg,
    373 				    cg * 100 / sblock.fs_ncg);
    374 				got_siginfo = 0;
    375 			}
    376 			if (ino < ROOTINO)
    377 				continue;
    378 			if ((dp = getnextinode(ino)) == NULL)
    379 				continue;
    380 			if ((mode = DIP(dp, mode) & IFMT) == 0)
    381 				continue;
    382 			if (qnp->flags & HASGRP) {
    383 				fup = addid((u_long)DIP(dp, gid), GRPQUOTA,
    384 				    (char *)0);
    385 				fup->fu_curinodes++;
    386 				if (mode == IFREG || mode == IFDIR ||
    387 				    mode == IFLNK)
    388 					fup->fu_curblocks += DIP(dp, blocks);
    389 			}
    390 			if (qnp->flags & HASUSR) {
    391 				fup = addid((u_long)DIP(dp, uid), USRQUOTA,
    392 				    (char *)0);
    393 				fup->fu_curinodes++;
    394 				if (mode == IFREG || mode == IFDIR ||
    395 				    mode == IFLNK)
    396 					fup->fu_curblocks += DIP(dp, blocks);
    397 			}
    398 		}
    399 	}
    400 	freeinodebuf();
    401 	free(cgp);
    402 	if (qnp->flags & HASUSR)
    403 		errs += update(mntpt, qnp->usrqfname, USRQUOTA);
    404 	if (qnp->flags & HASGRP)
    405 		errs += update(mntpt, qnp->grpqfname, GRPQUOTA);
    406 	close(fi);
    407 	return errs;
    408 }
    409 
    410 /*
    411  * Update a specified quota file.
    412  */
    413 static int
    414 update(fsname, quotafile, type)
    415 	const char *fsname, *quotafile;
    416 	int type;
    417 {
    418 	struct fileusage *fup;
    419 	FILE *qfi, *qfo;
    420 	u_long id, lastid;
    421 	struct dqblk dqbuf;
    422 	static int warned = 0;
    423 	static struct dqblk zerodqbuf;
    424 	static struct fileusage zerofileusage;
    425 
    426 	if ((qfo = fopen(quotafile, "r+")) == NULL) {
    427 		if (errno == ENOENT)
    428 			qfo = fopen(quotafile, "w+");
    429 		if (qfo) {
    430 			(void) fprintf(stderr,
    431 			    "quotacheck: creating quota file %s\n", quotafile);
    432 #define	MODE	(S_IRUSR|S_IWUSR|S_IRGRP)
    433 			(void) fchown(fileno(qfo), getuid(), getquotagid());
    434 			(void) fchmod(fileno(qfo), MODE);
    435 		} else {
    436 			(void) fprintf(stderr,
    437 			    "quotacheck: %s: %s\n", quotafile, strerror(errno));
    438 			return (1);
    439 		}
    440 	}
    441 	if ((qfi = fopen(quotafile, "r")) == NULL) {
    442 		(void) fprintf(stderr,
    443 		    "quotacheck: %s: %s\n", quotafile, strerror(errno));
    444 		(void) fclose(qfo);
    445 		return (1);
    446 	}
    447 	if (quotactl(fsname, QCMD(Q_SYNC, type), (u_long)0, (caddr_t)0) < 0 &&
    448 	    errno == EOPNOTSUPP && !warned && vflag) {
    449 		warned++;
    450 		(void)printf("*** Warning: %s\n",
    451 		    "Quotas are not compiled into this kernel");
    452 	}
    453 	for (lastid = highid[type], id = 0; id <= lastid; id++) {
    454 		if (fread((char *)&dqbuf, sizeof(struct dqblk), 1, qfi) == 0)
    455 			dqbuf = zerodqbuf;
    456 		if ((fup = lookup(id, type)) == 0)
    457 			fup = &zerofileusage;
    458 		if (dqbuf.dqb_curinodes == fup->fu_curinodes &&
    459 		    dqbuf.dqb_curblocks == fup->fu_curblocks) {
    460 			fup->fu_curinodes = 0;
    461 			fup->fu_curblocks = 0;
    462 			(void) fseek(qfo, (long)sizeof(struct dqblk), 1);
    463 			continue;
    464 		}
    465 		if (vflag) {
    466 			if (aflag)
    467 				printf("%s: ", fsname);
    468 			printf("%-8s fixed:", fup->fu_name);
    469 			if (dqbuf.dqb_curinodes != fup->fu_curinodes)
    470 				(void)printf("\tinodes %d -> %ld",
    471 					dqbuf.dqb_curinodes, fup->fu_curinodes);
    472 			if (dqbuf.dqb_curblocks != fup->fu_curblocks)
    473 				(void)printf("\tblocks %d -> %ld",
    474 					dqbuf.dqb_curblocks, fup->fu_curblocks);
    475 			(void)printf("\n");
    476 		}
    477 		/*
    478 		 * Reset time limit if have a soft limit and were
    479 		 * previously under it, but are now over it.
    480 		 */
    481 		if (dqbuf.dqb_bsoftlimit &&
    482 		    dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
    483 		    fup->fu_curblocks >= dqbuf.dqb_bsoftlimit)
    484 			dqbuf.dqb_btime = 0;
    485 		if (dqbuf.dqb_isoftlimit &&
    486 		    dqbuf.dqb_curblocks < dqbuf.dqb_isoftlimit &&
    487 		    fup->fu_curblocks >= dqbuf.dqb_isoftlimit)
    488 			dqbuf.dqb_itime = 0;
    489 		dqbuf.dqb_curinodes = fup->fu_curinodes;
    490 		dqbuf.dqb_curblocks = fup->fu_curblocks;
    491 		(void) fwrite((char *)&dqbuf, sizeof(struct dqblk), 1, qfo);
    492 		(void) quotactl(fsname, QCMD(Q_SETUSE, type), id,
    493 		    (caddr_t)&dqbuf);
    494 		fup->fu_curinodes = 0;
    495 		fup->fu_curblocks = 0;
    496 	}
    497 	(void) fclose(qfi);
    498 	(void) fflush(qfo);
    499 	(void) ftruncate(fileno(qfo),
    500 	    (off_t)((highid[type] + 1) * sizeof(struct dqblk)));
    501 	(void) fclose(qfo);
    502 	return (0);
    503 }
    504 
    505 /*
    506  * Check to see if target appears in list of size cnt.
    507  */
    508 static int
    509 oneof(target, list, cnt)
    510 	const char *target;
    511 	char *list[];
    512 	int cnt;
    513 {
    514 	int i;
    515 
    516 	for (i = 0; i < cnt; i++)
    517 		if (strcmp(target, list[i]) == 0)
    518 			return (i);
    519 	return (-1);
    520 }
    521 
    522 /*
    523  * Determine the group identifier for quota files.
    524  */
    525 static int
    526 getquotagid()
    527 {
    528 	struct group *gr;
    529 
    530 	if ((gr = getgrnam(quotagroup)) != NULL)
    531 		return (gr->gr_gid);
    532 	return (-1);
    533 }
    534 
    535 /*
    536  * Check to see if a particular quota is to be enabled.
    537  */
    538 static int
    539 hasquota(fs, type, qfnamep)
    540 	struct fstab *fs;
    541 	int type;
    542 	char **qfnamep;
    543 {
    544 	char *opt;
    545 	char *cp = NULL;
    546 	static char initname, usrname[100], grpname[100];
    547 	static char buf[BUFSIZ];
    548 
    549 	if (!initname) {
    550 		(void)snprintf(usrname, sizeof(usrname),
    551 		    "%s%s", qfextension[USRQUOTA], qfname);
    552 		(void)snprintf(grpname, sizeof(grpname),
    553 		    "%s%s", qfextension[GRPQUOTA], qfname);
    554 		initname = 1;
    555 	}
    556 	(void) strlcpy(buf, fs->fs_mntops, sizeof(buf));
    557 	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
    558 		if ((cp = strchr(opt, '=')) != NULL)
    559 			*cp++ = '\0';
    560 		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
    561 			break;
    562 		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
    563 			break;
    564 	}
    565 	if (!opt)
    566 		return (0);
    567 	if (cp)
    568 		*qfnamep = cp;
    569 	else {
    570 		(void)snprintf(buf, sizeof(buf),
    571 		    "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
    572 		*qfnamep = buf;
    573 	}
    574 	return (1);
    575 }
    576 
    577 /*
    578  * Routines to manage the file usage table.
    579  *
    580  * Lookup an id of a specific type.
    581  */
    582 static struct fileusage *
    583 lookup(id, type)
    584 	u_long id;
    585 	int type;
    586 {
    587 	struct fileusage *fup;
    588 
    589 	for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
    590 		if (fup->fu_id == id)
    591 			return (fup);
    592 	return (NULL);
    593 }
    594 
    595 /*
    596  * Add a new file usage id if it does not already exist.
    597  */
    598 static struct fileusage *
    599 addid(id, type, name)
    600 	u_long id;
    601 	int type;
    602 	const char *name;
    603 {
    604 	struct fileusage *fup, **fhp;
    605 	int len;
    606 
    607 	if ((fup = lookup(id, type)) != NULL)
    608 		return (fup);
    609 	if (name)
    610 		len = strlen(name);
    611 	else
    612 		len = 10;
    613 	if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
    614 		err(1, "%s", strerror(errno));
    615 	fhp = &fuhead[type][id & (FUHASH - 1)];
    616 	fup->fu_next = *fhp;
    617 	*fhp = fup;
    618 	fup->fu_id = id;
    619 	if (id > highid[type])
    620 		highid[type] = id;
    621 	if (name)
    622 		memmove(fup->fu_name, name, len + 1);
    623 	else
    624 		(void)sprintf(fup->fu_name, "%lu", id);
    625 	return (fup);
    626 }
    627 
    628 /*
    629  * Special purpose version of ginode used to optimize first pass
    630  * over all the inodes in numerical order.
    631  */
    632 static ino_t nextino, lastinum, lastvalidinum;
    633 static long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
    634 static union dinode *inodebuf;
    635 #define INOBUFSIZE	56*1024	/* size of buffer to read inodes */
    636 
    637 union dinode *
    638 getnextinode(inumber)
    639 	ino_t inumber;
    640 {
    641 	long size;
    642 	daddr_t dblk;
    643 	static union dinode *dp;
    644 	union dinode *ret;
    645 
    646 	if (inumber != nextino++ || inumber > lastvalidinum) {
    647 		errx(1, "bad inode number %d to nextinode", inumber);
    648 	}
    649 
    650 	if (inumber >= lastinum) {
    651 		readcnt++;
    652 		dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
    653 		if (readcnt % readpercg == 0) {
    654 			size = partialsize;
    655 			lastinum += partialcnt;
    656 		} else {
    657 			size = inobufsize;
    658 			lastinum += fullcnt;
    659 		}
    660 		(void)bread(dblk, (caddr_t)inodebuf, size);
    661 		if (needswap) {
    662 			if (is_ufs2)
    663 				swap_dinode2(inodebuf, lastinum - inumber);
    664 			else
    665 				swap_dinode1(inodebuf, lastinum - inumber);
    666 		}
    667 		dp = (union dinode *)inodebuf;
    668 	}
    669 	ret = dp;
    670 	dp = (union dinode *)
    671 	    ((char *)dp + (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE));
    672 	return ret;
    673 }
    674 
    675 void
    676 setinodebuf(inum)
    677 	ino_t inum;
    678 {
    679 
    680 	if (inum % sblock.fs_ipg != 0)
    681 		errx(1, "bad inode number %d to setinodebuf", inum);
    682 
    683 	lastvalidinum = inum + sblock.fs_ipg - 1;
    684 	nextino = inum;
    685 	lastinum = inum;
    686 	readcnt = 0;
    687 	if (inodebuf != NULL)
    688 		return;
    689 	inobufsize = blkroundup(&sblock, INOBUFSIZE);
    690 	fullcnt = inobufsize / (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE);
    691 	readpercg = sblock.fs_ipg / fullcnt;
    692 	partialcnt = sblock.fs_ipg % fullcnt;
    693 	partialsize = partialcnt * (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE);
    694 	if (partialcnt != 0) {
    695 		readpercg++;
    696 	} else {
    697 		partialcnt = fullcnt;
    698 		partialsize = inobufsize;
    699 	}
    700 	if (inodebuf == NULL &&
    701 	    (inodebuf = malloc((unsigned)inobufsize)) == NULL)
    702 		errx(1, "Cannot allocate space for inode buffer");
    703 	while (nextino < ROOTINO)
    704 		getnextinode(nextino);
    705 }
    706 
    707 void
    708 freeinodebuf()
    709 {
    710 
    711 	if (inodebuf != NULL)
    712 		free((char *)inodebuf);
    713 	inodebuf = NULL;
    714 }
    715 
    716 
    717 static void
    718 swap_dinode1(union dinode *dp, int n)
    719 {
    720 	int i;
    721 	struct ufs1_dinode *dp1;
    722 
    723 	dp1 = (struct ufs1_dinode *)&dp->dp1;
    724 	for (i = 0; i < n; i++, dp1++)
    725 		ffs_dinode1_swap(dp1, dp1);
    726 }
    727 
    728 static void
    729 swap_dinode2(union dinode *dp, int n)
    730 {
    731 	int i;
    732 	struct ufs2_dinode *dp2;
    733 
    734 	dp2 = (struct ufs2_dinode *)&dp->dp2;
    735 	for (i = 0; i < n; i++, dp2++)
    736 		ffs_dinode2_swap(dp2, dp2);
    737 }
    738 
    739 /*
    740  * Read specified disk blocks.
    741  */
    742 static void
    743 bread(bno, buf, cnt)
    744 	daddr_t bno;
    745 	char *buf;
    746 	long cnt;
    747 {
    748 
    749 	if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0 ||
    750 	    read(fi, buf, cnt) != cnt)
    751 		err(1, "block %lld", (long long)bno);
    752 }
    753 
    754 void
    755 infohandler(int sig)
    756 {
    757 	got_siginfo = 1;
    758 }
    759 
    760