give more complete information about pathnames while linking up files
[unix-history] / usr / src / sbin / fsck / main.c
CommitLineData
fa95fc59 1#ifndef lint
bec6e7fb 2char version[] = "@(#)main.c 2.28 (Berkeley) %G%";
fa95fc59 3#endif
07670f7d 4
6e884967
KM
5#include <stdio.h>
6#include <ctype.h>
4d308541
KM
7#include <sys/param.h>
8#include <sys/fs.h>
9#include <sys/inode.h>
4d308541 10#include <sys/stat.h>
1a724405 11#include <sys/wait.h>
6e884967 12#include <fstab.h>
1a724405
KM
13#define KERNEL
14#include <sys/dir.h>
15#undef KERNEL
6e884967 16
f5971be7
BJ
17/* RECONSTRUCT ONLY BAD CG IN PASS 6 */
18
6e884967
KM
19typedef int (*SIG_TYP)();
20
c312eebd
KM
21#define MAXNINDIR (MAXBSIZE / sizeof (daddr_t))
22#define MAXINOPB (MAXBSIZE / sizeof (struct dinode))
b6407c9d 23#define SPERB (MAXBSIZE / sizeof(short))
1a724405 24#define MINDIRSIZE (sizeof (struct dirtemplate))
6e884967
KM
25
26#define MAXDUP 10 /* limit on dup blks (per inode) */
27#define MAXBAD 10 /* limit on bad blks (per inode) */
28
29#define USTATE 0 /* inode not allocated */
30#define FSTATE 01 /* inode is file */
31#define DSTATE 02 /* inode is directory */
32#define CLEAR 03 /* inode is to be cleared */
33
34typedef struct dinode DINODE;
35typedef struct direct DIRECT;
36
37#define ALLOC ((dp->di_mode & IFMT) != 0)
24a31719 38#define DIRCT ((dp->di_mode & IFMT) == IFDIR)
6e884967
KM
39#define REG ((dp->di_mode & IFMT) == IFREG)
40#define BLK ((dp->di_mode & IFMT) == IFBLK)
41#define CHR ((dp->di_mode & IFMT) == IFCHR)
690f77ba 42#define LNK ((dp->di_mode & IFMT) == IFLNK)
af5ba2f8
KM
43#define SOCK ((dp->di_mode & IFMT) == IFSOCK)
44#define BADBLK ((dp->di_mode & IFMT) == IFMT)
ea47352d 45#define SPECIAL (BLK || CHR)
6e884967 46
6e884967
KM
47struct bufarea {
48 struct bufarea *b_next; /* must be first */
49 daddr_t b_bno;
50 int b_size;
51 union {
b6407c9d 52 char b_buf[MAXBSIZE]; /* buffer space */
6e884967 53 short b_lnks[SPERB]; /* link counts */
b6407c9d 54 daddr_t b_indir[MAXNINDIR]; /* indirect block */
6e884967
KM
55 struct fs b_fs; /* super block */
56 struct cg b_cg; /* cylinder group */
b6407c9d 57 struct dinode b_dinode[MAXINOPB]; /* inode block */
6e884967
KM
58 } b_un;
59 char b_dirty;
60};
61
62typedef struct bufarea BUFAREA;
63
64BUFAREA inoblk; /* inode blocks */
65BUFAREA fileblk; /* other blks in filesys */
66BUFAREA sblk; /* file system superblock */
1a724405 67BUFAREA cgblk; /* cylinder group blocks */
6e884967
KM
68
69#define initbarea(x) (x)->b_dirty = 0;(x)->b_bno = (daddr_t)-1
70#define dirty(x) (x)->b_dirty = 1
71#define inodirty() inoblk.b_dirty = 1
72#define sbdirty() sblk.b_dirty = 1
73#define cgdirty() cgblk.b_dirty = 1
74
75#define dirblk fileblk.b_un
76#define sblock sblk.b_un.b_fs
77#define cgrp cgblk.b_un.b_cg
78
79struct filecntl {
80 int rfdes;
81 int wfdes;
82 int mod;
83} dfile; /* file descriptors for filesys */
84
1a724405
KM
85struct inodesc {
86 char id_type; /* type of descriptor, DATA or ADDR */
87 int (*id_func)(); /* function to be applied to blocks of inode */
88 ino_t id_number; /* inode number described */
89 ino_t id_parent; /* for DATA nodes, their parent */
90 daddr_t id_blkno; /* current block number being examined */
91 int id_numfrags; /* number of frags contained in block */
92 long id_filesize; /* for DATA nodes, the size of the directory */
93 int id_loc; /* for DATA nodes, current location in dir */
94 int id_entryno; /* for DATA nodes, current entry number */
95 DIRECT *id_dirp; /* for data nodes, ptr to current entry */
96 enum {DONTKNOW, NOFIX, FIX} id_fix; /* policy on fixing errors */
97};
98/* file types */
99#define DATA 1
100#define ADDR 2
101
102
6e884967
KM
103#define DUPTBLSIZE 100 /* num of dup blocks to remember */
104daddr_t duplist[DUPTBLSIZE]; /* dup block table */
105daddr_t *enddup; /* next entry in dup table */
106daddr_t *muldup; /* multiple dups part of table */
107
5ca02310 108#define MAXLNCNT 500 /* num zero link cnts to remember */
6e884967
KM
109ino_t badlncnt[MAXLNCNT]; /* table of inos with zero link cnts */
110ino_t *badlnp; /* next entry in table */
111
112char rawflg;
113char nflag; /* assume a no response */
114char yflag; /* assume a yes response */
115int bflag; /* location of alternate super block */
6994bf5d 116int debug; /* output debugging info */
6e884967
KM
117char preen; /* just fix normal inconsistencies */
118char rplyflag; /* any questions asked? */
119char hotroot; /* checking root device */
120char fixcg; /* corrupted free list bit maps */
121
79f5f76a 122char *blockmap; /* ptr to primary blk allocation map */
6e884967
KM
123char *freemap; /* ptr to secondary blk allocation map */
124char *statemap; /* ptr to inode state table */
125short *lncntp; /* ptr to link count table */
126
6e884967 127char *srchname; /* name being searched for in dir */
1a724405
KM
128char pathname[BUFSIZ]; /* current pathname */
129char *pathp; /* pointer to pathname position */
aac37e2e 130char *endpathname = &pathname[BUFSIZ - 2];
6e884967
KM
131
132char *lfname = "lost+found";
133
6e884967 134ino_t imax; /* number of inodes */
6e884967
KM
135ino_t lastino; /* hiwater mark of inodes */
136ino_t lfdir; /* lost & found directory */
6e884967 137
6e884967 138off_t maxblk; /* largest logical blk in file */
79f5f76a 139off_t bmapsz; /* num chars in blockmap */
6e884967
KM
140
141daddr_t n_ffree; /* number of small free blocks */
142daddr_t n_bfree; /* number of large free blocks */
143daddr_t n_blks; /* number of blocks used */
144daddr_t n_files; /* number of files seen */
145daddr_t n_index;
146daddr_t n_bad;
147daddr_t fmax; /* number of blocks in the volume */
148
149daddr_t badblk;
150daddr_t dupblk;
151
152int inosumbad;
153int offsumbad;
f3c028b7 154int frsumbad;
184d432f 155int sbsumbad;
6e884967 156
6e884967
KM
157#define zapino(x) (*(x) = zino)
158struct dinode zino;
159
79f5f76a
KM
160#define setbmap(x) setbit(blockmap, x)
161#define getbmap(x) isset(blockmap, x)
162#define clrbmap(x) clrbit(blockmap, x)
6e884967
KM
163
164#define setfmap(x) setbit(freemap, x)
165#define getfmap(x) isset(freemap, x)
166#define clrfmap(x) clrbit(freemap, x)
167
1a724405 168#define ALTERED 010
6e884967
KM
169#define KEEPON 04
170#define SKIP 02
171#define STOP 01
172
173int (*signal())();
174long lseek();
175time_t time();
176DINODE *ginode();
1a724405 177DIRECT *fsck_readdir();
6e884967 178BUFAREA *getblk();
6e884967 179int catch();
1a724405
KM
180int findino(), mkentry(), chgdd();
181int pass1check(), pass1bcheck(), pass2check(), pass4check();
182char *rawname(), *unrawname();
183char *calloc(), *strcpy(), *strcat(), *rindex();
97656a89 184extern int inside[], around[];
b6407c9d 185extern unsigned char *fragtbl[];
6e884967
KM
186
187char *devname;
188
189main(argc, argv)
8ebf61ca
KM
190 int argc;
191 char *argv[];
6e884967
KM
192{
193 struct fstab *fsp;
194 int pid, passno, anygtr, sumstatus;
195
196 sync();
197 while (--argc > 0 && **++argv == '-') {
198 switch (*++*argv) {
199
200 case 'p':
201 preen++;
202 break;
203
204 case 'b':
1a724405
KM
205 if (argv[0][1] != '\0') {
206 bflag = atoi(argv[0]+1);
207 } else {
208 bflag = atoi(*++argv);
209 argc--;
210 }
6e884967
KM
211 printf("Alternate super block location: %d\n", bflag);
212 break;
213
6994bf5d
KM
214 case 'd':
215 debug++;
216 break;
217
6e884967
KM
218 case 'n': /* default no answer flag */
219 case 'N':
220 nflag++;
221 yflag = 0;
222 break;
223
224 case 'y': /* default yes answer flag */
225 case 'Y':
226 yflag++;
227 nflag = 0;
228 break;
229
230 default:
231 errexit("%c option?\n", **argv);
232 }
233 }
234 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1a724405 235 (void)signal(SIGINT, catch);
6e884967
KM
236 if (argc) {
237 while (argc-- > 0) {
238 hotroot = 0;
d2c95d76 239 checkfilesys(*argv++);
6e884967
KM
240 }
241 exit(0);
242 }
243 sumstatus = 0;
244 passno = 1;
245 do {
246 anygtr = 0;
247 if (setfsent() == 0)
248 errexit("Can't open checklist file: %s\n", FSTAB);
249 while ((fsp = getfsent()) != 0) {
250 if (strcmp(fsp->fs_type, FSTAB_RW) &&
fa95fc59
SL
251 strcmp(fsp->fs_type, FSTAB_RO) &&
252 strcmp(fsp->fs_type, FSTAB_RQ))
6e884967
KM
253 continue;
254 if (preen == 0 ||
255 passno == 1 && fsp->fs_passno == passno) {
256 if (blockcheck(fsp->fs_spec) == 0 && preen)
257 exit(8);
258 } else if (fsp->fs_passno > passno)
259 anygtr = 1;
260 else if (fsp->fs_passno == passno) {
261 pid = fork();
262 if (pid < 0) {
263 perror("fork");
264 exit(8);
265 }
266 if (pid == 0)
267 if (blockcheck(fsp->fs_spec)==0)
268 exit(8);
269 else
270 exit(0);
271 }
272 }
273 if (preen) {
1a724405 274 union wait status;
6e884967 275 while (wait(&status) != -1)
1a724405 276 sumstatus |= status.w_retcode;
6e884967
KM
277 }
278 passno++;
279 } while (anygtr);
280 if (sumstatus)
281 exit(8);
1a724405 282 (void)endfsent();
6e884967
KM
283 exit(0);
284}
285
286blockcheck(name)
287 char *name;
288{
4d308541 289 struct stat stslash, stblock, stchar;
6e884967
KM
290 char *raw;
291 int looped = 0;
292
293 hotroot = 0;
294 if (stat("/", &stslash) < 0){
295 error("Can't stat root\n");
296 return (0);
297 }
298retry:
299 if (stat(name, &stblock) < 0){
300 error("Can't stat %s\n", name);
301 return (0);
302 }
303 if (stblock.st_mode & S_IFBLK) {
304 raw = rawname(name);
305 if (stat(raw, &stchar) < 0){
306 error("Can't stat %s\n", raw);
307 return (0);
308 }
309 if (stchar.st_mode & S_IFCHR) {
310 if (stslash.st_dev == stblock.st_rdev) {
311 hotroot++;
addfa1aa 312 raw = unrawname(name);
6e884967 313 }
d2c95d76 314 checkfilesys(raw);
6e884967
KM
315 return (1);
316 } else {
317 error("%s is not a character device\n", raw);
318 return (0);
319 }
320 } else if (stblock.st_mode & S_IFCHR) {
321 if (looped) {
322 error("Can't make sense out of name %s\n", name);
323 return (0);
324 }
325 name = unrawname(name);
326 looped++;
327 goto retry;
328 }
329 error("Can't make sense out of name %s\n", name);
330 return (0);
331}
332
d2c95d76
BJ
333checkfilesys(filesys)
334 char *filesys;
6e884967 335{
6e884967 336
d2c95d76
BJ
337 devname = filesys;
338 if (setup(filesys) == 0) {
6e884967 339 if (preen)
d2c95d76 340 pfatal("CAN'T CHECK FILE SYSTEM.");
6e884967
KM
341 return;
342 }
d2c95d76 343/* 1: scan inodes tallying blocks used */
690f77ba
KM
344 if (preen == 0) {
345 printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
6e884967
KM
346 if (hotroot)
347 printf("** Root file system\n");
348 printf("** Phase 1 - Check Blocks and Sizes\n");
349 }
d2c95d76
BJ
350 pass1();
351
f5971be7 352/* 1b: locate first references to duplicates, if any */
d2c95d76
BJ
353 if (enddup != &duplist[0]) {
354 if (preen)
355 pfatal("INTERNAL ERROR: dups with -p");
356 printf("** Phase 1b - Rescan For More DUPS\n");
357 pass1b();
358 }
359
1a724405 360/* 2: traverse directories from root to mark all connected directories */
d2c95d76
BJ
361 if (preen == 0)
362 printf("** Phase 2 - Check Pathnames\n");
363 pass2();
364
1a724405 365/* 3: scan inodes looking for disconnected directories */
d2c95d76
BJ
366 if (preen == 0)
367 printf("** Phase 3 - Check Connectivity\n");
368 pass3();
369
1a724405 370/* 4: scan inodes looking for disconnected files; check reference counts */
d2c95d76
BJ
371 if (preen == 0)
372 printf("** Phase 4 - Check Reference Counts\n");
373 pass4();
374
1a724405 375/* 5: check resource counts in cylinder groups */
d2c95d76
BJ
376 if (preen == 0)
377 printf("** Phase 5 - Check Cyl groups\n");
378 pass5();
379
380 if (fixcg) {
381 if (preen == 0)
382 printf("** Phase 6 - Salvage Cylinder Groups\n");
383 makecg();
384 n_ffree = sblock.fs_cstotal.cs_nffree;
385 n_bfree = sblock.fs_cstotal.cs_nbfree;
386 }
387
388 pwarn("%d files, %d used, %d free (%d frags, %d blocks)\n",
389 n_files, n_blks - howmany(sblock.fs_cssize, sblock.fs_fsize),
390 n_ffree + sblock.fs_frag * n_bfree, n_ffree, n_bfree);
391 if (dfile.mod) {
1a724405 392 (void)time(&sblock.fs_time);
d2c95d76
BJ
393 sbdirty();
394 }
395 ckfini();
396 free(blockmap);
397 free(freemap);
398 free(statemap);
1a724405 399 free((char *)lncntp);
addfa1aa
BJ
400 if (!dfile.mod)
401 return;
402 if (!preen) {
403 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
404 if (hotroot)
405 printf("\n***** REBOOT UNIX *****\n");
406 }
407 if (hotroot) {
408 sync();
409 exit(4);
f5971be7 410 }
f5971be7
BJ
411}
412
413setup(dev)
414 char *dev;
415{
416 dev_t rootdev;
417 struct stat statb;
1a724405
KM
418 daddr_t super = bflag ? bflag : SBLOCK;
419 int i, j, c, d, cgd;
420 long size;
421 BUFAREA asblk;
422# define altsblock asblk.b_un.b_fs
f5971be7 423
f5971be7
BJ
424 if (stat("/", &statb) < 0)
425 errexit("Can't stat root\n");
426 rootdev = statb.st_dev;
427 if (stat(dev, &statb) < 0) {
428 error("Can't stat %s\n", dev);
429 return (0);
430 }
431 rawflg = 0;
432 if ((statb.st_mode & S_IFMT) == S_IFBLK)
433 ;
434 else if ((statb.st_mode & S_IFMT) == S_IFCHR)
435 rawflg++;
436 else {
437 if (reply("file is not a block or character device; OK") == 0)
438 return (0);
439 }
440 if (rootdev == statb.st_rdev)
441 hotroot++;
442 if ((dfile.rfdes = open(dev, 0)) < 0) {
443 error("Can't open %s\n", dev);
444 return (0);
445 }
446 if (preen == 0)
447 printf("** %s", dev);
448 if (nflag || (dfile.wfdes = open(dev, 1)) < 0) {
449 dfile.wfdes = -1;
450 if (preen)
451 pfatal("NO WRITE ACCESS");
452 printf(" (NO WRITE)");
453 }
454 if (preen == 0)
455 printf("\n");
456 fixcg = 0; inosumbad = 0; offsumbad = 0; frsumbad = 0; sbsumbad = 0;
457 dfile.mod = 0;
458 n_files = n_blks = n_ffree = n_bfree = 0;
459 muldup = enddup = &duplist[0];
460 badlnp = &badlncnt[0];
461 lfdir = 0;
462 rplyflag = 0;
463 initbarea(&sblk);
464 initbarea(&fileblk);
465 initbarea(&inoblk);
466 initbarea(&cgblk);
1a724405 467 initbarea(&asblk);
f5971be7
BJ
468 /*
469 * Read in the super block and its summary info.
470 */
1a724405 471 if (bread(&dfile, (char *)&sblock, super, (long)SBSIZE) == 0)
f5971be7
BJ
472 return (0);
473 sblk.b_bno = super;
474 sblk.b_size = SBSIZE;
475 /*
476 * run a few consistency checks of the super block
477 */
478 if (sblock.fs_magic != FS_MAGIC)
479 { badsb("MAGIC NUMBER WRONG"); return (0); }
480 if (sblock.fs_ncg < 1)
481 { badsb("NCG OUT OF RANGE"); return (0); }
482 if (sblock.fs_cpg < 1 || sblock.fs_cpg > MAXCPG)
483 { badsb("CPG OUT OF RANGE"); return (0); }
f5971be7
BJ
484 if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl ||
485 (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl)
486 { badsb("NCYL DOES NOT JIVE WITH NCG*CPG"); return (0); }
1a724405 487 if (sblock.fs_sbsize > SBSIZE)
f5971be7 488 { badsb("SIZE PREPOSTEROUSLY LARGE"); return (0); }
1a724405
KM
489 /*
490 * Set all possible fields that could differ, then do check
491 * of whole super block against an alternate super block.
492 * When an alternate super-block is specified this check is skipped.
493 */
494 if (bflag)
495 goto sbok;
496 if (getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1),
497 sblock.fs_sbsize) == 0)
498 return (0);
499 altsblock.fs_link = sblock.fs_link;
500 altsblock.fs_rlink = sblock.fs_rlink;
501 altsblock.fs_time = sblock.fs_time;
502 altsblock.fs_cstotal = sblock.fs_cstotal;
503 altsblock.fs_cgrotor = sblock.fs_cgrotor;
504 altsblock.fs_fmod = sblock.fs_fmod;
505 altsblock.fs_clean = sblock.fs_clean;
506 altsblock.fs_ronly = sblock.fs_ronly;
507 altsblock.fs_flags = sblock.fs_flags;
508 altsblock.fs_maxcontig = sblock.fs_maxcontig;
509 altsblock.fs_minfree = sblock.fs_minfree;
510 altsblock.fs_rotdelay = sblock.fs_rotdelay;
511 altsblock.fs_maxbpg = sblock.fs_maxbpg;
512 bcopy((char *)sblock.fs_csp, (char *)altsblock.fs_csp,
513 sizeof sblock.fs_csp);
514 bcopy((char *)sblock.fs_fsmnt, (char *)altsblock.fs_fsmnt,
515 sizeof sblock.fs_fsmnt);
516 if (bcmp((char *)&sblock, (char *)&altsblock, (int)sblock.fs_sbsize))
517 { badsb("TRASHED VALUES IN SUPER BLOCK"); return (0); }
518sbok:
f5971be7
BJ
519 fmax = sblock.fs_size;
520 imax = sblock.fs_ncg * sblock.fs_ipg;
521 n_bad = cgsblock(&sblock, 0); /* boot block plus dedicated sblock */
522 /*
523 * read in the summary info.
524 */
525 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
526 size = sblock.fs_cssize - i < sblock.fs_bsize ?
527 sblock.fs_cssize - i : sblock.fs_bsize;
1a724405
KM
528 sblock.fs_csp[j] = (struct csum *)calloc(1, (unsigned)size);
529 if (bread(&dfile, (char *)sblock.fs_csp[j],
f5971be7 530 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
1a724405
KM
531 size) == 0)
532 return (0);
f5971be7
BJ
533 }
534 /*
535 * allocate and initialize the necessary maps
536 */
537 bmapsz = roundup(howmany(fmax, NBBY), sizeof(short));
1a724405 538 blockmap = calloc((unsigned)bmapsz, sizeof (char));
f5971be7
BJ
539 if (blockmap == NULL) {
540 printf("cannot alloc %d bytes for blockmap\n", bmapsz);
1a724405 541 goto badsb;
f5971be7 542 }
1a724405 543 freemap = calloc((unsigned)bmapsz, sizeof (char));
f5971be7
BJ
544 if (freemap == NULL) {
545 printf("cannot alloc %d bytes for freemap\n", bmapsz);
1a724405 546 goto badsb;
f5971be7 547 }
1a724405 548 statemap = calloc((unsigned)(imax + 1), sizeof(char));
f5971be7
BJ
549 if (statemap == NULL) {
550 printf("cannot alloc %d bytes for statemap\n", imax + 1);
1a724405 551 goto badsb;
f5971be7 552 }
1a724405 553 lncntp = (short *)calloc((unsigned)(imax + 1), sizeof(short));
f5971be7
BJ
554 if (lncntp == NULL) {
555 printf("cannot alloc %d bytes for lncntp\n",
556 (imax + 1) * sizeof(short));
1a724405 557 goto badsb;
f5971be7
BJ
558 }
559 for (c = 0; c < sblock.fs_ncg; c++) {
560 cgd = cgdmin(&sblock, c);
561 if (c == 0) {
562 d = cgbase(&sblock, c);
563 cgd += howmany(sblock.fs_cssize, sblock.fs_fsize);
564 } else
565 d = cgsblock(&sblock, c);
566 for (; d < cgd; d++)
567 setbmap(d);
568 }
569
f5971be7
BJ
570 return (1);
571
572badsb:
573 ckfini();
574 return (0);
1a724405 575# undef altsblock
d2c95d76
BJ
576}
577
578pass1()
579{
580 register int c, i, n, j;
581 register DINODE *dp;
1a724405
KM
582 int ndb, partial;
583 struct inodesc idesc;
584 ino_t inumber;
585
586 bzero((char *)&idesc, sizeof(struct inodesc));
587 idesc.id_type = ADDR;
588 idesc.id_func = pass1check;
589 inumber = 0;
bf541624 590 n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
6e884967 591 for (c = 0; c < sblock.fs_ncg; c++) {
6994bf5d 592 if (getblk(&cgblk, cgtod(&sblock, c), sblock.fs_cgsize) == 0)
6e884967 593 continue;
f5971be7 594 if (cgrp.cg_magic != CG_MAGIC) {
1a724405
KM
595 pfatal("CG %d: BAD MAGIC NUMBER\n", c);
596 bzero((char *)&cgrp, (int)sblock.fs_cgsize);
f5971be7 597 }
6e884967 598 n = 0;
1a724405
KM
599 for (i = 0; i < sblock.fs_ipg; i++, inumber++) {
600 dp = ginode(inumber);
f3c028b7
KM
601 if (dp == NULL)
602 continue;
bf4c734c 603 n++;
6e884967
KM
604 if (ALLOC) {
605 if (!isset(cgrp.cg_iused, i)) {
6994bf5d
KM
606 if (debug)
607 printf("%d bad, not used\n",
1a724405 608 inumber);
6e884967
KM
609 inosumbad++;
610 }
bf4c734c 611 n--;
1a724405 612 lastino = inumber;
af5ba2f8
KM
613 if (!preen && BADBLK &&
614 reply("HOLD BAD BLOCK") == 1) {
615 dp->di_size = sblock.fs_fsize;
616 dp->di_mode = IFREG|0600;
617 inodirty();
618 } else if (ftypeok(dp) == 0)
6bccc723 619 goto unknown;
67272100
KM
620 if (dp->di_size < 0) {
621 if (debug)
622 printf("bad size %d:",
623 dp->di_size);
6bccc723 624 goto unknown;
67272100 625 }
6bccc723
KM
626 ndb = howmany(dp->di_size, sblock.fs_bsize);
627 if (SPECIAL)
628 ndb++;
629 for (j = ndb; j < NDADDR; j++)
67272100
KM
630 if (dp->di_db[j] != 0) {
631 if (debug)
af5ba2f8
KM
632 printf("bad direct addr: %d\n",
633 dp->di_db[j]);
6bccc723 634 goto unknown;
67272100 635 }
6bccc723
KM
636 for (j = 0, ndb -= NDADDR; ndb > 0; j++)
637 ndb /= NINDIR(&sblock);
638 for (; j < NIADDR; j++)
67272100
KM
639 if (dp->di_ib[j] != 0) {
640 if (debug)
af5ba2f8
KM
641 printf("bad indirect addr: %d\n",
642 dp->di_ib[j]);
6bccc723 643 goto unknown;
67272100 644 }
6e884967 645 n_files++;
1a724405 646 lncntp[inumber] = dp->di_nlink;
f5262822 647 if (dp->di_nlink <= 0) {
6e884967 648 if (badlnp < &badlncnt[MAXLNCNT])
1a724405 649 *badlnp++ = inumber;
6e884967
KM
650 else {
651 pfatal("LINK COUNT TABLE OVERFLOW");
652 if (reply("CONTINUE") == 0)
653 errexit("");
654 }
655 }
1a724405
KM
656 statemap[inumber] = DIRCT ? DSTATE : FSTATE;
657 badblk = dupblk = 0; maxblk = 0;
658 idesc.id_number = inumber;
659 idesc.id_filesize = 0;
660 (void)ckinode(dp, &idesc);
661 idesc.id_filesize *= btodb(sblock.fs_fsize);
662 if (dp->di_blocks != idesc.id_filesize) {
5e01ca34 663 pwarn("INCORRECT BLOCK COUNT I=%u (%ld should be %ld)",
1a724405
KM
664 inumber, dp->di_blocks,
665 idesc.id_filesize);
5e01ca34
KM
666 if (preen)
667 printf(" (CORRECTED)\n");
668 else if (reply("CORRECT") == 0)
669 continue;
1a724405 670 dp->di_blocks = idesc.id_filesize;
5e01ca34
KM
671 inodirty();
672 }
6bccc723
KM
673 continue;
674 unknown:
1a724405 675 pfatal("UNKNOWN FILE TYPE I=%u", inumber);
c10dd4b2 676 if (reply("CLEAR") == 1) {
6bccc723
KM
677 zapino(dp);
678 inodirty();
679 inosumbad++;
680 }
6e884967 681 } else {
6e884967 682 if (isset(cgrp.cg_iused, i)) {
6994bf5d
KM
683 if (debug)
684 printf("%d bad, marked used\n",
1a724405 685 inumber);
6e884967 686 inosumbad++;
f3c028b7 687 n--;
6e884967 688 }
6bccc723
KM
689 partial = 0;
690 for (j = 0; j < NDADDR; j++)
691 if (dp->di_db[j] != 0)
692 partial++;
693 for (j = 0; j < NIADDR; j++)
694 if (dp->di_ib[j] != 0)
695 partial++;
696 if (partial || dp->di_mode != 0 ||
697 dp->di_size != 0) {
1a724405
KM
698 pfatal("PARTIALLY ALLOCATED INODE I=%u",
699 inumber);
6e884967
KM
700 if (reply("CLEAR") == 1) {
701 zapino(dp);
702 inodirty();
703 inosumbad++;
704 }
705 }
706 }
707 }
0947395d 708 if (n != cgrp.cg_cs.cs_nifree) {
6994bf5d 709 if (debug)
bf4c734c 710 printf("cg[%d].cg_cs.cs_nifree is %d; calc %d\n",
6994bf5d 711 c, cgrp.cg_cs.cs_nifree, n);
6e884967
KM
712 inosumbad++;
713 }
184d432f
KM
714 if (cgrp.cg_cs.cs_nbfree != sblock.fs_cs(&sblock, c).cs_nbfree
715 || cgrp.cg_cs.cs_nffree != sblock.fs_cs(&sblock, c).cs_nffree
716 || cgrp.cg_cs.cs_nifree != sblock.fs_cs(&sblock, c).cs_nifree
717 || cgrp.cg_cs.cs_ndir != sblock.fs_cs(&sblock, c).cs_ndir)
718 sbsumbad++;
6e884967 719 }
d2c95d76
BJ
720}
721
1a724405
KM
722pass1check(idesc)
723 register struct inodesc *idesc;
d2c95d76
BJ
724{
725 register daddr_t *dlp;
726 int res = KEEPON;
1a724405
KM
727 int anyout, nfrags;
728 daddr_t blkno = idesc->id_blkno;
d2c95d76 729
1a724405
KM
730 anyout = outrange(blkno, idesc->id_numfrags);
731 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
732 if (anyout && outrange(blkno, 1)) {
733 blkerr(idesc->id_number, "BAD", blkno);
d2c95d76 734 if (++badblk >= MAXBAD) {
1a724405
KM
735 pwarn("EXCESSIVE BAD BLKS I=%u",
736 idesc->id_number);
d2c95d76
BJ
737 if (preen)
738 printf(" (SKIPPING)\n");
739 else if (reply("CONTINUE") == 0)
740 errexit("");
741 return (STOP);
742 }
743 res = SKIP;
1a724405
KM
744 } else if (getbmap(blkno)) {
745 blkerr(idesc->id_number, "DUP", blkno);
d2c95d76 746 if (++dupblk >= MAXDUP) {
1a724405
KM
747 pwarn("EXCESSIVE DUP BLKS I=%u",
748 idesc->id_number);
d2c95d76
BJ
749 if (preen)
750 printf(" (SKIPPING)\n");
751 else if (reply("CONTINUE") == 0)
752 errexit("");
753 return (STOP);
754 }
755 if (enddup >= &duplist[DUPTBLSIZE]) {
756 pfatal("DUP TABLE OVERFLOW.");
757 if (reply("CONTINUE") == 0)
758 errexit("");
759 return (STOP);
760 }
761 for (dlp = duplist; dlp < muldup; dlp++)
1a724405
KM
762 if (*dlp == blkno) {
763 *enddup++ = blkno;
d2c95d76
BJ
764 break;
765 }
766 if (dlp >= muldup) {
767 *enddup++ = *muldup;
1a724405 768 *muldup++ = blkno;
f3c028b7 769 }
d2c95d76
BJ
770 } else {
771 n_blks++;
1a724405 772 setbmap(blkno);
d2c95d76 773 }
1a724405 774 idesc->id_filesize++;
d2c95d76
BJ
775 }
776 return (res);
777}
778
779pass1b()
780{
781 register int c, i;
782 register DINODE *dp;
1a724405
KM
783 struct inodesc idesc;
784 ino_t inumber;
d2c95d76 785
1a724405
KM
786 bzero((char *)&idesc, sizeof(struct inodesc));
787 idesc.id_type = ADDR;
788 idesc.id_func = pass1bcheck;
789 inumber = 0;
d2c95d76 790 for (c = 0; c < sblock.fs_ncg; c++) {
1a724405
KM
791 for (i = 0; i < sblock.fs_ipg; i++, inumber++) {
792 dp = ginode(inumber);
d2c95d76
BJ
793 if (dp == NULL)
794 continue;
1a724405
KM
795 idesc.id_number = inumber;
796 if (statemap[inumber] != USTATE &&
797 (ckinode(dp, &idesc) & STOP))
d2c95d76 798 goto out1b;
6e884967
KM
799 }
800 }
801out1b:
f3c028b7 802 flush(&dfile, &inoblk);
d2c95d76
BJ
803}
804
1a724405
KM
805pass1bcheck(idesc)
806 register struct inodesc *idesc;
d2c95d76
BJ
807{
808 register daddr_t *dlp;
1a724405
KM
809 int nfrags, res = KEEPON;
810 daddr_t blkno = idesc->id_blkno;
d2c95d76 811
1a724405
KM
812 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
813 if (outrange(blkno, 1))
d2c95d76
BJ
814 res = SKIP;
815 for (dlp = duplist; dlp < muldup; dlp++)
1a724405
KM
816 if (*dlp == blkno) {
817 blkerr(idesc->id_number, "DUP", blkno);
d2c95d76 818 *dlp = *--muldup;
1a724405 819 *muldup = blkno;
d2c95d76
BJ
820 if (muldup == duplist)
821 return (STOP);
822 }
823 }
824 return (res);
825}
826
827pass2()
828{
829 register DINODE *dp;
1a724405 830 struct inodesc rootdesc;
d2c95d76 831
1a724405
KM
832 bzero((char *)&rootdesc, sizeof(struct inodesc));
833 rootdesc.id_type = ADDR;
834 rootdesc.id_func = pass2check;
835 rootdesc.id_number = ROOTINO;
836 pathp = pathname;
837 switch (statemap[ROOTINO]) {
6e884967
KM
838
839 case USTATE:
840 errexit("ROOT INODE UNALLOCATED. TERMINATING.\n");
841
842 case FSTATE:
843 pfatal("ROOT INODE NOT DIRECTORY");
1a724405 844 if (reply("FIX") == 0 || (dp = ginode(ROOTINO)) == NULL)
6e884967
KM
845 errexit("");
846 dp->di_mode &= ~IFMT;
847 dp->di_mode |= IFDIR;
848 inodirty();
849 inosumbad++;
1a724405 850 statemap[ROOTINO] = DSTATE;
6e884967
KM
851 /* fall into ... */
852
853 case DSTATE:
1a724405 854 descend(&rootdesc, ROOTINO);
6e884967
KM
855 break;
856
857 case CLEAR:
858 pfatal("DUPS/BAD IN ROOT INODE");
859 printf("\n");
860 if (reply("CONTINUE") == 0)
861 errexit("");
1a724405
KM
862 statemap[ROOTINO] = DSTATE;
863 descend(&rootdesc, ROOTINO);
6e884967 864 }
d2c95d76
BJ
865}
866
1a724405
KM
867pass2check(idesc)
868 struct inodesc *idesc;
d2c95d76 869{
1a724405
KM
870 register DIRECT *dirp = idesc->id_dirp;
871 char *curpathloc;
872 int n, entrysize, ret = 0;
d2c95d76 873 DINODE *dp;
1a724405 874 DIRECT proto;
d2c95d76 875
1a724405
KM
876 /*
877 * check for "."
878 */
879 if (idesc->id_entryno != 0)
880 goto chk1;
881 if (dirp->d_ino != 0 && dirp->d_namlen == 1 && dirp->d_name[0] == '.') {
882 if (dirp->d_ino != idesc->id_number) {
883 direrr(idesc->id_number, "BAD INODE NUMBER FOR '.'");
884 dirp->d_ino = idesc->id_number;
885 ret |= dofix(idesc);
886 }
887 goto chk1;
888 }
889 direrr(idesc->id_number, "MISSING '.'");
890 proto.d_ino = idesc->id_number;
891 proto.d_namlen = 1;
892 (void)strcpy(proto.d_name, ".");
893 entrysize = DIRSIZ(&proto);
894 if (dirp->d_ino != 0) {
895 pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
896 dirp->d_name);
897 } else if (dirp->d_reclen < entrysize) {
898 pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
899 } else if (dirp->d_reclen < 2 * entrysize) {
900 proto.d_reclen = dirp->d_reclen;
901 bcopy((char *)&proto, (char *)dirp, entrysize);
902 ret |= dofix(idesc);
903 } else {
904 n = dirp->d_reclen - entrysize;
905 proto.d_reclen = entrysize;
906 bcopy((char *)&proto, (char *)dirp, entrysize);
907 idesc->id_entryno++;
908 lncntp[dirp->d_ino]--;
909 dirp = (DIRECT *)((char *)(dirp) + entrysize);
910 bzero((char *)dirp, n);
911 dirp->d_reclen = n;
912 ret |= dofix(idesc);
913 }
914chk1:
915 if (idesc->id_entryno > 1)
916 goto chk2;
917 proto.d_ino = idesc->id_parent;
918 proto.d_namlen = 2;
919 (void)strcpy(proto.d_name, "..");
920 entrysize = DIRSIZ(&proto);
921 if (idesc->id_entryno == 0) {
922 n = DIRSIZ(dirp);
923 if (dirp->d_reclen < n + entrysize)
924 goto chk2;
925 proto.d_reclen = dirp->d_reclen - n;
926 dirp->d_reclen = n;
927 idesc->id_entryno++;
928 lncntp[dirp->d_ino]--;
929 dirp = (DIRECT *)((char *)(dirp) + n);
930 bzero((char *)dirp, n);
931 dirp->d_reclen = n;
932 }
933 if (dirp->d_ino != 0 && dirp->d_namlen == 2 &&
934 strcmp(dirp->d_name, "..") == 0) {
935 if (dirp->d_ino != idesc->id_parent) {
936 direrr(idesc->id_number, "BAD INODE NUMBER FOR '..'");
937 dirp->d_ino = idesc->id_parent;
938 ret |= dofix(idesc);
939 }
940 goto chk2;
941 }
942 direrr(idesc->id_number, "MISSING '..'");
943 if (dirp->d_ino != 0) {
944 pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
945 dirp->d_name);
946 } else if (dirp->d_reclen < entrysize) {
947 pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
948 } else {
949 proto.d_reclen = dirp->d_reclen;
950 bcopy((char *)&proto, (char *)dirp, entrysize);
951 ret |= dofix(idesc);
952 }
953chk2:
954 if (dirp->d_ino == 0)
955 return (ret|KEEPON);
956 if (idesc->id_entryno >= 2 &&
957 dirp->d_namlen <= 2 &&
958 dirp->d_name[0] == '.') {
959 if (dirp->d_namlen == 1) {
960 direrr(idesc->id_number, "EXTRA '.' ENTRY");
961 dirp->d_ino = 0;
962 return (KEEPON | dofix(idesc));
963 }
964 if (dirp->d_name[1] == '.') {
965 direrr(idesc->id_number, "EXTRA '..' ENTRY");
966 dirp->d_ino = 0;
967 return (KEEPON | dofix(idesc));
968 }
969 }
970 curpathloc = pathp;
971 *pathp++ = '/';
aac37e2e
KM
972 if (pathp + dirp->d_namlen >= endpathname) {
973 *pathp = '\0';
974 errexit("NAME TOO LONG %s%s\n", pathname, dirp->d_name);
975 }
1a724405
KM
976 bcopy(dirp->d_name, pathp, dirp->d_namlen + 1);
977 pathp += dirp->d_namlen;
978 idesc->id_entryno++;
d2c95d76 979 n = 0;
1a724405
KM
980 if (dirp->d_ino > imax || dirp->d_ino <= 0) {
981 direrr(dirp->d_ino, "I OUT OF RANGE");
982 n = reply("REMOVE");
983 } else {
d2c95d76 984again:
1a724405 985 switch (statemap[dirp->d_ino]) {
d2c95d76 986 case USTATE:
1a724405
KM
987 direrr(dirp->d_ino, "UNALLOCATED");
988 n = reply("REMOVE");
d2c95d76
BJ
989 break;
990
991 case CLEAR:
1a724405
KM
992 direrr(dirp->d_ino, "DUP/BAD");
993 if ((n = reply("REMOVE")) == 1)
d2c95d76 994 break;
1a724405 995 if ((dp = ginode(dirp->d_ino)) == NULL)
d2c95d76 996 break;
1a724405 997 statemap[dirp->d_ino] = DIRCT ? DSTATE : FSTATE;
d2c95d76
BJ
998 goto again;
999
1000 case FSTATE:
1a724405 1001 lncntp[dirp->d_ino]--;
d2c95d76
BJ
1002 break;
1003
1004 case DSTATE:
1a724405
KM
1005 descend(idesc, dirp->d_ino);
1006 if (statemap[dirp->d_ino] != CLEAR) {
1007 lncntp[dirp->d_ino]--;
1008 } else {
1009 dirp->d_ino = 0;
1010 ret |= ALTERED;
1011 }
d2c95d76
BJ
1012 break;
1013 }
1014 }
1a724405
KM
1015 pathp = curpathloc;
1016 *pathp = '\0';
d2c95d76 1017 if (n == 0)
1a724405 1018 return (ret|KEEPON);
d2c95d76 1019 dirp->d_ino = 0;
1a724405 1020 return (ret|KEEPON|ALTERED);
d2c95d76
BJ
1021}
1022
1023pass3()
1024{
d2c95d76 1025 register DINODE *dp;
1a724405
KM
1026 struct inodesc idesc;
1027 ino_t inumber, orphan;
1028 int len;
1029
1030 bzero((char *)&idesc, sizeof(struct inodesc));
1031 idesc.id_type = DATA;
1032 for (inumber = ROOTINO; inumber <= lastino; inumber++) {
1033 if (statemap[inumber] == DSTATE) {
bec6e7fb
KM
1034 pathp = pathname;
1035 *pathp++ = '?';
1036 *pathp = '\0';
1a724405 1037 idesc.id_func = findino;
6e884967 1038 srchname = "..";
1a724405 1039 idesc.id_parent = inumber;
6e884967 1040 do {
1a724405
KM
1041 orphan = idesc.id_parent;
1042 if ((dp = ginode(orphan)) == NULL)
6e884967 1043 break;
1a724405
KM
1044 idesc.id_parent = 0;
1045 idesc.id_filesize = dp->di_size;
1046 idesc.id_number = orphan;
1047 (void)ckinode(dp, &idesc);
1048 if (idesc.id_parent == 0)
6e884967 1049 break;
1a724405
KM
1050 } while (statemap[idesc.id_parent] == DSTATE);
1051 if (linkup(orphan, idesc.id_parent) == 1) {
1a724405
KM
1052 idesc.id_func = pass2check;
1053 idesc.id_number = lfdir;
1054 descend(&idesc, orphan);
6e884967 1055 }
6e884967
KM
1056 }
1057 }
d2c95d76 1058}
6e884967 1059
d2c95d76
BJ
1060pass4()
1061{
1a724405
KM
1062 register ino_t inumber, *blp;
1063 int n;
1064 struct inodesc idesc;
d2c95d76 1065
1a724405
KM
1066 bzero((char *)&idesc, sizeof(struct inodesc));
1067 idesc.id_type = ADDR;
1068 idesc.id_func = pass4check;
1069 for (inumber = ROOTINO; inumber <= lastino; inumber++) {
1070 idesc.id_number = inumber;
1071 switch (statemap[inumber]) {
d2c95d76
BJ
1072
1073 case FSTATE:
1a724405 1074 n = lncntp[inumber];
f5262822 1075 if (n)
1a724405 1076 adjust(&idesc, (short)n);
6e884967
KM
1077 else {
1078 for (blp = badlncnt;blp < badlnp; blp++)
1a724405
KM
1079 if (*blp == inumber) {
1080 clri(&idesc, "UNREF", 1);
6e884967
KM
1081 break;
1082 }
1083 }
1084 break;
1085
1086 case DSTATE:
1a724405 1087 clri(&idesc, "UNREF", 1);
6e884967
KM
1088 break;
1089
1090 case CLEAR:
1a724405 1091 clri(&idesc, "BAD/DUP", 1);
6e884967
KM
1092 break;
1093 }
1094 }
8e5c1c7c 1095 if (imax - ROOTINO - n_files != sblock.fs_cstotal.cs_nifree) {
6e884967
KM
1096 pwarn("FREE INODE COUNT WRONG IN SUPERBLK");
1097 if (preen)
1098 printf(" (FIXED)\n");
1099 if (preen || reply("FIX") == 1) {
bf4c734c 1100 sblock.fs_cstotal.cs_nifree = imax - ROOTINO - n_files;
6e884967
KM
1101 sbdirty();
1102 }
1103 }
1104 flush(&dfile, &fileblk);
d2c95d76 1105}
6e884967 1106
1a724405
KM
1107pass4check(idesc)
1108 register struct inodesc *idesc;
d2c95d76
BJ
1109{
1110 register daddr_t *dlp;
1a724405
KM
1111 int nfrags, res = KEEPON;
1112 daddr_t blkno = idesc->id_blkno;
d2c95d76 1113
1a724405
KM
1114 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
1115 if (outrange(blkno, 1))
d2c95d76 1116 res = SKIP;
1a724405 1117 else if (getbmap(blkno)) {
d2c95d76 1118 for (dlp = duplist; dlp < enddup; dlp++)
1a724405 1119 if (*dlp == blkno) {
d2c95d76
BJ
1120 *dlp = *--enddup;
1121 return (KEEPON);
1122 }
1a724405 1123 clrbmap(blkno);
d2c95d76
BJ
1124 n_blks--;
1125 }
1126 }
1127 return (res);
1128}
1129
1130pass5()
1131{
1132 register int c, n, i, b, d;
f5971be7
BJ
1133 short bo[MAXCPG][NRPOS];
1134 long botot[MAXCPG];
1135 long frsum[MAXFRAG];
1136 int blk;
1137 daddr_t cbase;
1138 int blockbits = (1<<sblock.fs_frag)-1;
d2c95d76 1139
0db712b3 1140 bcopy(blockmap, freemap, (unsigned)bmapsz);
6e884967 1141 dupblk = 0;
6994bf5d 1142 n_index = sblock.fs_ncg * (cgdmin(&sblock, 0) - cgtod(&sblock, 0));
6e884967 1143 for (c = 0; c < sblock.fs_ncg; c++) {
f5971be7 1144 cbase = cgbase(&sblock, c);
1a724405
KM
1145 bzero((char *)botot, sizeof (botot));
1146 bzero((char *)bo, sizeof (bo));
1147 bzero((char *)frsum, sizeof (frsum));
f3c028b7 1148 /*
bf541624 1149 * need to account for the super blocks
f3c028b7
KM
1150 * which appear (inaccurately) bad
1151 */
bf541624 1152 n_bad += cgtod(&sblock, c) - cgsblock(&sblock, c);
6994bf5d 1153 if (getblk(&cgblk, cgtod(&sblock, c), sblock.fs_cgsize) == 0)
6e884967 1154 continue;
f5971be7 1155 if (cgrp.cg_magic != CG_MAGIC) {
1a724405
KM
1156 pfatal("CG %d: BAD MAGIC NUMBER\n", c);
1157 bzero((char *)&cgrp, (int)sblock.fs_cgsize);
f5971be7 1158 }
b6407c9d 1159 for (b = 0; b < sblock.fs_fpg; b += sblock.fs_frag) {
f5971be7
BJ
1160 blk = blkmap(&sblock, cgrp.cg_free, b);
1161 if (blk == 0)
1162 continue;
1163 if (blk == blockbits) {
d2c95d76 1164 if (pass5check(cbase+b, sblock.fs_frag) == STOP)
6e884967
KM
1165 goto out5;
1166 /* this is clumsy ... */
d2c95d76
BJ
1167 n_ffree -= sblock.fs_frag;
1168 n_bfree++;
1169 botot[cbtocylno(&sblock, b)]++;
1170 bo[cbtocylno(&sblock, b)]
1171 [cbtorpos(&sblock, b)]++;
f5971be7 1172 continue;
d2c95d76 1173 }
f5971be7
BJ
1174 for (d = 0; d < sblock.fs_frag; d++)
1175 if ((blk & (1<<d)) &&
1a724405 1176 pass5check(cbase + b + d, (long)1) == STOP)
f5971be7
BJ
1177 goto out5;
1178 fragacct(&sblock, blk, frsum, 1);
6e884967 1179 }
1a724405 1180 if (bcmp((char *)cgrp.cg_frsum, (char *)frsum, sizeof(frsum))) {
f5971be7
BJ
1181 if (debug)
1182 for (i = 0; i < sblock.fs_frag; i++)
1183 if (cgrp.cg_frsum[i] != frsum[i])
1184 printf("cg[%d].cg_frsum[%d] have %d calc %d\n",
1185 c, i, cgrp.cg_frsum[i], frsum[i]);
1186 frsumbad++;
d2c95d76 1187 }
1a724405 1188 if (bcmp((char *)cgrp.cg_btot, (char *)botot, sizeof (botot))) {
f5971be7
BJ
1189 if (debug)
1190 for (n = 0; n < sblock.fs_cpg; n++)
1191 if (botot[n] != cgrp.cg_btot[n])
1192 printf("cg[%d].cg_btot[%d] have %d calc %d\n",
1193 c, n, cgrp.cg_btot[n], botot[n]);
1194 offsumbad++;
1195 }
1a724405 1196 if (bcmp((char *)cgrp.cg_b, (char *)bo, sizeof (bo))) {
f5971be7 1197 if (debug)
d2c95d76 1198 for (i = 0; i < NRPOS; i++)
f5971be7
BJ
1199 if (bo[n][i] != cgrp.cg_b[n][i])
1200 printf("cg[%d].cg_b[%d][%d] have %d calc %d\n",
1201 c, n, i, cgrp.cg_b[n][i], bo[n][i]);
1202 offsumbad++;
6e884967
KM
1203 }
1204 }
d2c95d76
BJ
1205out5:
1206 if (dupblk)
1207 pwarn("%d DUP BLKS IN BIT MAPS\n", dupblk);
1208 if (fixcg == 0) {
1209 if ((b = n_blks+n_ffree+sblock.fs_frag*n_bfree+n_index+n_bad) != fmax) {
1210 pwarn("%ld BLK(S) MISSING\n", fmax - b);
1211 fixcg = 1;
1212 } else if (inosumbad + offsumbad + frsumbad + sbsumbad) {
1213 pwarn("SUMMARY INFORMATION %s%s%s%sBAD\n",
1214 inosumbad ? "(INODE FREE) " : "",
1215 offsumbad ? "(BLOCK OFFSETS) " : "",
1216 frsumbad ? "(FRAG SUMMARIES) " : "",
1217 sbsumbad ? "(SUPER BLOCK SUMMARIES) " : "");
1218 fixcg = 1;
1219 } else if (n_ffree != sblock.fs_cstotal.cs_nffree ||
1220 n_bfree != sblock.fs_cstotal.cs_nbfree) {
1221 pwarn("FREE BLK COUNT(S) WRONG IN SUPERBLK");
1222 if (preen)
1223 printf(" (FIXED)\n");
1224 if (preen || reply("FIX") == 1) {
1225 sblock.fs_cstotal.cs_nffree = n_ffree;
1226 sblock.fs_cstotal.cs_nbfree = n_bfree;
1227 sbdirty();
1228 }
1229 }
1230 }
1231 if (fixcg) {
1232 pwarn("BAD CYLINDER GROUPS");
1233 if (preen)
1234 printf(" (SALVAGED)\n");
1235 else if (reply("SALVAGE") == 0)
1236 fixcg = 0;
1237 }
6e884967
KM
1238}
1239
d2c95d76 1240pass5check(blk, size)
6e884967 1241 daddr_t blk;
1a724405 1242 long size;
6e884967 1243{
6e884967 1244
1a724405 1245 if (outrange(blk, (int)size)) {
f5971be7
BJ
1246 fixcg = 1;
1247 if (preen)
1248 pfatal("BAD BLOCKS IN BIT MAPS.");
1249 if (++badblk >= MAXBAD) {
1250 printf("EXCESSIVE BAD BLKS IN BIT MAPS.");
1251 if (reply("CONTINUE") == 0)
1252 errexit("");
1253 return (STOP);
1254 }
1255 }
1256 for (; size > 0; blk++, size--)
1257 if (getfmap(blk)) {
6e884967 1258 fixcg = 1;
7ebb5f4e 1259 ++dupblk;
6e884967
KM
1260 } else {
1261 n_ffree++;
1262 setfmap(blk);
1263 }
f5971be7 1264 return (KEEPON);
6e884967
KM
1265}
1266
1a724405 1267ckinode(dp, idesc)
d2c95d76 1268 DINODE *dp;
1a724405 1269 register struct inodesc *idesc;
d2c95d76
BJ
1270{
1271 register daddr_t *ap;
1a724405 1272 int ret, n, ndb, offset;
d2c95d76
BJ
1273 DINODE dino;
1274
1275 if (SPECIAL)
1276 return (KEEPON);
1277 dino = *dp;
1a724405
KM
1278 idesc->id_fix = DONTKNOW;
1279 idesc->id_entryno = 0;
d2c95d76
BJ
1280 ndb = howmany(dino.di_size, sblock.fs_bsize);
1281 for (ap = &dino.di_db[0]; ap < &dino.di_db[NDADDR]; ap++) {
1282 if (--ndb == 0 && (offset = blkoff(&sblock, dino.di_size)) != 0)
1a724405
KM
1283 idesc->id_numfrags =
1284 numfrags(&sblock, fragroundup(&sblock, offset));
d2c95d76 1285 else
1a724405
KM
1286 idesc->id_numfrags = sblock.fs_frag;
1287 if (*ap == 0)
1288 continue;
1289 idesc->id_blkno = *ap;
1290 if (idesc->id_type == ADDR)
1291 ret = (*idesc->id_func)(idesc);
1292 else
1293 ret = dirscan(idesc);
1294 if (ret & STOP)
d2c95d76
BJ
1295 return (ret);
1296 }
1a724405 1297 idesc->id_numfrags = sblock.fs_frag;
d2c95d76 1298 for (ap = &dino.di_ib[0], n = 1; n <= 2; ap++, n++) {
d2c95d76 1299 if (*ap) {
1a724405
KM
1300 idesc->id_blkno = *ap;
1301 ret = iblock(idesc, n,
1302 dino.di_size - sblock.fs_bsize * NDADDR);
d2c95d76
BJ
1303 if (ret & STOP)
1304 return (ret);
1305 }
1306 }
1307 return (KEEPON);
1308}
1309
1a724405
KM
1310iblock(idesc, ilevel, isize)
1311 struct inodesc *idesc;
d2c95d76 1312 register ilevel;
1a724405 1313 long isize;
d2c95d76
BJ
1314{
1315 register daddr_t *ap;
1316 register daddr_t *aplim;
1a724405 1317 int i, n, (*func)(), nif;
d2c95d76
BJ
1318 BUFAREA ib;
1319
1a724405
KM
1320 if (idesc->id_type == ADDR) {
1321 func = idesc->id_func;
1322 if (((n = (*func)(idesc)) & KEEPON) == 0)
d2c95d76
BJ
1323 return (n);
1324 } else
1325 func = dirscan;
1a724405 1326 if (outrange(idesc->id_blkno, idesc->id_numfrags)) /* protect thyself */
d2c95d76
BJ
1327 return (SKIP);
1328 initbarea(&ib);
1a724405 1329 if (getblk(&ib, idesc->id_blkno, sblock.fs_bsize) == NULL)
d2c95d76
BJ
1330 return (SKIP);
1331 ilevel--;
1332 if (ilevel == 0) {
1333 nif = lblkno(&sblock, isize) + 1;
1334 } else /* ilevel == 1 */ {
1335 nif = isize / (sblock.fs_bsize * NINDIR(&sblock)) + 1;
1336 }
1337 if (nif > NINDIR(&sblock))
1338 nif = NINDIR(&sblock);
1339 aplim = &ib.b_un.b_indir[nif];
1340 for (ap = ib.b_un.b_indir, i = 1; ap < aplim; ap++, i++)
1341 if (*ap) {
1a724405 1342 idesc->id_blkno = *ap;
d2c95d76 1343 if (ilevel > 0)
1a724405 1344 n = iblock(idesc, ilevel,
d2c95d76
BJ
1345 isize - i*NINDIR(&sblock)*sblock.fs_bsize);
1346 else
1a724405 1347 n = (*func)(idesc);
d2c95d76
BJ
1348 if (n & STOP)
1349 return (n);
1350 }
1351 return (KEEPON);
1352}
1353
f5971be7 1354outrange(blk, cnt)
6e884967 1355 daddr_t blk;
f5971be7 1356 int cnt;
6e884967
KM
1357{
1358 register int c;
1359
f5971be7 1360 if ((unsigned)(blk+cnt) > fmax)
6e884967 1361 return (1);
f5971be7
BJ
1362 c = dtog(&sblock, blk);
1363 if (blk < cgdmin(&sblock, c)) {
1364 if ((blk+cnt) > cgsblock(&sblock, c)) {
1365 if (debug) {
1366 printf("blk %d < cgdmin %d;",
1367 blk, cgdmin(&sblock, c));
1368 printf(" blk+cnt %d > cgsbase %d\n",
1369 blk+cnt, cgsblock(&sblock, c));
1370 }
1371 return (1);
1372 }
1373 } else {
1374 if ((blk+cnt) > cgbase(&sblock, c+1)) {
1375 if (debug) {
1376 printf("blk %d >= cgdmin %d;",
1377 blk, cgdmin(&sblock, c));
1378 printf(" blk+cnt %d > sblock.fs_fpg %d\n",
1379 blk+cnt, sblock.fs_fpg);
1380 }
1381 return (1);
1382 }
07670f7d 1383 }
6e884967
KM
1384 return (0);
1385}
1386
1a724405
KM
1387blkerr(ino, s, blk)
1388 ino_t ino;
6e884967 1389 char *s;
1a724405 1390 daddr_t blk;
6e884967 1391{
f5262822 1392
1a724405 1393 pfatal("%ld %s I=%u", blk, s, ino);
6e884967 1394 printf("\n");
1a724405 1395 statemap[ino] = CLEAR;
6e884967
KM
1396}
1397
1a724405
KM
1398descend(parentino, inumber)
1399 struct inodesc *parentino;
1400 ino_t inumber;
6e884967
KM
1401{
1402 register DINODE *dp;
1a724405 1403 struct inodesc curino;
6e884967 1404
1a724405
KM
1405 bzero((char *)&curino, sizeof(struct inodesc));
1406 statemap[inumber] = FSTATE;
1407 if ((dp = ginode(inumber)) == NULL)
6e884967 1408 return;
1a724405
KM
1409 if (dp->di_size == 0) {
1410 direrr(inumber, "ZERO LENGTH DIRECTORY");
1411 if (reply("REMOVE") == 1)
1412 statemap[inumber] = CLEAR;
1413 return;
1414 }
1415 if (dp->di_size < MINDIRSIZE) {
1416 direrr(inumber, "DIRECTORY TOO SHORT");
1417 dp->di_size = MINDIRSIZE;
1418 if (reply("FIX") == 1)
1419 inodirty();
1420 }
1421 curino.id_type = DATA;
1422 curino.id_func = parentino->id_func;
1423 curino.id_parent = parentino->id_number;
1424 curino.id_number = inumber;
1425 curino.id_filesize = dp->di_size;
1426 (void)ckinode(dp, &curino);
6e884967
KM
1427}
1428
1a724405
KM
1429dirscan(idesc)
1430 register struct inodesc *idesc;
6e884967 1431{
24a31719 1432 register DIRECT *dp;
1a724405
KM
1433 int dsize, n;
1434 long blksiz;
754cadb5 1435 char dbuf[DIRBLKSIZ];
6e884967 1436
1a724405
KM
1437 if (idesc->id_type != DATA)
1438 errexit("wrong type to dirscan %d\n", idesc->id_type);
1439 blksiz = idesc->id_numfrags * sblock.fs_fsize;
1440 if (outrange(idesc->id_blkno, idesc->id_numfrags)) {
1441 idesc->id_filesize -= blksiz;
6e884967
KM
1442 return (SKIP);
1443 }
1a724405
KM
1444 idesc->id_loc = 0;
1445 for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) {
754cadb5 1446 dsize = dp->d_reclen;
1a724405
KM
1447 bcopy((char *)dp, dbuf, dsize);
1448 idesc->id_dirp = (DIRECT *)dbuf;
1449 if ((n = (*idesc->id_func)(idesc)) & ALTERED) {
1450 if (getblk(&fileblk, idesc->id_blkno, blksiz) != NULL) {
1451 bcopy(dbuf, (char *)dp, dsize);
8ebf61ca 1452 dirty(&fileblk);
6e884967
KM
1453 sbdirty();
1454 } else
1a724405 1455 n &= ~ALTERED;
6e884967 1456 }
24a31719 1457 if (n & STOP)
6e884967
KM
1458 return (n);
1459 }
1a724405 1460 return (idesc->id_filesize > 0 ? KEEPON : STOP);
6e884967
KM
1461}
1462
24a31719
KM
1463/*
1464 * get next entry in a directory.
1465 */
1466DIRECT *
1a724405
KM
1467fsck_readdir(idesc)
1468 register struct inodesc *idesc;
24a31719 1469{
754cadb5 1470 register DIRECT *dp, *ndp;
1a724405 1471 long size, blksiz;
24a31719 1472
1a724405
KM
1473 blksiz = idesc->id_numfrags * sblock.fs_fsize;
1474 if (getblk(&fileblk, idesc->id_blkno, blksiz) == NULL) {
1475 idesc->id_filesize -= blksiz - idesc->id_loc;
24a31719
KM
1476 return NULL;
1477 }
1a724405
KM
1478 if (idesc->id_loc % DIRBLKSIZ == 0 && idesc->id_filesize > 0 &&
1479 idesc->id_loc < blksiz) {
1480 dp = (DIRECT *)(dirblk.b_buf + idesc->id_loc);
1481 if (dircheck(idesc, dp))
1482 goto dpok;
1483 idesc->id_loc += DIRBLKSIZ;
1484 idesc->id_filesize -= DIRBLKSIZ;
690f77ba
KM
1485 dp->d_reclen = DIRBLKSIZ;
1486 dp->d_ino = 0;
1487 dp->d_namlen = 0;
1a724405
KM
1488 dp->d_name[0] = '\0';
1489 if (idesc->id_fix == DONTKNOW)
1490 direrr(idesc->id_number, "DIRECTORY CORRUPTED");
1491 if (dofix(idesc))
1492 dirty(&fileblk);
1493 return (dp);
690f77ba 1494 }
1a724405
KM
1495dpok:
1496 if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz)
690f77ba 1497 return NULL;
1a724405
KM
1498 dp = (DIRECT *)(dirblk.b_buf + idesc->id_loc);
1499 idesc->id_loc += dp->d_reclen;
1500 idesc->id_filesize -= dp->d_reclen;
1501 ndp = (DIRECT *)(dirblk.b_buf + idesc->id_loc);
1502 if ((idesc->id_filesize <= 0 && idesc->id_loc % DIRBLKSIZ != 0) ||
1503 (idesc->id_loc < blksiz && idesc->id_filesize > 0 &&
1504 dircheck(idesc, ndp) == 0)) {
1505 size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
1506 dp->d_reclen += size;
1507 idesc->id_loc += size;
1508 idesc->id_filesize -= size;
1509 if (idesc->id_fix == DONTKNOW)
1510 direrr(idesc->id_number, "DIRECTORY CORRUPTED");
1511 if (dofix(idesc))
690f77ba 1512 dirty(&fileblk);
24a31719 1513 }
690f77ba 1514 return (dp);
24a31719
KM
1515}
1516
1a724405
KM
1517/*
1518 * Verify that a directory entry is valid.
1519 * This is a superset of the checks made in the kernel.
1520 */
1521dircheck(idesc, dp)
1522 struct inodesc *idesc;
1523 register DIRECT *dp;
1524{
1525 register int size;
1526 register char *cp;
1527 int spaceleft;
1528
1529 size = DIRSIZ(dp);
1530 spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
1531 if (dp->d_ino < imax &&
1532 dp->d_reclen != 0 &&
1533 dp->d_reclen <= spaceleft &&
1534 (dp->d_reclen & 0x3) == 0 &&
1535 dp->d_reclen >= size &&
1536 idesc->id_filesize >= size &&
1537 dp->d_namlen <= MAXNAMLEN) {
1538 if (dp->d_ino == 0)
1539 return (1);
1540 for (cp = dp->d_name, size = 0; size < dp->d_namlen; size++)
1541 if (*cp == 0 || (*cp++ & 0200))
1542 return (0);
1543 if (*cp == 0)
1544 return (1);
1545 }
1546 return (0);
1547}
1548
1549direrr(ino, s)
1550 ino_t ino;
8ebf61ca 1551 char *s;
6e884967
KM
1552{
1553 register DINODE *dp;
1554
1555 pwarn("%s ", s);
1a724405 1556 pinode(ino);
6e884967 1557 printf("\n");
1a724405
KM
1558 if ((dp = ginode(ino)) != NULL && ftypeok(dp))
1559 pfatal("%s=%s\n", DIRCT?"DIR":"FILE", pathname);
6e884967 1560 else
1a724405 1561 pfatal("NAME=%s\n", pathname);
6e884967
KM
1562}
1563
1a724405
KM
1564adjust(idesc, lcnt)
1565 register struct inodesc *idesc;
1566 short lcnt;
6e884967
KM
1567{
1568 register DINODE *dp;
1569
1a724405 1570 if ((dp = ginode(idesc->id_number)) == NULL)
6e884967
KM
1571 return;
1572 if (dp->di_nlink == lcnt) {
1a724405
KM
1573 if (linkup(idesc->id_number, (ino_t)0) == 0)
1574 clri(idesc, "UNREF", 0);
6e884967
KM
1575 }
1576 else {
1577 pwarn("LINK COUNT %s",
1a724405
KM
1578 (lfdir==idesc->id_number)?lfname:(DIRCT?"DIR":"FILE"));
1579 pinode(idesc->id_number);
6e884967
KM
1580 printf(" COUNT %d SHOULD BE %d",
1581 dp->di_nlink, dp->di_nlink-lcnt);
1582 if (preen) {
1583 if (lcnt < 0) {
1584 printf("\n");
1585 preendie();
1586 }
1587 printf(" (ADJUSTED)\n");
1588 }
1589 if (preen || reply("ADJUST") == 1) {
1590 dp->di_nlink -= lcnt;
1591 inodirty();
1592 }
1593 }
1594}
1595
1a724405
KM
1596clri(idesc, s, flg)
1597 register struct inodesc *idesc;
8ebf61ca 1598 char *s;
1a724405 1599 int flg;
6e884967
KM
1600{
1601 register DINODE *dp;
1602
1a724405 1603 if ((dp = ginode(idesc->id_number)) == NULL)
6e884967
KM
1604 return;
1605 if (flg == 1) {
24a31719 1606 pwarn("%s %s", s, DIRCT?"DIR":"FILE");
1a724405 1607 pinode(idesc->id_number);
6e884967
KM
1608 }
1609 if (preen || reply("CLEAR") == 1) {
1610 if (preen)
1611 printf(" (CLEARED)\n");
1612 n_files--;
1a724405 1613 (void)ckinode(dp, idesc);
6e884967 1614 zapino(dp);
1a724405 1615 statemap[idesc->id_number] = USTATE;
6e884967
KM
1616 inodirty();
1617 inosumbad++;
1618 }
1619}
1620
6e884967
KM
1621badsb(s)
1622 char *s;
1623{
1624
1625 if (preen)
1626 printf("%s: ", devname);
1627 printf("BAD SUPER BLOCK: %s\n", s);
1628 pwarn("USE -b OPTION TO FSCK TO SPECIFY LOCATION OF AN ALTERNATE\n");
1629 pfatal("SUPER-BLOCK TO SUPPLY NEEDED INFORMATION; SEE fsck(8).\n");
1630}
1631
1632DINODE *
1a724405
KM
1633ginode(inumber)
1634 ino_t inumber;
6e884967
KM
1635{
1636 daddr_t iblk;
1a724405 1637 static ino_t startinum = 0; /* blk num of first in raw area */
6e884967 1638
1a724405
KM
1639
1640 if (inumber < ROOTINO || inumber > imax) {
1641 if (debug && inumber > imax)
1642 printf("inumber out of range (%d)\n", inumber);
6e884967 1643 return (NULL);
f3385032 1644 }
1a724405
KM
1645 if (startinum == 0 ||
1646 inumber < startinum || inumber >= startinum + INOPB(&sblock)) {
1647 iblk = itod(&sblock, inumber);
b6407c9d 1648 if (getblk(&inoblk, iblk, sblock.fs_bsize) == NULL) {
6e884967
KM
1649 return (NULL);
1650 }
1a724405 1651 startinum = (inumber / INOPB(&sblock)) * INOPB(&sblock);
6e884967 1652 }
1a724405 1653 return (&inoblk.b_un.b_dinode[inumber % INOPB(&sblock)]);
6e884967
KM
1654}
1655
1656ftypeok(dp)
1657 DINODE *dp;
1658{
1659 switch (dp->di_mode & IFMT) {
1660
1661 case IFDIR:
1662 case IFREG:
1663 case IFBLK:
1664 case IFCHR:
ea47352d 1665 case IFLNK:
c10dd4b2 1666 case IFSOCK:
6e884967
KM
1667 return (1);
1668
1669 default:
67272100
KM
1670 if (debug)
1671 printf("bad file type 0%o\n", dp->di_mode);
6e884967
KM
1672 return (0);
1673 }
1674}
1675
1676reply(s)
1677 char *s;
1678{
1679 char line[80];
1680
1681 if (preen)
1682 pfatal("INTERNAL ERROR: GOT TO reply()");
1683 rplyflag = 1;
1684 printf("\n%s? ", s);
1685 if (nflag || dfile.wfdes < 0) {
1686 printf(" no\n\n");
1687 return (0);
1688 }
1689 if (yflag) {
1690 printf(" yes\n\n");
1691 return (1);
1692 }
1693 if (getline(stdin, line, sizeof(line)) == EOF)
1694 errexit("\n");
1695 printf("\n");
1696 if (line[0] == 'y' || line[0] == 'Y')
1697 return (1);
1698 else
1699 return (0);
1700}
1701
1702getline(fp, loc, maxlen)
1703 FILE *fp;
1704 char *loc;
1705{
1706 register n;
1707 register char *p, *lastloc;
1708
1709 p = loc;
1710 lastloc = &p[maxlen-1];
1711 while ((n = getc(fp)) != '\n') {
1712 if (n == EOF)
1713 return (EOF);
1714 if (!isspace(n) && p < lastloc)
1715 *p++ = n;
1716 }
1717 *p = 0;
1718 return (p - loc);
1719}
1720
1721BUFAREA *
1722getblk(bp, blk, size)
6e884967 1723 register BUFAREA *bp;
1a724405
KM
1724 daddr_t blk;
1725 long size;
6e884967
KM
1726{
1727 register struct filecntl *fcp;
b6407c9d 1728 daddr_t dblk;
6e884967
KM
1729
1730 fcp = &dfile;
b6407c9d
KM
1731 dblk = fsbtodb(&sblock, blk);
1732 if (bp->b_bno == dblk)
6e884967
KM
1733 return (bp);
1734 flush(fcp, bp);
b6407c9d
KM
1735 if (bread(fcp, bp->b_un.b_buf, dblk, size) != 0) {
1736 bp->b_bno = dblk;
6e884967
KM
1737 bp->b_size = size;
1738 return (bp);
1739 }
1740 bp->b_bno = (daddr_t)-1;
1741 return (NULL);
1742}
1743
1744flush(fcp, bp)
1745 struct filecntl *fcp;
1746 register BUFAREA *bp;
1747{
1748
1749 if (bp->b_dirty)
1a724405 1750 (void)bwrite(fcp, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
6e884967
KM
1751 bp->b_dirty = 0;
1752}
1753
1754rwerr(s, blk)
1755 char *s;
1756 daddr_t blk;
1757{
1758
1759 if (preen == 0)
1760 printf("\n");
1761 pfatal("CANNOT %s: BLK %ld", s, blk);
1762 if (reply("CONTINUE") == 0)
1763 errexit("Program terminated\n");
1764}
1765
1766ckfini()
1767{
1768
1769 flush(&dfile, &fileblk);
1770 flush(&dfile, &sblk);
f3c028b7
KM
1771 if (sblk.b_bno != SBLOCK) {
1772 sblk.b_bno = SBLOCK;
1773 sbdirty();
1774 flush(&dfile, &sblk);
1775 }
6e884967 1776 flush(&dfile, &inoblk);
1a724405
KM
1777 (void)close(dfile.rfdes);
1778 (void)close(dfile.wfdes);
6e884967
KM
1779}
1780
1a724405
KM
1781pinode(ino)
1782 ino_t ino;
6e884967
KM
1783{
1784 register DINODE *dp;
1785 register char *p;
24a31719 1786 char uidbuf[BUFSIZ];
6e884967
KM
1787 char *ctime();
1788
1a724405
KM
1789 printf(" I=%u ", ino);
1790 if ((dp = ginode(ino)) == NULL)
6e884967
KM
1791 return;
1792 printf(" OWNER=");
1793 if (getpw((int)dp->di_uid, uidbuf) == 0) {
1794 for (p = uidbuf; *p != ':'; p++);
1795 *p = 0;
1796 printf("%s ", uidbuf);
1797 }
1798 else {
1799 printf("%d ", dp->di_uid);
1800 }
1801 printf("MODE=%o\n", dp->di_mode);
1802 if (preen)
1803 printf("%s: ", devname);
1804 printf("SIZE=%ld ", dp->di_size);
1805 p = ctime(&dp->di_mtime);
1806 printf("MTIME=%12.12s %4.4s ", p+4, p+20);
1807}
1808
6e884967
KM
1809makecg()
1810{
f3c028b7 1811 int c, blk;
bf541624 1812 daddr_t dbase, d, dlower, dupper, dmax;
6e884967 1813 long i, j, s;
1a724405 1814 ino_t inumber;
6e884967 1815 register struct csum *cs;
f3c028b7 1816 register DINODE *dp;
6e884967 1817
0947395d
KM
1818 sblock.fs_cstotal.cs_nbfree = 0;
1819 sblock.fs_cstotal.cs_nffree = 0;
1820 sblock.fs_cstotal.cs_nifree = 0;
1821 sblock.fs_cstotal.cs_ndir = 0;
6e884967 1822 for (c = 0; c < sblock.fs_ncg; c++) {
6994bf5d 1823 dbase = cgbase(&sblock, c);
6e884967 1824 dmax = dbase + sblock.fs_fpg;
8f99f49c 1825 if (dmax > sblock.fs_size) {
bf4c734c 1826 for ( ; dmax >= sblock.fs_size; dmax--)
2e9860b5 1827 clrbit(cgrp.cg_free, dmax - dbase);
8f99f49c
KM
1828 dmax++;
1829 }
bf541624
KM
1830 dlower = cgsblock(&sblock, c) - dbase;
1831 dupper = cgdmin(&sblock, c) - dbase;
b6407c9d 1832 cs = &sblock.fs_cs(&sblock, c);
1a724405 1833 (void)time(&cgrp.cg_time);
6e884967
KM
1834 cgrp.cg_magic = CG_MAGIC;
1835 cgrp.cg_cgx = c;
67272100
KM
1836 if (c == sblock.fs_ncg - 1)
1837 cgrp.cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg;
1838 else
1839 cgrp.cg_ncyl = sblock.fs_cpg;
6e884967
KM
1840 cgrp.cg_niblk = sblock.fs_ipg;
1841 cgrp.cg_ndblk = dmax - dbase;
0947395d
KM
1842 cgrp.cg_cs.cs_ndir = 0;
1843 cgrp.cg_cs.cs_nffree = 0;
1844 cgrp.cg_cs.cs_nbfree = 0;
1845 cgrp.cg_cs.cs_nifree = 0;
bf541624
KM
1846 cgrp.cg_rotor = 0;
1847 cgrp.cg_frotor = 0;
92ea6158 1848 cgrp.cg_irotor = 0;
b6407c9d 1849 for (i = 0; i < sblock.fs_frag; i++)
f3c028b7 1850 cgrp.cg_frsum[i] = 0;
1a724405
KM
1851 inumber = sblock.fs_ipg * c;
1852 for (i = 0; i < sblock.fs_ipg; inumber++, i++) {
bf4c734c
KM
1853 cgrp.cg_cs.cs_nifree++;
1854 clrbit(cgrp.cg_iused, i);
1a724405 1855 dp = ginode(inumber);
f3c028b7
KM
1856 if (dp == NULL)
1857 continue;
1858 if (ALLOC) {
24a31719 1859 if (DIRCT)
0947395d 1860 cgrp.cg_cs.cs_ndir++;
bf4c734c 1861 cgrp.cg_cs.cs_nifree--;
f3c028b7
KM
1862 setbit(cgrp.cg_iused, i);
1863 continue;
1864 }
6e884967
KM
1865 }
1866 while (i < MAXIPG) {
1867 clrbit(cgrp.cg_iused, i);
1868 i++;
1869 }
bf4c734c
KM
1870 if (c == 0)
1871 for (i = 0; i < ROOTINO; i++) {
1872 setbit(cgrp.cg_iused, i);
1873 cgrp.cg_cs.cs_nifree--;
1874 }
43f6367c
KM
1875 for (s = 0; s < MAXCPG; s++) {
1876 cgrp.cg_btot[s] = 0;
6e884967
KM
1877 for (i = 0; i < NRPOS; i++)
1878 cgrp.cg_b[s][i] = 0;
43f6367c 1879 }
6e884967 1880 if (c == 0) {
bf541624 1881 dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
6e884967 1882 }
bf541624 1883 for (d = dlower; d < dupper; d++)
6e884967 1884 clrbit(cgrp.cg_free, d);
bf541624
KM
1885 for (d = 0; (d + sblock.fs_frag) <= dmax - dbase;
1886 d += sblock.fs_frag) {
6e884967 1887 j = 0;
b6407c9d 1888 for (i = 0; i < sblock.fs_frag; i++) {
bf541624
KM
1889 if (!getbmap(dbase + d + i)) {
1890 setbit(cgrp.cg_free, d + i);
6e884967
KM
1891 j++;
1892 } else
1893 clrbit(cgrp.cg_free, d+i);
1894 }
b6407c9d 1895 if (j == sblock.fs_frag) {
0947395d 1896 cgrp.cg_cs.cs_nbfree++;
43f6367c 1897 cgrp.cg_btot[cbtocylno(&sblock, d)]++;
aca50d72
KM
1898 cgrp.cg_b[cbtocylno(&sblock, d)]
1899 [cbtorpos(&sblock, d)]++;
f3c028b7 1900 } else if (j > 0) {
0947395d 1901 cgrp.cg_cs.cs_nffree += j;
bf541624 1902 blk = blkmap(&sblock, cgrp.cg_free, d);
b6407c9d 1903 fragacct(&sblock, blk, cgrp.cg_frsum, 1);
f3c028b7 1904 }
6e884967 1905 }
f3c028b7 1906 for (j = d; d < dmax - dbase; d++) {
bf541624 1907 if (!getbmap(dbase + d)) {
6e884967 1908 setbit(cgrp.cg_free, d);
0947395d 1909 cgrp.cg_cs.cs_nffree++;
6e884967
KM
1910 } else
1911 clrbit(cgrp.cg_free, d);
1912 }
67272100
KM
1913 for (; d % sblock.fs_frag != 0; d++)
1914 clrbit(cgrp.cg_free, d);
f3c028b7 1915 if (j != d) {
bf541624 1916 blk = blkmap(&sblock, cgrp.cg_free, j);
b6407c9d 1917 fragacct(&sblock, blk, cgrp.cg_frsum, 1);
f3c028b7 1918 }
67272100
KM
1919 for (d /= sblock.fs_frag; d < MAXBPG(&sblock); d ++)
1920 clrblock(&sblock, cgrp.cg_free, d);
0947395d
KM
1921 sblock.fs_cstotal.cs_nffree += cgrp.cg_cs.cs_nffree;
1922 sblock.fs_cstotal.cs_nbfree += cgrp.cg_cs.cs_nbfree;
1923 sblock.fs_cstotal.cs_nifree += cgrp.cg_cs.cs_nifree;
1924 sblock.fs_cstotal.cs_ndir += cgrp.cg_cs.cs_ndir;
1925 *cs = cgrp.cg_cs;
1a724405
KM
1926 (void)bwrite(&dfile, (char *)&cgrp,
1927 fsbtodb(&sblock, cgtod(&sblock, c)), sblock.fs_cgsize);
6e884967 1928 }
bf541624 1929 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
1a724405 1930 (void)bwrite(&dfile, (char *)sblock.fs_csp[j],
bf541624
KM
1931 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
1932 sblock.fs_cssize - i < sblock.fs_bsize ?
1933 sblock.fs_cssize - i : sblock.fs_bsize);
c2a050cf 1934 }
6e884967
KM
1935 sblock.fs_ronly = 0;
1936 sblock.fs_fmod = 0;
1937 sbdirty();
1938}
1939
1a724405
KM
1940findino(idesc)
1941 struct inodesc *idesc;
6e884967 1942{
1a724405
KM
1943 register DIRECT *dirp = idesc->id_dirp;
1944
6e884967
KM
1945 if (dirp->d_ino == 0)
1946 return (KEEPON);
24a31719
KM
1947 if (!strcmp(dirp->d_name, srchname)) {
1948 if (dirp->d_ino >= ROOTINO && dirp->d_ino <= imax)
1a724405 1949 idesc->id_parent = dirp->d_ino;
24a31719 1950 return (STOP);
6e884967
KM
1951 }
1952 return (KEEPON);
1953}
1954
1a724405
KM
1955mkentry(idesc)
1956 struct inodesc *idesc;
6e884967 1957{
1a724405 1958 register DIRECT *dirp = idesc->id_dirp;
754cadb5
KM
1959 DIRECT newent;
1960 int newlen, oldlen;
6e884967 1961
754cadb5
KM
1962 newent.d_namlen = 11;
1963 newlen = DIRSIZ(&newent);
1964 if (dirp->d_ino != 0)
1965 oldlen = DIRSIZ(dirp);
1966 else
1967 oldlen = 0;
1968 if (dirp->d_reclen - oldlen < newlen)
6e884967 1969 return (KEEPON);
754cadb5
KM
1970 newent.d_reclen = dirp->d_reclen - oldlen;
1971 dirp->d_reclen = oldlen;
1972 dirp = (struct direct *)(((char *)dirp) + oldlen);
1a724405 1973 dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */
754cadb5 1974 dirp->d_reclen = newent.d_reclen;
1a724405
KM
1975 dirp->d_namlen = lftempname(dirp->d_name, idesc->id_parent);
1976 return (ALTERED|STOP);
6e884967
KM
1977}
1978
1a724405
KM
1979chgdd(idesc)
1980 struct inodesc *idesc;
6e884967 1981{
1a724405
KM
1982 register DIRECT *dirp = idesc->id_dirp;
1983
6e884967
KM
1984 if (dirp->d_name[0] == '.' && dirp->d_name[1] == '.' &&
1985 dirp->d_name[2] == 0) {
1986 dirp->d_ino = lfdir;
1a724405 1987 return (ALTERED|STOP);
6e884967
KM
1988 }
1989 return (KEEPON);
1990}
1991
1a724405
KM
1992linkup(orphan, pdir)
1993 ino_t orphan;
1994 ino_t pdir;
6e884967
KM
1995{
1996 register DINODE *dp;
bec6e7fb 1997 int lostdir, len;
1a724405 1998 struct inodesc idesc;
6e884967 1999
1a724405
KM
2000 bzero((char *)&idesc, sizeof(struct inodesc));
2001 if ((dp = ginode(orphan)) == NULL)
6e884967 2002 return (0);
24a31719 2003 lostdir = DIRCT;
6e884967 2004 pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
1a724405 2005 pinode(orphan);
6e884967
KM
2006 if (preen && dp->di_size == 0)
2007 return (0);
2008 if (preen)
2009 printf(" (RECONNECTED)\n");
2010 else
2011 if (reply("RECONNECT") == 0)
2012 return (0);
bec6e7fb
KM
2013 pathp = pathname;
2014 *pathp++ = '/';
2015 *pathp = '\0';
6e884967 2016 if (lfdir == 0) {
1a724405 2017 if ((dp = ginode(ROOTINO)) == NULL)
6e884967 2018 return (0);
6e884967 2019 srchname = lfname;
1a724405
KM
2020 idesc.id_type = DATA;
2021 idesc.id_func = findino;
2022 idesc.id_number = ROOTINO;
2023 idesc.id_filesize = dp->di_size;
2024 (void)ckinode(dp, &idesc);
2025 if ((lfdir = idesc.id_parent) == 0) {
6e884967
KM
2026 pfatal("SORRY. NO lost+found DIRECTORY");
2027 printf("\n\n");
2028 return (0);
2029 }
2030 }
1a724405
KM
2031 if ((dp = ginode(lfdir)) == NULL ||
2032 !DIRCT || statemap[lfdir] != FSTATE) {
6e884967
KM
2033 pfatal("SORRY. NO lost+found DIRECTORY");
2034 printf("\n\n");
2035 return (0);
2036 }
3352e84a
KM
2037 if (fragoff(&sblock, dp->di_size)) {
2038 dp->di_size = fragroundup(&sblock, dp->di_size);
6e884967
KM
2039 inodirty();
2040 }
bec6e7fb
KM
2041 len = strlen(lfname);
2042 bcopy(lfname, pathp, len + 1);
2043 pathp += len;
1a724405
KM
2044 idesc.id_type = DATA;
2045 idesc.id_func = mkentry;
2046 idesc.id_number = lfdir;
2047 idesc.id_filesize = dp->di_size;
2048 idesc.id_parent = orphan; /* this is the inode to enter */
bec6e7fb 2049 idesc.id_fix = DONTKNOW;
1a724405 2050 if ((ckinode(dp, &idesc) & ALTERED) == 0) {
6e884967
KM
2051 pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
2052 printf("\n\n");
2053 return (0);
2054 }
1a724405 2055 lncntp[orphan]--;
bec6e7fb
KM
2056 *pathp++ = '/';
2057 pathp += lftempname(pathp, orphan);
6e884967 2058 if (lostdir) {
1a724405
KM
2059 dp = ginode(orphan);
2060 idesc.id_type = DATA;
2061 idesc.id_func = chgdd;
2062 idesc.id_number = orphan;
2063 idesc.id_filesize = dp->di_size;
bec6e7fb 2064 idesc.id_fix = DONTKNOW;
1a724405
KM
2065 (void)ckinode(dp, &idesc);
2066 if ((dp = ginode(lfdir)) != NULL) {
6e884967
KM
2067 dp->di_nlink++;
2068 inodirty();
1a724405 2069 lncntp[lfdir]++;
6e884967 2070 }
6e884967
KM
2071 pwarn("DIR I=%u CONNECTED. ", orphan);
2072 printf("PARENT WAS I=%u\n", pdir);
2073 if (preen == 0)
2074 printf("\n");
2075 }
2076 return (1);
2077}
2078
1a724405
KM
2079/*
2080 * generate a temporary name for the lost+found directory.
2081 */
2082lftempname(bufp, ino)
2083 char *bufp;
2084 ino_t ino;
2085{
2086 register ino_t in;
2087 register char *cp;
2088 int namlen;
2089
2090 cp = bufp + 2;
2091 for (in = imax; in > 0; in /= 10)
2092 cp++;
2093 *--cp = 0;
2094 namlen = cp - bufp;
2095 in = ino;
2096 while (cp > bufp) {
2097 *--cp = (in % 10) + '0';
2098 in /= 10;
2099 }
2100 *cp = '#';
2101 return (namlen);
2102}
2103
6e884967 2104bread(fcp, buf, blk, size)
6e884967 2105 register struct filecntl *fcp;
6e884967 2106 char *buf;
1a724405
KM
2107 daddr_t blk;
2108 long size;
6e884967 2109{
1a724405 2110 if (lseek(fcp->rfdes, (long)dbtob(blk), 0) < 0)
6e884967 2111 rwerr("SEEK", blk);
1a724405 2112 else if (read(fcp->rfdes, buf, (int)size) == size)
6e884967
KM
2113 return (1);
2114 rwerr("READ", blk);
2115 return (0);
2116}
2117
2118bwrite(fcp, buf, blk, size)
6e884967 2119 register struct filecntl *fcp;
6e884967 2120 char *buf;
1a724405
KM
2121 daddr_t blk;
2122 long size;
6e884967
KM
2123{
2124
2125 if (fcp->wfdes < 0)
2126 return (0);
1a724405 2127 if (lseek(fcp->wfdes, (long)dbtob(blk), 0) < 0)
6e884967 2128 rwerr("SEEK", blk);
1a724405 2129 else if (write(fcp->wfdes, buf, (int)size) == size) {
6e884967
KM
2130 fcp->mod = 1;
2131 return (1);
2132 }
2133 rwerr("WRITE", blk);
2134 return (0);
2135}
2136
2137catch()
2138{
2139
2140 ckfini();
2141 exit(12);
2142}
b6407c9d 2143
d2c95d76
BJ
2144char *
2145unrawname(cp)
2146 char *cp;
2147{
2148 char *dp = rindex(cp, '/');
2149 struct stat stb;
2150
2151 if (dp == 0)
2152 return (cp);
2153 if (stat(cp, &stb) < 0)
2154 return (cp);
2155 if ((stb.st_mode&S_IFMT) != S_IFCHR)
2156 return (cp);
2157 if (*(dp+1) != 'r')
2158 return (cp);
1a724405 2159 (void)strcpy(dp+1, dp+2);
d2c95d76
BJ
2160 return (cp);
2161}
2162
2163char *
2164rawname(cp)
2165 char *cp;
2166{
2167 static char rawbuf[32];
2168 char *dp = rindex(cp, '/');
2169
2170 if (dp == 0)
2171 return (0);
2172 *dp = 0;
1a724405 2173 (void)strcpy(rawbuf, cp);
d2c95d76 2174 *dp = '/';
1a724405
KM
2175 (void)strcat(rawbuf, "/r");
2176 (void)strcat(rawbuf, dp+1);
d2c95d76
BJ
2177 return (rawbuf);
2178}
2179
1a724405
KM
2180/*
2181 * determine whether an inode should be fixed.
2182 */
2183dofix(idesc)
2184 register struct inodesc *idesc;
2185{
2186 switch (idesc->id_fix) {
2187
2188 case DONTKNOW:
2189 if (reply("FIX") == 0) {
2190 idesc->id_fix = NOFIX;
2191 return (0);
2192 }
2193 idesc->id_fix = FIX;
2194 return (ALTERED);
2195
2196 case FIX:
2197 return (ALTERED);
2198
2199 case NOFIX:
2200 return (0);
2201
2202 default:
2203 errexit("UNKNOWN INODESC FIX MODE %d\n", idesc->id_fix);
2204 }
2205 /* NOTREACHED */
2206}
2207
d2c95d76
BJ
2208/* VARARGS1 */
2209error(s1, s2, s3, s4)
2210 char *s1;
2211{
4d308541 2212
d2c95d76
BJ
2213 printf(s1, s2, s3, s4);
2214}
4d308541 2215
d2c95d76
BJ
2216/* VARARGS1 */
2217errexit(s1, s2, s3, s4)
2218 char *s1;
2219{
2220 error(s1, s2, s3, s4);
2221 exit(8);
2222}
4d308541
KM
2223
2224/*
d2c95d76 2225 * An inconsistency occured which shouldn't during normal operations.
af5ba2f8 2226 * Die if preening, otherwise just printf.
4d308541 2227 */
d2c95d76
BJ
2228/* VARARGS1 */
2229pfatal(s, a1, a2, a3)
2230 char *s;
2231{
4d308541 2232
d2c95d76
BJ
2233 if (preen) {
2234 printf("%s: ", devname);
2235 printf(s, a1, a2, a3);
2236 printf("\n");
2237 preendie();
2238 }
2239 printf(s, a1, a2, a3);
2240}
4d308541 2241
d2c95d76
BJ
2242preendie()
2243{
4d308541 2244
d2c95d76
BJ
2245 printf("%s: UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY.\n", devname);
2246 exit(8);
2247}
4d308541
KM
2248
2249/*
d2c95d76
BJ
2250 * Pwarn is like printf when not preening,
2251 * or a warning (preceded by filename) when preening.
4d308541 2252 */
d2c95d76
BJ
2253/* VARARGS1 */
2254pwarn(s, a1, a2, a3, a4, a5, a6)
2255 char *s;
2256{
2257
2258 if (preen)
2259 printf("%s: ", devname);
2260 printf(s, a1, a2, a3, a4, a5, a6);
2261}
9a4020f4 2262
1a724405
KM
2263#ifndef lint
2264/*
2265 * Stub for routines from kernel.
2266 */
9a4020f4
BJ
2267panic(s)
2268 char *s;
2269{
2270
1a724405 2271 pfatal("INTERNAL INCONSISTENCY: %s\n", s);
9a4020f4
BJ
2272 exit(12);
2273}
1a724405 2274#endif