122048Sdist /* 222048Sdist * Copyright (c) 1980 Regents of the University of California. 322048Sdist * All rights reserved. The Berkeley software License Agreement 422048Sdist * specifies the terms and conditions for redistribution. 522048Sdist */ 622048Sdist 716262Smckusick #ifndef lint 8*28084Smckusick static char sccsid[] = "@(#)pass1.c 5.3 (Berkeley) 05/13/86"; 922048Sdist #endif not lint 1016262Smckusick 1116262Smckusick #include <sys/param.h> 1216262Smckusick #include <sys/inode.h> 1316262Smckusick #include <sys/fs.h> 1416262Smckusick #include "fsck.h" 1516262Smckusick 1617937Smckusick static daddr_t badblk; 1717937Smckusick static daddr_t dupblk; 1817937Smckusick int pass1check(); 1916262Smckusick 2016262Smckusick pass1() 2116262Smckusick { 2217937Smckusick register int c, i, j; 2316262Smckusick register DINODE *dp; 2421759Smckusick struct zlncnt *zlnp; 2517931Smckusick int ndb, partial, cgd; 2616262Smckusick struct inodesc idesc; 2716262Smckusick ino_t inumber; 2816262Smckusick 2917931Smckusick /* 3017931Smckusick * Set file system reserved blocks in used block map. 3117931Smckusick */ 3217931Smckusick for (c = 0; c < sblock.fs_ncg; c++) { 3317931Smckusick cgd = cgdmin(&sblock, c); 3417931Smckusick if (c == 0) { 3517931Smckusick i = cgbase(&sblock, c); 3617931Smckusick cgd += howmany(sblock.fs_cssize, sblock.fs_fsize); 3717931Smckusick } else 3817931Smckusick i = cgsblock(&sblock, c); 3917931Smckusick for (; i < cgd; i++) 4017931Smckusick setbmap(i); 4117931Smckusick } 4217931Smckusick /* 4317931Smckusick * Find all allocated blocks. 4417931Smckusick */ 4516262Smckusick bzero((char *)&idesc, sizeof(struct inodesc)); 4616262Smckusick idesc.id_type = ADDR; 4716262Smckusick idesc.id_func = pass1check; 4816262Smckusick inumber = 0; 4917937Smckusick n_files = n_blks = 0; 5016262Smckusick for (c = 0; c < sblock.fs_ncg; c++) { 5116262Smckusick for (i = 0; i < sblock.fs_ipg; i++, inumber++) { 5217937Smckusick if (inumber < ROOTINO) 5316262Smckusick continue; 5417943Smckusick dp = ginode(inumber); 5517937Smckusick if (!ALLOC(dp)) { 5617937Smckusick if (bcmp((char *)dp->di_db, (char *)zino.di_db, 5717937Smckusick NDADDR * sizeof(daddr_t)) || 5817937Smckusick bcmp((char *)dp->di_ib, (char *)zino.di_ib, 5917937Smckusick NIADDR * sizeof(daddr_t)) || 6017937Smckusick dp->di_mode || dp->di_size) { 6116262Smckusick pfatal("PARTIALLY ALLOCATED INODE I=%u", 6216262Smckusick inumber); 6316262Smckusick if (reply("CLEAR") == 1) { 6416262Smckusick zapino(dp); 6516262Smckusick inodirty(); 6616262Smckusick } 6716262Smckusick } 6817937Smckusick statemap[inumber] = USTATE; 6917937Smckusick continue; 7016262Smckusick } 7117937Smckusick lastino = inumber; 7217937Smckusick if (dp->di_size < 0) { 7317937Smckusick if (debug) 7417937Smckusick printf("bad size %d:", dp->di_size); 7517937Smckusick goto unknown; 7617937Smckusick } 77*28084Smckusick if (!preen && (dp->di_mode & IFMT) == IFMT && 78*28084Smckusick reply("HOLD BAD BLOCK") == 1) { 79*28084Smckusick dp->di_size = sblock.fs_fsize; 80*28084Smckusick dp->di_mode = IFREG|0600; 81*28084Smckusick inodirty(); 82*28084Smckusick } 8317937Smckusick ndb = howmany(dp->di_size, sblock.fs_bsize); 8417937Smckusick if (SPECIAL(dp)) 8517937Smckusick ndb++; 8617937Smckusick for (j = ndb; j < NDADDR; j++) 8717937Smckusick if (dp->di_db[j] != 0) { 8817937Smckusick if (debug) 8917937Smckusick printf("bad direct addr: %d\n", 9017937Smckusick dp->di_db[j]); 9117937Smckusick goto unknown; 9217937Smckusick } 9317937Smckusick for (j = 0, ndb -= NDADDR; ndb > 0; j++) 9417937Smckusick ndb /= NINDIR(&sblock); 9517937Smckusick for (; j < NIADDR; j++) 9617937Smckusick if (dp->di_ib[j] != 0) { 9717937Smckusick if (debug) 9817937Smckusick printf("bad indirect addr: %d\n", 9917937Smckusick dp->di_ib[j]); 10017937Smckusick goto unknown; 10117937Smckusick } 102*28084Smckusick if (ftypeok(dp) == 0) 10317937Smckusick goto unknown; 10417937Smckusick n_files++; 10517937Smckusick lncntp[inumber] = dp->di_nlink; 10617937Smckusick if (dp->di_nlink <= 0) { 10721759Smckusick zlnp = (struct zlncnt *)malloc(sizeof *zlnp); 10821759Smckusick if (zlnp == NULL) { 10917937Smckusick pfatal("LINK COUNT TABLE OVERFLOW"); 11017937Smckusick if (reply("CONTINUE") == 0) 11117937Smckusick errexit(""); 11221759Smckusick } else { 11321759Smckusick zlnp->zlncnt = inumber; 11421759Smckusick zlnp->next = zlnhead; 11521759Smckusick zlnhead = zlnp; 11617937Smckusick } 11717937Smckusick } 11817937Smckusick statemap[inumber] = DIRCT(dp) ? DSTATE : FSTATE; 11917937Smckusick badblk = dupblk = 0; maxblk = 0; 12017937Smckusick idesc.id_number = inumber; 12117937Smckusick (void)ckinode(dp, &idesc); 12217990Smckusick idesc.id_entryno *= btodb(sblock.fs_fsize); 12317990Smckusick if (dp->di_blocks != idesc.id_entryno) { 12417937Smckusick pwarn("INCORRECT BLOCK COUNT I=%u (%ld should be %ld)", 12517990Smckusick inumber, dp->di_blocks, idesc.id_entryno); 12617937Smckusick if (preen) 12717937Smckusick printf(" (CORRECTED)\n"); 12817937Smckusick else if (reply("CORRECT") == 0) 12917937Smckusick continue; 13017990Smckusick dp->di_blocks = idesc.id_entryno; 13117937Smckusick inodirty(); 13217937Smckusick } 13317937Smckusick continue; 13417937Smckusick unknown: 13517937Smckusick pfatal("UNKNOWN FILE TYPE I=%u", inumber); 13626481Smckusick statemap[inumber] = FCLEAR; 13717937Smckusick if (reply("CLEAR") == 1) { 13826481Smckusick statemap[inumber] = USTATE; 13917937Smckusick zapino(dp); 14017937Smckusick inodirty(); 14117937Smckusick } 14216262Smckusick } 14316262Smckusick } 14416262Smckusick } 14516262Smckusick 14616262Smckusick pass1check(idesc) 14716262Smckusick register struct inodesc *idesc; 14816262Smckusick { 14916262Smckusick int res = KEEPON; 15016262Smckusick int anyout, nfrags; 15116262Smckusick daddr_t blkno = idesc->id_blkno; 15221744Smckusick register struct dups *dlp; 15321744Smckusick struct dups *new; 15416262Smckusick 15517931Smckusick if ((anyout = outrange(blkno, idesc->id_numfrags)) != 0) { 15617931Smckusick blkerr(idesc->id_number, "BAD", blkno); 15717931Smckusick if (++badblk >= MAXBAD) { 15817931Smckusick pwarn("EXCESSIVE BAD BLKS I=%u", 15917931Smckusick idesc->id_number); 16017931Smckusick if (preen) 16117931Smckusick printf(" (SKIPPING)\n"); 16217931Smckusick else if (reply("CONTINUE") == 0) 16317931Smckusick errexit(""); 16417931Smckusick return (STOP); 16517931Smckusick } 16617931Smckusick } 16716262Smckusick for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) { 16816262Smckusick if (anyout && outrange(blkno, 1)) { 16916262Smckusick res = SKIP; 17017931Smckusick } else if (!getbmap(blkno)) { 17117931Smckusick n_blks++; 17217931Smckusick setbmap(blkno); 17317931Smckusick } else { 17416262Smckusick blkerr(idesc->id_number, "DUP", blkno); 17516262Smckusick if (++dupblk >= MAXDUP) { 17616262Smckusick pwarn("EXCESSIVE DUP BLKS I=%u", 17716262Smckusick idesc->id_number); 17816262Smckusick if (preen) 17916262Smckusick printf(" (SKIPPING)\n"); 18016262Smckusick else if (reply("CONTINUE") == 0) 18116262Smckusick errexit(""); 18216262Smckusick return (STOP); 18316262Smckusick } 18421744Smckusick new = (struct dups *)malloc(sizeof(struct dups)); 18521744Smckusick if (new == NULL) { 18616262Smckusick pfatal("DUP TABLE OVERFLOW."); 18716262Smckusick if (reply("CONTINUE") == 0) 18816262Smckusick errexit(""); 18916262Smckusick return (STOP); 19016262Smckusick } 19121744Smckusick new->dup = blkno; 19221744Smckusick if (muldup == 0) { 19321744Smckusick duplist = muldup = new; 19421744Smckusick new->next = 0; 19521744Smckusick } else { 19621744Smckusick new->next = muldup->next; 19721744Smckusick muldup->next = new; 19821744Smckusick } 19921744Smckusick for (dlp = duplist; dlp != muldup; dlp = dlp->next) 20021744Smckusick if (dlp->dup == blkno) 20116262Smckusick break; 20221744Smckusick if (dlp == muldup && dlp->dup != blkno) 20321744Smckusick muldup = new; 20416262Smckusick } 20517990Smckusick /* 20617990Smckusick * count the number of blocks found in id_entryno 20717990Smckusick */ 20817990Smckusick idesc->id_entryno++; 20916262Smckusick } 21016262Smckusick return (res); 21116262Smckusick } 212