quotacheck.c revision 1.50 1 /* $NetBSD: quotacheck.c,v 1.50 2022/11/17 06:40:41 chs 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.50 2022/11/17 06:40:41 chs 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_UFS2EA_MAGIC:
371 sblock.fs_magic = FS_UFS2_MAGIC;
372 /*FALLTHROUGH*/
373 case FS_UFS2_MAGIC:
374 is_ufs2 = 1;
375 /*FALLTHROUGH*/
376 #endif
377 case FS_UFS1_MAGIC:
378 break;
379 #ifdef HAVE_UFSv2
380 case FS_UFS2EA_MAGIC_SWAPPED:
381 sblock.fs_magic = FS_UFS2_MAGIC_SWAPPED;
382 /*FALLTHROUGH*/
383 case FS_UFS2_MAGIC_SWAPPED:
384 is_ufs2 = 1;
385 /*FALLTHROUGH*/
386 #endif
387 case FS_UFS1_MAGIC_SWAPPED:
388 needswap = 1;
389 ffs_sb_swap(&sblock, &sblock);
390 break;
391 default:
392 continue;
393 }
394
395 #ifdef HAVE_UFSv2
396 if (is_ufs2 || sblock.fs_old_flags & FS_FLAGS_UPDATED) {
397 if (sblock.fs_sblockloc != sblock_try[i])
398 continue;
399 } else {
400 if (sblock_try[i] == SBLOCK_UFS2)
401 continue;
402 }
403 #endif
404 break;
405 }
406
407 cgp = malloc(sblock.fs_cgsize);
408 if (cgp == NULL) {
409 warn("%s: can't allocate %d bytes of cg space", fsname,
410 sblock.fs_cgsize);
411 if (pid != NULL)
412 exit(1);
413 return 1;
414 }
415
416 dev_bsize = sblock.fs_fsize / FFS_FSBTODB(&sblock, 1);
417 maxino = sblock.fs_ncg * sblock.fs_ipg;
418 for (cg = 0; cg < sblock.fs_ncg; cg++) {
419 ino = cg * sblock.fs_ipg;
420 setinodebuf(ino);
421 #ifdef HAVE_UFSv2
422 if (sblock.fs_magic == FS_UFS2_MAGIC) {
423 bread(FFS_FSBTODB(&sblock, cgtod(&sblock, cg)), (char *)cgp,
424 sblock.fs_cgsize);
425 if (needswap)
426 ffs_cg_swap(cgp, cgp, &sblock);
427 inosused = cgp->cg_initediblk;
428 } else
429 #endif
430 inosused = sblock.fs_ipg;
431 for (i = 0; i < inosused; i++, ino++) {
432 if (got_siginfo) {
433 fprintf(stderr,
434 "%s: cyl group %d of %d (%d%%)\n",
435 fsname, cg, sblock.fs_ncg,
436 cg * 100 / sblock.fs_ncg);
437 got_siginfo = 0;
438 }
439 if (ino < UFS_ROOTINO)
440 continue;
441 if ((dp = getnextinode(ino)) == NULL)
442 continue;
443 if ((mode = DIP(dp, mode) & IFMT) == 0)
444 continue;
445 if (qnp->flags & HASGRP) {
446 fup = addid(DIP(dp, gid), GRPQUOTA,
447 (char *)0);
448 fup->fu_curinodes++;
449 if (mode == IFREG || mode == IFDIR ||
450 mode == IFLNK)
451 fup->fu_curblocks += DIP(dp, blocks);
452 }
453 if (qnp->flags & HASUSR) {
454 fup = addid(DIP(dp, uid), USRQUOTA,
455 (char *)0);
456 fup->fu_curinodes++;
457 if (mode == IFREG || mode == IFDIR ||
458 mode == IFLNK)
459 fup->fu_curblocks += DIP(dp, blocks);
460 }
461 }
462 }
463 freeinodebuf();
464 free(cgp);
465 if (qnp->flags & HASUSR)
466 errs += update(mntpt, qnp->usrqfname, USRQUOTA);
467 if (qnp->flags & HASGRP)
468 errs += update(mntpt, qnp->grpqfname, GRPQUOTA);
469 close(fi);
470 if (pid != NULL)
471 exit(errs);
472 return errs;
473 }
474
475 /*
476 * Update a specified quota file.
477 */
478 static int
479 update(const char *fsname, const char *quotafile, int type)
480 {
481 struct fileusage *fup;
482 FILE *qfi, *qfo;
483 uint32_t id, lastid, nextid;
484 int need_seek;
485 struct dqblk dqbuf;
486 static struct dqblk zerodqbuf;
487 static struct fileusage zerofileusage;
488 struct statvfs *fst;
489 int nfst, i;
490
491 nfst = getmntinfo(&fst, MNT_WAIT);
492 if (nfst == 0)
493 errx(1, "no filesystems mounted!");
494
495 for (i = 0; i < nfst; i++) {
496 if (strncmp(fst[i].f_fstypename, "ffs",
497 sizeof(fst[i].f_fstypename)) == 0 &&
498 strncmp(fst[i].f_mntonname, fsname,
499 sizeof(fst[i].f_mntonname)) == 0 &&
500 (fst[i].f_flag & ST_QUOTA) != 0) {
501 warnx("filesystem %s has quotas already turned on",
502 fsname);
503 }
504 }
505
506 if ((qfo = fopen(quotafile, "r+")) == NULL) {
507 if (errno == ENOENT)
508 qfo = fopen(quotafile, "w+");
509 if (qfo) {
510 (void) fprintf(stderr,
511 "quotacheck: creating quota file %s\n", quotafile);
512 #define MODE (S_IRUSR|S_IWUSR|S_IRGRP)
513 (void) fchown(fileno(qfo), getuid(), getquotagid());
514 (void) fchmod(fileno(qfo), MODE);
515 } else {
516 (void) fprintf(stderr,
517 "quotacheck: %s: %s\n", quotafile, strerror(errno));
518 return (1);
519 }
520 }
521 if ((qfi = fopen(quotafile, "r")) == NULL) {
522 (void) fprintf(stderr,
523 "quotacheck: %s: %s\n", quotafile, strerror(errno));
524 (void) fclose(qfo);
525 return (1);
526 }
527 need_seek = 1;
528 for (lastid = highid[type], id = 0; id <= lastid; id = nextid) {
529 if (fread((char *)&dqbuf, sizeof(struct dqblk), 1, qfi) == 0)
530 dqbuf = zerodqbuf;
531 if ((fup = lookup(id, type)) == 0)
532 fup = &zerofileusage;
533
534 nextid = subsequent(id, type);
535 /* watch out for id == UINT32_MAX */
536 if (nextid > 0 && nextid != id + 1)
537 nextid = skipforward(id, nextid, qfi);
538
539 if (got_siginfo) {
540 /*
541 * XXX this could try to show percentage through
542 * the ID list
543 */
544 fprintf(stderr, "%s: updating %s quotas for id=%"
545 PRIu32 " (%s)\n", fsname,
546 qfextension[type < MAXQUOTAS ? type : MAXQUOTAS],
547 id, fup->fu_name);
548 got_siginfo = 0;
549 }
550 if (dqbuf.dqb_curinodes == fup->fu_curinodes &&
551 dqbuf.dqb_curblocks == fup->fu_curblocks) {
552 fup->fu_curinodes = 0; /* reset usage */
553 fup->fu_curblocks = 0; /* for next filesystem */
554
555 need_seek = 1;
556 /* infinite loop avoidance (OR do as "nextid < id"?) */
557 if (id == UINT32_MAX || nextid == 0) {
558 break;
559 }
560 continue;
561 }
562 if (vflag) {
563 if (aflag)
564 printf("%s: ", fsname);
565 printf("%-8s fixed:", fup->fu_name);
566 if (dqbuf.dqb_curinodes != fup->fu_curinodes)
567 (void)printf("\tinodes %d -> %ld",
568 dqbuf.dqb_curinodes, fup->fu_curinodes);
569 if (dqbuf.dqb_curblocks != fup->fu_curblocks)
570 (void)printf("\tblocks %d -> %ld",
571 dqbuf.dqb_curblocks, fup->fu_curblocks);
572 (void)printf("\n");
573 }
574 /*
575 * Reset time limit if have a soft limit and were
576 * previously under it, but are now over it.
577 */
578 if (dqbuf.dqb_bsoftlimit &&
579 dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
580 fup->fu_curblocks >= dqbuf.dqb_bsoftlimit)
581 dqbuf.dqb_btime = 0;
582 if (dqbuf.dqb_isoftlimit &&
583 dqbuf.dqb_curinodes < dqbuf.dqb_isoftlimit &&
584 fup->fu_curinodes >= dqbuf.dqb_isoftlimit)
585 dqbuf.dqb_itime = 0;
586 dqbuf.dqb_curinodes = fup->fu_curinodes;
587 dqbuf.dqb_curblocks = fup->fu_curblocks;
588
589 if (need_seek) {
590 (void) fseeko(qfo, (off_t)id * sizeof(struct dqblk),
591 SEEK_SET);
592 need_seek = nextid != id + 1;
593 }
594 (void) fwrite(&dqbuf, sizeof(struct dqblk), 1, qfo);
595
596 fup->fu_curinodes = 0;
597 fup->fu_curblocks = 0;
598 /* infinite loop avoidance (OR do as "nextid < id"?) */
599 if (id == UINT32_MAX || nextid == 0) {
600 break;
601 }
602 }
603 (void)fclose(qfi);
604 (void)fflush(qfo);
605 if (highid[type] != UINT32_MAX)
606 (void)ftruncate(fileno(qfo),
607 (off_t)((highid[type] + 1) * sizeof(struct dqblk)));
608 (void)fclose(qfo);
609 return 0;
610 }
611
612 static uint32_t
613 skipforward(uint32_t cur, uint32_t to, FILE *qfi)
614 {
615 struct dqblk dqbuf;
616
617 if (qflag) {
618 (void)fseeko(qfi, (off_t)to * sizeof(struct dqblk), SEEK_SET);
619 return to;
620 }
621
622 while (++cur < to) {
623 /*
624 * if EOF occurs, nothing left to read, we're done
625 */
626 if (fread((char *)&dqbuf, sizeof(struct dqblk), 1, qfi) == 0)
627 return (to);
628
629 /*
630 * If we find an entry that shows usage, before the next
631 * id that has actual usage, we have to stop here, so the
632 * incorrect entry can be corrected in the file
633 */
634 if (dqbuf.dqb_curinodes != 0 || dqbuf.dqb_curblocks != 0) {
635 (void)fseek(qfi, -(long)sizeof(struct dqblk), SEEK_CUR);
636 return cur;
637 }
638 }
639 return to;
640 }
641
642 /*
643 * Determine the group identifier for quota files.
644 */
645 static int
646 getquotagid(void)
647 {
648 struct group *gr;
649
650 if ((gr = getgrnam(quotagroup)) != NULL)
651 return gr->gr_gid;
652 return -1;
653 }
654
655 /*
656 * Routines to manage the file usage table.
657 *
658 * Lookup an id of a specific type.
659 */
660 static struct fileusage *
661 lookup(uint32_t id, int type)
662 {
663 struct fileusage *fup;
664
665 for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
666 if (fup->fu_id == id)
667 return fup;
668 return NULL;
669 }
670
671 /*
672 * Add a new file usage id if it does not already exist.
673 */
674 static struct fileusage *
675 addid(uint32_t id, int type, const char *name)
676 {
677 struct fileusage *fup, **fhp;
678 size_t len;
679
680 if ((fup = lookup(id, type)) != NULL)
681 return fup;
682 if (name)
683 len = strlen(name);
684 else
685 len = 10;
686 if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
687 err(1, "%s", strerror(errno));
688 fhp = &fuhead[type][id & (FUHASH - 1)];
689 fup->fu_next = *fhp;
690 *fhp = fup;
691 fup->fu_id = id;
692 if (id > highid[type])
693 highid[type] = id;
694 if (name)
695 memmove(fup->fu_name, name, len + 1);
696 else
697 (void)snprintf(fup->fu_name, len + 1, "%" PRIu32, id);
698 return fup;
699 }
700
701 static uint32_t
702 subsequent(uint32_t id, int type)
703 {
704 struct fileusage *fup, **iup, **cup;
705 uint32_t next, offset;
706
707 next = highid[type] + 1;
708 offset = 0;
709 cup = iup = &fuhead[type][id & (FUHASH-1)];
710 do {
711 ++offset;
712 if (++cup >= &fuhead[type][FUHASH])
713 cup = &fuhead[type][0];
714 for (fup = *cup; fup != 0; fup = fup->fu_next) {
715 if (fup->fu_id > id && fup->fu_id <= id + offset)
716 return (fup->fu_id);
717 if (fup->fu_id > id && fup->fu_id < next)
718 next = fup->fu_id;
719 }
720 } while (cup != iup);
721
722 return next;
723 }
724
725 /*
726 * Special purpose version of ginode used to optimize first pass
727 * over all the inodes in numerical order.
728 */
729 static ino_t nextino, lastinum, lastvalidinum;
730 static long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
731 static union comb_dinode *inodebuf;
732 #define INOBUFSIZE 56*1024 /* size of buffer to read inodes */
733
734 static union comb_dinode *
735 getnextinode(ino_t inumber)
736 {
737 long size;
738 daddr_t dblk;
739 static union comb_dinode *dp;
740 union comb_dinode *ret;
741
742 if (inumber != nextino++ || inumber > lastvalidinum) {
743 errx(1, "bad inode number %llu to nextinode",
744 (unsigned long long)inumber);
745 }
746
747 if (inumber >= lastinum) {
748 readcnt++;
749 dblk = FFS_FSBTODB(&sblock, ino_to_fsba(&sblock, lastinum));
750 if (readcnt % readpercg == 0) {
751 size = partialsize;
752 lastinum += partialcnt;
753 } else {
754 size = inobufsize;
755 lastinum += fullcnt;
756 }
757 (void)bread(dblk, (caddr_t)inodebuf, size);
758 if (needswap) {
759 #ifdef HAVE_UFSv2
760 if (is_ufs2)
761 swap_dinode2(inodebuf, lastinum - inumber);
762 else
763 #endif
764 swap_dinode1(inodebuf, lastinum - inumber);
765 }
766 dp = (union comb_dinode *)inodebuf;
767 }
768 ret = dp;
769 dp = (union comb_dinode *)
770 ((char *)dp + (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE));
771 return ret;
772 }
773
774 static void
775 setinodebuf(ino_t inum)
776 {
777
778 if (inum % sblock.fs_ipg != 0)
779 errx(1, "bad inode number %llu to setinodebuf",
780 (unsigned long long)inum);
781
782 lastvalidinum = inum + sblock.fs_ipg - 1;
783 nextino = inum;
784 lastinum = inum;
785 readcnt = 0;
786 if (inodebuf != NULL)
787 return;
788 inobufsize = ffs_blkroundup(&sblock, INOBUFSIZE);
789 fullcnt = inobufsize / (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE);
790 readpercg = sblock.fs_ipg / fullcnt;
791 partialcnt = sblock.fs_ipg % fullcnt;
792 partialsize = partialcnt * (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE);
793 if (partialcnt != 0) {
794 readpercg++;
795 } else {
796 partialcnt = fullcnt;
797 partialsize = inobufsize;
798 }
799 if (inodebuf == NULL &&
800 (inodebuf = malloc((unsigned)inobufsize)) == NULL)
801 errx(1, "Cannot allocate space for inode buffer");
802 while (nextino < UFS_ROOTINO)
803 getnextinode(nextino);
804 }
805
806 static void
807 freeinodebuf(void)
808 {
809
810 free(inodebuf);
811 inodebuf = NULL;
812 }
813
814
815 #ifdef HAVE_UFSv2
816 static void
817 swap_dinode1(union comb_dinode *dp, int n)
818 {
819 int i;
820 struct ufs1_dinode *dp1;
821
822 dp1 = (struct ufs1_dinode *)&dp->dp1;
823 for (i = 0; i < n; i++, dp1++)
824 ffs_dinode1_swap(dp1, dp1);
825 }
826
827 static void
828 swap_dinode2(union comb_dinode *dp, int n)
829 {
830 int i;
831 struct ufs2_dinode *dp2;
832
833 dp2 = (struct ufs2_dinode *)&dp->dp2;
834 for (i = 0; i < n; i++, dp2++)
835 ffs_dinode2_swap(dp2, dp2);
836 }
837
838 #else
839
840 static void
841 swap_dinode1(union comb_dinode *dp, int n)
842 {
843 int i;
844 struct dinode *dp1;
845
846 dp1 = (struct dinode *) &dp->dp1;
847 for (i = 0; i < n; i++, dp1++)
848 ffs_dinode_swap(dp1, dp1);
849 }
850
851 #endif
852
853 /*
854 * Read specified disk blocks.
855 */
856 static void
857 bread(daddr_t bno, char *buf, long cnt)
858 {
859
860 if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0 ||
861 read(fi, buf, cnt) != cnt)
862 err(1, "block %lld", (long long)bno);
863 }
864
865 static void
866 infohandler(int sig)
867 {
868 got_siginfo = 1;
869 }
870