xref: /openbsd/sbin/quotacheck/quotacheck.c (revision 7e932812)
1 /*	$OpenBSD: quotacheck.c,v 1.29 2012/05/31 13:55:54 krw Exp $	*/
2 /*	$NetBSD: quotacheck.c,v 1.12 1996/03/30 22:34:25 mark Exp $	*/
3 
4 /*
5  * Copyright (c) 1980, 1990, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Robert Elz at The University of Melbourne.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 /*
37  * Fix up / report on disk quotas & usage
38  */
39 #include <sys/param.h>
40 #include <sys/stat.h>
41 #include <sys/wait.h>
42 
43 #include <ufs/ufs/dinode.h>
44 #include <ufs/ufs/quota.h>
45 #include <ufs/ffs/fs.h>
46 
47 #include <fcntl.h>
48 #include <fstab.h>
49 #include <pwd.h>
50 #include <grp.h>
51 #include <errno.h>
52 #include <unistd.h>
53 #include <util.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <err.h>
58 #include "fsutil.h"
59 
60 char *qfname = QUOTAFILENAME;
61 char *qfextension[] = INITQFNAMES;
62 char *quotagroup = QUOTAGROUP;
63 
64 union {
65 	struct	fs	sblk;
66 	char	dummy[MAXBSIZE];
67 } sb_un;
68 #define	sblock	sb_un.sblk
69 union {
70 	struct	cg	cgblk;
71 	char	dummy[MAXBSIZE];
72 } cg_un;
73 #define	cgblk	cg_un.cgblk
74 
75 long dev_bsize;
76 long maxino;
77 
78 union dinode {
79 	struct ufs1_dinode dp1;
80 	struct ufs2_dinode dp2;
81 };
82 #define	DIP(dp, field) \
83 	((sblock.fs_magic == FS_UFS1_MAGIC) ? \
84 	(dp)->dp1.field : (dp)->dp2.field)
85 
86 struct quotaname {
87 	long	flags;
88 	char	grpqfname[MAXPATHLEN + 1];
89 	char	usrqfname[MAXPATHLEN + 1];
90 };
91 #define	HASUSR	1
92 #define	HASGRP	2
93 
94 struct fileusage {
95 	struct fileusage *fu_next;
96 	u_int32_t	fu_curinodes;
97 	u_int32_t	fu_curblocks;
98 	u_int32_t	fu_id;	/* uid_t or gid_t */
99 	char		fu_name[1];
100 	/* actually bigger */
101 };
102 #define FUHASH 1024	/* must be power of two */
103 struct fileusage *fuhead[MAXQUOTAS][FUHASH];
104 
105 int	gflag;			/* check group quotas */
106 int	uflag;			/* check user quotas */
107 int	flags;			/* check flags (avd) */
108 int	fi;			/* open disk file descriptor */
109 u_int32_t highid[MAXQUOTAS];	/* highest addid()'ed identifier per type */
110 
111 struct fileusage *
112 	 addid(u_int32_t, int, char *);
113 char	*blockcheck(char *);
114 void	 bread(daddr64_t, char *, long);
115 int	 chkquota(const char *, const char *, const char *, void *, pid_t *);
116 void	 freeinodebuf(void);
117 union dinode *
118 	 getnextinode(ino_t);
119 int	 getquotagid(void);
120 int	 hasquota(struct fstab *, int, char **);
121 struct fileusage *
122 	 lookup(u_int32_t, int);
123 void	*needchk(struct fstab *);
124 int	 oneof_realpath(char *, char*[], int);
125 int	 oneof_specname(char *, char*[], int);
126 void	 setinodebuf(ino_t);
127 int	 update(const char *, const char *, int);
128 void	 usage(void);
129 
130 int
131 main(int argc, char *argv[])
132 {
133 	struct fstab *fs;
134 	struct passwd *pw;
135 	struct group *gr;
136 	struct quotaname *auxdata;
137 	int i, argnum, maxrun, errs, ch;
138 	u_int64_t done = 0;	/* XXX supports maximum 64 filesystems */
139 	char *name;
140 
141 	errs = maxrun = 0;
142 	while ((ch = getopt(argc, argv, "adguvl:")) != -1) {
143 		switch(ch) {
144 		case 'a':
145 			flags |= CHECK_PREEN;
146 			break;
147 		case 'd':
148 			flags |= CHECK_DEBUG;
149 			break;
150 		case 'g':
151 			gflag++;
152 			break;
153 		case 'l':
154 			maxrun = atoi(optarg);
155 			break;
156 		case 'u':
157 			uflag++;
158 			break;
159 		case 'v':
160 			flags |= CHECK_VERBOSE;
161 			break;
162 		default:
163 			usage();
164 		}
165 	}
166 	argc -= optind;
167 	argv += optind;
168 	if ((argc == 0 && !(flags&CHECK_PREEN)) ||
169 	    (argc > 0 && (flags&CHECK_PREEN)))
170 		usage();
171 	if (!gflag && !uflag) {
172 		gflag++;
173 		uflag++;
174 	}
175 	if (gflag) {
176 		setgrent();
177 		while ((gr = getgrent()) != 0)
178 			(void) addid(gr->gr_gid, GRPQUOTA, gr->gr_name);
179 		endgrent();
180 	}
181 	if (uflag) {
182 		setpwent();
183 		while ((pw = getpwent()) != 0)
184 			(void) addid(pw->pw_uid, USRQUOTA, pw->pw_name);
185 		endpwent();
186 	}
187 	if (flags&CHECK_PREEN)
188 		exit(checkfstab(flags, maxrun, needchk, chkquota));
189 	if (setfsent() == 0)
190 		err(1, "%s: can't open", FSTAB);
191 	while ((fs = getfsent()) != NULL) {
192 		if (((argnum = oneof_realpath(fs->fs_file, argv, argc)) >= 0 ||
193 		    (argnum = oneof_specname(fs->fs_spec, argv, argc)) >= 0) &&
194 		    (auxdata = needchk(fs)) &&
195 		    (name = blockcheck(fs->fs_spec))) {
196 			done |= 1 << argnum;
197 			errs += chkquota(fs->fs_vfstype, name,
198 			    fs->fs_file, auxdata, NULL);
199 		}
200 	}
201 	endfsent();
202 	for (i = 0; i < argc; i++)
203 		if ((done & (1 << i)) == 0)
204 			fprintf(stderr, "%s not found in %s\n",
205 			    argv[i], FSTAB);
206 	exit(errs);
207 }
208 
209 void
210 usage(void)
211 {
212 	extern char *__progname;
213 	(void)fprintf(stderr, "usage: %s [-adguv] [-l maxparallel] "
214 	    "filesystem ...\n", __progname);
215 	exit(1);
216 }
217 
218 void *
219 needchk(struct fstab *fs)
220 {
221 	struct quotaname *qnp;
222 	char *qfnp;
223 
224 	if (fs->fs_passno == 0)
225 		return NULL;
226 	if (strcmp(fs->fs_type, FSTAB_RW))
227 		return (NULL);
228 	if (strcmp(fs->fs_vfstype, "ffs") &&
229 	    strcmp(fs->fs_vfstype, "ufs") &&
230 	    strcmp(fs->fs_vfstype, "mfs"))
231 		return (NULL);
232 	if ((qnp = malloc(sizeof(*qnp))) == NULL)
233 		err(1, "%s", strerror(errno));
234 	qnp->flags = 0;
235 	if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) {
236 		strlcpy(qnp->grpqfname, qfnp, sizeof qnp->grpqfname);
237 		qnp->flags |= HASGRP;
238 	}
239 	if (uflag && hasquota(fs, USRQUOTA, &qfnp)) {
240 		strlcpy(qnp->usrqfname, qfnp, sizeof qnp->usrqfname);
241 		qnp->flags |= HASUSR;
242 	}
243 	if (qnp->flags)
244 		return (qnp);
245 	free(qnp);
246 	return (NULL);
247 }
248 
249 /*
250  * Possible superblock locations ordered from most to least likely.
251  */
252 static int sblock_try[] = SBLOCKSEARCH;
253 
254 /*
255  * Scan the specified file system to check quota(s) present on it.
256  */
257 int
258 chkquota(const char *vfstype, const char *fsname, const char *mntpt,
259     void *auxarg, pid_t *pidp)
260 {
261 	struct quotaname *qnp = auxarg;
262 	struct fileusage *fup;
263 	union dinode *dp;
264 	int cg, i, mode, errs = 0, status;
265 	ino_t ino, inosused;
266 	pid_t pid;
267 	char *cp;
268 
269 	switch (pid = fork()) {
270 	case -1:	/* error */
271 		warn("fork");
272 		return 1;
273 	case 0:		/* child */
274 		if ((fi = opendev(fsname, O_RDONLY, 0, NULL)) < 0)
275 			err(1, "%s", fsname);
276 		sync();
277 		dev_bsize = 1;
278 		for (i = 0; sblock_try[i] != -1; i++) {
279 			bread(sblock_try[i], (char *)&sblock, (long)SBLOCKSIZE);
280 			if ((sblock.fs_magic == FS_UFS1_MAGIC ||
281 			     (sblock.fs_magic == FS_UFS2_MAGIC &&
282 			      sblock.fs_sblockloc == sblock_try[i])) &&
283 			    sblock.fs_bsize <= MAXBSIZE &&
284 			    sblock.fs_bsize >= sizeof(struct fs))
285 				break;
286 		}
287 		if (sblock_try[i] == -1) {
288 			warn("Cannot find file system superblock");
289 			return (1);
290 		}
291 		dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
292 		maxino = sblock.fs_ncg * sblock.fs_ipg;
293 		for (cg = 0; cg < sblock.fs_ncg; cg++) {
294 			ino = cg * sblock.fs_ipg;
295 			setinodebuf(ino);
296 			bread(fsbtodb(&sblock, cgtod(&sblock, cg)),
297 			    (char *)(&cgblk), sblock.fs_cgsize);
298 			if (sblock.fs_magic == FS_UFS2_MAGIC)
299 				inosused = cgblk.cg_initediblk;
300 			else
301 				inosused = sblock.fs_ipg;
302 			/*
303 			 * If we are using soft updates, then we can trust the
304 			 * cylinder group inode allocation maps to tell us which
305 			 * inodes are allocated. We will scan the used inode map
306 			 * to find the inodes that are really in use, and then
307 			 * read only those inodes in from disk.
308 			 */
309 			if (sblock.fs_flags & FS_DOSOFTDEP) {
310 				if (!cg_chkmagic(&cgblk))
311 					errx(1, "CG %d: BAD MAGIC NUMBER\n", cg);
312 				cp = &cg_inosused(&cgblk)[(inosused - 1) / CHAR_BIT];
313 				for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) {
314 					if (*cp == 0)
315 						continue;
316 					for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
317 						if (*cp & i)
318 							break;
319 						inosused--;
320 					}
321 					break;
322 				}
323 				if (inosused <= 0)
324 					continue;
325 			}
326 			for (i = 0; i < inosused; i++, ino++) {
327 				if ((dp = getnextinode(ino)) == NULL ||
328 				    ino < ROOTINO ||
329 				    (mode = DIP(dp, di_mode) & IFMT) == 0)
330 					continue;
331 				if (qnp->flags & HASGRP) {
332 					fup = addid(DIP(dp, di_gid),
333 					    GRPQUOTA, NULL);
334 					fup->fu_curinodes++;
335 					if (mode == IFREG || mode == IFDIR ||
336 					    mode == IFLNK)
337 						fup->fu_curblocks +=
338 						    DIP(dp, di_blocks);
339 				}
340 				if (qnp->flags & HASUSR) {
341 					fup = addid(DIP(dp, di_uid),
342 					    USRQUOTA, NULL);
343 					fup->fu_curinodes++;
344 					if (mode == IFREG || mode == IFDIR ||
345 					    mode == IFLNK)
346 						fup->fu_curblocks +=
347 						    DIP(dp, di_blocks);
348 				}
349 			}
350 		}
351 		freeinodebuf();
352 		if (flags&(CHECK_DEBUG|CHECK_VERBOSE)) {
353 			(void)printf("*** Checking ");
354 			if (qnp->flags & HASUSR) {
355 				(void)printf("%s", qfextension[USRQUOTA]);
356 				if (qnp->flags & HASGRP)
357 					(void)printf(" and ");
358 			}
359 			if (qnp->flags & HASGRP)
360 				(void)printf("%s", qfextension[GRPQUOTA]);
361 			(void)printf(" quotas for %s (%s), %swait\n",
362 			    fsname, mntpt, pidp? "no" : "");
363 		}
364 		if (qnp->flags & HASUSR)
365 			errs += update(mntpt, qnp->usrqfname, USRQUOTA);
366 		if (qnp->flags & HASGRP)
367 			errs += update(mntpt, qnp->grpqfname, GRPQUOTA);
368 		close(fi);
369 		exit (errs);
370 		break;
371 	default:	/* parent */
372 		if (pidp != NULL) {
373 			*pidp = pid;
374 			return 0;
375 		}
376 		if (waitpid(pid, &status, 0) < 0) {
377 			warn("waitpid");
378 			return 1;
379 		}
380 		if (WIFEXITED(status)) {
381 			if (WEXITSTATUS(status) != 0)
382 				return WEXITSTATUS(status);
383 		} else if (WIFSIGNALED(status)) {
384 			warnx("%s: %s", fsname, strsignal(WTERMSIG(status)));
385 			return 1;
386 		}
387 		break;
388 	}
389 	return (0);
390 }
391 
392 /*
393  * Update a specified quota file.
394  */
395 int
396 update(const char *fsname, const char *quotafile, int type)
397 {
398 	struct fileusage *fup;
399 	FILE *qfi, *qfo;
400 	u_int32_t id, lastid;
401 	struct dqblk dqbuf;
402 	static int warned = 0;
403 	static struct dqblk zerodqbuf;
404 	static struct fileusage zerofileusage;
405 
406 	if (flags&CHECK_DEBUG)
407 		printf("updating: %s\n", quotafile);
408 
409 	if ((qfo = fopen(quotafile, (flags&CHECK_DEBUG)? "r" : "r+")) == NULL) {
410 		if (errno == ENOENT)
411 			qfo = fopen(quotafile, "w+");
412 		if (qfo) {
413 			warnx("creating quota file: %s", quotafile);
414 #define	MODE	(S_IRUSR|S_IWUSR|S_IRGRP)
415 			(void) fchown(fileno(qfo), getuid(), getquotagid());
416 			(void) fchmod(fileno(qfo), MODE);
417 		} else {
418 			warn("%s", quotafile);
419 			return (1);
420 		}
421 	}
422 	if ((qfi = fopen(quotafile, "r")) == NULL) {
423 		warn("%s", quotafile);
424 		(void) fclose(qfo);
425 		return (1);
426 	}
427 	if (quotactl(fsname, QCMD(Q_SYNC, type), 0, (caddr_t)0) < 0 &&
428 	    errno == EOPNOTSUPP && !warned &&
429 	    (flags&(CHECK_DEBUG|CHECK_VERBOSE))) {
430 		warned++;
431 		(void)printf("*** Warning: %s\n",
432 		    "Quotas are not compiled into this kernel");
433 	}
434 	for (lastid = highid[type], id = 0; id <= lastid; id++) {
435 		if (fread((char *)&dqbuf, sizeof(struct dqblk), 1, qfi) == 0)
436 			dqbuf = zerodqbuf;
437 		if ((fup = lookup(id, type)) == 0)
438 			fup = &zerofileusage;
439 		if (dqbuf.dqb_curinodes == fup->fu_curinodes &&
440 		    dqbuf.dqb_curblocks == fup->fu_curblocks) {
441 			fup->fu_curinodes = 0;
442 			fup->fu_curblocks = 0;
443 			fseek(qfo, (long)sizeof(struct dqblk), SEEK_CUR);
444 			continue;
445 		}
446 		if (flags&(CHECK_DEBUG|CHECK_VERBOSE)) {
447 			if (flags&CHECK_PREEN)
448 				printf("%s: ", fsname);
449 			printf("%-8s fixed:", fup->fu_name);
450 			if (dqbuf.dqb_curinodes != fup->fu_curinodes)
451 				(void)printf("\tinodes %d -> %u",
452 				    dqbuf.dqb_curinodes, fup->fu_curinodes);
453 			if (dqbuf.dqb_curblocks != fup->fu_curblocks)
454 				(void)printf("\tblocks %u -> %u",
455 				    dqbuf.dqb_curblocks, fup->fu_curblocks);
456 			(void)printf("\n");
457 		}
458 		/*
459 		 * Reset time limit if have a soft limit and were
460 		 * previously under it, but are now over it.
461 		 */
462 		if (dqbuf.dqb_bsoftlimit &&
463 		    dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
464 		    fup->fu_curblocks >= dqbuf.dqb_bsoftlimit)
465 			dqbuf.dqb_btime = 0;
466 		if (dqbuf.dqb_isoftlimit &&
467 		    dqbuf.dqb_curblocks < dqbuf.dqb_isoftlimit &&
468 		    fup->fu_curblocks >= dqbuf.dqb_isoftlimit)
469 			dqbuf.dqb_itime = 0;
470 		dqbuf.dqb_curinodes = fup->fu_curinodes;
471 		dqbuf.dqb_curblocks = fup->fu_curblocks;
472 		if (!(flags & CHECK_DEBUG)) {
473 			fwrite((char *)&dqbuf, sizeof(struct dqblk), 1, qfo);
474 			(void) quotactl(fsname, QCMD(Q_SETUSE, type), id,
475 			    (caddr_t)&dqbuf);
476 		}
477 		fup->fu_curinodes = 0;
478 		fup->fu_curblocks = 0;
479 	}
480 	fclose(qfi);
481 	fflush(qfo);
482 	if (!(flags & CHECK_DEBUG))
483 		ftruncate(fileno(qfo),
484 		    (off_t)((highid[type] + 1) * sizeof(struct dqblk)));
485 	fclose(qfo);
486 	return (0);
487 }
488 
489 /*
490  * Check to see if realpath(target) matches a realpath() in list of size cnt.
491  */
492 int
493 oneof_realpath(char *target, char *list[], int cnt)
494 {
495 	int i;
496 	char realtarget[PATH_MAX], realargv[PATH_MAX];
497 	char *rv;
498 
499 	rv = realpath(target, realtarget);
500 	if (rv == NULL)
501 		return (-1);
502 
503 	for (i = 0; i < cnt; i++) {
504 		rv = realpath(list[i], realargv);
505 		if (rv && strcmp(realtarget, realargv) == 0)
506 			break;
507 	}
508 
509 	if (i < cnt)
510 		return (i);
511 	else
512 		return (-1);
513 }
514 
515 /*
516  * Check to see if opendev(target) matches a opendev() in list of size cnt.
517  */
518 int
519 oneof_specname(char *target, char *list[], int cnt)
520 {
521 	int i, fd;
522 	char *tmp, *targetdev, *argvdev;
523 
524 	fd = opendev(target, O_RDONLY, 0, &tmp);
525 	if (fd == -1)
526 		return (-1);
527 	close(fd);
528 	targetdev = strdup(tmp);
529 
530 	for (i = 0; i < cnt; i++) {
531 		fd = opendev(list[i], O_RDONLY, 0, &argvdev);
532 		if (fd == -1)
533 			continue;
534 		close(fd);
535 		if (strcmp(targetdev, argvdev) == 0)
536 			break;
537 	}
538 
539 	free(targetdev);
540 
541 	if (i < cnt)
542 		return (i);
543 	else
544 		return (-1);
545 }
546 
547 /*
548  * Determine the group identifier for quota files.
549  */
550 int
551 getquotagid(void)
552 {
553 	struct group *gr;
554 
555 	if ((gr = getgrnam(quotagroup)) != NULL)
556 		return (gr->gr_gid);
557 	return (-1);
558 }
559 
560 /*
561  * Check to see if a particular quota is to be enabled.
562  */
563 int
564 hasquota(struct fstab *fs, int type, char **qfnamep)
565 {
566 	char *opt, *cp;
567 	static char initname, usrname[100], grpname[100];
568 	static char buf[BUFSIZ];
569 
570 	if (!initname) {
571 		(void)snprintf(usrname, sizeof(usrname),
572 		    "%s%s", qfextension[USRQUOTA], qfname);
573 		(void)snprintf(grpname, sizeof(grpname),
574 		    "%s%s", qfextension[GRPQUOTA], qfname);
575 		initname = 1;
576 	}
577 	(void)strlcpy(buf, fs->fs_mntops, sizeof(buf));
578 	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
579 		if ((cp = strchr(opt, '=')) != NULL)
580 			*cp++ = '\0';
581 		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
582 			break;
583 		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
584 			break;
585 	}
586 	if (!opt)
587 		return (0);
588 	if (cp)
589 		*qfnamep = cp;
590 	else {
591 		(void)snprintf(buf, sizeof(buf),
592 		    "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
593 		*qfnamep = buf;
594 	}
595 	return (1);
596 }
597 
598 /*
599  * Routines to manage the file usage table.
600  *
601  * Lookup an id of a specific type.
602  */
603 struct fileusage *
604 lookup(u_int32_t id, int type)
605 {
606 	struct fileusage *fup;
607 
608 	for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
609 		if (fup->fu_id == id)
610 			return (fup);
611 	return (NULL);
612 }
613 
614 /*
615  * Add a new file usage id if it does not already exist.
616  */
617 struct fileusage *
618 addid(u_int32_t id, int type, char *name)
619 {
620 	struct fileusage *fup, **fhp;
621 	int len;
622 
623 	if ((fup = lookup(id, type)) != NULL)
624 		return (fup);
625 	if (name)
626 		len = strlen(name);
627 	else
628 		len = 10;
629 	if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
630 		err(1, "%s", strerror(errno));
631 	fhp = &fuhead[type][id & (FUHASH - 1)];
632 	fup->fu_next = *fhp;
633 	*fhp = fup;
634 	fup->fu_id = id;
635 	if (id > highid[type])
636 		highid[type] = id;
637 	if (name)
638 		memcpy(fup->fu_name, name, len + 1);
639 	else
640 		(void)snprintf(fup->fu_name, len, "%u", id);
641 	return (fup);
642 }
643 
644 /*
645  * Special purpose version of ginode used to optimize pass
646  * over all the inodes in numerical order.
647  */
648 static ino_t nextino, lastinum, lastvalidinum;
649 static long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
650 static caddr_t inodebuf;
651 #define	INOBUFSIZE	56*1024		/* size of buffer to read inodes */
652 
653 union dinode *
654 getnextinode(ino_t inumber)
655 {
656 	long size;
657 	daddr64_t dblk;
658 	union dinode *dp;
659 	static caddr_t nextinop;
660 
661 	if (inumber != nextino++ || inumber > lastvalidinum)
662 		err(1, "bad inode number %u to nextinode", inumber);
663 	if (inumber >= lastinum) {
664 		readcnt++;
665 		dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
666 		if (readcnt % readpercg == 0) {
667 			size = partialsize;
668 			lastinum += partialcnt;
669 		} else {
670 			size = inobufsize;
671 			lastinum += fullcnt;
672 		}
673 		/*
674 		 * If bread returns an error, it will already have zeroed
675 		 * out the buffer, so we do not need to do so here.
676 		 */
677 		bread(dblk, inodebuf, size);
678 		nextinop = inodebuf;
679 	}
680 	dp = (union dinode *)nextinop;
681 	if (sblock.fs_magic == FS_UFS1_MAGIC)
682 		nextinop += sizeof(struct ufs1_dinode);
683 	else
684 		nextinop += sizeof(struct ufs2_dinode);
685 	return (dp);
686 }
687 
688 /*
689  * Prepare to scan a set of inodes.
690  */
691 void
692 setinodebuf(ino_t inum)
693 {
694 
695 	if (inum % sblock.fs_ipg != 0)
696 		errx(1, "bad inode number %d to setinodebuf", inum);
697 	lastvalidinum = inum + sblock.fs_ipg - 1;
698 	nextino = inum;
699 	lastinum = inum;
700 	readcnt = 0;
701 	if (inodebuf != NULL)
702 		return;
703 	inobufsize = blkroundup(&sblock, INOBUFSIZE);
704 	fullcnt = inobufsize / ((sblock.fs_magic == FS_UFS1_MAGIC) ?
705 	    sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
706 	readpercg = sblock.fs_ipg / fullcnt;
707 	partialcnt = sblock.fs_ipg % fullcnt;
708 	partialsize = partialcnt * ((sblock.fs_magic == FS_UFS1_MAGIC) ?
709 	    sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
710 	if (partialcnt != 0) {
711 		readpercg++;
712 	} else {
713 		partialcnt = fullcnt;
714 		partialsize = inobufsize;
715 	}
716 	if ((inodebuf = malloc((size_t)inobufsize)) == NULL)
717 		errx(1, "cannot allocate space for inode buffer");
718 }
719 
720 /*
721  * Free up data structures used to scan inodes.
722  */
723 void
724 freeinodebuf(void)
725 {
726 
727 	if (inodebuf != NULL)
728 		free(inodebuf);
729 	inodebuf = NULL;
730 }
731 
732 /*
733  * Read specified disk blocks.
734  */
735 void
736 bread(daddr64_t bno, char *buf, long cnt)
737 {
738 
739 	if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0 ||
740 	    read(fi, buf, cnt) != cnt)
741 		err(1, "bread failed on block %lld", (long long)bno);
742 }
743