1 /* $OpenBSD: setup.c,v 1.7 2001/09/18 17:43:15 art Exp $ */ 2 /* $NetBSD: setup.c,v 1.1 1997/06/11 11:22:01 bouyer Exp $ */ 3 4 /* 5 * Copyright (c) 1997 Manuel Bouyer. 6 * Copyright (c) 1980, 1986, 1993 7 * The Regents of the University of California. All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 */ 37 38 #define DKTYPENAMES 39 #include <sys/param.h> 40 #include <sys/time.h> 41 #include <ufs/ext2fs/ext2fs_dinode.h> 42 #include <ufs/ext2fs/ext2fs.h> 43 #include <sys/stat.h> 44 #include <sys/ioctl.h> 45 #include <sys/disklabel.h> 46 #include <sys/file.h> 47 48 #include <errno.h> 49 #include <fcntl.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 #include <ctype.h> 54 55 #include "fsck.h" 56 #include "extern.h" 57 #include "fsutil.h" 58 59 #define POWEROF2(num) (((num) & ((num) - 1)) == 0) 60 61 void badsb __P((int, char *)); 62 int calcsb __P((char *, int, struct m_ext2fs *)); 63 static struct disklabel *getdisklabel __P((char *, int)); 64 static int readsb __P((int)); 65 66 int 67 setup(dev) 68 char *dev; 69 { 70 long cg, asked, i; 71 long bmapsize; 72 struct disklabel *lp; 73 off_t sizepb; 74 struct stat statb; 75 struct m_ext2fs proto; 76 int doskipclean; 77 u_int64_t maxfilesize; 78 79 havesb = 0; 80 fswritefd = -1; 81 doskipclean = skipclean; 82 if (stat(dev, &statb) < 0) { 83 printf("Can't stat %s: %s\n", dev, strerror(errno)); 84 return (0); 85 } 86 if (!S_ISCHR(statb.st_mode)) { 87 pfatal("%s is not a character device", dev); 88 if (reply("CONTINUE") == 0) 89 return (0); 90 } 91 if ((fsreadfd = open(dev, O_RDONLY)) < 0) { 92 printf("Can't open %s: %s\n", dev, strerror(errno)); 93 return (0); 94 } 95 if (preen == 0) 96 printf("** %s", dev); 97 if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) { 98 fswritefd = -1; 99 if (preen) 100 pfatal("NO WRITE ACCESS"); 101 printf(" (NO WRITE)"); 102 } 103 if (preen == 0) 104 printf("\n"); 105 fsmodified = 0; 106 lfdir = 0; 107 initbarea(&sblk); 108 initbarea(&asblk); 109 sblk.b_un.b_buf = malloc(SBSIZE); 110 asblk.b_un.b_buf = malloc(SBSIZE); 111 if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL) 112 errexit("cannot allocate space for superblock\n"); 113 if ((lp = getdisklabel((char *)NULL, fsreadfd)) != NULL) 114 dev_bsize = secsize = lp->d_secsize; 115 else 116 dev_bsize = secsize = DEV_BSIZE; 117 /* 118 * Read in the superblock, looking for alternates if necessary 119 */ 120 if (readsb(1) == 0) { 121 if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0) 122 return(0); 123 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0) 124 return (0); 125 for (cg = 1; cg < proto.e2fs_ncg; cg++) { 126 bflag = fsbtodb(&proto, 127 cg * proto.e2fs.e2fs_bpg + proto.e2fs.e2fs_first_dblock); 128 if (readsb(0) != 0) 129 break; 130 } 131 if (cg >= proto.e2fs_ncg) { 132 printf("%s %s\n%s %s\n%s %s\n", 133 "SEARCH FOR ALTERNATE SUPER-BLOCK", 134 "FAILED. YOU MUST USE THE", 135 "-b OPTION TO FSCK_FFS TO SPECIFY THE", 136 "LOCATION OF AN ALTERNATE", 137 "SUPER-BLOCK TO SUPPLY NEEDED", 138 "INFORMATION; SEE fsck_ext2fs(8)."); 139 return(0); 140 } 141 doskipclean = 0; 142 pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag); 143 } 144 if (debug) 145 printf("state = %d\n", sblock.e2fs.e2fs_state); 146 if (sblock.e2fs.e2fs_state == E2FS_ISCLEAN) { 147 if (doskipclean) { 148 pwarn("%sile system is clean; not checking\n", 149 preen ? "f" : "** F"); 150 return (-1); 151 } 152 if (!preen) 153 pwarn("** File system is already clean\n"); 154 } 155 maxfsblock = sblock.e2fs.e2fs_bcount; 156 maxino = sblock.e2fs_ncg * sblock.e2fs.e2fs_ipg; 157 sizepb = sblock.e2fs_bsize; 158 maxfilesize = sblock.e2fs_bsize * NDADDR - 1; 159 for (i = 0; i < NIADDR; i++) { 160 sizepb *= NINDIR(&sblock); 161 maxfilesize += sizepb; 162 } 163 /* 164 * Check and potentially fix certain fields in the super block. 165 */ 166 if ((sblock.e2fs.e2fs_rbcount < 0) || 167 (sblock.e2fs.e2fs_rbcount > sblock.e2fs.e2fs_bcount)) { 168 pfatal("IMPOSSIBLE RESERVED BLOCK COUNT=%d IN SUPERBLOCK", 169 sblock.e2fs.e2fs_rbcount); 170 if (reply("SET TO DEFAULT") == 1) { 171 sblock.e2fs.e2fs_rbcount = sblock.e2fs.e2fs_bcount * 0.1; 172 sbdirty(); 173 dirty(&asblk); 174 } 175 } 176 if (sblock.e2fs.e2fs_bpg != sblock.e2fs.e2fs_fpg) { 177 pfatal("WRONG FPG=%d (BPG=%d) IN SUPERBLOCK", 178 sblock.e2fs.e2fs_fpg, sblock.e2fs.e2fs_bpg); 179 return 0; 180 } 181 if (asblk.b_dirty && !bflag) { 182 copyback_sb(&asblk); 183 flush(fswritefd, &asblk); 184 } 185 /* 186 * read in the summary info. 187 */ 188 189 sblock.e2fs_gd = malloc(sblock.e2fs_ngdb * sblock.e2fs_bsize); 190 if (sblock.e2fs_gd == NULL) 191 errexit("out of memory\n"); 192 asked = 0; 193 for (i=0; i < sblock.e2fs_ngdb; i++) { 194 if (bread(fsreadfd,(char *) 195 &sblock.e2fs_gd[i* sblock.e2fs_bsize / sizeof(struct ext2_gd)], 196 fsbtodb(&sblock, ((sblock.e2fs_bsize>1024)?0:1)+i+1), 197 sblock.e2fs_bsize) != 0 && !asked) { 198 pfatal("BAD SUMMARY INFORMATION"); 199 if (reply("CONTINUE") == 0) 200 errexit("%s\n", ""); 201 asked++; 202 } 203 } 204 /* 205 * allocate and initialize the necessary maps 206 */ 207 bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t)); 208 blockmap = calloc((unsigned)bmapsize, sizeof (char)); 209 if (blockmap == NULL) { 210 printf("cannot alloc %u bytes for blockmap\n", 211 (unsigned)bmapsize); 212 goto badsblabel; 213 } 214 statemap = calloc((unsigned)(maxino + 2), sizeof(char)); 215 if (statemap == NULL) { 216 printf("cannot alloc %u bytes for statemap\n", 217 (unsigned)(maxino + 1)); 218 goto badsblabel; 219 } 220 typemap = calloc((unsigned)(maxino + 1), sizeof(char)); 221 if (typemap == NULL) { 222 printf("cannot alloc %u bytes for typemap\n", 223 (unsigned)(maxino + 1)); 224 goto badsblabel; 225 } 226 lncntp = (int16_t *)calloc((unsigned)(maxino + 1), sizeof(int16_t)); 227 if (lncntp == NULL) { 228 printf("cannot alloc %u bytes for lncntp\n", 229 (unsigned)((maxino + 1) * sizeof(int16_t))); 230 goto badsblabel; 231 } 232 for (numdirs = 0, cg = 0; cg < sblock.e2fs_ncg; cg++) { 233 numdirs += fs2h16(sblock.e2fs_gd[cg].ext2bgd_ndirs); 234 } 235 inplast = 0; 236 listmax = numdirs + 10; 237 inpsort = (struct inoinfo **)calloc((unsigned)listmax, 238 sizeof(struct inoinfo *)); 239 inphead = (struct inoinfo **)calloc((unsigned)numdirs, 240 sizeof(struct inoinfo *)); 241 if (inpsort == NULL || inphead == NULL) { 242 printf("cannot alloc %u bytes for inphead\n", 243 (unsigned)(numdirs * sizeof(struct inoinfo *))); 244 goto badsblabel; 245 } 246 bufinit(); 247 return (1); 248 249 badsblabel: 250 ckfini(0); 251 return (0); 252 } 253 254 /* 255 * Read in the super block and its summary info. 256 */ 257 static int 258 readsb(listerr) 259 int listerr; 260 { 261 daddr_t super = bflag ? bflag : SBOFF / dev_bsize; 262 263 if (bread(fsreadfd, (char *)sblk.b_un.b_fs, super, (long)SBSIZE) != 0) 264 return (0); 265 sblk.b_bno = super; 266 sblk.b_size = SBSIZE; 267 268 /* Copy the superblock in memory */ 269 e2fs_sbload(sblk.b_un.b_fs, &sblock.e2fs); 270 271 /* 272 * run a few consistency checks of the super block 273 */ 274 if (sblock.e2fs.e2fs_magic != E2FS_MAGIC) { 275 badsb(listerr, "MAGIC NUMBER WRONG"); return (0); 276 } 277 if (sblock.e2fs.e2fs_log_bsize > 2) { 278 badsb(listerr, "BAD LOG_BSIZE"); return (0); 279 } 280 281 /* compute the dynamic fields of the in-memory sb */ 282 /* compute dynamic sb infos */ 283 sblock.e2fs_ncg = 284 howmany(sblock.e2fs.e2fs_bcount - sblock.e2fs.e2fs_first_dblock, 285 sblock.e2fs.e2fs_bpg); 286 /* XXX assume hw bsize = 512 */ 287 sblock.e2fs_fsbtodb = sblock.e2fs.e2fs_log_bsize + 1; 288 sblock.e2fs_bsize = 1024 << sblock.e2fs.e2fs_log_bsize; 289 sblock.e2fs_bshift = LOG_MINBSIZE + sblock.e2fs.e2fs_log_bsize; 290 sblock.e2fs_qbmask = sblock.e2fs_bsize - 1; 291 sblock.e2fs_bmask = ~sblock.e2fs_qbmask; 292 sblock.e2fs_ngdb = howmany(sblock.e2fs_ncg, 293 sblock.e2fs_bsize / sizeof(struct ext2_gd)); 294 sblock.e2fs_ipb = sblock.e2fs_bsize / sizeof(struct ext2fs_dinode); 295 sblock.e2fs_itpg = sblock.e2fs.e2fs_ipg/sblock.e2fs_ipb; 296 297 /* 298 * Compute block size that the filesystem is based on, 299 * according to fsbtodb, and adjust superblock block number 300 * so we can tell if this is an alternate later. 301 */ 302 super *= dev_bsize; 303 dev_bsize = sblock.e2fs_bsize / fsbtodb(&sblock, 1); 304 sblk.b_bno = super / dev_bsize; 305 306 getblk(&asblk, 1 * sblock.e2fs.e2fs_bpg + sblock.e2fs.e2fs_first_dblock, 307 (long)SBSIZE); 308 if (asblk.b_errs) 309 return (0); 310 if (bflag) { 311 havesb = 1; 312 return (1); 313 } 314 315 /* 316 * Set all possible fields that could differ, then do check 317 * of whole super block against an alternate super block. 318 * When an alternate super-block is specified this check is skipped. 319 */ 320 asblk.b_un.b_fs->e2fs_rbcount = sblk.b_un.b_fs->e2fs_rbcount; 321 asblk.b_un.b_fs->e2fs_fbcount = sblk.b_un.b_fs->e2fs_fbcount; 322 asblk.b_un.b_fs->e2fs_ficount = sblk.b_un.b_fs->e2fs_ficount; 323 asblk.b_un.b_fs->e2fs_mtime = sblk.b_un.b_fs->e2fs_mtime; 324 asblk.b_un.b_fs->e2fs_wtime = sblk.b_un.b_fs->e2fs_wtime; 325 asblk.b_un.b_fs->e2fs_mnt_count = sblk.b_un.b_fs->e2fs_mnt_count; 326 asblk.b_un.b_fs->e2fs_max_mnt_count = sblk.b_un.b_fs->e2fs_max_mnt_count; 327 asblk.b_un.b_fs->e2fs_state = sblk.b_un.b_fs->e2fs_state; 328 asblk.b_un.b_fs->e2fs_beh = sblk.b_un.b_fs->e2fs_beh; 329 asblk.b_un.b_fs->e2fs_lastfsck = sblk.b_un.b_fs->e2fs_lastfsck; 330 asblk.b_un.b_fs->e2fs_fsckintv = sblk.b_un.b_fs->e2fs_fsckintv; 331 asblk.b_un.b_fs->e2fs_ruid = sblk.b_un.b_fs->e2fs_ruid; 332 asblk.b_un.b_fs->e2fs_rgid = sblk.b_un.b_fs->e2fs_rgid; 333 asblk.b_un.b_fs->e2fs_block_group_nr = 334 sblk.b_un.b_fs->e2fs_block_group_nr; 335 if (sblock.e2fs.e2fs_rev > E2FS_REV0 && 336 ((sblock.e2fs.e2fs_features_incompat & ~EXT2F_INCOMPAT_SUPP) || 337 (sblock.e2fs.e2fs_features_rocompat & ~EXT2F_ROCOMPAT_SUPP))) { 338 if (debug) { 339 printf("compat 0x%08x, incompat 0x%08x, compat_ro " 340 "0x%08x\n", 341 sblock.e2fs.e2fs_features_compat, 342 sblock.e2fs.e2fs_features_incompat, 343 sblock.e2fs.e2fs_features_rocompat); 344 } 345 badsb(listerr,"INCOMPATIBLE FEATURE BITS IN SUPER BLOCK"); 346 return 0; 347 } 348 if (memcmp(sblk.b_un.b_fs, asblk.b_un.b_fs, SBSIZE)) { 349 if (debug) { 350 u_int32_t *nlp, *olp, *endlp; 351 352 printf("superblock mismatches\n"); 353 nlp = (u_int32_t *)asblk.b_un.b_fs; 354 olp = (u_int32_t *)sblk.b_un.b_fs; 355 endlp = olp + (SBSIZE / sizeof *olp); 356 for ( ; olp < endlp; olp++, nlp++) { 357 if (*olp == *nlp) 358 continue; 359 printf("offset %ld, original %ld, alternate %ld\n", 360 (long)(olp - (u_int32_t *)sblk.b_un.b_fs), 361 (long)fs2h32(*olp), 362 (long)fs2h32(*nlp)); 363 } 364 } 365 badsb(listerr, 366 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE"); 367 return (0); 368 } 369 havesb = 1; 370 return (1); 371 } 372 373 void 374 copyback_sb(bp) 375 struct bufarea *bp; 376 { 377 /* Copy the in-memory superblock back to buffer */ 378 bp->b_un.b_fs->e2fs_icount = fs2h32(sblock.e2fs.e2fs_icount); 379 bp->b_un.b_fs->e2fs_bcount = fs2h32(sblock.e2fs.e2fs_bcount); 380 bp->b_un.b_fs->e2fs_rbcount = fs2h32(sblock.e2fs.e2fs_rbcount); 381 bp->b_un.b_fs->e2fs_fbcount = fs2h32(sblock.e2fs.e2fs_fbcount); 382 bp->b_un.b_fs->e2fs_ficount = fs2h32(sblock.e2fs.e2fs_ficount); 383 bp->b_un.b_fs->e2fs_first_dblock = 384 fs2h32(sblock.e2fs.e2fs_first_dblock); 385 bp->b_un.b_fs->e2fs_log_bsize = fs2h32(sblock.e2fs.e2fs_log_bsize); 386 bp->b_un.b_fs->e2fs_fsize = fs2h32(sblock.e2fs.e2fs_fsize); 387 bp->b_un.b_fs->e2fs_bpg = fs2h32(sblock.e2fs.e2fs_bpg); 388 bp->b_un.b_fs->e2fs_fpg = fs2h32(sblock.e2fs.e2fs_fpg); 389 bp->b_un.b_fs->e2fs_ipg = fs2h32(sblock.e2fs.e2fs_ipg); 390 bp->b_un.b_fs->e2fs_mtime = fs2h32(sblock.e2fs.e2fs_mtime); 391 bp->b_un.b_fs->e2fs_wtime = fs2h32(sblock.e2fs.e2fs_wtime); 392 bp->b_un.b_fs->e2fs_lastfsck = fs2h32(sblock.e2fs.e2fs_lastfsck); 393 bp->b_un.b_fs->e2fs_fsckintv = fs2h32(sblock.e2fs.e2fs_fsckintv); 394 bp->b_un.b_fs->e2fs_creator = fs2h32(sblock.e2fs.e2fs_creator); 395 bp->b_un.b_fs->e2fs_rev = fs2h32(sblock.e2fs.e2fs_rev); 396 bp->b_un.b_fs->e2fs_mnt_count = fs2h16(sblock.e2fs.e2fs_mnt_count); 397 bp->b_un.b_fs->e2fs_max_mnt_count = 398 fs2h16(sblock.e2fs.e2fs_max_mnt_count); 399 bp->b_un.b_fs->e2fs_magic = fs2h16(sblock.e2fs.e2fs_magic); 400 bp->b_un.b_fs->e2fs_state = fs2h16(sblock.e2fs.e2fs_state); 401 bp->b_un.b_fs->e2fs_beh = fs2h16(sblock.e2fs.e2fs_beh); 402 bp->b_un.b_fs->e2fs_ruid = fs2h16(sblock.e2fs.e2fs_ruid); 403 bp->b_un.b_fs->e2fs_rgid = fs2h16(sblock.e2fs.e2fs_rgid); 404 } 405 406 void 407 badsb(listerr, s) 408 int listerr; 409 char *s; 410 { 411 412 if (!listerr) 413 return; 414 if (preen) 415 printf("%s: ", cdevname()); 416 pfatal("BAD SUPER BLOCK: %s\n", s); 417 } 418 419 /* 420 * Calculate a prototype superblock based on information in the disk label. 421 * When done the cgsblock macro can be calculated and the fs_ncg field 422 * can be used. Do NOT attempt to use other macros without verifying that 423 * their needed information is available! 424 */ 425 426 int 427 calcsb(dev, devfd, fs) 428 char *dev; 429 int devfd; 430 struct m_ext2fs *fs; 431 { 432 struct disklabel *lp; 433 struct partition *pp; 434 char *cp; 435 436 cp = strchr(dev, '\0') - 1; 437 if ((cp == (char *)-1 || (*cp < 'a' || *cp > 'h')) && !isdigit(*cp)) { 438 pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev); 439 return (0); 440 } 441 lp = getdisklabel(dev, devfd); 442 if (isdigit(*cp)) 443 pp = &lp->d_partitions[0]; 444 else 445 pp = &lp->d_partitions[*cp - 'a']; 446 if (pp->p_fstype != FS_EXT2FS) { 447 pfatal("%s: NOT LABELED AS A EXT2 FILE SYSTEM (%s)\n", 448 dev, pp->p_fstype < FSMAXTYPES ? 449 fstypenames[pp->p_fstype] : "unknown"); 450 return (0); 451 } 452 memset(fs, 0, sizeof(struct m_ext2fs)); 453 fs->e2fs_bsize = pp->p_fsize; 454 fs->e2fs.e2fs_log_bsize = pp->p_fsize / 1024; 455 fs->e2fs.e2fs_bcount = (pp->p_size * DEV_BSIZE) / fs->e2fs_bsize; 456 fs->e2fs.e2fs_first_dblock = (fs->e2fs.e2fs_log_bsize == 0) ? 1 : 0; 457 fs->e2fs.e2fs_bpg = fs->e2fs_bsize * NBBY; 458 fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize; 459 fs->e2fs_qbmask = fs->e2fs_bsize - 1; 460 fs->e2fs_bmask = ~fs->e2fs_qbmask; 461 fs->e2fs_ncg = 462 howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock, 463 fs->e2fs.e2fs_bpg); 464 fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + 1; 465 fs->e2fs_ngdb = howmany(fs->e2fs_ncg, 466 fs->e2fs_bsize / sizeof(struct ext2_gd)); 467 468 return (1); 469 } 470 471 static struct disklabel * 472 getdisklabel(s, fd) 473 char *s; 474 int fd; 475 { 476 static struct disklabel lab; 477 478 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) { 479 if (s == NULL) 480 return ((struct disklabel *)NULL); 481 pwarn("ioctl (GCINFO): %s\n", strerror(errno)); 482 errexit("%s: can't read disk label\n", s); 483 } 484 return (&lab); 485 } 486 487 daddr_t 488 cgoverhead(c) 489 int c; 490 { 491 int overh; 492 overh = 1 /* block bitmap */ + 493 1 /* inode bitmap */ + 494 sblock.e2fs_itpg; 495 if (sblock.e2fs.e2fs_rev > E2FS_REV0 && 496 sblock.e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) { 497 if (cg_has_sb(c) == 0) 498 return overh; 499 } 500 overh += 1 + sblock.e2fs_ngdb; 501 return overh; 502 } 503