xref: /freebsd/sbin/quotacheck/quotacheck.c (revision 5e3190f700637fcfc1a52daeaa4a031fdd2557c7)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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 #if 0
36 #ifndef lint
37 static const char copyright[] =
38 "@(#) Copyright (c) 1980, 1990, 1993\n\
39 	The Regents of the University of California.  All rights reserved.\n";
40 #endif /* not lint */
41 
42 #ifndef lint
43 static char sccsid[] = "@(#)quotacheck.c	8.3 (Berkeley) 1/29/94";
44 #endif /* not lint */
45 #endif
46 #include <sys/cdefs.h>
47 /*
48  * Fix up / report on disk quotas & usage
49  */
50 #include <sys/param.h>
51 #include <sys/disklabel.h>
52 #include <sys/mount.h>
53 #include <sys/stat.h>
54 
55 #include <ufs/ufs/dinode.h>
56 #include <ufs/ufs/quota.h>
57 #include <ufs/ffs/fs.h>
58 
59 #include <err.h>
60 #include <errno.h>
61 #include <fcntl.h>
62 #include <fstab.h>
63 #include <grp.h>
64 #include <libufs.h>
65 #include <libutil.h>
66 #include <pwd.h>
67 #include <stdint.h>
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <unistd.h>
72 
73 #include "quotacheck.h"
74 
75 const char *qfname = QUOTAFILENAME;
76 const char *qfextension[] = INITQFNAMES;
77 const char *quotagroup = QUOTAGROUP;
78 
79 union {
80 	struct	fs	sblk;
81 	char	dummy[MAXBSIZE];
82 } sb_un;
83 #define	sblock	sb_un.sblk
84 union {
85 	struct	cg	cgblk;
86 	char	dummy[MAXBSIZE];
87 } cg_un;
88 #define	cgblk	cg_un.cgblk
89 long dev_bsize = 1;
90 ino_t maxino;
91 
92 union dinode {
93 	struct ufs1_dinode dp1;
94 	struct ufs2_dinode dp2;
95 };
96 #define	DIP(dp, field) \
97 	((sblock.fs_magic == FS_UFS1_MAGIC) ? \
98 	(dp)->dp1.field : (dp)->dp2.field)
99 
100 #define	HASUSR	1
101 #define	HASGRP	2
102 
103 struct fileusage {
104 	struct	fileusage *fu_next;
105 	u_long	fu_curinodes;
106 	u_long	fu_curblocks;
107 	u_long	fu_id;
108 	char	fu_name[1];
109 	/* actually bigger */
110 };
111 #define FUHASH 1024	/* must be power of two */
112 struct fileusage *fuhead[MAXQUOTAS][FUHASH];
113 
114 int	aflag;			/* all file systems */
115 int	cflag;			/* convert format to 32 or 64 bit size */
116 int	gflag;			/* check group quotas */
117 int	uflag;			/* check user quotas */
118 int	vflag;			/* verbose */
119 int	fi;			/* open disk file descriptor */
120 
121 struct fileusage *
122 	 addid(u_long, int, char *, const char *);
123 void	 blkread(ufs2_daddr_t, char *, long);
124 void	 freeinodebuf(void);
125 union dinode *
126 	 getnextinode(ino_t);
127 int	 getquotagid(void);
128 struct fileusage *
129 	 lookup(u_long, int);
130 int	 oneof(char *, char*[], int);
131 void	 printchanges(const char *, int, struct dqblk *, struct fileusage *,
132 	    u_long);
133 void	 setinodebuf(ino_t);
134 int	 update(const char *, struct quotafile *, int);
135 void	 usage(void);
136 
137 int
138 main(int argc, char *argv[])
139 {
140 	struct fstab *fs;
141 	struct passwd *pw;
142 	struct group *gr;
143 	struct quotafile *qfu, *qfg;
144 	int i, argnum, maxrun, errs, ch;
145 	long done = 0;
146 	char *name;
147 
148 	errs = maxrun = 0;
149 	while ((ch = getopt(argc, argv, "ac:guvl:")) != -1) {
150 		switch(ch) {
151 		case 'a':
152 			aflag++;
153 			break;
154 		case 'c':
155 			if (cflag)
156 				usage();
157 			cflag = atoi(optarg);
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 (cflag && cflag != 32 && cflag != 64)
180 		usage();
181 	if (!gflag && !uflag) {
182 		gflag++;
183 		uflag++;
184 	}
185 	if (gflag) {
186 		setgrent();
187 		while ((gr = getgrent()) != NULL)
188 			(void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name,
189 			    NULL);
190 		endgrent();
191 	}
192 	if (uflag) {
193 		setpwent();
194 		while ((pw = getpwent()) != NULL)
195 			(void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name,
196 			    NULL);
197 		endpwent();
198 	}
199 	/*
200 	 * The maxrun (-l) option is now deprecated.
201 	 */
202 	if (maxrun > 0)
203 		warnx("the -l option is now deprecated");
204 	if (aflag)
205 		exit(checkfstab(uflag, gflag));
206 	if (setfsent() == 0)
207 		errx(1, "%s: can't open", FSTAB);
208 	while ((fs = getfsent()) != NULL) {
209 		if (((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
210 		     (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) &&
211 		    (name = blockcheck(fs->fs_spec))) {
212 			done |= 1 << argnum;
213 			qfu = NULL;
214 			if (uflag)
215 				qfu = quota_open(fs, USRQUOTA, O_CREAT|O_RDWR);
216 			qfg = NULL;
217 			if (gflag)
218 				qfg = quota_open(fs, GRPQUOTA, O_CREAT|O_RDWR);
219 			if (qfu == NULL && qfg == NULL)
220 				continue;
221 			errs += chkquota(name, qfu, qfg);
222 			if (qfu)
223 				quota_close(qfu);
224 			if (qfg)
225 				quota_close(qfg);
226 		}
227 	}
228 	endfsent();
229 	for (i = 0; i < argc; i++)
230 		if ((done & (1 << i)) == 0)
231 			fprintf(stderr, "%s not found in %s\n",
232 				argv[i], FSTAB);
233 	exit(errs);
234 }
235 
236 void
237 usage(void)
238 {
239 	(void)fprintf(stderr, "%s\n%s\n",
240 		"usage: quotacheck [-guv] [-c 32 | 64] [-l maxrun] -a",
241 		"       quotacheck [-guv] [-c 32 | 64] filesystem ...");
242 	exit(1);
243 }
244 
245 /*
246  * Scan the specified file system to check quota(s) present on it.
247  */
248 int
249 chkquota(char *specname, struct quotafile *qfu, struct quotafile *qfg)
250 {
251 	struct fileusage *fup;
252 	union dinode *dp;
253 	struct fs *fs;
254 	int i, ret, mode, errs = 0;
255 	u_int32_t cg;
256 	ino_t curino, ino, inosused, userino = 0, groupino = 0;
257 	dev_t dev, userdev = 0, groupdev = 0;
258 	struct stat sb;
259 	const char *mntpt;
260 	char *cp;
261 
262 	if (qfu != NULL)
263 		mntpt = quota_fsname(qfu);
264 	else if (qfg != NULL)
265 		mntpt = quota_fsname(qfg);
266 	else
267 		errx(1, "null quotafile information passed to chkquota()\n");
268 	if (cflag) {
269 		if (vflag && qfu != NULL)
270 			printf("%s: convert user quota to %d bits\n",
271 			    mntpt, cflag);
272 		if (qfu != NULL && quota_convert(qfu, cflag) < 0) {
273 			if (errno == EBADF)
274 				errx(1,
275 				    "%s: cannot convert an active quota file",
276 				    mntpt);
277 			err(1, "user quota conversion to size %d failed",
278 			    cflag);
279 		}
280 		if (vflag && qfg != NULL)
281 			printf("%s: convert group quota to %d bits\n",
282 			    mntpt, cflag);
283 		if (qfg != NULL && quota_convert(qfg, cflag) < 0) {
284 			if (errno == EBADF)
285 				errx(1,
286 				    "%s: cannot convert an active quota file",
287 				    mntpt);
288 			err(1, "group quota conversion to size %d failed",
289 			    cflag);
290 		}
291 	}
292 	if ((fi = open(specname, O_RDONLY, 0)) < 0) {
293 		warn("%s", specname);
294 		return (1);
295 	}
296 	if ((stat(mntpt, &sb)) < 0) {
297 		warn("%s", mntpt);
298 		return (1);
299 	}
300 	dev = sb.st_dev;
301 	if (vflag) {
302 		(void)printf("*** Checking ");
303 		if (qfu)
304 			(void)printf("user%s", qfg ? " and " : "");
305 		if (qfg)
306 			(void)printf("group");
307 		(void)printf(" quotas for %s (%s)\n", specname, mntpt);
308 	}
309 	if (qfu) {
310 		if (stat(quota_qfname(qfu), &sb) == 0) {
311 			userino = sb.st_ino;
312 			userdev = sb.st_dev;
313 		}
314 	}
315 	if (qfg) {
316 		if (stat(quota_qfname(qfg), &sb) == 0) {
317 			groupino = sb.st_ino;
318 			groupdev = sb.st_dev;
319 		}
320 	}
321 	sync();
322 	if ((ret = sbget(fi, &fs, UFS_STDSB, UFS_NOCSUM)) != 0) {
323 		switch (ret) {
324 		case ENOENT:
325 			warn("Cannot find file system superblock");
326 			return (1);
327 		default:
328 			warn("Unable to read file system superblock");
329 			return (1);
330 		}
331 	}
332 	bcopy(fs, &sblock, fs->fs_sbsize);
333 	free(fs);
334 	dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
335 	maxino = sblock.fs_ncg * sblock.fs_ipg;
336 	for (cg = 0; cg < sblock.fs_ncg; cg++) {
337 		ino = cg * sblock.fs_ipg;
338 		setinodebuf(ino);
339 		blkread(fsbtodb(&sblock, cgtod(&sblock, cg)), (char *)(&cgblk),
340 		    sblock.fs_cgsize);
341 		if (sblock.fs_magic == FS_UFS2_MAGIC)
342 			inosused = cgblk.cg_initediblk;
343 		else
344 			inosused = sblock.fs_ipg;
345 		/*
346 		 * If we are using soft updates, then we can trust the
347 		 * cylinder group inode allocation maps to tell us which
348 		 * inodes are allocated. We will scan the used inode map
349 		 * to find the inodes that are really in use, and then
350 		 * read only those inodes in from disk.
351 		 */
352 		if (sblock.fs_flags & FS_DOSOFTDEP) {
353 			if (!cg_chkmagic(&cgblk))
354 				errx(1, "CG %d: BAD MAGIC NUMBER\n", cg);
355 			cp = &cg_inosused(&cgblk)[(inosused - 1) / CHAR_BIT];
356 			for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) {
357 				if (*cp == 0)
358 					continue;
359 				for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
360 					if (*cp & i)
361 						break;
362 					inosused--;
363 				}
364 				break;
365 			}
366 			if (inosused <= 0)
367 				continue;
368 		}
369 		for (curino = 0; curino < inosused; curino++, ino++) {
370 			if ((dp = getnextinode(ino)) == NULL ||
371 			    ino < UFS_ROOTINO ||
372 			    (mode = DIP(dp, di_mode) & IFMT) == 0)
373 				continue;
374 			/*
375 			 * XXX: Do not account for UIDs or GIDs that appear
376 			 * to be negative to prevent generating 100GB+
377 			 * quota files.
378 			 */
379 			if ((int)DIP(dp, di_uid) < 0 ||
380 			    (int)DIP(dp, di_gid) < 0) {
381 				if (vflag) {
382 					if (aflag)
383 						(void)printf("%s: ", mntpt);
384 			(void)printf("out of range UID/GID (%u/%u) ino=%ju\n",
385 					    DIP(dp, di_uid), DIP(dp,di_gid),
386 					    (uintmax_t)ino);
387 				}
388 				continue;
389 			}
390 
391 			/*
392 			 * Do not account for file system snapshot files
393 			 * or the actual quota data files to be consistent
394 			 * with how they are handled inside the kernel.
395 			 */
396 #ifdef	SF_SNAPSHOT
397 			if (DIP(dp, di_flags) & SF_SNAPSHOT)
398 				continue;
399 #endif
400 			if ((ino == userino && dev == userdev) ||
401 			    (ino == groupino && dev == groupdev))
402 				continue;
403 			if (qfg) {
404 				fup = addid((u_long)DIP(dp, di_gid), GRPQUOTA,
405 				    NULL, mntpt);
406 				fup->fu_curinodes++;
407 				if (mode == IFREG || mode == IFDIR ||
408 				    mode == IFLNK)
409 					fup->fu_curblocks += DIP(dp, di_blocks);
410 			}
411 			if (qfu) {
412 				fup = addid((u_long)DIP(dp, di_uid), USRQUOTA,
413 				    NULL, mntpt);
414 				fup->fu_curinodes++;
415 				if (mode == IFREG || mode == IFDIR ||
416 				    mode == IFLNK)
417 					fup->fu_curblocks += DIP(dp, di_blocks);
418 			}
419 		}
420 	}
421 	freeinodebuf();
422 	if (qfu)
423 		errs += update(mntpt, qfu, USRQUOTA);
424 	if (qfg)
425 		errs += update(mntpt, qfg, GRPQUOTA);
426 	close(fi);
427 	(void)fflush(stdout);
428 	return (errs);
429 }
430 
431 /*
432  * Update a specified quota file.
433  */
434 int
435 update(const char *fsname, struct quotafile *qf, int type)
436 {
437 	struct fileusage *fup;
438 	u_long id, lastid, highid = 0;
439 	struct dqblk dqbuf;
440 	struct stat sb;
441 	static struct dqblk zerodqbuf;
442 	static struct fileusage zerofileusage;
443 
444 	/*
445 	 * Scan the on-disk quota file and record any usage changes.
446 	 */
447 	lastid = quota_maxid(qf);
448 	for (id = 0; id <= lastid; id++) {
449 		if (quota_read(qf, &dqbuf, id) < 0)
450 			dqbuf = zerodqbuf;
451 		if ((fup = lookup(id, type)) == NULL)
452 			fup = &zerofileusage;
453 		if (fup->fu_curinodes || fup->fu_curblocks ||
454 		    dqbuf.dqb_bsoftlimit || dqbuf.dqb_bhardlimit ||
455 		    dqbuf.dqb_isoftlimit || dqbuf.dqb_ihardlimit)
456 			highid = id;
457 		if (dqbuf.dqb_curinodes == fup->fu_curinodes &&
458 		    dqbuf.dqb_curblocks == fup->fu_curblocks) {
459 			fup->fu_curinodes = 0;
460 			fup->fu_curblocks = 0;
461 			continue;
462 		}
463 		printchanges(fsname, type, &dqbuf, fup, id);
464 		dqbuf.dqb_curinodes = fup->fu_curinodes;
465 		dqbuf.dqb_curblocks = fup->fu_curblocks;
466 		(void) quota_write_usage(qf, &dqbuf, id);
467 		fup->fu_curinodes = 0;
468 		fup->fu_curblocks = 0;
469 	}
470 
471 	/*
472 	 * Walk the hash table looking for ids with non-zero usage
473 	 * that are not currently recorded in the quota file. E.g.
474 	 * ids that are past the end of the current file.
475 	 */
476 	for (id = 0; id < FUHASH; id++) {
477 		for (fup = fuhead[type][id]; fup != NULL; fup = fup->fu_next) {
478 			if (fup->fu_id <= lastid)
479 				continue;
480 			if (fup->fu_curinodes == 0 && fup->fu_curblocks == 0)
481 				continue;
482 			bzero(&dqbuf, sizeof(struct dqblk));
483 			if (fup->fu_id > highid)
484 				highid = fup->fu_id;
485 			printchanges(fsname, type, &dqbuf, fup, fup->fu_id);
486 			dqbuf.dqb_curinodes = fup->fu_curinodes;
487 			dqbuf.dqb_curblocks = fup->fu_curblocks;
488 			(void) quota_write_usage(qf, &dqbuf, fup->fu_id);
489 			fup->fu_curinodes = 0;
490 			fup->fu_curblocks = 0;
491 		}
492 	}
493 	/*
494 	 * If this is old format file, then size may be smaller,
495 	 * so ensure that we only truncate when it will make things
496 	 * smaller, and not if it will grow an old format file.
497 	 */
498 	if (highid < lastid &&
499 	    stat(quota_qfname(qf), &sb) == 0 &&
500 	    sb.st_size > (off_t)((highid + 2) * sizeof(struct dqblk)))
501 		truncate(quota_qfname(qf),
502 		    (((off_t)highid + 2) * sizeof(struct dqblk)));
503 	return (0);
504 }
505 
506 /*
507  * Check to see if target appears in list of size cnt.
508  */
509 int
510 oneof(char *target, char *list[], int cnt)
511 {
512 	int i;
513 
514 	for (i = 0; i < cnt; i++)
515 		if (strcmp(target, list[i]) == 0)
516 			return (i);
517 	return (-1);
518 }
519 
520 /*
521  * Determine the group identifier for quota files.
522  */
523 int
524 getquotagid(void)
525 {
526 	struct group *gr;
527 
528 	if ((gr = getgrnam(quotagroup)) != NULL)
529 		return (gr->gr_gid);
530 	return (-1);
531 }
532 
533 /*
534  * Routines to manage the file usage table.
535  *
536  * Lookup an id of a specific type.
537  */
538 struct fileusage *
539 lookup(u_long id, int type)
540 {
541 	struct fileusage *fup;
542 
543 	for (fup = fuhead[type][id & (FUHASH-1)]; fup != NULL; fup = fup->fu_next)
544 		if (fup->fu_id == id)
545 			return (fup);
546 	return (NULL);
547 }
548 
549 /*
550  * Add a new file usage id if it does not already exist.
551  */
552 struct fileusage *
553 addid(u_long id, int type, char *name, const char *fsname)
554 {
555 	struct fileusage *fup, **fhp;
556 	int len;
557 
558 	if ((fup = lookup(id, type)) != NULL)
559 		return (fup);
560 	if (name)
561 		len = strlen(name);
562 	else
563 		len = 0;
564 	if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
565 		errx(1, "calloc failed");
566 	fhp = &fuhead[type][id & (FUHASH - 1)];
567 	fup->fu_next = *fhp;
568 	*fhp = fup;
569 	fup->fu_id = id;
570 	if (name)
571 		bcopy(name, fup->fu_name, len + 1);
572 	else {
573 		(void)sprintf(fup->fu_name, "%lu", id);
574 		if (vflag) {
575 			if (aflag && fsname != NULL)
576 				(void)printf("%s: ", fsname);
577 			printf("unknown %cid: %lu\n",
578 			    type == USRQUOTA ? 'u' : 'g', id);
579 		}
580 	}
581 	return (fup);
582 }
583 
584 /*
585  * Special purpose version of ginode used to optimize pass
586  * over all the inodes in numerical order.
587  */
588 static ino_t nextino, lastinum, lastvalidinum;
589 static long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
590 static caddr_t inodebuf;
591 #define INOBUFSIZE	56*1024		/* size of buffer to read inodes */
592 
593 union dinode *
594 getnextinode(ino_t inumber)
595 {
596 	long size;
597 	ufs2_daddr_t dblk;
598 	union dinode *dp;
599 	static caddr_t nextinop;
600 
601 	if (inumber != nextino++ || inumber > lastvalidinum)
602 		errx(1, "bad inode number %ju to nextinode",
603 		    (uintmax_t)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 		/*
615 		 * If blkread returns an error, it will already have zeroed
616 		 * out the buffer, so we do not need to do so here.
617 		 */
618 		blkread(dblk, inodebuf, size);
619 		nextinop = inodebuf;
620 	}
621 	dp = (union dinode *)nextinop;
622 	if (sblock.fs_magic == FS_UFS1_MAGIC)
623 		nextinop += sizeof(struct ufs1_dinode);
624 	else
625 		nextinop += sizeof(struct ufs2_dinode);
626 	return (dp);
627 }
628 
629 /*
630  * Prepare to scan a set of inodes.
631  */
632 void
633 setinodebuf(ino_t inum)
634 {
635 
636 	if (inum % sblock.fs_ipg != 0)
637 		errx(1, "bad inode number %ju to setinodebuf", (uintmax_t)inum);
638 	lastvalidinum = inum + sblock.fs_ipg - 1;
639 	nextino = inum;
640 	lastinum = inum;
641 	readcnt = 0;
642 	if (inodebuf != NULL)
643 		return;
644 	inobufsize = blkroundup(&sblock, INOBUFSIZE);
645 	fullcnt = inobufsize / ((sblock.fs_magic == FS_UFS1_MAGIC) ?
646 	    sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
647 	readpercg = sblock.fs_ipg / fullcnt;
648 	partialcnt = sblock.fs_ipg % fullcnt;
649 	partialsize = partialcnt * ((sblock.fs_magic == FS_UFS1_MAGIC) ?
650 	    sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
651 	if (partialcnt != 0) {
652 		readpercg++;
653 	} else {
654 		partialcnt = fullcnt;
655 		partialsize = inobufsize;
656 	}
657 	if ((inodebuf = malloc((unsigned)inobufsize)) == NULL)
658 		errx(1, "cannot allocate space for inode buffer");
659 }
660 
661 /*
662  * Free up data structures used to scan inodes.
663  */
664 void
665 freeinodebuf(void)
666 {
667 
668 	if (inodebuf != NULL)
669 		free(inodebuf);
670 	inodebuf = NULL;
671 }
672 
673 /*
674  * Read specified disk blocks.
675  */
676 void
677 blkread(ufs2_daddr_t bno, char *buf, long cnt)
678 {
679 
680 	if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0 ||
681 	    read(fi, buf, cnt) != cnt)
682 		errx(1, "blkread failed on block %ld", (long)bno);
683 }
684 
685 /*
686  * Display updated block and i-node counts.
687  */
688 void
689 printchanges(const char *fsname, int type, struct dqblk *dp,
690     struct fileusage *fup, u_long id)
691 {
692 	if (!vflag)
693 		return;
694 	if (aflag)
695 		(void)printf("%s: ", fsname);
696 	if (fup->fu_name[0] == '\0')
697 		(void)printf("%-8lu fixed ", id);
698 	else
699 		(void)printf("%-8s fixed ", fup->fu_name);
700 	switch (type) {
701 
702 	case GRPQUOTA:
703 		(void)printf("(group):");
704 		break;
705 
706 	case USRQUOTA:
707 		(void)printf("(user): ");
708 		break;
709 
710 	default:
711 		(void)printf("(unknown quota type %d)", type);
712 		break;
713 	}
714 	if (dp->dqb_curinodes != fup->fu_curinodes)
715 		(void)printf("\tinodes %lu -> %lu", (u_long)dp->dqb_curinodes,
716 		    (u_long)fup->fu_curinodes);
717 	if (dp->dqb_curblocks != fup->fu_curblocks)
718 		(void)printf("\tblocks %lu -> %lu",
719 		    (u_long)dp->dqb_curblocks,
720 		    (u_long)fup->fu_curblocks);
721 	(void)printf("\n");
722 }
723