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