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