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