2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
34 static const char sccsid[] = "@(#)setup.c 8.10 (Berkeley) 5/9/95";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
44 #include <sys/disklabel.h>
46 #include <sys/sysctl.h>
48 #include <ufs/ufs/dinode.h>
49 #include <ufs/ffs/fs.h>
61 struct inoinfo **inphead, **inpsort; /* info about all inodes */
64 #define altsblock (*asblk.b_un.b_fs)
65 #define POWEROF2(num) (((num) & ((num) - 1)) == 0)
67 static int calcsb(char *dev, int devfd, struct fs *fs);
68 static void saverecovery(int readfd, int writefd);
69 static int chkrecovery(int devfd);
72 * Read in a superblock finding an alternate if necessary.
73 * Return 1 if successful, 0 if unsuccessful, -1 if file system
74 * is already clean (ckclean and preen mode only).
83 * We are expected to have an open file descriptor
88 * If we do not yet have a superblock, read it in looking
89 * for alternates if necessary.
91 if (havesb == 0 && readsb(1) == 0) {
93 if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
95 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
97 for (cg = 0; cg < proto.fs_ncg; cg++) {
98 bflag = fsbtodb(&proto, cgsblock(&proto, cg));
102 if (cg >= proto.fs_ncg) {
103 printf("SEARCH FOR ALTERNATE SUPER-BLOCK FAILED. "
104 "YOU MUST USE THE\n-b OPTION TO FSCK TO SPECIFY "
105 "THE LOCATION OF AN ALTERNATE\nSUPER-BLOCK TO "
106 "SUPPLY NEEDED INFORMATION; SEE fsck_ffs(8).\n");
110 pwarn("USING ALTERNATE SUPERBLOCK AT %jd\n", bflag);
114 printf("** %s", dev);
115 if (bkgrdflag == 0 &&
116 (nflag || (fswritefd = open(dev, O_WRONLY)) < 0)) {
119 pfatal("NO WRITE ACCESS");
120 printf(" (NO WRITE)");
124 if (sbhashfailed != 0) {
125 pwarn("SUPERBLOCK CHECK HASH FAILED");
127 pwarn("OPENED READONLY SO CANNOT CORRECT CHECK HASH\n");
128 else if (preen || reply("CORRECT CHECK HASH") != 0) {
130 printf(" (CORRECTED)\n");
135 if (skipclean && ckclean && sblock.fs_clean) {
136 pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
139 maxfsblock = sblock.fs_size;
140 maxino = sblock.fs_ncg * sblock.fs_ipg;
142 * Check and potentially fix certain fields in the super block.
144 if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
145 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
146 if (reply("SET TO DEFAULT") == 1) {
147 sblock.fs_optim = FS_OPTTIME;
151 if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
152 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
154 if (reply("SET TO DEFAULT") == 1) {
155 sblock.fs_minfree = 10;
159 if (sblock.fs_magic == FS_UFS1_MAGIC &&
160 sblock.fs_old_inodefmt < FS_44INODEFMT) {
161 pwarn("Format of file system is too old.\n");
162 pwarn("Must update to modern format using a version of fsck\n");
163 pfatal("from before 2002 with the command ``fsck -c 2''\n");
166 if ((asblk.b_flags & B_DIRTY) != 0 && !bflag) {
167 memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize);
168 flush(fswritefd, &asblk);
170 if (preen == 0 && yflag == 0 && sblock.fs_magic == FS_UFS2_MAGIC &&
171 fswritefd != -1 && chkrecovery(fsreadfd) == 0 &&
172 reply("SAVE DATA TO FIND ALTERNATE SUPERBLOCKS") != 0)
173 saverecovery(fsreadfd, fswritefd);
175 * allocate and initialize the necessary maps
177 bmapsize = roundup(howmany(maxfsblock, CHAR_BIT), sizeof(short));
178 blockmap = Calloc((unsigned)bmapsize, sizeof (char));
179 if (blockmap == NULL) {
180 printf("cannot alloc %u bytes for blockmap\n",
184 inostathead = Calloc(sblock.fs_ncg, sizeof(struct inostatlist));
185 if (inostathead == NULL) {
186 printf("cannot alloc %u bytes for inostathead\n",
187 (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg)));
190 numdirs = MAX(sblock.fs_cstotal.cs_ndir, 128);
193 listmax = numdirs + 10;
194 inpsort = (struct inoinfo **)Calloc(listmax, sizeof(struct inoinfo *));
195 inphead = (struct inoinfo **)Calloc(numdirs, sizeof(struct inoinfo *));
196 if (inpsort == NULL || inphead == NULL) {
197 printf("cannot alloc %ju bytes for inphead\n",
198 (uintmax_t)numdirs * sizeof(struct inoinfo *));
202 if (sblock.fs_flags & FS_DOSOFTDEP)
214 * Read in the super block and its summary info.
223 super = bflag ? bflag * dev_bsize :
224 sbhashfailed ? STDSB_NOHASHFAIL_NOMSG : STDSB_NOMSG;
225 readcnt[sblk.b_type]++;
226 while ((ret = sbget(fsreadfd, &fs, super)) != 0) {
229 if (bflag || super == STDSB_NOHASHFAIL_NOMSG)
231 super = STDSB_NOHASHFAIL_NOMSG;
236 printf("%jd is not a file system "
237 "superblock\n", super / dev_bsize);
239 printf("Cannot find file system "
244 printf("I/O error reading %jd\n",
249 memcpy(&sblock, fs, fs->fs_sbsize);
252 * Compute block size that the file system is based on,
253 * according to fsbtodb, and adjust superblock block number
254 * so we can tell if this is an alternate later.
256 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
257 sblk.b_bno = sblock.fs_sblockactualloc / dev_bsize;
258 sblk.b_size = SBLOCKSIZE;
260 * Compare all fields that should not differ in alternate super block.
261 * When an alternate super-block is specified this check is skipped.
265 getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
270 if (altsblock.x != sblock.x) { \
272 if (listerr && debug) \
273 printf("SUPER BLOCK VS ALTERNATE MISMATCH %s: " y " vs " y "\n", \
274 #x, (intmax_t)sblock.x, (intmax_t)altsblock.x); \
276 CHK(fs_sblkno, "%jd");
277 CHK(fs_cblkno, "%jd");
278 CHK(fs_iblkno, "%jd");
279 CHK(fs_dblkno, "%jd");
281 CHK(fs_bsize, "%jd");
282 CHK(fs_fsize, "%jd");
284 CHK(fs_bmask, "%#jx");
285 CHK(fs_fmask, "%#jx");
286 CHK(fs_bshift, "%jd");
287 CHK(fs_fshift, "%jd");
288 CHK(fs_fragshift, "%jd");
289 CHK(fs_fsbtodb, "%jd");
290 CHK(fs_sbsize, "%jd");
291 CHK(fs_nindir, "%jd");
292 CHK(fs_inopb, "%jd");
293 CHK(fs_cssize, "%jd");
296 CHK(fs_magic, "%#jx");
302 printf("%s: ", cdevname);
304 "VALUES IN SUPER BLOCK LSB=%jd DISAGREE WITH THOSE IN\n"
305 "LAST ALTERNATE LSB=%jd\n",
306 sblk.b_bno, asblk.b_bno);
307 if (reply("IGNORE ALTERNATE SUPER BLOCK") == 0)
312 * If not yet done, update UFS1 superblock with new wider fields.
314 if (sblock.fs_magic == FS_UFS1_MAGIC &&
315 sblock.fs_maxbsize != sblock.fs_bsize) {
316 sblock.fs_maxbsize = sblock.fs_bsize;
317 sblock.fs_time = sblock.fs_old_time;
318 sblock.fs_size = sblock.fs_old_size;
319 sblock.fs_dsize = sblock.fs_old_dsize;
320 sblock.fs_csaddr = sblock.fs_old_csaddr;
321 sblock.fs_cstotal.cs_ndir = sblock.fs_old_cstotal.cs_ndir;
322 sblock.fs_cstotal.cs_nbfree = sblock.fs_old_cstotal.cs_nbfree;
323 sblock.fs_cstotal.cs_nifree = sblock.fs_old_cstotal.cs_nifree;
324 sblock.fs_cstotal.cs_nffree = sblock.fs_old_cstotal.cs_nffree;
337 initbarea(&sblk, BT_SUPERBLK);
338 initbarea(&asblk, BT_SUPERBLK);
339 sblk.b_un.b_buf = Malloc(SBLOCKSIZE);
340 asblk.b_un.b_buf = Malloc(SBLOCKSIZE);
341 if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
342 errx(EEXIT, "cannot allocate space for superblock");
343 dev_bsize = secsize = DEV_BSIZE;
347 * Calculate a prototype superblock based on information in the boot area.
348 * When done the cgsblock macro can be calculated and the fs_ncg field
349 * can be used. Do NOT attempt to use other macros without verifying that
350 * their needed information is available!
353 calcsb(char *dev, int devfd, struct fs *fs)
355 struct fsrecovery *fsr;
360 * We need fragments-per-group and the partition-size.
362 * Newfs stores these details at the end of the boot block area
363 * at the start of the filesystem partition. If they have been
364 * overwritten by a boot block, we fail. But usually they are
365 * there and we can use them.
367 if (ioctl(devfd, DIOCGSECTORSIZE, &secsize) == -1)
369 fsrbuf = Malloc(secsize);
371 errx(EEXIT, "calcsb: cannot allocate recovery buffer");
372 if (blread(devfd, fsrbuf,
373 (SBLOCK_UFS2 - secsize) / dev_bsize, secsize) != 0) {
377 fsr = (struct fsrecovery *)&fsrbuf[secsize - sizeof *fsr];
378 if (fsr->fsr_magic != FS_UFS2_MAGIC) {
382 memset(fs, 0, sizeof(struct fs));
383 fs->fs_fpg = fsr->fsr_fpg;
384 fs->fs_fsbtodb = fsr->fsr_fsbtodb;
385 fs->fs_sblkno = fsr->fsr_sblkno;
386 fs->fs_magic = fsr->fsr_magic;
387 fs->fs_ncg = fsr->fsr_ncg;
393 * Check to see if recovery information exists.
394 * Return 1 if it exists or cannot be created.
395 * Return 0 if it does not exist and can be created.
398 chkrecovery(int devfd)
400 struct fsrecovery *fsr;
405 * Could not determine if backup material exists, so do not
406 * offer to create it.
409 if (ioctl(devfd, DIOCGSECTORSIZE, &secsize) == -1 ||
410 (fsrbuf = Malloc(secsize)) == NULL ||
411 blread(devfd, fsrbuf, (SBLOCK_UFS2 - secsize) / dev_bsize,
417 * Recovery material has already been created, so do not
418 * need to create it again.
420 fsr = (struct fsrecovery *)&fsrbuf[secsize - sizeof *fsr];
421 if (fsr->fsr_magic == FS_UFS2_MAGIC) {
426 * Recovery material has not been created and can be if desired.
433 * Read the last sector of the boot block, replace the last
434 * 20 bytes with the recovery information, then write it back.
435 * The recovery information only works for UFS2 filesystems.
438 saverecovery(int readfd, int writefd)
440 struct fsrecovery *fsr;
445 if (sblock.fs_magic != FS_UFS2_MAGIC ||
446 ioctl(readfd, DIOCGSECTORSIZE, &secsize) == -1 ||
447 (fsrbuf = Malloc(secsize)) == NULL ||
448 blread(readfd, fsrbuf, (SBLOCK_UFS2 - secsize) / dev_bsize,
450 printf("RECOVERY DATA COULD NOT BE CREATED\n");
454 fsr = (struct fsrecovery *)&fsrbuf[secsize - sizeof *fsr];
455 fsr->fsr_magic = sblock.fs_magic;
456 fsr->fsr_fpg = sblock.fs_fpg;
457 fsr->fsr_fsbtodb = sblock.fs_fsbtodb;
458 fsr->fsr_sblkno = sblock.fs_sblkno;
459 fsr->fsr_ncg = sblock.fs_ncg;
460 blwrite(writefd, fsrbuf, (SBLOCK_UFS2 - secsize) / secsize, secsize);