xref: /netbsd-src/usr.sbin/quotacheck/quotacheck.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: quotacheck.c,v 1.49 2015/06/16 23:04:14 christos Exp $	*/
2 
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 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\
38  The Regents of the University of California.  All rights reserved.");
39 #endif /* not lint */
40 
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)quotacheck.c	8.6 (Berkeley) 4/28/95";
44 #else
45 __RCSID("$NetBSD: quotacheck.c,v 1.49 2015/06/16 23:04:14 christos Exp $");
46 #endif
47 #endif /* not lint */
48 
49 /*
50  * Fix up / report on disk quotas & usage
51  */
52 #include <sys/param.h>
53 #include <sys/stat.h>
54 #include <sys/queue.h>
55 #include <sys/statvfs.h>
56 
57 #include <ufs/ufs/dinode.h>
58 #include <ufs/ufs/quota1.h>
59 #include <ufs/ufs/ufs_bswap.h>
60 #include <ufs/ffs/fs.h>
61 #include <ufs/ffs/ffs_extern.h>
62 
63 #include <err.h>
64 #include <fcntl.h>
65 #include <fstab.h>
66 #include <pwd.h>
67 #include <grp.h>
68 #include <errno.h>
69 #include <unistd.h>
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <string.h>
73 #include <util.h>
74 
75 #include "fsutil.h"
76 #include "quotautil.h"
77 
78 #ifndef FS_UFS1_MAGIC
79 # define FS_UFS1_MAGIC		FS_MAGIC /* 0x011954 */
80 # define FS_UFS1_MAGIC_SWAPPED	0x54190100 /* bswap32(0x011954) */
81 # define DINODE1_SIZE		sizeof(struct dinode)
82 # define DINODE2_SIZE		0
83 #else
84 # define HAVE_UFSv2	1
85 #endif
86 
87 #ifndef SBLOCKSIZE
88 # define SBLOCKSIZE	SBSIZE
89 #endif
90 #ifndef SBLOCKSEARCH
91 # define SBLOCKSEARCH	{ SBSIZE, -1 }
92 #endif
93 
94 static const char *quotagroup = QUOTAGROUP;
95 
96 static union {
97 	struct	fs	sblk;
98 	char	dummy[MAXBSIZE];
99 } un;
100 #define	sblock	un.sblk
101 static long dev_bsize;
102 static long maxino;
103 
104 struct quotaname {
105 	long	flags;
106 	char	grpqfname[MAXPATHLEN + 1];
107 	char	usrqfname[MAXPATHLEN + 1];
108 };
109 #define	HASUSR	1
110 #define	HASGRP	2
111 
112 struct fileusage {
113 	struct	fileusage *fu_next;
114 	u_long	fu_curinodes;
115 	u_long	fu_curblocks;
116 	uint32_t fu_id;		/* uid_t, gid_t */
117 	char	fu_name[1];
118 	/* actually bigger */
119 };
120 #define FUHASH 1024	/* must be power of two */
121 static struct fileusage *fuhead[MAXQUOTAS][FUHASH];
122 
123 
124 union comb_dinode {
125 #ifdef HAVE_UFSv2
126 	struct ufs1_dinode dp1;
127 	struct ufs2_dinode dp2;
128 #else
129 	struct dinode dp1;
130 #endif
131 };
132 #ifdef HAVE_UFSv2
133 #define DIP(dp, field) \
134 	(is_ufs2 ? (dp)->dp2.di_##field : (dp)->dp1.di_##field)
135 #else
136 #define DIP(dp, field) (dp)->dp1.di_##field
137 #endif
138 
139 
140 static int	aflag;		/* all file systems */
141 static int	gflag;		/* check group quotas */
142 static int	uflag;		/* check user quotas */
143 static int	vflag;		/* verbose */
144 static int	qflag;		/* quick but untidy mode */
145 static int	fi;		/* open disk file descriptor */
146 static uint32_t highid[MAXQUOTAS];/* highest addid()'ed identifier per type */
147 static int needswap;	/* FS is in swapped order */
148 static int got_siginfo = 0; /* got a siginfo signal */
149 static int is_ufs2;
150 
151 
152 static void usage(void) __attribute__((__noreturn__));
153 static void *needchk(struct fstab *);
154 static int chkquota(const char *, const char *, const char *, void *, pid_t *);
155 static int update(const char *, const char *, int);
156 static uint32_t skipforward(uint32_t, uint32_t, FILE *);
157 static int getquotagid(void);
158 static struct fileusage *lookup(uint32_t, int);
159 static struct fileusage *addid(uint32_t, int, const char *);
160 static uint32_t subsequent(uint32_t, int) ;
161 static union comb_dinode *getnextinode(ino_t);
162 static void setinodebuf(ino_t);
163 static void freeinodebuf(void);
164 static void bread(daddr_t, char *, long);
165 static void infohandler(int sig);
166 static void swap_dinode1(union comb_dinode *, int);
167 #ifdef HAVE_UFSv2
168 static void swap_dinode2(union comb_dinode *, int);
169 #endif
170 
171 int
172 main(int argc, char *argv[])
173 {
174 	struct fstab *fs;
175 	struct passwd *pw;
176 	struct group *gr;
177 	struct quotaname *auxdata;
178 	int i, argnum, maxrun, errs;
179 	long done = 0;
180 	int flags = CHECK_PREEN;
181 	const char *name;
182 	int ch;
183 
184 	errs = maxrun = 0;
185 	while ((ch = getopt(argc, argv, "aguvqdl:")) != -1) {
186 		switch(ch) {
187 		case 'a':
188 			aflag++;
189 			break;
190 		case 'd':
191 			flags |= CHECK_DEBUG;
192 			break;
193 		case 'g':
194 			gflag++;
195 			break;
196 		case 'u':
197 			uflag++;
198 			break;
199 		case 'q':
200 			qflag++;
201 			break;
202 		case 'v':
203 			vflag++;
204 			break;
205 		case 'l':
206 			maxrun = atoi(optarg);
207 			break;
208 		default:
209 			usage();
210 		}
211 	}
212 	argc -= optind;
213 	argv += optind;
214 	if ((argc == 0 && !aflag) || (argc > 0 && aflag) || (!aflag && maxrun))
215 		usage();
216 	if (!gflag && !uflag) {
217 		gflag++;
218 		uflag++;
219 	}
220 
221 	/* If -a, we do not want to pay the cost of processing every
222 	 * group and password entry if there are no filesystems with quotas
223 	 */
224 	if (aflag) {
225 		i = 0;
226 		while ((fs = getfsent()) != NULL) {
227 			if (needchk(fs))
228 				i = 1;
229 		}
230 		endfsent();
231 		if (!i)	/* No filesystems with quotas */
232 			return 0;
233 	}
234 
235 	if (gflag) {
236 		setgrent();
237 		while ((gr = getgrent()) != 0)
238 			(void) addid((uint32_t)gr->gr_gid, GRPQUOTA, gr->gr_name);
239 		endgrent();
240 	}
241 	if (uflag) {
242 		setpwent();
243 		while ((pw = getpwent()) != 0)
244 			(void) addid((uint32_t)pw->pw_uid, USRQUOTA, pw->pw_name);
245 		endpwent();
246 	}
247 	if (aflag)
248 		exit(checkfstab(flags, maxrun, needchk, chkquota));
249 	if (setfsent() == 0)
250 		err(1, "%s: can't open", FSTAB);
251 	while ((fs = getfsent()) != NULL) {
252 		const char *fsspec;
253 		char buf[MAXPATHLEN];
254 		fsspec = getfsspecname(buf, sizeof(buf), fs->fs_spec);
255 		if (fsspec == NULL) {
256 			warn("%s", buf);
257 			continue;
258 		}
259 		if (((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
260 		    (argnum = oneof(fsspec, argv, argc)) >= 0) &&
261 		    (auxdata = needchk(fs)) &&
262 		    (name = blockcheck(fsspec))) {
263 			done |= 1 << argnum;
264 			errs += chkquota(fs->fs_type, name, fs->fs_file,
265 			    auxdata, NULL);
266 		}
267 	}
268 	endfsent();
269 	for (i = 0; i < argc; i++)
270 		if ((done & (1 << i)) == 0)
271 			warnx("%s not found in %s", argv[i], FSTAB);
272 	return errs;
273 }
274 
275 static void
276 usage(void)
277 {
278 	const char *p = getprogname();
279 	(void)fprintf(stderr, "Usage: %s -a [-gquv] [-l <maxparallel>]\n"
280 	    "\t%s [-gquv] <filesys> ...\n", p, p);
281 	exit(1);
282 }
283 
284 static void *
285 needchk(struct fstab *fs)
286 {
287 	struct quotaname *qnp;
288 	char qfnp[MAXPATHLEN];
289 
290 	if (strcmp(fs->fs_vfstype, "ffs") ||
291 	    strcmp(fs->fs_type, FSTAB_RW))
292 		return (NULL);
293 	if ((qnp = malloc(sizeof(*qnp))) == NULL)
294 		err(1, "%s", strerror(errno));
295 	qnp->flags = 0;
296 	if (gflag && hasquota(qfnp, sizeof(qfnp), fs, GRPQUOTA)) {
297 		strlcpy(qnp->grpqfname, qfnp, sizeof(qnp->grpqfname));
298 		qnp->flags |= HASGRP;
299 	}
300 	if (uflag && hasquota(qfnp, sizeof(qfnp), fs, USRQUOTA)) {
301 		strlcpy(qnp->usrqfname, qfnp, sizeof(qnp->usrqfname));
302 		qnp->flags |= HASUSR;
303 	}
304 	if (qnp->flags)
305 		return (qnp);
306 	free(qnp);
307 	return (NULL);
308 }
309 
310 static off_t sblock_try[] = SBLOCKSEARCH;
311 
312 /*
313  * Scan the specified filesystem to check quota(s) present on it.
314  */
315 static int
316 chkquota(const char *type, const char *fsname, const char *mntpt, void *v,
317     pid_t *pid)
318 {
319 	struct quotaname *qnp = v;
320 	struct fileusage *fup;
321 	union comb_dinode *dp;
322 	int cg, i, mode, errs = 0, inosused;
323 	ino_t ino;
324 	struct cg *cgp;
325 	char msgbuf[4096];
326 
327 	if (pid != NULL) {
328 		fflush(stdout);
329 		switch ((*pid = fork())) {
330 		default:
331 			return 0;
332 		case 0:
333 			break;
334 		case -1:
335 			err(1, "Cannot fork");
336 		}
337 		setvbuf(stdout, msgbuf, _IOFBF, sizeof msgbuf);
338 	}
339 
340 	if ((fi = open(fsname, O_RDONLY, 0)) < 0) {
341 		warn("Cannot open %s", fsname);
342 		if (pid != NULL)
343 			exit(1);
344 		return 1;
345 	}
346 	if (vflag) {
347 		(void)printf("*** Checking ");
348 		if (qnp->flags & HASUSR)
349 			(void)printf("%s%s", qfextension[USRQUOTA],
350 			    (qnp->flags & HASGRP) ? " and " : "");
351 		if (qnp->flags & HASGRP)
352 			(void)printf("%s", qfextension[GRPQUOTA]);
353 		(void)printf(" quotas for %s (%s)\n", fsname, mntpt);
354 		fflush(stdout);
355 	}
356 	signal(SIGINFO, infohandler);
357 	sync();
358 	dev_bsize = 1;
359 
360 	for (i = 0; ; i++) {
361 		if (sblock_try[i] == -1) {
362 			warnx("%s: superblock not found", fsname);
363 			if (pid != NULL)
364 				exit(1);
365 			return 1;
366 		}
367 		bread(sblock_try[i], (char *)&sblock, SBLOCKSIZE);
368 		switch (sblock.fs_magic) {
369 #ifdef HAVE_UFSv2
370 		case FS_UFS2_MAGIC:
371 			is_ufs2 = 1;
372 			/*FALLTHROUGH*/
373 #endif
374 		case FS_UFS1_MAGIC:
375 			break;
376 #ifdef HAVE_UFSv2
377 		case FS_UFS2_MAGIC_SWAPPED:
378 			is_ufs2 = 1;
379 			/*FALLTHROUGH*/
380 #endif
381 		case FS_UFS1_MAGIC_SWAPPED:
382 			needswap = 1;
383 			ffs_sb_swap(&sblock, &sblock);
384 			break;
385 		default:
386 			continue;
387 		}
388 
389 #ifdef HAVE_UFSv2
390 		if (is_ufs2 || sblock.fs_old_flags & FS_FLAGS_UPDATED) {
391 			if (sblock.fs_sblockloc != sblock_try[i])
392 				continue;
393 		} else {
394 			if (sblock_try[i] == SBLOCK_UFS2)
395 				continue;
396 		}
397 #endif
398 		break;
399 	}
400 
401 	cgp = malloc(sblock.fs_cgsize);
402 	if (cgp == NULL) {
403 		warn("%s: can't allocate %d bytes of cg space", fsname,
404 		    sblock.fs_cgsize);
405 		if (pid != NULL)
406 			exit(1);
407 		return 1;
408 	}
409 
410 	dev_bsize = sblock.fs_fsize / FFS_FSBTODB(&sblock, 1);
411 	maxino = sblock.fs_ncg * sblock.fs_ipg;
412 	for (cg = 0; cg < sblock.fs_ncg; cg++) {
413 		ino = cg * sblock.fs_ipg;
414 		setinodebuf(ino);
415 #ifdef HAVE_UFSv2
416 		if (sblock.fs_magic == FS_UFS2_MAGIC) {
417 			bread(FFS_FSBTODB(&sblock, cgtod(&sblock, cg)), (char *)cgp,
418 			    sblock.fs_cgsize);
419 			if (needswap)
420 				ffs_cg_swap(cgp, cgp, &sblock);
421 			inosused = cgp->cg_initediblk;
422 		} else
423 #endif
424 			inosused = sblock.fs_ipg;
425 		for (i = 0; i < inosused; i++, ino++) {
426 			if (got_siginfo) {
427 				fprintf(stderr,
428 				    "%s: cyl group %d of %d (%d%%)\n",
429 				    fsname, cg, sblock.fs_ncg,
430 				    cg * 100 / sblock.fs_ncg);
431 				got_siginfo = 0;
432 			}
433 			if (ino < UFS_ROOTINO)
434 				continue;
435 			if ((dp = getnextinode(ino)) == NULL)
436 				continue;
437 			if ((mode = DIP(dp, mode) & IFMT) == 0)
438 				continue;
439 			if (qnp->flags & HASGRP) {
440 				fup = addid(DIP(dp, gid), GRPQUOTA,
441 				    (char *)0);
442 				fup->fu_curinodes++;
443 				if (mode == IFREG || mode == IFDIR ||
444 				    mode == IFLNK)
445 					fup->fu_curblocks += DIP(dp, blocks);
446 			}
447 			if (qnp->flags & HASUSR) {
448 				fup = addid(DIP(dp, uid), USRQUOTA,
449 				    (char *)0);
450 				fup->fu_curinodes++;
451 				if (mode == IFREG || mode == IFDIR ||
452 				    mode == IFLNK)
453 					fup->fu_curblocks += DIP(dp, blocks);
454 			}
455 		}
456 	}
457 	freeinodebuf();
458 	free(cgp);
459 	if (qnp->flags & HASUSR)
460 		errs += update(mntpt, qnp->usrqfname, USRQUOTA);
461 	if (qnp->flags & HASGRP)
462 		errs += update(mntpt, qnp->grpqfname, GRPQUOTA);
463 	close(fi);
464 	if (pid != NULL)
465 		exit(errs);
466 	return errs;
467 }
468 
469 /*
470  * Update a specified quota file.
471  */
472 static int
473 update(const char *fsname, const char *quotafile, int type)
474 {
475 	struct fileusage *fup;
476 	FILE *qfi, *qfo;
477 	uint32_t id, lastid, nextid;
478 	int need_seek;
479 	struct dqblk dqbuf;
480 	static struct dqblk zerodqbuf;
481 	static struct fileusage zerofileusage;
482 	struct statvfs *fst;
483 	int nfst, i;
484 
485 	nfst = getmntinfo(&fst, MNT_WAIT);
486 	if (nfst == 0)
487 		errx(1, "no filesystems mounted!");
488 
489 	for (i = 0; i < nfst; i++) {
490 		if (strncmp(fst[i].f_fstypename, "ffs",
491 		    sizeof(fst[i].f_fstypename)) == 0 &&
492 		    strncmp(fst[i].f_mntonname, fsname,
493 		    sizeof(fst[i].f_mntonname)) == 0 &&
494 		    (fst[i].f_flag & ST_QUOTA) != 0) {
495 			warnx("filesystem %s has quotas already turned on",
496 			    fsname);
497 		}
498 	}
499 
500 	if ((qfo = fopen(quotafile, "r+")) == NULL) {
501 		if (errno == ENOENT)
502 			qfo = fopen(quotafile, "w+");
503 		if (qfo) {
504 			(void) fprintf(stderr,
505 			    "quotacheck: creating quota file %s\n", quotafile);
506 #define	MODE	(S_IRUSR|S_IWUSR|S_IRGRP)
507 			(void) fchown(fileno(qfo), getuid(), getquotagid());
508 			(void) fchmod(fileno(qfo), MODE);
509 		} else {
510 			(void) fprintf(stderr,
511 			    "quotacheck: %s: %s\n", quotafile, strerror(errno));
512 			return (1);
513 		}
514 	}
515 	if ((qfi = fopen(quotafile, "r")) == NULL) {
516 		(void) fprintf(stderr,
517 		    "quotacheck: %s: %s\n", quotafile, strerror(errno));
518 		(void) fclose(qfo);
519 		return (1);
520 	}
521 	need_seek = 1;
522 	for (lastid = highid[type], id = 0; id <= lastid; id = nextid) {
523 		if (fread((char *)&dqbuf, sizeof(struct dqblk), 1, qfi) == 0)
524 			dqbuf = zerodqbuf;
525 		if ((fup = lookup(id, type)) == 0)
526 			fup = &zerofileusage;
527 
528 		nextid = subsequent(id, type);
529 		/* watch out for id == UINT32_MAX */
530 		if (nextid > 0 && nextid != id + 1)
531 			nextid = skipforward(id, nextid, qfi);
532 
533 		if (got_siginfo) {
534 			/*
535 			 * XXX this could try to show percentage through
536 			 * the ID list
537 			 */
538 			fprintf(stderr, "%s: updating %s quotas for id=%"
539 			    PRIu32 " (%s)\n", fsname,
540 			    qfextension[type < MAXQUOTAS ? type : MAXQUOTAS],
541 			    id, fup->fu_name);
542 			got_siginfo = 0;
543 		}
544 		if (dqbuf.dqb_curinodes == fup->fu_curinodes &&
545 		    dqbuf.dqb_curblocks == fup->fu_curblocks) {
546 			fup->fu_curinodes = 0;	/* reset usage  */
547 			fup->fu_curblocks = 0;	/* for next filesystem */
548 
549 			need_seek = 1;
550 			/* infinite loop avoidance (OR do as "nextid < id"?) */
551 			if (id == UINT32_MAX || nextid == 0) {
552 				break;
553 			}
554 			continue;
555 		}
556 		if (vflag) {
557 			if (aflag)
558 				printf("%s: ", fsname);
559 			printf("%-8s fixed:", fup->fu_name);
560 			if (dqbuf.dqb_curinodes != fup->fu_curinodes)
561 				(void)printf("\tinodes %d -> %ld",
562 					dqbuf.dqb_curinodes, fup->fu_curinodes);
563 			if (dqbuf.dqb_curblocks != fup->fu_curblocks)
564 				(void)printf("\tblocks %d -> %ld",
565 					dqbuf.dqb_curblocks, fup->fu_curblocks);
566 			(void)printf("\n");
567 		}
568 		/*
569 		 * Reset time limit if have a soft limit and were
570 		 * previously under it, but are now over it.
571 		 */
572 		if (dqbuf.dqb_bsoftlimit &&
573 		    dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
574 		    fup->fu_curblocks >= dqbuf.dqb_bsoftlimit)
575 			dqbuf.dqb_btime = 0;
576 		if (dqbuf.dqb_isoftlimit &&
577 		    dqbuf.dqb_curinodes < dqbuf.dqb_isoftlimit &&
578 		    fup->fu_curinodes >= dqbuf.dqb_isoftlimit)
579 			dqbuf.dqb_itime = 0;
580 		dqbuf.dqb_curinodes = fup->fu_curinodes;
581 		dqbuf.dqb_curblocks = fup->fu_curblocks;
582 
583 		if (need_seek) {
584 			(void) fseeko(qfo, (off_t)id * sizeof(struct dqblk),
585 			    SEEK_SET);
586 			need_seek = nextid != id + 1;
587 		}
588 		(void) fwrite(&dqbuf, sizeof(struct dqblk), 1, qfo);
589 
590 		fup->fu_curinodes = 0;
591 		fup->fu_curblocks = 0;
592 		/* infinite loop avoidance (OR do as "nextid < id"?) */
593 		if (id == UINT32_MAX || nextid == 0) {
594 			break;
595 		}
596 	}
597 	(void)fclose(qfi);
598 	(void)fflush(qfo);
599 	if (highid[type] != UINT32_MAX)
600 		(void)ftruncate(fileno(qfo),
601 		    (off_t)((highid[type] + 1) * sizeof(struct dqblk)));
602 	(void)fclose(qfo);
603 	return 0;
604 }
605 
606 static uint32_t
607 skipforward(uint32_t cur, uint32_t to, FILE *qfi)
608 {
609 	struct dqblk dqbuf;
610 
611 	if (qflag) {
612 		(void)fseeko(qfi, (off_t)to * sizeof(struct dqblk), SEEK_SET);
613 		return to;
614 	}
615 
616 	while (++cur < to) {
617 		/*
618 		 * if EOF occurs, nothing left to read, we're done
619 		 */
620 		if (fread((char *)&dqbuf, sizeof(struct dqblk), 1, qfi) == 0)
621 			return (to);
622 
623 		/*
624 		 * If we find an entry that shows usage, before the next
625 		 * id that has actual usage, we have to stop here, so the
626 		 * incorrect entry can be corrected in the file
627 		 */
628 		if (dqbuf.dqb_curinodes != 0 || dqbuf.dqb_curblocks != 0) {
629 			(void)fseek(qfi, -(long)sizeof(struct dqblk), SEEK_CUR);
630 			return cur;
631 		}
632 	}
633 	return to;
634 }
635 
636 /*
637  * Determine the group identifier for quota files.
638  */
639 static int
640 getquotagid(void)
641 {
642 	struct group *gr;
643 
644 	if ((gr = getgrnam(quotagroup)) != NULL)
645 		return gr->gr_gid;
646 	return -1;
647 }
648 
649 /*
650  * Routines to manage the file usage table.
651  *
652  * Lookup an id of a specific type.
653  */
654 static struct fileusage *
655 lookup(uint32_t id, int type)
656 {
657 	struct fileusage *fup;
658 
659 	for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
660 		if (fup->fu_id == id)
661 			return fup;
662 	return NULL;
663 }
664 
665 /*
666  * Add a new file usage id if it does not already exist.
667  */
668 static struct fileusage *
669 addid(uint32_t id, int type, const char *name)
670 {
671 	struct fileusage *fup, **fhp;
672 	size_t len;
673 
674 	if ((fup = lookup(id, type)) != NULL)
675 		return fup;
676 	if (name)
677 		len = strlen(name);
678 	else
679 		len = 10;
680 	if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
681 		err(1, "%s", strerror(errno));
682 	fhp = &fuhead[type][id & (FUHASH - 1)];
683 	fup->fu_next = *fhp;
684 	*fhp = fup;
685 	fup->fu_id = id;
686 	if (id > highid[type])
687 		highid[type] = id;
688 	if (name)
689 		memmove(fup->fu_name, name, len + 1);
690 	else
691 		(void)snprintf(fup->fu_name, len + 1, "%" PRIu32, id);
692 	return fup;
693 }
694 
695 static uint32_t
696 subsequent(uint32_t id, int type)
697 {
698 	struct fileusage *fup, **iup, **cup;
699 	uint32_t next, offset;
700 
701 	next = highid[type] + 1;
702 	offset = 0;
703 	cup = iup = &fuhead[type][id & (FUHASH-1)];
704 	do {
705 		++offset;
706 		if (++cup >= &fuhead[type][FUHASH])
707 			cup = &fuhead[type][0];
708 		for (fup = *cup; fup != 0; fup = fup->fu_next) {
709 			if (fup->fu_id > id && fup->fu_id <= id + offset)
710 				return (fup->fu_id);
711 			if (fup->fu_id > id && fup->fu_id < next)
712 				next = fup->fu_id;
713 		}
714 	} while (cup != iup);
715 
716 	return next;
717 }
718 
719 /*
720  * Special purpose version of ginode used to optimize first pass
721  * over all the inodes in numerical order.
722  */
723 static ino_t nextino, lastinum, lastvalidinum;
724 static long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
725 static union comb_dinode *inodebuf;
726 #define INOBUFSIZE	56*1024	/* size of buffer to read inodes */
727 
728 static union comb_dinode *
729 getnextinode(ino_t inumber)
730 {
731 	long size;
732 	daddr_t dblk;
733 	static union comb_dinode *dp;
734 	union comb_dinode *ret;
735 
736 	if (inumber != nextino++ || inumber > lastvalidinum) {
737 		errx(1, "bad inode number %llu to nextinode",
738 		    (unsigned long long)inumber);
739 	}
740 
741 	if (inumber >= lastinum) {
742 		readcnt++;
743 		dblk = FFS_FSBTODB(&sblock, ino_to_fsba(&sblock, lastinum));
744 		if (readcnt % readpercg == 0) {
745 			size = partialsize;
746 			lastinum += partialcnt;
747 		} else {
748 			size = inobufsize;
749 			lastinum += fullcnt;
750 		}
751 		(void)bread(dblk, (caddr_t)inodebuf, size);
752 		if (needswap) {
753 #ifdef HAVE_UFSv2
754 			if (is_ufs2)
755 				swap_dinode2(inodebuf, lastinum - inumber);
756 			else
757 #endif
758 				swap_dinode1(inodebuf, lastinum - inumber);
759 		}
760 		dp = (union comb_dinode *)inodebuf;
761 	}
762 	ret = dp;
763 	dp = (union comb_dinode *)
764 	    ((char *)dp + (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE));
765 	return ret;
766 }
767 
768 static void
769 setinodebuf(ino_t inum)
770 {
771 
772 	if (inum % sblock.fs_ipg != 0)
773 		errx(1, "bad inode number %llu to setinodebuf",
774 		    (unsigned long long)inum);
775 
776 	lastvalidinum = inum + sblock.fs_ipg - 1;
777 	nextino = inum;
778 	lastinum = inum;
779 	readcnt = 0;
780 	if (inodebuf != NULL)
781 		return;
782 	inobufsize = ffs_blkroundup(&sblock, INOBUFSIZE);
783 	fullcnt = inobufsize / (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE);
784 	readpercg = sblock.fs_ipg / fullcnt;
785 	partialcnt = sblock.fs_ipg % fullcnt;
786 	partialsize = partialcnt * (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE);
787 	if (partialcnt != 0) {
788 		readpercg++;
789 	} else {
790 		partialcnt = fullcnt;
791 		partialsize = inobufsize;
792 	}
793 	if (inodebuf == NULL &&
794 	    (inodebuf = malloc((unsigned)inobufsize)) == NULL)
795 		errx(1, "Cannot allocate space for inode buffer");
796 	while (nextino < UFS_ROOTINO)
797 		getnextinode(nextino);
798 }
799 
800 static void
801 freeinodebuf(void)
802 {
803 
804 	free(inodebuf);
805 	inodebuf = NULL;
806 }
807 
808 
809 #ifdef HAVE_UFSv2
810 static void
811 swap_dinode1(union comb_dinode *dp, int n)
812 {
813 	int i;
814 	struct ufs1_dinode *dp1;
815 
816 	dp1 = (struct ufs1_dinode *)&dp->dp1;
817 	for (i = 0; i < n; i++, dp1++)
818 		ffs_dinode1_swap(dp1, dp1);
819 }
820 
821 static void
822 swap_dinode2(union comb_dinode *dp, int n)
823 {
824 	int i;
825 	struct ufs2_dinode *dp2;
826 
827 	dp2 = (struct ufs2_dinode *)&dp->dp2;
828 	for (i = 0; i < n; i++, dp2++)
829 		ffs_dinode2_swap(dp2, dp2);
830 }
831 
832 #else
833 
834 static void
835 swap_dinode1(union comb_dinode *dp, int n)
836 {
837 	int i;
838 	struct dinode *dp1;
839 
840 	dp1 = (struct dinode *) &dp->dp1;
841 	for (i = 0; i < n; i++, dp1++)
842 		ffs_dinode_swap(dp1, dp1);
843 }
844 
845 #endif
846 
847 /*
848  * Read specified disk blocks.
849  */
850 static void
851 bread(daddr_t bno, char *buf, long cnt)
852 {
853 
854 	if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0 ||
855 	    read(fi, buf, cnt) != cnt)
856 		err(1, "block %lld", (long long)bno);
857 }
858 
859 static void
860 infohandler(int sig)
861 {
862 	got_siginfo = 1;
863 }
864