xref: /freebsd/sbin/fsck_ffs/setup.c (revision 1165fc9a526630487a1feb63daef65c5aee1a583)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1986, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  */
31 
32 #if 0
33 #ifndef lint
34 static const char sccsid[] = "@(#)setup.c	8.10 (Berkeley) 5/9/95";
35 #endif /* not lint */
36 #endif
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include <sys/param.h>
41 #include <sys/disk.h>
42 #include <sys/stat.h>
43 #define FSTYPENAMES
44 #include <sys/disklabel.h>
45 #include <sys/file.h>
46 #include <sys/sysctl.h>
47 
48 #include <ufs/ufs/dinode.h>
49 #include <ufs/ffs/fs.h>
50 
51 #include <ctype.h>
52 #include <err.h>
53 #include <errno.h>
54 #include <limits.h>
55 #include <stdint.h>
56 #include <string.h>
57 #include <libufs.h>
58 
59 #include "fsck.h"
60 
61 struct inoinfo **inphead, **inpsort;	/* info about all inodes */
62 
63 struct bufarea asblk;
64 #define altsblock (*asblk.b_un.b_fs)
65 #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
66 
67 static int calcsb(char *dev, int devfd, struct fs *fs);
68 static void saverecovery(int readfd, int writefd);
69 static int chkrecovery(int devfd);
70 
71 /*
72  * Read in a superblock finding an alternate if necessary.
73  * Return 1 if successful, 0 if unsuccessful, -1 if file system
74  * is already clean (ckclean and preen mode only).
75  */
76 int
77 setup(char *dev)
78 {
79 	long cg, bmapsize;
80 	struct fs proto;
81 
82 	/*
83 	 * We are expected to have an open file descriptor
84 	 */
85 	if (fsreadfd < 0)
86 		return (0);
87 	/*
88 	 * If we do not yet have a superblock, read it in looking
89 	 * for alternates if necessary.
90 	 */
91 	if (havesb == 0 && readsb(1) == 0) {
92 		skipclean = 0;
93 		if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
94 			return(0);
95 		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
96 			return (0);
97 		for (cg = 0; cg < proto.fs_ncg; cg++) {
98 			bflag = fsbtodb(&proto, cgsblock(&proto, cg));
99 			if (readsb(0) != 0)
100 				break;
101 		}
102 		if (cg >= proto.fs_ncg) {
103 			printf("SEARCH FOR ALTERNATE SUPER-BLOCK FAILED. "
104 			    "YOU MUST USE THE\n-b OPTION TO FSCK TO SPECIFY "
105 			    "THE LOCATION OF AN ALTERNATE\nSUPER-BLOCK TO "
106 			    "SUPPLY NEEDED INFORMATION; SEE fsck_ffs(8).\n");
107 			bflag = 0;
108 			return(0);
109 		}
110 		pwarn("USING ALTERNATE SUPERBLOCK AT %jd\n", bflag);
111 		bflag = 0;
112 	}
113 	if (preen == 0)
114 		printf("** %s", dev);
115 	if (bkgrdflag == 0 &&
116 	    (nflag || (fswritefd = open(dev, O_WRONLY)) < 0)) {
117 		fswritefd = -1;
118 		if (preen)
119 			pfatal("NO WRITE ACCESS");
120 		printf(" (NO WRITE)");
121 	}
122 	if (preen == 0)
123 		printf("\n");
124 	if (sbhashfailed != 0) {
125 		pwarn("SUPERBLOCK CHECK HASH FAILED");
126 		if (fswritefd == -1)
127 			pwarn("OPENED READONLY SO CANNOT CORRECT CHECK HASH\n");
128 		else if (preen || reply("CORRECT CHECK HASH") != 0) {
129 			if (preen)
130 				printf(" (CORRECTED)\n");
131 			sblock.fs_clean = 0;
132 			sbdirty();
133 		}
134 	}
135 	if (skipclean && ckclean && sblock.fs_clean) {
136 		pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
137 		return (-1);
138 	}
139 	maxfsblock = sblock.fs_size;
140 	maxino = sblock.fs_ncg * sblock.fs_ipg;
141 	/*
142 	 * Check and potentially fix certain fields in the super block.
143 	 */
144 	if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
145 		pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
146 		if (reply("SET TO DEFAULT") == 1) {
147 			sblock.fs_optim = FS_OPTTIME;
148 			sbdirty();
149 		}
150 	}
151 	if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
152 		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
153 			sblock.fs_minfree);
154 		if (reply("SET TO DEFAULT") == 1) {
155 			sblock.fs_minfree = 10;
156 			sbdirty();
157 		}
158 	}
159 	if (sblock.fs_magic == FS_UFS1_MAGIC &&
160 	    sblock.fs_old_inodefmt < FS_44INODEFMT) {
161 		pwarn("Format of file system is too old.\n");
162 		pwarn("Must update to modern format using a version of fsck\n");
163 		pfatal("from before 2002 with the command ``fsck -c 2''\n");
164 		exit(EEXIT);
165 	}
166 	if ((asblk.b_flags & B_DIRTY) != 0 && !bflag) {
167 		memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize);
168 		flush(fswritefd, &asblk);
169 	}
170 	if (preen == 0 && yflag == 0 && sblock.fs_magic == FS_UFS2_MAGIC &&
171 	    fswritefd != -1 && chkrecovery(fsreadfd) == 0 &&
172 	    reply("SAVE DATA TO FIND ALTERNATE SUPERBLOCKS") != 0)
173 		saverecovery(fsreadfd, fswritefd);
174 	/*
175 	 * allocate and initialize the necessary maps
176 	 */
177 	bmapsize = roundup(howmany(maxfsblock, CHAR_BIT), sizeof(short));
178 	blockmap = Calloc((unsigned)bmapsize, sizeof (char));
179 	if (blockmap == NULL) {
180 		printf("cannot alloc %u bytes for blockmap\n",
181 		    (unsigned)bmapsize);
182 		goto badsb;
183 	}
184 	inostathead = Calloc(sblock.fs_ncg, sizeof(struct inostatlist));
185 	if (inostathead == NULL) {
186 		printf("cannot alloc %u bytes for inostathead\n",
187 		    (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg)));
188 		goto badsb;
189 	}
190 	numdirs = MAX(sblock.fs_cstotal.cs_ndir, 128);
191 	dirhash = numdirs;
192 	inplast = 0;
193 	listmax = numdirs + 10;
194 	inpsort = (struct inoinfo **)Calloc(listmax, sizeof(struct inoinfo *));
195 	inphead = (struct inoinfo **)Calloc(numdirs, sizeof(struct inoinfo *));
196 	if (inpsort == NULL || inphead == NULL) {
197 		printf("cannot alloc %ju bytes for inphead\n",
198 		    (uintmax_t)numdirs * sizeof(struct inoinfo *));
199 		goto badsb;
200 	}
201 	bufinit();
202 	if (sblock.fs_flags & FS_DOSOFTDEP)
203 		usedsoftdep = 1;
204 	else
205 		usedsoftdep = 0;
206 	return (1);
207 
208 badsb:
209 	ckfini(0);
210 	return (0);
211 }
212 
213 /*
214  * Open a device or file to be checked by fsck.
215  */
216 int
217 openfilesys(char *dev)
218 {
219 	struct stat statb;
220 	int saved_fsreadfd;
221 
222 	if (stat(dev, &statb) < 0)
223 		return (0);
224 	if ((statb.st_mode & S_IFMT) != S_IFCHR &&
225 	    (statb.st_mode & S_IFMT) != S_IFBLK) {
226 		if (bkgrdflag != 0 && (statb.st_flags & SF_SNAPSHOT) == 0) {
227 			pfatal("BACKGROUND FSCK LACKS A SNAPSHOT\n");
228 			exit(EEXIT);
229 		}
230 		if (bkgrdflag != 0) {
231 			cursnapshot = statb.st_ino;
232 		} else {
233 			pfatal("%s IS NOT A DISK DEVICE\n", dev);
234 			if (reply("CONTINUE") == 0)
235 				return (0);
236 		}
237 	}
238 	saved_fsreadfd = fsreadfd;
239 	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
240 		fsreadfd = saved_fsreadfd;
241 		return (0);
242 	}
243 	if (saved_fsreadfd != -1)
244 		close(saved_fsreadfd);
245 	return (1);
246 }
247 
248 /*
249  * Read in the super block and its summary info.
250  */
251 int
252 readsb(int listerr)
253 {
254 	off_t super;
255 	int bad, ret, flags;
256 	struct fs *fs;
257 
258 	super = bflag ? bflag * dev_bsize : UFS_STDSB;
259 	flags = sbhashfailed ? UFS_NOHASHFAIL | UFS_NOMSG : UFS_NOMSG;
260 	readcnt[sblk.b_type]++;
261 	while ((ret = sbget(fsreadfd, &fs, super, flags)) != 0) {
262 		switch (ret) {
263 		case EINTEGRITY:
264 			if (bflag || (super == UFS_STDSB &&
265 			    flags == (UFS_NOHASHFAIL | UFS_NOMSG)))
266 				return (0);
267 			super = UFS_STDSB;
268 			flags = UFS_NOHASHFAIL | UFS_NOMSG;
269 			sbhashfailed = 1;
270 			continue;
271 		case ENOENT:
272 			if (bflag)
273 				printf("%jd is not a file system "
274 				    "superblock\n", super / dev_bsize);
275 			else
276 				printf("Cannot find file system "
277 				    "superblock\n");
278 			return (0);
279 		case EIO:
280 		default:
281 			printf("I/O error reading %jd\n",
282 			    super / dev_bsize);
283 			return (0);
284 		}
285 	}
286 	memcpy(&sblock, fs, fs->fs_sbsize);
287 	free(fs);
288 	/*
289 	 * Compute block size that the file system is based on,
290 	 * according to fsbtodb, and adjust superblock block number
291 	 * so we can tell if this is an alternate later.
292 	 */
293 	dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
294 	sblk.b_bno = sblock.fs_sblockactualloc / dev_bsize;
295 	sblk.b_size = SBLOCKSIZE;
296 	/*
297 	 * Compare all fields that should not differ in alternate super block.
298 	 * When an alternate super-block is specified this check is skipped.
299 	 */
300 	if (bflag)
301 		goto out;
302 	getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
303 	if (asblk.b_errs)
304 		return (0);
305 	bad = 0;
306 #define CHK(x, y)				\
307 	if (altsblock.x != sblock.x) {		\
308 		bad++;				\
309 		if (listerr && debug)		\
310 			printf("SUPER BLOCK VS ALTERNATE MISMATCH %s: " y " vs " y "\n", \
311 			    #x, (intmax_t)sblock.x, (intmax_t)altsblock.x); \
312 	}
313 	CHK(fs_sblkno, "%jd");
314 	CHK(fs_cblkno, "%jd");
315 	CHK(fs_iblkno, "%jd");
316 	CHK(fs_dblkno, "%jd");
317 	CHK(fs_ncg, "%jd");
318 	CHK(fs_bsize, "%jd");
319 	CHK(fs_fsize, "%jd");
320 	CHK(fs_frag, "%jd");
321 	CHK(fs_bmask, "%#jx");
322 	CHK(fs_fmask, "%#jx");
323 	CHK(fs_bshift, "%jd");
324 	CHK(fs_fshift, "%jd");
325 	CHK(fs_fragshift, "%jd");
326 	CHK(fs_fsbtodb, "%jd");
327 	CHK(fs_sbsize, "%jd");
328 	CHK(fs_nindir, "%jd");
329 	CHK(fs_inopb, "%jd");
330 	CHK(fs_cssize, "%jd");
331 	CHK(fs_ipg, "%jd");
332 	CHK(fs_fpg, "%jd");
333 	CHK(fs_magic, "%#jx");
334 #undef CHK
335 	if (bad) {
336 		if (listerr == 0)
337 			return (0);
338 		if (preen)
339 			printf("%s: ", cdevname);
340 		printf(
341 		    "VALUES IN SUPER BLOCK LSB=%jd DISAGREE WITH THOSE IN\n"
342 		    "LAST ALTERNATE LSB=%jd\n",
343 		    sblk.b_bno, asblk.b_bno);
344 		if (reply("IGNORE ALTERNATE SUPER BLOCK") == 0)
345 			return (0);
346 	}
347 out:
348 	/*
349 	 * If not yet done, update UFS1 superblock with new wider fields.
350 	 */
351 	if (sblock.fs_magic == FS_UFS1_MAGIC &&
352 	    sblock.fs_maxbsize != sblock.fs_bsize) {
353 		sblock.fs_maxbsize = sblock.fs_bsize;
354 		sblock.fs_time = sblock.fs_old_time;
355 		sblock.fs_size = sblock.fs_old_size;
356 		sblock.fs_dsize = sblock.fs_old_dsize;
357 		sblock.fs_csaddr = sblock.fs_old_csaddr;
358 		sblock.fs_cstotal.cs_ndir = sblock.fs_old_cstotal.cs_ndir;
359 		sblock.fs_cstotal.cs_nbfree = sblock.fs_old_cstotal.cs_nbfree;
360 		sblock.fs_cstotal.cs_nifree = sblock.fs_old_cstotal.cs_nifree;
361 		sblock.fs_cstotal.cs_nffree = sblock.fs_old_cstotal.cs_nffree;
362 	}
363 	havesb = 1;
364 	return (1);
365 }
366 
367 void
368 sblock_init(void)
369 {
370 
371 	fsreadfd = -1;
372 	fswritefd = -1;
373 	fsmodified = 0;
374 	lfdir = 0;
375 	initbarea(&sblk, BT_SUPERBLK);
376 	initbarea(&asblk, BT_SUPERBLK);
377 	sblk.b_un.b_buf = Malloc(SBLOCKSIZE);
378 	asblk.b_un.b_buf = Malloc(SBLOCKSIZE);
379 	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
380 		errx(EEXIT, "cannot allocate space for superblock");
381 	dev_bsize = secsize = DEV_BSIZE;
382 }
383 
384 /*
385  * Calculate a prototype superblock based on information in the boot area.
386  * When done the cgsblock macro can be calculated and the fs_ncg field
387  * can be used. Do NOT attempt to use other macros without verifying that
388  * their needed information is available!
389  */
390 static int
391 calcsb(char *dev, int devfd, struct fs *fs)
392 {
393 	struct fsrecovery *fsr;
394 	char *fsrbuf;
395 	u_int secsize;
396 
397 	/*
398 	 * We need fragments-per-group and the partition-size.
399 	 *
400 	 * Newfs stores these details at the end of the boot block area
401 	 * at the start of the filesystem partition. If they have been
402 	 * overwritten by a boot block, we fail. But usually they are
403 	 * there and we can use them.
404 	 */
405 	if (ioctl(devfd, DIOCGSECTORSIZE, &secsize) == -1)
406 		return (0);
407 	fsrbuf = Malloc(secsize);
408 	if (fsrbuf == NULL)
409 		errx(EEXIT, "calcsb: cannot allocate recovery buffer");
410 	if (blread(devfd, fsrbuf,
411 	    (SBLOCK_UFS2 - secsize) / dev_bsize, secsize) != 0) {
412 		free(fsrbuf);
413 		return (0);
414 	}
415 	fsr = (struct fsrecovery *)&fsrbuf[secsize - sizeof *fsr];
416 	if (fsr->fsr_magic != FS_UFS2_MAGIC) {
417 		free(fsrbuf);
418 		return (0);
419 	}
420 	memset(fs, 0, sizeof(struct fs));
421 	fs->fs_fpg = fsr->fsr_fpg;
422 	fs->fs_fsbtodb = fsr->fsr_fsbtodb;
423 	fs->fs_sblkno = fsr->fsr_sblkno;
424 	fs->fs_magic = fsr->fsr_magic;
425 	fs->fs_ncg = fsr->fsr_ncg;
426 	free(fsrbuf);
427 	return (1);
428 }
429 
430 /*
431  * Check to see if recovery information exists.
432  * Return 1 if it exists or cannot be created.
433  * Return 0 if it does not exist and can be created.
434  */
435 static int
436 chkrecovery(int devfd)
437 {
438 	struct fsrecovery *fsr;
439 	char *fsrbuf;
440 	u_int secsize, rdsize;
441 
442 	/*
443 	 * Could not determine if backup material exists, so do not
444 	 * offer to create it.
445 	 */
446 	fsrbuf = NULL;
447 	rdsize = sblock.fs_fsize;
448 	if (ioctl(devfd, DIOCGSECTORSIZE, &secsize) == -1 ||
449 	    rdsize % secsize != 0 ||
450 	    (fsrbuf = Malloc(rdsize)) == NULL ||
451 	    blread(devfd, fsrbuf, (SBLOCK_UFS2 - rdsize) / dev_bsize,
452 	      rdsize) != 0) {
453 		free(fsrbuf);
454 		return (1);
455 	}
456 	/*
457 	 * Recovery material has already been created, so do not
458 	 * need to create it again.
459 	 */
460 	fsr = (struct fsrecovery *)&fsrbuf[rdsize - sizeof *fsr];
461 	if (fsr->fsr_magic == FS_UFS2_MAGIC) {
462 		free(fsrbuf);
463 		return (1);
464 	}
465 	/*
466 	 * Recovery material has not been created and can be if desired.
467 	 */
468 	free(fsrbuf);
469 	return (0);
470 }
471 
472 /*
473  * Read the last filesystem-size piece of the boot block, replace the
474  * last 20 bytes with the recovery information, then write it back.
475  * The recovery information only works for UFS2 filesystems.
476  */
477 static void
478 saverecovery(int readfd, int writefd)
479 {
480 	struct fsrecovery *fsr;
481 	char *fsrbuf;
482 	u_int secsize, rdsize;
483 
484 	fsrbuf = NULL;
485 	rdsize = sblock.fs_fsize;
486 	if (sblock.fs_magic != FS_UFS2_MAGIC ||
487 	    ioctl(readfd, DIOCGSECTORSIZE, &secsize) == -1 ||
488 	    rdsize % secsize != 0 ||
489 	    (fsrbuf = Malloc(rdsize)) == NULL ||
490 	    blread(readfd, fsrbuf, (SBLOCK_UFS2 - rdsize) / dev_bsize,
491 	      rdsize) != 0) {
492 		printf("RECOVERY DATA COULD NOT BE CREATED\n");
493 		free(fsrbuf);
494 		return;
495 	}
496 	fsr = (struct fsrecovery *)&fsrbuf[rdsize - sizeof *fsr];
497 	fsr->fsr_magic = sblock.fs_magic;
498 	fsr->fsr_fpg = sblock.fs_fpg;
499 	fsr->fsr_fsbtodb = sblock.fs_fsbtodb;
500 	fsr->fsr_sblkno = sblock.fs_sblkno;
501 	fsr->fsr_ncg = sblock.fs_ncg;
502 	blwrite(writefd, fsrbuf, (SBLOCK_UFS2 - rdsize) / dev_bsize, rdsize);
503 	free(fsrbuf);
504 }
505