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