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