xref: /freebsd/sbin/fsck_ffs/setup.c (revision 42c159fe388a3765f69860c84183700af37aca8a)
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 #include <sys/sysctl.h>
48 
49 #include <ufs/ufs/dinode.h>
50 #include <ufs/ffs/fs.h>
51 
52 #include <ctype.h>
53 #include <err.h>
54 #include <errno.h>
55 #include <string.h>
56 
57 #include "fsck.h"
58 
59 struct bufarea asblk;
60 #define altsblock (*asblk.b_un.b_fs)
61 #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
62 
63 static void badsb(int listerr, char *s);
64 static int calcsb(char *dev, int devfd, struct fs *fs);
65 static struct disklabel *getdisklabel(char *s, int fd);
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(char *dev)
74 {
75 	long cg, asked, i, j;
76 	long bmapsize;
77 	off_t sizepb;
78 	struct stat statb;
79 	struct fs proto;
80 	size_t size;
81 
82 	havesb = 0;
83 	fswritefd = -1;
84 	cursnapshot = 0;
85 	if (stat(dev, &statb) < 0) {
86 		printf("Can't stat %s: %s\n", dev, strerror(errno));
87 		if (bkgrdflag) {
88 			unlink(snapname);
89 			bkgrdflag = 0;
90 		}
91 		return (0);
92 	}
93 	if ((statb.st_mode & S_IFMT) != S_IFCHR &&
94 	    (statb.st_mode & S_IFMT) != S_IFBLK) {
95 		if (bkgrdflag != 0 && (statb.st_flags & SF_SNAPSHOT) == 0) {
96 			unlink(snapname);
97 			printf("background fsck lacks a snapshot\n");
98 			exit(EEXIT);
99 		}
100 		if ((statb.st_flags & SF_SNAPSHOT) != 0 && cvtlevel == 0) {
101 			cursnapshot = statb.st_ino;
102 		} else {
103 			if (cvtlevel == 0 ||
104 			    (statb.st_flags & SF_SNAPSHOT) == 0) {
105 				if (preen && bkgrdflag) {
106 					unlink(snapname);
107 					bkgrdflag = 0;
108 				}
109 				pfatal("%s is not a disk device", dev);
110 				if (reply("CONTINUE") == 0) {
111 					if (bkgrdflag) {
112 						unlink(snapname);
113 						bkgrdflag = 0;
114 					}
115 					return (0);
116 				}
117 			} else {
118 				if (bkgrdflag) {
119 					unlink(snapname);
120 					bkgrdflag = 0;
121 				}
122 				pfatal("cannot convert a snapshot");
123 				exit(EEXIT);
124 			}
125 		}
126 	}
127 	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
128 		if (bkgrdflag) {
129 			unlink(snapname);
130 			bkgrdflag = 0;
131 		}
132 		printf("Can't open %s: %s\n", dev, strerror(errno));
133 		return (0);
134 	}
135 	if (bkgrdflag) {
136 		unlink(snapname);
137 		size = MIBSIZE;
138 		if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0||
139 		    sysctlnametomib("vfs.ffs.adjblkcnt", adjblkcnt, &size) < 0||
140 		    sysctlnametomib("vfs.ffs.freefiles", freefiles, &size) < 0||
141 		    sysctlnametomib("vfs.ffs.freedirs", freedirs, &size) < 0 ||
142 		    sysctlnametomib("vfs.ffs.freeblks", freeblks, &size) < 0) {
143 			pfatal("kernel lacks background fsck support\n");
144 			exit(EEXIT);
145 		}
146 		cmd.version = FFS_CMD_VERSION;
147 		cmd.handle = fsreadfd;
148 		fswritefd = -1;
149 	}
150 	if (preen == 0)
151 		printf("** %s", dev);
152 	if (bkgrdflag == 0 &&
153 	    (nflag || (fswritefd = open(dev, O_WRONLY)) < 0)) {
154 		fswritefd = -1;
155 		if (preen)
156 			pfatal("NO WRITE ACCESS");
157 		printf(" (NO WRITE)");
158 	}
159 	if (preen == 0)
160 		printf("\n");
161 	/*
162 	 * Read in the superblock, looking for alternates if necessary
163 	 */
164 	if (readsb(1) == 0) {
165 		skipclean = 0;
166 		if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
167 			return(0);
168 		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
169 			return (0);
170 		for (cg = 0; cg < proto.fs_ncg; cg++) {
171 			bflag = fsbtodb(&proto, cgsblock(&proto, cg));
172 			if (readsb(0) != 0)
173 				break;
174 		}
175 		if (cg >= proto.fs_ncg) {
176 			printf("%s %s\n%s %s\n%s %s\n",
177 				"SEARCH FOR ALTERNATE SUPER-BLOCK",
178 				"FAILED. YOU MUST USE THE",
179 				"-b OPTION TO FSCK TO SPECIFY THE",
180 				"LOCATION OF AN ALTERNATE",
181 				"SUPER-BLOCK TO SUPPLY NEEDED",
182 				"INFORMATION; SEE fsck(8).");
183 			bflag = 0;
184 			return(0);
185 		}
186 		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
187 		bflag = 0;
188 	}
189 	if (skipclean && preen && sblock.fs_clean) {
190 		pwarn("FILESYSTEM CLEAN; SKIPPING CHECKS\n");
191 		return (-1);
192 	}
193 	maxfsblock = sblock.fs_size;
194 	maxino = sblock.fs_ncg * sblock.fs_ipg;
195 	/*
196 	 * Check and potentially fix certain fields in the super block.
197 	 */
198 	if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
199 		pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
200 		if (reply("SET TO DEFAULT") == 1) {
201 			sblock.fs_optim = FS_OPTTIME;
202 			sbdirty();
203 		}
204 	}
205 	if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
206 		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
207 			sblock.fs_minfree);
208 		if (reply("SET TO DEFAULT") == 1) {
209 			sblock.fs_minfree = 10;
210 			sbdirty();
211 		}
212 	}
213 	if (sblock.fs_interleave < 1 ||
214 	    sblock.fs_interleave > sblock.fs_nsect) {
215 		pfatal("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
216 			sblock.fs_interleave);
217 		sblock.fs_interleave = 1;
218 		if (preen)
219 			printf(" (FIXED)\n");
220 		if (preen || reply("SET TO DEFAULT") == 1) {
221 			sbdirty();
222 			dirty(&asblk);
223 		}
224 	}
225 	if (sblock.fs_npsect < sblock.fs_nsect ||
226 	    sblock.fs_npsect > sblock.fs_nsect*2) {
227 		pfatal("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
228 			sblock.fs_npsect);
229 		sblock.fs_npsect = sblock.fs_nsect;
230 		if (preen)
231 			printf(" (FIXED)\n");
232 		if (preen || reply("SET TO DEFAULT") == 1) {
233 			sbdirty();
234 			dirty(&asblk);
235 		}
236 	}
237 	if (sblock.fs_inodefmt >= FS_44INODEFMT) {
238 		newinofmt = 1;
239 	} else {
240 		sblock.fs_qbmask = ~sblock.fs_bmask;
241 		sblock.fs_qfmask = ~sblock.fs_fmask;
242 		/* This should match the kernel limit in ffs_oldfscompat(). */
243 		sblock.fs_maxfilesize = (u_int64_t)1 << 39;
244 		newinofmt = 0;
245 	}
246 	/*
247 	 * Convert to new inode format.
248 	 */
249 	if (cvtlevel >= 2 && sblock.fs_inodefmt < FS_44INODEFMT) {
250 		if (preen)
251 			pwarn("CONVERTING TO NEW INODE FORMAT\n");
252 		else if (!reply("CONVERT TO NEW INODE FORMAT"))
253 			return(0);
254 		doinglevel2++;
255 		sblock.fs_inodefmt = FS_44INODEFMT;
256 		sizepb = sblock.fs_bsize;
257 		sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
258 		for (i = 0; i < NIADDR; i++) {
259 			sizepb *= NINDIR(&sblock);
260 			sblock.fs_maxfilesize += sizepb;
261 		}
262 		sblock.fs_maxsymlinklen = MAXSYMLINKLEN;
263 		sblock.fs_qbmask = ~sblock.fs_bmask;
264 		sblock.fs_qfmask = ~sblock.fs_fmask;
265 		sbdirty();
266 		dirty(&asblk);
267 	}
268 	/*
269 	 * Convert to new cylinder group format.
270 	 */
271 	if (cvtlevel >= 1 && sblock.fs_postblformat == FS_42POSTBLFMT) {
272 		if (preen)
273 			pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n");
274 		else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT"))
275 			return(0);
276 		doinglevel1++;
277 		sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
278 		sblock.fs_nrpos = 8;
279 		sblock.fs_postbloff =
280 		    (char *)(&sblock.fs_opostbl[0][0]) -
281 		    (char *)(&sblock.fs_firstfield);
282 		sblock.fs_rotbloff = &sblock.fs_space[0] -
283 		    (u_char *)(&sblock.fs_firstfield);
284 		sblock.fs_cgsize =
285 			fragroundup(&sblock, CGSIZE(&sblock));
286 		sbdirty();
287 		dirty(&asblk);
288 	}
289 	if (asblk.b_dirty && !bflag) {
290 		memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize);
291 		flush(fswritefd, &asblk);
292 	}
293 	/*
294 	 * read in the summary info.
295 	 */
296 	asked = 0;
297 	sblock.fs_csp = calloc(1, sblock.fs_cssize);
298 	for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
299 		size = sblock.fs_cssize - i < sblock.fs_bsize ?
300 		    sblock.fs_cssize - i : sblock.fs_bsize;
301 		if (bread(fsreadfd, (char *)sblock.fs_csp + i,
302 		    fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
303 		    size) != 0 && !asked) {
304 			pfatal("BAD SUMMARY INFORMATION");
305 			if (reply("CONTINUE") == 0) {
306 				ckfini(0);
307 				exit(EEXIT);
308 			}
309 			asked++;
310 		}
311 	}
312 	/*
313 	 * allocate and initialize the necessary maps
314 	 */
315 	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(short));
316 	blockmap = calloc((unsigned)bmapsize, sizeof (char));
317 	if (blockmap == NULL) {
318 		printf("cannot alloc %u bytes for blockmap\n",
319 		    (unsigned)bmapsize);
320 		goto badsb;
321 	}
322 	inostathead = calloc((unsigned)(sblock.fs_ncg),
323 	    sizeof(struct inostatlist));
324 	if (inostathead == NULL) {
325 		printf("cannot alloc %u bytes for inostathead\n",
326 		    (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg)));
327 		goto badsb;
328 	}
329 	numdirs = sblock.fs_cstotal.cs_ndir;
330 	dirhash = numdirs;
331 	if (numdirs == 0) {
332 		printf("numdirs is zero, try using an alternate superblock\n");
333 		goto badsb;
334 	}
335 	inplast = 0;
336 	listmax = numdirs + 10;
337 	inpsort = (struct inoinfo **)calloc((unsigned)listmax,
338 	    sizeof(struct inoinfo *));
339 	inphead = (struct inoinfo **)calloc((unsigned)numdirs,
340 	    sizeof(struct inoinfo *));
341 	if (inpsort == NULL || inphead == NULL) {
342 		printf("cannot alloc %u bytes for inphead\n",
343 		    (unsigned)numdirs * sizeof(struct inoinfo *));
344 		goto badsb;
345 	}
346 	bufinit();
347 	if (sblock.fs_flags & FS_DOSOFTDEP)
348 		usedsoftdep = 1;
349 	else
350 		usedsoftdep = 0;
351 	return (1);
352 
353 badsb:
354 	ckfini(0);
355 	return (0);
356 }
357 
358 /*
359  * Read in the super block and its summary info.
360  */
361 int
362 readsb(int listerr)
363 {
364 	ufs_daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
365 
366 	if (bread(fsreadfd, (char *)&sblock, super, (long)SBSIZE) != 0)
367 		return (0);
368 	sblk.b_bno = super;
369 	sblk.b_size = SBSIZE;
370 	/*
371 	 * run a few consistency checks of the super block
372 	 */
373 	if (sblock.fs_magic != FS_MAGIC)
374 		{ badsb(listerr, "MAGIC NUMBER WRONG"); return (0); }
375 	if (sblock.fs_ncg < 1)
376 		{ badsb(listerr, "NCG OUT OF RANGE"); return (0); }
377 	if (sblock.fs_cpg < 1)
378 		{ badsb(listerr, "CPG OUT OF RANGE"); return (0); }
379 	if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl ||
380 	    (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl)
381 		{ badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); }
382 	if (sblock.fs_sbsize > SBSIZE)
383 		{ badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
384 	/*
385 	 * Compute block size that the filesystem is based on,
386 	 * according to fsbtodb, and adjust superblock block number
387 	 * so we can tell if this is an alternate later.
388 	 */
389 	super *= dev_bsize;
390 	dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
391 	sblk.b_bno = super / dev_bsize;
392 	if (bflag) {
393 		havesb = 1;
394 		return (1);
395 	}
396 	/*
397 	 * Set all possible fields that could differ, then do check
398 	 * of whole super block against an alternate super block.
399 	 * When an alternate super-block is specified this check is skipped.
400 	 */
401 	getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
402 	if (asblk.b_errs)
403 		return (0);
404 	altsblock.fs_firstfield = sblock.fs_firstfield;
405 	altsblock.fs_unused_1 = sblock.fs_unused_1;
406 	altsblock.fs_time = sblock.fs_time;
407 	altsblock.fs_cstotal = sblock.fs_cstotal;
408 	altsblock.fs_cgrotor = sblock.fs_cgrotor;
409 	altsblock.fs_fmod = sblock.fs_fmod;
410 	altsblock.fs_clean = sblock.fs_clean;
411 	altsblock.fs_ronly = sblock.fs_ronly;
412 	altsblock.fs_flags = sblock.fs_flags;
413 	altsblock.fs_maxcontig = sblock.fs_maxcontig;
414 	altsblock.fs_minfree = sblock.fs_minfree;
415 	altsblock.fs_optim = sblock.fs_optim;
416 	altsblock.fs_rotdelay = sblock.fs_rotdelay;
417 	altsblock.fs_maxbpg = sblock.fs_maxbpg;
418 	memmove(altsblock.fs_ocsp, sblock.fs_ocsp, sizeof sblock.fs_ocsp);
419 	altsblock.fs_csp = sblock.fs_csp;
420 	altsblock.fs_maxcluster = sblock.fs_maxcluster;
421 	altsblock.fs_contigdirs = sblock.fs_contigdirs;
422 	altsblock.fs_active = sblock.fs_active;
423 	altsblock.fs_avgfilesize = sblock.fs_avgfilesize;
424 	altsblock.fs_avgfpdir = sblock.fs_avgfpdir;
425 	altsblock.fs_pendingblocks = sblock.fs_pendingblocks;
426 	altsblock.fs_pendinginodes = sblock.fs_pendinginodes;
427 	memmove(altsblock.fs_fsmnt, sblock.fs_fsmnt, sizeof sblock.fs_fsmnt);
428 	memmove(altsblock.fs_snapinum, sblock.fs_snapinum,
429 		sizeof sblock.fs_snapinum);
430 	memmove(altsblock.fs_sparecon,
431 		sblock.fs_sparecon, sizeof sblock.fs_sparecon);
432 	/*
433 	 * The following should not have to be copied.
434 	 */
435 	altsblock.fs_fsbtodb = sblock.fs_fsbtodb;
436 	altsblock.fs_interleave = sblock.fs_interleave;
437 	altsblock.fs_npsect = sblock.fs_npsect;
438 	altsblock.fs_nrpos = sblock.fs_nrpos;
439 	altsblock.fs_state = sblock.fs_state;
440 	altsblock.fs_qbmask = sblock.fs_qbmask;
441 	altsblock.fs_qfmask = sblock.fs_qfmask;
442 	altsblock.fs_state = sblock.fs_state;
443 	altsblock.fs_maxfilesize = sblock.fs_maxfilesize;
444 	if (memcmp(&sblock, &altsblock, (int)sblock.fs_sbsize)) {
445 		if (debug) {
446 			long *nlp, *olp, *endlp;
447 
448 			printf("superblock mismatches\n");
449 			nlp = (long *)&altsblock;
450 			olp = (long *)&sblock;
451 			endlp = olp + (sblock.fs_sbsize / sizeof *olp);
452 			for ( ; olp < endlp; olp++, nlp++) {
453 				if (*olp == *nlp)
454 					continue;
455 				printf(
456 				    "offset %d, original %ld, alternate %ld\n",
457 				    olp - (long *)&sblock, *olp, *nlp);
458 			}
459 		}
460 		badsb(listerr,
461 		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
462 		return (0);
463 	}
464 	havesb = 1;
465 	return (1);
466 }
467 
468 static void
469 badsb(int listerr, char *s)
470 {
471 
472 	if (!listerr)
473 		return;
474 	if (preen)
475 		printf("%s: ", cdevname);
476 	pfatal("BAD SUPER BLOCK: %s\n", s);
477 }
478 
479 void
480 sblock_init(void)
481 {
482 	struct disklabel *lp;
483 
484 	fswritefd = -1;
485 	fsmodified = 0;
486 	lfdir = 0;
487 	initbarea(&sblk);
488 	initbarea(&asblk);
489 	sblk.b_un.b_buf = malloc(SBSIZE);
490 	asblk.b_un.b_buf = malloc(SBSIZE);
491 	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
492 		errx(EEXIT, "cannot allocate space for superblock");
493 	if ((lp = getdisklabel(NULL, fsreadfd)))
494 		dev_bsize = secsize = lp->d_secsize;
495 	else
496 		dev_bsize = secsize = DEV_BSIZE;
497 }
498 
499 /*
500  * Calculate a prototype superblock based on information in the disk label.
501  * When done the cgsblock macro can be calculated and the fs_ncg field
502  * can be used. Do NOT attempt to use other macros without verifying that
503  * their needed information is available!
504  */
505 static int
506 calcsb(char *dev, int devfd, struct fs *fs)
507 {
508 	struct disklabel *lp;
509 	struct partition *pp;
510 	char *cp;
511 	int i;
512 
513 	cp = strchr(dev, '\0') - 1;
514 	if (cp == (char *)-1 || ((*cp < 'a' || *cp > 'h') && !isdigit(*cp))) {
515 		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
516 		return (0);
517 	}
518 	lp = getdisklabel(dev, devfd);
519 	if (isdigit(*cp))
520 		pp = &lp->d_partitions[0];
521 	else
522 		pp = &lp->d_partitions[*cp - 'a'];
523 	if (pp->p_fstype != FS_BSDFFS) {
524 		pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
525 			dev, pp->p_fstype < FSMAXTYPES ?
526 			fstypenames[pp->p_fstype] : "unknown");
527 		return (0);
528 	}
529 	if (pp->p_fsize == 0 || pp->p_frag == 0 ||
530 	    pp->p_cpg == 0 || pp->p_size == 0) {
531 		pfatal("%s: %s: type %s fsize %d, frag %d, cpg %d, size %d\n",
532 		    dev, "INCOMPLETE LABEL", fstypenames[pp->p_fstype],
533 		    pp->p_fsize, pp->p_frag, pp->p_cpg, pp->p_size);
534 		return (0);
535 	}
536 	memset(fs, 0, sizeof(struct fs));
537 	fs->fs_fsize = pp->p_fsize;
538 	fs->fs_frag = pp->p_frag;
539 	fs->fs_cpg = pp->p_cpg;
540 	fs->fs_size = pp->p_size;
541 	fs->fs_ntrak = lp->d_ntracks;
542 	fs->fs_nsect = lp->d_nsectors;
543 	fs->fs_spc = lp->d_secpercyl;
544 	fs->fs_nspf = fs->fs_fsize / lp->d_secsize;
545 	fs->fs_sblkno = roundup(
546 		howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
547 		fs->fs_frag);
548 	fs->fs_cgmask = 0xffffffff;
549 	for (i = fs->fs_ntrak; i > 1; i >>= 1)
550 		fs->fs_cgmask <<= 1;
551 	if (!POWEROF2(fs->fs_ntrak))
552 		fs->fs_cgmask <<= 1;
553 	fs->fs_cgoffset = roundup(
554 		howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag);
555 	fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs);
556 	fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg);
557 	for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1)
558 		fs->fs_fsbtodb++;
559 	dev_bsize = lp->d_secsize;
560 	return (1);
561 }
562 
563 static struct disklabel *
564 getdisklabel(char *s, int fd)
565 {
566 	static struct disklabel lab;
567 
568 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
569 		if (s == NULL)
570 			return ((struct disklabel *)NULL);
571 		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
572 		errx(EEXIT, "%s: can't read disk label", s);
573 	}
574 	return (&lab);
575 }
576