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 * 3. 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 void badsb(int listerr, const char *s);
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).
79 if (stat(dev, &statb) < 0) {
80 printf("Can't stat %s: %s\n", dev, strerror(errno));
87 if ((statb.st_mode & S_IFMT) != S_IFCHR &&
88 (statb.st_mode & S_IFMT) != S_IFBLK) {
89 if (bkgrdflag != 0 && (statb.st_flags & SF_SNAPSHOT) == 0) {
91 printf("background fsck lacks a snapshot\n");
94 if ((statb.st_flags & SF_SNAPSHOT) != 0 && cvtlevel == 0) {
95 cursnapshot = statb.st_ino;
98 (statb.st_flags & SF_SNAPSHOT) == 0) {
99 if (preen && bkgrdflag) {
103 pfatal("%s is not a disk device", dev);
104 if (reply("CONTINUE") == 0) {
116 pfatal("cannot convert a snapshot");
121 if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
126 printf("Can't open %s: %s\n", dev, strerror(errno));
132 if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0||
133 sysctlnametomib("vfs.ffs.adjblkcnt", adjblkcnt, &size) < 0||
134 sysctlnametomib("vfs.ffs.freefiles", freefiles, &size) < 0||
135 sysctlnametomib("vfs.ffs.freedirs", freedirs, &size) < 0 ||
136 sysctlnametomib("vfs.ffs.freeblks", freeblks, &size) < 0) {
137 pfatal("kernel lacks background fsck support\n");
141 * When kernel is lack of runtime bgfsck superblock summary
142 * adjustment functionality, it does not mean we can not
143 * continue, as old kernels will recompute the summary at
144 * mount time. However, it will be an unexpected softupdates
145 * inconsistency if it turns out that the summary is still
146 * incorrect. Set a flag so subsequent operation can know
150 if (sysctlnametomib("vfs.ffs.adjndir", adjndir, &size) < 0 ||
151 sysctlnametomib("vfs.ffs.adjnbfree", adjnbfree, &size) < 0 ||
152 sysctlnametomib("vfs.ffs.adjnifree", adjnifree, &size) < 0 ||
153 sysctlnametomib("vfs.ffs.adjnffree", adjnffree, &size) < 0 ||
154 sysctlnametomib("vfs.ffs.adjnumclusters", adjnumclusters, &size) < 0) {
156 pwarn("kernel lacks runtime superblock summary adjustment support");
158 cmd.version = FFS_CMD_VERSION;
159 cmd.handle = fsreadfd;
163 printf("** %s", dev);
164 if (bkgrdflag == 0 &&
165 (nflag || (fswritefd = open(dev, O_WRONLY)) < 0)) {
168 pfatal("NO WRITE ACCESS");
169 printf(" (NO WRITE)");
174 * Read in the superblock, looking for alternates if necessary
176 if (readsb(1) == 0) {
180 /* Looking for alternates is hard punt for now but retain structure */
183 if (skipclean && ckclean && sblock.fs_clean) {
184 pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
187 maxfsblock = sblock.fs_size;
188 maxino = sblock.fs_ncg * sblock.fs_ipg;
190 * Check and potentially fix certain fields in the super block.
192 if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
193 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
194 if (reply("SET TO DEFAULT") == 1) {
195 sblock.fs_optim = FS_OPTTIME;
199 if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
200 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
202 if (reply("SET TO DEFAULT") == 1) {
203 sblock.fs_minfree = 10;
207 if (sblock.fs_magic == FS_UFS1_MAGIC &&
208 sblock.fs_old_inodefmt < FS_44INODEFMT) {
209 pwarn("Format of file system is too old.\n");
210 pwarn("Must update to modern format using a version of fsck\n");
211 pfatal("from before 2002 with the command ``fsck -c 2''\n");
214 if (asblk.b_dirty && !bflag) {
215 memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize);
216 flush(fswritefd, &asblk);
219 * read in the summary info.
222 sblock.fs_csp = Calloc(1, sblock.fs_cssize);
223 if (sblock.fs_csp == NULL) {
224 printf("cannot alloc %u bytes for cg summary info\n",
225 (unsigned)sblock.fs_cssize);
228 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
229 size = MIN(sblock.fs_cssize - i, sblock.fs_bsize);
230 readcnt[sblk.b_type]++;
231 if (blread(fsreadfd, (char *)sblock.fs_csp + i,
232 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
233 size) != 0 && !asked) {
234 pfatal("BAD SUMMARY INFORMATION");
235 if (reply("CONTINUE") == 0) {
243 * allocate and initialize the necessary maps
245 bmapsize = roundup(howmany(maxfsblock, CHAR_BIT), sizeof(short));
246 blockmap = Calloc((unsigned)bmapsize, sizeof (char));
247 if (blockmap == NULL) {
248 printf("cannot alloc %u bytes for blockmap\n",
252 inostathead = Calloc(sblock.fs_ncg, sizeof(struct inostatlist));
253 if (inostathead == NULL) {
254 printf("cannot alloc %u bytes for inostathead\n",
255 (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg)));
258 numdirs = MAX(sblock.fs_cstotal.cs_ndir, 128);
261 listmax = numdirs + 10;
262 inpsort = (struct inoinfo **)Calloc(listmax, sizeof(struct inoinfo *));
263 inphead = (struct inoinfo **)Calloc(numdirs, sizeof(struct inoinfo *));
264 if (inpsort == NULL || inphead == NULL) {
265 printf("cannot alloc %ju bytes for inphead\n",
266 (uintmax_t)numdirs * sizeof(struct inoinfo *));
270 if (sblock.fs_flags & FS_DOSOFTDEP)
282 * Possible superblock locations ordered from most to least likely.
284 static int sblock_try[] = SBLOCKSEARCH;
286 #define BAD_MAGIC_MSG \
287 "The previous newfs operation on this volume did not complete.\n" \
288 "You must complete newfs before mounting this volume.\n"
291 * Read in the super block and its summary info.
301 readcnt[sblk.b_type]++;
302 if ((blread(fsreadfd, (char *)&sblock, super, (long)SBLOCKSIZE)))
304 if (sblock.fs_magic == FS_BAD_MAGIC) {
305 fprintf(stderr, BAD_MAGIC_MSG);
308 if (sblock.fs_magic != FS_UFS1_MAGIC &&
309 sblock.fs_magic != FS_UFS2_MAGIC) {
310 fprintf(stderr, "%jd is not a file system superblock\n",
315 for (i = 0; sblock_try[i] != -1; i++) {
316 super = sblock_try[i] / dev_bsize;
317 readcnt[sblk.b_type]++;
318 if ((blread(fsreadfd, (char *)&sblock, super,
321 if (sblock.fs_magic == FS_BAD_MAGIC) {
322 fprintf(stderr, BAD_MAGIC_MSG);
325 if ((sblock.fs_magic == FS_UFS1_MAGIC ||
326 (sblock.fs_magic == FS_UFS2_MAGIC &&
327 sblock.fs_sblockloc == sblock_try[i])) &&
328 sblock.fs_ncg >= 1 &&
329 sblock.fs_bsize >= MINBSIZE &&
330 sblock.fs_sbsize >= roundup(sizeof(struct fs), dev_bsize))
333 if (sblock_try[i] == -1) {
334 fprintf(stderr, "Cannot find file system superblock\n");
339 * Compute block size that the file system is based on,
340 * according to fsbtodb, and adjust superblock block number
341 * so we can tell if this is an alternate later.
344 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
345 sblk.b_bno = super / dev_bsize;
346 sblk.b_size = SBLOCKSIZE;
350 * Compare all fields that should not differ in alternate super block.
351 * When an alternate super-block is specified this check is skipped.
353 getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
356 if (altsblock.fs_sblkno != sblock.fs_sblkno ||
357 altsblock.fs_cblkno != sblock.fs_cblkno ||
358 altsblock.fs_iblkno != sblock.fs_iblkno ||
359 altsblock.fs_dblkno != sblock.fs_dblkno ||
360 altsblock.fs_ncg != sblock.fs_ncg ||
361 altsblock.fs_bsize != sblock.fs_bsize ||
362 altsblock.fs_fsize != sblock.fs_fsize ||
363 altsblock.fs_frag != sblock.fs_frag ||
364 altsblock.fs_bmask != sblock.fs_bmask ||
365 altsblock.fs_fmask != sblock.fs_fmask ||
366 altsblock.fs_bshift != sblock.fs_bshift ||
367 altsblock.fs_fshift != sblock.fs_fshift ||
368 altsblock.fs_fragshift != sblock.fs_fragshift ||
369 altsblock.fs_fsbtodb != sblock.fs_fsbtodb ||
370 altsblock.fs_sbsize != sblock.fs_sbsize ||
371 altsblock.fs_nindir != sblock.fs_nindir ||
372 altsblock.fs_inopb != sblock.fs_inopb ||
373 altsblock.fs_cssize != sblock.fs_cssize ||
374 altsblock.fs_ipg != sblock.fs_ipg ||
375 altsblock.fs_fpg != sblock.fs_fpg ||
376 altsblock.fs_magic != sblock.fs_magic) {
380 printf("%s: ", cdevname);
382 "VALUES IN SUPER BLOCK LSB=%jd DISAGREE WITH THOSE IN\n"
383 "FIRST ALTERNATE LSB=%jd\n",
384 sblk.b_bno, asblk.b_bno);
385 if (reply("IGNORE ALTERNATE SUPER BLOCK") == 0)
390 * If not yet done, update UFS1 superblock with new wider fields.
392 if (sblock.fs_magic == FS_UFS1_MAGIC &&
393 sblock.fs_maxbsize != sblock.fs_bsize) {
394 sblock.fs_maxbsize = sblock.fs_bsize;
395 sblock.fs_time = sblock.fs_old_time;
396 sblock.fs_size = sblock.fs_old_size;
397 sblock.fs_dsize = sblock.fs_old_dsize;
398 sblock.fs_csaddr = sblock.fs_old_csaddr;
399 sblock.fs_cstotal.cs_ndir = sblock.fs_old_cstotal.cs_ndir;
400 sblock.fs_cstotal.cs_nbfree = sblock.fs_old_cstotal.cs_nbfree;
401 sblock.fs_cstotal.cs_nifree = sblock.fs_old_cstotal.cs_nifree;
402 sblock.fs_cstotal.cs_nffree = sblock.fs_old_cstotal.cs_nffree;
415 initbarea(&sblk, BT_SUPERBLK);
416 initbarea(&asblk, BT_SUPERBLK);
417 sblk.b_un.b_buf = Malloc(SBLOCKSIZE);
418 asblk.b_un.b_buf = Malloc(SBLOCKSIZE);
419 if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
420 errx(EEXIT, "cannot allocate space for superblock");
421 dev_bsize = secsize = DEV_BSIZE;