xref: /freebsd/sbin/quotacheck/quotacheck.c (revision 35a04710d7286aa9538917fd7f8e417dbee95b82)
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  * 4. 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 <pwd.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <unistd.h>
69 
70 char *qfname = QUOTAFILENAME;
71 char *qfextension[] = INITQFNAMES;
72 char *quotagroup = QUOTAGROUP;
73 
74 union {
75 	struct	fs	sblk;
76 	char	dummy[MAXBSIZE];
77 } sb_un;
78 #define	sblock	sb_un.sblk
79 union {
80 	struct	cg	cgblk;
81 	char	dummy[MAXBSIZE];
82 } cg_un;
83 #define	cgblk	cg_un.cgblk
84 long dev_bsize = 1;
85 ino_t maxino;
86 
87 union dinode {
88 	struct ufs1_dinode dp1;
89 	struct ufs2_dinode dp2;
90 };
91 #define	DIP(dp, field) \
92 	((sblock.fs_magic == FS_UFS1_MAGIC) ? \
93 	(dp)->dp1.field : (dp)->dp2.field)
94 
95 struct quotaname {
96 	long	flags;
97 	char	grpqfname[PATH_MAX];
98 	char	usrqfname[PATH_MAX];
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	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 *, char *);
122 char	*blockcheck(char *);
123 void	 bread(ufs2_daddr_t, char *, long);
124 extern int checkfstab(int, int, void * (*)(struct fstab *),
125 				int (*)(char *, char *, struct quotaname *));
126 int	 chkquota(char *, char *, struct quotaname *);
127 void	 freeinodebuf(void);
128 union dinode *
129 	 getnextinode(ino_t);
130 int	 getquotagid(void);
131 int	 hasquota(struct fstab *, int, char **);
132 struct fileusage *
133 	 lookup(u_long, int);
134 void	*needchk(struct fstab *);
135 int	 oneof(char *, char*[], int);
136 void	 printchanges(char *, int, struct dqblk *, struct fileusage *, u_long);
137 void	 setinodebuf(ino_t);
138 int	 update(char *, char *, int);
139 void	 usage(void);
140 
141 int
142 main(argc, argv)
143 	int argc;
144 	char *argv[];
145 {
146 	struct fstab *fs;
147 	struct passwd *pw;
148 	struct group *gr;
149 	struct quotaname *auxdata;
150 	int i, argnum, maxrun, errs, ch;
151 	long done = 0;
152 	char *name;
153 
154 	errs = maxrun = 0;
155 	while ((ch = getopt(argc, argv, "aguvl:")) != -1) {
156 		switch(ch) {
157 		case 'a':
158 			aflag++;
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 (!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 	 * Setting maxrun (-l) makes no sense without the -a flag.
200 	 * Historically this was never an error, so we just warn.
201 	 */
202 	if (maxrun > 0 && !aflag)
203 		warnx("ignoring -l without -a");
204 	if (aflag)
205 		exit(checkfstab(1, maxrun, needchk, chkquota));
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 		    (auxdata = needchk(fs)) &&
212 		    (name = blockcheck(fs->fs_spec))) {
213 			done |= 1 << argnum;
214 			errs += chkquota(name, fs->fs_file, auxdata);
215 		}
216 	}
217 	endfsent();
218 	for (i = 0; i < argc; i++)
219 		if ((done & (1 << i)) == 0)
220 			fprintf(stderr, "%s not found in %s\n",
221 				argv[i], FSTAB);
222 	exit(errs);
223 }
224 
225 void
226 usage()
227 {
228 	(void)fprintf(stderr, "%s\n%s\n",
229 		"usage: quotacheck [-guv] [-l maxrun] -a",
230 		"       quotacheck [-guv] filesystem ...");
231 	exit(1);
232 }
233 
234 void *
235 needchk(fs)
236 	struct fstab *fs;
237 {
238 	struct quotaname *qnp;
239 	char *qfnp;
240 
241 	if (strcmp(fs->fs_vfstype, "ufs") ||
242 	    strcmp(fs->fs_type, FSTAB_RW))
243 		return (NULL);
244 	if ((qnp = malloc(sizeof(*qnp))) == NULL)
245 		errx(1, "malloc failed");
246 	qnp->flags = 0;
247 	if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) {
248 		strcpy(qnp->grpqfname, qfnp);
249 		qnp->flags |= HASGRP;
250 	}
251 	if (uflag && hasquota(fs, USRQUOTA, &qfnp)) {
252 		strcpy(qnp->usrqfname, qfnp);
253 		qnp->flags |= HASUSR;
254 	}
255 	if (qnp->flags)
256 		return (qnp);
257 	free(qnp);
258 	return (NULL);
259 }
260 
261 /*
262  * Possible superblock locations ordered from most to least likely.
263  */
264 static int sblock_try[] = SBLOCKSEARCH;
265 
266 /*
267  * Scan the specified file system to check quota(s) present on it.
268  */
269 int
270 chkquota(fsname, mntpt, qnp)
271 	char *fsname, *mntpt;
272 	struct quotaname *qnp;
273 {
274 	struct fileusage *fup;
275 	union dinode *dp;
276 	int cg, i, mode, errs = 0;
277 	ino_t ino, inosused, userino = 0, groupino = 0;
278 	char *cp;
279 	struct stat sb;
280 
281 	if ((fi = open(fsname, O_RDONLY, 0)) < 0) {
282 		warn("%s", fsname);
283 		return (1);
284 	}
285 	if (vflag) {
286 		(void)printf("*** Checking ");
287 		if (qnp->flags & HASUSR)
288 			(void)printf("%s%s", qfextension[USRQUOTA],
289 			    (qnp->flags & HASGRP) ? " and " : "");
290 		if (qnp->flags & HASGRP)
291 			(void)printf("%s", qfextension[GRPQUOTA]);
292 		(void)printf(" quotas for %s (%s)\n", fsname, mntpt);
293 	}
294 	if (qnp->flags & HASUSR) {
295 		if (stat(qnp->usrqfname, &sb) == 0)
296 			userino = sb.st_ino;
297 	}
298 	if (qnp->flags & HASGRP) {
299 		if (stat(qnp->grpqfname, &sb) == 0)
300 			groupino = sb.st_ino;
301 	}
302 	sync();
303 	dev_bsize = 1;
304 	for (i = 0; sblock_try[i] != -1; i++) {
305 		bread(sblock_try[i], (char *)&sblock, (long)SBLOCKSIZE);
306 		if ((sblock.fs_magic == FS_UFS1_MAGIC ||
307 		     (sblock.fs_magic == FS_UFS2_MAGIC &&
308 		      sblock.fs_sblockloc == sblock_try[i])) &&
309 		    sblock.fs_bsize <= MAXBSIZE &&
310 		    sblock.fs_bsize >= sizeof(struct fs))
311 			break;
312 	}
313 	if (sblock_try[i] == -1) {
314 		warn("Cannot find file system superblock");
315 		return (1);
316 	}
317 	dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
318 	maxino = sblock.fs_ncg * sblock.fs_ipg;
319 	for (cg = 0; cg < sblock.fs_ncg; cg++) {
320 		ino = cg * sblock.fs_ipg;
321 		setinodebuf(ino);
322 		bread(fsbtodb(&sblock, cgtod(&sblock, cg)), (char *)(&cgblk),
323 		    sblock.fs_cgsize);
324 		if (sblock.fs_magic == FS_UFS2_MAGIC)
325 			inosused = cgblk.cg_initediblk;
326 		else
327 			inosused = sblock.fs_ipg;
328 		/*
329 		 * If we are using soft updates, then we can trust the
330 		 * cylinder group inode allocation maps to tell us which
331 		 * inodes are allocated. We will scan the used inode map
332 		 * to find the inodes that are really in use, and then
333 		 * read only those inodes in from disk.
334 		 */
335 		if (sblock.fs_flags & FS_DOSOFTDEP) {
336 			if (!cg_chkmagic(&cgblk))
337 				errx(1, "CG %d: BAD MAGIC NUMBER\n", cg);
338 			cp = &cg_inosused(&cgblk)[(inosused - 1) / CHAR_BIT];
339 			for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) {
340 				if (*cp == 0)
341 					continue;
342 				for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
343 					if (*cp & i)
344 						break;
345 					inosused--;
346 				}
347 				break;
348 			}
349 			if (inosused <= 0)
350 				continue;
351 		}
352 		for (i = 0; i < inosused; i++, ino++) {
353 			if ((dp = getnextinode(ino)) == NULL || ino < ROOTINO ||
354 			    (mode = DIP(dp, di_mode) & IFMT) == 0)
355 				continue;
356 			/*
357 			 * XXX: Do not account for UIDs or GIDs that appear
358 			 * to be negative to prevent generating 100GB+
359 			 * quota files.
360 			 */
361 			if ((int)DIP(dp, di_uid) < 0 ||
362 			    (int)DIP(dp, di_gid) < 0) {
363 				if (vflag) {
364 					if (aflag)
365 						(void)printf("%s: ", mntpt);
366 			(void)printf("out of range UID/GID (%u/%u) ino=%u\n",
367 					    DIP(dp, di_uid), DIP(dp,di_gid),
368 					    ino);
369 				}
370 				continue;
371 			}
372 
373 			/*
374 			 * Do not account for file system snapshot files
375 			 * or the actual quota data files to be consistent
376 			 * with how they are handled inside the kernel.
377 			 */
378 #ifdef	SF_SNAPSHOT
379 			if (DIP(dp, di_flags) & SF_SNAPSHOT)
380 				continue;
381 #endif
382 			if (ino == userino || ino == groupino)
383 				continue;
384 			if (qnp->flags & HASGRP) {
385 				fup = addid((u_long)DIP(dp, di_gid), GRPQUOTA,
386 				    (char *)0, mntpt);
387 				fup->fu_curinodes++;
388 				if (mode == IFREG || mode == IFDIR ||
389 				    mode == IFLNK)
390 					fup->fu_curblocks += DIP(dp, di_blocks);
391 			}
392 			if (qnp->flags & HASUSR) {
393 				fup = addid((u_long)DIP(dp, di_uid), USRQUOTA,
394 				    (char *)0, mntpt);
395 				fup->fu_curinodes++;
396 				if (mode == IFREG || mode == IFDIR ||
397 				    mode == IFLNK)
398 					fup->fu_curblocks += DIP(dp, di_blocks);
399 			}
400 		}
401 	}
402 	freeinodebuf();
403 	if (qnp->flags & HASUSR)
404 		errs += update(mntpt, qnp->usrqfname, USRQUOTA);
405 	if (qnp->flags & HASGRP)
406 		errs += update(mntpt, qnp->grpqfname, GRPQUOTA);
407 	close(fi);
408 	(void)fflush(stdout);
409 	return (errs);
410 }
411 
412 /*
413  * Update a specified quota file.
414  */
415 int
416 update(fsname, quotafile, type)
417 	char *fsname, *quotafile;
418 	int type;
419 {
420 	struct fileusage *fup;
421 	FILE *qfi, *qfo;
422 	u_long id, lastid, highid = 0;
423 	off_t offset;
424 	int i;
425 	struct dqblk dqbuf;
426 	struct stat sb;
427 	static int warned = 0;
428 	static struct dqblk zerodqbuf;
429 	static struct fileusage zerofileusage;
430 
431 	if ((qfo = fopen(quotafile, "r+")) == NULL) {
432 		if (errno == ENOENT)
433 			qfo = fopen(quotafile, "w+");
434 		if (qfo) {
435 			warnx("creating quota file %s", quotafile);
436 #define	MODE	(S_IRUSR|S_IWUSR|S_IRGRP)
437 			(void) fchown(fileno(qfo), getuid(), getquotagid());
438 			(void) fchmod(fileno(qfo), MODE);
439 		} else {
440 			warn("%s", quotafile);
441 			return (1);
442 		}
443 	}
444 	if ((qfi = fopen(quotafile, "r")) == NULL) {
445 		warn("%s", quotafile);
446 		(void) fclose(qfo);
447 		return (1);
448 	}
449 	if (quotactl(fsname, QCMD(Q_SYNC, type), (u_long)0, (caddr_t)0) < 0 &&
450 	    errno == EOPNOTSUPP && !warned && vflag) {
451 		warned++;
452 		(void)printf("*** Warning: %s\n",
453 		    "Quotas are not compiled into this kernel");
454 	}
455 	if (fstat(fileno(qfi), &sb) < 0) {
456 		warn("Cannot fstat quota file %s\n", quotafile);
457 		(void) fclose(qfo);
458 		(void) fclose(qfi);
459 		return (1);
460 	}
461 	if ((sb.st_size % sizeof(struct dqblk)) != 0)
462 		warn("%s size is not a multiple of dqblk\n", quotafile);
463 
464 	/*
465 	 * Scan the on-disk quota file and record any usage changes.
466 	 */
467 
468 	if (sb.st_size != 0)
469 		lastid = (sb.st_size / sizeof(struct dqblk)) - 1;
470 	else
471 		lastid = 0;
472 	for (id = 0, offset = 0; id <= lastid;
473 	    id++, offset += sizeof(struct dqblk)) {
474 		if (fread((char *)&dqbuf, sizeof(struct dqblk), 1, qfi) == 0)
475 			dqbuf = zerodqbuf;
476 		if ((fup = lookup(id, type)) == NULL)
477 			fup = &zerofileusage;
478 		if (fup->fu_curinodes || fup->fu_curblocks ||
479 		    dqbuf.dqb_bsoftlimit || dqbuf.dqb_bhardlimit ||
480 		    dqbuf.dqb_isoftlimit || dqbuf.dqb_ihardlimit)
481 			highid = id;
482 		if (dqbuf.dqb_curinodes == fup->fu_curinodes &&
483 		    dqbuf.dqb_curblocks == fup->fu_curblocks) {
484 			fup->fu_curinodes = 0;
485 			fup->fu_curblocks = 0;
486 			continue;
487 		}
488 		printchanges(fsname, type, &dqbuf, fup, id);
489 		/*
490 		 * Reset time limit if have a soft limit and were
491 		 * previously under it, but are now over it.
492 		 */
493 		if (dqbuf.dqb_bsoftlimit && id != 0 &&
494 		    dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
495 		    fup->fu_curblocks >= dqbuf.dqb_bsoftlimit)
496 			dqbuf.dqb_btime = 0;
497 		if (dqbuf.dqb_isoftlimit && id != 0 &&
498 		    dqbuf.dqb_curinodes < dqbuf.dqb_isoftlimit &&
499 		    fup->fu_curinodes >= dqbuf.dqb_isoftlimit)
500 			dqbuf.dqb_itime = 0;
501 		dqbuf.dqb_curinodes = fup->fu_curinodes;
502 		dqbuf.dqb_curblocks = fup->fu_curblocks;
503 		if (fseeko(qfo, offset, SEEK_SET) < 0) {
504 			warn("%s: seek failed", quotafile);
505 			return(1);
506 		}
507 		fwrite((char *)&dqbuf, sizeof(struct dqblk), 1, qfo);
508 		(void) quotactl(fsname, QCMD(Q_SETUSE, type), id,
509 		    (caddr_t)&dqbuf);
510 		fup->fu_curinodes = 0;
511 		fup->fu_curblocks = 0;
512 	}
513 
514 	/*
515 	 * Walk the hash table looking for ids with non-zero usage
516 	 * that are not currently recorded in the quota file. E.g.
517 	 * ids that are past the end of the current file.
518 	 */
519 
520 	for (i = 0; i < FUHASH; i++) {
521 		for (fup = fuhead[type][i]; fup != NULL; fup = fup->fu_next) {
522 			if (fup->fu_id <= lastid)
523 				continue;
524 			if (fup->fu_curinodes == 0 && fup->fu_curblocks == 0)
525 				continue;
526 			bzero(&dqbuf, sizeof(struct dqblk));
527 			if (fup->fu_id > highid)
528 				highid = fup->fu_id;
529 			printchanges(fsname, type, &dqbuf, fup, id);
530 			dqbuf.dqb_curinodes = fup->fu_curinodes;
531 			dqbuf.dqb_curblocks = fup->fu_curblocks;
532 			offset = (off_t)fup->fu_id * sizeof(struct dqblk);
533 			if (fseeko(qfo, offset, SEEK_SET) < 0) {
534 				warn("%s: seek failed", quotafile);
535 				return(1);
536 			}
537 			fwrite((char *)&dqbuf, sizeof(struct dqblk), 1, qfo);
538 			(void) quotactl(fsname, QCMD(Q_SETUSE, type), id,
539 		    	    (caddr_t)&dqbuf);
540 			fup->fu_curinodes = 0;
541 			fup->fu_curblocks = 0;
542 		}
543 	}
544 	fclose(qfi);
545 	fflush(qfo);
546 	ftruncate(fileno(qfo),
547 	    (((off_t)highid + 1) * sizeof(struct dqblk)));
548 	fclose(qfo);
549 	return (0);
550 }
551 
552 /*
553  * Check to see if target appears in list of size cnt.
554  */
555 int
556 oneof(target, list, cnt)
557 	char *target, *list[];
558 	int cnt;
559 {
560 	int i;
561 
562 	for (i = 0; i < cnt; i++)
563 		if (strcmp(target, list[i]) == 0)
564 			return (i);
565 	return (-1);
566 }
567 
568 /*
569  * Determine the group identifier for quota files.
570  */
571 int
572 getquotagid()
573 {
574 	struct group *gr;
575 
576 	if ((gr = getgrnam(quotagroup)) != NULL)
577 		return (gr->gr_gid);
578 	return (-1);
579 }
580 
581 /*
582  * Check to see if a particular quota is to be enabled.
583  */
584 int
585 hasquota(fs, type, qfnamep)
586 	struct fstab *fs;
587 	int type;
588 	char **qfnamep;
589 {
590 	char *opt;
591 	char *cp;
592 	struct statfs sfb;
593 	static char initname, usrname[100], grpname[100];
594 	static char buf[BUFSIZ];
595 
596 	if (!initname) {
597 		(void)snprintf(usrname, sizeof(usrname), "%s%s",
598 		    qfextension[USRQUOTA], qfname);
599 		(void)snprintf(grpname, sizeof(grpname), "%s%s",
600 		    qfextension[GRPQUOTA], qfname);
601 		initname = 1;
602 	}
603 	strcpy(buf, fs->fs_mntops);
604 	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
605 		if ((cp = index(opt, '=')) != NULL)
606 			*cp++ = '\0';
607 		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
608 			break;
609 		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
610 			break;
611 	}
612 	if (!opt)
613 		return (0);
614 	if (cp)
615 		*qfnamep = cp;
616 	else {
617 		(void)snprintf(buf, sizeof(buf), "%s/%s.%s", fs->fs_file,
618 		    qfname, qfextension[type]);
619 		*qfnamep = buf;
620 	}
621 	if (statfs(fs->fs_file, &sfb) != 0) {
622 		warn("cannot statfs mount point %s", fs->fs_file);
623 		return (0);
624 	}
625 	if (strcmp(fs->fs_file, sfb.f_mntonname)) {
626 		warnx("%s not mounted for %s quotas", fs->fs_file,
627 		    type == USRQUOTA ? "user" : "group");
628 		return (0);
629 	}
630 	return (1);
631 }
632 
633 /*
634  * Routines to manage the file usage table.
635  *
636  * Lookup an id of a specific type.
637  */
638 struct fileusage *
639 lookup(id, type)
640 	u_long id;
641 	int type;
642 {
643 	struct fileusage *fup;
644 
645 	for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
646 		if (fup->fu_id == id)
647 			return (fup);
648 	return (NULL);
649 }
650 
651 /*
652  * Add a new file usage id if it does not already exist.
653  */
654 struct fileusage *
655 addid(id, type, name, fsname)
656 	u_long id;
657 	int type;
658 	char *name;
659 	char *fsname;
660 {
661 	struct fileusage *fup, **fhp;
662 	int len;
663 
664 	if ((fup = lookup(id, type)) != NULL)
665 		return (fup);
666 	if (name)
667 		len = strlen(name);
668 	else
669 		len = 0;
670 	if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
671 		errx(1, "calloc failed");
672 	fhp = &fuhead[type][id & (FUHASH - 1)];
673 	fup->fu_next = *fhp;
674 	*fhp = fup;
675 	fup->fu_id = id;
676 	if (name)
677 		bcopy(name, fup->fu_name, len + 1);
678 	else {
679 		(void)sprintf(fup->fu_name, "%lu", id);
680 		if (vflag) {
681 			if (aflag && fsname != NULL)
682 				(void)printf("%s: ", fsname);
683 			printf("unknown %cid: %lu\n",
684 			    type == USRQUOTA ? 'u' : 'g', id);
685 		}
686 	}
687 	return (fup);
688 }
689 
690 /*
691  * Special purpose version of ginode used to optimize pass
692  * over all the inodes in numerical order.
693  */
694 static ino_t nextino, lastinum, lastvalidinum;
695 static long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
696 static caddr_t inodebuf;
697 #define INOBUFSIZE	56*1024		/* size of buffer to read inodes */
698 
699 union dinode *
700 getnextinode(ino_t inumber)
701 {
702 	long size;
703 	ufs2_daddr_t dblk;
704 	union dinode *dp;
705 	static caddr_t nextinop;
706 
707 	if (inumber != nextino++ || inumber > lastvalidinum)
708 		errx(1, "bad inode number %d to nextinode", inumber);
709 	if (inumber >= lastinum) {
710 		readcnt++;
711 		dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
712 		if (readcnt % readpercg == 0) {
713 			size = partialsize;
714 			lastinum += partialcnt;
715 		} else {
716 			size = inobufsize;
717 			lastinum += fullcnt;
718 		}
719 		/*
720 		 * If bread returns an error, it will already have zeroed
721 		 * out the buffer, so we do not need to do so here.
722 		 */
723 		bread(dblk, inodebuf, size);
724 		nextinop = inodebuf;
725 	}
726 	dp = (union dinode *)nextinop;
727 	if (sblock.fs_magic == FS_UFS1_MAGIC)
728 		nextinop += sizeof(struct ufs1_dinode);
729 	else
730 		nextinop += sizeof(struct ufs2_dinode);
731 	return (dp);
732 }
733 
734 /*
735  * Prepare to scan a set of inodes.
736  */
737 void
738 setinodebuf(ino_t inum)
739 {
740 
741 	if (inum % sblock.fs_ipg != 0)
742 		errx(1, "bad inode number %d to setinodebuf", inum);
743 	lastvalidinum = inum + sblock.fs_ipg - 1;
744 	nextino = inum;
745 	lastinum = inum;
746 	readcnt = 0;
747 	if (inodebuf != NULL)
748 		return;
749 	inobufsize = blkroundup(&sblock, INOBUFSIZE);
750 	fullcnt = inobufsize / ((sblock.fs_magic == FS_UFS1_MAGIC) ?
751 	    sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
752 	readpercg = sblock.fs_ipg / fullcnt;
753 	partialcnt = sblock.fs_ipg % fullcnt;
754 	partialsize = partialcnt * ((sblock.fs_magic == FS_UFS1_MAGIC) ?
755 	    sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
756 	if (partialcnt != 0) {
757 		readpercg++;
758 	} else {
759 		partialcnt = fullcnt;
760 		partialsize = inobufsize;
761 	}
762 	if ((inodebuf = malloc((unsigned)inobufsize)) == NULL)
763 		errx(1, "cannot allocate space for inode buffer");
764 }
765 
766 /*
767  * Free up data structures used to scan inodes.
768  */
769 void
770 freeinodebuf()
771 {
772 
773 	if (inodebuf != NULL)
774 		free(inodebuf);
775 	inodebuf = NULL;
776 }
777 
778 /*
779  * Read specified disk blocks.
780  */
781 void
782 bread(bno, buf, cnt)
783 	ufs2_daddr_t bno;
784 	char *buf;
785 	long cnt;
786 {
787 
788 	if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0 ||
789 	    read(fi, buf, cnt) != cnt)
790 		errx(1, "bread failed on block %ld", (long)bno);
791 }
792 
793 /*
794  * Display updated block and i-node counts.
795  */
796 void
797 printchanges(fsname, type, dp, fup, id)
798 	char *fsname;
799 	int type;
800 	struct dqblk *dp;
801 	struct fileusage *fup;
802 	u_long id;
803 {
804 	if (!vflag)
805 		return;
806 	if (aflag)
807 		(void)printf("%s: ", fsname);
808 	if (fup->fu_name[0] == '\0')
809 		(void)printf("%-8lu fixed ", id);
810 	else
811 		(void)printf("%-8s fixed ", fup->fu_name);
812 	switch (type) {
813 
814 	case GRPQUOTA:
815 		(void)printf("(group):");
816 		break;
817 
818 	case USRQUOTA:
819 		(void)printf("(user): ");
820 		break;
821 
822 	default:
823 		(void)printf("(unknown quota type %d)", type);
824 		break;
825 	}
826 	if (dp->dqb_curinodes != fup->fu_curinodes)
827 		(void)printf("\tinodes %lu -> %lu", (u_long)dp->dqb_curinodes,
828 		    (u_long)fup->fu_curinodes);
829 	if (dp->dqb_curblocks != fup->fu_curblocks)
830 		(void)printf("\tblocks %lu -> %lu",
831 		    (u_long)dp->dqb_curblocks,
832 		    (u_long)fup->fu_curblocks);
833 	(void)printf("\n");
834 }
835