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