setup.c revision 75557
189857Sobrien/* 2218822Sdim * Copyright (c) 1980, 1986, 1993 389857Sobrien * The Regents of the University of California. All rights reserved. 489857Sobrien * 589857Sobrien * Redistribution and use in source and binary forms, with or without 689857Sobrien * modification, are permitted provided that the following conditions 789857Sobrien * are met: 8218822Sdim * 1. Redistributions of source code must retain the above copyright 989857Sobrien * notice, this list of conditions and the following disclaimer. 10218822Sdim * 2. Redistributions in binary form must reproduce the above copyright 11218822Sdim * notice, this list of conditions and the following disclaimer in the 12218822Sdim * documentation and/or other materials provided with the distribution. 13218822Sdim * 3. All advertising materials mentioning features or use of this software 1489857Sobrien * must display the following acknowledgement: 15218822Sdim * This product includes software developed by the University of 16218822Sdim * California, Berkeley and its contributors. 17218822Sdim * 4. Neither the name of the University nor the names of its contributors 18218822Sdim * may be used to endorse or promote products derived from this software 1989857Sobrien * without specific prior written permission. 20218822Sdim * 21218822Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22218822Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2389857Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2489857Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2589857Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26218822Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2789857Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2889857Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2989857Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3089857Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3189857Sobrien * SUCH DAMAGE. 3289857Sobrien */ 3389857Sobrien 34130561Sobrien#ifndef lint 35130561Sobrien#if 0 36130561Sobrienstatic const char sccsid[] = "@(#)setup.c 8.10 (Berkeley) 5/9/95"; 3789857Sobrien#endif 3889857Sobrienstatic const char rcsid[] = 3989857Sobrien "$FreeBSD: head/sbin/fsck_ffs/setup.c 75557 2001-04-16 22:22:21Z mckusick $"; 40130561Sobrien#endif /* not lint */ 41130561Sobrien 4289857Sobrien#define DKTYPENAMES 4389857Sobrien#include <sys/param.h> 4489857Sobrien#include <sys/stat.h> 4589857Sobrien#include <sys/disklabel.h> 4689857Sobrien#include <sys/file.h> 4789857Sobrien 4889857Sobrien#include <ufs/ufs/dinode.h> 4989857Sobrien#include <ufs/ffs/fs.h> 5089857Sobrien 5189857Sobrien#include <ctype.h> 5289857Sobrien#include <err.h> 5389857Sobrien#include <errno.h> 5489857Sobrien#include <string.h> 5589857Sobrien 5689857Sobrien#include "fsck.h" 5789857Sobrien 58130561Sobrienstruct bufarea asblk; 5989857Sobrien#define altsblock (*asblk.b_un.b_fs) 60130561Sobrien#define POWEROF2(num) (((num) & ((num) - 1)) == 0) 6189857Sobrien 62130561Sobrienstatic void badsb __P((int listerr, char *s)); 6389857Sobrienstatic int calcsb __P((char *dev, int devfd, struct fs *fs)); 6489857Sobrienstatic struct disklabel *getdisklabel __P((char *s, int fd)); 65130561Sobrien 6689857Sobrien/* 6789857Sobrien * Read in a superblock finding an alternate if necessary. 68218822Sdim * Return 1 if successful, 0 if unsuccessful, -1 if filesystem 6989857Sobrien * is already clean (preen mode only). 7089857Sobrien */ 71218822Sdimint 7289857Sobriensetup(dev) 73130561Sobrien char *dev; 74130561Sobrien{ 7589857Sobrien long cg, size, asked, i, j; 7689857Sobrien long bmapsize; 7789857Sobrien off_t sizepb; 7889857Sobrien struct stat statb; 79130561Sobrien struct fs proto; 8089857Sobrien 81130561Sobrien havesb = 0; 8289857Sobrien fswritefd = -1; 83130561Sobrien cursnapshot = 0; 8489857Sobrien if (stat(dev, &statb) < 0) { 8589857Sobrien printf("Can't stat %s: %s\n", dev, strerror(errno)); 8689857Sobrien if (bkgrdflag) { 87130561Sobrien unlink(snapname); 8889857Sobrien bkgrdflag = 0; 8989857Sobrien } 9089857Sobrien return (0); 9189857Sobrien } 9289857Sobrien if ((statb.st_mode & S_IFMT) != S_IFCHR && 9389857Sobrien (statb.st_mode & S_IFMT) != S_IFBLK) { 9489857Sobrien if (bkgrdflag != 0 && (statb.st_flags & SF_SNAPSHOT) == 0) { 9589857Sobrien unlink(snapname); 9689857Sobrien printf("background fsck lacks a snapshot\n"); 97130561Sobrien exit(EEXIT); 9889857Sobrien } 99130561Sobrien if ((statb.st_flags & SF_SNAPSHOT) != 0 && cvtlevel == 0) { 10089857Sobrien cursnapshot = statb.st_ino; 10189857Sobrien } else { 10289857Sobrien if (cvtlevel == 0 || 103130561Sobrien (statb.st_flags & SF_SNAPSHOT) == 0) { 10489857Sobrien if (preen && bkgrdflag) { 10589857Sobrien unlink(snapname); 10689857Sobrien bkgrdflag = 0; 10789857Sobrien } 10889857Sobrien pfatal("%s is not a disk device", dev); 10989857Sobrien if (reply("CONTINUE") == 0) { 11089857Sobrien if (bkgrdflag) { 11189857Sobrien unlink(snapname); 11289857Sobrien bkgrdflag = 0; 11389857Sobrien } 11489857Sobrien return (0); 11589857Sobrien } 11689857Sobrien } else { 11789857Sobrien if (bkgrdflag) { 11889857Sobrien unlink(snapname); 11989857Sobrien bkgrdflag = 0; 12089857Sobrien } 12189857Sobrien pfatal("cannot convert a snapshot"); 12289857Sobrien exit(EEXIT); 12389857Sobrien } 12489857Sobrien } 12589857Sobrien } 12689857Sobrien if ((fsreadfd = open(dev, O_RDONLY)) < 0) { 12789857Sobrien if (bkgrdflag) { 12889857Sobrien unlink(snapname); 129130561Sobrien bkgrdflag = 0; 13089857Sobrien } 13189857Sobrien printf("Can't open %s: %s\n", dev, strerror(errno)); 132130561Sobrien return (0); 13389857Sobrien } 13489857Sobrien if (bkgrdflag) { 13589857Sobrien unlink(snapname); 13689857Sobrien size = MIBSIZE; 13789857Sobrien if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0|| 138130561Sobrien sysctlnametomib("vfs.ffs.adjblkcnt", adjblkcnt, &size) < 0|| 13989857Sobrien sysctlnametomib("vfs.ffs.freefiles", freefiles, &size) < 0|| 14089857Sobrien sysctlnametomib("vfs.ffs.freedirs", freedirs, &size) < 0 || 14189857Sobrien sysctlnametomib("vfs.ffs.freeblks", freeblks, &size) < 0) { 14289857Sobrien pfatal("kernel lacks background fsck support\n"); 14389857Sobrien exit(EEXIT); 14489857Sobrien } 145130561Sobrien cmd.version = FFS_CMD_VERSION; 146130561Sobrien cmd.handle = fsreadfd; 147130561Sobrien fswritefd = -1; 148130561Sobrien } 149130561Sobrien if (preen == 0) 150130561Sobrien printf("** %s", dev); 15189857Sobrien if (bkgrdflag == 0 && 15289857Sobrien (nflag || (fswritefd = open(dev, O_WRONLY)) < 0)) { 15389857Sobrien fswritefd = -1; 15489857Sobrien if (preen) 15589857Sobrien pfatal("NO WRITE ACCESS"); 15689857Sobrien printf(" (NO WRITE)"); 15789857Sobrien } 15889857Sobrien if (preen == 0) 15989857Sobrien printf("\n"); 16089857Sobrien /* 16189857Sobrien * Read in the superblock, looking for alternates if necessary 16289857Sobrien */ 16389857Sobrien if (readsb(1) == 0) { 16489857Sobrien skipclean = 0; 16589857Sobrien if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0) 16689857Sobrien return(0); 16789857Sobrien if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0) 16889857Sobrien return (0); 16989857Sobrien for (cg = 0; cg < proto.fs_ncg; cg++) { 17089857Sobrien bflag = fsbtodb(&proto, cgsblock(&proto, cg)); 171218822Sdim if (readsb(0) != 0) 172218822Sdim break; 173218822Sdim } 174218822Sdim if (cg >= proto.fs_ncg) { 175218822Sdim printf("%s %s\n%s %s\n%s %s\n", 176218822Sdim "SEARCH FOR ALTERNATE SUPER-BLOCK", 17789857Sobrien "FAILED. YOU MUST USE THE", 178218822Sdim "-b OPTION TO FSCK TO SPECIFY THE", 179218822Sdim "LOCATION OF AN ALTERNATE", 180218822Sdim "SUPER-BLOCK TO SUPPLY NEEDED", 181218822Sdim "INFORMATION; SEE fsck(8)."); 18289857Sobrien bflag = 0; 18389857Sobrien return(0); 18489857Sobrien } 185130561Sobrien pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag); 18689857Sobrien bflag = 0; 187130561Sobrien } 18889857Sobrien if (skipclean && preen && sblock.fs_clean) { 18989857Sobrien pwarn("FILESYSTEM CLEAN; SKIPPING CHECKS\n"); 190130561Sobrien return (-1); 191130561Sobrien } 19289857Sobrien maxfsblock = sblock.fs_size; 19389857Sobrien maxino = sblock.fs_ncg * sblock.fs_ipg; 19489857Sobrien /* 19589857Sobrien * Check and potentially fix certain fields in the super block. 19689857Sobrien */ 19789857Sobrien if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) { 19889857Sobrien pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK"); 19989857Sobrien if (reply("SET TO DEFAULT") == 1) { 20089857Sobrien sblock.fs_optim = FS_OPTTIME; 201130561Sobrien sbdirty(); 20289857Sobrien } 20389857Sobrien } 20489857Sobrien if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) { 20589857Sobrien pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK", 20689857Sobrien sblock.fs_minfree); 20789857Sobrien if (reply("SET TO DEFAULT") == 1) { 20889857Sobrien sblock.fs_minfree = 10; 209130561Sobrien sbdirty(); 210130561Sobrien } 21189857Sobrien } 21289857Sobrien if (sblock.fs_interleave < 1 || 21389857Sobrien sblock.fs_interleave > sblock.fs_nsect) { 21489857Sobrien pfatal("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK", 21589857Sobrien sblock.fs_interleave); 21689857Sobrien sblock.fs_interleave = 1; 21789857Sobrien if (preen) 218218822Sdim printf(" (FIXED)\n"); 21989857Sobrien if (preen || reply("SET TO DEFAULT") == 1) { 22089857Sobrien sbdirty(); 22189857Sobrien dirty(&asblk); 22289857Sobrien } 22389857Sobrien } 22489857Sobrien if (sblock.fs_npsect < sblock.fs_nsect || 22589857Sobrien sblock.fs_npsect > sblock.fs_nsect*2) { 226130561Sobrien pfatal("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK", 227130561Sobrien sblock.fs_npsect); 22889857Sobrien sblock.fs_npsect = sblock.fs_nsect; 22989857Sobrien if (preen) 23089857Sobrien printf(" (FIXED)\n"); 23189857Sobrien if (preen || reply("SET TO DEFAULT") == 1) { 23289857Sobrien sbdirty(); 23389857Sobrien dirty(&asblk); 234130561Sobrien } 235130561Sobrien } 23689857Sobrien if (sblock.fs_inodefmt >= FS_44INODEFMT) { 23789857Sobrien newinofmt = 1; 23889857Sobrien } else { 239130561Sobrien sblock.fs_qbmask = ~sblock.fs_bmask; 24089857Sobrien sblock.fs_qfmask = ~sblock.fs_fmask; 241 /* This should match the kernel limit in ffs_oldfscompat(). */ 242 sblock.fs_maxfilesize = (u_int64_t)1 << 39; 243 newinofmt = 0; 244 } 245 /* 246 * Convert to new inode format. 247 */ 248 if (cvtlevel >= 2 && sblock.fs_inodefmt < FS_44INODEFMT) { 249 if (preen) 250 pwarn("CONVERTING TO NEW INODE FORMAT\n"); 251 else if (!reply("CONVERT TO NEW INODE FORMAT")) 252 return(0); 253 doinglevel2++; 254 sblock.fs_inodefmt = FS_44INODEFMT; 255 sizepb = sblock.fs_bsize; 256 sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1; 257 for (i = 0; i < NIADDR; i++) { 258 sizepb *= NINDIR(&sblock); 259 sblock.fs_maxfilesize += sizepb; 260 } 261 sblock.fs_maxsymlinklen = MAXSYMLINKLEN; 262 sblock.fs_qbmask = ~sblock.fs_bmask; 263 sblock.fs_qfmask = ~sblock.fs_fmask; 264 sbdirty(); 265 dirty(&asblk); 266 } 267 /* 268 * Convert to new cylinder group format. 269 */ 270 if (cvtlevel >= 1 && sblock.fs_postblformat == FS_42POSTBLFMT) { 271 if (preen) 272 pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n"); 273 else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT")) 274 return(0); 275 doinglevel1++; 276 sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT; 277 sblock.fs_nrpos = 8; 278 sblock.fs_postbloff = 279 (char *)(&sblock.fs_opostbl[0][0]) - 280 (char *)(&sblock.fs_firstfield); 281 sblock.fs_rotbloff = &sblock.fs_space[0] - 282 (u_char *)(&sblock.fs_firstfield); 283 sblock.fs_cgsize = 284 fragroundup(&sblock, CGSIZE(&sblock)); 285 sbdirty(); 286 dirty(&asblk); 287 } 288 if (asblk.b_dirty && !bflag) { 289 memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize); 290 flush(fswritefd, &asblk); 291 } 292 /* 293 * read in the summary info. 294 */ 295 asked = 0; 296 sblock.fs_csp = calloc(1, sblock.fs_cssize); 297 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) { 298 size = sblock.fs_cssize - i < sblock.fs_bsize ? 299 sblock.fs_cssize - i : sblock.fs_bsize; 300 if (bread(fsreadfd, (char *)sblock.fs_csp + i, 301 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag), 302 size) != 0 && !asked) { 303 pfatal("BAD SUMMARY INFORMATION"); 304 if (reply("CONTINUE") == 0) { 305 ckfini(0); 306 exit(EEXIT); 307 } 308 asked++; 309 } 310 } 311 /* 312 * allocate and initialize the necessary maps 313 */ 314 bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(short)); 315 blockmap = calloc((unsigned)bmapsize, sizeof (char)); 316 if (blockmap == NULL) { 317 printf("cannot alloc %u bytes for blockmap\n", 318 (unsigned)bmapsize); 319 goto badsb; 320 } 321 inostathead = calloc((unsigned)(sblock.fs_ncg), 322 sizeof(struct inostatlist)); 323 if (inostathead == NULL) { 324 printf("cannot alloc %u bytes for inostathead\n", 325 (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg))); 326 goto badsb; 327 } 328 numdirs = sblock.fs_cstotal.cs_ndir; 329 dirhash = numdirs; 330 if (numdirs == 0) { 331 printf("numdirs is zero, try using an alternate superblock\n"); 332 goto badsb; 333 } 334 inplast = 0; 335 listmax = numdirs + 10; 336 inpsort = (struct inoinfo **)calloc((unsigned)listmax, 337 sizeof(struct inoinfo *)); 338 inphead = (struct inoinfo **)calloc((unsigned)numdirs, 339 sizeof(struct inoinfo *)); 340 if (inpsort == NULL || inphead == NULL) { 341 printf("cannot alloc %u bytes for inphead\n", 342 (unsigned)numdirs * sizeof(struct inoinfo *)); 343 goto badsb; 344 } 345 bufinit(); 346 if (sblock.fs_flags & FS_DOSOFTDEP) 347 usedsoftdep = 1; 348 else 349 usedsoftdep = 0; 350 return (1); 351 352badsb: 353 ckfini(0); 354 return (0); 355} 356 357/* 358 * Read in the super block and its summary info. 359 */ 360int 361readsb(listerr) 362 int listerr; 363{ 364 ufs_daddr_t super = bflag ? bflag : SBOFF / dev_bsize; 365 366 if (bread(fsreadfd, (char *)&sblock, super, (long)SBSIZE) != 0) 367 return (0); 368 sblk.b_bno = super; 369 sblk.b_size = SBSIZE; 370 /* 371 * run a few consistency checks of the super block 372 */ 373 if (sblock.fs_magic != FS_MAGIC) 374 { badsb(listerr, "MAGIC NUMBER WRONG"); return (0); } 375 if (sblock.fs_ncg < 1) 376 { badsb(listerr, "NCG OUT OF RANGE"); return (0); } 377 if (sblock.fs_cpg < 1) 378 { badsb(listerr, "CPG OUT OF RANGE"); return (0); } 379 if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl || 380 (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl) 381 { badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); } 382 if (sblock.fs_sbsize > SBSIZE) 383 { badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); } 384 /* 385 * Compute block size that the filesystem is based on, 386 * according to fsbtodb, and adjust superblock block number 387 * so we can tell if this is an alternate later. 388 */ 389 super *= dev_bsize; 390 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1); 391 sblk.b_bno = super / dev_bsize; 392 if (bflag) { 393 havesb = 1; 394 return (1); 395 } 396 /* 397 * Set all possible fields that could differ, then do check 398 * of whole super block against an alternate super block. 399 * When an alternate super-block is specified this check is skipped. 400 */ 401 getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize); 402 if (asblk.b_errs) 403 return (0); 404 altsblock.fs_firstfield = sblock.fs_firstfield; 405 altsblock.fs_unused_1 = sblock.fs_unused_1; 406 altsblock.fs_time = sblock.fs_time; 407 altsblock.fs_cstotal = sblock.fs_cstotal; 408 altsblock.fs_cgrotor = sblock.fs_cgrotor; 409 altsblock.fs_fmod = sblock.fs_fmod; 410 altsblock.fs_clean = sblock.fs_clean; 411 altsblock.fs_ronly = sblock.fs_ronly; 412 altsblock.fs_flags = sblock.fs_flags; 413 altsblock.fs_maxcontig = sblock.fs_maxcontig; 414 altsblock.fs_minfree = sblock.fs_minfree; 415 altsblock.fs_optim = sblock.fs_optim; 416 altsblock.fs_rotdelay = sblock.fs_rotdelay; 417 altsblock.fs_maxbpg = sblock.fs_maxbpg; 418 memmove(altsblock.fs_ocsp, sblock.fs_ocsp, sizeof sblock.fs_ocsp); 419 altsblock.fs_csp = sblock.fs_csp; 420 altsblock.fs_maxcluster = sblock.fs_maxcluster; 421 altsblock.fs_contigdirs = sblock.fs_contigdirs; 422 altsblock.fs_avgfilesize = sblock.fs_avgfilesize; 423 altsblock.fs_avgfpdir = sblock.fs_avgfpdir; 424 memmove(altsblock.fs_fsmnt, sblock.fs_fsmnt, sizeof sblock.fs_fsmnt); 425 memmove(altsblock.fs_snapinum, sblock.fs_snapinum, 426 sizeof sblock.fs_snapinum); 427 memmove(altsblock.fs_sparecon, 428 sblock.fs_sparecon, sizeof sblock.fs_sparecon); 429 /* 430 * The following should not have to be copied. 431 */ 432 altsblock.fs_fsbtodb = sblock.fs_fsbtodb; 433 altsblock.fs_interleave = sblock.fs_interleave; 434 altsblock.fs_npsect = sblock.fs_npsect; 435 altsblock.fs_nrpos = sblock.fs_nrpos; 436 altsblock.fs_state = sblock.fs_state; 437 altsblock.fs_qbmask = sblock.fs_qbmask; 438 altsblock.fs_qfmask = sblock.fs_qfmask; 439 altsblock.fs_state = sblock.fs_state; 440 altsblock.fs_maxfilesize = sblock.fs_maxfilesize; 441 if (memcmp(&sblock, &altsblock, (int)sblock.fs_sbsize)) { 442 if (debug) { 443 long *nlp, *olp, *endlp; 444 445 printf("superblock mismatches\n"); 446 nlp = (long *)&altsblock; 447 olp = (long *)&sblock; 448 endlp = olp + (sblock.fs_sbsize / sizeof *olp); 449 for ( ; olp < endlp; olp++, nlp++) { 450 if (*olp == *nlp) 451 continue; 452 printf( 453 "offset %d, original %ld, alternate %ld\n", 454 olp - (long *)&sblock, *olp, *nlp); 455 } 456 } 457 badsb(listerr, 458 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE"); 459 return (0); 460 } 461 havesb = 1; 462 return (1); 463} 464 465static void 466badsb(listerr, s) 467 int listerr; 468 char *s; 469{ 470 471 if (!listerr) 472 return; 473 if (preen) 474 printf("%s: ", cdevname); 475 pfatal("BAD SUPER BLOCK: %s\n", s); 476} 477 478sblock_init() 479{ 480 struct disklabel *lp; 481 482 fswritefd = -1; 483 fsmodified = 0; 484 lfdir = 0; 485 initbarea(&sblk); 486 initbarea(&asblk); 487 sblk.b_un.b_buf = malloc(SBSIZE); 488 asblk.b_un.b_buf = malloc(SBSIZE); 489 if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL) 490 errx(EEXIT, "cannot allocate space for superblock"); 491 if ((lp = getdisklabel(NULL, fsreadfd))) 492 dev_bsize = secsize = lp->d_secsize; 493 else 494 dev_bsize = secsize = DEV_BSIZE; 495} 496 497/* 498 * Calculate a prototype superblock based on information in the disk label. 499 * When done the cgsblock macro can be calculated and the fs_ncg field 500 * can be used. Do NOT attempt to use other macros without verifying that 501 * their needed information is available! 502 */ 503static int 504calcsb(dev, devfd, fs) 505 char *dev; 506 int devfd; 507 register struct fs *fs; 508{ 509 register struct disklabel *lp; 510 register struct partition *pp; 511 register char *cp; 512 int i; 513 514 cp = strchr(dev, '\0') - 1; 515 if (cp == (char *)-1 || ((*cp < 'a' || *cp > 'h') && !isdigit(*cp))) { 516 pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev); 517 return (0); 518 } 519 lp = getdisklabel(dev, devfd); 520 if (isdigit(*cp)) 521 pp = &lp->d_partitions[0]; 522 else 523 pp = &lp->d_partitions[*cp - 'a']; 524 if (pp->p_fstype != FS_BSDFFS) { 525 pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n", 526 dev, pp->p_fstype < FSMAXTYPES ? 527 fstypenames[pp->p_fstype] : "unknown"); 528 return (0); 529 } 530 if (pp->p_fsize == 0 || pp->p_frag == 0 || 531 pp->p_cpg == 0 || pp->p_size == 0) { 532 pfatal("%s: %s: type %s fsize %d, frag %d, cpg %d, size %d\n", 533 dev, "INCOMPLETE LABEL", fstypenames[pp->p_fstype], 534 pp->p_fsize, pp->p_frag, pp->p_cpg, pp->p_size); 535 return (0); 536 } 537 memset(fs, 0, sizeof(struct fs)); 538 fs->fs_fsize = pp->p_fsize; 539 fs->fs_frag = pp->p_frag; 540 fs->fs_cpg = pp->p_cpg; 541 fs->fs_size = pp->p_size; 542 fs->fs_ntrak = lp->d_ntracks; 543 fs->fs_nsect = lp->d_nsectors; 544 fs->fs_spc = lp->d_secpercyl; 545 fs->fs_nspf = fs->fs_fsize / lp->d_secsize; 546 fs->fs_sblkno = roundup( 547 howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize), 548 fs->fs_frag); 549 fs->fs_cgmask = 0xffffffff; 550 for (i = fs->fs_ntrak; i > 1; i >>= 1) 551 fs->fs_cgmask <<= 1; 552 if (!POWEROF2(fs->fs_ntrak)) 553 fs->fs_cgmask <<= 1; 554 fs->fs_cgoffset = roundup( 555 howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag); 556 fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs); 557 fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg); 558 for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1) 559 fs->fs_fsbtodb++; 560 dev_bsize = lp->d_secsize; 561 return (1); 562} 563 564static struct disklabel * 565getdisklabel(s, fd) 566 char *s; 567 int fd; 568{ 569 static struct disklabel lab; 570 571 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) { 572 if (s == NULL) 573 return ((struct disklabel *)NULL); 574 pwarn("ioctl (GCINFO): %s\n", strerror(errno)); 575 errx(EEXIT, "%s: can't read disk label", s); 576 } 577 return (&lab); 578} 579