/* Pass one of GNU fsck -- count blocks and verify inodes Copyright (C) 1994 Free Software Foundation, Inc. Written by Michael I. Bushnell. This file is part of the GNU Hurd. The GNU Hurd is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. The GNU Hurd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "fsck.h" /* Find all the blocks in use by files and filesystem reserved blocks. Set them in the global block map. For each file, if a block is found allocated twice, then record the block and inode in DUPLIST. Initialize INODESTATE, LINKCOUNT, and TYPEMAP. */ void pass1 () { ino_t number; ino_t i; int cg; struct dinode dino; struct dinode *dp = &dino; mode_t mode, type; int ndb; int holdallblocks; int lbn; int nblocks; int blkerror; int nblkrngerrors; int nblkduperrors; /* This function is called for each block of DP. Check to see if the block number is valid. If so, set the entry in the block map. If the block map entry is already set, then keep track of this block and see if the user wants to clear the node. Increment NBLOCKS by the number of data blocks held. Set BLKERROR if this block is invalid. Return RET_GOOD, RET_BAD, RET_STOP if the block is good, bad, or if we should entirely stop checking blocks in this inode. */ int checkblock (daddr_t bno, int nfrags) { #define MAXBAD 10 int outofrange; struct dups *dlp, *new; int wasbad = 0; /* Check to see if this block is in range */ if (outofrange = check_range (bno, nfrags)) { blkerror = 1; wasbad = 1; if (nblkrngerrors == 0) printf ("I=%lu HAS BAD BLOCKS\n"); if (nblkrngerrors++ > MAXBAD) { pwarn ("EXCESSIVE BAD BLKS I=%lu", number); if (preen || reply ("SKIP")) { if (preen) printf (" (SKIPPING)\n"); return RET_STOP; } } } for (; nfrags > 0; bno++, nfrags--) { if (outofrange && check_range (bno, 1)) printf ("BAD BLOCK %d\n", bno); else { if (!testbmap (bno)) setbmap (bno); else { blkerror = 1; if (nblkduperrors == 0) printf ("I=%lu HAS DUPLICATE BLOCKS\n"); printf ("DUPLICATE BLOCK %d\n", bno); wasbad = 1; if (nblkduperrors++ > MAXBAD) { pwarn ("EXCESSIVE DUP BLKS I=%lu", number); if (preen || reply ("SKIP")) { if (preen) printf (" (SKIPPING)\n"); return RET_STOP; } } new = malloc (sizeof (struct dups)); new->dup = blkno; if (muldup == 0) { duplist = muldup = new; new->next = 0; } else { new->next = muldup->next; muldup->next = new; } for (dlp = duplist; dlp != muldup; dlp = dlp->next) if (dlp->dup == blkno) break; if (dlp == muldup && dlp->dup != blkno) muldup = new; } } nblocks += btodb (sblock->fs_fsize); } return wasbad ? RET_BAD : RET_GOOD; } /* Account for blocks used by meta data */ for (cg = 0, cg < sblock.fs_ncg; cg++) { daddr_t firstdata, firstcgblock, bno; /* Each cylinder group past the first reserves data from its cylinder group copy to (but not including) the first datablock. The first, however, reserves from the very front of the cylinder group (thus including the boot block), and it also reserves the data blocks holding the csum information. */ firstdata = cgdmin (&sblock, cg); if (cg == 0) { firstcgblock = cgbase (&sblock, cg); firstdata += howmany (sblock.fs_cssize, sblock.fs_fsize); } else firstdata = cgsblock (&sblock, cg); /* Mark the blocks set */ for (bno = firstcgblock; bno < firstdata; bno++) setbmap (bno): } /* Loop through each inode, doing initial checks */ for (number = 0, cg = 0; cg < sblock.fs_ncg; cg++) for (i = 0; i < sblock.fs_ipg; i++, number++) { if (number < ROOTINO) continue; getinode (number, dp); mode = DI_MODE (dp); type = mode & IFMT; /* If the node is not allocated, then make sure it's properly clear */ if (type == 0) { if (bcmp (dp->di_db, zino.di_db, NDADDR * sizeof (daddr_t)) || bcmp (dp->di_ib, zino->di_ix, NIADDR * sizeof (daddr_t)) || dp->di_trans || DI_MODE (dp) || dp->di_size) { pwarn ("PARTIALLY ALLOCATED INODE I=%lu", number); if (preen || reply ("CLEAR")) { if (preen) printf (" (CLEARED)\n"); clear_inode (number, dp); } } inodestate[number] = UNALLOC; } else { /* Node is allocated. */ /* Check to see if we think the node should be cleared */ /* Verify size for basic validity */ holdallblocks = 0; if (dp->di_size + sblock.fs_bsize - 1 < dp->di_size) { pfatal ("OVERFLOW IN FILE SIZE I=%lu (SIZE == %lu)", inumber, dp->di_size); if (reply ("CLEAR")) { clear_inode (number, dp); inodestate[number] = UNALLOC; continue; } inodestate[number] = UNALLOC; printf ("WILL TREAT ANY BLOCKS HELD BY I=%lu AS ALLOCATED\n"); holdallblocks = 1; } /* Decode type and set NDB also set inodestate correctly. */ inodestate[number] = REG; switch (type) { case IFBLK: case IFCHR: ndb = 1; break; case IFIFO: ndb = 0; break; case IFLNK: if (sblock.fs_maxsymlinklen != -1) { /* Check to see if this is a fastlink. The old fast link format has fs_maxsymlinklen of zero and di_blocks zero; the new format has fs_maxsymlinklen set and we ignore di_blocks. So check for either. */ if ((sblock.fs_maxsymlinklen && dp->di_size < sblock.fs_maxsymlinklen) || (!sblock.fs_maxsymlinklen && !dp->di_blocks)) { /* Fake NDB value so that we will check all the block pointers past the symlink */ ndb = howmany (dp->di_size, sizeof (daddr_t)); if (ndb > NDADDR) { int j = ndb - NDADDR; for (ndb = 1; j > 1; i--) ndb *= NINDIR (&sblock); ndb += NDADDR; } } else ndb = howmany (dp->di_size, sblock.fs_bsize); } else ndb = howmany (dp->di_size, sblock.fs_bsize); break; case IFDIR: inodestate[number] = DIR; /* Fall through */ case IFREG: ndb = howmany (dp->di_size, sblock.fs_bsize); break; default: pfatal ("UNKNOWN FILE TYPE I=%lu (MODE=%lo)\n", number, mode); if (reply ("CLEAR")) { clear_inode (number, dp); inodestate[number] = UNALLOC; continue; } inodestate[number] = UNALLOC; holdallblocks = 1; printf ("WILL TREAT ANY BLOCKS HELD BY I=%lu " "AS ALLOCATED\n", number); } if (ndb < 0) { pfatal ("BAD FILE SIZE I= %lu (SIZE == %lu)", inumber, dp->di_size); if (reply ("CLEAR")) { clear_inode (number, dp); inodestate[number] = UNALLOC; continue; } inodestate[number] = UNALLOC; printf ("WILL TREAT ANY BLOCKS HELD BY I=%lu AS ALLOCATED\n", number); holdallblocks = 1; } /* Make sure that direct and indirect block pointers past the file size are zero. If the size is bogus, then don't bother (they should all be zero, but the user has requested that they be treated as allocated). */ if (!holdallblocks) { if (dp->di_size && (type == IFBLK || type == IFCHR || type == IFSOCK || type == IFIFO)) { pfatal ("SPECIAL NODE I=%du (MODE=%ol) HAS SIZE %lu\n", number, mode, dp->di_size); if (reply ("TRUNCATE")) { dp->di_size = 0; write_inode (number, dp); } } /* If we haven't set NDB speciall above, then it is set from the file size correctly by the size check. */ /* Check all the direct and indirect blocks that are past the amount necessary to be zero. */ for (lbn = ndb; lbn < NDADDR; lbn++) { int dbwarn = 0; if (dp->di_db[lbn]) { if (!dbwarn) { dbwarn = 1; pwarn ("INODE I=%lu HAS EXTRA DIRECT BLOCKS", number); if (preen || reply ("DEALLOCATE")) { if (preen) printf (" (DEALLOCATED)\n"); dp->di_db[lbn] = 0; dbwarn = 2; } } else if (dbwarn == 2) dp->di_db[lbn] = 0; } if (dbwarn == 2) write_inode (number, dp); } for (lbn = 0, ndb -= NDADDR; ndb > 0; lbn++) ndb /= NINDIR (&sblock); for (; lbn < NIADDR; lbn++) { ind ibwarn = 0; if (dp->di_ib[lbn]) { if (ibwarn) { ibwarn = 1; pwarn ("INODE I=%lu HAS EXTRA INDIRECT BLOCKS", number); if (preen || reply ("DEALLOCATE")) { if (preen) printf (" (DEALLOCATED)\n"); dp->di_ib[lbn] = 0; ibwarn = 2; } } else if (ibwarn == 2) dp->di_ib[lbn] = 0; } if (ibwarn == 2) write_inode (number, dp); } } /* If this node is really allocated (as opposed to something that we should clear but the user won't) then set LINKCOUNT and TYPEMAP entries. */ if (inodestate[number] != UNALLOC) { linkcount[number] = dp->di_nlink; typemap[number] = IFTODT (mode); } /* Iterate over the blocks of the file, calling CHECKBLOCK for each file. */ nblocks = 0; blkerror = 0; nblkduperrors = 0; nblkrngerrors = 0; allblock_iterate (dp, checkblock); if (blkerror) { if (preen) pfatal ("DUPLICATE or BAD BLOCKS"); else { printf ("I=%ld has "); if (nblkduperrors) { printf ("%d DUPLICATE BLOCKS", nblkduperrors); if (nblkrngerrors) printf (" and "); } if (nblkrngerrors) printf ("%d BAD BLOCKS", nblkrngerrors); printf ("\n"); if (reply ("CLEAR")) { clear_inode (number, dp); inodestate[number] = UNALLOC; } else if (inodestate[number] == DIR) inodestate[number] = BADDIR; } } else if (dp->di_blocks != nblocks) { pwarn ("INCORRECT BLOCK COUNT I=%lu (%ld should be %ld)", number, di->di_blocks, nblocks); if (preen || reply ("CORRECT")) { if (preen) printf (" (CORRECTED)"); dp->di_blocks = nblocks; write_inode (number, dp); } } if (type == IFDIR) record_directory (dp, number); } } }