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