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