2000-01-28 19:01:46 +03:00
|
|
|
/* $NetBSD: pass1.c,v 1.8 2000/01/28 16:01:46 bouyer Exp $ */
|
1997-06-11 15:21:39 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1997 Manuel Bouyer.
|
|
|
|
* Copyright (c) 1980, 1986, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-09-14 18:27:23 +04:00
|
|
|
#include <sys/cdefs.h>
|
1997-06-11 15:21:39 +04:00
|
|
|
#ifndef lint
|
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)pass1.c 8.1 (Berkeley) 6/5/93";
|
|
|
|
#else
|
2000-01-28 19:01:46 +03:00
|
|
|
__RCSID("$NetBSD: pass1.c,v 1.8 2000/01/28 16:01:46 bouyer Exp $");
|
1997-06-11 15:21:39 +04:00
|
|
|
#endif
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <ufs/ext2fs/ext2fs_dinode.h>
|
|
|
|
#include <ufs/ext2fs/ext2fs_dir.h>
|
|
|
|
#include <ufs/ext2fs/ext2fs.h>
|
|
|
|
|
|
|
|
#include <ufs/ufs/dinode.h> /* for IFMT & friends */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1998-04-01 19:26:00 +04:00
|
|
|
#include <time.h>
|
1997-06-11 15:21:39 +04:00
|
|
|
|
|
|
|
#include "fsck.h"
|
|
|
|
#include "extern.h"
|
|
|
|
#include "fsutil.h"
|
|
|
|
|
|
|
|
static daddr_t badblk;
|
|
|
|
static daddr_t dupblk;
|
|
|
|
static void checkinode __P((ino_t, struct inodesc *));
|
|
|
|
|
|
|
|
void
|
|
|
|
pass1()
|
|
|
|
{
|
|
|
|
ino_t inumber;
|
2000-01-28 19:01:46 +03:00
|
|
|
int c, i;
|
|
|
|
daddr_t dbase;
|
1997-06-11 15:21:39 +04:00
|
|
|
struct inodesc idesc;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set file system reserved blocks in used block map.
|
|
|
|
*/
|
|
|
|
for (c = 0; c < sblock.e2fs_ncg; c++) {
|
2000-01-28 19:01:46 +03:00
|
|
|
dbase = c * sblock.e2fs.e2fs_bpg +
|
|
|
|
sblock.e2fs.e2fs_first_dblock;
|
|
|
|
/* Mark the blocks used for the inode table */
|
|
|
|
if (sblock.e2fs_gd[c].ext2bgd_i_tables >= dbase) {
|
|
|
|
for (i = 0; i < sblock.e2fs_itpg; i++)
|
|
|
|
setbmap(sblock.e2fs_gd[c].ext2bgd_i_tables + i);
|
|
|
|
}
|
|
|
|
/* Mark the blocks used for the block bitmap */
|
|
|
|
if (sblock.e2fs_gd[c].ext2bgd_b_bitmap >= dbase)
|
|
|
|
setbmap(sblock.e2fs_gd[c].ext2bgd_b_bitmap);
|
|
|
|
/* Mark the blocks used for the inode bitmap */
|
|
|
|
if (sblock.e2fs_gd[c].ext2bgd_i_bitmap >= dbase)
|
|
|
|
setbmap(sblock.e2fs_gd[c].ext2bgd_i_bitmap);
|
|
|
|
|
|
|
|
if (sblock.e2fs.e2fs_rev == E2FS_REV0 ||
|
|
|
|
(sblock.e2fs.e2fs_features_rocompat &
|
|
|
|
EXT2F_ROCOMPAT_SPARSESUPER) == 0 ||
|
|
|
|
cg_has_sb(c)) {
|
|
|
|
/* Mark copuy of SB and descriptors */
|
|
|
|
setbmap(dbase);
|
|
|
|
for (i = 1; i <= sblock.e2fs_ngdb; i++)
|
|
|
|
setbmap(dbase+i);
|
|
|
|
}
|
1997-06-11 15:21:39 +04:00
|
|
|
|
2000-01-28 19:01:46 +03:00
|
|
|
|
|
|
|
if (c == 0) {
|
|
|
|
for(i = 0; i < dbase; i++)
|
|
|
|
setbmap(i);
|
|
|
|
}
|
1997-06-11 15:21:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find all allocated blocks.
|
|
|
|
*/
|
|
|
|
memset(&idesc, 0, sizeof(struct inodesc));
|
|
|
|
idesc.id_type = ADDR;
|
|
|
|
idesc.id_func = pass1check;
|
|
|
|
inumber = 1;
|
|
|
|
n_files = n_blks = 0;
|
|
|
|
resetinodebuf();
|
|
|
|
for (c = 0; c < sblock.e2fs_ncg; c++) {
|
|
|
|
for (i = 0;
|
|
|
|
i < sblock.e2fs.e2fs_ipg && inumber <= sblock.e2fs.e2fs_icount;
|
|
|
|
i++, inumber++) {
|
|
|
|
if (inumber < EXT2_ROOTINO) /* XXX */
|
|
|
|
continue;
|
|
|
|
checkinode(inumber, &idesc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
freeinodebuf();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
checkinode(inumber, idesc)
|
|
|
|
ino_t inumber;
|
1997-09-14 18:27:23 +04:00
|
|
|
struct inodesc *idesc;
|
1997-06-11 15:21:39 +04:00
|
|
|
{
|
1997-09-14 18:27:23 +04:00
|
|
|
struct ext2fs_dinode *dp;
|
1997-06-11 15:21:39 +04:00
|
|
|
struct zlncnt *zlnp;
|
|
|
|
int ndb, j;
|
|
|
|
mode_t mode;
|
|
|
|
|
|
|
|
dp = getnextinode(inumber);
|
|
|
|
if (inumber < EXT2_FIRSTINO && inumber != EXT2_ROOTINO)
|
|
|
|
return;
|
|
|
|
|
1997-10-09 17:19:32 +04:00
|
|
|
mode = fs2h16(dp->e2di_mode) & IFMT;
|
1997-06-11 15:21:39 +04:00
|
|
|
if (mode == 0 || (dp->e2di_dtime != 0 && dp->e2di_nlink == 0)) {
|
|
|
|
if (mode == 0 && (
|
|
|
|
memcmp(dp->e2di_blocks, zino.e2di_blocks,
|
|
|
|
(NDADDR + NIADDR) * sizeof(u_int32_t)) ||
|
|
|
|
dp->e2di_mode || dp->e2di_size)) {
|
|
|
|
pfatal("PARTIALLY ALLOCATED INODE I=%u", inumber);
|
|
|
|
if (reply("CLEAR") == 1) {
|
|
|
|
dp = ginode(inumber);
|
|
|
|
clearinode(dp);
|
|
|
|
inodirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef notyet /* it seems that dtime == 0 is valid for a unallocated inode */
|
|
|
|
if (dp->e2di_dtime == 0) {
|
|
|
|
pwarn("DELETED INODE I=%u HAS A NULL DTIME", inumber);
|
|
|
|
if (preen) {
|
|
|
|
printf(" (CORRECTED)\n");
|
|
|
|
}
|
|
|
|
if (preen || reply("CORRECT")) {
|
|
|
|
time_t t;
|
|
|
|
time(&t);
|
1997-10-09 17:19:32 +04:00
|
|
|
dp->e2di_dtime = h2fs32(t);
|
1997-06-11 15:21:39 +04:00
|
|
|
dp = ginode(inumber);
|
|
|
|
inodirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
statemap[inumber] = USTATE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
lastino = inumber;
|
|
|
|
if (dp->e2di_dtime != 0) {
|
1997-10-09 17:19:32 +04:00
|
|
|
time_t t = fs2h32(dp->e2di_dtime);
|
1997-06-11 15:21:39 +04:00
|
|
|
char *p = ctime(&t);
|
|
|
|
pwarn("INODE I=%u HAS DTIME=%12.12s %4.4s", inumber, &p[4], &p[20]);
|
|
|
|
if (preen) {
|
|
|
|
printf(" (CORRECTED)\n");
|
|
|
|
}
|
|
|
|
if (preen || reply("CORRECT")) {
|
|
|
|
dp = ginode(inumber);
|
|
|
|
dp->e2di_dtime = 0;
|
|
|
|
inodirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (/* dp->di_size < 0 || */
|
1997-10-09 17:19:32 +04:00
|
|
|
fs2h32(dp->e2di_size) + sblock.e2fs_bsize - 1 <
|
|
|
|
fs2h32(dp->e2di_size)) {
|
1997-06-11 15:21:39 +04:00
|
|
|
if (debug)
|
1997-10-09 17:19:32 +04:00
|
|
|
printf("bad size %lu:", (u_long)fs2h32(dp->e2di_size));
|
1997-06-11 15:21:39 +04:00
|
|
|
goto unknown;
|
|
|
|
}
|
|
|
|
if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
|
|
|
|
dp = ginode(inumber);
|
1997-10-09 17:19:32 +04:00
|
|
|
dp->e2di_size = h2fs32(sblock.e2fs_bsize);
|
|
|
|
dp->e2di_mode = h2fs16(IFREG|0600);
|
1997-06-11 15:21:39 +04:00
|
|
|
inodirty();
|
|
|
|
}
|
1997-10-09 17:19:32 +04:00
|
|
|
ndb = howmany(fs2h32(dp->e2di_size), sblock.e2fs_bsize);
|
1997-06-11 15:21:39 +04:00
|
|
|
if (ndb < 0) {
|
|
|
|
if (debug)
|
1997-07-10 08:52:39 +04:00
|
|
|
printf("bad size %lu ndb %d:",
|
1997-10-09 17:19:32 +04:00
|
|
|
(u_long)fs2h32(dp->e2di_size), ndb);
|
1997-06-11 15:21:39 +04:00
|
|
|
goto unknown;
|
|
|
|
}
|
|
|
|
if (mode == IFBLK || mode == IFCHR)
|
|
|
|
ndb++;
|
|
|
|
if (mode == IFLNK) {
|
|
|
|
/*
|
|
|
|
* Fake ndb value so direct/indirect block checks below
|
|
|
|
* will detect any garbage after symlink string.
|
|
|
|
*/
|
1997-10-09 17:19:32 +04:00
|
|
|
if (fs2h32(dp->e2di_size) < EXT2_MAXSYMLINKLEN ||
|
1997-06-11 15:21:39 +04:00
|
|
|
(EXT2_MAXSYMLINKLEN == 0 && dp->e2di_blocks == 0)) {
|
1997-10-09 17:19:32 +04:00
|
|
|
ndb = howmany(fs2h32(dp->e2di_size), sizeof(u_int32_t));
|
1997-06-11 15:21:39 +04:00
|
|
|
if (ndb > NDADDR) {
|
|
|
|
j = ndb - NDADDR;
|
|
|
|
for (ndb = 1; j > 1; j--)
|
|
|
|
ndb *= NINDIR(&sblock);
|
|
|
|
ndb += NDADDR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-02-17 16:11:19 +03:00
|
|
|
/* Linux puts things in blocks for FIFO, so skip this check */
|
|
|
|
if (mode != IFIFO) {
|
|
|
|
for (j = ndb; j < NDADDR; j++)
|
|
|
|
if (dp->e2di_blocks[j] != 0) {
|
|
|
|
if (debug)
|
|
|
|
printf("bad direct addr: %d\n",
|
|
|
|
fs2h32(dp->e2di_blocks[j]));
|
|
|
|
goto unknown;
|
|
|
|
}
|
|
|
|
for (j = 0, ndb -= NDADDR; ndb > 0; j++)
|
|
|
|
ndb /= NINDIR(&sblock);
|
|
|
|
for (; j < NIADDR; j++) {
|
|
|
|
if (dp->e2di_blocks[j+NDADDR] != 0) {
|
|
|
|
if (debug)
|
|
|
|
printf("bad indirect addr: %d\n",
|
|
|
|
fs2h32(dp->e2di_blocks[j+NDADDR]));
|
|
|
|
goto unknown;
|
|
|
|
}
|
1997-06-11 15:21:39 +04:00
|
|
|
}
|
1999-02-17 16:11:19 +03:00
|
|
|
}
|
1997-06-11 15:21:39 +04:00
|
|
|
if (ftypeok(dp) == 0)
|
|
|
|
goto unknown;
|
|
|
|
n_files++;
|
1997-10-09 17:19:32 +04:00
|
|
|
lncntp[inumber] = fs2h16(dp->e2di_nlink);
|
|
|
|
if (dp->e2di_nlink == 0) {
|
1997-06-11 15:21:39 +04:00
|
|
|
zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
|
|
|
|
if (zlnp == NULL) {
|
|
|
|
pfatal("LINK COUNT TABLE OVERFLOW");
|
|
|
|
if (reply("CONTINUE") == 0)
|
|
|
|
errexit("%s\n", "");
|
|
|
|
} else {
|
|
|
|
zlnp->zlncnt = inumber;
|
|
|
|
zlnp->next = zlnhead;
|
|
|
|
zlnhead = zlnp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mode == IFDIR) {
|
|
|
|
if (dp->e2di_size == 0)
|
|
|
|
statemap[inumber] = DCLEAR;
|
|
|
|
else
|
|
|
|
statemap[inumber] = DSTATE;
|
|
|
|
cacheino(dp, inumber);
|
|
|
|
} else {
|
|
|
|
statemap[inumber] = FSTATE;
|
|
|
|
}
|
2000-01-28 19:01:46 +03:00
|
|
|
typemap[inumber] = E2IFTODT(mode);
|
1997-06-11 15:21:39 +04:00
|
|
|
badblk = dupblk = 0;
|
|
|
|
idesc->id_number = inumber;
|
|
|
|
(void)ckinode(dp, idesc);
|
|
|
|
idesc->id_entryno *= btodb(sblock.e2fs_bsize);
|
1997-10-09 17:19:32 +04:00
|
|
|
if (fs2h32(dp->e2di_nblock) != idesc->id_entryno) {
|
1997-06-11 15:21:39 +04:00
|
|
|
pwarn("INCORRECT BLOCK COUNT I=%u (%d should be %d)",
|
1997-10-09 17:19:32 +04:00
|
|
|
inumber, fs2h32(dp->e2di_nblock), idesc->id_entryno);
|
1997-06-11 15:21:39 +04:00
|
|
|
if (preen)
|
|
|
|
printf(" (CORRECTED)\n");
|
|
|
|
else if (reply("CORRECT") == 0)
|
|
|
|
return;
|
|
|
|
dp = ginode(inumber);
|
1997-10-09 17:19:32 +04:00
|
|
|
dp->e2di_nblock = h2fs32(idesc->id_entryno);
|
1997-06-11 15:21:39 +04:00
|
|
|
inodirty();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
unknown:
|
|
|
|
pfatal("UNKNOWN FILE TYPE I=%u", inumber);
|
|
|
|
statemap[inumber] = FCLEAR;
|
|
|
|
if (reply("CLEAR") == 1) {
|
|
|
|
statemap[inumber] = USTATE;
|
|
|
|
dp = ginode(inumber);
|
|
|
|
clearinode(dp);
|
|
|
|
inodirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
pass1check(idesc)
|
1997-09-14 18:27:23 +04:00
|
|
|
struct inodesc *idesc;
|
1997-06-11 15:21:39 +04:00
|
|
|
{
|
|
|
|
int res = KEEPON;
|
|
|
|
int anyout, nfrags;
|
|
|
|
daddr_t blkno = idesc->id_blkno;
|
1997-09-14 18:27:23 +04:00
|
|
|
struct dups *dlp;
|
1997-06-11 15:21:39 +04:00
|
|
|
struct dups *new;
|
|
|
|
|
|
|
|
if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
|
|
|
|
blkerror(idesc->id_number, "BAD", blkno);
|
|
|
|
if (badblk++ >= MAXBAD) {
|
|
|
|
pwarn("EXCESSIVE BAD BLKS I=%u",
|
|
|
|
idesc->id_number);
|
|
|
|
if (preen)
|
|
|
|
printf(" (SKIPPING)\n");
|
|
|
|
else if (reply("CONTINUE") == 0)
|
|
|
|
errexit("%s\n", "");
|
|
|
|
return (STOP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
|
|
|
|
if (anyout && chkrange(blkno, 1)) {
|
|
|
|
res = SKIP;
|
|
|
|
} else if (!testbmap(blkno)) {
|
|
|
|
n_blks++;
|
|
|
|
setbmap(blkno);
|
|
|
|
} else {
|
|
|
|
blkerror(idesc->id_number, "DUP", blkno);
|
|
|
|
if (dupblk++ >= MAXDUP) {
|
|
|
|
pwarn("EXCESSIVE DUP BLKS I=%u",
|
|
|
|
idesc->id_number);
|
|
|
|
if (preen)
|
|
|
|
printf(" (SKIPPING)\n");
|
|
|
|
else if (reply("CONTINUE") == 0)
|
|
|
|
errexit("%s\n", "");
|
|
|
|
return (STOP);
|
|
|
|
}
|
|
|
|
new = (struct dups *)malloc(sizeof(struct dups));
|
|
|
|
if (new == NULL) {
|
|
|
|
pfatal("DUP TABLE OVERFLOW.");
|
|
|
|
if (reply("CONTINUE") == 0)
|
|
|
|
errexit("%s\n", "");
|
|
|
|
return (STOP);
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* count the number of blocks found in id_entryno
|
|
|
|
*/
|
|
|
|
idesc->id_entryno++;
|
|
|
|
}
|
|
|
|
return (res);
|
|
|
|
}
|