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 static int sbhashfailed;
64 #define POWEROF2(num) (((num) & ((num) - 1)) == 0)
66 static int calcsb(char *dev, int devfd, struct fs *fs);
67 static void saverecovery(int readfd, int writefd);
68 static int chkrecovery(int devfd);
71 * Read in a superblock finding an alternate if necessary.
72 * Return 1 if successful, 0 if unsuccessful, -1 if file system
73 * is already clean (ckclean and preen mode only).
81 * We are expected to have an open file descriptor and a superblock.
83 if (fsreadfd < 0 || havesb == 0) {
85 printf("setup: bad fsreadfd or missing superblock\n");
91 (nflag || (fswritefd = open(dev, O_WRONLY)) < 0)) {
94 pfatal("NO WRITE ACCESS");
95 printf(" (NO WRITE)");
99 if (sbhashfailed != 0) {
100 pwarn("SUPERBLOCK CHECK HASH FAILED");
102 pwarn("OPENED READONLY SO CANNOT CORRECT CHECK HASH\n");
103 else if (preen || reply("CORRECT CHECK HASH") != 0) {
105 printf(" (CORRECTED)\n");
110 if (skipclean && ckclean && sblock.fs_clean) {
111 pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
114 maxfsblock = sblock.fs_size;
115 maxino = sblock.fs_ncg * sblock.fs_ipg;
117 * Check and potentially fix certain fields in the super block.
119 if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
120 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
121 if (reply("SET TO DEFAULT") == 1) {
122 sblock.fs_optim = FS_OPTTIME;
126 if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
127 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
129 if (reply("SET TO DEFAULT") == 1) {
130 sblock.fs_minfree = 10;
134 if (sblock.fs_magic == FS_UFS1_MAGIC &&
135 sblock.fs_old_inodefmt < FS_44INODEFMT) {
136 pwarn("Format of file system is too old.\n");
137 pwarn("Must update to modern format using a version of fsck\n");
138 pfatal("from before 2002 with the command ``fsck -c 2''\n");
141 if (preen == 0 && yflag == 0 && sblock.fs_magic == FS_UFS2_MAGIC &&
142 fswritefd != -1 && chkrecovery(fsreadfd) == 0 &&
143 reply("SAVE DATA TO FIND ALTERNATE SUPERBLOCKS") != 0)
144 saverecovery(fsreadfd, fswritefd);
146 * allocate and initialize the necessary maps
148 bmapsize = roundup(howmany(maxfsblock, CHAR_BIT), sizeof(short));
149 blockmap = Calloc((unsigned)bmapsize, sizeof (char));
150 if (blockmap == NULL) {
151 printf("cannot alloc %u bytes for blockmap\n",
155 inostathead = Calloc(sblock.fs_ncg, sizeof(struct inostatlist));
156 if (inostathead == NULL) {
157 printf("cannot alloc %u bytes for inostathead\n",
158 (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg)));
161 numdirs = MAX(sblock.fs_cstotal.cs_ndir, 128);
164 listmax = numdirs + 10;
165 inpsort = (struct inoinfo **)Calloc(listmax, sizeof(struct inoinfo *));
166 inphead = (struct inoinfo **)Calloc(numdirs, sizeof(struct inoinfo *));
167 if (inpsort == NULL || inphead == NULL) {
168 printf("cannot alloc %ju bytes for inphead\n",
169 (uintmax_t)numdirs * sizeof(struct inoinfo *));
173 if (sblock.fs_flags & FS_DOSOFTDEP)
185 * Open a device or file to be checked by fsck.
188 openfilesys(char *dev)
193 if (stat(dev, &statb) < 0)
195 if ((statb.st_mode & S_IFMT) != S_IFCHR &&
196 (statb.st_mode & S_IFMT) != S_IFBLK) {
197 if (bkgrdflag != 0 && (statb.st_flags & SF_SNAPSHOT) == 0) {
198 pfatal("BACKGROUND FSCK LACKS A SNAPSHOT\n");
201 if (bkgrdflag != 0) {
202 cursnapshot = statb.st_ino;
204 pfatal("%s IS NOT A DISK DEVICE\n", dev);
205 if (reply("CONTINUE") == 0)
209 saved_fsreadfd = fsreadfd;
210 if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
211 fsreadfd = saved_fsreadfd;
214 if (saved_fsreadfd != -1)
215 close(saved_fsreadfd);
220 * Read in the super block and its summary info.
228 readcnt[sblk.b_type]++;
230 * If bflag is given, then check just that superblock.
233 switch (sbget(fsreadfd, &fs, bflag * dev_bsize, UFS_NOMSG)) {
237 printf("Check hash failed for superblock at %jd\n",
241 printf("%jd is not a file system superblock\n", bflag);
245 printf("I/O error reading %jd\n", bflag);
250 * Check for the standard superblock and use it if good.
252 if (sbget(fsreadfd, &fs, UFS_STDSB, UFS_NOMSG) == 0)
255 * Check if the only problem is a check-hash failure.
258 if (sbget(fsreadfd, &fs, UFS_STDSB, UFS_NOMSG | UFS_NOHASHFAIL) == 0) {
263 * Do an exhaustive search for a usable superblock.
265 switch (sbsearch(fsreadfd, &fs, 0)) {
269 printf("SEARCH FOR ALTERNATE SUPER-BLOCK FAILED. "
270 "YOU MUST USE THE\n-b OPTION TO FSCK TO SPECIFY "
271 "THE LOCATION OF AN ALTERNATE\nSUPER-BLOCK TO "
272 "SUPPLY NEEDED INFORMATION; SEE fsck_ffs(8).\n");
276 printf("I/O error reading a usable superblock\n");
281 memcpy(&sblock, fs, fs->fs_sbsize);
284 * Compute block size that the file system is based on,
285 * according to fsbtodb, and adjust superblock block number
286 * so we can tell if this is an alternate later.
288 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
289 sblk.b_bno = sblock.fs_sblockactualloc / dev_bsize;
290 sblk.b_size = SBLOCKSIZE;
292 * If not yet done, update UFS1 superblock with new wider fields.
294 if (sblock.fs_magic == FS_UFS1_MAGIC &&
295 sblock.fs_maxbsize != sblock.fs_bsize) {
296 sblock.fs_maxbsize = sblock.fs_bsize;
297 sblock.fs_time = sblock.fs_old_time;
298 sblock.fs_size = sblock.fs_old_size;
299 sblock.fs_dsize = sblock.fs_old_dsize;
300 sblock.fs_csaddr = sblock.fs_old_csaddr;
301 sblock.fs_cstotal.cs_ndir = sblock.fs_old_cstotal.cs_ndir;
302 sblock.fs_cstotal.cs_nbfree = sblock.fs_old_cstotal.cs_nbfree;
303 sblock.fs_cstotal.cs_nifree = sblock.fs_old_cstotal.cs_nifree;
304 sblock.fs_cstotal.cs_nffree = sblock.fs_old_cstotal.cs_nffree;
318 initbarea(&sblk, BT_SUPERBLK);
319 sblk.b_un.b_buf = Malloc(SBLOCKSIZE);
320 if (sblk.b_un.b_buf == NULL)
321 errx(EEXIT, "cannot allocate space for superblock");
322 dev_bsize = secsize = DEV_BSIZE;
326 * Calculate a prototype superblock based on information in the boot area.
327 * When done the cgsblock macro can be calculated and the fs_ncg field
328 * can be used. Do NOT attempt to use other macros without verifying that
329 * their needed information is available!
332 calcsb(char *dev, int devfd, struct fs *fs)
334 struct fsrecovery *fsr;
339 * We need fragments-per-group and the partition-size.
341 * Newfs stores these details at the end of the boot block area
342 * at the start of the filesystem partition. If they have been
343 * overwritten by a boot block, we fail. But usually they are
344 * there and we can use them.
346 if (ioctl(devfd, DIOCGSECTORSIZE, &secsize) == -1)
348 fsrbuf = Malloc(secsize);
350 errx(EEXIT, "calcsb: cannot allocate recovery buffer");
351 if (blread(devfd, fsrbuf,
352 (SBLOCK_UFS2 - secsize) / dev_bsize, secsize) != 0) {
356 fsr = (struct fsrecovery *)&fsrbuf[secsize - sizeof *fsr];
357 if (fsr->fsr_magic != FS_UFS2_MAGIC) {
361 memset(fs, 0, sizeof(struct fs));
362 fs->fs_fpg = fsr->fsr_fpg;
363 fs->fs_fsbtodb = fsr->fsr_fsbtodb;
364 fs->fs_sblkno = fsr->fsr_sblkno;
365 fs->fs_magic = fsr->fsr_magic;
366 fs->fs_ncg = fsr->fsr_ncg;
372 * Check to see if recovery information exists.
373 * Return 1 if it exists or cannot be created.
374 * Return 0 if it does not exist and can be created.
377 chkrecovery(int devfd)
379 struct fsrecovery *fsr;
381 u_int secsize, rdsize;
384 * Could not determine if backup material exists, so do not
385 * offer to create it.
388 rdsize = sblock.fs_fsize;
389 if (ioctl(devfd, DIOCGSECTORSIZE, &secsize) == -1 ||
390 rdsize % secsize != 0 ||
391 (fsrbuf = Malloc(rdsize)) == NULL ||
392 blread(devfd, fsrbuf, (SBLOCK_UFS2 - rdsize) / dev_bsize,
398 * Recovery material has already been created, so do not
399 * need to create it again.
401 fsr = (struct fsrecovery *)&fsrbuf[rdsize - sizeof *fsr];
402 if (fsr->fsr_magic == FS_UFS2_MAGIC) {
407 * Recovery material has not been created and can be if desired.
414 * Read the last filesystem-size piece of the boot block, replace the
415 * last 20 bytes with the recovery information, then write it back.
416 * The recovery information only works for UFS2 filesystems.
419 saverecovery(int readfd, int writefd)
421 struct fsrecovery *fsr;
423 u_int secsize, rdsize;
426 rdsize = sblock.fs_fsize;
427 if (sblock.fs_magic != FS_UFS2_MAGIC ||
428 ioctl(readfd, DIOCGSECTORSIZE, &secsize) == -1 ||
429 rdsize % secsize != 0 ||
430 (fsrbuf = Malloc(rdsize)) == NULL ||
431 blread(readfd, fsrbuf, (SBLOCK_UFS2 - rdsize) / dev_bsize,
433 printf("RECOVERY DATA COULD NOT BE CREATED\n");
437 fsr = (struct fsrecovery *)&fsrbuf[rdsize - sizeof *fsr];
438 fsr->fsr_magic = sblock.fs_magic;
439 fsr->fsr_fpg = sblock.fs_fpg;
440 fsr->fsr_fsbtodb = sblock.fs_fsbtodb;
441 fsr->fsr_sblkno = sblock.fs_sblkno;
442 fsr->fsr_ncg = sblock.fs_ncg;
443 blwrite(writefd, fsrbuf, (SBLOCK_UFS2 - rdsize) / dev_bsize, rdsize);