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