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