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. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)setup.c 8.10 (Berkeley) 5/9/95 30 * $FreeBSD: src/sbin/fsck/setup.c,v 1.17.2.4 2002/06/24 05:10:41 dillon Exp $ 31 */ 32 33 #define DKTYPENAMES 34 #include <sys/param.h> 35 #include <sys/stat.h> 36 #include <sys/diskslice.h> 37 38 #include <vfs/ufs/dinode.h> 39 #include <vfs/ufs/fs.h> 40 41 #include <ctype.h> 42 #include <err.h> 43 #include <errno.h> 44 #include <fcntl.h> 45 #include <string.h> 46 47 #include "fsck.h" 48 49 struct bufarea asblk; 50 #define altsblock (*asblk.b_un.b_fs) 51 52 static void badsb(int listerr, char *s); 53 static int readsb(int listerr); 54 55 /* 56 * Read in a superblock finding an alternate if necessary. 57 * Return 1 if successful, 0 if unsuccessful, -1 if filesystem 58 * is already clean (preen mode only). 59 */ 60 int 61 setup(char *dev) 62 { 63 long size, asked, i, j; 64 long skipclean, bmapsize; 65 off_t sizepb; 66 struct stat statb; 67 68 havesb = 0; 69 fswritefd = -1; 70 skipclean = fflag ? 0 : preen; 71 if (stat(dev, &statb) < 0) { 72 printf("Can't stat %s: %s\n", dev, strerror(errno)); 73 return (0); 74 } 75 if ((statb.st_mode & S_IFMT) != S_IFCHR && 76 (statb.st_mode & S_IFMT) != S_IFBLK) { 77 pfatal("%s is not a disk device", dev); 78 if (reply("CONTINUE") == 0) 79 return (0); 80 } 81 if ((fsreadfd = open(dev, O_RDONLY)) < 0) { 82 printf("Can't open %s: %s\n", dev, strerror(errno)); 83 return (0); 84 } 85 if (preen == 0) 86 printf("** %s", dev); 87 if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) { 88 fswritefd = -1; 89 if (preen) 90 pfatal("NO WRITE ACCESS"); 91 printf(" (NO WRITE)"); 92 } 93 if (preen == 0) 94 printf("\n"); 95 fsmodified = 0; 96 lfdir = 0; 97 initbarea(&sblk); 98 initbarea(&asblk); 99 sblk.b_un.b_buf = malloc(SBSIZE); 100 asblk.b_un.b_buf = malloc(SBSIZE); 101 if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL) 102 errx(EEXIT, "cannot allocate space for superblock"); 103 104 /* 105 * Figure out the device block size and the sector size. The 106 * block size is updated by readsb() later on. 107 */ 108 { 109 struct partinfo pinfo; 110 111 if (ioctl(fsreadfd, DIOCGPART, &pinfo) == 0) { 112 dev_bsize = secsize = pinfo.media_blksize; 113 } else { 114 dev_bsize = secsize = DEV_BSIZE; 115 } 116 } 117 118 /* 119 * Read in the superblock, looking for alternates if necessary 120 */ 121 if (readsb(1) == 0) { 122 skipclean = 0; 123 if (bflag || preen) 124 return(0); 125 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0) 126 return (0); 127 bflag = 32; 128 if (readsb(0) == 0) { 129 printf( 130 "YOU MUST USE THE -b OPTION TO FSCK TO SPECIFY\n" 131 "THE LOCATION OF AN ALTERNATE SUPER-BLOCK TO\n" 132 "SUPPLY NEEDED INFORMATION; SEE fsck(8)."); 133 bflag = 0; 134 return(0); 135 } 136 pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag); 137 bflag = 0; 138 } 139 if (skipclean && sblock.fs_clean) { 140 pwarn("FILESYSTEM CLEAN; SKIPPING CHECKS\n"); 141 return (-1); 142 } 143 maxfsblock = sblock.fs_size; 144 maxino = sblock.fs_ncg * sblock.fs_ipg; 145 /* 146 * Check and potentially fix certain fields in the super block. 147 */ 148 if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) { 149 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK"); 150 if (reply("SET TO DEFAULT") == 1) { 151 sblock.fs_optim = FS_OPTTIME; 152 sbdirty(); 153 } 154 } 155 if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) { 156 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK", 157 sblock.fs_minfree); 158 if (reply("SET TO DEFAULT") == 1) { 159 sblock.fs_minfree = 10; 160 sbdirty(); 161 } 162 } 163 if (sblock.fs_interleave < 1 || 164 sblock.fs_interleave > sblock.fs_nsect) { 165 pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK", 166 sblock.fs_interleave); 167 sblock.fs_interleave = 1; 168 if (preen) 169 printf(" (FIXED)\n"); 170 if (preen || reply("SET TO DEFAULT") == 1) { 171 sbdirty(); 172 dirty(&asblk); 173 } 174 } 175 if (sblock.fs_npsect < sblock.fs_nsect || 176 sblock.fs_npsect > sblock.fs_nsect*2) { 177 pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK", 178 sblock.fs_npsect); 179 sblock.fs_npsect = sblock.fs_nsect; 180 if (preen) 181 printf(" (FIXED)\n"); 182 if (preen || reply("SET TO DEFAULT") == 1) { 183 sbdirty(); 184 dirty(&asblk); 185 } 186 } 187 if (sblock.fs_inodefmt >= FS_44INODEFMT) { 188 newinofmt = 1; 189 } else { 190 sblock.fs_qbmask = ~sblock.fs_bmask; 191 sblock.fs_qfmask = ~sblock.fs_fmask; 192 /* This should match the kernel limit in ffs_oldfscompat(). */ 193 sblock.fs_maxfilesize = (u_int64_t)1 << 39; 194 newinofmt = 0; 195 } 196 /* 197 * Convert to new inode format. 198 */ 199 if (cvtlevel >= 2 && sblock.fs_inodefmt < FS_44INODEFMT) { 200 if (preen) 201 pwarn("CONVERTING TO NEW INODE FORMAT\n"); 202 else if (!reply("CONVERT TO NEW INODE FORMAT")) 203 return(0); 204 doinglevel2++; 205 sblock.fs_inodefmt = FS_44INODEFMT; 206 sizepb = sblock.fs_bsize; 207 sblock.fs_maxfilesize = sblock.fs_bsize * UFS_NDADDR - 1; 208 for (i = 0; i < UFS_NIADDR; i++) { 209 sizepb *= NINDIR(&sblock); 210 sblock.fs_maxfilesize += sizepb; 211 } 212 sblock.fs_maxsymlinklen = UFS1_MAXSYMLINKLEN; 213 sblock.fs_qbmask = ~sblock.fs_bmask; 214 sblock.fs_qfmask = ~sblock.fs_fmask; 215 sbdirty(); 216 dirty(&asblk); 217 } 218 /* 219 * Convert to new cylinder group format. 220 */ 221 if (cvtlevel >= 1 && sblock.fs_postblformat == FS_42POSTBLFMT) { 222 if (preen) 223 pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n"); 224 else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT")) 225 return(0); 226 doinglevel1++; 227 sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT; 228 sblock.fs_nrpos = 8; 229 sblock.fs_postbloff = 230 (char *)(&sblock.fs_opostbl[0][0]) - 231 (char *)(&sblock.fs_firstfield); 232 sblock.fs_rotbloff = &sblock.fs_space[0] - 233 (u_char *)(&sblock.fs_firstfield); 234 sblock.fs_cgsize = 235 fragroundup(&sblock, CGSIZE(&sblock)); 236 sbdirty(); 237 dirty(&asblk); 238 } 239 if (asblk.b_dirty && !bflag) { 240 memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize); 241 flush(fswritefd, &asblk); 242 } 243 /* 244 * read in the summary info. 245 */ 246 asked = 0; 247 sblock.fs_csp = calloc(1, sblock.fs_cssize); 248 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) { 249 size = sblock.fs_cssize - i < sblock.fs_bsize ? 250 sblock.fs_cssize - i : sblock.fs_bsize; 251 if (bread(fsreadfd, (char *)sblock.fs_csp + i, 252 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag), 253 size) != 0 && !asked) { 254 pfatal("BAD SUMMARY INFORMATION"); 255 if (reply("CONTINUE") == 0) { 256 ckfini(0); 257 exit(EEXIT); 258 } 259 asked++; 260 } 261 } 262 /* 263 * allocate and initialize the necessary maps 264 */ 265 bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(short)); 266 blockmap = calloc((unsigned)bmapsize, sizeof (char)); 267 if (blockmap == NULL) { 268 printf("cannot alloc %u bytes for blockmap\n", 269 (unsigned)bmapsize); 270 goto badsb; 271 } 272 inostathead = calloc((unsigned)(sblock.fs_ncg), 273 sizeof(struct inostatlist)); 274 if (inostathead == NULL) { 275 printf("cannot alloc %u bytes for inostathead\n", 276 (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg))); 277 goto badsb; 278 } 279 numdirs = sblock.fs_cstotal.cs_ndir; 280 281 /* 282 * Calculate the directory hash table size. Do not allocate 283 * a ridiculous amount of memory if we have a lot of directories. 284 */ 285 for (dirhash = 16; dirhash < numdirs; dirhash <<= 1) 286 ; 287 if (dirhash > 1024*1024) 288 dirhash /= 8; 289 dirhashmask = dirhash - 1; 290 291 if (numdirs == 0) { 292 printf("numdirs is zero, try using an alternate superblock\n"); 293 goto badsb; 294 } 295 inplast = 0; 296 listmax = numdirs + 10; 297 inpsort = calloc((unsigned)listmax, sizeof(struct inoinfo *)); 298 inphead = calloc((unsigned)dirhash, sizeof(struct inoinfo *)); 299 if (inpsort == NULL || inphead == NULL) { 300 printf("cannot allocate base structures for %ld directories\n", 301 numdirs); 302 goto badsb; 303 } 304 bufinit(); 305 if (sblock.fs_flags & FS_DOSOFTDEP) 306 usedsoftdep = 1; 307 else 308 usedsoftdep = 0; 309 return (1); 310 311 badsb: 312 ckfini(0); 313 return (0); 314 } 315 316 /* 317 * Read in the super block and its summary info. 318 */ 319 static int 320 readsb(int listerr) 321 { 322 ufs_daddr_t super = bflag ? bflag : SBOFF / dev_bsize; 323 324 if (bread(fsreadfd, (char *)&sblock, super, (long)SBSIZE) != 0) 325 return (0); 326 sblk.b_bno = super; 327 sblk.b_size = SBSIZE; 328 /* 329 * run a few consistency checks of the super block 330 */ 331 if (sblock.fs_magic != FS_MAGIC) 332 { badsb(listerr, "MAGIC NUMBER WRONG"); return (0); } 333 if (sblock.fs_ncg < 1) 334 { badsb(listerr, "NCG OUT OF RANGE"); return (0); } 335 if (sblock.fs_cpg < 1) 336 { badsb(listerr, "CPG OUT OF RANGE"); return (0); } 337 if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl || 338 (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl) 339 { badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); } 340 if (sblock.fs_sbsize > SBSIZE) 341 { badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); } 342 /* 343 * Compute block size that the filesystem is based on, 344 * according to fsbtodb, and adjust superblock block number 345 * so we can tell if this is an alternate later. 346 */ 347 super *= dev_bsize; 348 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1); 349 sblk.b_bno = super / dev_bsize; 350 if (bflag) { 351 havesb = 1; 352 return (1); 353 } 354 /* 355 * Compare all fields that should not differ in alternate super block. 356 * When an alternate super-block is specified this check is skipped. 357 */ 358 getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize); 359 if (asblk.b_errs) 360 return (0); 361 if (altsblock.fs_sblkno != sblock.fs_sblkno || 362 altsblock.fs_cblkno != sblock.fs_cblkno || 363 altsblock.fs_iblkno != sblock.fs_iblkno || 364 altsblock.fs_dblkno != sblock.fs_dblkno || 365 altsblock.fs_cgoffset != sblock.fs_cgoffset || 366 altsblock.fs_cgmask != sblock.fs_cgmask || 367 altsblock.fs_ncg != sblock.fs_ncg || 368 altsblock.fs_bsize != sblock.fs_bsize || 369 altsblock.fs_fsize != sblock.fs_fsize || 370 altsblock.fs_frag != sblock.fs_frag || 371 altsblock.fs_bmask != sblock.fs_bmask || 372 altsblock.fs_fmask != sblock.fs_fmask || 373 altsblock.fs_bshift != sblock.fs_bshift || 374 altsblock.fs_fshift != sblock.fs_fshift || 375 altsblock.fs_fragshift != sblock.fs_fragshift || 376 altsblock.fs_fsbtodb != sblock.fs_fsbtodb || 377 altsblock.fs_sbsize != sblock.fs_sbsize || 378 altsblock.fs_nindir != sblock.fs_nindir || 379 altsblock.fs_inopb != sblock.fs_inopb || 380 altsblock.fs_cssize != sblock.fs_cssize || 381 altsblock.fs_cpg != sblock.fs_cpg || 382 altsblock.fs_ipg != sblock.fs_ipg || 383 altsblock.fs_fpg != sblock.fs_fpg || 384 altsblock.fs_magic != sblock.fs_magic) { 385 badsb(listerr, 386 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE"); 387 return (0); 388 } 389 havesb = 1; 390 return (1); 391 } 392 393 static void 394 badsb(int listerr, char *s) 395 { 396 397 if (!listerr) 398 return; 399 if (preen) 400 printf("%s: ", cdevname); 401 pfatal("BAD SUPER BLOCK: %s\n", s); 402 } 403 404