xref: /freebsd/sbin/fsck_ffs/setup.c (revision 32e86a82f54826f14ea381affa6674db3aa3b5ae)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
48fae3551SRodney W. Grimes  * Copyright (c) 1980, 1986, 1993
58fae3551SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
68fae3551SRodney W. Grimes  *
78fae3551SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
88fae3551SRodney W. Grimes  * modification, are permitted provided that the following conditions
98fae3551SRodney W. Grimes  * are met:
108fae3551SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
118fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
128fae3551SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
138fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
148fae3551SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
168fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
178fae3551SRodney W. Grimes  *    without specific prior written permission.
188fae3551SRodney W. Grimes  *
198fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
208fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
218fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
228fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
238fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
248fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
258fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
268fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
278fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
288fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
298fae3551SRodney W. Grimes  * SUCH DAMAGE.
308fae3551SRodney W. Grimes  */
318fae3551SRodney W. Grimes 
328fae3551SRodney W. Grimes #include <sys/param.h>
33855662c6SKirk McKusick #include <sys/disk.h>
348fae3551SRodney W. Grimes #include <sys/stat.h>
35b35e6950SBosko Milekic #define FSTYPENAMES
368fae3551SRodney W. Grimes #include <sys/disklabel.h>
378fae3551SRodney W. Grimes #include <sys/file.h>
38bf58d635SIan Dowse #include <sys/sysctl.h>
39780a5c1eSPeter Wemm 
40780a5c1eSPeter Wemm #include <ufs/ufs/dinode.h>
41780a5c1eSPeter Wemm #include <ufs/ffs/fs.h>
42780a5c1eSPeter Wemm 
43780a5c1eSPeter Wemm #include <ctype.h>
44780a5c1eSPeter Wemm #include <err.h>
458fae3551SRodney W. Grimes #include <errno.h>
4689fdc4e1SMike Barcroft #include <limits.h>
4784fc0d7eSMaxime Henrion #include <stdint.h>
488fae3551SRodney W. Grimes #include <string.h>
49780a5c1eSPeter Wemm 
508fae3551SRodney W. Grimes #include "fsck.h"
518fae3551SRodney W. Grimes 
5252f97104SKirk McKusick struct inohash *inphash;	       /* hash list of directory inode info */
5352f97104SKirk McKusick struct inoinfo **inpsort;	       /* disk order list of directory inodes */
54460ed610SKirk McKusick struct inode snaplist[FSMAXSNAP + 1];  /* list of active snapshots */
55460ed610SKirk McKusick int snapcnt;			       /* number of active snapshots */
56460ed610SKirk McKusick char *copybuf;			       /* buffer to copy snapshot blocks */
57c3e9752eSKyle Evans 
58e6886616SKirk McKusick static int sbhashfailed;
598fae3551SRodney W. Grimes #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
608fae3551SRodney W. Grimes 
6177b63aa0SKirk McKusick static int calcsb(char *dev, int devfd, struct fs *fs);
6277b63aa0SKirk McKusick static void saverecovery(int readfd, int writefd);
6377b63aa0SKirk McKusick static int chkrecovery(int devfd);
64460ed610SKirk McKusick static int getlbnblkno(struct inodesc *);
65460ed610SKirk McKusick static int checksnapinfo(struct inode *);
6677b63aa0SKirk McKusick 
67780a5c1eSPeter Wemm /*
68780a5c1eSPeter Wemm  * Read in a superblock finding an alternate if necessary.
69780a5c1eSPeter Wemm  * Return 1 if successful, 0 if unsuccessful, -1 if file system
70111a5220SDavid E. O'Brien  * is already clean (ckclean and preen mode only).
71780a5c1eSPeter Wemm  */
7231f4ab50SBruce Evans int
setup(char * dev)73b70cd7eeSWarner Losh setup(char *dev)
748fae3551SRodney W. Grimes {
75460ed610SKirk McKusick 	long i, bmapsize;
76460ed610SKirk McKusick 	struct inode ip;
778fae3551SRodney W. Grimes 
78c0ed8991SXin LI 	/*
79e6886616SKirk McKusick 	 * We are expected to have an open file descriptor and a superblock.
80c0ed8991SXin LI 	 */
81e6886616SKirk McKusick 	if (fsreadfd < 0 || havesb == 0) {
824b08a62eSKirk McKusick 		if (debug) {
834b08a62eSKirk McKusick 			if (fsreadfd < 0)
844b08a62eSKirk McKusick 				printf("setup: missing fsreadfd\n");
854b08a62eSKirk McKusick 			else
864b08a62eSKirk McKusick 				printf("setup: missing superblock\n");
874b08a62eSKirk McKusick 		}
88c0bfa109SKirk McKusick 		return (0);
897578c6abSKirk McKusick 	}
908fae3551SRodney W. Grimes 	if (preen == 0)
918fae3551SRodney W. Grimes 		printf("** %s", dev);
927578c6abSKirk McKusick 	if (bkgrdflag == 0 &&
935cc52631SKirk McKusick 	    (nflag || (fswritefd = open(dev, O_WRONLY)) < 0)) {
948fae3551SRodney W. Grimes 		fswritefd = -1;
958fae3551SRodney W. Grimes 		if (preen)
968fae3551SRodney W. Grimes 			pfatal("NO WRITE ACCESS");
978fae3551SRodney W. Grimes 		printf(" (NO WRITE)");
988fae3551SRodney W. Grimes 	}
998fae3551SRodney W. Grimes 	if (preen == 0)
1008fae3551SRodney W. Grimes 		printf("\n");
101c0bfa109SKirk McKusick 	if (sbhashfailed != 0) {
102c0bfa109SKirk McKusick 		pwarn("SUPERBLOCK CHECK HASH FAILED");
103c0bfa109SKirk McKusick 		if (fswritefd == -1)
104c0bfa109SKirk McKusick 			pwarn("OPENED READONLY SO CANNOT CORRECT CHECK HASH\n");
105c0bfa109SKirk McKusick 		else if (preen || reply("CORRECT CHECK HASH") != 0) {
106c0bfa109SKirk McKusick 			if (preen)
107c0bfa109SKirk McKusick 				printf(" (CORRECTED)\n");
108c0bfa109SKirk McKusick 			sblock.fs_clean = 0;
109c0bfa109SKirk McKusick 			sbdirty();
11077b63aa0SKirk McKusick 		}
1118fae3551SRodney W. Grimes 	}
112111a5220SDavid E. O'Brien 	if (skipclean && ckclean && sblock.fs_clean) {
113d33e92f9SJulian Elischer 		pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
114d33e92f9SJulian Elischer 		return (-1);
115d33e92f9SJulian Elischer 	}
1168fae3551SRodney W. Grimes 	maxfsblock = sblock.fs_size;
1178fae3551SRodney W. Grimes 	maxino = sblock.fs_ncg * sblock.fs_ipg;
1188fae3551SRodney W. Grimes 	/*
1198fae3551SRodney W. Grimes 	 * Check and potentially fix certain fields in the super block.
1208fae3551SRodney W. Grimes 	 */
1218fae3551SRodney W. Grimes 	if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
1228fae3551SRodney W. Grimes 		pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
1238fae3551SRodney W. Grimes 		if (reply("SET TO DEFAULT") == 1) {
1248fae3551SRodney W. Grimes 			sblock.fs_optim = FS_OPTTIME;
1258fae3551SRodney W. Grimes 			sbdirty();
1268fae3551SRodney W. Grimes 		}
1278fae3551SRodney W. Grimes 	}
1288fae3551SRodney W. Grimes 	if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
1298fae3551SRodney W. Grimes 		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
1308fae3551SRodney W. Grimes 			sblock.fs_minfree);
1318fae3551SRodney W. Grimes 		if (reply("SET TO DEFAULT") == 1) {
1328fae3551SRodney W. Grimes 			sblock.fs_minfree = 10;
1338fae3551SRodney W. Grimes 			sbdirty();
1348fae3551SRodney W. Grimes 		}
1358fae3551SRodney W. Grimes 	}
1361c85e6a3SKirk McKusick 	if (sblock.fs_magic == FS_UFS1_MAGIC &&
1371c85e6a3SKirk McKusick 	    sblock.fs_old_inodefmt < FS_44INODEFMT) {
138381ee4c2SPoul-Henning Kamp 		pwarn("Format of file system is too old.\n");
139381ee4c2SPoul-Henning Kamp 		pwarn("Must update to modern format using a version of fsck\n");
140381ee4c2SPoul-Henning Kamp 		pfatal("from before 2002 with the command ``fsck -c 2''\n");
141381ee4c2SPoul-Henning Kamp 		exit(EEXIT);
1428fae3551SRodney W. Grimes 	}
14377b63aa0SKirk McKusick 	if (preen == 0 && yflag == 0 && sblock.fs_magic == FS_UFS2_MAGIC &&
14477b63aa0SKirk McKusick 	    fswritefd != -1 && chkrecovery(fsreadfd) == 0 &&
14577b63aa0SKirk McKusick 	    reply("SAVE DATA TO FIND ALTERNATE SUPERBLOCKS") != 0)
14677b63aa0SKirk McKusick 		saverecovery(fsreadfd, fswritefd);
1478fae3551SRodney W. Grimes 	/*
1488fae3551SRodney W. Grimes 	 * allocate and initialize the necessary maps
1498fae3551SRodney W. Grimes 	 */
1506e821c35SKirk McKusick 	bufinit();
15189fdc4e1SMike Barcroft 	bmapsize = roundup(howmany(maxfsblock, CHAR_BIT), sizeof(short));
15281fbded2SKirk McKusick 	blockmap = Calloc((unsigned)bmapsize, sizeof (char));
1538fae3551SRodney W. Grimes 	if (blockmap == NULL) {
1548fae3551SRodney W. Grimes 		printf("cannot alloc %u bytes for blockmap\n",
1558fae3551SRodney W. Grimes 		    (unsigned)bmapsize);
1568fae3551SRodney W. Grimes 		goto badsb;
1578fae3551SRodney W. Grimes 	}
158f6717697SPedro F. Giffuni 	inostathead = Calloc(sblock.fs_ncg, sizeof(struct inostatlist));
159d33e92f9SJulian Elischer 	if (inostathead == NULL) {
160d33e92f9SJulian Elischer 		printf("cannot alloc %u bytes for inostathead\n",
161d33e92f9SJulian Elischer 		    (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg)));
1628fae3551SRodney W. Grimes 		goto badsb;
1638fae3551SRodney W. Grimes 	}
16452f97104SKirk McKusick 	numdirs = sblock.fs_cstotal.cs_ndir;
16552f97104SKirk McKusick 	dirhash = MAX(numdirs / 2, 1);
1668fae3551SRodney W. Grimes 	inplast = 0;
1678fae3551SRodney W. Grimes 	listmax = numdirs + 10;
168f6717697SPedro F. Giffuni 	inpsort = (struct inoinfo **)Calloc(listmax, sizeof(struct inoinfo *));
16952f97104SKirk McKusick 	inphash = (struct inohash *)Calloc(dirhash, sizeof(struct inohash));
17052f97104SKirk McKusick 	if (inpsort == NULL || inphash == NULL) {
17152f97104SKirk McKusick 		printf("cannot alloc %ju bytes for inphash\n",
17284fc0d7eSMaxime Henrion 		    (uintmax_t)numdirs * sizeof(struct inoinfo *));
1738fae3551SRodney W. Grimes 		goto badsb;
1748fae3551SRodney W. Grimes 	}
175b1897c19SJulian Elischer 	if (sblock.fs_flags & FS_DOSOFTDEP)
176b1897c19SJulian Elischer 		usedsoftdep = 1;
177b1897c19SJulian Elischer 	else
178b1897c19SJulian Elischer 		usedsoftdep = 0;
179460ed610SKirk McKusick 	/*
180460ed610SKirk McKusick 	 * Collect any snapshot inodes so that we can allow them to
181460ed610SKirk McKusick 	 * claim any blocks that we free. The code for doing this is
182460ed610SKirk McKusick 	 * imported here and into inode.c from sys/ufs/ffs/ffs_snapshot.c.
183460ed610SKirk McKusick 	 */
184460ed610SKirk McKusick 	for (snapcnt = 0; snapcnt < FSMAXSNAP; snapcnt++) {
185460ed610SKirk McKusick 		if (sblock.fs_snapinum[snapcnt] == 0)
186460ed610SKirk McKusick 			break;
187460ed610SKirk McKusick 		ginode(sblock.fs_snapinum[snapcnt], &ip);
188460ed610SKirk McKusick 		if ((DIP(ip.i_dp, di_mode) & IFMT) == IFREG &&
189460ed610SKirk McKusick 		    (DIP(ip.i_dp, di_flags) & SF_SNAPSHOT) != 0 &&
190460ed610SKirk McKusick 		    checksnapinfo(&ip)) {
191460ed610SKirk McKusick 			if (debug)
192460ed610SKirk McKusick 				printf("Load snapshot %jd\n",
193460ed610SKirk McKusick 				    (intmax_t)sblock.fs_snapinum[snapcnt]);
194460ed610SKirk McKusick 			snaplist[snapcnt] = ip;
195460ed610SKirk McKusick 			continue;
196460ed610SKirk McKusick 		}
197460ed610SKirk McKusick 		printf("Removing non-snapshot inode %ju from snapshot list\n",
198460ed610SKirk McKusick 		    (uintmax_t)sblock.fs_snapinum[snapcnt]);
199460ed610SKirk McKusick 		irelse(&ip);
200460ed610SKirk McKusick 		for (i = snapcnt + 1; i < FSMAXSNAP; i++) {
201460ed610SKirk McKusick 			if (sblock.fs_snapinum[i] == 0)
202460ed610SKirk McKusick 				break;
203460ed610SKirk McKusick 			sblock.fs_snapinum[i - 1] = sblock.fs_snapinum[i];
204460ed610SKirk McKusick 		}
205460ed610SKirk McKusick 		sblock.fs_snapinum[i - 1] = 0;
206460ed610SKirk McKusick 		snapcnt--;
207460ed610SKirk McKusick 		sbdirty();
208460ed610SKirk McKusick 	}
209460ed610SKirk McKusick 	if (snapcnt > 0 && copybuf == NULL) {
210*772430ddSKirk McKusick 		copybuf = Balloc(sblock.fs_bsize);
211460ed610SKirk McKusick 		if (copybuf == NULL)
212460ed610SKirk McKusick 			errx(EEXIT, "cannot allocate space for snapshot "
213460ed610SKirk McKusick 			    "copy buffer");
214460ed610SKirk McKusick 	}
2158fae3551SRodney W. Grimes 	return (1);
2168fae3551SRodney W. Grimes 
2178fae3551SRodney W. Grimes badsb:
218780a5c1eSPeter Wemm 	ckfini(0);
2198fae3551SRodney W. Grimes 	return (0);
2208fae3551SRodney W. Grimes }
2218fae3551SRodney W. Grimes 
2228fae3551SRodney W. Grimes /*
223460ed610SKirk McKusick  * Check for valid snapshot information.
224460ed610SKirk McKusick  *
225460ed610SKirk McKusick  * Each snapshot has a list of blocks that have been copied. This list
226460ed610SKirk McKusick  * is consulted before checking the snapshot inode. Its purpose is to
227460ed610SKirk McKusick  * speed checking of commonly checked blocks and to avoid recursive
228460ed610SKirk McKusick  * checks of the snapshot inode. In particular, the list must contain
229460ed610SKirk McKusick  * the superblock, the superblock summary information, and all the
230460ed610SKirk McKusick  * cylinder group blocks. The list may contain other commonly checked
231460ed610SKirk McKusick  * pointers such as those of the blocks that contain the snapshot inodes.
232460ed610SKirk McKusick  * The list is sorted into block order to allow binary search lookup.
233460ed610SKirk McKusick  *
234460ed610SKirk McKusick  * The twelve direct direct block pointers of the snapshot are always
235460ed610SKirk McKusick  * copied, so we test for them first before checking the list itself
236460ed610SKirk McKusick  * (i.e., they are not in the list).
237460ed610SKirk McKusick  *
238460ed610SKirk McKusick  * The checksnapinfo() routine needs to ensure that the list contains at
239460ed610SKirk McKusick  * least the super block, its summary information, and the cylinder groups.
240460ed610SKirk McKusick  * Here we check the list first for the superblock, zero or more cylinder
241460ed610SKirk McKusick  * groups up to the location of the superblock summary information, the
242460ed610SKirk McKusick  * summary group information, and any remaining cylinder group maps that
243460ed610SKirk McKusick  * follow it. We skip over any other entries in the list.
244460ed610SKirk McKusick  */
245460ed610SKirk McKusick #define CHKBLKINLIST(chkblk)						\
246460ed610SKirk McKusick 	/* All UFS_NDADDR blocks are copied */				\
247460ed610SKirk McKusick 	if ((chkblk) >= UFS_NDADDR) {					\
248460ed610SKirk McKusick 		/* Skip over blocks that are not of interest */		\
249460ed610SKirk McKusick 		while (*blkp < (chkblk) && blkp < lastblkp)		\
250460ed610SKirk McKusick 			blkp++;						\
251460ed610SKirk McKusick 		/* Fail if end of list and not all blocks found */	\
252460ed610SKirk McKusick 		if (blkp >= lastblkp) {					\
253460ed610SKirk McKusick 			pwarn("UFS%d snapshot inode %jd failed: "	\
254460ed610SKirk McKusick 			    "improper block list length (%jd)\n",	\
255460ed610SKirk McKusick 			    sblock.fs_magic == FS_UFS1_MAGIC ? 1 : 2,	\
256460ed610SKirk McKusick 			    (intmax_t)snapip->i_number,			\
257460ed610SKirk McKusick 			    (intmax_t)(lastblkp - &snapblklist[0]));	\
258460ed610SKirk McKusick 			status = 0;					\
259460ed610SKirk McKusick 		}							\
260460ed610SKirk McKusick 		/* Fail if block we seek is missing */			\
261460ed610SKirk McKusick 		else if (*blkp++ != (chkblk)) {				\
262460ed610SKirk McKusick 			pwarn("UFS%d snapshot inode %jd failed: "	\
263460ed610SKirk McKusick 			    "block list (%jd) != %s (%jd)\n",		\
264460ed610SKirk McKusick 			    sblock.fs_magic == FS_UFS1_MAGIC ? 1 : 2,	\
265460ed610SKirk McKusick 			    (intmax_t)snapip->i_number,			\
266460ed610SKirk McKusick 			    (intmax_t)blkp[-1],	#chkblk,		\
267460ed610SKirk McKusick 			    (intmax_t)chkblk);				\
268460ed610SKirk McKusick 			status = 0;					\
269460ed610SKirk McKusick 		}							\
270460ed610SKirk McKusick 	}
271460ed610SKirk McKusick 
272460ed610SKirk McKusick static int
checksnapinfo(struct inode * snapip)273460ed610SKirk McKusick checksnapinfo(struct inode *snapip)
274460ed610SKirk McKusick {
275460ed610SKirk McKusick 	struct fs *fs;
276460ed610SKirk McKusick 	struct bufarea *bp;
277460ed610SKirk McKusick 	struct inodesc idesc;
278460ed610SKirk McKusick 	daddr_t *snapblklist, *blkp, *lastblkp, csblkno;
279460ed610SKirk McKusick 	int cg, loc, len, status;
280460ed610SKirk McKusick 	ufs_lbn_t lbn;
281460ed610SKirk McKusick 	size_t size;
282460ed610SKirk McKusick 
283460ed610SKirk McKusick 	fs = &sblock;
284460ed610SKirk McKusick 	memset(&idesc, 0, sizeof(struct inodesc));
285460ed610SKirk McKusick 	idesc.id_type = ADDR;
286460ed610SKirk McKusick 	idesc.id_func = getlbnblkno;
287460ed610SKirk McKusick 	idesc.id_number = snapip->i_number;
288460ed610SKirk McKusick 	lbn = howmany(fs->fs_size, fs->fs_frag);
289460ed610SKirk McKusick 	idesc.id_parent = lbn;		/* sought after blkno */
290460ed610SKirk McKusick 	if ((ckinode(snapip->i_dp, &idesc) & FOUND) == 0)
291460ed610SKirk McKusick 		return (0);
292460ed610SKirk McKusick 	size = fragroundup(fs,
293460ed610SKirk McKusick 	    DIP(snapip->i_dp, di_size) - lblktosize(fs, lbn));
294460ed610SKirk McKusick 	bp = getdatablk(idesc.id_parent, size, BT_DATA);
2951e39a088SKirk McKusick 	if (bp->b_errs != 0)
2961e39a088SKirk McKusick 		return (0);
297460ed610SKirk McKusick 	snapblklist = (daddr_t *)bp->b_un.b_buf;
298460ed610SKirk McKusick 	/*
299460ed610SKirk McKusick 	 * snapblklist[0] is the size of the list
300460ed610SKirk McKusick 	 * snapblklist[1] is the first element of the list
301460ed610SKirk McKusick 	 *
302460ed610SKirk McKusick 	 * We need to be careful to bound the size of the list and verify
303460ed610SKirk McKusick 	 * that we have not run off the end of it if it or its size has
304460ed610SKirk McKusick 	 * been corrupted.
305460ed610SKirk McKusick 	 */
306460ed610SKirk McKusick 	blkp = &snapblklist[1];
307460ed610SKirk McKusick 	lastblkp = &snapblklist[MAX(0,
308460ed610SKirk McKusick 	    MIN(snapblklist[0] + 1, size / sizeof(daddr_t)))];
309460ed610SKirk McKusick 	status = 1;
310460ed610SKirk McKusick 	/* Check that the superblock is listed. */
311460ed610SKirk McKusick 	CHKBLKINLIST(lblkno(fs, fs->fs_sblockloc));
312460ed610SKirk McKusick 	if (status == 0)
313460ed610SKirk McKusick 		goto out;
314460ed610SKirk McKusick 	/*
315460ed610SKirk McKusick 	 * Calculate where the summary information is located.
316460ed610SKirk McKusick 	 * Usually it is in the first cylinder group, but growfs
317460ed610SKirk McKusick 	 * may move it to the first cylinder group that it adds.
318460ed610SKirk McKusick 	 *
319460ed610SKirk McKusick 	 * Check all cylinder groups up to the summary information.
320460ed610SKirk McKusick 	 */
321460ed610SKirk McKusick 	csblkno = fragstoblks(fs, fs->fs_csaddr);
322460ed610SKirk McKusick 	for (cg = 0; cg < fs->fs_ncg; cg++) {
323460ed610SKirk McKusick 		if (fragstoblks(fs, cgtod(fs, cg)) > csblkno)
324460ed610SKirk McKusick 			break;
325460ed610SKirk McKusick 		CHKBLKINLIST(fragstoblks(fs, cgtod(fs, cg)));
326460ed610SKirk McKusick 		if (status == 0)
327460ed610SKirk McKusick 			goto out;
328460ed610SKirk McKusick 	}
329460ed610SKirk McKusick 	/* Check the summary information block(s). */
330460ed610SKirk McKusick 	len = howmany(fs->fs_cssize, fs->fs_bsize);
331460ed610SKirk McKusick 	for (loc = 0; loc < len; loc++) {
332460ed610SKirk McKusick 		CHKBLKINLIST(csblkno + loc);
333460ed610SKirk McKusick 		if (status == 0)
334460ed610SKirk McKusick 			goto out;
335460ed610SKirk McKusick 	}
336460ed610SKirk McKusick 	/* Check the remaining cylinder groups. */
337460ed610SKirk McKusick 	for (; cg < fs->fs_ncg; cg++) {
338460ed610SKirk McKusick 		CHKBLKINLIST(fragstoblks(fs, cgtod(fs, cg)));
339460ed610SKirk McKusick 		if (status == 0)
340460ed610SKirk McKusick 			goto out;
341460ed610SKirk McKusick 	}
342460ed610SKirk McKusick out:
343460ed610SKirk McKusick 	brelse(bp);
344460ed610SKirk McKusick 	return (status);
345460ed610SKirk McKusick }
346460ed610SKirk McKusick 
347460ed610SKirk McKusick /*
348460ed610SKirk McKusick  * Return the block number associated with a specified inode lbn.
349460ed610SKirk McKusick  * Requested lbn is in id_parent. If found, block is returned in
350460ed610SKirk McKusick  * id_parent.
351460ed610SKirk McKusick  */
352460ed610SKirk McKusick static int
getlbnblkno(struct inodesc * idesc)353460ed610SKirk McKusick getlbnblkno(struct inodesc *idesc)
354460ed610SKirk McKusick {
355460ed610SKirk McKusick 
356460ed610SKirk McKusick 	if (idesc->id_lbn < idesc->id_parent)
357460ed610SKirk McKusick 		return (KEEPON);
358460ed610SKirk McKusick 	idesc->id_parent = idesc->id_blkno;
359460ed610SKirk McKusick 	return (STOP | FOUND);
360460ed610SKirk McKusick }
361460ed610SKirk McKusick 
362460ed610SKirk McKusick /*
363c5d476c9SKirk McKusick  * Open a device or file to be checked by fsck.
364c5d476c9SKirk McKusick  */
365c5d476c9SKirk McKusick int
openfilesys(char * dev)366c5d476c9SKirk McKusick openfilesys(char *dev)
367c5d476c9SKirk McKusick {
368c5d476c9SKirk McKusick 	struct stat statb;
369c5d476c9SKirk McKusick 	int saved_fsreadfd;
370c5d476c9SKirk McKusick 
3712983ec0aSKirk McKusick 	if (stat(dev, &statb) < 0)
372c5d476c9SKirk McKusick 		return (0);
373c5d476c9SKirk McKusick 	if ((statb.st_mode & S_IFMT) != S_IFCHR &&
374c5d476c9SKirk McKusick 	    (statb.st_mode & S_IFMT) != S_IFBLK) {
375c5d476c9SKirk McKusick 		if (bkgrdflag != 0 && (statb.st_flags & SF_SNAPSHOT) == 0) {
376d3a36e4bSKirk McKusick 			pwarn("BACKGROUND FSCK LACKS A SNAPSHOT\n");
377d3a36e4bSKirk McKusick 			return (0);
378c5d476c9SKirk McKusick 		}
379c5d476c9SKirk McKusick 		if (bkgrdflag != 0) {
380c5d476c9SKirk McKusick 			cursnapshot = statb.st_ino;
381c5d476c9SKirk McKusick 		} else {
382d3a36e4bSKirk McKusick 			pwarn("%s IS NOT A DISK DEVICE\n", dev);
383d3a36e4bSKirk McKusick 			if (preen || reply("CONTINUE") == 0)
384c5d476c9SKirk McKusick 				return (0);
385c5d476c9SKirk McKusick 		}
386c5d476c9SKirk McKusick 	}
387c5d476c9SKirk McKusick 	saved_fsreadfd = fsreadfd;
388c5d476c9SKirk McKusick 	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
389c5d476c9SKirk McKusick 		fsreadfd = saved_fsreadfd;
390c5d476c9SKirk McKusick 		return (0);
391c5d476c9SKirk McKusick 	}
392c5d476c9SKirk McKusick 	if (saved_fsreadfd != -1)
393c5d476c9SKirk McKusick 		close(saved_fsreadfd);
394c5d476c9SKirk McKusick 	return (1);
395c5d476c9SKirk McKusick }
396c5d476c9SKirk McKusick 
397c5d476c9SKirk McKusick /*
3988fae3551SRodney W. Grimes  * Read in the super block and its summary info.
3998fae3551SRodney W. Grimes  */
40038375c40SKirk McKusick int
readsb(void)401e6886616SKirk McKusick readsb(void)
4028fae3551SRodney W. Grimes {
403dffce215SKirk McKusick 	struct fs *fs;
4048fae3551SRodney W. Grimes 
405e6886616SKirk McKusick 	sbhashfailed = 0;
406ed75b5a1SKirk McKusick 	readcnt[sblk.b_type]++;
407e6886616SKirk McKusick 	/*
408e6886616SKirk McKusick 	 * If bflag is given, then check just that superblock.
409e6886616SKirk McKusick 	 */
410e6886616SKirk McKusick 	if (bflag) {
4114f9606c9SKirk McKusick 		switch (sbget(fsreadfd, &fs, bflag * dev_bsize, 0)) {
412e6886616SKirk McKusick 		case 0:
413e6886616SKirk McKusick 			goto goodsb;
414c0bfa109SKirk McKusick 		case EINTEGRITY:
415e6886616SKirk McKusick 			printf("Check hash failed for superblock at %jd\n",
416e6886616SKirk McKusick 			    bflag);
417038c170fSKirk McKusick 			return (0);
418dffce215SKirk McKusick 		case ENOENT:
419e6886616SKirk McKusick 			printf("%jd is not a file system superblock\n", bflag);
420f033309fSKirk McKusick 			return (0);
421dffce215SKirk McKusick 		case EIO:
422dffce215SKirk McKusick 		default:
423e6886616SKirk McKusick 			printf("I/O error reading %jd\n", bflag);
4241c85e6a3SKirk McKusick 			return (0);
4251c85e6a3SKirk McKusick 		}
4261c85e6a3SKirk McKusick 	}
427e6886616SKirk McKusick 	/*
428e6886616SKirk McKusick 	 * Check for the standard superblock and use it if good.
429e6886616SKirk McKusick 	 */
430e6886616SKirk McKusick 	if (sbget(fsreadfd, &fs, UFS_STDSB, UFS_NOMSG) == 0)
431e6886616SKirk McKusick 		goto goodsb;
432e6886616SKirk McKusick 	/*
433e6886616SKirk McKusick 	 * Check if the only problem is a check-hash failure.
434e6886616SKirk McKusick 	 */
435e6886616SKirk McKusick 	skipclean = 0;
436e6886616SKirk McKusick 	if (sbget(fsreadfd, &fs, UFS_STDSB, UFS_NOMSG | UFS_NOHASHFAIL) == 0) {
437e6886616SKirk McKusick 		sbhashfailed = 1;
438e6886616SKirk McKusick 		goto goodsb;
439e6886616SKirk McKusick 	}
440e6886616SKirk McKusick 	/*
441e6886616SKirk McKusick 	 * Do an exhaustive search for a usable superblock.
442e6886616SKirk McKusick 	 */
443e6886616SKirk McKusick 	switch (sbsearch(fsreadfd, &fs, 0)) {
444e6886616SKirk McKusick 	case 0:
445e6886616SKirk McKusick 		goto goodsb;
446e6886616SKirk McKusick 	case ENOENT:
447e6886616SKirk McKusick 		printf("SEARCH FOR ALTERNATE SUPER-BLOCK FAILED. "
448e6886616SKirk McKusick 		    "YOU MUST USE THE\n-b OPTION TO FSCK TO SPECIFY "
449e6886616SKirk McKusick 		    "THE LOCATION OF AN ALTERNATE\nSUPER-BLOCK TO "
450e6886616SKirk McKusick 		    "SUPPLY NEEDED INFORMATION; SEE fsck_ffs(8).\n");
451e6886616SKirk McKusick 		return (0);
452e6886616SKirk McKusick 	case EIO:
453e6886616SKirk McKusick 	default:
454e6886616SKirk McKusick 		printf("I/O error reading a usable superblock\n");
455e6886616SKirk McKusick 		return (0);
456e6886616SKirk McKusick 	}
457e6886616SKirk McKusick 
458e6886616SKirk McKusick goodsb:
459dffce215SKirk McKusick 	memcpy(&sblock, fs, fs->fs_sbsize);
460dffce215SKirk McKusick 	free(fs);
4618fae3551SRodney W. Grimes 	/*
4628fae3551SRodney W. Grimes 	 * Compute block size that the file system is based on,
4638fae3551SRodney W. Grimes 	 * according to fsbtodb, and adjust superblock block number
4648fae3551SRodney W. Grimes 	 * so we can tell if this is an alternate later.
4658fae3551SRodney W. Grimes 	 */
4668fae3551SRodney W. Grimes 	dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
467dffce215SKirk McKusick 	sblk.b_bno = sblock.fs_sblockactualloc / dev_bsize;
4681c85e6a3SKirk McKusick 	sblk.b_size = SBLOCKSIZE;
4698fae3551SRodney W. Grimes 	/*
4701c85e6a3SKirk McKusick 	 * If not yet done, update UFS1 superblock with new wider fields.
4711c85e6a3SKirk McKusick 	 */
4721c85e6a3SKirk McKusick 	if (sblock.fs_magic == FS_UFS1_MAGIC &&
4731c85e6a3SKirk McKusick 	    sblock.fs_maxbsize != sblock.fs_bsize) {
4741c85e6a3SKirk McKusick 		sblock.fs_maxbsize = sblock.fs_bsize;
4751c85e6a3SKirk McKusick 		sblock.fs_time = sblock.fs_old_time;
4761c85e6a3SKirk McKusick 		sblock.fs_size = sblock.fs_old_size;
4771c85e6a3SKirk McKusick 		sblock.fs_dsize = sblock.fs_old_dsize;
4781c85e6a3SKirk McKusick 		sblock.fs_csaddr = sblock.fs_old_csaddr;
4791c85e6a3SKirk McKusick 		sblock.fs_cstotal.cs_ndir = sblock.fs_old_cstotal.cs_ndir;
4801c85e6a3SKirk McKusick 		sblock.fs_cstotal.cs_nbfree = sblock.fs_old_cstotal.cs_nbfree;
4811c85e6a3SKirk McKusick 		sblock.fs_cstotal.cs_nifree = sblock.fs_old_cstotal.cs_nifree;
4821c85e6a3SKirk McKusick 		sblock.fs_cstotal.cs_nffree = sblock.fs_old_cstotal.cs_nffree;
4831c85e6a3SKirk McKusick 	}
4848fae3551SRodney W. Grimes 	havesb = 1;
4858fae3551SRodney W. Grimes 	return (1);
4868fae3551SRodney W. Grimes }
4878fae3551SRodney W. Grimes 
488d164d805SIan Dowse void
sblock_init(void)489b70cd7eeSWarner Losh sblock_init(void)
49038375c40SKirk McKusick {
49138375c40SKirk McKusick 
492c5d476c9SKirk McKusick 	fsreadfd = -1;
49338375c40SKirk McKusick 	fswritefd = -1;
49438375c40SKirk McKusick 	fsmodified = 0;
49538375c40SKirk McKusick 	lfdir = 0;
496ed75b5a1SKirk McKusick 	initbarea(&sblk, BT_SUPERBLK);
497*772430ddSKirk McKusick 	sblk.b_un.b_buf = Balloc(SBLOCKSIZE);
49823c8b19cSKirk McKusick 	if (sblk.b_un.b_buf == NULL)
49938375c40SKirk McKusick 		errx(EEXIT, "cannot allocate space for superblock");
50038375c40SKirk McKusick 	dev_bsize = secsize = DEV_BSIZE;
50138375c40SKirk McKusick }
50277b63aa0SKirk McKusick 
50377b63aa0SKirk McKusick /*
50477b63aa0SKirk McKusick  * Calculate a prototype superblock based on information in the boot area.
50577b63aa0SKirk McKusick  * When done the cgsblock macro can be calculated and the fs_ncg field
50677b63aa0SKirk McKusick  * can be used. Do NOT attempt to use other macros without verifying that
50777b63aa0SKirk McKusick  * their needed information is available!
50877b63aa0SKirk McKusick  */
50977b63aa0SKirk McKusick static int
calcsb(char * dev,int devfd,struct fs * fs)51077b63aa0SKirk McKusick calcsb(char *dev, int devfd, struct fs *fs)
51177b63aa0SKirk McKusick {
512855662c6SKirk McKusick 	struct fsrecovery *fsr;
513855662c6SKirk McKusick 	char *fsrbuf;
514855662c6SKirk McKusick 	u_int secsize;
51577b63aa0SKirk McKusick 
51677b63aa0SKirk McKusick 	/*
51777b63aa0SKirk McKusick 	 * We need fragments-per-group and the partition-size.
51877b63aa0SKirk McKusick 	 *
51977b63aa0SKirk McKusick 	 * Newfs stores these details at the end of the boot block area
52077b63aa0SKirk McKusick 	 * at the start of the filesystem partition. If they have been
52177b63aa0SKirk McKusick 	 * overwritten by a boot block, we fail. But usually they are
52277b63aa0SKirk McKusick 	 * there and we can use them.
52377b63aa0SKirk McKusick 	 */
524855662c6SKirk McKusick 	if (ioctl(devfd, DIOCGSECTORSIZE, &secsize) == -1)
525855662c6SKirk McKusick 		return (0);
526*772430ddSKirk McKusick 	fsrbuf = Balloc(secsize);
527855662c6SKirk McKusick 	if (fsrbuf == NULL)
528855662c6SKirk McKusick 		errx(EEXIT, "calcsb: cannot allocate recovery buffer");
529855662c6SKirk McKusick 	if (blread(devfd, fsrbuf,
53015da40b0SEric van Gyzen 	    (SBLOCK_UFS2 - secsize) / dev_bsize, secsize) != 0) {
53115da40b0SEric van Gyzen 		free(fsrbuf);
532855662c6SKirk McKusick 		return (0);
53315da40b0SEric van Gyzen 	}
534855662c6SKirk McKusick 	fsr = (struct fsrecovery *)&fsrbuf[secsize - sizeof *fsr];
53515da40b0SEric van Gyzen 	if (fsr->fsr_magic != FS_UFS2_MAGIC) {
53615da40b0SEric van Gyzen 		free(fsrbuf);
53777b63aa0SKirk McKusick 		return (0);
53815da40b0SEric van Gyzen 	}
53977b63aa0SKirk McKusick 	memset(fs, 0, sizeof(struct fs));
540855662c6SKirk McKusick 	fs->fs_fpg = fsr->fsr_fpg;
541855662c6SKirk McKusick 	fs->fs_fsbtodb = fsr->fsr_fsbtodb;
542855662c6SKirk McKusick 	fs->fs_sblkno = fsr->fsr_sblkno;
543855662c6SKirk McKusick 	fs->fs_magic = fsr->fsr_magic;
544855662c6SKirk McKusick 	fs->fs_ncg = fsr->fsr_ncg;
545855662c6SKirk McKusick 	free(fsrbuf);
54677b63aa0SKirk McKusick 	return (1);
54777b63aa0SKirk McKusick }
54877b63aa0SKirk McKusick 
54977b63aa0SKirk McKusick /*
55077b63aa0SKirk McKusick  * Check to see if recovery information exists.
551855662c6SKirk McKusick  * Return 1 if it exists or cannot be created.
552855662c6SKirk McKusick  * Return 0 if it does not exist and can be created.
55377b63aa0SKirk McKusick  */
55477b63aa0SKirk McKusick static int
chkrecovery(int devfd)55577b63aa0SKirk McKusick chkrecovery(int devfd)
55677b63aa0SKirk McKusick {
557855662c6SKirk McKusick 	struct fsrecovery *fsr;
558855662c6SKirk McKusick 	char *fsrbuf;
5597a1c1f6aSKirk McKusick 	u_int secsize, rdsize;
56077b63aa0SKirk McKusick 
561855662c6SKirk McKusick 	/*
562855662c6SKirk McKusick 	 * Could not determine if backup material exists, so do not
563855662c6SKirk McKusick 	 * offer to create it.
564855662c6SKirk McKusick 	 */
56515da40b0SEric van Gyzen 	fsrbuf = NULL;
5667a1c1f6aSKirk McKusick 	rdsize = sblock.fs_fsize;
567855662c6SKirk McKusick 	if (ioctl(devfd, DIOCGSECTORSIZE, &secsize) == -1 ||
5687a1c1f6aSKirk McKusick 	    rdsize % secsize != 0 ||
569*772430ddSKirk McKusick 	    (fsrbuf = Balloc(rdsize)) == NULL ||
5707a1c1f6aSKirk McKusick 	    blread(devfd, fsrbuf, (SBLOCK_UFS2 - rdsize) / dev_bsize,
5717a1c1f6aSKirk McKusick 	      rdsize) != 0) {
57215da40b0SEric van Gyzen 		free(fsrbuf);
57377b63aa0SKirk McKusick 		return (1);
57415da40b0SEric van Gyzen 	}
575855662c6SKirk McKusick 	/*
576855662c6SKirk McKusick 	 * Recovery material has already been created, so do not
577855662c6SKirk McKusick 	 * need to create it again.
578855662c6SKirk McKusick 	 */
5797a1c1f6aSKirk McKusick 	fsr = (struct fsrecovery *)&fsrbuf[rdsize - sizeof *fsr];
580855662c6SKirk McKusick 	if (fsr->fsr_magic == FS_UFS2_MAGIC) {
581855662c6SKirk McKusick 		free(fsrbuf);
582855662c6SKirk McKusick 		return (1);
583855662c6SKirk McKusick 	}
584855662c6SKirk McKusick 	/*
585855662c6SKirk McKusick 	 * Recovery material has not been created and can be if desired.
586855662c6SKirk McKusick 	 */
587855662c6SKirk McKusick 	free(fsrbuf);
588855662c6SKirk McKusick 	return (0);
58977b63aa0SKirk McKusick }
59077b63aa0SKirk McKusick 
59177b63aa0SKirk McKusick /*
5927a1c1f6aSKirk McKusick  * Read the last filesystem-size piece of the boot block, replace the
5937a1c1f6aSKirk McKusick  * last 20 bytes with the recovery information, then write it back.
59477b63aa0SKirk McKusick  * The recovery information only works for UFS2 filesystems.
59577b63aa0SKirk McKusick  */
59677b63aa0SKirk McKusick static void
saverecovery(int readfd,int writefd)59777b63aa0SKirk McKusick saverecovery(int readfd, int writefd)
59877b63aa0SKirk McKusick {
599855662c6SKirk McKusick 	struct fsrecovery *fsr;
600855662c6SKirk McKusick 	char *fsrbuf;
6017a1c1f6aSKirk McKusick 	u_int secsize, rdsize;
60277b63aa0SKirk McKusick 
60315da40b0SEric van Gyzen 	fsrbuf = NULL;
6047a1c1f6aSKirk McKusick 	rdsize = sblock.fs_fsize;
60577b63aa0SKirk McKusick 	if (sblock.fs_magic != FS_UFS2_MAGIC ||
606855662c6SKirk McKusick 	    ioctl(readfd, DIOCGSECTORSIZE, &secsize) == -1 ||
6077a1c1f6aSKirk McKusick 	    rdsize % secsize != 0 ||
608*772430ddSKirk McKusick 	    (fsrbuf = Balloc(rdsize)) == NULL ||
6097a1c1f6aSKirk McKusick 	    blread(readfd, fsrbuf, (SBLOCK_UFS2 - rdsize) / dev_bsize,
6107a1c1f6aSKirk McKusick 	      rdsize) != 0) {
611855662c6SKirk McKusick 		printf("RECOVERY DATA COULD NOT BE CREATED\n");
61215da40b0SEric van Gyzen 		free(fsrbuf);
61377b63aa0SKirk McKusick 		return;
614855662c6SKirk McKusick 	}
6157a1c1f6aSKirk McKusick 	fsr = (struct fsrecovery *)&fsrbuf[rdsize - sizeof *fsr];
616855662c6SKirk McKusick 	fsr->fsr_magic = sblock.fs_magic;
617855662c6SKirk McKusick 	fsr->fsr_fpg = sblock.fs_fpg;
618855662c6SKirk McKusick 	fsr->fsr_fsbtodb = sblock.fs_fsbtodb;
619855662c6SKirk McKusick 	fsr->fsr_sblkno = sblock.fs_sblkno;
620855662c6SKirk McKusick 	fsr->fsr_ncg = sblock.fs_ncg;
6217a1c1f6aSKirk McKusick 	blwrite(writefd, fsrbuf, (SBLOCK_UFS2 - rdsize) / dev_bsize, rdsize);
622855662c6SKirk McKusick 	free(fsrbuf);
62377b63aa0SKirk McKusick }
624