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