1*a7b9eedcSflorian /* $OpenBSD: pass1.c,v 1.19 2024/05/09 08:35:40 florian Exp $ */
20190393fSart /* $NetBSD: pass1.c,v 1.9 2000/01/31 11:40:12 bouyer Exp $ */
38c424e8eSdownsj
48c424e8eSdownsj /*
55ff4e0c8Sdownsj * Copyright (c) 1997 Manuel Bouyer.
68c424e8eSdownsj * Copyright (c) 1980, 1986, 1993
78c424e8eSdownsj * The Regents of the University of California. All rights reserved.
88c424e8eSdownsj *
98c424e8eSdownsj * Redistribution and use in source and binary forms, with or without
108c424e8eSdownsj * modification, are permitted provided that the following conditions
118c424e8eSdownsj * are met:
128c424e8eSdownsj * 1. Redistributions of source code must retain the above copyright
138c424e8eSdownsj * notice, this list of conditions and the following disclaimer.
148c424e8eSdownsj * 2. Redistributions in binary form must reproduce the above copyright
158c424e8eSdownsj * notice, this list of conditions and the following disclaimer in the
168c424e8eSdownsj * documentation and/or other materials provided with the distribution.
171ef0d710Smillert * 3. Neither the name of the University nor the names of its contributors
188c424e8eSdownsj * may be used to endorse or promote products derived from this software
198c424e8eSdownsj * without specific prior written permission.
208c424e8eSdownsj *
218c424e8eSdownsj * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
228c424e8eSdownsj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
238c424e8eSdownsj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
248c424e8eSdownsj * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
258c424e8eSdownsj * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
268c424e8eSdownsj * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
278c424e8eSdownsj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
288c424e8eSdownsj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
298c424e8eSdownsj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
308c424e8eSdownsj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
318c424e8eSdownsj * SUCH DAMAGE.
328c424e8eSdownsj */
338c424e8eSdownsj
34b9fc9a72Sderaadt #include <sys/param.h> /* setbit btodb isset */
358c424e8eSdownsj #include <sys/time.h>
368c424e8eSdownsj #include <ufs/ext2fs/ext2fs_dinode.h>
378c424e8eSdownsj #include <ufs/ext2fs/ext2fs_dir.h>
388c424e8eSdownsj #include <ufs/ext2fs/ext2fs.h>
398c424e8eSdownsj
408c424e8eSdownsj #include <ufs/ufs/dinode.h> /* for IFMT & friends */
418c424e8eSdownsj
428c424e8eSdownsj #include <stdio.h>
438c424e8eSdownsj #include <stdlib.h>
448c424e8eSdownsj #include <string.h>
4565348f21Sjasoni #include <time.h>
468c424e8eSdownsj
478c424e8eSdownsj #include "fsck.h"
488c424e8eSdownsj #include "extern.h"
498c424e8eSdownsj #include "fsutil.h"
508c424e8eSdownsj
51b6d2e2d5Sderaadt static daddr32_t badblk;
52b6d2e2d5Sderaadt static daddr32_t dupblk;
53c72b5b24Smillert static void checkinode(ino_t, struct inodesc *);
548c424e8eSdownsj
558c424e8eSdownsj void
pass1(void)568809fabbSderaadt pass1(void)
578c424e8eSdownsj {
588c424e8eSdownsj ino_t inumber;
5965348f21Sjasoni int c, i;
60b6d2e2d5Sderaadt daddr32_t dbase;
618c424e8eSdownsj struct inodesc idesc;
628c424e8eSdownsj
638c424e8eSdownsj /*
648c424e8eSdownsj * Set file system reserved blocks in used block map.
658c424e8eSdownsj */
668c424e8eSdownsj for (c = 0; c < sblock.e2fs_ncg; c++) {
6765348f21Sjasoni dbase = c * sblock.e2fs.e2fs_bpg +
6865348f21Sjasoni sblock.e2fs.e2fs_first_dblock;
6965348f21Sjasoni /* Mark the blocks used for the inode table */
7060a51e06Spelikan if (letoh32(sblock.e2fs_gd[c].ext2bgd_i_tables) >= dbase) {
7165348f21Sjasoni for (i = 0; i < sblock.e2fs_itpg; i++)
7265348f21Sjasoni setbmap(
7360a51e06Spelikan letoh32(sblock.e2fs_gd[c].ext2bgd_i_tables)
7465348f21Sjasoni + i);
7565348f21Sjasoni }
7665348f21Sjasoni /* Mark the blocks used for the block bitmap */
7760a51e06Spelikan if (letoh32(sblock.e2fs_gd[c].ext2bgd_b_bitmap) >= dbase)
7860a51e06Spelikan setbmap(letoh32(sblock.e2fs_gd[c].ext2bgd_b_bitmap));
7965348f21Sjasoni /* Mark the blocks used for the inode bitmap */
8060a51e06Spelikan if (letoh32(sblock.e2fs_gd[c].ext2bgd_i_bitmap) >= dbase)
8160a51e06Spelikan setbmap(letoh32(sblock.e2fs_gd[c].ext2bgd_i_bitmap));
828c424e8eSdownsj
8365348f21Sjasoni if (sblock.e2fs.e2fs_rev == E2FS_REV0 ||
8465348f21Sjasoni (sblock.e2fs.e2fs_features_rocompat &
85bf2afe82Skevlo EXT2F_ROCOMPAT_SPARSE_SUPER) == 0 ||
8665348f21Sjasoni cg_has_sb(c)) {
8765348f21Sjasoni /* Mark copuy of SB and descriptors */
8865348f21Sjasoni setbmap(dbase);
8965348f21Sjasoni for (i = 1; i <= sblock.e2fs_ngdb; i++)
9065348f21Sjasoni setbmap(dbase+i);
9165348f21Sjasoni }
9265348f21Sjasoni
9365348f21Sjasoni
9465348f21Sjasoni if (c == 0) {
9565348f21Sjasoni for(i = 0; i < dbase; i++)
968c424e8eSdownsj setbmap(i);
978c424e8eSdownsj }
9865348f21Sjasoni }
998c424e8eSdownsj
1008c424e8eSdownsj /*
1018c424e8eSdownsj * Find all allocated blocks.
1028c424e8eSdownsj */
1038c424e8eSdownsj memset(&idesc, 0, sizeof(struct inodesc));
1048c424e8eSdownsj idesc.id_type = ADDR;
1058c424e8eSdownsj idesc.id_func = pass1check;
1068c424e8eSdownsj inumber = 1;
1078c424e8eSdownsj n_files = n_blks = 0;
1088c424e8eSdownsj resetinodebuf();
1098c424e8eSdownsj for (c = 0; c < sblock.e2fs_ncg; c++) {
1108c424e8eSdownsj for (i = 0;
1118c424e8eSdownsj i < sblock.e2fs.e2fs_ipg && inumber <= sblock.e2fs.e2fs_icount;
1128c424e8eSdownsj i++, inumber++) {
1138c424e8eSdownsj if (inumber < EXT2_ROOTINO) /* XXX */
1148c424e8eSdownsj continue;
1158c424e8eSdownsj checkinode(inumber, &idesc);
1168c424e8eSdownsj }
1178c424e8eSdownsj }
1188c424e8eSdownsj freeinodebuf();
1198c424e8eSdownsj }
1208c424e8eSdownsj
1218c424e8eSdownsj static void
checkinode(ino_t inumber,struct inodesc * idesc)1228809fabbSderaadt checkinode(ino_t inumber, struct inodesc *idesc)
1238c424e8eSdownsj {
1240190393fSart struct ext2fs_dinode *dp;
1258c424e8eSdownsj struct zlncnt *zlnp;
1268c424e8eSdownsj int ndb, j;
1278c424e8eSdownsj mode_t mode;
1288c424e8eSdownsj
1298c424e8eSdownsj dp = getnextinode(inumber);
1308c424e8eSdownsj if (inumber < EXT2_FIRSTINO && inumber != EXT2_ROOTINO)
1318c424e8eSdownsj return;
1328c424e8eSdownsj
13360a51e06Spelikan mode = letoh16(dp->e2di_mode) & IFMT;
1348c424e8eSdownsj if (mode == 0 || (dp->e2di_dtime != 0 && dp->e2di_nlink == 0)) {
1358c424e8eSdownsj if (mode == 0 && (
1368c424e8eSdownsj memcmp(dp->e2di_blocks, zino.e2di_blocks,
1378c424e8eSdownsj (NDADDR + NIADDR) * sizeof(u_int32_t)) ||
138935730fbSniallo dp->e2di_mode || inosize(dp))) {
1393b92bd08Sderaadt pfatal("PARTIALLY ALLOCATED INODE I=%llu",
1403b92bd08Sderaadt (unsigned long long)inumber);
1418c424e8eSdownsj if (reply("CLEAR") == 1) {
1428c424e8eSdownsj dp = ginode(inumber);
1438c424e8eSdownsj clearinode(dp);
1448c424e8eSdownsj inodirty();
1458c424e8eSdownsj }
1468c424e8eSdownsj }
1478c424e8eSdownsj #ifdef notyet /* it seems that dtime == 0 is valid for a unallocated inode */
1488c424e8eSdownsj if (dp->e2di_dtime == 0) {
1493b92bd08Sderaadt pwarn("DELETED INODE I=%llu HAS A NULL DTIME",
1503b92bd08Sderaadt (unsigned long long)inumber);
1518c424e8eSdownsj if (preen) {
1528c424e8eSdownsj printf(" (CORRECTED)\n");
1538c424e8eSdownsj }
1548c424e8eSdownsj if (preen || reply("CORRECT")) {
1558c424e8eSdownsj time_t t;
1568c424e8eSdownsj time(&t);
15760a51e06Spelikan dp->e2di_dtime = htole32(t);
1588c424e8eSdownsj dp = ginode(inumber);
1598c424e8eSdownsj inodirty();
1608c424e8eSdownsj }
1618c424e8eSdownsj }
1628c424e8eSdownsj #endif
1638c424e8eSdownsj statemap[inumber] = USTATE;
1648c424e8eSdownsj return;
1658c424e8eSdownsj }
1668c424e8eSdownsj lastino = inumber;
1678c424e8eSdownsj if (dp->e2di_dtime != 0) {
16860a51e06Spelikan time_t t = letoh32(dp->e2di_dtime);
1698c424e8eSdownsj char *p = ctime(&t);
170*a7b9eedcSflorian if (p)
1713b92bd08Sderaadt pwarn("INODE I=%llu HAS DTIME=%12.12s %4.4s",
1723b92bd08Sderaadt (unsigned long long)inumber, &p[4], &p[20]);
173*a7b9eedcSflorian else
174*a7b9eedcSflorian pwarn("INODE I=%llu HAS DTIME=%lld",
175*a7b9eedcSflorian (unsigned long long)inumber, t);
1768c424e8eSdownsj if (preen) {
1778c424e8eSdownsj printf(" (CORRECTED)\n");
1788c424e8eSdownsj }
1798c424e8eSdownsj if (preen || reply("CORRECT")) {
1808c424e8eSdownsj dp = ginode(inumber);
1818c424e8eSdownsj dp->e2di_dtime = 0;
1828c424e8eSdownsj inodirty();
1838c424e8eSdownsj }
1848c424e8eSdownsj }
185935730fbSniallo if (inosize(dp) + sblock.e2fs_bsize - 1 < inosize(dp)) {
1868c424e8eSdownsj if (debug)
187935730fbSniallo printf("bad size %llu:", (unsigned long long)inosize(dp));
1888c424e8eSdownsj goto unknown;
1898c424e8eSdownsj }
1908c424e8eSdownsj if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
1918c424e8eSdownsj dp = ginode(inumber);
19260a51e06Spelikan dp->e2di_mode = htole16(IFREG|0600);
193935730fbSniallo inossize(dp, sblock.e2fs_bsize);
1948c424e8eSdownsj inodirty();
1958c424e8eSdownsj }
196935730fbSniallo ndb = howmany(inosize(dp), sblock.e2fs_bsize);
1978c424e8eSdownsj if (ndb < 0) {
1988c424e8eSdownsj if (debug)
199935730fbSniallo printf("bad size %llu ndb %d:",
200935730fbSniallo (unsigned long long)inosize(dp), ndb);
2018c424e8eSdownsj goto unknown;
2028c424e8eSdownsj }
2038c424e8eSdownsj if (mode == IFBLK || mode == IFCHR)
2048c424e8eSdownsj ndb++;
2058c424e8eSdownsj if (mode == IFLNK) {
2068c424e8eSdownsj /*
2078c424e8eSdownsj * Fake ndb value so direct/indirect block checks below
2088c424e8eSdownsj * will detect any garbage after symlink string.
2098c424e8eSdownsj */
210935730fbSniallo if (inosize(dp) < EXT2_MAXSYMLINKLEN ||
2115814667eSotto (EXT2_MAXSYMLINKLEN == 0 && htole32(dp->e2di_nblock) == 0)) {
212935730fbSniallo ndb = howmany(inosize(dp), sizeof(u_int32_t));
2138c424e8eSdownsj if (ndb > NDADDR) {
2148c424e8eSdownsj j = ndb - NDADDR;
2158c424e8eSdownsj for (ndb = 1; j > 1; j--)
2168c424e8eSdownsj ndb *= NINDIR(&sblock);
2178c424e8eSdownsj ndb += NDADDR;
2188c424e8eSdownsj }
2198c424e8eSdownsj }
2208c424e8eSdownsj }
22165348f21Sjasoni /* Linux puts things in blocks for FIFO, so skip this check */
22265348f21Sjasoni if (mode != IFIFO) {
2238c424e8eSdownsj for (j = ndb; j < NDADDR; j++)
2248c424e8eSdownsj if (dp->e2di_blocks[j] != 0) {
2258c424e8eSdownsj if (debug)
22665348f21Sjasoni printf("bad direct addr: %d\n",
22760a51e06Spelikan letoh32(dp->e2di_blocks[j]));
2288c424e8eSdownsj goto unknown;
2298c424e8eSdownsj }
2308c424e8eSdownsj for (j = 0, ndb -= NDADDR; ndb > 0; j++)
2318c424e8eSdownsj ndb /= NINDIR(&sblock);
23265348f21Sjasoni for (; j < NIADDR; j++) {
2338c424e8eSdownsj if (dp->e2di_blocks[j+NDADDR] != 0) {
2348c424e8eSdownsj if (debug)
2358c424e8eSdownsj printf("bad indirect addr: %d\n",
23660a51e06Spelikan letoh32(dp->e2di_blocks[j+NDADDR]));
2378c424e8eSdownsj goto unknown;
2388c424e8eSdownsj }
23986e0126cSderaadt }
24065348f21Sjasoni }
2418c424e8eSdownsj if (ftypeok(dp) == 0)
2428c424e8eSdownsj goto unknown;
2438c424e8eSdownsj n_files++;
24460a51e06Spelikan lncntp[inumber] = letoh16(dp->e2di_nlink);
24565348f21Sjasoni if (dp->e2di_nlink == 0) {
2465ae94ef8Sderaadt zlnp = malloc(sizeof *zlnp);
2478c424e8eSdownsj if (zlnp == NULL) {
2488c424e8eSdownsj pfatal("LINK COUNT TABLE OVERFLOW");
2498c424e8eSdownsj if (reply("CONTINUE") == 0)
2508c424e8eSdownsj errexit("%s\n", "");
2518c424e8eSdownsj } else {
2528c424e8eSdownsj zlnp->zlncnt = inumber;
2538c424e8eSdownsj zlnp->next = zlnhead;
2548c424e8eSdownsj zlnhead = zlnp;
2558c424e8eSdownsj }
2568c424e8eSdownsj }
2578c424e8eSdownsj if (mode == IFDIR) {
258935730fbSniallo if (inosize(dp) == 0)
2598c424e8eSdownsj statemap[inumber] = DCLEAR;
2608c424e8eSdownsj else
2618c424e8eSdownsj statemap[inumber] = DSTATE;
2628c424e8eSdownsj cacheino(dp, inumber);
2638c424e8eSdownsj } else {
2648c424e8eSdownsj statemap[inumber] = FSTATE;
2658c424e8eSdownsj }
26665348f21Sjasoni typemap[inumber] = E2IFTODT(mode);
2678c424e8eSdownsj badblk = dupblk = 0;
2688c424e8eSdownsj idesc->id_number = inumber;
2698c424e8eSdownsj (void)ckinode(dp, idesc);
2708c424e8eSdownsj idesc->id_entryno *= btodb(sblock.e2fs_bsize);
27160a51e06Spelikan if (letoh32(dp->e2di_nblock) != idesc->id_entryno) {
2723b92bd08Sderaadt pwarn("INCORRECT BLOCK COUNT I=%llu (%d should be %d)",
2733b92bd08Sderaadt (unsigned long long)inumber,
27460a51e06Spelikan letoh32(dp->e2di_nblock), idesc->id_entryno);
2758c424e8eSdownsj if (preen)
2768c424e8eSdownsj printf(" (CORRECTED)\n");
2778c424e8eSdownsj else if (reply("CORRECT") == 0)
2788c424e8eSdownsj return;
2798c424e8eSdownsj dp = ginode(inumber);
28060a51e06Spelikan dp->e2di_nblock = htole32(idesc->id_entryno);
2818c424e8eSdownsj inodirty();
2828c424e8eSdownsj }
2838c424e8eSdownsj return;
2848c424e8eSdownsj unknown:
2853b92bd08Sderaadt pfatal("UNKNOWN FILE TYPE I=%llu", (unsigned long long)inumber);
2868c424e8eSdownsj statemap[inumber] = FCLEAR;
2878c424e8eSdownsj if (reply("CLEAR") == 1) {
2888c424e8eSdownsj statemap[inumber] = USTATE;
2898c424e8eSdownsj dp = ginode(inumber);
2908c424e8eSdownsj clearinode(dp);
2918c424e8eSdownsj inodirty();
2928c424e8eSdownsj }
2938c424e8eSdownsj }
2948c424e8eSdownsj
2958c424e8eSdownsj int
pass1check(struct inodesc * idesc)2968809fabbSderaadt pass1check(struct inodesc *idesc)
2978c424e8eSdownsj {
2988c424e8eSdownsj int res = KEEPON;
2998c424e8eSdownsj int anyout, nfrags;
300b6d2e2d5Sderaadt daddr32_t blkno = idesc->id_blkno;
3010190393fSart struct dups *dlp;
3028c424e8eSdownsj struct dups *new;
3038c424e8eSdownsj
3048c424e8eSdownsj if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
3058c424e8eSdownsj blkerror(idesc->id_number, "BAD", blkno);
3068c424e8eSdownsj if (badblk++ >= MAXBAD) {
3073b92bd08Sderaadt pwarn("EXCESSIVE BAD BLKS I=%llu",
3083b92bd08Sderaadt (unsigned long long)idesc->id_number);
3098c424e8eSdownsj if (preen)
3108c424e8eSdownsj printf(" (SKIPPING)\n");
3118c424e8eSdownsj else if (reply("CONTINUE") == 0)
3128c424e8eSdownsj errexit("%s\n", "");
3138c424e8eSdownsj return (STOP);
3148c424e8eSdownsj }
3158c424e8eSdownsj }
3168c424e8eSdownsj for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
3178c424e8eSdownsj if (anyout && chkrange(blkno, 1)) {
3188c424e8eSdownsj res = SKIP;
3198c424e8eSdownsj } else if (!testbmap(blkno)) {
3208c424e8eSdownsj n_blks++;
3218c424e8eSdownsj setbmap(blkno);
3228c424e8eSdownsj } else {
3238c424e8eSdownsj blkerror(idesc->id_number, "DUP", blkno);
3248c424e8eSdownsj if (dupblk++ >= MAXDUP) {
3253b92bd08Sderaadt pwarn("EXCESSIVE DUP BLKS I=%lluu",
3263b92bd08Sderaadt (unsigned long long)idesc->id_number);
3278c424e8eSdownsj if (preen)
3288c424e8eSdownsj printf(" (SKIPPING)\n");
3298c424e8eSdownsj else if (reply("CONTINUE") == 0)
3308c424e8eSdownsj errexit("%s\n", "");
3318c424e8eSdownsj return (STOP);
3328c424e8eSdownsj }
3335ae94ef8Sderaadt new = malloc(sizeof(struct dups));
3348c424e8eSdownsj if (new == NULL) {
3358c424e8eSdownsj pfatal("DUP TABLE OVERFLOW.");
3368c424e8eSdownsj if (reply("CONTINUE") == 0)
3378c424e8eSdownsj errexit("%s\n", "");
3388c424e8eSdownsj return (STOP);
3398c424e8eSdownsj }
3408c424e8eSdownsj new->dup = blkno;
3418c424e8eSdownsj if (muldup == 0) {
3428c424e8eSdownsj duplist = muldup = new;
3438c424e8eSdownsj new->next = 0;
3448c424e8eSdownsj } else {
3458c424e8eSdownsj new->next = muldup->next;
3468c424e8eSdownsj muldup->next = new;
3478c424e8eSdownsj }
3488c424e8eSdownsj for (dlp = duplist; dlp != muldup; dlp = dlp->next)
3498c424e8eSdownsj if (dlp->dup == blkno)
3508c424e8eSdownsj break;
3518c424e8eSdownsj if (dlp == muldup && dlp->dup != blkno)
3528c424e8eSdownsj muldup = new;
3538c424e8eSdownsj }
3548c424e8eSdownsj /*
3558c424e8eSdownsj * count the number of blocks found in id_entryno
3568c424e8eSdownsj */
3578c424e8eSdownsj idesc->id_entryno++;
3588c424e8eSdownsj }
3598c424e8eSdownsj return (res);
3608c424e8eSdownsj }
361