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