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