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