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