1 /* 2 * Copyright (c) 1980, 1986, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 static const char sccsid[] = "@(#)pass1.c 8.1 (Berkeley) 6/5/93"; 36 #endif /* not lint */ 37 38 #include <sys/param.h> 39 #include <sys/time.h> 40 #include <ufs/ufs/dinode.h> 41 #include <ufs/ufs/dir.h> 42 #include <ufs/ffs/fs.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <string.h> 46 #include "fsck.h" 47 48 static daddr_t badblk; 49 static daddr_t dupblk; 50 51 static void checkinode __P((ino_t inumber, struct inodesc *idesc)); 52 53 void 54 pass1() 55 { 56 ino_t inumber; 57 int c, i, cgd; 58 struct inodesc idesc; 59 60 /* 61 * Set file system reserved blocks in used block map. 62 */ 63 for (c = 0; c < sblock.fs_ncg; c++) { 64 cgd = cgdmin(&sblock, c); 65 if (c == 0) { 66 i = cgbase(&sblock, c); 67 cgd += howmany(sblock.fs_cssize, sblock.fs_fsize); 68 } else 69 i = cgsblock(&sblock, c); 70 for (; i < cgd; i++) 71 setbmap(i); 72 } 73 /* 74 * Find all allocated blocks. 75 */ 76 bzero((char *)&idesc, sizeof(struct inodesc)); 77 idesc.id_type = ADDR; 78 idesc.id_func = pass1check; 79 inumber = 0; 80 n_files = n_blks = 0; 81 resetinodebuf(); 82 for (c = 0; c < sblock.fs_ncg; c++) { 83 for (i = 0; i < sblock.fs_ipg; i++, inumber++) { 84 if (inumber < ROOTINO) 85 continue; 86 checkinode(inumber, &idesc); 87 } 88 } 89 freeinodebuf(); 90 } 91 92 void 93 checkinode(inumber, idesc) 94 ino_t inumber; 95 register struct inodesc *idesc; 96 { 97 register struct dinode *dp; 98 struct zlncnt *zlnp; 99 int ndb, j; 100 mode_t mode; 101 char *symbuf; 102 103 dp = getnextinode(inumber); 104 mode = dp->di_mode & IFMT; 105 if (mode == 0) { 106 if (bcmp((char *)dp->di_db, (char *)zino.di_db, 107 NDADDR * sizeof(daddr_t)) || 108 bcmp((char *)dp->di_ib, (char *)zino.di_ib, 109 NIADDR * sizeof(daddr_t)) || 110 dp->di_mode || dp->di_size) { 111 pfatal("PARTIALLY ALLOCATED INODE I=%lu", inumber); 112 if (reply("CLEAR") == 1) { 113 dp = ginode(inumber); 114 clearinode(dp); 115 inodirty(); 116 } 117 } 118 statemap[inumber] = USTATE; 119 return; 120 } 121 lastino = inumber; 122 if (/* dp->di_size < 0 || */ 123 dp->di_size + sblock.fs_bsize - 1 < dp->di_size) { 124 if (debug) 125 printf("bad size %qu:", dp->di_size); 126 goto unknown; 127 } 128 if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) { 129 dp = ginode(inumber); 130 dp->di_size = sblock.fs_fsize; 131 dp->di_mode = IFREG|0600; 132 inodirty(); 133 } 134 ndb = howmany(dp->di_size, sblock.fs_bsize); 135 if (ndb < 0) { 136 if (debug) 137 printf("bad size %qu ndb %d:", 138 dp->di_size, ndb); 139 goto unknown; 140 } 141 if (mode == IFBLK || mode == IFCHR) 142 ndb++; 143 if (mode == IFLNK) { 144 if (doinglevel2 && 145 dp->di_size > 0 && dp->di_size < MAXSYMLINKLEN && 146 dp->di_blocks != 0) { 147 symbuf = alloca(secsize); 148 if (bread(fsreadfd, symbuf, 149 fsbtodb(&sblock, dp->di_db[0]), 150 (long)secsize) != 0) 151 errexit("cannot read symlink"); 152 if (debug) { 153 symbuf[dp->di_size] = 0; 154 printf("convert symlink %ld(%s) of size %ld\n", 155 inumber, symbuf, (long)dp->di_size); 156 } 157 dp = ginode(inumber); 158 bcopy(symbuf, (caddr_t)dp->di_shortlink, 159 (long)dp->di_size); 160 dp->di_blocks = 0; 161 inodirty(); 162 } 163 /* 164 * Fake ndb value so direct/indirect block checks below 165 * will detect any garbage after symlink string. 166 */ 167 if ((dp->di_size < sblock.fs_maxsymlinklen) || dp->di_blocks == 0) { 168 ndb = howmany(dp->di_size, sizeof(daddr_t)); 169 if (ndb > NDADDR) { 170 j = ndb - NDADDR; 171 for (ndb = 1; j > 1; j--) 172 ndb *= NINDIR(&sblock); 173 ndb += NDADDR; 174 } 175 } 176 } 177 for (j = ndb; j < NDADDR; j++) 178 if (dp->di_db[j] != 0) { 179 if (debug) 180 printf("bad direct addr: %ld\n", dp->di_db[j]); 181 goto unknown; 182 } 183 for (j = 0, ndb -= NDADDR; ndb > 0; j++) 184 ndb /= NINDIR(&sblock); 185 for (; j < NIADDR; j++) 186 if (dp->di_ib[j] != 0) { 187 if (debug) 188 printf("bad indirect addr: %ld\n", 189 dp->di_ib[j]); 190 goto unknown; 191 } 192 if (ftypeok(dp) == 0) 193 goto unknown; 194 n_files++; 195 lncntp[inumber] = dp->di_nlink; 196 if (dp->di_nlink <= 0) { 197 zlnp = (struct zlncnt *)malloc(sizeof *zlnp); 198 if (zlnp == NULL) { 199 pfatal("LINK COUNT TABLE OVERFLOW"); 200 if (reply("CONTINUE") == 0) 201 errexit(""); 202 } else { 203 zlnp->zlncnt = inumber; 204 zlnp->next = zlnhead; 205 zlnhead = zlnp; 206 } 207 } 208 if (mode == IFDIR) { 209 if (dp->di_size == 0) 210 statemap[inumber] = DCLEAR; 211 else 212 statemap[inumber] = DSTATE; 213 cacheino(dp, inumber); 214 } else 215 statemap[inumber] = FSTATE; 216 typemap[inumber] = IFTODT(mode); 217 if (doinglevel2 && 218 (dp->di_ouid != (u_short)-1 || dp->di_ogid != (u_short)-1)) { 219 dp = ginode(inumber); 220 dp->di_uid = dp->di_ouid; 221 dp->di_ouid = -1; 222 dp->di_gid = dp->di_ogid; 223 dp->di_ogid = -1; 224 inodirty(); 225 } 226 badblk = dupblk = 0; 227 idesc->id_number = inumber; 228 (void)ckinode(dp, idesc); 229 idesc->id_entryno *= btodb(sblock.fs_fsize); 230 if (dp->di_blocks != idesc->id_entryno) { 231 pwarn("INCORRECT BLOCK COUNT I=%lu (%ld should be %ld)", 232 inumber, dp->di_blocks, idesc->id_entryno); 233 if (preen) 234 printf(" (CORRECTED)\n"); 235 else if (reply("CORRECT") == 0) 236 return; 237 dp = ginode(inumber); 238 dp->di_blocks = idesc->id_entryno; 239 inodirty(); 240 } 241 return; 242 unknown: 243 pfatal("UNKNOWN FILE TYPE I=%lu", inumber); 244 statemap[inumber] = FCLEAR; 245 if (reply("CLEAR") == 1) { 246 statemap[inumber] = USTATE; 247 dp = ginode(inumber); 248 clearinode(dp); 249 inodirty(); 250 } 251 } 252 253 int 254 pass1check(idesc) 255 register struct inodesc *idesc; 256 { 257 int res = KEEPON; 258 int anyout, nfrags; 259 daddr_t blkno = idesc->id_blkno; 260 register struct dups *dlp; 261 struct dups *new; 262 263 if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) { 264 blkerror(idesc->id_number, "BAD", blkno); 265 if (badblk++ >= MAXBAD) { 266 pwarn("EXCESSIVE BAD BLKS I=%lu", 267 idesc->id_number); 268 if (preen) 269 printf(" (SKIPPING)\n"); 270 else if (reply("CONTINUE") == 0) 271 errexit(""); 272 return (STOP); 273 } 274 } 275 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) { 276 if (anyout && chkrange(blkno, 1)) { 277 res = SKIP; 278 } else if (!testbmap(blkno)) { 279 n_blks++; 280 setbmap(blkno); 281 } else { 282 blkerror(idesc->id_number, "DUP", blkno); 283 if (dupblk++ >= MAXDUP) { 284 pwarn("EXCESSIVE DUP BLKS I=%lu", 285 idesc->id_number); 286 if (preen) 287 printf(" (SKIPPING)\n"); 288 else if (reply("CONTINUE") == 0) 289 errexit(""); 290 return (STOP); 291 } 292 new = (struct dups *)malloc(sizeof(struct dups)); 293 if (new == NULL) { 294 pfatal("DUP TABLE OVERFLOW."); 295 if (reply("CONTINUE") == 0) 296 errexit(""); 297 return (STOP); 298 } 299 new->dup = blkno; 300 if (muldup == 0) { 301 duplist = muldup = new; 302 new->next = 0; 303 } else { 304 new->next = muldup->next; 305 muldup->next = new; 306 } 307 for (dlp = duplist; dlp != muldup; dlp = dlp->next) 308 if (dlp->dup == blkno) 309 break; 310 if (dlp == muldup && dlp->dup != blkno) 311 muldup = new; 312 } 313 /* 314 * count the number of blocks found in id_entryno 315 */ 316 idesc->id_entryno++; 317 } 318 return (res); 319 } 320