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;
65 #define altsblock (*asblk.b_un.b_fs)
66 #define POWEROF2(num) (((num) & ((num) - 1)) == 0)
68 static int calcsb(char *dev, int devfd, struct fs *fs);
69 static void saverecovery(int readfd, int writefd);
70 static int chkrecovery(int devfd);
73 * Read in a superblock finding an alternate if necessary.
74 * Return 1 if successful, 0 if unsuccessful, -1 if file system
75 * is already clean (ckclean and preen mode only).
89 if (stat(dev, &statb) < 0) {
90 printf("Can't stat %s: %s\n", dev, strerror(errno));
97 if ((statb.st_mode & S_IFMT) != S_IFCHR &&
98 (statb.st_mode & S_IFMT) != S_IFBLK) {
99 if (bkgrdflag != 0 && (statb.st_flags & SF_SNAPSHOT) == 0) {
101 printf("background fsck lacks a snapshot\n");
104 if ((statb.st_flags & SF_SNAPSHOT) != 0 && cvtlevel == 0) {
105 cursnapshot = statb.st_ino;
108 (statb.st_flags & SF_SNAPSHOT) == 0) {
109 if (preen && bkgrdflag) {
113 pfatal("%s is not a disk device", dev);
114 if (reply("CONTINUE") == 0) {
126 pfatal("cannot convert a snapshot");
131 if ((fsreadfd = open(dev, O_RDONLY)) < 0 ||
132 ufs_disk_fillout_blank(&disk, dev) < 0) {
137 printf("Can't open %s: %s\n", dev, strerror(errno));
143 if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0||
144 sysctlnametomib("vfs.ffs.adjblkcnt", adjblkcnt, &size) < 0||
145 sysctlnametomib("vfs.ffs.setsize", setsize, &size) < 0 ||
146 sysctlnametomib("vfs.ffs.freefiles", freefiles, &size) < 0||
147 sysctlnametomib("vfs.ffs.freedirs", freedirs, &size) < 0 ||
148 sysctlnametomib("vfs.ffs.freeblks", freeblks, &size) < 0) {
149 pfatal("kernel lacks background fsck support\n");
153 * When kernel is lack of runtime bgfsck superblock summary
154 * adjustment functionality, it does not mean we can not
155 * continue, as old kernels will recompute the summary at
156 * mount time. However, it will be an unexpected softupdates
157 * inconsistency if it turns out that the summary is still
158 * incorrect. Set a flag so subsequent operation can know
162 if (sysctlnametomib("vfs.ffs.adjndir", adjndir, &size) < 0 ||
163 sysctlnametomib("vfs.ffs.adjnbfree", adjnbfree, &size) < 0 ||
164 sysctlnametomib("vfs.ffs.adjnifree", adjnifree, &size) < 0 ||
165 sysctlnametomib("vfs.ffs.adjnffree", adjnffree, &size) < 0 ||
166 sysctlnametomib("vfs.ffs.adjnumclusters", adjnumclusters, &size) < 0) {
168 pwarn("kernel lacks runtime superblock summary adjustment support");
170 cmd.version = FFS_CMD_VERSION;
171 cmd.handle = fsreadfd;
175 printf("** %s", dev);
176 if (bkgrdflag == 0 &&
177 (nflag || ufs_disk_write(&disk) < 0 ||
178 (fswritefd = dup(disk.d_fd)) < 0)) {
181 pfatal("NO WRITE ACCESS");
182 printf(" (NO WRITE)");
187 * Read in the superblock, looking for alternates if necessary
189 if (readsb(1) == 0) {
191 if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
193 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
195 for (cg = 0; cg < proto.fs_ncg; cg++) {
196 bflag = fsbtodb(&proto, cgsblock(&proto, cg));
200 if (cg >= proto.fs_ncg) {
201 printf("%s %s\n%s %s\n%s %s\n",
202 "SEARCH FOR ALTERNATE SUPER-BLOCK",
203 "FAILED. YOU MUST USE THE",
204 "-b OPTION TO FSCK TO SPECIFY THE",
205 "LOCATION OF AN ALTERNATE",
206 "SUPER-BLOCK TO SUPPLY NEEDED",
207 "INFORMATION; SEE fsck_ffs(8).");
211 pwarn("USING ALTERNATE SUPERBLOCK AT %jd\n", bflag);
214 /* Save copy of things needed by libufs */
215 memcpy(&disk.d_fs, &sblock, sblock.fs_sbsize);
216 disk.d_ufs = (sblock.fs_magic == FS_UFS1_MAGIC) ? 1 : 2;
217 disk.d_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
218 disk.d_sblock = sblock.fs_sblockloc / disk.d_bsize;
219 disk.d_si = sblock.fs_si;
221 if (skipclean && ckclean && sblock.fs_clean) {
222 pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
225 maxfsblock = sblock.fs_size;
226 maxino = sblock.fs_ncg * sblock.fs_ipg;
228 * Check and potentially fix certain fields in the super block.
230 if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
231 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
232 if (reply("SET TO DEFAULT") == 1) {
233 sblock.fs_optim = FS_OPTTIME;
237 if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
238 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
240 if (reply("SET TO DEFAULT") == 1) {
241 sblock.fs_minfree = 10;
245 if (sblock.fs_magic == FS_UFS1_MAGIC &&
246 sblock.fs_old_inodefmt < FS_44INODEFMT) {
247 pwarn("Format of file system is too old.\n");
248 pwarn("Must update to modern format using a version of fsck\n");
249 pfatal("from before 2002 with the command ``fsck -c 2''\n");
252 if (asblk.b_dirty && !bflag) {
253 memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize);
254 flush(fswritefd, &asblk);
256 if (preen == 0 && yflag == 0 && sblock.fs_magic == FS_UFS2_MAGIC &&
257 fswritefd != -1 && chkrecovery(fsreadfd) == 0 &&
258 reply("SAVE DATA TO FIND ALTERNATE SUPERBLOCKS") != 0)
259 saverecovery(fsreadfd, fswritefd);
261 * read in the summary info.
264 sblock.fs_csp = Calloc(1, sblock.fs_cssize);
265 if (sblock.fs_csp == NULL) {
266 printf("cannot alloc %u bytes for cg summary info\n",
267 (unsigned)sblock.fs_cssize);
270 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
271 size = MIN(sblock.fs_cssize - i, sblock.fs_bsize);
272 readcnt[sblk.b_type]++;
273 if (blread(fsreadfd, (char *)sblock.fs_csp + i,
274 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
275 size) != 0 && !asked) {
276 pfatal("BAD SUMMARY INFORMATION");
277 if (reply("CONTINUE") == 0) {
285 * allocate and initialize the necessary maps
287 bmapsize = roundup(howmany(maxfsblock, CHAR_BIT), sizeof(short));
288 blockmap = Calloc((unsigned)bmapsize, sizeof (char));
289 if (blockmap == NULL) {
290 printf("cannot alloc %u bytes for blockmap\n",
294 inostathead = Calloc(sblock.fs_ncg, sizeof(struct inostatlist));
295 if (inostathead == NULL) {
296 printf("cannot alloc %u bytes for inostathead\n",
297 (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg)));
300 numdirs = MAX(sblock.fs_cstotal.cs_ndir, 128);
303 listmax = numdirs + 10;
304 inpsort = (struct inoinfo **)Calloc(listmax, sizeof(struct inoinfo *));
305 inphead = (struct inoinfo **)Calloc(numdirs, sizeof(struct inoinfo *));
306 if (inpsort == NULL || inphead == NULL) {
307 printf("cannot alloc %ju bytes for inphead\n",
308 (uintmax_t)numdirs * sizeof(struct inoinfo *));
312 if (sblock.fs_flags & FS_DOSOFTDEP)
324 * Read in the super block and its summary info.
333 super = bflag ? bflag * dev_bsize : STDSB_NOHASHFAIL;
334 readcnt[sblk.b_type]++;
335 if ((ret = sbget(fsreadfd, &fs, super)) != 0) {
338 /* Superblock check-hash failed */
342 printf("%jd is not a file system "
343 "superblock\n", super / dev_bsize);
345 printf("Cannot find file system "
350 printf("I/O error reading %jd\n",
355 memcpy(&sblock, fs, fs->fs_sbsize);
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.
362 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
363 sblk.b_bno = sblock.fs_sblockactualloc / dev_bsize;
364 sblk.b_size = SBLOCKSIZE;
366 * Compare all fields that should not differ in alternate super block.
367 * When an alternate super-block is specified this check is skipped.
371 getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
376 if (altsblock.x != sblock.x) { \
378 if (listerr && debug) \
379 printf("SUPER BLOCK VS ALTERNATE MISMATCH %s: " y " vs " y "\n", \
380 #x, (intmax_t)sblock.x, (intmax_t)altsblock.x); \
382 CHK(fs_sblkno, "%jd");
383 CHK(fs_cblkno, "%jd");
384 CHK(fs_iblkno, "%jd");
385 CHK(fs_dblkno, "%jd");
387 CHK(fs_bsize, "%jd");
388 CHK(fs_fsize, "%jd");
390 CHK(fs_bmask, "%#jx");
391 CHK(fs_fmask, "%#jx");
392 CHK(fs_bshift, "%jd");
393 CHK(fs_fshift, "%jd");
394 CHK(fs_fragshift, "%jd");
395 CHK(fs_fsbtodb, "%jd");
396 CHK(fs_sbsize, "%jd");
397 CHK(fs_nindir, "%jd");
398 CHK(fs_inopb, "%jd");
399 CHK(fs_cssize, "%jd");
402 CHK(fs_magic, "%#jx");
408 printf("%s: ", cdevname);
410 "VALUES IN SUPER BLOCK LSB=%jd DISAGREE WITH THOSE IN\n"
411 "LAST ALTERNATE LSB=%jd\n",
412 sblk.b_bno, asblk.b_bno);
413 if (reply("IGNORE ALTERNATE SUPER BLOCK") == 0)
418 * If not yet done, update UFS1 superblock with new wider fields.
420 if (sblock.fs_magic == FS_UFS1_MAGIC &&
421 sblock.fs_maxbsize != sblock.fs_bsize) {
422 sblock.fs_maxbsize = sblock.fs_bsize;
423 sblock.fs_time = sblock.fs_old_time;
424 sblock.fs_size = sblock.fs_old_size;
425 sblock.fs_dsize = sblock.fs_old_dsize;
426 sblock.fs_csaddr = sblock.fs_old_csaddr;
427 sblock.fs_cstotal.cs_ndir = sblock.fs_old_cstotal.cs_ndir;
428 sblock.fs_cstotal.cs_nbfree = sblock.fs_old_cstotal.cs_nbfree;
429 sblock.fs_cstotal.cs_nifree = sblock.fs_old_cstotal.cs_nifree;
430 sblock.fs_cstotal.cs_nffree = sblock.fs_old_cstotal.cs_nffree;
443 initbarea(&sblk, BT_SUPERBLK);
444 initbarea(&asblk, BT_SUPERBLK);
445 sblk.b_un.b_buf = Malloc(SBLOCKSIZE);
446 asblk.b_un.b_buf = Malloc(SBLOCKSIZE);
447 if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
448 errx(EEXIT, "cannot allocate space for superblock");
449 dev_bsize = secsize = DEV_BSIZE;
453 * Calculate a prototype superblock based on information in the boot area.
454 * When done the cgsblock macro can be calculated and the fs_ncg field
455 * can be used. Do NOT attempt to use other macros without verifying that
456 * their needed information is available!
459 calcsb(char *dev, int devfd, struct fs *fs)
461 struct fsrecovery *fsr;
466 * We need fragments-per-group and the partition-size.
468 * Newfs stores these details at the end of the boot block area
469 * at the start of the filesystem partition. If they have been
470 * overwritten by a boot block, we fail. But usually they are
471 * there and we can use them.
473 if (ioctl(devfd, DIOCGSECTORSIZE, &secsize) == -1)
475 fsrbuf = Malloc(secsize);
477 errx(EEXIT, "calcsb: cannot allocate recovery buffer");
478 if (blread(devfd, fsrbuf,
479 (SBLOCK_UFS2 - secsize) / dev_bsize, secsize) != 0) {
483 fsr = (struct fsrecovery *)&fsrbuf[secsize - sizeof *fsr];
484 if (fsr->fsr_magic != FS_UFS2_MAGIC) {
488 memset(fs, 0, sizeof(struct fs));
489 fs->fs_fpg = fsr->fsr_fpg;
490 fs->fs_fsbtodb = fsr->fsr_fsbtodb;
491 fs->fs_sblkno = fsr->fsr_sblkno;
492 fs->fs_magic = fsr->fsr_magic;
493 fs->fs_ncg = fsr->fsr_ncg;
499 * Check to see if recovery information exists.
500 * Return 1 if it exists or cannot be created.
501 * Return 0 if it does not exist and can be created.
504 chkrecovery(int devfd)
506 struct fsrecovery *fsr;
511 * Could not determine if backup material exists, so do not
512 * offer to create it.
515 if (ioctl(devfd, DIOCGSECTORSIZE, &secsize) == -1 ||
516 (fsrbuf = Malloc(secsize)) == NULL ||
517 blread(devfd, fsrbuf, (SBLOCK_UFS2 - secsize) / dev_bsize,
523 * Recovery material has already been created, so do not
524 * need to create it again.
526 fsr = (struct fsrecovery *)&fsrbuf[secsize - sizeof *fsr];
527 if (fsr->fsr_magic == FS_UFS2_MAGIC) {
532 * Recovery material has not been created and can be if desired.
539 * Read the last sector of the boot block, replace the last
540 * 20 bytes with the recovery information, then write it back.
541 * The recovery information only works for UFS2 filesystems.
544 saverecovery(int readfd, int writefd)
546 struct fsrecovery *fsr;
551 if (sblock.fs_magic != FS_UFS2_MAGIC ||
552 ioctl(readfd, DIOCGSECTORSIZE, &secsize) == -1 ||
553 (fsrbuf = Malloc(secsize)) == NULL ||
554 blread(readfd, fsrbuf, (SBLOCK_UFS2 - secsize) / dev_bsize,
556 printf("RECOVERY DATA COULD NOT BE CREATED\n");
560 fsr = (struct fsrecovery *)&fsrbuf[secsize - sizeof *fsr];
561 fsr->fsr_magic = sblock.fs_magic;
562 fsr->fsr_fpg = sblock.fs_fpg;
563 fsr->fsr_fsbtodb = sblock.fs_fsbtodb;
564 fsr->fsr_sblkno = sblock.fs_sblkno;
565 fsr->fsr_ncg = sblock.fs_ncg;
566 blwrite(writefd, fsrbuf, (SBLOCK_UFS2 - secsize) / secsize, secsize);