xref: /freebsd/sbin/fsck_ffs/setup.c (revision d2ba5111c125104b09aa1acd1bfe8af2a24c79cc)
1 /*
2  * Copyright (c) 1980, 1986, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #if 0
31 #ifndef lint
32 static const char sccsid[] = "@(#)setup.c	8.10 (Berkeley) 5/9/95";
33 #endif /* not lint */
34 #endif
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <sys/param.h>
39 #include <sys/stat.h>
40 #define FSTYPENAMES
41 #include <sys/disklabel.h>
42 #include <sys/file.h>
43 #include <sys/sysctl.h>
44 
45 #include <ufs/ufs/dinode.h>
46 #include <ufs/ffs/fs.h>
47 
48 #include <ctype.h>
49 #include <err.h>
50 #include <errno.h>
51 #include <limits.h>
52 #include <stdint.h>
53 #include <string.h>
54 
55 #include "fsck.h"
56 
57 struct bufarea asblk;
58 #define altsblock (*asblk.b_un.b_fs)
59 #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
60 
61 static void badsb(int listerr, const char *s);
62 
63 /*
64  * Read in a superblock finding an alternate if necessary.
65  * Return 1 if successful, 0 if unsuccessful, -1 if file system
66  * is already clean (ckclean and preen mode only).
67  */
68 int
69 setup(char *dev)
70 {
71 	long asked, i, j;
72 	long bmapsize;
73 	struct stat statb;
74 	size_t size;
75 
76 	havesb = 0;
77 	fswritefd = -1;
78 	cursnapshot = 0;
79 	if (stat(dev, &statb) < 0) {
80 		printf("Can't stat %s: %s\n", dev, strerror(errno));
81 		if (bkgrdflag) {
82 			unlink(snapname);
83 			bkgrdflag = 0;
84 		}
85 		return (0);
86 	}
87 	if ((statb.st_mode & S_IFMT) != S_IFCHR &&
88 	    (statb.st_mode & S_IFMT) != S_IFBLK) {
89 		if (bkgrdflag != 0 && (statb.st_flags & SF_SNAPSHOT) == 0) {
90 			unlink(snapname);
91 			printf("background fsck lacks a snapshot\n");
92 			exit(EEXIT);
93 		}
94 		if ((statb.st_flags & SF_SNAPSHOT) != 0 && cvtlevel == 0) {
95 			cursnapshot = statb.st_ino;
96 		} else {
97 			if (cvtlevel == 0 ||
98 			    (statb.st_flags & SF_SNAPSHOT) == 0) {
99 				if (preen && bkgrdflag) {
100 					unlink(snapname);
101 					bkgrdflag = 0;
102 				}
103 				pfatal("%s is not a disk device", dev);
104 				if (reply("CONTINUE") == 0) {
105 					if (bkgrdflag) {
106 						unlink(snapname);
107 						bkgrdflag = 0;
108 					}
109 					return (0);
110 				}
111 			} else {
112 				if (bkgrdflag) {
113 					unlink(snapname);
114 					bkgrdflag = 0;
115 				}
116 				pfatal("cannot convert a snapshot");
117 				exit(EEXIT);
118 			}
119 		}
120 	}
121 	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
122 		if (bkgrdflag) {
123 			unlink(snapname);
124 			bkgrdflag = 0;
125 		}
126 		printf("Can't open %s: %s\n", dev, strerror(errno));
127 		return (0);
128 	}
129 	if (bkgrdflag) {
130 		unlink(snapname);
131 		size = MIBSIZE;
132 		if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0||
133 		    sysctlnametomib("vfs.ffs.adjblkcnt", adjblkcnt, &size) < 0||
134 		    sysctlnametomib("vfs.ffs.freefiles", freefiles, &size) < 0||
135 		    sysctlnametomib("vfs.ffs.freedirs", freedirs, &size) < 0 ||
136 		    sysctlnametomib("vfs.ffs.freeblks", freeblks, &size) < 0) {
137 			pfatal("kernel lacks background fsck support\n");
138 			exit(EEXIT);
139 		}
140 		/*
141 		 * When kernel is lack of runtime bgfsck superblock summary
142 		 * adjustment functionality, it does not mean we can not
143 		 * continue, as old kernels will recompute the summary at
144 		 * mount time.  However, it will be an unexpected softupdates
145 		 * inconsistency if it turns out that the summary is still
146 		 * incorrect.  Set a flag so subsequent operation can know
147 		 * this.
148 		 */
149 		bkgrdsumadj = 1;
150 		if (sysctlnametomib("vfs.ffs.adjndir", adjndir, &size) < 0 ||
151 		    sysctlnametomib("vfs.ffs.adjnbfree", adjnbfree, &size) < 0 ||
152 		    sysctlnametomib("vfs.ffs.adjnifree", adjnifree, &size) < 0 ||
153 		    sysctlnametomib("vfs.ffs.adjnffree", adjnffree, &size) < 0 ||
154 		    sysctlnametomib("vfs.ffs.adjnumclusters", adjnumclusters, &size) < 0) {
155 			bkgrdsumadj = 0;
156 			pwarn("kernel lacks runtime superblock summary adjustment support");
157 		}
158 		cmd.version = FFS_CMD_VERSION;
159 		cmd.handle = fsreadfd;
160 		fswritefd = -1;
161 	}
162 	if (preen == 0)
163 		printf("** %s", dev);
164 	if (bkgrdflag == 0 &&
165 	    (nflag || (fswritefd = open(dev, O_WRONLY)) < 0)) {
166 		fswritefd = -1;
167 		if (preen)
168 			pfatal("NO WRITE ACCESS");
169 		printf(" (NO WRITE)");
170 	}
171 	if (preen == 0)
172 		printf("\n");
173 	/*
174 	 * Read in the superblock, looking for alternates if necessary
175 	 */
176 	if (readsb(1) == 0) {
177 		skipclean = 0;
178 		if (bflag || preen)
179 			return(0);
180 		/* Looking for alternates is hard punt for now but retain structure */
181 		return (0);
182 	}
183 	if (skipclean && ckclean && sblock.fs_clean) {
184 		pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
185 		return (-1);
186 	}
187 	maxfsblock = sblock.fs_size;
188 	maxino = sblock.fs_ncg * sblock.fs_ipg;
189 	/*
190 	 * Check and potentially fix certain fields in the super block.
191 	 */
192 	if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
193 		pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
194 		if (reply("SET TO DEFAULT") == 1) {
195 			sblock.fs_optim = FS_OPTTIME;
196 			sbdirty();
197 		}
198 	}
199 	if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
200 		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
201 			sblock.fs_minfree);
202 		if (reply("SET TO DEFAULT") == 1) {
203 			sblock.fs_minfree = 10;
204 			sbdirty();
205 		}
206 	}
207 	if (sblock.fs_magic == FS_UFS1_MAGIC &&
208 	    sblock.fs_old_inodefmt < FS_44INODEFMT) {
209 		pwarn("Format of file system is too old.\n");
210 		pwarn("Must update to modern format using a version of fsck\n");
211 		pfatal("from before 2002 with the command ``fsck -c 2''\n");
212 		exit(EEXIT);
213 	}
214 	if (asblk.b_dirty && !bflag) {
215 		memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize);
216 		flush(fswritefd, &asblk);
217 	}
218 	/*
219 	 * read in the summary info.
220 	 */
221 	asked = 0;
222 	sblock.fs_csp = Calloc(1, sblock.fs_cssize);
223 	if (sblock.fs_csp == NULL) {
224 		printf("cannot alloc %u bytes for cg summary info\n",
225 		    (unsigned)sblock.fs_cssize);
226 		goto badsb;
227 	}
228 	for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
229 		size = MIN(sblock.fs_cssize - i, sblock.fs_bsize);
230 		readcnt[sblk.b_type]++;
231 		if (blread(fsreadfd, (char *)sblock.fs_csp + i,
232 		    fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
233 		    size) != 0 && !asked) {
234 			pfatal("BAD SUMMARY INFORMATION");
235 			if (reply("CONTINUE") == 0) {
236 				ckfini(0);
237 				exit(EEXIT);
238 			}
239 			asked++;
240 		}
241 	}
242 	/*
243 	 * allocate and initialize the necessary maps
244 	 */
245 	bmapsize = roundup(howmany(maxfsblock, CHAR_BIT), sizeof(short));
246 	blockmap = Calloc((unsigned)bmapsize, sizeof (char));
247 	if (blockmap == NULL) {
248 		printf("cannot alloc %u bytes for blockmap\n",
249 		    (unsigned)bmapsize);
250 		goto badsb;
251 	}
252 	inostathead = Calloc(sblock.fs_ncg, sizeof(struct inostatlist));
253 	if (inostathead == NULL) {
254 		printf("cannot alloc %u bytes for inostathead\n",
255 		    (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg)));
256 		goto badsb;
257 	}
258 	numdirs = MAX(sblock.fs_cstotal.cs_ndir, 128);
259 	dirhash = numdirs;
260 	inplast = 0;
261 	listmax = numdirs + 10;
262 	inpsort = (struct inoinfo **)Calloc(listmax, sizeof(struct inoinfo *));
263 	inphead = (struct inoinfo **)Calloc(numdirs, sizeof(struct inoinfo *));
264 	if (inpsort == NULL || inphead == NULL) {
265 		printf("cannot alloc %ju bytes for inphead\n",
266 		    (uintmax_t)numdirs * sizeof(struct inoinfo *));
267 		goto badsb;
268 	}
269 	bufinit();
270 	if (sblock.fs_flags & FS_DOSOFTDEP)
271 		usedsoftdep = 1;
272 	else
273 		usedsoftdep = 0;
274 	return (1);
275 
276 badsb:
277 	ckfini(0);
278 	return (0);
279 }
280 
281 /*
282  * Possible superblock locations ordered from most to least likely.
283  */
284 static int sblock_try[] = SBLOCKSEARCH;
285 
286 #define BAD_MAGIC_MSG \
287 "The previous newfs operation on this volume did not complete.\n" \
288 "You must complete newfs before mounting this volume.\n"
289 
290 /*
291  * Read in the super block and its summary info.
292  */
293 int
294 readsb(int listerr)
295 {
296 	ufs2_daddr_t super;
297 	int i, bad;
298 
299 	if (bflag) {
300 		super = bflag;
301 		readcnt[sblk.b_type]++;
302 		if ((blread(fsreadfd, (char *)&sblock, super, (long)SBLOCKSIZE)))
303 			return (0);
304 		if (sblock.fs_magic == FS_BAD_MAGIC) {
305 			fprintf(stderr, BAD_MAGIC_MSG);
306 			exit(11);
307 		}
308 		if (sblock.fs_magic != FS_UFS1_MAGIC &&
309 		    sblock.fs_magic != FS_UFS2_MAGIC) {
310 			fprintf(stderr, "%jd is not a file system superblock\n",
311 			    bflag);
312 			return (0);
313 		}
314 	} else {
315 		for (i = 0; sblock_try[i] != -1; i++) {
316 			super = sblock_try[i] / dev_bsize;
317 			readcnt[sblk.b_type]++;
318 			if ((blread(fsreadfd, (char *)&sblock, super,
319 			    (long)SBLOCKSIZE)))
320 				return (0);
321 			if (sblock.fs_magic == FS_BAD_MAGIC) {
322 				fprintf(stderr, BAD_MAGIC_MSG);
323 				exit(11);
324 			}
325 			if ((sblock.fs_magic == FS_UFS1_MAGIC ||
326 			     (sblock.fs_magic == FS_UFS2_MAGIC &&
327 			      sblock.fs_sblockloc == sblock_try[i])) &&
328 			    sblock.fs_ncg >= 1 &&
329 			    sblock.fs_bsize >= MINBSIZE &&
330 			    sblock.fs_sbsize >= roundup(sizeof(struct fs), dev_bsize))
331 				break;
332 		}
333 		if (sblock_try[i] == -1) {
334 			fprintf(stderr, "Cannot find file system superblock\n");
335 			return (0);
336 		}
337 	}
338 	/*
339 	 * Compute block size that the file system is based on,
340 	 * according to fsbtodb, and adjust superblock block number
341 	 * so we can tell if this is an alternate later.
342 	 */
343 	super *= dev_bsize;
344 	dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
345 	sblk.b_bno = super / dev_bsize;
346 	sblk.b_size = SBLOCKSIZE;
347 	/*
348 	 * Compare all fields that should not differ in alternate super block.
349 	 * When an alternate super-block is specified this check is skipped.
350 	 */
351 	if (bflag)
352 		goto out;
353 	getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
354 	if (asblk.b_errs)
355 		return (0);
356 	bad = 0;
357 #define CHK(x, y)				\
358 	if (altsblock.x != sblock.x) {		\
359 		bad++;				\
360 		if (listerr && debug)		\
361 			printf("SUPER BLOCK VS ALTERNATE MISMATCH %s: " y " vs " y "\n", \
362 			    #x, (intmax_t)sblock.x, (intmax_t)altsblock.x); \
363 	}
364 	CHK(fs_sblkno, "%jd");
365 	CHK(fs_cblkno, "%jd");
366 	CHK(fs_iblkno, "%jd");
367 	CHK(fs_dblkno, "%jd");
368 	CHK(fs_ncg, "%jd");
369 	CHK(fs_bsize, "%jd");
370 	CHK(fs_fsize, "%jd");
371 	CHK(fs_frag, "%jd");
372 	CHK(fs_bmask, "%#jx");
373 	CHK(fs_fmask, "%#jx");
374 	CHK(fs_bshift, "%jd");
375 	CHK(fs_fshift, "%jd");
376 	CHK(fs_fragshift, "%jd");
377 	CHK(fs_fsbtodb, "%jd");
378 	CHK(fs_sbsize, "%jd");
379 	CHK(fs_nindir, "%jd");
380 	CHK(fs_inopb, "%jd");
381 	CHK(fs_cssize, "%jd");
382 	CHK(fs_ipg, "%jd");
383 	CHK(fs_fpg, "%jd");
384 	CHK(fs_magic, "%#jx");
385 #undef CHK
386 	if (bad) {
387 		if (listerr == 0)
388 			return (0);
389 		if (preen)
390 			printf("%s: ", cdevname);
391 		printf(
392 		    "VALUES IN SUPER BLOCK LSB=%jd DISAGREE WITH THOSE IN\n"
393 		    "LAST ALTERNATE LSB=%jd\n",
394 		    sblk.b_bno, asblk.b_bno);
395 		if (reply("IGNORE ALTERNATE SUPER BLOCK") == 0)
396 			return (0);
397 	}
398 out:
399 	/*
400 	 * If not yet done, update UFS1 superblock with new wider fields.
401 	 */
402 	if (sblock.fs_magic == FS_UFS1_MAGIC &&
403 	    sblock.fs_maxbsize != sblock.fs_bsize) {
404 		sblock.fs_maxbsize = sblock.fs_bsize;
405 		sblock.fs_time = sblock.fs_old_time;
406 		sblock.fs_size = sblock.fs_old_size;
407 		sblock.fs_dsize = sblock.fs_old_dsize;
408 		sblock.fs_csaddr = sblock.fs_old_csaddr;
409 		sblock.fs_cstotal.cs_ndir = sblock.fs_old_cstotal.cs_ndir;
410 		sblock.fs_cstotal.cs_nbfree = sblock.fs_old_cstotal.cs_nbfree;
411 		sblock.fs_cstotal.cs_nifree = sblock.fs_old_cstotal.cs_nifree;
412 		sblock.fs_cstotal.cs_nffree = sblock.fs_old_cstotal.cs_nffree;
413 	}
414 	havesb = 1;
415 	return (1);
416 }
417 
418 void
419 sblock_init(void)
420 {
421 
422 	fswritefd = -1;
423 	fsmodified = 0;
424 	lfdir = 0;
425 	initbarea(&sblk, BT_SUPERBLK);
426 	initbarea(&asblk, BT_SUPERBLK);
427 	sblk.b_un.b_buf = Malloc(SBLOCKSIZE);
428 	asblk.b_un.b_buf = Malloc(SBLOCKSIZE);
429 	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
430 		errx(EEXIT, "cannot allocate space for superblock");
431 	dev_bsize = secsize = DEV_BSIZE;
432 }
433