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