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(sblock.fs_ncg, sizeof(struct inostatlist)); 272 if (inostathead == NULL) { 273 printf("cannot alloc %u bytes for inostathead\n", 274 (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg))); 275 goto badsb; 276 } 277 numdirs = MAX(sblock.fs_cstotal.cs_ndir, 128); 278 dirhash = numdirs; 279 inplast = 0; 280 listmax = numdirs + 10; 281 inpsort = (struct inoinfo **)Calloc(listmax, sizeof(struct inoinfo *)); 282 inphead = (struct inoinfo **)Calloc(numdirs, sizeof(struct inoinfo *)); 283 if (inpsort == NULL || inphead == NULL) { 284 printf("cannot alloc %ju bytes for inphead\n", 285 (uintmax_t)numdirs * sizeof(struct inoinfo *)); 286 goto badsb; 287 } 288 bufinit(); 289 if (sblock.fs_flags & FS_DOSOFTDEP) 290 usedsoftdep = 1; 291 else 292 usedsoftdep = 0; 293 return (1); 294 295 badsb: 296 ckfini(0); 297 return (0); 298 } 299 300 /* 301 * Possible superblock locations ordered from most to least likely. 302 */ 303 static int sblock_try[] = SBLOCKSEARCH; 304 305 #define BAD_MAGIC_MSG \ 306 "The previous newfs operation on this volume did not complete.\n" \ 307 "You must complete newfs before mounting this volume.\n" 308 309 /* 310 * Read in the super block and its summary info. 311 */ 312 int 313 readsb(int listerr) 314 { 315 ufs2_daddr_t super; 316 int i; 317 318 if (bflag) { 319 super = bflag; 320 readcnt[sblk.b_type]++; 321 if ((blread(fsreadfd, (char *)&sblock, super, (long)SBLOCKSIZE))) 322 return (0); 323 if (sblock.fs_magic == FS_BAD_MAGIC) { 324 fprintf(stderr, BAD_MAGIC_MSG); 325 exit(11); 326 } 327 if (sblock.fs_magic != FS_UFS1_MAGIC && 328 sblock.fs_magic != FS_UFS2_MAGIC) { 329 fprintf(stderr, "%jd is not a file system superblock\n", 330 bflag); 331 return (0); 332 } 333 } else { 334 for (i = 0; sblock_try[i] != -1; i++) { 335 super = sblock_try[i] / dev_bsize; 336 readcnt[sblk.b_type]++; 337 if ((blread(fsreadfd, (char *)&sblock, super, 338 (long)SBLOCKSIZE))) 339 return (0); 340 if (sblock.fs_magic == FS_BAD_MAGIC) { 341 fprintf(stderr, BAD_MAGIC_MSG); 342 exit(11); 343 } 344 if ((sblock.fs_magic == FS_UFS1_MAGIC || 345 (sblock.fs_magic == FS_UFS2_MAGIC && 346 sblock.fs_sblockloc == sblock_try[i])) && 347 sblock.fs_ncg >= 1 && 348 sblock.fs_bsize >= MINBSIZE && 349 sblock.fs_sbsize >= roundup(sizeof(struct fs), dev_bsize)) 350 break; 351 } 352 if (sblock_try[i] == -1) { 353 fprintf(stderr, "Cannot find file system superblock\n"); 354 return (0); 355 } 356 } 357 /* 358 * Compute block size that the file system is based on, 359 * according to fsbtodb, and adjust superblock block number 360 * so we can tell if this is an alternate later. 361 */ 362 super *= dev_bsize; 363 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1); 364 sblk.b_bno = super / dev_bsize; 365 sblk.b_size = SBLOCKSIZE; 366 if (bflag) 367 goto out; 368 /* 369 * Compare all fields that should not differ in alternate super block. 370 * When an alternate super-block is specified this check is skipped. 371 */ 372 getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize); 373 if (asblk.b_errs) 374 return (0); 375 if (altsblock.fs_sblkno != sblock.fs_sblkno || 376 altsblock.fs_cblkno != sblock.fs_cblkno || 377 altsblock.fs_iblkno != sblock.fs_iblkno || 378 altsblock.fs_dblkno != sblock.fs_dblkno || 379 altsblock.fs_ncg != sblock.fs_ncg || 380 altsblock.fs_bsize != sblock.fs_bsize || 381 altsblock.fs_fsize != sblock.fs_fsize || 382 altsblock.fs_frag != sblock.fs_frag || 383 altsblock.fs_bmask != sblock.fs_bmask || 384 altsblock.fs_fmask != sblock.fs_fmask || 385 altsblock.fs_bshift != sblock.fs_bshift || 386 altsblock.fs_fshift != sblock.fs_fshift || 387 altsblock.fs_fragshift != sblock.fs_fragshift || 388 altsblock.fs_fsbtodb != sblock.fs_fsbtodb || 389 altsblock.fs_sbsize != sblock.fs_sbsize || 390 altsblock.fs_nindir != sblock.fs_nindir || 391 altsblock.fs_inopb != sblock.fs_inopb || 392 altsblock.fs_cssize != sblock.fs_cssize || 393 altsblock.fs_ipg != sblock.fs_ipg || 394 altsblock.fs_fpg != sblock.fs_fpg || 395 altsblock.fs_magic != sblock.fs_magic) { 396 badsb(listerr, 397 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE"); 398 return (0); 399 } 400 out: 401 /* 402 * If not yet done, update UFS1 superblock with new wider fields. 403 */ 404 if (sblock.fs_magic == FS_UFS1_MAGIC && 405 sblock.fs_maxbsize != sblock.fs_bsize) { 406 sblock.fs_maxbsize = sblock.fs_bsize; 407 sblock.fs_time = sblock.fs_old_time; 408 sblock.fs_size = sblock.fs_old_size; 409 sblock.fs_dsize = sblock.fs_old_dsize; 410 sblock.fs_csaddr = sblock.fs_old_csaddr; 411 sblock.fs_cstotal.cs_ndir = sblock.fs_old_cstotal.cs_ndir; 412 sblock.fs_cstotal.cs_nbfree = sblock.fs_old_cstotal.cs_nbfree; 413 sblock.fs_cstotal.cs_nifree = sblock.fs_old_cstotal.cs_nifree; 414 sblock.fs_cstotal.cs_nffree = sblock.fs_old_cstotal.cs_nffree; 415 } 416 havesb = 1; 417 return (1); 418 } 419 420 static void 421 badsb(int listerr, const char *s) 422 { 423 424 if (!listerr) 425 return; 426 if (preen) 427 printf("%s: ", cdevname); 428 pfatal("BAD SUPER BLOCK: %s\n", s); 429 } 430 431 void 432 sblock_init(void) 433 { 434 435 fswritefd = -1; 436 fsmodified = 0; 437 lfdir = 0; 438 initbarea(&sblk, BT_SUPERBLK); 439 initbarea(&asblk, BT_SUPERBLK); 440 sblk.b_un.b_buf = Malloc(SBLOCKSIZE); 441 asblk.b_un.b_buf = Malloc(SBLOCKSIZE); 442 if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL) 443 errx(EEXIT, "cannot allocate space for superblock"); 444 dev_bsize = secsize = DEV_BSIZE; 445 } 446