xref: /freebsd/sbin/fsck_ffs/setup.c (revision ee41f1b1cf5e3d4f586cb85b46123b416275862c)
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. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 #if 0
36 static const char sccsid[] = "@(#)setup.c	8.10 (Berkeley) 5/9/95";
37 #endif
38 static const char rcsid[] =
39   "$FreeBSD$";
40 #endif /* not lint */
41 
42 #define DKTYPENAMES
43 #include <sys/param.h>
44 #include <sys/stat.h>
45 #include <sys/disklabel.h>
46 #include <sys/file.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 <string.h>
55 
56 #include "fsck.h"
57 
58 struct bufarea asblk;
59 #define altsblock (*asblk.b_un.b_fs)
60 #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
61 
62 static void badsb __P((int listerr, char *s));
63 static int calcsb __P((char *dev, int devfd, struct fs *fs));
64 static struct disklabel *getdisklabel __P((char *s, int fd));
65 static int readsb __P((int listerr));
66 
67 /*
68  * Read in a superblock finding an alternate if necessary.
69  * Return 1 if successful, 0 if unsuccessful, -1 if filesystem
70  * is already clean (preen mode only).
71  */
72 int
73 setup(dev)
74 	char *dev;
75 {
76 	long cg, size, asked, i, j;
77 	long skipclean, bmapsize;
78 	struct disklabel *lp;
79 	off_t sizepb;
80 	struct stat statb;
81 	struct fs proto;
82 
83 	havesb = 0;
84 	fswritefd = -1;
85 	cursnapshot = 0;
86 	skipclean = fflag ? 0 : preen;
87 	if (stat(dev, &statb) < 0) {
88 		printf("Can't stat %s: %s\n", dev, strerror(errno));
89 		return (0);
90 	}
91 	if ((statb.st_mode & S_IFMT) != S_IFCHR &&
92 	    (statb.st_mode & S_IFMT) != S_IFBLK) {
93 		if ((statb.st_flags & SF_SNAPSHOT) != 0) {
94 			cursnapshot = statb.st_ino;
95 		} else {
96 			pfatal("%s is not a disk device", dev);
97 			if (reply("CONTINUE") == 0)
98 				return (0);
99 		}
100 	}
101 	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
102 		printf("Can't open %s: %s\n", dev, strerror(errno));
103 		return (0);
104 	}
105 	if (preen == 0)
106 		printf("** %s", dev);
107 	if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
108 		fswritefd = -1;
109 		if (preen)
110 			pfatal("NO WRITE ACCESS");
111 		printf(" (NO WRITE)");
112 	}
113 	if (preen == 0)
114 		printf("\n");
115 	fsmodified = 0;
116 	lfdir = 0;
117 	initbarea(&sblk);
118 	initbarea(&asblk);
119 	sblk.b_un.b_buf = malloc(SBSIZE);
120 	asblk.b_un.b_buf = malloc(SBSIZE);
121 	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
122 		errx(EEXIT, "cannot allocate space for superblock");
123 	if ((lp = getdisklabel(NULL, fsreadfd)))
124 		dev_bsize = secsize = lp->d_secsize;
125 	else
126 		dev_bsize = secsize = DEV_BSIZE;
127 	/*
128 	 * Read in the superblock, looking for alternates if necessary
129 	 */
130 	if (readsb(1) == 0) {
131 		skipclean = 0;
132 		if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
133 			return(0);
134 		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
135 			return (0);
136 		for (cg = 0; cg < proto.fs_ncg; cg++) {
137 			bflag = fsbtodb(&proto, cgsblock(&proto, cg));
138 			if (readsb(0) != 0)
139 				break;
140 		}
141 		if (cg >= proto.fs_ncg) {
142 			printf("%s %s\n%s %s\n%s %s\n",
143 				"SEARCH FOR ALTERNATE SUPER-BLOCK",
144 				"FAILED. YOU MUST USE THE",
145 				"-b OPTION TO FSCK TO SPECIFY THE",
146 				"LOCATION OF AN ALTERNATE",
147 				"SUPER-BLOCK TO SUPPLY NEEDED",
148 				"INFORMATION; SEE fsck(8).");
149 			bflag = 0;
150 			return(0);
151 		}
152 		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
153 		bflag = 0;
154 	}
155 	if (skipclean && sblock.fs_clean) {
156 		pwarn("FILESYSTEM CLEAN; SKIPPING CHECKS\n");
157 		return (-1);
158 	}
159 	maxfsblock = sblock.fs_size;
160 	maxino = sblock.fs_ncg * sblock.fs_ipg;
161 	/*
162 	 * Check and potentially fix certain fields in the super block.
163 	 */
164 	if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
165 		pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
166 		if (reply("SET TO DEFAULT") == 1) {
167 			sblock.fs_optim = FS_OPTTIME;
168 			sbdirty();
169 		}
170 	}
171 	if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
172 		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
173 			sblock.fs_minfree);
174 		if (reply("SET TO DEFAULT") == 1) {
175 			sblock.fs_minfree = 10;
176 			sbdirty();
177 		}
178 	}
179 	if (sblock.fs_interleave < 1 ||
180 	    sblock.fs_interleave > sblock.fs_nsect) {
181 		pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
182 			sblock.fs_interleave);
183 		sblock.fs_interleave = 1;
184 		if (preen)
185 			printf(" (FIXED)\n");
186 		if (preen || reply("SET TO DEFAULT") == 1) {
187 			sbdirty();
188 			dirty(&asblk);
189 		}
190 	}
191 	if (sblock.fs_npsect < sblock.fs_nsect ||
192 	    sblock.fs_npsect > sblock.fs_nsect*2) {
193 		pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
194 			sblock.fs_npsect);
195 		sblock.fs_npsect = sblock.fs_nsect;
196 		if (preen)
197 			printf(" (FIXED)\n");
198 		if (preen || reply("SET TO DEFAULT") == 1) {
199 			sbdirty();
200 			dirty(&asblk);
201 		}
202 	}
203 	if (sblock.fs_inodefmt >= FS_44INODEFMT) {
204 		newinofmt = 1;
205 	} else {
206 		sblock.fs_qbmask = ~sblock.fs_bmask;
207 		sblock.fs_qfmask = ~sblock.fs_fmask;
208 		/* This should match the kernel limit in ffs_oldfscompat(). */
209 		sblock.fs_maxfilesize = (u_int64_t)1 << 39;
210 		newinofmt = 0;
211 	}
212 	/*
213 	 * Convert to new inode format.
214 	 */
215 	if (cvtlevel >= 2 && sblock.fs_inodefmt < FS_44INODEFMT) {
216 		if (preen)
217 			pwarn("CONVERTING TO NEW INODE FORMAT\n");
218 		else if (!reply("CONVERT TO NEW INODE FORMAT"))
219 			return(0);
220 		doinglevel2++;
221 		sblock.fs_inodefmt = FS_44INODEFMT;
222 		sizepb = sblock.fs_bsize;
223 		sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
224 		for (i = 0; i < NIADDR; i++) {
225 			sizepb *= NINDIR(&sblock);
226 			sblock.fs_maxfilesize += sizepb;
227 		}
228 		sblock.fs_maxsymlinklen = MAXSYMLINKLEN;
229 		sblock.fs_qbmask = ~sblock.fs_bmask;
230 		sblock.fs_qfmask = ~sblock.fs_fmask;
231 		sbdirty();
232 		dirty(&asblk);
233 	}
234 	/*
235 	 * Convert to new cylinder group format.
236 	 */
237 	if (cvtlevel >= 1 && sblock.fs_postblformat == FS_42POSTBLFMT) {
238 		if (preen)
239 			pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n");
240 		else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT"))
241 			return(0);
242 		doinglevel1++;
243 		sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
244 		sblock.fs_nrpos = 8;
245 		sblock.fs_postbloff =
246 		    (char *)(&sblock.fs_opostbl[0][0]) -
247 		    (char *)(&sblock.fs_firstfield);
248 		sblock.fs_rotbloff = &sblock.fs_space[0] -
249 		    (u_char *)(&sblock.fs_firstfield);
250 		sblock.fs_cgsize =
251 			fragroundup(&sblock, CGSIZE(&sblock));
252 		sbdirty();
253 		dirty(&asblk);
254 	}
255 	if (asblk.b_dirty && !bflag) {
256 		memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize);
257 		flush(fswritefd, &asblk);
258 	}
259 	/*
260 	 * read in the summary info.
261 	 */
262 	asked = 0;
263 	sblock.fs_csp = calloc(1, sblock.fs_cssize);
264 	for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
265 		size = sblock.fs_cssize - i < sblock.fs_bsize ?
266 		    sblock.fs_cssize - i : sblock.fs_bsize;
267 		if (bread(fsreadfd, (char *)sblock.fs_csp + i,
268 		    fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
269 		    size) != 0 && !asked) {
270 			pfatal("BAD SUMMARY INFORMATION");
271 			if (reply("CONTINUE") == 0) {
272 				ckfini(0);
273 				exit(EEXIT);
274 			}
275 			asked++;
276 		}
277 	}
278 	/*
279 	 * allocate and initialize the necessary maps
280 	 */
281 	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(short));
282 	blockmap = calloc((unsigned)bmapsize, sizeof (char));
283 	if (blockmap == NULL) {
284 		printf("cannot alloc %u bytes for blockmap\n",
285 		    (unsigned)bmapsize);
286 		goto badsb;
287 	}
288 	inostathead = calloc((unsigned)(sblock.fs_ncg),
289 	    sizeof(struct inostatlist));
290 	if (inostathead == NULL) {
291 		printf("cannot alloc %u bytes for inostathead\n",
292 		    (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg)));
293 		goto badsb;
294 	}
295 	numdirs = sblock.fs_cstotal.cs_ndir;
296 	dirhash = numdirs;
297 	if (numdirs == 0) {
298 		printf("numdirs is zero, try using an alternate superblock\n");
299 		goto badsb;
300 	}
301 	inplast = 0;
302 	listmax = numdirs + 10;
303 	inpsort = (struct inoinfo **)calloc((unsigned)listmax,
304 	    sizeof(struct inoinfo *));
305 	inphead = (struct inoinfo **)calloc((unsigned)numdirs,
306 	    sizeof(struct inoinfo *));
307 	if (inpsort == NULL || inphead == NULL) {
308 		printf("cannot alloc %u bytes for inphead\n",
309 		    (unsigned)numdirs * sizeof(struct inoinfo *));
310 		goto badsb;
311 	}
312 	bufinit();
313 	if (sblock.fs_flags & FS_DOSOFTDEP)
314 		usedsoftdep = 1;
315 	else
316 		usedsoftdep = 0;
317 	return (1);
318 
319 badsb:
320 	ckfini(0);
321 	return (0);
322 }
323 
324 /*
325  * Read in the super block and its summary info.
326  */
327 static int
328 readsb(listerr)
329 	int listerr;
330 {
331 	ufs_daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
332 
333 	if (bread(fsreadfd, (char *)&sblock, super, (long)SBSIZE) != 0)
334 		return (0);
335 	sblk.b_bno = super;
336 	sblk.b_size = SBSIZE;
337 	/*
338 	 * run a few consistency checks of the super block
339 	 */
340 	if (sblock.fs_magic != FS_MAGIC)
341 		{ badsb(listerr, "MAGIC NUMBER WRONG"); return (0); }
342 	if (sblock.fs_ncg < 1)
343 		{ badsb(listerr, "NCG OUT OF RANGE"); return (0); }
344 	if (sblock.fs_cpg < 1)
345 		{ badsb(listerr, "CPG OUT OF RANGE"); return (0); }
346 	if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl ||
347 	    (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl)
348 		{ badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); }
349 	if (sblock.fs_sbsize > SBSIZE)
350 		{ badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
351 	/*
352 	 * Compute block size that the filesystem is based on,
353 	 * according to fsbtodb, and adjust superblock block number
354 	 * so we can tell if this is an alternate later.
355 	 */
356 	super *= dev_bsize;
357 	dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
358 	sblk.b_bno = super / dev_bsize;
359 	if (bflag) {
360 		havesb = 1;
361 		return (1);
362 	}
363 	/*
364 	 * Set all possible fields that could differ, then do check
365 	 * of whole super block against an alternate super block.
366 	 * When an alternate super-block is specified this check is skipped.
367 	 */
368 	getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
369 	if (asblk.b_errs)
370 		return (0);
371 	altsblock.fs_firstfield = sblock.fs_firstfield;
372 	altsblock.fs_unused_1 = sblock.fs_unused_1;
373 	altsblock.fs_time = sblock.fs_time;
374 	altsblock.fs_cstotal = sblock.fs_cstotal;
375 	altsblock.fs_cgrotor = sblock.fs_cgrotor;
376 	altsblock.fs_fmod = sblock.fs_fmod;
377 	altsblock.fs_clean = sblock.fs_clean;
378 	altsblock.fs_ronly = sblock.fs_ronly;
379 	altsblock.fs_flags = sblock.fs_flags;
380 	altsblock.fs_maxcontig = sblock.fs_maxcontig;
381 	altsblock.fs_minfree = sblock.fs_minfree;
382 	altsblock.fs_optim = sblock.fs_optim;
383 	altsblock.fs_rotdelay = sblock.fs_rotdelay;
384 	altsblock.fs_maxbpg = sblock.fs_maxbpg;
385 	memmove(altsblock.fs_ocsp, sblock.fs_ocsp, sizeof sblock.fs_ocsp);
386 	altsblock.fs_csp = sblock.fs_csp;
387 	altsblock.fs_maxcluster = sblock.fs_maxcluster;
388 	memmove(altsblock.fs_fsmnt, sblock.fs_fsmnt, sizeof sblock.fs_fsmnt);
389 	memmove(altsblock.fs_snapinum, sblock.fs_snapinum,
390 		sizeof sblock.fs_snapinum);
391 	memmove(altsblock.fs_sparecon,
392 		sblock.fs_sparecon, sizeof sblock.fs_sparecon);
393 	/*
394 	 * The following should not have to be copied.
395 	 */
396 	altsblock.fs_fsbtodb = sblock.fs_fsbtodb;
397 	altsblock.fs_interleave = sblock.fs_interleave;
398 	altsblock.fs_npsect = sblock.fs_npsect;
399 	altsblock.fs_nrpos = sblock.fs_nrpos;
400 	altsblock.fs_state = sblock.fs_state;
401 	altsblock.fs_qbmask = sblock.fs_qbmask;
402 	altsblock.fs_qfmask = sblock.fs_qfmask;
403 	altsblock.fs_state = sblock.fs_state;
404 	altsblock.fs_maxfilesize = sblock.fs_maxfilesize;
405 	if (memcmp(&sblock, &altsblock, (int)sblock.fs_sbsize)) {
406 		if (debug) {
407 			long *nlp, *olp, *endlp;
408 
409 			printf("superblock mismatches\n");
410 			nlp = (long *)&altsblock;
411 			olp = (long *)&sblock;
412 			endlp = olp + (sblock.fs_sbsize / sizeof *olp);
413 			for ( ; olp < endlp; olp++, nlp++) {
414 				if (*olp == *nlp)
415 					continue;
416 				printf(
417 				    "offset %d, original %ld, alternate %ld\n",
418 				    olp - (long *)&sblock, *olp, *nlp);
419 			}
420 		}
421 		badsb(listerr,
422 		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
423 		return (0);
424 	}
425 	havesb = 1;
426 	return (1);
427 }
428 
429 static void
430 badsb(listerr, s)
431 	int listerr;
432 	char *s;
433 {
434 
435 	if (!listerr)
436 		return;
437 	if (preen)
438 		printf("%s: ", cdevname);
439 	pfatal("BAD SUPER BLOCK: %s\n", s);
440 }
441 
442 /*
443  * Calculate a prototype superblock based on information in the disk label.
444  * When done the cgsblock macro can be calculated and the fs_ncg field
445  * can be used. Do NOT attempt to use other macros without verifying that
446  * their needed information is available!
447  */
448 static int
449 calcsb(dev, devfd, fs)
450 	char *dev;
451 	int devfd;
452 	register struct fs *fs;
453 {
454 	register struct disklabel *lp;
455 	register struct partition *pp;
456 	register char *cp;
457 	int i;
458 
459 	cp = strchr(dev, '\0') - 1;
460 	if (cp == (char *)-1 || ((*cp < 'a' || *cp > 'h') && !isdigit(*cp))) {
461 		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
462 		return (0);
463 	}
464 	lp = getdisklabel(dev, devfd);
465 	if (isdigit(*cp))
466 		pp = &lp->d_partitions[0];
467 	else
468 		pp = &lp->d_partitions[*cp - 'a'];
469 	if (pp->p_fstype != FS_BSDFFS) {
470 		pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
471 			dev, pp->p_fstype < FSMAXTYPES ?
472 			fstypenames[pp->p_fstype] : "unknown");
473 		return (0);
474 	}
475 	if (pp->p_fsize == 0 || pp->p_frag == 0 ||
476 	    pp->p_cpg == 0 || pp->p_size == 0) {
477 		pfatal("%s: %s: type %s fsize %d, frag %d, cpg %d, size %d\n",
478 		    dev, "INCOMPLETE LABEL", fstypenames[pp->p_fstype],
479 		    pp->p_fsize, pp->p_frag, pp->p_cpg, pp->p_size);
480 		return (0);
481 	}
482 	memset(fs, 0, sizeof(struct fs));
483 	fs->fs_fsize = pp->p_fsize;
484 	fs->fs_frag = pp->p_frag;
485 	fs->fs_cpg = pp->p_cpg;
486 	fs->fs_size = pp->p_size;
487 	fs->fs_ntrak = lp->d_ntracks;
488 	fs->fs_nsect = lp->d_nsectors;
489 	fs->fs_spc = lp->d_secpercyl;
490 	fs->fs_nspf = fs->fs_fsize / lp->d_secsize;
491 	fs->fs_sblkno = roundup(
492 		howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
493 		fs->fs_frag);
494 	fs->fs_cgmask = 0xffffffff;
495 	for (i = fs->fs_ntrak; i > 1; i >>= 1)
496 		fs->fs_cgmask <<= 1;
497 	if (!POWEROF2(fs->fs_ntrak))
498 		fs->fs_cgmask <<= 1;
499 	fs->fs_cgoffset = roundup(
500 		howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag);
501 	fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs);
502 	fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg);
503 	for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1)
504 		fs->fs_fsbtodb++;
505 	dev_bsize = lp->d_secsize;
506 	return (1);
507 }
508 
509 static struct disklabel *
510 getdisklabel(s, fd)
511 	char *s;
512 	int	fd;
513 {
514 	static struct disklabel lab;
515 
516 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
517 		if (s == NULL)
518 			return ((struct disklabel *)NULL);
519 		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
520 		errx(EEXIT, "%s: can't read disk label", s);
521 	}
522 	return (&lab);
523 }
524