1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1980, 1986, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #if 0 33 #ifndef lint 34 static const char sccsid[] = "@(#)setup.c 8.10 (Berkeley) 5/9/95"; 35 #endif /* not lint */ 36 #endif 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #include <sys/param.h> 41 #include <sys/disk.h> 42 #include <sys/stat.h> 43 #define FSTYPENAMES 44 #include <sys/disklabel.h> 45 #include <sys/file.h> 46 #include <sys/sysctl.h> 47 48 #include <ufs/ufs/dinode.h> 49 #include <ufs/ffs/fs.h> 50 51 #include <ctype.h> 52 #include <err.h> 53 #include <errno.h> 54 #include <limits.h> 55 #include <stdint.h> 56 #include <string.h> 57 #include <libufs.h> 58 59 #include "fsck.h" 60 61 struct inoinfo **inphead, **inpsort; /* info about all inodes */ 62 63 static int sbhashfailed; 64 #define POWEROF2(num) (((num) & ((num) - 1)) == 0) 65 66 static int calcsb(char *dev, int devfd, struct fs *fs); 67 static void saverecovery(int readfd, int writefd); 68 static int chkrecovery(int devfd); 69 70 /* 71 * Read in a superblock finding an alternate if necessary. 72 * Return 1 if successful, 0 if unsuccessful, -1 if file system 73 * is already clean (ckclean and preen mode only). 74 */ 75 int 76 setup(char *dev) 77 { 78 long bmapsize; 79 80 /* 81 * We are expected to have an open file descriptor and a superblock. 82 */ 83 if (fsreadfd < 0 || havesb == 0) { 84 if (debug) 85 printf("setup: bad fsreadfd or missing superblock\n"); 86 return (0); 87 } 88 if (preen == 0) 89 printf("** %s", dev); 90 if (bkgrdflag == 0 && 91 (nflag || (fswritefd = open(dev, O_WRONLY)) < 0)) { 92 fswritefd = -1; 93 if (preen) 94 pfatal("NO WRITE ACCESS"); 95 printf(" (NO WRITE)"); 96 } 97 if (preen == 0) 98 printf("\n"); 99 if (sbhashfailed != 0) { 100 pwarn("SUPERBLOCK CHECK HASH FAILED"); 101 if (fswritefd == -1) 102 pwarn("OPENED READONLY SO CANNOT CORRECT CHECK HASH\n"); 103 else if (preen || reply("CORRECT CHECK HASH") != 0) { 104 if (preen) 105 printf(" (CORRECTED)\n"); 106 sblock.fs_clean = 0; 107 sbdirty(); 108 } 109 } 110 if (skipclean && ckclean && sblock.fs_clean) { 111 pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n"); 112 return (-1); 113 } 114 maxfsblock = sblock.fs_size; 115 maxino = sblock.fs_ncg * sblock.fs_ipg; 116 /* 117 * Check and potentially fix certain fields in the super block. 118 */ 119 if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) { 120 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK"); 121 if (reply("SET TO DEFAULT") == 1) { 122 sblock.fs_optim = FS_OPTTIME; 123 sbdirty(); 124 } 125 } 126 if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) { 127 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK", 128 sblock.fs_minfree); 129 if (reply("SET TO DEFAULT") == 1) { 130 sblock.fs_minfree = 10; 131 sbdirty(); 132 } 133 } 134 if (sblock.fs_magic == FS_UFS1_MAGIC && 135 sblock.fs_old_inodefmt < FS_44INODEFMT) { 136 pwarn("Format of file system is too old.\n"); 137 pwarn("Must update to modern format using a version of fsck\n"); 138 pfatal("from before 2002 with the command ``fsck -c 2''\n"); 139 exit(EEXIT); 140 } 141 if (preen == 0 && yflag == 0 && sblock.fs_magic == FS_UFS2_MAGIC && 142 fswritefd != -1 && chkrecovery(fsreadfd) == 0 && 143 reply("SAVE DATA TO FIND ALTERNATE SUPERBLOCKS") != 0) 144 saverecovery(fsreadfd, fswritefd); 145 /* 146 * allocate and initialize the necessary maps 147 */ 148 bufinit(); 149 bmapsize = roundup(howmany(maxfsblock, CHAR_BIT), sizeof(short)); 150 blockmap = Calloc((unsigned)bmapsize, sizeof (char)); 151 if (blockmap == NULL) { 152 printf("cannot alloc %u bytes for blockmap\n", 153 (unsigned)bmapsize); 154 goto badsb; 155 } 156 inostathead = Calloc(sblock.fs_ncg, sizeof(struct inostatlist)); 157 if (inostathead == NULL) { 158 printf("cannot alloc %u bytes for inostathead\n", 159 (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg))); 160 goto badsb; 161 } 162 numdirs = MAX(sblock.fs_cstotal.cs_ndir, 128); 163 dirhash = numdirs; 164 inplast = 0; 165 listmax = numdirs + 10; 166 inpsort = (struct inoinfo **)Calloc(listmax, sizeof(struct inoinfo *)); 167 inphead = (struct inoinfo **)Calloc(numdirs, sizeof(struct inoinfo *)); 168 if (inpsort == NULL || inphead == NULL) { 169 printf("cannot alloc %ju bytes for inphead\n", 170 (uintmax_t)numdirs * sizeof(struct inoinfo *)); 171 goto badsb; 172 } 173 if (sblock.fs_flags & FS_DOSOFTDEP) 174 usedsoftdep = 1; 175 else 176 usedsoftdep = 0; 177 return (1); 178 179 badsb: 180 ckfini(0); 181 return (0); 182 } 183 184 /* 185 * Open a device or file to be checked by fsck. 186 */ 187 int 188 openfilesys(char *dev) 189 { 190 struct stat statb; 191 int saved_fsreadfd; 192 193 if (stat(dev, &statb) < 0) 194 return (0); 195 if ((statb.st_mode & S_IFMT) != S_IFCHR && 196 (statb.st_mode & S_IFMT) != S_IFBLK) { 197 if (bkgrdflag != 0 && (statb.st_flags & SF_SNAPSHOT) == 0) { 198 pfatal("BACKGROUND FSCK LACKS A SNAPSHOT\n"); 199 exit(EEXIT); 200 } 201 if (bkgrdflag != 0) { 202 cursnapshot = statb.st_ino; 203 } else { 204 pfatal("%s IS NOT A DISK DEVICE\n", dev); 205 if (reply("CONTINUE") == 0) 206 return (0); 207 } 208 } 209 saved_fsreadfd = fsreadfd; 210 if ((fsreadfd = open(dev, O_RDONLY)) < 0) { 211 fsreadfd = saved_fsreadfd; 212 return (0); 213 } 214 if (saved_fsreadfd != -1) 215 close(saved_fsreadfd); 216 return (1); 217 } 218 219 /* 220 * Read in the super block and its summary info. 221 */ 222 int 223 readsb(void) 224 { 225 struct fs *fs; 226 227 sbhashfailed = 0; 228 readcnt[sblk.b_type]++; 229 /* 230 * If bflag is given, then check just that superblock. 231 */ 232 if (bflag) { 233 switch (sbget(fsreadfd, &fs, bflag * dev_bsize, 0)) { 234 case 0: 235 goto goodsb; 236 case EINTEGRITY: 237 printf("Check hash failed for superblock at %jd\n", 238 bflag); 239 return (0); 240 case ENOENT: 241 printf("%jd is not a file system superblock\n", bflag); 242 return (0); 243 case EIO: 244 default: 245 printf("I/O error reading %jd\n", bflag); 246 return (0); 247 } 248 } 249 /* 250 * Check for the standard superblock and use it if good. 251 */ 252 if (sbget(fsreadfd, &fs, UFS_STDSB, UFS_NOMSG) == 0) 253 goto goodsb; 254 /* 255 * Check if the only problem is a check-hash failure. 256 */ 257 skipclean = 0; 258 if (sbget(fsreadfd, &fs, UFS_STDSB, UFS_NOMSG | UFS_NOHASHFAIL) == 0) { 259 sbhashfailed = 1; 260 goto goodsb; 261 } 262 /* 263 * Do an exhaustive search for a usable superblock. 264 */ 265 switch (sbsearch(fsreadfd, &fs, 0)) { 266 case 0: 267 goto goodsb; 268 case ENOENT: 269 printf("SEARCH FOR ALTERNATE SUPER-BLOCK FAILED. " 270 "YOU MUST USE THE\n-b OPTION TO FSCK TO SPECIFY " 271 "THE LOCATION OF AN ALTERNATE\nSUPER-BLOCK TO " 272 "SUPPLY NEEDED INFORMATION; SEE fsck_ffs(8).\n"); 273 return (0); 274 case EIO: 275 default: 276 printf("I/O error reading a usable superblock\n"); 277 return (0); 278 } 279 280 goodsb: 281 memcpy(&sblock, fs, fs->fs_sbsize); 282 free(fs); 283 /* 284 * Compute block size that the file system is based on, 285 * according to fsbtodb, and adjust superblock block number 286 * so we can tell if this is an alternate later. 287 */ 288 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1); 289 sblk.b_bno = sblock.fs_sblockactualloc / dev_bsize; 290 sblk.b_size = SBLOCKSIZE; 291 /* 292 * If not yet done, update UFS1 superblock with new wider fields. 293 */ 294 if (sblock.fs_magic == FS_UFS1_MAGIC && 295 sblock.fs_maxbsize != sblock.fs_bsize) { 296 sblock.fs_maxbsize = sblock.fs_bsize; 297 sblock.fs_time = sblock.fs_old_time; 298 sblock.fs_size = sblock.fs_old_size; 299 sblock.fs_dsize = sblock.fs_old_dsize; 300 sblock.fs_csaddr = sblock.fs_old_csaddr; 301 sblock.fs_cstotal.cs_ndir = sblock.fs_old_cstotal.cs_ndir; 302 sblock.fs_cstotal.cs_nbfree = sblock.fs_old_cstotal.cs_nbfree; 303 sblock.fs_cstotal.cs_nifree = sblock.fs_old_cstotal.cs_nifree; 304 sblock.fs_cstotal.cs_nffree = sblock.fs_old_cstotal.cs_nffree; 305 } 306 havesb = 1; 307 return (1); 308 } 309 310 void 311 sblock_init(void) 312 { 313 314 fsreadfd = -1; 315 fswritefd = -1; 316 fsmodified = 0; 317 lfdir = 0; 318 initbarea(&sblk, BT_SUPERBLK); 319 sblk.b_un.b_buf = Malloc(SBLOCKSIZE); 320 if (sblk.b_un.b_buf == NULL) 321 errx(EEXIT, "cannot allocate space for superblock"); 322 dev_bsize = secsize = DEV_BSIZE; 323 } 324 325 /* 326 * Calculate a prototype superblock based on information in the boot area. 327 * When done the cgsblock macro can be calculated and the fs_ncg field 328 * can be used. Do NOT attempt to use other macros without verifying that 329 * their needed information is available! 330 */ 331 static int 332 calcsb(char *dev, int devfd, struct fs *fs) 333 { 334 struct fsrecovery *fsr; 335 char *fsrbuf; 336 u_int secsize; 337 338 /* 339 * We need fragments-per-group and the partition-size. 340 * 341 * Newfs stores these details at the end of the boot block area 342 * at the start of the filesystem partition. If they have been 343 * overwritten by a boot block, we fail. But usually they are 344 * there and we can use them. 345 */ 346 if (ioctl(devfd, DIOCGSECTORSIZE, &secsize) == -1) 347 return (0); 348 fsrbuf = Malloc(secsize); 349 if (fsrbuf == NULL) 350 errx(EEXIT, "calcsb: cannot allocate recovery buffer"); 351 if (blread(devfd, fsrbuf, 352 (SBLOCK_UFS2 - secsize) / dev_bsize, secsize) != 0) { 353 free(fsrbuf); 354 return (0); 355 } 356 fsr = (struct fsrecovery *)&fsrbuf[secsize - sizeof *fsr]; 357 if (fsr->fsr_magic != FS_UFS2_MAGIC) { 358 free(fsrbuf); 359 return (0); 360 } 361 memset(fs, 0, sizeof(struct fs)); 362 fs->fs_fpg = fsr->fsr_fpg; 363 fs->fs_fsbtodb = fsr->fsr_fsbtodb; 364 fs->fs_sblkno = fsr->fsr_sblkno; 365 fs->fs_magic = fsr->fsr_magic; 366 fs->fs_ncg = fsr->fsr_ncg; 367 free(fsrbuf); 368 return (1); 369 } 370 371 /* 372 * Check to see if recovery information exists. 373 * Return 1 if it exists or cannot be created. 374 * Return 0 if it does not exist and can be created. 375 */ 376 static int 377 chkrecovery(int devfd) 378 { 379 struct fsrecovery *fsr; 380 char *fsrbuf; 381 u_int secsize, rdsize; 382 383 /* 384 * Could not determine if backup material exists, so do not 385 * offer to create it. 386 */ 387 fsrbuf = NULL; 388 rdsize = sblock.fs_fsize; 389 if (ioctl(devfd, DIOCGSECTORSIZE, &secsize) == -1 || 390 rdsize % secsize != 0 || 391 (fsrbuf = Malloc(rdsize)) == NULL || 392 blread(devfd, fsrbuf, (SBLOCK_UFS2 - rdsize) / dev_bsize, 393 rdsize) != 0) { 394 free(fsrbuf); 395 return (1); 396 } 397 /* 398 * Recovery material has already been created, so do not 399 * need to create it again. 400 */ 401 fsr = (struct fsrecovery *)&fsrbuf[rdsize - sizeof *fsr]; 402 if (fsr->fsr_magic == FS_UFS2_MAGIC) { 403 free(fsrbuf); 404 return (1); 405 } 406 /* 407 * Recovery material has not been created and can be if desired. 408 */ 409 free(fsrbuf); 410 return (0); 411 } 412 413 /* 414 * Read the last filesystem-size piece of the boot block, replace the 415 * last 20 bytes with the recovery information, then write it back. 416 * The recovery information only works for UFS2 filesystems. 417 */ 418 static void 419 saverecovery(int readfd, int writefd) 420 { 421 struct fsrecovery *fsr; 422 char *fsrbuf; 423 u_int secsize, rdsize; 424 425 fsrbuf = NULL; 426 rdsize = sblock.fs_fsize; 427 if (sblock.fs_magic != FS_UFS2_MAGIC || 428 ioctl(readfd, DIOCGSECTORSIZE, &secsize) == -1 || 429 rdsize % secsize != 0 || 430 (fsrbuf = Malloc(rdsize)) == NULL || 431 blread(readfd, fsrbuf, (SBLOCK_UFS2 - rdsize) / dev_bsize, 432 rdsize) != 0) { 433 printf("RECOVERY DATA COULD NOT BE CREATED\n"); 434 free(fsrbuf); 435 return; 436 } 437 fsr = (struct fsrecovery *)&fsrbuf[rdsize - sizeof *fsr]; 438 fsr->fsr_magic = sblock.fs_magic; 439 fsr->fsr_fpg = sblock.fs_fpg; 440 fsr->fsr_fsbtodb = sblock.fs_fsbtodb; 441 fsr->fsr_sblkno = sblock.fs_sblkno; 442 fsr->fsr_ncg = sblock.fs_ncg; 443 blwrite(writefd, fsrbuf, (SBLOCK_UFS2 - rdsize) / dev_bsize, rdsize); 444 free(fsrbuf); 445 } 446