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 30 #if 0 31 #ifndef lint 32 static const char sccsid[] = "@(#)setup.c 8.10 (Berkeley) 5/9/95"; 33 #endif /* not lint */ 34 #endif 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include <sys/param.h> 39 #include <sys/stat.h> 40 #define FSTYPENAMES 41 #include <sys/disklabel.h> 42 #include <sys/file.h> 43 #include <sys/sysctl.h> 44 45 #include <ufs/ufs/dinode.h> 46 #include <ufs/ffs/fs.h> 47 48 #include <ctype.h> 49 #include <err.h> 50 #include <errno.h> 51 #include <limits.h> 52 #include <stdint.h> 53 #include <string.h> 54 55 #include "fsck.h" 56 57 struct bufarea asblk; 58 #define altsblock (*asblk.b_un.b_fs) 59 #define POWEROF2(num) (((num) & ((num) - 1)) == 0) 60 61 static void badsb(int listerr, const char *s); 62 63 /* 64 * Read in a superblock finding an alternate if necessary. 65 * Return 1 if successful, 0 if unsuccessful, -1 if file system 66 * is already clean (ckclean and preen mode only). 67 */ 68 int 69 setup(char *dev) 70 { 71 long cg, asked, i, j; 72 long bmapsize; 73 struct stat statb; 74 struct fs proto; 75 size_t size; 76 77 havesb = 0; 78 fswritefd = -1; 79 cursnapshot = 0; 80 if (stat(dev, &statb) < 0) { 81 printf("Can't stat %s: %s\n", dev, strerror(errno)); 82 if (bkgrdflag) { 83 unlink(snapname); 84 bkgrdflag = 0; 85 } 86 return (0); 87 } 88 if ((statb.st_mode & S_IFMT) != S_IFCHR && 89 (statb.st_mode & S_IFMT) != S_IFBLK) { 90 if (bkgrdflag != 0 && (statb.st_flags & SF_SNAPSHOT) == 0) { 91 unlink(snapname); 92 printf("background fsck lacks a snapshot\n"); 93 exit(EEXIT); 94 } 95 if ((statb.st_flags & SF_SNAPSHOT) != 0 && cvtlevel == 0) { 96 cursnapshot = statb.st_ino; 97 } else { 98 if (cvtlevel == 0 || 99 (statb.st_flags & SF_SNAPSHOT) == 0) { 100 if (preen && bkgrdflag) { 101 unlink(snapname); 102 bkgrdflag = 0; 103 } 104 pfatal("%s is not a disk device", dev); 105 if (reply("CONTINUE") == 0) { 106 if (bkgrdflag) { 107 unlink(snapname); 108 bkgrdflag = 0; 109 } 110 return (0); 111 } 112 } else { 113 if (bkgrdflag) { 114 unlink(snapname); 115 bkgrdflag = 0; 116 } 117 pfatal("cannot convert a snapshot"); 118 exit(EEXIT); 119 } 120 } 121 } 122 if ((fsreadfd = open(dev, O_RDONLY)) < 0) { 123 if (bkgrdflag) { 124 unlink(snapname); 125 bkgrdflag = 0; 126 } 127 printf("Can't open %s: %s\n", dev, strerror(errno)); 128 return (0); 129 } 130 if (bkgrdflag) { 131 unlink(snapname); 132 size = MIBSIZE; 133 if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0|| 134 sysctlnametomib("vfs.ffs.adjblkcnt", adjblkcnt, &size) < 0|| 135 sysctlnametomib("vfs.ffs.freefiles", freefiles, &size) < 0|| 136 sysctlnametomib("vfs.ffs.freedirs", freedirs, &size) < 0 || 137 sysctlnametomib("vfs.ffs.freeblks", freeblks, &size) < 0) { 138 pfatal("kernel lacks background fsck support\n"); 139 exit(EEXIT); 140 } 141 /* 142 * When kernel is lack of runtime bgfsck superblock summary 143 * adjustment functionality, it does not mean we can not 144 * continue, as old kernels will recompute the summary at 145 * mount time. However, it will be an unexpected softupdates 146 * inconsistency if it turns out that the summary is still 147 * incorrect. Set a flag so subsequent operation can know 148 * this. 149 */ 150 bkgrdsumadj = 1; 151 if (sysctlnametomib("vfs.ffs.adjndir", adjndir, &size) < 0 || 152 sysctlnametomib("vfs.ffs.adjnbfree", adjnbfree, &size) < 0 || 153 sysctlnametomib("vfs.ffs.adjnifree", adjnifree, &size) < 0 || 154 sysctlnametomib("vfs.ffs.adjnffree", adjnffree, &size) < 0 || 155 sysctlnametomib("vfs.ffs.adjnumclusters", adjnumclusters, &size) < 0) { 156 bkgrdsumadj = 0; 157 pwarn("kernel lacks runtime superblock summary adjustment support"); 158 } 159 cmd.version = FFS_CMD_VERSION; 160 cmd.handle = fsreadfd; 161 fswritefd = -1; 162 } 163 if (preen == 0) 164 printf("** %s", dev); 165 if (bkgrdflag == 0 && 166 (nflag || (fswritefd = open(dev, O_WRONLY)) < 0)) { 167 fswritefd = -1; 168 if (preen) 169 pfatal("NO WRITE ACCESS"); 170 printf(" (NO WRITE)"); 171 } 172 if (preen == 0) 173 printf("\n"); 174 /* 175 * Read in the superblock, looking for alternates if necessary 176 */ 177 if (readsb(1) == 0) { 178 skipclean = 0; 179 if (bflag || preen) 180 return(0); 181 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0) 182 return (0); 183 for (cg = 0; cg < proto.fs_ncg; cg++) { 184 bflag = fsbtodb(&proto, cgsblock(&proto, cg)); 185 if (readsb(0) != 0) 186 break; 187 } 188 if (cg >= proto.fs_ncg) { 189 printf("%s %s\n%s %s\n%s %s\n", 190 "SEARCH FOR ALTERNATE SUPER-BLOCK", 191 "FAILED. YOU MUST USE THE", 192 "-b OPTION TO FSCK TO SPECIFY THE", 193 "LOCATION OF AN ALTERNATE", 194 "SUPER-BLOCK TO SUPPLY NEEDED", 195 "INFORMATION; SEE fsck_ffs(8)."); 196 bflag = 0; 197 return(0); 198 } 199 pwarn("USING ALTERNATE SUPERBLOCK AT %jd\n", bflag); 200 bflag = 0; 201 } 202 if (skipclean && ckclean && sblock.fs_clean) { 203 pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n"); 204 return (-1); 205 } 206 maxfsblock = sblock.fs_size; 207 maxino = sblock.fs_ncg * sblock.fs_ipg; 208 /* 209 * Check and potentially fix certain fields in the super block. 210 */ 211 if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) { 212 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK"); 213 if (reply("SET TO DEFAULT") == 1) { 214 sblock.fs_optim = FS_OPTTIME; 215 sbdirty(); 216 } 217 } 218 if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) { 219 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK", 220 sblock.fs_minfree); 221 if (reply("SET TO DEFAULT") == 1) { 222 sblock.fs_minfree = 10; 223 sbdirty(); 224 } 225 } 226 if (sblock.fs_magic == FS_UFS1_MAGIC && 227 sblock.fs_old_inodefmt < FS_44INODEFMT) { 228 pwarn("Format of file system is too old.\n"); 229 pwarn("Must update to modern format using a version of fsck\n"); 230 pfatal("from before 2002 with the command ``fsck -c 2''\n"); 231 exit(EEXIT); 232 } 233 if (asblk.b_dirty && !bflag) { 234 memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize); 235 flush(fswritefd, &asblk); 236 } 237 /* 238 * read in the summary info. 239 */ 240 asked = 0; 241 sblock.fs_csp = Calloc(1, sblock.fs_cssize); 242 if (sblock.fs_csp == NULL) { 243 printf("cannot alloc %u bytes for cg summary info\n", 244 (unsigned)sblock.fs_cssize); 245 goto badsb; 246 } 247 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) { 248 size = MIN(sblock.fs_cssize - i, sblock.fs_bsize); 249 readcnt[sblk.b_type]++; 250 if (blread(fsreadfd, (char *)sblock.fs_csp + i, 251 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag), 252 size) != 0 && !asked) { 253 pfatal("BAD SUMMARY INFORMATION"); 254 if (reply("CONTINUE") == 0) { 255 ckfini(0); 256 exit(EEXIT); 257 } 258 asked++; 259 } 260 } 261 /* 262 * allocate and initialize the necessary maps 263 */ 264 bmapsize = roundup(howmany(maxfsblock, CHAR_BIT), sizeof(short)); 265 blockmap = Calloc((unsigned)bmapsize, sizeof (char)); 266 if (blockmap == NULL) { 267 printf("cannot alloc %u bytes for blockmap\n", 268 (unsigned)bmapsize); 269 goto badsb; 270 } 271 inostathead = Calloc((unsigned)(sblock.fs_ncg), 272 sizeof(struct inostatlist)); 273 if (inostathead == NULL) { 274 printf("cannot alloc %u bytes for inostathead\n", 275 (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg))); 276 goto badsb; 277 } 278 numdirs = MAX(sblock.fs_cstotal.cs_ndir, 128); 279 dirhash = numdirs; 280 inplast = 0; 281 listmax = numdirs + 10; 282 inpsort = (struct inoinfo **)Calloc((unsigned)listmax, 283 sizeof(struct inoinfo *)); 284 inphead = (struct inoinfo **)Calloc((unsigned)numdirs, 285 sizeof(struct inoinfo *)); 286 if (inpsort == NULL || inphead == NULL) { 287 printf("cannot alloc %ju bytes for inphead\n", 288 (uintmax_t)numdirs * sizeof(struct inoinfo *)); 289 goto badsb; 290 } 291 bufinit(); 292 if (sblock.fs_flags & FS_DOSOFTDEP) 293 usedsoftdep = 1; 294 else 295 usedsoftdep = 0; 296 return (1); 297 298 badsb: 299 ckfini(0); 300 return (0); 301 } 302 303 /* 304 * Possible superblock locations ordered from most to least likely. 305 */ 306 static int sblock_try[] = SBLOCKSEARCH; 307 308 #define BAD_MAGIC_MSG \ 309 "The previous newfs operation on this volume did not complete.\n" \ 310 "You must complete newfs before mounting this volume.\n" 311 312 /* 313 * Read in the super block and its summary info. 314 */ 315 int 316 readsb(int listerr) 317 { 318 ufs2_daddr_t super; 319 int i; 320 321 if (bflag) { 322 super = bflag; 323 readcnt[sblk.b_type]++; 324 if ((blread(fsreadfd, (char *)&sblock, super, (long)SBLOCKSIZE))) 325 return (0); 326 if (sblock.fs_magic == FS_BAD_MAGIC) { 327 fprintf(stderr, BAD_MAGIC_MSG); 328 exit(11); 329 } 330 if (sblock.fs_magic != FS_UFS1_MAGIC && 331 sblock.fs_magic != FS_UFS2_MAGIC) { 332 fprintf(stderr, "%jd is not a file system superblock\n", 333 bflag); 334 return (0); 335 } 336 } else { 337 for (i = 0; sblock_try[i] != -1; i++) { 338 super = sblock_try[i] / dev_bsize; 339 readcnt[sblk.b_type]++; 340 if ((blread(fsreadfd, (char *)&sblock, super, 341 (long)SBLOCKSIZE))) 342 return (0); 343 if (sblock.fs_magic == FS_BAD_MAGIC) { 344 fprintf(stderr, BAD_MAGIC_MSG); 345 exit(11); 346 } 347 if ((sblock.fs_magic == FS_UFS1_MAGIC || 348 (sblock.fs_magic == FS_UFS2_MAGIC && 349 sblock.fs_sblockloc == sblock_try[i])) && 350 sblock.fs_ncg >= 1 && 351 sblock.fs_bsize >= MINBSIZE && 352 sblock.fs_sbsize >= roundup(sizeof(struct fs), dev_bsize)) 353 break; 354 } 355 if (sblock_try[i] == -1) { 356 fprintf(stderr, "Cannot find file system superblock\n"); 357 return (0); 358 } 359 } 360 /* 361 * Compute block size that the file system is based on, 362 * according to fsbtodb, and adjust superblock block number 363 * so we can tell if this is an alternate later. 364 */ 365 super *= dev_bsize; 366 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1); 367 sblk.b_bno = super / dev_bsize; 368 sblk.b_size = SBLOCKSIZE; 369 if (bflag) 370 goto out; 371 /* 372 * Compare all fields that should not differ in alternate super block. 373 * When an alternate super-block is specified this check is skipped. 374 */ 375 getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize); 376 if (asblk.b_errs) 377 return (0); 378 if (altsblock.fs_sblkno != sblock.fs_sblkno || 379 altsblock.fs_cblkno != sblock.fs_cblkno || 380 altsblock.fs_iblkno != sblock.fs_iblkno || 381 altsblock.fs_dblkno != sblock.fs_dblkno || 382 altsblock.fs_ncg != sblock.fs_ncg || 383 altsblock.fs_bsize != sblock.fs_bsize || 384 altsblock.fs_fsize != sblock.fs_fsize || 385 altsblock.fs_frag != sblock.fs_frag || 386 altsblock.fs_bmask != sblock.fs_bmask || 387 altsblock.fs_fmask != sblock.fs_fmask || 388 altsblock.fs_bshift != sblock.fs_bshift || 389 altsblock.fs_fshift != sblock.fs_fshift || 390 altsblock.fs_fragshift != sblock.fs_fragshift || 391 altsblock.fs_fsbtodb != sblock.fs_fsbtodb || 392 altsblock.fs_sbsize != sblock.fs_sbsize || 393 altsblock.fs_nindir != sblock.fs_nindir || 394 altsblock.fs_inopb != sblock.fs_inopb || 395 altsblock.fs_cssize != sblock.fs_cssize || 396 altsblock.fs_ipg != sblock.fs_ipg || 397 altsblock.fs_fpg != sblock.fs_fpg || 398 altsblock.fs_magic != sblock.fs_magic) { 399 badsb(listerr, 400 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE"); 401 return (0); 402 } 403 out: 404 /* 405 * If not yet done, update UFS1 superblock with new wider fields. 406 */ 407 if (sblock.fs_magic == FS_UFS1_MAGIC && 408 sblock.fs_maxbsize != sblock.fs_bsize) { 409 sblock.fs_maxbsize = sblock.fs_bsize; 410 sblock.fs_time = sblock.fs_old_time; 411 sblock.fs_size = sblock.fs_old_size; 412 sblock.fs_dsize = sblock.fs_old_dsize; 413 sblock.fs_csaddr = sblock.fs_old_csaddr; 414 sblock.fs_cstotal.cs_ndir = sblock.fs_old_cstotal.cs_ndir; 415 sblock.fs_cstotal.cs_nbfree = sblock.fs_old_cstotal.cs_nbfree; 416 sblock.fs_cstotal.cs_nifree = sblock.fs_old_cstotal.cs_nifree; 417 sblock.fs_cstotal.cs_nffree = sblock.fs_old_cstotal.cs_nffree; 418 } 419 havesb = 1; 420 return (1); 421 } 422 423 static void 424 badsb(int listerr, const char *s) 425 { 426 427 if (!listerr) 428 return; 429 if (preen) 430 printf("%s: ", cdevname); 431 pfatal("BAD SUPER BLOCK: %s\n", s); 432 } 433 434 void 435 sblock_init(void) 436 { 437 438 fswritefd = -1; 439 fsmodified = 0; 440 lfdir = 0; 441 initbarea(&sblk, BT_SUPERBLK); 442 initbarea(&asblk, BT_SUPERBLK); 443 sblk.b_un.b_buf = Malloc(SBLOCKSIZE); 444 asblk.b_un.b_buf = Malloc(SBLOCKSIZE); 445 if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL) 446 errx(EEXIT, "cannot allocate space for superblock"); 447 dev_bsize = secsize = DEV_BSIZE; 448 } 449