xref: /netbsd-src/sbin/fsck_ext2fs/setup.c (revision dc306354b0b29af51801a7632f1e95265a68cd81)
1 /*	$NetBSD: setup.c,v 1.8 1998/11/12 16:19:48 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 Manuel Bouyer.
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 #include <sys/cdefs.h>
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)setup.c	8.5 (Berkeley) 11/23/94";
41 #else
42 __RCSID("$NetBSD: setup.c,v 1.8 1998/11/12 16:19:48 christos Exp $");
43 #endif
44 #endif /* not lint */
45 
46 #define FSTYPENAMES
47 #include <sys/param.h>
48 #include <sys/time.h>
49 #include <ufs/ext2fs/ext2fs_dinode.h>
50 #include <ufs/ext2fs/ext2fs.h>
51 #include <sys/stat.h>
52 #include <sys/ioctl.h>
53 #include <sys/disklabel.h>
54 #include <sys/file.h>
55 
56 #include <errno.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <ctype.h>
61 
62 #include "fsck.h"
63 #include "extern.h"
64 #include "fsutil.h"
65 
66 #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
67 
68 void badsb __P((int, char *));
69 int calcsb __P((const char *, int, struct m_ext2fs *));
70 static struct disklabel *getdisklabel __P((const char *, int));
71 static int readsb __P((int));
72 
73 int
74 setup(dev)
75 	const char *dev;
76 {
77 	long cg, asked, i;
78 	long bmapsize;
79 	struct disklabel *lp;
80 	off_t sizepb;
81 	struct stat statb;
82 	struct m_ext2fs proto;
83 	int doskipclean;
84 	u_int64_t maxfilesize;
85 
86 	havesb = 0;
87 	fswritefd = -1;
88 	doskipclean = skipclean;
89 	if (stat(dev, &statb) < 0) {
90 		printf("Can't stat %s: %s\n", dev, strerror(errno));
91 		return (0);
92 	}
93 	if (!S_ISCHR(statb.st_mode)) {
94 		pfatal("%s is not a character device", dev);
95 		if (reply("CONTINUE") == 0)
96 			return (0);
97 	}
98 	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
99 		printf("Can't open %s: %s\n", dev, strerror(errno));
100 		return (0);
101 	}
102 	if (preen == 0)
103 		printf("** %s", dev);
104 	if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
105 		fswritefd = -1;
106 		if (preen)
107 			pfatal("NO WRITE ACCESS");
108 		printf(" (NO WRITE)");
109 	}
110 	if (preen == 0)
111 		printf("\n");
112 	fsmodified = 0;
113 	lfdir = 0;
114 	initbarea(&sblk);
115 	initbarea(&asblk);
116 	sblk.b_un.b_buf = malloc(SBSIZE);
117 	asblk.b_un.b_buf = malloc(SBSIZE);
118 	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
119 		errexit("cannot allocate space for superblock\n");
120 	if ((lp = getdisklabel((char *)NULL, fsreadfd)) != NULL)
121 		dev_bsize = secsize = lp->d_secsize;
122 	else
123 		dev_bsize = secsize = DEV_BSIZE;
124 	/*
125 	 * Read in the superblock, looking for alternates if necessary
126 	 */
127 	if (readsb(1) == 0) {
128 		if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
129 			return(0);
130 		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
131 			return (0);
132 		for (cg = 1; cg < proto.e2fs_ncg; cg++) {
133 			bflag = fsbtodb(&proto,
134 				cg * proto.e2fs.e2fs_bpg + proto.e2fs.e2fs_first_dblock);
135 			if (readsb(0) != 0)
136 				break;
137 		}
138 		if (cg >= proto.e2fs_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_ext2fs(8).");
146 			return(0);
147 		}
148 		doskipclean = 0;
149 		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
150 	}
151 	if (debug)
152 		printf("state = %d\n", sblock.e2fs.e2fs_state);
153 	if (sblock.e2fs.e2fs_state == E2FS_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.e2fs.e2fs_bcount;
163 	maxino = sblock.e2fs_ncg * sblock.e2fs.e2fs_ipg;
164 	sizepb = sblock.e2fs_bsize;
165 	maxfilesize = sblock.e2fs_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.e2fs.e2fs_rbcount < 0) ||
174 			(sblock.e2fs.e2fs_rbcount > sblock.e2fs.e2fs_bcount)) {
175 		pfatal("IMPOSSIBLE RESERVED BLOCK COUNT=%d IN SUPERBLOCK",
176 			sblock.e2fs.e2fs_rbcount);
177 		if (reply("SET TO DEFAULT") == 1) {
178 			sblock.e2fs.e2fs_rbcount = sblock.e2fs.e2fs_bcount * 0.1;
179 			sbdirty();
180 			dirty(&asblk);
181 		}
182 	}
183 	if (sblock.e2fs.e2fs_bpg != sblock.e2fs.e2fs_fpg) {
184 		pfatal("WRONG FPG=%d (BPG=%d) IN SUPERBLOCK",
185 			sblock.e2fs.e2fs_fpg, sblock.e2fs.e2fs_bpg);
186 		return 0;
187 	}
188 	if (asblk.b_dirty && !bflag) {
189 		copyback_sb(&asblk);
190 		flush(fswritefd, &asblk);
191 	}
192 	/*
193 	 * read in the summary info.
194 	 */
195 
196 	sblock.e2fs_gd = malloc(sblock.e2fs_ngdb * sblock.e2fs_bsize);
197 	asked = 0;
198 	for (i=0; i < sblock.e2fs_ngdb; i++) {
199 		if (bread(fsreadfd,(char *)
200 			&sblock.e2fs_gd[i* sblock.e2fs_bsize / sizeof(struct ext2_gd)],
201 			fsbtodb(&sblock, ((sblock.e2fs_bsize>1024)?0:1)+i+1),
202 			sblock.e2fs_bsize) != 0 && !asked) {
203 			pfatal("BAD SUMMARY INFORMATION");
204 			if (reply("CONTINUE") == 0)
205 				errexit("%s\n", "");
206 			asked++;
207 		}
208 	}
209 	/*
210 	 * allocate and initialize the necessary maps
211 	 */
212 	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
213 	blockmap = calloc((unsigned)bmapsize, sizeof (char));
214 	if (blockmap == NULL) {
215 		printf("cannot alloc %u bytes for blockmap\n",
216 			(unsigned)bmapsize);
217 		goto badsblabel;
218 	}
219 	statemap = calloc((unsigned)(maxino + 2), sizeof(char));
220 	if (statemap == NULL) {
221 		printf("cannot alloc %u bytes for statemap\n",
222 			(unsigned)(maxino + 1));
223 		goto badsblabel;
224 	}
225 	lncntp = (int16_t *)calloc((unsigned)(maxino + 1), sizeof(int16_t));
226 	if (lncntp == NULL) {
227 		printf("cannot alloc %u bytes for lncntp\n",
228 			(unsigned)((maxino + 1) * sizeof(int16_t)));
229 		goto badsblabel;
230 	}
231 	for (numdirs = 0, cg = 0; cg < sblock.e2fs_ncg; cg++) {
232 		numdirs += fs2h16(sblock.e2fs_gd[cg].ext2bgd_ndirs);
233 	}
234 	inplast = 0;
235 	listmax = numdirs + 10;
236 	inpsort = (struct inoinfo **)calloc((unsigned)listmax,
237 		sizeof(struct inoinfo *));
238 	inphead = (struct inoinfo **)calloc((unsigned)numdirs,
239 		sizeof(struct inoinfo *));
240 	if (inpsort == NULL || inphead == NULL) {
241 		printf("cannot alloc %u bytes for inphead\n",
242 			(unsigned)(numdirs * sizeof(struct inoinfo *)));
243 		goto badsblabel;
244 	}
245 	bufinit();
246 	return (1);
247 
248 badsblabel:
249 	ckfini(0);
250 	return (0);
251 }
252 
253 /*
254  * Read in the super block and its summary info, convert to host byte order.
255  */
256 static int
257 readsb(listerr)
258 	int listerr;
259 {
260 	daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
261 
262 	if (bread(fsreadfd, (char *)sblk.b_un.b_fs, super, (long)SBSIZE) != 0)
263 		return (0);
264 	sblk.b_bno = super;
265 	sblk.b_size = SBSIZE;
266 
267 	/* Copy the superblock in memory */
268 	sblock.e2fs.e2fs_icount = fs2h32(sblk.b_un.b_fs->e2fs_icount);
269 	sblock.e2fs.e2fs_bcount = fs2h32(sblk.b_un.b_fs->e2fs_bcount);
270 	sblock.e2fs.e2fs_rbcount = fs2h32(sblk.b_un.b_fs->e2fs_rbcount);
271 	sblock.e2fs.e2fs_fbcount = fs2h32(sblk.b_un.b_fs->e2fs_fbcount);
272 	sblock.e2fs.e2fs_ficount = fs2h32(sblk.b_un.b_fs->e2fs_ficount);
273 	sblock.e2fs.e2fs_first_dblock = fs2h32(sblk.b_un.b_fs->e2fs_first_dblock);
274 	sblock.e2fs.e2fs_log_bsize = fs2h32(sblk.b_un.b_fs->e2fs_log_bsize);
275 	sblock.e2fs.e2fs_fsize = fs2h32(sblk.b_un.b_fs->e2fs_fsize);
276 	sblock.e2fs.e2fs_bpg = fs2h32(sblk.b_un.b_fs->e2fs_bpg);
277 	sblock.e2fs.e2fs_fpg = fs2h32(sblk.b_un.b_fs->e2fs_fpg);
278 	sblock.e2fs.e2fs_ipg = fs2h32(sblk.b_un.b_fs->e2fs_ipg);
279 	sblock.e2fs.e2fs_mtime = fs2h32(sblk.b_un.b_fs->e2fs_mtime);
280 	sblock.e2fs.e2fs_wtime = fs2h32(sblk.b_un.b_fs->e2fs_wtime);
281 	sblock.e2fs.e2fs_lastfsck = fs2h32(sblk.b_un.b_fs->e2fs_lastfsck);
282 	sblock.e2fs.e2fs_fsckintv = fs2h32(sblk.b_un.b_fs->e2fs_fsckintv);
283 	sblock.e2fs.e2fs_creator = fs2h32(sblk.b_un.b_fs->e2fs_creator);
284 	sblock.e2fs.e2fs_rev = fs2h32(sblk.b_un.b_fs->e2fs_rev);
285 	sblock.e2fs.e2fs_mnt_count = fs2h16(sblk.b_un.b_fs->e2fs_mnt_count);
286 	sblock.e2fs.e2fs_max_mnt_count = fs2h16(sblk.b_un.b_fs->e2fs_max_mnt_count);
287 	sblock.e2fs.e2fs_magic = fs2h16(sblk.b_un.b_fs->e2fs_magic);
288 	sblock.e2fs.e2fs_state = fs2h16(sblk.b_un.b_fs->e2fs_state);
289 	sblock.e2fs.e2fs_beh = fs2h16(sblk.b_un.b_fs->e2fs_beh);
290 	sblock.e2fs.e2fs_ruid = fs2h16(sblk.b_un.b_fs->e2fs_ruid);
291 	sblock.e2fs.e2fs_rgid = fs2h16(sblk.b_un.b_fs->e2fs_rgid);
292 
293 	/*
294 	 * run a few consistency checks of the super block
295 	 */
296 	if (sblock.e2fs.e2fs_magic != E2FS_MAGIC) {
297 		badsb(listerr, "MAGIC NUMBER WRONG"); return (0);
298 	}
299 	if (sblock.e2fs.e2fs_log_bsize > 2) {
300 		badsb(listerr, "BAD LOG_BSIZE"); return (0);
301 	}
302 
303 	/* compute the dynamic fields of the in-memory sb */
304 	/* compute dynamic sb infos */
305 	sblock.e2fs_ncg =
306 		howmany(sblock.e2fs.e2fs_bcount - sblock.e2fs.e2fs_first_dblock,
307 		sblock.e2fs.e2fs_bpg);
308 	/* XXX assume hw bsize = 512 */
309 	sblock.e2fs_fsbtodb = sblock.e2fs.e2fs_log_bsize + 1;
310 	sblock.e2fs_bsize = 1024 << sblock.e2fs.e2fs_log_bsize;
311 	sblock.e2fs_bshift = LOG_MINBSIZE + sblock.e2fs.e2fs_log_bsize;
312 	sblock.e2fs_qbmask = sblock.e2fs_bsize - 1;
313 	sblock.e2fs_bmask = ~sblock.e2fs_qbmask;
314 	sblock.e2fs_ngdb = howmany(sblock.e2fs_ncg,
315 		sblock.e2fs_bsize / sizeof(struct ext2_gd));
316 	sblock.e2fs_ipb = sblock.e2fs_bsize / sizeof(struct ext2fs_dinode);
317 	sblock.e2fs_itpg = sblock.e2fs.e2fs_ipg/sblock.e2fs_ipb;
318 
319 	cgoverhead =	1 /* super block */ +
320 					sblock.e2fs_ngdb +
321 					1 /* block bitmap */ +
322 					1 /* inode bitmap */ +
323 					sblock.e2fs_itpg;
324 
325 	if (debug) /* DDD */
326 		printf("cg overhead %d blocks \n", cgoverhead);
327 	/*
328 	 * Compute block size that the filesystem is based on,
329 	 * according to fsbtodb, and adjust superblock block number
330 	 * so we can tell if this is an alternate later.
331 	 */
332 	super *= dev_bsize;
333 	dev_bsize = sblock.e2fs_bsize / fsbtodb(&sblock, 1);
334 	sblk.b_bno = super / dev_bsize;
335 
336 	getblk(&asblk, 1 * sblock.e2fs.e2fs_bpg + sblock.e2fs.e2fs_first_dblock,
337 		(long)SBSIZE);
338 	if (asblk.b_errs)
339 		return (0);
340 	if (bflag) {
341 		havesb = 1;
342 		return (1);
343 	}
344 
345 	/*
346 	 * Set all possible fields that could differ, then do check
347 	 * of whole super block against an alternate super block.
348 	 * When an alternate super-block is specified this check is skipped.
349 	 */
350 	asblk.b_un.b_fs->e2fs_rbcount = sblk.b_un.b_fs->e2fs_rbcount;
351 	asblk.b_un.b_fs->e2fs_fbcount = sblk.b_un.b_fs->e2fs_fbcount;
352 	asblk.b_un.b_fs->e2fs_ficount = sblk.b_un.b_fs->e2fs_ficount;
353 	asblk.b_un.b_fs->e2fs_mtime = sblk.b_un.b_fs->e2fs_mtime;
354 	asblk.b_un.b_fs->e2fs_wtime = sblk.b_un.b_fs->e2fs_wtime;
355 	asblk.b_un.b_fs->e2fs_mnt_count = sblk.b_un.b_fs->e2fs_mnt_count;
356 	asblk.b_un.b_fs->e2fs_max_mnt_count = sblk.b_un.b_fs->e2fs_max_mnt_count;
357 	asblk.b_un.b_fs->e2fs_state = sblk.b_un.b_fs->e2fs_state;
358 	asblk.b_un.b_fs->e2fs_beh = sblk.b_un.b_fs->e2fs_beh;
359 	asblk.b_un.b_fs->e2fs_lastfsck = sblk.b_un.b_fs->e2fs_lastfsck;
360 	asblk.b_un.b_fs->e2fs_fsckintv = sblk.b_un.b_fs->e2fs_fsckintv;
361 	asblk.b_un.b_fs->e2fs_ruid = sblk.b_un.b_fs->e2fs_ruid;
362 	asblk.b_un.b_fs->e2fs_rgid = sblk.b_un.b_fs->e2fs_rgid;
363 	if (memcmp(sblk.b_un.b_fs, asblk.b_un.b_fs, SBSIZE)) {
364 		if (debug) {
365 			u_int32_t *nlp, *olp, *endlp;
366 
367 			printf("superblock mismatches\n");
368 			nlp = (u_int32_t *)asblk.b_un.b_fs;
369 			olp = (u_int32_t *)sblk.b_un.b_fs;
370 			endlp = olp + (SBSIZE / sizeof *olp);
371 			for ( ; olp < endlp; olp++, nlp++) {
372 				if (*olp == *nlp)
373 					continue;
374 				printf("offset %ld, original %ld, alternate %ld\n",
375 					(long)(olp - (u_int32_t *)sblk.b_un.b_fs),
376 					(long)fs2h32(*olp),
377 					(long)fs2h32(*nlp));
378 			}
379 		}
380 		badsb(listerr,
381 		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
382 		return (0);
383 	}
384 	havesb = 1;
385 	return (1);
386 }
387 
388 void
389 copyback_sb(bp)
390 	struct bufarea *bp;
391 {
392 	/* Copy the in-memory superblock back to buffer */
393 	bp->b_un.b_fs->e2fs_icount = fs2h32(sblock.e2fs.e2fs_icount);
394 	bp->b_un.b_fs->e2fs_bcount = fs2h32(sblock.e2fs.e2fs_bcount);
395 	bp->b_un.b_fs->e2fs_rbcount = fs2h32(sblock.e2fs.e2fs_rbcount);
396 	bp->b_un.b_fs->e2fs_fbcount = fs2h32(sblock.e2fs.e2fs_fbcount);
397 	bp->b_un.b_fs->e2fs_ficount = fs2h32(sblock.e2fs.e2fs_ficount);
398 	bp->b_un.b_fs->e2fs_first_dblock =
399 								fs2h32(sblock.e2fs.e2fs_first_dblock);
400 	bp->b_un.b_fs->e2fs_log_bsize = fs2h32(sblock.e2fs.e2fs_log_bsize);
401 	bp->b_un.b_fs->e2fs_fsize = fs2h32(sblock.e2fs.e2fs_fsize);
402 	bp->b_un.b_fs->e2fs_bpg = fs2h32(sblock.e2fs.e2fs_bpg);
403 	bp->b_un.b_fs->e2fs_fpg = fs2h32(sblock.e2fs.e2fs_fpg);
404 	bp->b_un.b_fs->e2fs_ipg = fs2h32(sblock.e2fs.e2fs_ipg);
405 	bp->b_un.b_fs->e2fs_mtime = fs2h32(sblock.e2fs.e2fs_mtime);
406 	bp->b_un.b_fs->e2fs_wtime = fs2h32(sblock.e2fs.e2fs_wtime);
407 	bp->b_un.b_fs->e2fs_lastfsck = fs2h32(sblock.e2fs.e2fs_lastfsck);
408 	bp->b_un.b_fs->e2fs_fsckintv = fs2h32(sblock.e2fs.e2fs_fsckintv);
409 	bp->b_un.b_fs->e2fs_creator = fs2h32(sblock.e2fs.e2fs_creator);
410 	bp->b_un.b_fs->e2fs_rev = fs2h32(sblock.e2fs.e2fs_rev);
411 	bp->b_un.b_fs->e2fs_mnt_count = fs2h16(sblock.e2fs.e2fs_mnt_count);
412 	bp->b_un.b_fs->e2fs_max_mnt_count =
413 								fs2h16(sblock.e2fs.e2fs_max_mnt_count);
414 	bp->b_un.b_fs->e2fs_magic = fs2h16(sblock.e2fs.e2fs_magic);
415 	bp->b_un.b_fs->e2fs_state = fs2h16(sblock.e2fs.e2fs_state);
416 	bp->b_un.b_fs->e2fs_beh = fs2h16(sblock.e2fs.e2fs_beh);
417 	bp->b_un.b_fs->e2fs_ruid = fs2h16(sblock.e2fs.e2fs_ruid);
418 	bp->b_un.b_fs->e2fs_rgid = fs2h16(sblock.e2fs.e2fs_rgid);
419 }
420 
421 void
422 badsb(listerr, s)
423 	int listerr;
424 	char *s;
425 {
426 
427 	if (!listerr)
428 		return;
429 	if (preen)
430 		printf("%s: ", cdevname());
431 	pfatal("BAD SUPER BLOCK: %s\n", s);
432 }
433 
434 /*
435  * Calculate a prototype superblock based on information in the disk label.
436  * When done the cgsblock macro can be calculated and the fs_ncg field
437  * can be used. Do NOT attempt to use other macros without verifying that
438  * their needed information is available!
439  */
440 
441 int
442 calcsb(dev, devfd, fs)
443 	const char *dev;
444 	int devfd;
445 	struct m_ext2fs *fs;
446 {
447 	struct disklabel *lp;
448 	struct partition *pp;
449 	char *cp;
450 
451 	cp = strchr(dev, '\0') - 1;
452 	if ((cp == (char *)-1 || (*cp < 'a' || *cp > 'h')) && !isdigit(*cp)) {
453 		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
454 		return (0);
455 	}
456 	lp = getdisklabel(dev, devfd);
457 	if (isdigit(*cp))
458 		pp = &lp->d_partitions[0];
459 	else
460 		pp = &lp->d_partitions[*cp - 'a'];
461 	if (pp->p_fstype != FS_EX2FS) {
462 		pfatal("%s: NOT LABELED AS A EXT2 FILE SYSTEM (%s)\n",
463 			dev, pp->p_fstype < FSMAXTYPES ?
464 			fstypenames[pp->p_fstype] : "unknown");
465 		return (0);
466 	}
467 	memset(fs, 0, sizeof(struct m_ext2fs));
468 	fs->e2fs_bsize = pp->p_fsize;
469 	fs->e2fs.e2fs_log_bsize = pp->p_fsize / 1024;
470 	fs->e2fs.e2fs_bcount = (pp->p_size * DEV_BSIZE) / fs->e2fs_bsize;
471 	fs->e2fs.e2fs_first_dblock = (fs->e2fs.e2fs_log_bsize == 0) ? 1 : 0;
472 	fs->e2fs.e2fs_bpg = fs->e2fs_bsize * NBBY;
473 	fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize;
474 	fs->e2fs_qbmask = fs->e2fs_bsize - 1;
475 	fs->e2fs_bmask = ~fs->e2fs_qbmask;
476 	fs->e2fs_ncg =
477 		howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock,
478 		fs->e2fs.e2fs_bpg);
479 	fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + 1;
480 	fs->e2fs_ngdb = howmany(fs->e2fs_ncg,
481 		fs->e2fs_bsize / sizeof(struct ext2_gd));
482 
483 	return (1);
484 }
485 
486 static struct disklabel *
487 getdisklabel(s, fd)
488 	const char *s;
489 	int	fd;
490 {
491 	static struct disklabel lab;
492 
493 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
494 		if (s == NULL)
495 			return ((struct disklabel *)NULL);
496 		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
497 		errexit("%s: can't read disk label\n", s);
498 	}
499 	return (&lab);
500 }
501