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