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 */
63 #define POWEROF2(num) (((num) & ((num) - 1)) == 0)
65 static int calcsb(char *dev, int devfd, struct fs *fs);
66 static void saverecovery(int readfd, int writefd);
67 static int chkrecovery(int devfd);
70 * Read in a superblock finding an alternate if necessary.
71 * Return 1 if successful, 0 if unsuccessful, -1 if file system
72 * is already clean (ckclean and preen mode only).
81 * We are expected to have an open file descriptor
86 * If we do not yet have a superblock, read it in looking
87 * for alternates if necessary.
89 if (havesb == 0 && readsb(1) == 0) {
91 if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
93 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
95 for (cg = 0; cg < proto.fs_ncg; cg++) {
96 bflag = fsbtodb(&proto, cgsblock(&proto, cg));
100 if (cg >= proto.fs_ncg) {
101 printf("SEARCH FOR ALTERNATE SUPER-BLOCK FAILED. "
102 "YOU MUST USE THE\n-b OPTION TO FSCK TO SPECIFY "
103 "THE LOCATION OF AN ALTERNATE\nSUPER-BLOCK TO "
104 "SUPPLY NEEDED INFORMATION; SEE fsck_ffs(8).\n");
108 pwarn("USING ALTERNATE SUPERBLOCK AT %jd\n", bflag);
112 printf("** %s", dev);
113 if (bkgrdflag == 0 &&
114 (nflag || (fswritefd = open(dev, O_WRONLY)) < 0)) {
117 pfatal("NO WRITE ACCESS");
118 printf(" (NO WRITE)");
122 if (sbhashfailed != 0) {
123 pwarn("SUPERBLOCK CHECK HASH FAILED");
125 pwarn("OPENED READONLY SO CANNOT CORRECT CHECK HASH\n");
126 else if (preen || reply("CORRECT CHECK HASH") != 0) {
128 printf(" (CORRECTED)\n");
133 if (skipclean && ckclean && sblock.fs_clean) {
134 pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
137 maxfsblock = sblock.fs_size;
138 maxino = sblock.fs_ncg * sblock.fs_ipg;
140 * Check and potentially fix certain fields in the super block.
142 if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
143 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
144 if (reply("SET TO DEFAULT") == 1) {
145 sblock.fs_optim = FS_OPTTIME;
149 if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
150 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
152 if (reply("SET TO DEFAULT") == 1) {
153 sblock.fs_minfree = 10;
157 if (sblock.fs_magic == FS_UFS1_MAGIC &&
158 sblock.fs_old_inodefmt < FS_44INODEFMT) {
159 pwarn("Format of file system is too old.\n");
160 pwarn("Must update to modern format using a version of fsck\n");
161 pfatal("from before 2002 with the command ``fsck -c 2''\n");
164 if (preen == 0 && yflag == 0 && sblock.fs_magic == FS_UFS2_MAGIC &&
165 fswritefd != -1 && chkrecovery(fsreadfd) == 0 &&
166 reply("SAVE DATA TO FIND ALTERNATE SUPERBLOCKS") != 0)
167 saverecovery(fsreadfd, fswritefd);
169 * allocate and initialize the necessary maps
171 bmapsize = roundup(howmany(maxfsblock, CHAR_BIT), sizeof(short));
172 blockmap = Calloc((unsigned)bmapsize, sizeof (char));
173 if (blockmap == NULL) {
174 printf("cannot alloc %u bytes for blockmap\n",
178 inostathead = Calloc(sblock.fs_ncg, sizeof(struct inostatlist));
179 if (inostathead == NULL) {
180 printf("cannot alloc %u bytes for inostathead\n",
181 (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg)));
184 numdirs = MAX(sblock.fs_cstotal.cs_ndir, 128);
187 listmax = numdirs + 10;
188 inpsort = (struct inoinfo **)Calloc(listmax, sizeof(struct inoinfo *));
189 inphead = (struct inoinfo **)Calloc(numdirs, sizeof(struct inoinfo *));
190 if (inpsort == NULL || inphead == NULL) {
191 printf("cannot alloc %ju bytes for inphead\n",
192 (uintmax_t)numdirs * sizeof(struct inoinfo *));
196 if (sblock.fs_flags & FS_DOSOFTDEP)
208 * Open a device or file to be checked by fsck.
211 openfilesys(char *dev)
216 if (stat(dev, &statb) < 0)
218 if ((statb.st_mode & S_IFMT) != S_IFCHR &&
219 (statb.st_mode & S_IFMT) != S_IFBLK) {
220 if (bkgrdflag != 0 && (statb.st_flags & SF_SNAPSHOT) == 0) {
221 pfatal("BACKGROUND FSCK LACKS A SNAPSHOT\n");
224 if (bkgrdflag != 0) {
225 cursnapshot = statb.st_ino;
227 pfatal("%s IS NOT A DISK DEVICE\n", dev);
228 if (reply("CONTINUE") == 0)
232 saved_fsreadfd = fsreadfd;
233 if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
234 fsreadfd = saved_fsreadfd;
237 if (saved_fsreadfd != -1)
238 close(saved_fsreadfd);
243 * Read in the super block and its summary info.
252 super = bflag ? bflag * dev_bsize : UFS_STDSB;
253 flags = sbhashfailed ? UFS_NOHASHFAIL | UFS_NOMSG : UFS_NOMSG;
254 readcnt[sblk.b_type]++;
255 while ((ret = sbget(fsreadfd, &fs, super, flags)) != 0) {
258 if (bflag || (super == UFS_STDSB &&
259 flags == (UFS_NOHASHFAIL | UFS_NOMSG)))
262 flags = UFS_NOHASHFAIL | UFS_NOMSG;
267 printf("%jd is not a file system "
268 "superblock\n", super / dev_bsize);
270 printf("Cannot find file system "
275 printf("I/O error reading %jd\n",
280 memcpy(&sblock, fs, fs->fs_sbsize);
283 * Compute block size that the file system is based on,
284 * according to fsbtodb, and adjust superblock block number
285 * so we can tell if this is an alternate later.
287 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
288 sblk.b_bno = sblock.fs_sblockactualloc / dev_bsize;
289 sblk.b_size = SBLOCKSIZE;
291 * If not yet done, update UFS1 superblock with new wider fields.
293 if (sblock.fs_magic == FS_UFS1_MAGIC &&
294 sblock.fs_maxbsize != sblock.fs_bsize) {
295 sblock.fs_maxbsize = sblock.fs_bsize;
296 sblock.fs_time = sblock.fs_old_time;
297 sblock.fs_size = sblock.fs_old_size;
298 sblock.fs_dsize = sblock.fs_old_dsize;
299 sblock.fs_csaddr = sblock.fs_old_csaddr;
300 sblock.fs_cstotal.cs_ndir = sblock.fs_old_cstotal.cs_ndir;
301 sblock.fs_cstotal.cs_nbfree = sblock.fs_old_cstotal.cs_nbfree;
302 sblock.fs_cstotal.cs_nifree = sblock.fs_old_cstotal.cs_nifree;
303 sblock.fs_cstotal.cs_nffree = sblock.fs_old_cstotal.cs_nffree;
317 initbarea(&sblk, BT_SUPERBLK);
318 sblk.b_un.b_buf = Malloc(SBLOCKSIZE);
319 if (sblk.b_un.b_buf == NULL)
320 errx(EEXIT, "cannot allocate space for superblock");
321 dev_bsize = secsize = DEV_BSIZE;
325 * Calculate a prototype superblock based on information in the boot area.
326 * When done the cgsblock macro can be calculated and the fs_ncg field
327 * can be used. Do NOT attempt to use other macros without verifying that
328 * their needed information is available!
331 calcsb(char *dev, int devfd, struct fs *fs)
333 struct fsrecovery *fsr;
338 * We need fragments-per-group and the partition-size.
340 * Newfs stores these details at the end of the boot block area
341 * at the start of the filesystem partition. If they have been
342 * overwritten by a boot block, we fail. But usually they are
343 * there and we can use them.
345 if (ioctl(devfd, DIOCGSECTORSIZE, &secsize) == -1)
347 fsrbuf = Malloc(secsize);
349 errx(EEXIT, "calcsb: cannot allocate recovery buffer");
350 if (blread(devfd, fsrbuf,
351 (SBLOCK_UFS2 - secsize) / dev_bsize, secsize) != 0) {
355 fsr = (struct fsrecovery *)&fsrbuf[secsize - sizeof *fsr];
356 if (fsr->fsr_magic != FS_UFS2_MAGIC) {
360 memset(fs, 0, sizeof(struct fs));
361 fs->fs_fpg = fsr->fsr_fpg;
362 fs->fs_fsbtodb = fsr->fsr_fsbtodb;
363 fs->fs_sblkno = fsr->fsr_sblkno;
364 fs->fs_magic = fsr->fsr_magic;
365 fs->fs_ncg = fsr->fsr_ncg;
371 * Check to see if recovery information exists.
372 * Return 1 if it exists or cannot be created.
373 * Return 0 if it does not exist and can be created.
376 chkrecovery(int devfd)
378 struct fsrecovery *fsr;
380 u_int secsize, rdsize;
383 * Could not determine if backup material exists, so do not
384 * offer to create it.
387 rdsize = sblock.fs_fsize;
388 if (ioctl(devfd, DIOCGSECTORSIZE, &secsize) == -1 ||
389 rdsize % secsize != 0 ||
390 (fsrbuf = Malloc(rdsize)) == NULL ||
391 blread(devfd, fsrbuf, (SBLOCK_UFS2 - rdsize) / dev_bsize,
397 * Recovery material has already been created, so do not
398 * need to create it again.
400 fsr = (struct fsrecovery *)&fsrbuf[rdsize - sizeof *fsr];
401 if (fsr->fsr_magic == FS_UFS2_MAGIC) {
406 * Recovery material has not been created and can be if desired.
413 * Read the last filesystem-size piece of the boot block, replace the
414 * last 20 bytes with the recovery information, then write it back.
415 * The recovery information only works for UFS2 filesystems.
418 saverecovery(int readfd, int writefd)
420 struct fsrecovery *fsr;
422 u_int secsize, rdsize;
425 rdsize = sblock.fs_fsize;
426 if (sblock.fs_magic != FS_UFS2_MAGIC ||
427 ioctl(readfd, DIOCGSECTORSIZE, &secsize) == -1 ||
428 rdsize % secsize != 0 ||
429 (fsrbuf = Malloc(rdsize)) == NULL ||
430 blread(readfd, fsrbuf, (SBLOCK_UFS2 - rdsize) / dev_bsize,
432 printf("RECOVERY DATA COULD NOT BE CREATED\n");
436 fsr = (struct fsrecovery *)&fsrbuf[rdsize - sizeof *fsr];
437 fsr->fsr_magic = sblock.fs_magic;
438 fsr->fsr_fpg = sblock.fs_fpg;
439 fsr->fsr_fsbtodb = sblock.fs_fsbtodb;
440 fsr->fsr_sblkno = sblock.fs_sblkno;
441 fsr->fsr_ncg = sblock.fs_ncg;
442 blwrite(writefd, fsrbuf, (SBLOCK_UFS2 - rdsize) / dev_bsize, rdsize);