xref: /netbsd-src/sbin/fsck_ext2fs/setup.c (revision eb7c1594f145c931049e1fd9eb056a5987e87e59)
1 /*	$NetBSD: setup.c,v 1.13 2003/08/07 10:04:18 agc 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. Neither the name of the University nor the names of its contributors
16  *	may be used to endorse or promote products derived from this software
17  *	without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1997 Manuel Bouyer.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  * 1. Redistributions of source code must retain the above copyright
39  *	notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *	notice, this list of conditions and the following disclaimer in the
42  *	documentation and/or other materials provided with the distribution.
43  * 3. All advertising materials mentioning features or use of this software
44  *	must display the following acknowledgement:
45  *	This product includes software developed by the University of
46  *	California, Berkeley and its contributors.
47  * 4. Neither the name of the University nor the names of its contributors
48  *	may be used to endorse or promote products derived from this software
49  *	without specific prior written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61  * SUCH DAMAGE.
62  */
63 
64 #include <sys/cdefs.h>
65 #ifndef lint
66 #if 0
67 static char sccsid[] = "@(#)setup.c	8.5 (Berkeley) 11/23/94";
68 #else
69 __RCSID("$NetBSD: setup.c,v 1.13 2003/08/07 10:04:18 agc Exp $");
70 #endif
71 #endif /* not lint */
72 
73 #define FSTYPENAMES
74 #include <sys/param.h>
75 #include <sys/time.h>
76 #include <ufs/ext2fs/ext2fs_dinode.h>
77 #include <ufs/ext2fs/ext2fs.h>
78 #include <sys/stat.h>
79 #include <sys/ioctl.h>
80 #include <sys/disklabel.h>
81 #include <sys/file.h>
82 
83 #include <errno.h>
84 #include <stdio.h>
85 #include <stdlib.h>
86 #include <string.h>
87 #include <ctype.h>
88 
89 #include "fsck.h"
90 #include "extern.h"
91 #include "fsutil.h"
92 
93 #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
94 
95 void badsb __P((int, char *));
96 int calcsb __P((const char *, int, struct m_ext2fs *));
97 static struct disklabel *getdisklabel __P((const char *, int));
98 static int readsb __P((int));
99 
100 int
101 setup(dev)
102 	const char *dev;
103 {
104 	long cg, asked, i;
105 	long bmapsize;
106 	struct disklabel *lp;
107 	off_t sizepb;
108 	struct stat statb;
109 	struct m_ext2fs proto;
110 	int doskipclean;
111 	u_int64_t maxfilesize;
112 
113 	havesb = 0;
114 	fswritefd = -1;
115 	doskipclean = skipclean;
116 	if (stat(dev, &statb) < 0) {
117 		printf("Can't stat %s: %s\n", dev, strerror(errno));
118 		return (0);
119 	}
120 	if (!S_ISCHR(statb.st_mode)) {
121 		pfatal("%s is not a character device", dev);
122 		if (reply("CONTINUE") == 0)
123 			return (0);
124 	}
125 	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
126 		printf("Can't open %s: %s\n", dev, strerror(errno));
127 		return (0);
128 	}
129 	if (preen == 0)
130 		printf("** %s", dev);
131 	if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
132 		fswritefd = -1;
133 		if (preen)
134 			pfatal("NO WRITE ACCESS");
135 		printf(" (NO WRITE)");
136 	}
137 	if (preen == 0)
138 		printf("\n");
139 	fsmodified = 0;
140 	lfdir = 0;
141 	initbarea(&sblk);
142 	initbarea(&asblk);
143 	sblk.b_un.b_buf = malloc(SBSIZE);
144 	asblk.b_un.b_buf = malloc(SBSIZE);
145 	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
146 		errexit("cannot allocate space for superblock\n");
147 	if ((lp = getdisklabel((char *)NULL, fsreadfd)) != NULL)
148 		dev_bsize = secsize = lp->d_secsize;
149 	else
150 		dev_bsize = secsize = DEV_BSIZE;
151 	/*
152 	 * Read in the superblock, looking for alternates if necessary
153 	 */
154 	if (readsb(1) == 0) {
155 		if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
156 			return(0);
157 		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
158 			return (0);
159 		for (cg = 1; cg < proto.e2fs_ncg; cg++) {
160 			bflag = fsbtodb(&proto,
161 				cg * proto.e2fs.e2fs_bpg + proto.e2fs.e2fs_first_dblock);
162 			if (readsb(0) != 0)
163 				break;
164 		}
165 		if (cg >= proto.e2fs_ncg) {
166 			printf("%s %s\n%s %s\n%s %s\n",
167 				"SEARCH FOR ALTERNATE SUPER-BLOCK",
168 				"FAILED. YOU MUST USE THE",
169 				"-b OPTION TO FSCK_FFS TO SPECIFY THE",
170 				"LOCATION OF AN ALTERNATE",
171 				"SUPER-BLOCK TO SUPPLY NEEDED",
172 				"INFORMATION; SEE fsck_ext2fs(8).");
173 			return(0);
174 		}
175 		doskipclean = 0;
176 		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
177 	}
178 	if (debug)
179 		printf("state = %d\n", sblock.e2fs.e2fs_state);
180 	if (sblock.e2fs.e2fs_state == E2FS_ISCLEAN) {
181 		if (doskipclean) {
182 			pwarn("%sile system is clean; not checking\n",
183 				preen ? "f" : "** F");
184 			return (-1);
185 		}
186 		if (!preen)
187 			pwarn("** File system is already clean\n");
188 	}
189 	maxfsblock = sblock.e2fs.e2fs_bcount;
190 	maxino = sblock.e2fs_ncg * sblock.e2fs.e2fs_ipg;
191 	sizepb = sblock.e2fs_bsize;
192 	maxfilesize = sblock.e2fs_bsize * NDADDR - 1;
193 	for (i = 0; i < NIADDR; i++) {
194 		sizepb *= NINDIR(&sblock);
195 		maxfilesize += sizepb;
196 	}
197 	/*
198 	 * Check and potentially fix certain fields in the super block.
199 	 */
200 	if ((sblock.e2fs.e2fs_rbcount < 0) ||
201 			(sblock.e2fs.e2fs_rbcount > sblock.e2fs.e2fs_bcount)) {
202 		pfatal("IMPOSSIBLE RESERVED BLOCK COUNT=%d IN SUPERBLOCK",
203 			sblock.e2fs.e2fs_rbcount);
204 		if (reply("SET TO DEFAULT") == 1) {
205 			sblock.e2fs.e2fs_rbcount = sblock.e2fs.e2fs_bcount * 0.1;
206 			sbdirty();
207 			dirty(&asblk);
208 		}
209 	}
210 	if (sblock.e2fs.e2fs_bpg != sblock.e2fs.e2fs_fpg) {
211 		pfatal("WRONG FPG=%d (BPG=%d) IN SUPERBLOCK",
212 			sblock.e2fs.e2fs_fpg, sblock.e2fs.e2fs_bpg);
213 		return 0;
214 	}
215 	if (asblk.b_dirty && !bflag) {
216 		copyback_sb(&asblk);
217 		flush(fswritefd, &asblk);
218 	}
219 	/*
220 	 * read in the summary info.
221 	 */
222 
223 	sblock.e2fs_gd = malloc(sblock.e2fs_ngdb * sblock.e2fs_bsize);
224 	if (sblock.e2fs_gd == NULL)
225 		errexit("out of memory\n");
226 	asked = 0;
227 	for (i=0; i < sblock.e2fs_ngdb; i++) {
228 		if (bread(fsreadfd,(char *)
229 			&sblock.e2fs_gd[i* sblock.e2fs_bsize / sizeof(struct ext2_gd)],
230 			fsbtodb(&sblock, ((sblock.e2fs_bsize>1024)?0:1)+i+1),
231 			sblock.e2fs_bsize) != 0 && !asked) {
232 			pfatal("BAD SUMMARY INFORMATION");
233 			if (reply("CONTINUE") == 0)
234 				errexit("%s\n", "");
235 			asked++;
236 		}
237 	}
238 	/*
239 	 * allocate and initialize the necessary maps
240 	 */
241 	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
242 	blockmap = calloc((unsigned)bmapsize, sizeof (char));
243 	if (blockmap == NULL) {
244 		printf("cannot alloc %u bytes for blockmap\n",
245 			(unsigned)bmapsize);
246 		goto badsblabel;
247 	}
248 	statemap = calloc((unsigned)(maxino + 2), sizeof(char));
249 	if (statemap == NULL) {
250 		printf("cannot alloc %u bytes for statemap\n",
251 			(unsigned)(maxino + 1));
252 		goto badsblabel;
253 	}
254 	typemap = calloc((unsigned)(maxino + 1), sizeof(char));
255 	if (typemap == NULL) {
256 		printf("cannot alloc %u bytes for typemap\n",
257 		    (unsigned)(maxino + 1));
258 		goto badsblabel;
259 	}
260 	lncntp = (int16_t *)calloc((unsigned)(maxino + 1), sizeof(int16_t));
261 	if (lncntp == NULL) {
262 		printf("cannot alloc %u bytes for lncntp\n",
263 			(unsigned)((maxino + 1) * sizeof(int16_t)));
264 		goto badsblabel;
265 	}
266 	for (numdirs = 0, cg = 0; cg < sblock.e2fs_ncg; cg++) {
267 		numdirs += fs2h16(sblock.e2fs_gd[cg].ext2bgd_ndirs);
268 	}
269 	inplast = 0;
270 	listmax = numdirs + 10;
271 	inpsort = (struct inoinfo **)calloc((unsigned)listmax,
272 		sizeof(struct inoinfo *));
273 	inphead = (struct inoinfo **)calloc((unsigned)numdirs,
274 		sizeof(struct inoinfo *));
275 	if (inpsort == NULL || inphead == NULL) {
276 		printf("cannot alloc %u bytes for inphead\n",
277 			(unsigned)(numdirs * sizeof(struct inoinfo *)));
278 		goto badsblabel;
279 	}
280 	bufinit();
281 	return (1);
282 
283 badsblabel:
284 	ckfini(0);
285 	return (0);
286 }
287 
288 /*
289  * Read in the super block and its summary info, convert to host byte order.
290  */
291 static int
292 readsb(listerr)
293 	int listerr;
294 {
295 	daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
296 
297 	if (bread(fsreadfd, (char *)sblk.b_un.b_fs, super, (long)SBSIZE) != 0)
298 		return (0);
299 	sblk.b_bno = super;
300 	sblk.b_size = SBSIZE;
301 
302 	/* Copy the superblock in memory */
303 	e2fs_sbload(sblk.b_un.b_fs, &sblock.e2fs);
304 
305 	/*
306 	 * run a few consistency checks of the super block
307 	 */
308 	if (sblock.e2fs.e2fs_magic != E2FS_MAGIC) {
309 		badsb(listerr, "MAGIC NUMBER WRONG"); return (0);
310 	}
311 	if (sblock.e2fs.e2fs_log_bsize > 2) {
312 		badsb(listerr, "BAD LOG_BSIZE"); return (0);
313 	}
314 
315 	/* compute the dynamic fields of the in-memory sb */
316 	/* compute dynamic sb infos */
317 	sblock.e2fs_ncg =
318 		howmany(sblock.e2fs.e2fs_bcount - sblock.e2fs.e2fs_first_dblock,
319 		sblock.e2fs.e2fs_bpg);
320 	/* XXX assume hw bsize = 512 */
321 	sblock.e2fs_fsbtodb = sblock.e2fs.e2fs_log_bsize + 1;
322 	sblock.e2fs_bsize = 1024 << sblock.e2fs.e2fs_log_bsize;
323 	sblock.e2fs_bshift = LOG_MINBSIZE + sblock.e2fs.e2fs_log_bsize;
324 	sblock.e2fs_qbmask = sblock.e2fs_bsize - 1;
325 	sblock.e2fs_bmask = ~sblock.e2fs_qbmask;
326 	sblock.e2fs_ngdb = howmany(sblock.e2fs_ncg,
327 		sblock.e2fs_bsize / sizeof(struct ext2_gd));
328 	sblock.e2fs_ipb = sblock.e2fs_bsize / sizeof(struct ext2fs_dinode);
329 	sblock.e2fs_itpg = sblock.e2fs.e2fs_ipg/sblock.e2fs_ipb;
330 
331 	/*
332 	 * Compute block size that the filesystem is based on,
333 	 * according to fsbtodb, and adjust superblock block number
334 	 * so we can tell if this is an alternate later.
335 	 */
336 	super *= dev_bsize;
337 	dev_bsize = sblock.e2fs_bsize / fsbtodb(&sblock, 1);
338 	sblk.b_bno = super / dev_bsize;
339 
340 	getblk(&asblk, 1 * sblock.e2fs.e2fs_bpg + sblock.e2fs.e2fs_first_dblock,
341 		(long)SBSIZE);
342 	if (asblk.b_errs)
343 		return (0);
344 	if (bflag) {
345 		havesb = 1;
346 		return (1);
347 	}
348 
349 	/*
350 	 * Set all possible fields that could differ, then do check
351 	 * of whole super block against an alternate super block.
352 	 * When an alternate super-block is specified this check is skipped.
353 	 */
354 	asblk.b_un.b_fs->e2fs_rbcount = sblk.b_un.b_fs->e2fs_rbcount;
355 	asblk.b_un.b_fs->e2fs_fbcount = sblk.b_un.b_fs->e2fs_fbcount;
356 	asblk.b_un.b_fs->e2fs_ficount = sblk.b_un.b_fs->e2fs_ficount;
357 	asblk.b_un.b_fs->e2fs_mtime = sblk.b_un.b_fs->e2fs_mtime;
358 	asblk.b_un.b_fs->e2fs_wtime = sblk.b_un.b_fs->e2fs_wtime;
359 	asblk.b_un.b_fs->e2fs_mnt_count = sblk.b_un.b_fs->e2fs_mnt_count;
360 	asblk.b_un.b_fs->e2fs_max_mnt_count = sblk.b_un.b_fs->e2fs_max_mnt_count;
361 	asblk.b_un.b_fs->e2fs_state = sblk.b_un.b_fs->e2fs_state;
362 	asblk.b_un.b_fs->e2fs_beh = sblk.b_un.b_fs->e2fs_beh;
363 	asblk.b_un.b_fs->e2fs_lastfsck = sblk.b_un.b_fs->e2fs_lastfsck;
364 	asblk.b_un.b_fs->e2fs_fsckintv = sblk.b_un.b_fs->e2fs_fsckintv;
365 	asblk.b_un.b_fs->e2fs_ruid = sblk.b_un.b_fs->e2fs_ruid;
366 	asblk.b_un.b_fs->e2fs_rgid = sblk.b_un.b_fs->e2fs_rgid;
367 	asblk.b_un.b_fs->e2fs_block_group_nr =
368 	    sblk.b_un.b_fs->e2fs_block_group_nr;
369 	if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
370 	    ((sblock.e2fs.e2fs_features_incompat & ~EXT2F_INCOMPAT_SUPP) ||
371 	    (sblock.e2fs.e2fs_features_rocompat & ~EXT2F_ROCOMPAT_SUPP))) {
372 		if (debug) {
373 			printf("compat 0x%08x, incompat 0x%08x, compat_ro "
374 			    "0x%08x\n",
375 			    sblock.e2fs.e2fs_features_compat,
376 			    sblock.e2fs.e2fs_features_incompat,
377 			    sblock.e2fs.e2fs_features_rocompat);
378 		}
379 		badsb(listerr,"INCOMPATIBLE FEATURE BITS IN SUPER BLOCK");
380 		return 0;
381 	}
382 	if (memcmp(sblk.b_un.b_fs, asblk.b_un.b_fs, SBSIZE)) {
383 		if (debug) {
384 			u_int32_t *nlp, *olp, *endlp;
385 
386 			printf("superblock mismatches\n");
387 			nlp = (u_int32_t *)asblk.b_un.b_fs;
388 			olp = (u_int32_t *)sblk.b_un.b_fs;
389 			endlp = olp + (SBSIZE / sizeof *olp);
390 			for ( ; olp < endlp; olp++, nlp++) {
391 				if (*olp == *nlp)
392 					continue;
393 				printf("offset %ld, original %ld, alternate %ld\n",
394 					(long)(olp - (u_int32_t *)sblk.b_un.b_fs),
395 					(long)fs2h32(*olp),
396 					(long)fs2h32(*nlp));
397 			}
398 		}
399 		badsb(listerr,
400 		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
401 		return (0);
402 	}
403 	havesb = 1;
404 	return (1);
405 }
406 
407 void
408 copyback_sb(bp)
409 	struct bufarea *bp;
410 {
411 	/* Copy the in-memory superblock back to buffer */
412 	bp->b_un.b_fs->e2fs_icount = fs2h32(sblock.e2fs.e2fs_icount);
413 	bp->b_un.b_fs->e2fs_bcount = fs2h32(sblock.e2fs.e2fs_bcount);
414 	bp->b_un.b_fs->e2fs_rbcount = fs2h32(sblock.e2fs.e2fs_rbcount);
415 	bp->b_un.b_fs->e2fs_fbcount = fs2h32(sblock.e2fs.e2fs_fbcount);
416 	bp->b_un.b_fs->e2fs_ficount = fs2h32(sblock.e2fs.e2fs_ficount);
417 	bp->b_un.b_fs->e2fs_first_dblock =
418 					fs2h32(sblock.e2fs.e2fs_first_dblock);
419 	bp->b_un.b_fs->e2fs_log_bsize = fs2h32(sblock.e2fs.e2fs_log_bsize);
420 	bp->b_un.b_fs->e2fs_fsize = fs2h32(sblock.e2fs.e2fs_fsize);
421 	bp->b_un.b_fs->e2fs_bpg = fs2h32(sblock.e2fs.e2fs_bpg);
422 	bp->b_un.b_fs->e2fs_fpg = fs2h32(sblock.e2fs.e2fs_fpg);
423 	bp->b_un.b_fs->e2fs_ipg = fs2h32(sblock.e2fs.e2fs_ipg);
424 	bp->b_un.b_fs->e2fs_mtime = fs2h32(sblock.e2fs.e2fs_mtime);
425 	bp->b_un.b_fs->e2fs_wtime = fs2h32(sblock.e2fs.e2fs_wtime);
426 	bp->b_un.b_fs->e2fs_lastfsck = fs2h32(sblock.e2fs.e2fs_lastfsck);
427 	bp->b_un.b_fs->e2fs_fsckintv = fs2h32(sblock.e2fs.e2fs_fsckintv);
428 	bp->b_un.b_fs->e2fs_creator = fs2h32(sblock.e2fs.e2fs_creator);
429 	bp->b_un.b_fs->e2fs_rev = fs2h32(sblock.e2fs.e2fs_rev);
430 	bp->b_un.b_fs->e2fs_mnt_count = fs2h16(sblock.e2fs.e2fs_mnt_count);
431 	bp->b_un.b_fs->e2fs_max_mnt_count =
432 					fs2h16(sblock.e2fs.e2fs_max_mnt_count);
433 	bp->b_un.b_fs->e2fs_magic = fs2h16(sblock.e2fs.e2fs_magic);
434 	bp->b_un.b_fs->e2fs_state = fs2h16(sblock.e2fs.e2fs_state);
435 	bp->b_un.b_fs->e2fs_beh = fs2h16(sblock.e2fs.e2fs_beh);
436 	bp->b_un.b_fs->e2fs_ruid = fs2h16(sblock.e2fs.e2fs_ruid);
437 	bp->b_un.b_fs->e2fs_rgid = fs2h16(sblock.e2fs.e2fs_rgid);
438 }
439 
440 void
441 badsb(listerr, s)
442 	int listerr;
443 	char *s;
444 {
445 
446 	if (!listerr)
447 		return;
448 	if (preen)
449 		printf("%s: ", cdevname());
450 	pfatal("BAD SUPER BLOCK: %s\n", s);
451 }
452 
453 /*
454  * Calculate a prototype superblock based on information in the disk label.
455  * When done the cgsblock macro can be calculated and the fs_ncg field
456  * can be used. Do NOT attempt to use other macros without verifying that
457  * their needed information is available!
458  */
459 
460 int
461 calcsb(dev, devfd, fs)
462 	const char *dev;
463 	int devfd;
464 	struct m_ext2fs *fs;
465 {
466 	struct disklabel *lp;
467 	struct partition *pp;
468 	char *cp;
469 
470 	cp = strchr(dev, '\0') - 1;
471 	if ((cp == (char *)-1 || (*cp < 'a' || *cp > 'h')) && !isdigit(*cp)) {
472 		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
473 		return (0);
474 	}
475 	lp = getdisklabel(dev, devfd);
476 	if (isdigit(*cp))
477 		pp = &lp->d_partitions[0];
478 	else
479 		pp = &lp->d_partitions[*cp - 'a'];
480 	if (pp->p_fstype != FS_EX2FS) {
481 		pfatal("%s: NOT LABELED AS A EXT2 FILE SYSTEM (%s)\n",
482 			dev, pp->p_fstype < FSMAXTYPES ?
483 			fstypenames[pp->p_fstype] : "unknown");
484 		return (0);
485 	}
486 	memset(fs, 0, sizeof(struct m_ext2fs));
487 	fs->e2fs_bsize = pp->p_fsize;
488 	fs->e2fs.e2fs_log_bsize = pp->p_fsize / 1024;
489 	fs->e2fs.e2fs_bcount = (pp->p_size * DEV_BSIZE) / fs->e2fs_bsize;
490 	fs->e2fs.e2fs_first_dblock = (fs->e2fs.e2fs_log_bsize == 0) ? 1 : 0;
491 	fs->e2fs.e2fs_bpg = fs->e2fs_bsize * NBBY;
492 	fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize;
493 	fs->e2fs_qbmask = fs->e2fs_bsize - 1;
494 	fs->e2fs_bmask = ~fs->e2fs_qbmask;
495 	fs->e2fs_ncg =
496 		howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock,
497 		fs->e2fs.e2fs_bpg);
498 	fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + 1;
499 	fs->e2fs_ngdb = howmany(fs->e2fs_ncg,
500 		fs->e2fs_bsize / sizeof(struct ext2_gd));
501 
502 	return (1);
503 }
504 
505 static struct disklabel *
506 getdisklabel(s, fd)
507 	const char *s;
508 	int	fd;
509 {
510 	static struct disklabel lab;
511 
512 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
513 		if (s == NULL)
514 			return ((struct disklabel *)NULL);
515 		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
516 		errexit("%s: can't read disk label\n", s);
517 	}
518 	return (&lab);
519 }
520 
521 daddr_t
522 cgoverhead(c)
523 	int c;
524 {
525 	int overh;
526 	overh =	1 /* block bitmap */ +
527 		1 /* inode bitmap */ +
528 		sblock.e2fs_itpg;
529 	if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
530 	    sblock.e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
531 		if (cg_has_sb(c) == 0)
532 			return overh;
533 	}
534 	overh += 1 + sblock.e2fs_ngdb;
535 	return overh;
536 }
537