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 inohash *inphash; /* hash list of directory inode info */
62 struct inoinfo **inpsort; /* disk order list of directory inodes */
63 struct inode snaplist[FSMAXSNAP + 1]; /* list of active snapshots */
64 int snapcnt; /* number of active snapshots */
65 char *copybuf; /* buffer to copy snapshot blocks */
67 static int sbhashfailed;
68 #define POWEROF2(num) (((num) & ((num) - 1)) == 0)
70 static int calcsb(char *dev, int devfd, struct fs *fs);
71 static void saverecovery(int readfd, int writefd);
72 static int chkrecovery(int devfd);
73 static int getlbnblkno(struct inodesc *);
74 static int checksnapinfo(struct inode *);
77 * Read in a superblock finding an alternate if necessary.
78 * Return 1 if successful, 0 if unsuccessful, -1 if file system
79 * is already clean (ckclean and preen mode only).
88 * We are expected to have an open file descriptor and a superblock.
90 if (fsreadfd < 0 || havesb == 0) {
92 printf("setup: bad fsreadfd or missing superblock\n");
98 (nflag || (fswritefd = open(dev, O_WRONLY)) < 0)) {
101 pfatal("NO WRITE ACCESS");
102 printf(" (NO WRITE)");
106 if (sbhashfailed != 0) {
107 pwarn("SUPERBLOCK CHECK HASH FAILED");
109 pwarn("OPENED READONLY SO CANNOT CORRECT CHECK HASH\n");
110 else if (preen || reply("CORRECT CHECK HASH") != 0) {
112 printf(" (CORRECTED)\n");
117 if (skipclean && ckclean && sblock.fs_clean) {
118 pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
121 maxfsblock = sblock.fs_size;
122 maxino = sblock.fs_ncg * sblock.fs_ipg;
124 * Check and potentially fix certain fields in the super block.
126 if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
127 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
128 if (reply("SET TO DEFAULT") == 1) {
129 sblock.fs_optim = FS_OPTTIME;
133 if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
134 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
136 if (reply("SET TO DEFAULT") == 1) {
137 sblock.fs_minfree = 10;
141 if (sblock.fs_magic == FS_UFS1_MAGIC &&
142 sblock.fs_old_inodefmt < FS_44INODEFMT) {
143 pwarn("Format of file system is too old.\n");
144 pwarn("Must update to modern format using a version of fsck\n");
145 pfatal("from before 2002 with the command ``fsck -c 2''\n");
148 if (preen == 0 && yflag == 0 && sblock.fs_magic == FS_UFS2_MAGIC &&
149 fswritefd != -1 && chkrecovery(fsreadfd) == 0 &&
150 reply("SAVE DATA TO FIND ALTERNATE SUPERBLOCKS") != 0)
151 saverecovery(fsreadfd, fswritefd);
153 * allocate and initialize the necessary maps
156 bmapsize = roundup(howmany(maxfsblock, CHAR_BIT), sizeof(short));
157 blockmap = Calloc((unsigned)bmapsize, sizeof (char));
158 if (blockmap == NULL) {
159 printf("cannot alloc %u bytes for blockmap\n",
163 inostathead = Calloc(sblock.fs_ncg, sizeof(struct inostatlist));
164 if (inostathead == NULL) {
165 printf("cannot alloc %u bytes for inostathead\n",
166 (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg)));
169 numdirs = sblock.fs_cstotal.cs_ndir;
170 dirhash = MAX(numdirs / 2, 1);
172 listmax = numdirs + 10;
173 inpsort = (struct inoinfo **)Calloc(listmax, sizeof(struct inoinfo *));
174 inphash = (struct inohash *)Calloc(dirhash, sizeof(struct inohash));
175 if (inpsort == NULL || inphash == NULL) {
176 printf("cannot alloc %ju bytes for inphash\n",
177 (uintmax_t)numdirs * sizeof(struct inoinfo *));
180 if (sblock.fs_flags & FS_DOSOFTDEP)
185 * Collect any snapshot inodes so that we can allow them to
186 * claim any blocks that we free. The code for doing this is
187 * imported here and into inode.c from sys/ufs/ffs/ffs_snapshot.c.
189 for (snapcnt = 0; snapcnt < FSMAXSNAP; snapcnt++) {
190 if (sblock.fs_snapinum[snapcnt] == 0)
192 ginode(sblock.fs_snapinum[snapcnt], &ip);
193 if ((DIP(ip.i_dp, di_mode) & IFMT) == IFREG &&
194 (DIP(ip.i_dp, di_flags) & SF_SNAPSHOT) != 0 &&
195 checksnapinfo(&ip)) {
197 printf("Load snapshot %jd\n",
198 (intmax_t)sblock.fs_snapinum[snapcnt]);
199 snaplist[snapcnt] = ip;
202 printf("Removing non-snapshot inode %ju from snapshot list\n",
203 (uintmax_t)sblock.fs_snapinum[snapcnt]);
205 for (i = snapcnt + 1; i < FSMAXSNAP; i++) {
206 if (sblock.fs_snapinum[i] == 0)
208 sblock.fs_snapinum[i - 1] = sblock.fs_snapinum[i];
210 sblock.fs_snapinum[i - 1] = 0;
214 if (snapcnt > 0 && copybuf == NULL) {
215 copybuf = Malloc(sblock.fs_bsize);
217 errx(EEXIT, "cannot allocate space for snapshot "
228 * Check for valid snapshot information.
230 * Each snapshot has a list of blocks that have been copied. This list
231 * is consulted before checking the snapshot inode. Its purpose is to
232 * speed checking of commonly checked blocks and to avoid recursive
233 * checks of the snapshot inode. In particular, the list must contain
234 * the superblock, the superblock summary information, and all the
235 * cylinder group blocks. The list may contain other commonly checked
236 * pointers such as those of the blocks that contain the snapshot inodes.
237 * The list is sorted into block order to allow binary search lookup.
239 * The twelve direct direct block pointers of the snapshot are always
240 * copied, so we test for them first before checking the list itself
241 * (i.e., they are not in the list).
243 * The checksnapinfo() routine needs to ensure that the list contains at
244 * least the super block, its summary information, and the cylinder groups.
245 * Here we check the list first for the superblock, zero or more cylinder
246 * groups up to the location of the superblock summary information, the
247 * summary group information, and any remaining cylinder group maps that
248 * follow it. We skip over any other entries in the list.
250 #define CHKBLKINLIST(chkblk) \
251 /* All UFS_NDADDR blocks are copied */ \
252 if ((chkblk) >= UFS_NDADDR) { \
253 /* Skip over blocks that are not of interest */ \
254 while (*blkp < (chkblk) && blkp < lastblkp) \
256 /* Fail if end of list and not all blocks found */ \
257 if (blkp >= lastblkp) { \
258 pwarn("UFS%d snapshot inode %jd failed: " \
259 "improper block list length (%jd)\n", \
260 sblock.fs_magic == FS_UFS1_MAGIC ? 1 : 2, \
261 (intmax_t)snapip->i_number, \
262 (intmax_t)(lastblkp - &snapblklist[0])); \
265 /* Fail if block we seek is missing */ \
266 else if (*blkp++ != (chkblk)) { \
267 pwarn("UFS%d snapshot inode %jd failed: " \
268 "block list (%jd) != %s (%jd)\n", \
269 sblock.fs_magic == FS_UFS1_MAGIC ? 1 : 2, \
270 (intmax_t)snapip->i_number, \
271 (intmax_t)blkp[-1], #chkblk, \
278 checksnapinfo(struct inode *snapip)
282 struct inodesc idesc;
283 daddr_t *snapblklist, *blkp, *lastblkp, csblkno;
284 int cg, loc, len, status;
289 memset(&idesc, 0, sizeof(struct inodesc));
290 idesc.id_type = ADDR;
291 idesc.id_func = getlbnblkno;
292 idesc.id_number = snapip->i_number;
293 lbn = howmany(fs->fs_size, fs->fs_frag);
294 idesc.id_parent = lbn; /* sought after blkno */
295 if ((ckinode(snapip->i_dp, &idesc) & FOUND) == 0)
297 size = fragroundup(fs,
298 DIP(snapip->i_dp, di_size) - lblktosize(fs, lbn));
299 bp = getdatablk(idesc.id_parent, size, BT_DATA);
300 snapblklist = (daddr_t *)bp->b_un.b_buf;
302 * snapblklist[0] is the size of the list
303 * snapblklist[1] is the first element of the list
305 * We need to be careful to bound the size of the list and verify
306 * that we have not run off the end of it if it or its size has
309 blkp = &snapblklist[1];
310 lastblkp = &snapblklist[MAX(0,
311 MIN(snapblklist[0] + 1, size / sizeof(daddr_t)))];
313 /* Check that the superblock is listed. */
314 CHKBLKINLIST(lblkno(fs, fs->fs_sblockloc));
318 * Calculate where the summary information is located.
319 * Usually it is in the first cylinder group, but growfs
320 * may move it to the first cylinder group that it adds.
322 * Check all cylinder groups up to the summary information.
324 csblkno = fragstoblks(fs, fs->fs_csaddr);
325 for (cg = 0; cg < fs->fs_ncg; cg++) {
326 if (fragstoblks(fs, cgtod(fs, cg)) > csblkno)
328 CHKBLKINLIST(fragstoblks(fs, cgtod(fs, cg)));
332 /* Check the summary information block(s). */
333 len = howmany(fs->fs_cssize, fs->fs_bsize);
334 for (loc = 0; loc < len; loc++) {
335 CHKBLKINLIST(csblkno + loc);
339 /* Check the remaining cylinder groups. */
340 for (; cg < fs->fs_ncg; cg++) {
341 CHKBLKINLIST(fragstoblks(fs, cgtod(fs, cg)));
351 * Return the block number associated with a specified inode lbn.
352 * Requested lbn is in id_parent. If found, block is returned in
356 getlbnblkno(struct inodesc *idesc)
359 if (idesc->id_lbn < idesc->id_parent)
361 idesc->id_parent = idesc->id_blkno;
362 return (STOP | FOUND);
366 * Open a device or file to be checked by fsck.
369 openfilesys(char *dev)
374 if (stat(dev, &statb) < 0)
376 if ((statb.st_mode & S_IFMT) != S_IFCHR &&
377 (statb.st_mode & S_IFMT) != S_IFBLK) {
378 if (bkgrdflag != 0 && (statb.st_flags & SF_SNAPSHOT) == 0) {
379 pfatal("BACKGROUND FSCK LACKS A SNAPSHOT\n");
382 if (bkgrdflag != 0) {
383 cursnapshot = statb.st_ino;
385 pfatal("%s IS NOT A DISK DEVICE\n", dev);
386 if (reply("CONTINUE") == 0)
390 saved_fsreadfd = fsreadfd;
391 if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
392 fsreadfd = saved_fsreadfd;
395 if (saved_fsreadfd != -1)
396 close(saved_fsreadfd);
401 * Read in the super block and its summary info.
409 readcnt[sblk.b_type]++;
411 * If bflag is given, then check just that superblock.
414 switch (sbget(fsreadfd, &fs, bflag * dev_bsize, 0)) {
418 printf("Check hash failed for superblock at %jd\n",
422 printf("%jd is not a file system superblock\n", bflag);
426 printf("I/O error reading %jd\n", bflag);
431 * Check for the standard superblock and use it if good.
433 if (sbget(fsreadfd, &fs, UFS_STDSB, UFS_NOMSG) == 0)
436 * Check if the only problem is a check-hash failure.
439 if (sbget(fsreadfd, &fs, UFS_STDSB, UFS_NOMSG | UFS_NOHASHFAIL) == 0) {
444 * Do an exhaustive search for a usable superblock.
446 switch (sbsearch(fsreadfd, &fs, 0)) {
450 printf("SEARCH FOR ALTERNATE SUPER-BLOCK FAILED. "
451 "YOU MUST USE THE\n-b OPTION TO FSCK TO SPECIFY "
452 "THE LOCATION OF AN ALTERNATE\nSUPER-BLOCK TO "
453 "SUPPLY NEEDED INFORMATION; SEE fsck_ffs(8).\n");
457 printf("I/O error reading a usable superblock\n");
462 memcpy(&sblock, fs, fs->fs_sbsize);
465 * Compute block size that the file system is based on,
466 * according to fsbtodb, and adjust superblock block number
467 * so we can tell if this is an alternate later.
469 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
470 sblk.b_bno = sblock.fs_sblockactualloc / dev_bsize;
471 sblk.b_size = SBLOCKSIZE;
473 * If not yet done, update UFS1 superblock with new wider fields.
475 if (sblock.fs_magic == FS_UFS1_MAGIC &&
476 sblock.fs_maxbsize != sblock.fs_bsize) {
477 sblock.fs_maxbsize = sblock.fs_bsize;
478 sblock.fs_time = sblock.fs_old_time;
479 sblock.fs_size = sblock.fs_old_size;
480 sblock.fs_dsize = sblock.fs_old_dsize;
481 sblock.fs_csaddr = sblock.fs_old_csaddr;
482 sblock.fs_cstotal.cs_ndir = sblock.fs_old_cstotal.cs_ndir;
483 sblock.fs_cstotal.cs_nbfree = sblock.fs_old_cstotal.cs_nbfree;
484 sblock.fs_cstotal.cs_nifree = sblock.fs_old_cstotal.cs_nifree;
485 sblock.fs_cstotal.cs_nffree = sblock.fs_old_cstotal.cs_nffree;
499 initbarea(&sblk, BT_SUPERBLK);
500 sblk.b_un.b_buf = Malloc(SBLOCKSIZE);
501 if (sblk.b_un.b_buf == NULL)
502 errx(EEXIT, "cannot allocate space for superblock");
503 dev_bsize = secsize = DEV_BSIZE;
507 * Calculate a prototype superblock based on information in the boot area.
508 * When done the cgsblock macro can be calculated and the fs_ncg field
509 * can be used. Do NOT attempt to use other macros without verifying that
510 * their needed information is available!
513 calcsb(char *dev, int devfd, struct fs *fs)
515 struct fsrecovery *fsr;
520 * We need fragments-per-group and the partition-size.
522 * Newfs stores these details at the end of the boot block area
523 * at the start of the filesystem partition. If they have been
524 * overwritten by a boot block, we fail. But usually they are
525 * there and we can use them.
527 if (ioctl(devfd, DIOCGSECTORSIZE, &secsize) == -1)
529 fsrbuf = Malloc(secsize);
531 errx(EEXIT, "calcsb: cannot allocate recovery buffer");
532 if (blread(devfd, fsrbuf,
533 (SBLOCK_UFS2 - secsize) / dev_bsize, secsize) != 0) {
537 fsr = (struct fsrecovery *)&fsrbuf[secsize - sizeof *fsr];
538 if (fsr->fsr_magic != FS_UFS2_MAGIC) {
542 memset(fs, 0, sizeof(struct fs));
543 fs->fs_fpg = fsr->fsr_fpg;
544 fs->fs_fsbtodb = fsr->fsr_fsbtodb;
545 fs->fs_sblkno = fsr->fsr_sblkno;
546 fs->fs_magic = fsr->fsr_magic;
547 fs->fs_ncg = fsr->fsr_ncg;
553 * Check to see if recovery information exists.
554 * Return 1 if it exists or cannot be created.
555 * Return 0 if it does not exist and can be created.
558 chkrecovery(int devfd)
560 struct fsrecovery *fsr;
562 u_int secsize, rdsize;
565 * Could not determine if backup material exists, so do not
566 * offer to create it.
569 rdsize = sblock.fs_fsize;
570 if (ioctl(devfd, DIOCGSECTORSIZE, &secsize) == -1 ||
571 rdsize % secsize != 0 ||
572 (fsrbuf = Malloc(rdsize)) == NULL ||
573 blread(devfd, fsrbuf, (SBLOCK_UFS2 - rdsize) / dev_bsize,
579 * Recovery material has already been created, so do not
580 * need to create it again.
582 fsr = (struct fsrecovery *)&fsrbuf[rdsize - sizeof *fsr];
583 if (fsr->fsr_magic == FS_UFS2_MAGIC) {
588 * Recovery material has not been created and can be if desired.
595 * Read the last filesystem-size piece of the boot block, replace the
596 * last 20 bytes with the recovery information, then write it back.
597 * The recovery information only works for UFS2 filesystems.
600 saverecovery(int readfd, int writefd)
602 struct fsrecovery *fsr;
604 u_int secsize, rdsize;
607 rdsize = sblock.fs_fsize;
608 if (sblock.fs_magic != FS_UFS2_MAGIC ||
609 ioctl(readfd, DIOCGSECTORSIZE, &secsize) == -1 ||
610 rdsize % secsize != 0 ||
611 (fsrbuf = Malloc(rdsize)) == NULL ||
612 blread(readfd, fsrbuf, (SBLOCK_UFS2 - rdsize) / dev_bsize,
614 printf("RECOVERY DATA COULD NOT BE CREATED\n");
618 fsr = (struct fsrecovery *)&fsrbuf[rdsize - sizeof *fsr];
619 fsr->fsr_magic = sblock.fs_magic;
620 fsr->fsr_fpg = sblock.fs_fpg;
621 fsr->fsr_fsbtodb = sblock.fs_fsbtodb;
622 fsr->fsr_sblkno = sblock.fs_sblkno;
623 fsr->fsr_ncg = sblock.fs_ncg;
624 blwrite(writefd, fsrbuf, (SBLOCK_UFS2 - rdsize) / dev_bsize, rdsize);