xref: /netbsd-src/sbin/fsck_ffs/setup.c (revision bcc8ec9959e7b01e313d813067bfb43a3ad70551)
1 /*	$NetBSD: setup.c,v 1.40 2001/01/09 06:05:10 mycroft Exp $	*/
2 
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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)setup.c	8.10 (Berkeley) 5/9/95";
40 #else
41 __RCSID("$NetBSD: setup.c,v 1.40 2001/01/09 06:05:10 mycroft Exp $");
42 #endif
43 #endif /* not lint */
44 
45 #include <sys/param.h>
46 #include <sys/time.h>
47 #include <sys/stat.h>
48 #include <sys/ioctl.h>
49 #define FSTYPENAMES
50 #include <sys/disklabel.h>
51 #include <sys/file.h>
52 
53 #include <ufs/ufs/dinode.h>
54 #include <ufs/ufs/ufs_bswap.h>
55 #include <ufs/ffs/fs.h>
56 #include <ufs/ffs/ffs_extern.h>
57 
58 #include <ctype.h>
59 #include <err.h>
60 #include <errno.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 
65 #include "fsck.h"
66 #include "extern.h"
67 #include "fsutil.h"
68 
69 struct bufarea asblk;
70 struct fs *altsblock;
71 #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
72 
73 static void badsb __P((int, char *));
74 static int calcsb __P((const char *, int, struct fs *));
75 static struct disklabel *getdisklabel __P((const char *, int));
76 static int readsb __P((int));
77 
78 /*
79  * Read in a superblock finding an alternate if necessary.
80  * Return 1 if successful, 0 if unsuccessful, -1 if filesystem
81  * is already clean (preen mode only).
82  */
83 int
84 setup(dev)
85 	const char *dev;
86 {
87 	long cg, size, asked, i, j;
88 	long bmapsize;
89 	struct disklabel *lp;
90 	off_t sizepb;
91 	struct stat statb;
92 	struct fs proto;
93 	int doskipclean;
94 	u_int64_t maxfilesize;
95 
96 	havesb = 0;
97 	fswritefd = -1;
98 	doskipclean = skipclean;
99 	if (stat(dev, &statb) < 0) {
100 		printf("Can't stat %s: %s\n", dev, strerror(errno));
101 		return (0);
102 	}
103 	if (!S_ISCHR(statb.st_mode)) {
104 		pfatal("%s is not a character device", dev);
105 		if (reply("CONTINUE") == 0)
106 			return (0);
107 	}
108 	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
109 		printf("Can't open %s: %s\n", dev, strerror(errno));
110 		return (0);
111 	}
112 	if (preen == 0)
113 		printf("** %s", dev);
114 	if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
115 		fswritefd = -1;
116 		if (preen)
117 			pfatal("NO WRITE ACCESS");
118 		printf(" (NO WRITE)");
119 	}
120 	if (preen == 0)
121 		printf("\n");
122 	fsmodified = 0;
123 	lfdir = 0;
124 	initbarea(&sblk);
125 	initbarea(&asblk);
126 	sblk.b_un.b_buf = malloc(SBSIZE);
127 	sblock = malloc(SBSIZE);
128 	asblk.b_un.b_buf = malloc(SBSIZE);
129 	altsblock = malloc(SBSIZE);
130 	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL ||
131 		sblock == NULL || altsblock == NULL)
132 		errx(EEXIT, "cannot allocate space for superblock");
133 	if ((lp = getdisklabel(NULL, fsreadfd)) != NULL)
134 		dev_bsize = secsize = lp->d_secsize;
135 	else
136 		dev_bsize = secsize = DEV_BSIZE;
137 	/*
138 	 * Read in the superblock, looking for alternates if necessary
139 	 */
140 	if (readsb(1) == 0) {
141 		if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
142 			return(0);
143 		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
144 			return (0);
145 		for (cg = 0; cg < proto.fs_ncg; cg++) {
146 			bflag = fsbtodb(&proto, cgsblock(&proto, cg));
147 			if (readsb(0) != 0)
148 				break;
149 		}
150 		if (cg >= proto.fs_ncg) {
151 			printf("%s %s\n%s %s\n%s %s\n",
152 				"SEARCH FOR ALTERNATE SUPER-BLOCK",
153 				"FAILED. YOU MUST USE THE",
154 				"-b OPTION TO FSCK_FFS TO SPECIFY THE",
155 				"LOCATION OF AN ALTERNATE",
156 				"SUPER-BLOCK TO SUPPLY NEEDED",
157 				"INFORMATION; SEE fsck_ffs(8).");
158 			return(0);
159 		}
160 		doskipclean = 0;
161 		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
162 	}
163 	if (debug)
164 		printf("clean = %d\n", sblock->fs_clean);
165 	if (doswap)
166 		doskipclean = 0;
167 	if (sblock->fs_clean & FS_ISCLEAN) {
168 		if (doskipclean) {
169 			pwarn("%sile system is clean; not checking\n",
170 			    preen ? "f" : "** F");
171 			return (-1);
172 		}
173 		if (!preen && !doswap)
174 			pwarn("** File system is already clean\n");
175 	}
176 	maxfsblock = sblock->fs_size;
177 	maxino = sblock->fs_ncg * sblock->fs_ipg;
178 	sizepb = sblock->fs_bsize;
179 	maxfilesize = sblock->fs_bsize * NDADDR - 1;
180 	for (i = 0; i < NIADDR; i++) {
181 		sizepb *= NINDIR(sblock);
182 		maxfilesize += sizepb;
183 	}
184 	/*
185 	 * Check and potentially fix certain fields in the super block.
186 	 */
187 	if (sblock->fs_optim != FS_OPTTIME && sblock->fs_optim != FS_OPTSPACE) {
188 		pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
189 		if (reply("SET TO DEFAULT") == 1) {
190 			sblock->fs_optim = FS_OPTTIME;
191 			sbdirty();
192 		}
193 	}
194 	if ((sblock->fs_minfree < 0 || sblock->fs_minfree > 99)) {
195 		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
196 			sblock->fs_minfree);
197 		if (reply("SET TO DEFAULT") == 1) {
198 			sblock->fs_minfree = 10;
199 			sbdirty();
200 		}
201 	}
202 	if (sblock->fs_interleave < 1 ||
203 	    sblock->fs_interleave > sblock->fs_nsect) {
204 		pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
205 			sblock->fs_interleave);
206 		sblock->fs_interleave = 1;
207 		if (preen)
208 			printf(" (FIXED)\n");
209 		if (preen || reply("SET TO DEFAULT") == 1) {
210 			sbdirty();
211 			dirty(&asblk);
212 		}
213 	}
214 	if (sblock->fs_npsect < sblock->fs_nsect ||
215 	    sblock->fs_npsect > sblock->fs_nsect*2) {
216 		pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
217 			sblock->fs_npsect);
218 		sblock->fs_npsect = sblock->fs_nsect;
219 		if (preen)
220 			printf(" (FIXED)\n");
221 		if (preen || reply("SET TO DEFAULT") == 1) {
222 			sbdirty();
223 			dirty(&asblk);
224 		}
225 	}
226 	if (sblock->fs_bmask != ~(sblock->fs_bsize - 1)) {
227 		pwarn("INCORRECT BMASK=0x%x IN SUPERBLOCK",
228 			sblock->fs_bmask);
229 		sblock->fs_bmask = ~(sblock->fs_bsize - 1);
230 		if (preen)
231 			printf(" (FIXED)\n");
232 		if (preen || reply("FIX") == 1) {
233 			sbdirty();
234 			dirty(&asblk);
235 		}
236 	}
237 	if (sblock->fs_fmask != ~(sblock->fs_fsize - 1)) {
238 		pwarn("INCORRECT FMASK=0x%x IN SUPERBLOCK",
239 			sblock->fs_fmask);
240 		sblock->fs_fmask = ~(sblock->fs_fsize - 1);
241 		if (preen)
242 			printf(" (FIXED)\n");
243 		if (preen || reply("FIX") == 1) {
244 			sbdirty();
245 			dirty(&asblk);
246 		}
247 	}
248 	if (sblock->fs_inodefmt >= FS_44INODEFMT) {
249 		if (sblock->fs_maxfilesize != maxfilesize) {
250 			pwarn("INCORRECT MAXFILESIZE=%lld IN SUPERBLOCK",
251 			    (unsigned long long)sblock->fs_maxfilesize);
252 			sblock->fs_maxfilesize = maxfilesize;
253 			if (preen)
254 				printf(" (FIXED)\n");
255 			if (preen || reply("FIX") == 1) {
256 				sbdirty();
257 				dirty(&asblk);
258 			}
259 		}
260 		if (sblock->fs_maxsymlinklen != MAXSYMLINKLEN) {
261 			pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK",
262 				sblock->fs_maxsymlinklen);
263 			sblock->fs_maxsymlinklen = MAXSYMLINKLEN;
264 			if (preen)
265 				printf(" (FIXED)\n");
266 			if (preen || reply("FIX") == 1) {
267 				sbdirty();
268 				dirty(&asblk);
269 			}
270 		}
271 		if (sblock->fs_qbmask != ~sblock->fs_bmask) {
272 			pwarn("INCORRECT QBMASK=%llx IN SUPERBLOCK",
273 			    (unsigned long long)sblock->fs_qbmask);
274 			sblock->fs_qbmask = ~sblock->fs_bmask;
275 			if (preen)
276 				printf(" (FIXED)\n");
277 			if (preen || reply("FIX") == 1) {
278 				sbdirty();
279 				dirty(&asblk);
280 			}
281 		}
282 		if (sblock->fs_qfmask != ~sblock->fs_fmask) {
283 			pwarn("INCORRECT QFMASK=%llx IN SUPERBLOCK",
284 			    (unsigned long long)sblock->fs_qfmask);
285 			sblock->fs_qfmask = ~sblock->fs_fmask;
286 			if (preen)
287 				printf(" (FIXED)\n");
288 			if (preen || reply("FIX") == 1) {
289 				sbdirty();
290 				dirty(&asblk);
291 			}
292 		}
293 		newinofmt = 1;
294 	} else {
295 		sblock->fs_qbmask = ~sblock->fs_bmask;
296 		sblock->fs_qfmask = ~sblock->fs_fmask;
297 		newinofmt = 0;
298 	}
299 	/*
300 	 * Convert to new inode format.
301 	 */
302 	if (cvtlevel >= 2 && sblock->fs_inodefmt < FS_44INODEFMT) {
303 		if (preen)
304 			pwarn("CONVERTING TO NEW INODE FORMAT\n");
305 		else if (!reply("CONVERT TO NEW INODE FORMAT"))
306 			return(0);
307 		doinglevel2++;
308 		sblock->fs_inodefmt = FS_44INODEFMT;
309 		sblock->fs_maxfilesize = maxfilesize;
310 		sblock->fs_maxsymlinklen = MAXSYMLINKLEN;
311 		sblock->fs_qbmask = ~sblock->fs_bmask;
312 		sblock->fs_qfmask = ~sblock->fs_fmask;
313 		sbdirty();
314 		dirty(&asblk);
315 	}
316 	/*
317 	 * Convert to new cylinder group format.
318 	 */
319 	if (cvtlevel >= 1 && sblock->fs_postblformat == FS_42POSTBLFMT) {
320 		if (preen)
321 			pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n");
322 		else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT"))
323 			return(0);
324 		doinglevel1++;
325 		sblock->fs_postblformat = FS_DYNAMICPOSTBLFMT;
326 		sblock->fs_nrpos = 8;
327 		sblock->fs_postbloff =
328 		    (char *)(&sblock->fs_opostbl[0][0]) -
329 		    (char *)(&sblock->fs_firstfield);
330 		sblock->fs_rotbloff = &sblock->fs_space[0] -
331 		    (u_char *)(&sblock->fs_firstfield);
332 		sblock->fs_cgsize =
333 			fragroundup(sblock, CGSIZE(sblock));
334 		sbdirty();
335 		dirty(&asblk);
336 	}
337 	if (asblk.b_dirty && !bflag) {
338 		memmove((struct fs*)sblk.b_un.b_fs, sblock, SBSIZE);
339 		if (needswap)
340 			ffs_sb_swap(sblock, (struct fs*)sblk.b_un.b_fs, 1);
341 		memmove(asblk.b_un.b_fs, sblk.b_un.b_fs, (size_t)sblock->fs_sbsize);
342 		flush(fswritefd, &asblk);
343 	}
344 	/*
345 	 * read in the summary info.
346 	 */
347 	asked = 0;
348 	for (i = 0, j = 0; i < sblock->fs_cssize; i += sblock->fs_bsize, j++) {
349 		size = sblock->fs_cssize - i < sblock->fs_bsize ?
350 		    sblock->fs_cssize - i : sblock->fs_bsize;
351 		sblock->fs_csp[j] = (struct csum *)calloc(1, (unsigned)size);
352 		if (bread(fsreadfd, (char *)sblock->fs_csp[j],
353 		    fsbtodb(sblock, sblock->fs_csaddr + j * sblock->fs_frag),
354 		    size) != 0 && !asked) {
355 			pfatal("BAD SUMMARY INFORMATION");
356 			if (reply("CONTINUE") == 0) {
357 				markclean = 0;
358 				exit(EEXIT);
359 			}
360 			asked++;
361 		}
362 		/*
363 		 * The following assumes that struct csum is made of
364 		 * u_int32_t
365 		 */
366 		if (doswap) {
367 			int k;
368 			u_int32_t *cd = (u_int32_t *)sblock->fs_csp[j];
369 
370 			for (k = 0; k < size / sizeof(u_int32_t); k++)
371 				cd[k] = bswap32(cd[k]);
372 			bwrite(fswritefd, (char *)sblock->fs_csp[j],
373 			    fsbtodb(sblock,
374 				sblock->fs_csaddr + j * sblock->fs_frag),
375 			    size);
376 		}
377 		if (needswap) {
378 			int k;
379 			u_int32_t *cd = (u_int32_t *)sblock->fs_csp[j];
380 
381 			for (k = 0; k < size / sizeof(u_int32_t); k++)
382 				cd[k] = bswap32(cd[k]);
383 		}
384 	}
385 	/*
386 	 * allocate and initialize the necessary maps
387 	 */
388 	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
389 	blockmap = calloc((unsigned)bmapsize, sizeof (char));
390 	if (blockmap == NULL) {
391 		printf("cannot alloc %u bytes for blockmap\n",
392 		    (unsigned)bmapsize);
393 		goto badsblabel;
394 	}
395 	statemap = calloc((unsigned)(maxino + 1), sizeof(char));
396 	if (statemap == NULL) {
397 		printf("cannot alloc %u bytes for statemap\n",
398 		    (unsigned)(maxino + 1));
399 		goto badsblabel;
400 	}
401 	typemap = calloc((unsigned)(maxino + 1), sizeof(char));
402 	if (typemap == NULL) {
403 		printf("cannot alloc %u bytes for typemap\n",
404 		    (unsigned)(maxino + 1));
405 		goto badsblabel;
406 	}
407 	lncntp = (int16_t *)calloc((unsigned)(maxino + 1), sizeof(int16_t));
408 	if (lncntp == NULL) {
409 		printf("cannot alloc %u bytes for lncntp\n",
410 		    (unsigned)((maxino + 1) * sizeof(int16_t)));
411 		goto badsblabel;
412 	}
413 	/*
414 	 * cs_ndir may be inaccurate, particularly if we're using the -b
415 	 * option, so set a minimum to prevent bogus subdirectory reconnects
416 	 * and really inefficient directory scans.
417 	 * Also set a maximum in case the value is too large.
418 	 */
419 	numdirs = sblock->fs_cstotal.cs_ndir;
420 	if (numdirs < 1024)
421 		numdirs = 1024;
422 	if (numdirs > maxino + 1)
423 		numdirs = maxino + 1;
424 	inplast = 0;
425 	listmax = numdirs + 10;
426 	inpsort = (struct inoinfo **)calloc((unsigned)listmax,
427 	    sizeof(struct inoinfo *));
428 	inphead = (struct inoinfo **)calloc((unsigned)numdirs,
429 	    sizeof(struct inoinfo *));
430 	if (inpsort == NULL || inphead == NULL) {
431 		printf("cannot alloc %u bytes for inphead\n",
432 		    (unsigned)(numdirs * sizeof(struct inoinfo *)));
433 		goto badsblabel;
434 	}
435 	cgrp = malloc(sblock->fs_cgsize);
436 	if (cgrp == NULL) {
437 		printf("cannot alloc %u bytes for cylinder group\n",
438 		    sblock->fs_cgsize);
439 		goto badsblabel;
440 	}
441 	bufinit();
442 	if (sblock->fs_flags & FS_DOSOFTDEP)
443 		usedsoftdep = 1;
444 	else
445 		usedsoftdep = 0;
446 	return (1);
447 
448 badsblabel:
449 	markclean=0;
450 	ckfini();
451 	return (0);
452 }
453 
454 /*
455  * Read in the super block and its summary info.
456  */
457 static int
458 readsb(listerr)
459 	int listerr;
460 {
461 	ufs_daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
462 	struct fs *fs;
463 
464 	if (bread(fsreadfd, (char *)sblk.b_un.b_fs, super, (long)SBSIZE) != 0)
465 		return (0);
466 	sblk.b_bno = super;
467 	sblk.b_size = SBSIZE;
468 
469 	fs = sblk.b_un.b_fs;
470 	/* auto detect byte order */
471 	if( fs->fs_magic == FS_MAGIC) {
472 			if (endian == 0 || BYTE_ORDER == endian) {
473 				needswap = 0;
474 				doswap = do_blkswap = do_dirswap = 0;
475 			} else {
476 				needswap = 1;
477 				doswap = do_blkswap = do_dirswap = 1;
478 			}
479 	} else if (fs->fs_magic == bswap32(FS_MAGIC)) {
480 		if (endian == 0 || BYTE_ORDER != endian) {
481 			needswap = 1;
482 			doswap = do_blkswap = do_dirswap = 0;
483 		} else {
484 			needswap = 0;
485 			doswap = do_blkswap = do_dirswap = 1;
486 		}
487 	} else {
488 		badsb(listerr, "MAGIC NUMBER WRONG");
489 		return (0);
490 	}
491 	if (doswap) {
492 		if (preen)
493 			errx(EEXIT, "incompatible options -B and -p");
494 		if (nflag)
495 			errx(EEXIT, "incompatible options -B and -n");
496 		if (endian == LITTLE_ENDIAN) {
497 			if (!reply("CONVERT TO LITTLE ENDIAN"))
498 				return 0;
499 		} else if (endian == BIG_ENDIAN) {
500 			if (!reply("CONVERT TO BIG ENDIAN"))
501 				return 0;
502 		} else
503 			pfatal("INTERNAL ERROR: unknown endian");
504 	}
505 	if (needswap)
506 		printf("** Swapped byte order\n");
507 	/* swap SB byte order if asked */
508 	if (doswap)
509 		ffs_sb_swap(sblk.b_un.b_fs, sblk.b_un.b_fs, needswap);
510 
511 	memmove(sblock, sblk.b_un.b_fs, SBSIZE);
512 	if (needswap)
513 		ffs_sb_swap(sblk.b_un.b_fs, sblock, 0);
514 
515 	/*
516 	 * run a few consistency checks of the super block
517 	 */
518 	if (sblock->fs_ncg < 1)
519 		{ badsb(listerr, "NCG OUT OF RANGE"); return (0); }
520 	if (sblock->fs_cpg < 1)
521 		{ badsb(listerr, "CPG OUT OF RANGE"); return (0); }
522 	if (sblock->fs_ncg * sblock->fs_cpg < sblock->fs_ncyl ||
523 	    (sblock->fs_ncg - 1) * sblock->fs_cpg >= sblock->fs_ncyl)
524 		{ badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); }
525 	if (sblock->fs_sbsize > SBSIZE)
526 		{ badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
527 	/*
528 	 * Compute block size that the filesystem is based on,
529 	 * according to fsbtodb, and adjust superblock block number
530 	 * so we can tell if this is an alternate later.
531 	 */
532 	super *= dev_bsize;
533 	dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
534 	sblk.b_bno = super / dev_bsize;
535 
536 	if (bflag) {
537 		havesb = 1;
538 		return (1);
539 	}
540 	/*
541 	 * Set all possible fields that could differ, then do check
542 	 * of whole super block against an alternate super block.
543 	 * When an alternate super-block is specified this check is skipped.
544 	 */
545 	getblk(&asblk, cgsblock(sblock, sblock->fs_ncg - 1), sblock->fs_sbsize);
546 	if (asblk.b_errs)
547 		return (0);
548 	/* swap SB byte order if asked */
549 	if (doswap)
550 		ffs_sb_swap(asblk.b_un.b_fs, asblk.b_un.b_fs, needswap);
551 
552 	memmove(altsblock, asblk.b_un.b_fs, sblock->fs_sbsize);
553 	if (needswap)
554 		ffs_sb_swap(asblk.b_un.b_fs, altsblock, 0);
555 	altsblock->fs_firstfield = sblock->fs_firstfield;
556 	altsblock->fs_fscktime = sblock->fs_fscktime;
557 	altsblock->fs_unused_1 = sblock->fs_unused_1;
558 	altsblock->fs_time = sblock->fs_time;
559 	altsblock->fs_cstotal = sblock->fs_cstotal;
560 	altsblock->fs_cgrotor = sblock->fs_cgrotor;
561 	altsblock->fs_fmod = sblock->fs_fmod;
562 	altsblock->fs_clean = sblock->fs_clean;
563 	altsblock->fs_ronly = sblock->fs_ronly;
564 	altsblock->fs_flags = sblock->fs_flags;
565 	altsblock->fs_maxcontig = sblock->fs_maxcontig;
566 	altsblock->fs_minfree = sblock->fs_minfree;
567 	altsblock->fs_optim = sblock->fs_optim;
568 	altsblock->fs_rotdelay = sblock->fs_rotdelay;
569 	altsblock->fs_maxbpg = sblock->fs_maxbpg;
570 	memmove(altsblock->fs_csp, sblock->fs_csp, sizeof sblock->fs_csp);
571 	altsblock->fs_maxcluster = sblock->fs_maxcluster;
572 	memmove(altsblock->fs_fsmnt, sblock->fs_fsmnt, sizeof sblock->fs_fsmnt);
573 	memmove(altsblock->fs_sparecon,
574 		sblock->fs_sparecon, sizeof sblock->fs_sparecon);
575 	/*
576 	 * The following should not have to be copied.
577 	 */
578 	altsblock->fs_fsbtodb = sblock->fs_fsbtodb;
579 	altsblock->fs_interleave = sblock->fs_interleave;
580 	altsblock->fs_npsect = sblock->fs_npsect;
581 	altsblock->fs_nrpos = sblock->fs_nrpos;
582 	altsblock->fs_state = sblock->fs_state;
583 	altsblock->fs_qbmask = sblock->fs_qbmask;
584 	altsblock->fs_qfmask = sblock->fs_qfmask;
585 	altsblock->fs_state = sblock->fs_state;
586 	altsblock->fs_maxfilesize = sblock->fs_maxfilesize;
587 	if (memcmp(sblock, altsblock, (int)sblock->fs_sbsize)) {
588 		if (debug) {
589 			long *nlp, *olp, *endlp;
590 
591 			printf("superblock mismatches\n");
592 			nlp = (long *)altsblock;
593 			olp = (long *)sblock;
594 			endlp = olp + (sblock->fs_sbsize / sizeof *olp);
595 			for ( ; olp < endlp; olp++, nlp++) {
596 				if (*olp == *nlp)
597 					continue;
598 				printf("offset %ld, original %lx, alternate %lx\n",
599 				    (long)(olp - (long *)sblock), *olp, *nlp);
600 			}
601 		}
602 		badsb(listerr,
603 		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
604 		return (0);
605 	}
606 	/* Now we know the SB is valid, we can write it back if needed */
607 	if (doswap) {
608 		sbdirty();
609 		dirty(&asblk);
610 	}
611 	havesb = 1;
612 	return (1);
613 }
614 
615 static void
616 badsb(listerr, s)
617 	int listerr;
618 	char *s;
619 {
620 
621 	if (!listerr)
622 		return;
623 	if (preen)
624 		printf("%s: ", cdevname());
625 	pfatal("BAD SUPER BLOCK: %s\n", s);
626 }
627 
628 /*
629  * Calculate a prototype superblock based on information in the disk label.
630  * When done the cgsblock macro can be calculated and the fs_ncg field
631  * can be used. Do NOT attempt to use other macros without verifying that
632  * their needed information is available!
633  */
634 static int
635 calcsb(dev, devfd, fs)
636 	const char *dev;
637 	int devfd;
638 	struct fs *fs;
639 {
640 	struct disklabel *lp;
641 	struct partition *pp;
642 	char *cp;
643 	int i;
644 
645 	cp = strchr(dev, '\0') - 1;
646 	if ((cp == (char *)-1 || (*cp < 'a' || *cp > 'h')) && !isdigit(*cp)) {
647 		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
648 		return (0);
649 	}
650 	lp = getdisklabel(dev, devfd);
651 	if (isdigit(*cp))
652 		pp = &lp->d_partitions[0];
653 	else
654 		pp = &lp->d_partitions[*cp - 'a'];
655 	if (pp->p_fstype != FS_BSDFFS) {
656 		pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
657 			dev, pp->p_fstype < FSMAXTYPES ?
658 			fstypenames[pp->p_fstype] : "unknown");
659 		return (0);
660 	}
661 	/* avoid divide by 0 */
662 	if (pp->p_fsize == 0 || pp->p_frag == 0)
663 		return (0);
664 	memset(fs, 0, sizeof(struct fs));
665 	fs->fs_fsize = pp->p_fsize;
666 	fs->fs_frag = pp->p_frag;
667 	fs->fs_cpg = pp->p_cpg;
668 	fs->fs_size = pp->p_size;
669 	fs->fs_ntrak = lp->d_ntracks;
670 	fs->fs_nsect = lp->d_nsectors;
671 	fs->fs_spc = lp->d_secpercyl;
672 	fs->fs_nspf = fs->fs_fsize / lp->d_secsize;
673 	fs->fs_sblkno = roundup(
674 		howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
675 		fs->fs_frag);
676 	fs->fs_cgmask = 0xffffffff;
677 	for (i = fs->fs_ntrak; i > 1; i >>= 1)
678 		fs->fs_cgmask <<= 1;
679 	if (!POWEROF2(fs->fs_ntrak))
680 		fs->fs_cgmask <<= 1;
681 	fs->fs_cgoffset = roundup(
682 		howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag);
683 	fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs);
684 	fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg);
685 	for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1)
686 		fs->fs_fsbtodb++;
687 	dev_bsize = lp->d_secsize;
688 	return (1);
689 }
690 
691 static struct disklabel *
692 getdisklabel(s, fd)
693 	const char *s;
694 	int	fd;
695 {
696 	static struct disklabel lab;
697 
698 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
699 		if (s == NULL)
700 			return ((struct disklabel *)NULL);
701 		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
702 		errx(EEXIT, "%s: can't read disk label", s);
703 	}
704 	return (&lab);
705 }
706