2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
32 static const char sccsid[] = "@(#)setup.c 8.10 (Berkeley) 5/9/95";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
41 #include <sys/disklabel.h>
43 #include <sys/sysctl.h>
45 #include <ufs/ufs/dinode.h>
46 #include <ufs/ffs/fs.h>
58 #define altsblock (*asblk.b_un.b_fs)
59 #define POWEROF2(num) (((num) & ((num) - 1)) == 0)
61 static int calcsb(char *dev, int devfd, struct fs *fs);
62 static void saverecovery(int readfd, int writefd);
63 static int chkrecovery(int devfd);
66 * Read in a superblock finding an alternate if necessary.
67 * Return 1 if successful, 0 if unsuccessful, -1 if file system
68 * is already clean (ckclean and preen mode only).
82 if (stat(dev, &statb) < 0) {
83 printf("Can't stat %s: %s\n", dev, strerror(errno));
90 if ((statb.st_mode & S_IFMT) != S_IFCHR &&
91 (statb.st_mode & S_IFMT) != S_IFBLK) {
92 if (bkgrdflag != 0 && (statb.st_flags & SF_SNAPSHOT) == 0) {
94 printf("background fsck lacks a snapshot\n");
97 if ((statb.st_flags & SF_SNAPSHOT) != 0 && cvtlevel == 0) {
98 cursnapshot = statb.st_ino;
101 (statb.st_flags & SF_SNAPSHOT) == 0) {
102 if (preen && bkgrdflag) {
106 pfatal("%s is not a disk device", dev);
107 if (reply("CONTINUE") == 0) {
119 pfatal("cannot convert a snapshot");
124 if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
129 printf("Can't open %s: %s\n", dev, strerror(errno));
135 if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0||
136 sysctlnametomib("vfs.ffs.adjblkcnt", adjblkcnt, &size) < 0||
137 sysctlnametomib("vfs.ffs.freefiles", freefiles, &size) < 0||
138 sysctlnametomib("vfs.ffs.freedirs", freedirs, &size) < 0 ||
139 sysctlnametomib("vfs.ffs.freeblks", freeblks, &size) < 0) {
140 pfatal("kernel lacks background fsck support\n");
144 * When kernel is lack of runtime bgfsck superblock summary
145 * adjustment functionality, it does not mean we can not
146 * continue, as old kernels will recompute the summary at
147 * mount time. However, it will be an unexpected softupdates
148 * inconsistency if it turns out that the summary is still
149 * incorrect. Set a flag so subsequent operation can know
153 if (sysctlnametomib("vfs.ffs.adjndir", adjndir, &size) < 0 ||
154 sysctlnametomib("vfs.ffs.adjnbfree", adjnbfree, &size) < 0 ||
155 sysctlnametomib("vfs.ffs.adjnifree", adjnifree, &size) < 0 ||
156 sysctlnametomib("vfs.ffs.adjnffree", adjnffree, &size) < 0 ||
157 sysctlnametomib("vfs.ffs.adjnumclusters", adjnumclusters, &size) < 0) {
159 pwarn("kernel lacks runtime superblock summary adjustment support");
161 cmd.version = FFS_CMD_VERSION;
162 cmd.handle = fsreadfd;
166 printf("** %s", dev);
167 if (bkgrdflag == 0 &&
168 (nflag || (fswritefd = open(dev, O_WRONLY)) < 0)) {
171 pfatal("NO WRITE ACCESS");
172 printf(" (NO WRITE)");
177 * Read in the superblock, looking for alternates if necessary
179 if (readsb(1) == 0) {
181 if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
183 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
185 for (cg = 0; cg < proto.fs_ncg; cg++) {
186 bflag = fsbtodb(&proto, cgsblock(&proto, cg));
190 if (cg >= proto.fs_ncg) {
191 printf("%s %s\n%s %s\n%s %s\n",
192 "SEARCH FOR ALTERNATE SUPER-BLOCK",
193 "FAILED. YOU MUST USE THE",
194 "-b OPTION TO FSCK TO SPECIFY THE",
195 "LOCATION OF AN ALTERNATE",
196 "SUPER-BLOCK TO SUPPLY NEEDED",
197 "INFORMATION; SEE fsck_ffs(8).");
201 pwarn("USING ALTERNATE SUPERBLOCK AT %jd\n", bflag);
204 if (skipclean && ckclean && sblock.fs_clean) {
205 pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
208 maxfsblock = sblock.fs_size;
209 maxino = sblock.fs_ncg * sblock.fs_ipg;
211 * Check and potentially fix certain fields in the super block.
213 if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
214 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
215 if (reply("SET TO DEFAULT") == 1) {
216 sblock.fs_optim = FS_OPTTIME;
220 if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
221 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
223 if (reply("SET TO DEFAULT") == 1) {
224 sblock.fs_minfree = 10;
228 if (sblock.fs_magic == FS_UFS1_MAGIC &&
229 sblock.fs_old_inodefmt < FS_44INODEFMT) {
230 pwarn("Format of file system is too old.\n");
231 pwarn("Must update to modern format using a version of fsck\n");
232 pfatal("from before 2002 with the command ``fsck -c 2''\n");
235 if (asblk.b_dirty && !bflag) {
236 memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize);
237 flush(fswritefd, &asblk);
239 if (preen == 0 && yflag == 0 && sblock.fs_magic == FS_UFS2_MAGIC &&
240 fswritefd != -1 && chkrecovery(fsreadfd) == 0 &&
241 reply("SAVE DATA TO FIND ALTERNATE SUPERBLOCKS") != 0)
242 saverecovery(fsreadfd, fswritefd);
244 * read in the summary info.
247 sblock.fs_csp = Calloc(1, sblock.fs_cssize);
248 if (sblock.fs_csp == NULL) {
249 printf("cannot alloc %u bytes for cg summary info\n",
250 (unsigned)sblock.fs_cssize);
253 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
254 size = sblock.fs_cssize - i < sblock.fs_bsize ?
255 sblock.fs_cssize - i : sblock.fs_bsize;
256 readcnt[sblk.b_type]++;
257 if (blread(fsreadfd, (char *)sblock.fs_csp + i,
258 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
259 size) != 0 && !asked) {
260 pfatal("BAD SUMMARY INFORMATION");
261 if (reply("CONTINUE") == 0) {
269 * allocate and initialize the necessary maps
271 bmapsize = roundup(howmany(maxfsblock, CHAR_BIT), sizeof(short));
272 blockmap = Calloc((unsigned)bmapsize, sizeof (char));
273 if (blockmap == NULL) {
274 printf("cannot alloc %u bytes for blockmap\n",
278 inostathead = Calloc((unsigned)(sblock.fs_ncg),
279 sizeof(struct inostatlist));
280 if (inostathead == NULL) {
281 printf("cannot alloc %u bytes for inostathead\n",
282 (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg)));
285 numdirs = MAX(sblock.fs_cstotal.cs_ndir, 128);
288 listmax = numdirs + 10;
289 inpsort = (struct inoinfo **)Calloc((unsigned)listmax,
290 sizeof(struct inoinfo *));
291 inphead = (struct inoinfo **)Calloc((unsigned)numdirs,
292 sizeof(struct inoinfo *));
293 if (inpsort == NULL || inphead == NULL) {
294 printf("cannot alloc %ju bytes for inphead\n",
295 (uintmax_t)numdirs * sizeof(struct inoinfo *));
299 if (sblock.fs_flags & FS_DOSOFTDEP)
311 * Possible superblock locations ordered from most to least likely.
313 static int sblock_try[] = SBLOCKSEARCH;
315 #define BAD_MAGIC_MSG \
316 "The previous newfs operation on this volume did not complete.\n" \
317 "You must complete newfs before mounting this volume.\n"
320 * Read in the super block and its summary info.
330 readcnt[sblk.b_type]++;
331 if ((blread(fsreadfd, (char *)&sblock, super, (long)SBLOCKSIZE)))
333 if (sblock.fs_magic == FS_BAD_MAGIC) {
334 fprintf(stderr, BAD_MAGIC_MSG);
337 if (sblock.fs_magic != FS_UFS1_MAGIC &&
338 sblock.fs_magic != FS_UFS2_MAGIC) {
339 fprintf(stderr, "%jd is not a file system superblock\n",
344 for (i = 0; sblock_try[i] != -1; i++) {
345 super = sblock_try[i] / dev_bsize;
346 readcnt[sblk.b_type]++;
347 if ((blread(fsreadfd, (char *)&sblock, super,
350 if (sblock.fs_magic == FS_BAD_MAGIC) {
351 fprintf(stderr, BAD_MAGIC_MSG);
354 if ((sblock.fs_magic == FS_UFS1_MAGIC ||
355 (sblock.fs_magic == FS_UFS2_MAGIC &&
356 sblock.fs_sblockloc == sblock_try[i])) &&
357 sblock.fs_ncg >= 1 &&
358 sblock.fs_bsize >= MINBSIZE &&
359 sblock.fs_sbsize >= roundup(sizeof(struct fs), dev_bsize))
362 if (sblock_try[i] == -1) {
363 fprintf(stderr, "Cannot find file system superblock\n");
368 * Compute block size that the file system is based on,
369 * according to fsbtodb, and adjust superblock block number
370 * so we can tell if this is an alternate later.
373 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
374 sblk.b_bno = super / dev_bsize;
375 sblk.b_size = SBLOCKSIZE;
377 * Compare all fields that should not differ in alternate super block.
378 * When an alternate super-block is specified this check is skipped.
382 getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
387 if (altsblock.x != sblock.x) { \
389 if (listerr && debug) \
390 printf("SUPER BLOCK VS ALTERNATE MISMATCH %s: " y " vs " y "\n", \
391 #x, (intmax_t)sblock.x, (intmax_t)altsblock.x); \
393 CHK(fs_sblkno, "%jd");
394 CHK(fs_cblkno, "%jd");
395 CHK(fs_iblkno, "%jd");
396 CHK(fs_dblkno, "%jd");
398 CHK(fs_bsize, "%jd");
399 CHK(fs_fsize, "%jd");
401 CHK(fs_bmask, "%#jx");
402 CHK(fs_fmask, "%#jx");
403 CHK(fs_bshift, "%jd");
404 CHK(fs_fshift, "%jd");
405 CHK(fs_fragshift, "%jd");
406 CHK(fs_fsbtodb, "%jd");
407 CHK(fs_sbsize, "%jd");
408 CHK(fs_nindir, "%jd");
409 CHK(fs_inopb, "%jd");
410 CHK(fs_cssize, "%jd");
413 CHK(fs_magic, "%#jx");
419 printf("%s: ", cdevname);
421 "VALUES IN SUPER BLOCK LSB=%jd DISAGREE WITH THOSE IN\n"
422 "LAST ALTERNATE LSB=%jd\n",
423 sblk.b_bno, asblk.b_bno);
424 if (reply("IGNORE ALTERNATE SUPER BLOCK") == 0)
429 * If not yet done, update UFS1 superblock with new wider fields.
431 if (sblock.fs_magic == FS_UFS1_MAGIC &&
432 sblock.fs_maxbsize != sblock.fs_bsize) {
433 sblock.fs_maxbsize = sblock.fs_bsize;
434 sblock.fs_time = sblock.fs_old_time;
435 sblock.fs_size = sblock.fs_old_size;
436 sblock.fs_dsize = sblock.fs_old_dsize;
437 sblock.fs_csaddr = sblock.fs_old_csaddr;
438 sblock.fs_cstotal.cs_ndir = sblock.fs_old_cstotal.cs_ndir;
439 sblock.fs_cstotal.cs_nbfree = sblock.fs_old_cstotal.cs_nbfree;
440 sblock.fs_cstotal.cs_nifree = sblock.fs_old_cstotal.cs_nifree;
441 sblock.fs_cstotal.cs_nffree = sblock.fs_old_cstotal.cs_nffree;
454 initbarea(&sblk, BT_SUPERBLK);
455 initbarea(&asblk, BT_SUPERBLK);
456 sblk.b_un.b_buf = Malloc(SBLOCKSIZE);
457 asblk.b_un.b_buf = Malloc(SBLOCKSIZE);
458 if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
459 errx(EEXIT, "cannot allocate space for superblock");
460 dev_bsize = secsize = DEV_BSIZE;
464 * Calculate a prototype superblock based on information in the boot area.
465 * When done the cgsblock macro can be calculated and the fs_ncg field
466 * can be used. Do NOT attempt to use other macros without verifying that
467 * their needed information is available!
470 calcsb(char *dev, int devfd, struct fs *fs)
472 struct fsrecovery fsr;
475 * We need fragments-per-group and the partition-size.
477 * Newfs stores these details at the end of the boot block area
478 * at the start of the filesystem partition. If they have been
479 * overwritten by a boot block, we fail. But usually they are
480 * there and we can use them.
482 if (blread(devfd, (char *)&fsr,
483 (SBLOCK_UFS2 - sizeof(fsr)) / dev_bsize, sizeof(fsr)) ||
484 fsr.fsr_magic != FS_UFS2_MAGIC)
486 memset(fs, 0, sizeof(struct fs));
487 fs->fs_fpg = fsr.fsr_fpg;
488 fs->fs_fsbtodb = fsr.fsr_fsbtodb;
489 fs->fs_sblkno = fsr.fsr_sblkno;
490 fs->fs_magic = fsr.fsr_magic;
491 fs->fs_ncg = fsr.fsr_ncg;
496 * Check to see if recovery information exists.
499 chkrecovery(int devfd)
501 struct fsrecovery fsr;
503 if (blread(devfd, (char *)&fsr,
504 (SBLOCK_UFS2 - sizeof(fsr)) / dev_bsize, sizeof(fsr)) ||
505 fsr.fsr_magic != FS_UFS2_MAGIC)
511 * Read the last sector of the boot block, replace the last
512 * 20 bytes with the recovery information, then write it back.
513 * The recovery information only works for UFS2 filesystems.
516 saverecovery(int readfd, int writefd)
518 struct fsrecovery fsr;
520 if (sblock.fs_magic != FS_UFS2_MAGIC ||
521 blread(readfd, (char *)&fsr,
522 (SBLOCK_UFS2 - sizeof(fsr)) / dev_bsize, sizeof(fsr)))
524 fsr.fsr_magic = sblock.fs_magic;
525 fsr.fsr_fpg = sblock.fs_fpg;
526 fsr.fsr_fsbtodb = sblock.fs_fsbtodb;
527 fsr.fsr_sblkno = sblock.fs_sblkno;
528 fsr.fsr_ncg = sblock.fs_ncg;
529 blwrite(writefd, (char *)&fsr, (SBLOCK_UFS2 - sizeof(fsr)) / dev_bsize,