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).
80 if (stat(dev, &statb) < 0) {
81 printf("Can't stat %s: %s\n", dev, strerror(errno));
88 if ((statb.st_mode & S_IFMT) != S_IFCHR &&
89 (statb.st_mode & S_IFMT) != S_IFBLK) {
90 if (bkgrdflag != 0 && (statb.st_flags & SF_SNAPSHOT) == 0) {
92 printf("background fsck lacks a snapshot\n");
95 if ((statb.st_flags & SF_SNAPSHOT) != 0 && cvtlevel == 0) {
96 cursnapshot = statb.st_ino;
99 (statb.st_flags & SF_SNAPSHOT) == 0) {
100 if (preen && bkgrdflag) {
104 pfatal("%s is not a disk device", dev);
105 if (reply("CONTINUE") == 0) {
117 pfatal("cannot convert a snapshot");
122 if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
127 printf("Can't open %s: %s\n", dev, strerror(errno));
133 if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0||
134 sysctlnametomib("vfs.ffs.adjblkcnt", adjblkcnt, &size) < 0||
135 sysctlnametomib("vfs.ffs.freefiles", freefiles, &size) < 0||
136 sysctlnametomib("vfs.ffs.freedirs", freedirs, &size) < 0 ||
137 sysctlnametomib("vfs.ffs.freeblks", freeblks, &size) < 0) {
138 pfatal("kernel lacks background fsck support\n");
142 * When kernel is lack of runtime bgfsck superblock summary
143 * adjustment functionality, it does not mean we can not
144 * continue, as old kernels will recompute the summary at
145 * mount time. However, it will be an unexpected softupdates
146 * inconsistency if it turns out that the summary is still
147 * incorrect. Set a flag so subsequent operation can know
151 if (sysctlnametomib("vfs.ffs.adjndir", adjndir, &size) < 0 ||
152 sysctlnametomib("vfs.ffs.adjnbfree", adjnbfree, &size) < 0 ||
153 sysctlnametomib("vfs.ffs.adjnifree", adjnifree, &size) < 0 ||
154 sysctlnametomib("vfs.ffs.adjnffree", adjnffree, &size) < 0 ||
155 sysctlnametomib("vfs.ffs.adjnumclusters", adjnumclusters, &size) < 0) {
157 pwarn("kernel lacks runtime superblock summary adjustment support");
159 cmd.version = FFS_CMD_VERSION;
160 cmd.handle = fsreadfd;
164 printf("** %s", dev);
165 if (bkgrdflag == 0 &&
166 (nflag || (fswritefd = open(dev, O_WRONLY)) < 0)) {
169 pfatal("NO WRITE ACCESS");
170 printf(" (NO WRITE)");
175 * Read in the superblock, looking for alternates if necessary
177 if (readsb(1) == 0) {
181 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
183 for (cg = 0; cg < proto.fs_ncg; cg++) {
184 bflag = fsbtodb(&proto, cgsblock(&proto, cg));
188 if (cg >= proto.fs_ncg) {
189 printf("%s %s\n%s %s\n%s %s\n",
190 "SEARCH FOR ALTERNATE SUPER-BLOCK",
191 "FAILED. YOU MUST USE THE",
192 "-b OPTION TO FSCK TO SPECIFY THE",
193 "LOCATION OF AN ALTERNATE",
194 "SUPER-BLOCK TO SUPPLY NEEDED",
195 "INFORMATION; SEE fsck_ffs(8).");
199 pwarn("USING ALTERNATE SUPERBLOCK AT %jd\n", bflag);
202 if (skipclean && ckclean && sblock.fs_clean) {
203 pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
206 maxfsblock = sblock.fs_size;
207 maxino = sblock.fs_ncg * sblock.fs_ipg;
209 * Check and potentially fix certain fields in the super block.
211 if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
212 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
213 if (reply("SET TO DEFAULT") == 1) {
214 sblock.fs_optim = FS_OPTTIME;
218 if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
219 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
221 if (reply("SET TO DEFAULT") == 1) {
222 sblock.fs_minfree = 10;
226 if (sblock.fs_magic == FS_UFS1_MAGIC &&
227 sblock.fs_old_inodefmt < FS_44INODEFMT) {
228 pwarn("Format of file system is too old.\n");
229 pwarn("Must update to modern format using a version of fsck\n");
230 pfatal("from before 2002 with the command ``fsck -c 2''\n");
233 if (asblk.b_dirty && !bflag) {
234 memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize);
235 flush(fswritefd, &asblk);
238 * read in the summary info.
241 sblock.fs_csp = Calloc(1, sblock.fs_cssize);
242 if (sblock.fs_csp == NULL) {
243 printf("cannot alloc %u bytes for cg summary info\n",
244 (unsigned)sblock.fs_cssize);
247 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
248 size = MIN(sblock.fs_cssize - i, sblock.fs_bsize);
249 readcnt[sblk.b_type]++;
250 if (blread(fsreadfd, (char *)sblock.fs_csp + i,
251 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
252 size) != 0 && !asked) {
253 pfatal("BAD SUMMARY INFORMATION");
254 if (reply("CONTINUE") == 0) {
262 * allocate and initialize the necessary maps
264 bmapsize = roundup(howmany(maxfsblock, CHAR_BIT), sizeof(short));
265 blockmap = Calloc((unsigned)bmapsize, sizeof (char));
266 if (blockmap == NULL) {
267 printf("cannot alloc %u bytes for blockmap\n",
271 inostathead = Calloc(sblock.fs_ncg, sizeof(struct inostatlist));
272 if (inostathead == NULL) {
273 printf("cannot alloc %u bytes for inostathead\n",
274 (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg)));
277 numdirs = MAX(sblock.fs_cstotal.cs_ndir, 128);
280 listmax = numdirs + 10;
281 inpsort = (struct inoinfo **)Calloc(listmax, sizeof(struct inoinfo *));
282 inphead = (struct inoinfo **)Calloc(numdirs, sizeof(struct inoinfo *));
283 if (inpsort == NULL || inphead == NULL) {
284 printf("cannot alloc %ju bytes for inphead\n",
285 (uintmax_t)numdirs * sizeof(struct inoinfo *));
289 if (sblock.fs_flags & FS_DOSOFTDEP)
301 * Possible superblock locations ordered from most to least likely.
303 static int sblock_try[] = SBLOCKSEARCH;
305 #define BAD_MAGIC_MSG \
306 "The previous newfs operation on this volume did not complete.\n" \
307 "You must complete newfs before mounting this volume.\n"
310 * Read in the super block and its summary info.
320 readcnt[sblk.b_type]++;
321 if ((blread(fsreadfd, (char *)&sblock, super, (long)SBLOCKSIZE)))
323 if (sblock.fs_magic == FS_BAD_MAGIC) {
324 fprintf(stderr, BAD_MAGIC_MSG);
327 if (sblock.fs_magic != FS_UFS1_MAGIC &&
328 sblock.fs_magic != FS_UFS2_MAGIC) {
329 fprintf(stderr, "%jd is not a file system superblock\n",
334 for (i = 0; sblock_try[i] != -1; i++) {
335 super = sblock_try[i] / dev_bsize;
336 readcnt[sblk.b_type]++;
337 if ((blread(fsreadfd, (char *)&sblock, super,
340 if (sblock.fs_magic == FS_BAD_MAGIC) {
341 fprintf(stderr, BAD_MAGIC_MSG);
344 if ((sblock.fs_magic == FS_UFS1_MAGIC ||
345 (sblock.fs_magic == FS_UFS2_MAGIC &&
346 sblock.fs_sblockloc == sblock_try[i])) &&
347 sblock.fs_ncg >= 1 &&
348 sblock.fs_bsize >= MINBSIZE &&
349 sblock.fs_sbsize >= roundup(sizeof(struct fs), dev_bsize))
352 if (sblock_try[i] == -1) {
353 fprintf(stderr, "Cannot find file system superblock\n");
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.
363 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
364 sblk.b_bno = super / dev_bsize;
365 sblk.b_size = SBLOCKSIZE;
369 * Compare all fields that should not differ in alternate super block.
370 * When an alternate super-block is specified this check is skipped.
372 getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
375 if (altsblock.fs_sblkno != sblock.fs_sblkno ||
376 altsblock.fs_cblkno != sblock.fs_cblkno ||
377 altsblock.fs_iblkno != sblock.fs_iblkno ||
378 altsblock.fs_dblkno != sblock.fs_dblkno ||
379 altsblock.fs_ncg != sblock.fs_ncg ||
380 altsblock.fs_bsize != sblock.fs_bsize ||
381 altsblock.fs_fsize != sblock.fs_fsize ||
382 altsblock.fs_frag != sblock.fs_frag ||
383 altsblock.fs_bmask != sblock.fs_bmask ||
384 altsblock.fs_fmask != sblock.fs_fmask ||
385 altsblock.fs_bshift != sblock.fs_bshift ||
386 altsblock.fs_fshift != sblock.fs_fshift ||
387 altsblock.fs_fragshift != sblock.fs_fragshift ||
388 altsblock.fs_fsbtodb != sblock.fs_fsbtodb ||
389 altsblock.fs_sbsize != sblock.fs_sbsize ||
390 altsblock.fs_nindir != sblock.fs_nindir ||
391 altsblock.fs_inopb != sblock.fs_inopb ||
392 altsblock.fs_cssize != sblock.fs_cssize ||
393 altsblock.fs_ipg != sblock.fs_ipg ||
394 altsblock.fs_fpg != sblock.fs_fpg ||
395 altsblock.fs_magic != sblock.fs_magic) {
397 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
402 * If not yet done, update UFS1 superblock with new wider fields.
404 if (sblock.fs_magic == FS_UFS1_MAGIC &&
405 sblock.fs_maxbsize != sblock.fs_bsize) {
406 sblock.fs_maxbsize = sblock.fs_bsize;
407 sblock.fs_time = sblock.fs_old_time;
408 sblock.fs_size = sblock.fs_old_size;
409 sblock.fs_dsize = sblock.fs_old_dsize;
410 sblock.fs_csaddr = sblock.fs_old_csaddr;
411 sblock.fs_cstotal.cs_ndir = sblock.fs_old_cstotal.cs_ndir;
412 sblock.fs_cstotal.cs_nbfree = sblock.fs_old_cstotal.cs_nbfree;
413 sblock.fs_cstotal.cs_nifree = sblock.fs_old_cstotal.cs_nifree;
414 sblock.fs_cstotal.cs_nffree = sblock.fs_old_cstotal.cs_nffree;
421 badsb(int listerr, const char *s)
427 printf("%s: ", cdevname);
428 pfatal("BAD SUPER BLOCK: %s\n", s);
438 initbarea(&sblk, BT_SUPERBLK);
439 initbarea(&asblk, BT_SUPERBLK);
440 sblk.b_un.b_buf = Malloc(SBLOCKSIZE);
441 asblk.b_un.b_buf = Malloc(SBLOCKSIZE);
442 if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
443 errx(EEXIT, "cannot allocate space for superblock");
444 dev_bsize = secsize = DEV_BSIZE;