1 /* $NetBSD: mkfs.c,v 1.22 2011/10/09 22:30:13 christos Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-3-Clause 5 * 6 * Copyright (c) 2002 Networks Associates Technology, Inc. 7 * All rights reserved. 8 * 9 * This software was developed for the FreeBSD Project by Marshall 10 * Kirk McKusick and Network Associates Laboratories, the Security 11 * Research Division of Network Associates, Inc. under DARPA/SPAWAR 12 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS 13 * research program 14 * 15 * Copyright (c) 1980, 1989, 1993 16 * The Regents of the University of California. All rights reserved. 17 * 18 * Redistribution and use in source and binary forms, with or without 19 * modification, are permitted provided that the following conditions 20 * are met: 21 * 1. Redistributions of source code must retain the above copyright 22 * notice, this list of conditions and the following disclaimer. 23 * 2. Redistributions in binary form must reproduce the above copyright 24 * notice, this list of conditions and the following disclaimer in the 25 * documentation and/or other materials provided with the distribution. 26 * 3. Neither the name of the University nor the names of its contributors 27 * may be used to endorse or promote products derived from this software 28 * without specific prior written permission. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 40 * SUCH DAMAGE. 41 * 42 * $FreeBSD: head/usr.sbin/makefs/ffs/mkfs.c 326025 2017-11-20 19:49:47Z pfg $ 43 */ 44 45 #include <sys/param.h> 46 #include <sys/time.h> 47 #include <sys/resource.h> 48 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <unistd.h> 53 #include <errno.h> 54 #include <util.h> 55 56 #include "makefs.h" 57 #include "ffs.h" 58 59 #include <vfs/ufs/dinode.h> 60 #include <vfs/ufs/fs.h> 61 62 #include "ffs/ufs_bswap.h" 63 #include "ffs/ufs_inode.h" 64 #include "ffs/ffs_extern.h" 65 #include "ffs/newfs_extern.h" 66 67 #ifndef BBSIZE 68 #define BBSIZE 8192 /* size of boot area, with label */ 69 #endif 70 71 static void initcg(uint32_t, time_t, const fsinfo_t *); 72 static int ilog2(int); 73 74 static int count_digits(int); 75 76 /* 77 * make file system for cylinder-group style file systems 78 */ 79 #define UMASK 0755 80 81 static union { 82 struct fs fs; 83 char pad[SBLOCKSIZE]; 84 } fsun; 85 #define sblock fsun.fs 86 87 static union { 88 struct cg cg; 89 char pad[FFS_MAXBSIZE]; 90 } cgun; 91 #define acg cgun.cg 92 93 static char *iobuf; 94 static int iobufsize; 95 96 static char writebuf[FFS_MAXBSIZE]; 97 98 static int Oflag; /* format as an 4.3BSD file system */ 99 static int64_t fssize; /* file system size */ 100 static int sectorsize; /* bytes/sector */ 101 static int fsize; /* fragment size */ 102 static int bsize; /* block size */ 103 #ifndef __DragonFly__ 104 static int maxbsize; /* maximum clustering */ 105 #endif 106 static int maxblkspercg; 107 static int minfree; /* free space threshold */ 108 static int opt; /* optimization preference (space or time) */ 109 static int density; /* number of bytes per inode */ 110 static int maxcontig; /* max contiguous blocks to allocate */ 111 static int maxbpg; /* maximum blocks per file in a cyl group */ 112 static int bbsize; /* boot block size */ 113 static int sbsize; /* superblock size */ 114 static int avgfilesize; /* expected average file size */ 115 static int avgfpdir; /* expected number of files per directory */ 116 117 struct fs * 118 ffs_mkfs(const char *fsys, const fsinfo_t *fsopts, time_t tstamp) 119 { 120 int fragsperinode, optimalfpg, origdensity, minfpg, lastminfpg; 121 int32_t csfrags; 122 uint32_t i, cylno; 123 long long sizepb; 124 void *space; 125 int size; 126 int nprintcols, printcolwidth; 127 ffs_opt_t *ffs_opts = fsopts->fs_specific; 128 129 Oflag = ffs_opts->version; 130 fssize = fsopts->size / fsopts->sectorsize; 131 sectorsize = fsopts->sectorsize; 132 fsize = ffs_opts->fsize; 133 bsize = ffs_opts->bsize; 134 #ifndef __DragonFly__ 135 maxbsize = ffs_opts->maxbsize; 136 #endif 137 maxblkspercg = ffs_opts->maxblkspercg; 138 minfree = ffs_opts->minfree; 139 opt = ffs_opts->optimization; 140 density = ffs_opts->density; 141 maxcontig = ffs_opts->maxcontig; 142 maxbpg = ffs_opts->maxbpg; 143 avgfilesize = ffs_opts->avgfilesize; 144 avgfpdir = ffs_opts->avgfpdir; 145 bbsize = BBSIZE; 146 sbsize = SBLOCKSIZE; 147 148 strlcpy(sblock.fs_volname, ffs_opts->label, sizeof(sblock.fs_volname)); 149 150 #ifndef __DragonFly__ /* XXX dead code */ 151 if (Oflag == 0) { 152 sblock.fs_old_inodefmt = FS_42INODEFMT; 153 sblock.fs_maxsymlinklen = 0; 154 sblock.fs_old_flags = 0; 155 } else 156 #endif 157 { 158 sblock.fs_old_inodefmt = FS_44INODEFMT; 159 #ifndef __DragonFly__ /* XXX UFS2 */ 160 sblock.fs_maxsymlinklen = (Oflag == 1 ? UFS1_MAXSYMLINKLEN : 161 UFS2_MAXSYMLINKLEN); 162 #else 163 sblock.fs_maxsymlinklen = UFS1_MAXSYMLINKLEN; 164 #endif 165 #ifndef __DragonFly__ 166 sblock.fs_old_flags = FS_FLAGS_UPDATED; 167 #endif 168 sblock.fs_flags = 0; 169 } 170 /* 171 * Validate the given file system size. 172 * Verify that its last block can actually be accessed. 173 * Convert to file system fragment sized units. 174 */ 175 if (fssize <= 0) { 176 printf("preposterous size %lld\n", (long long)fssize); 177 exit(13); 178 } 179 ffs_wtfs(fssize - 1, sectorsize, (char *)&sblock, fsopts); 180 181 /* 182 * collect and verify the filesystem density info 183 */ 184 sblock.fs_avgfilesize = avgfilesize; 185 sblock.fs_avgfpdir = avgfpdir; 186 if (sblock.fs_avgfilesize <= 0) 187 printf("illegal expected average file size %d\n", 188 sblock.fs_avgfilesize), exit(14); 189 if (sblock.fs_avgfpdir <= 0) 190 printf("illegal expected number of files per directory %d\n", 191 sblock.fs_avgfpdir), exit(15); 192 /* 193 * collect and verify the block and fragment sizes 194 */ 195 sblock.fs_bsize = bsize; 196 sblock.fs_fsize = fsize; 197 if (!powerof2(sblock.fs_bsize)) { 198 printf("block size must be a power of 2, not %d\n", 199 sblock.fs_bsize); 200 exit(16); 201 } 202 if (!powerof2(sblock.fs_fsize)) { 203 printf("fragment size must be a power of 2, not %d\n", 204 sblock.fs_fsize); 205 exit(17); 206 } 207 if (sblock.fs_fsize < sectorsize) { 208 printf("fragment size %d is too small, minimum is %d\n", 209 sblock.fs_fsize, sectorsize); 210 exit(18); 211 } 212 if (sblock.fs_bsize < MINBSIZE) { 213 printf("block size %d is too small, minimum is %d\n", 214 sblock.fs_bsize, MINBSIZE); 215 exit(19); 216 } 217 if (sblock.fs_bsize > FFS_MAXBSIZE) { 218 printf("block size %d is too large, maximum is %d\n", 219 sblock.fs_bsize, FFS_MAXBSIZE); 220 exit(19); 221 } 222 if (sblock.fs_bsize < sblock.fs_fsize) { 223 printf("block size (%d) cannot be smaller than fragment size (%d)\n", 224 sblock.fs_bsize, sblock.fs_fsize); 225 exit(20); 226 } 227 228 #ifndef __DragonFly__ 229 if (maxbsize < bsize || !powerof2(maxbsize)) { 230 sblock.fs_maxbsize = sblock.fs_bsize; 231 printf("Extent size set to %d\n", sblock.fs_maxbsize); 232 } else if (sblock.fs_maxbsize > FS_MAXCONTIG * sblock.fs_bsize) { 233 sblock.fs_maxbsize = FS_MAXCONTIG * sblock.fs_bsize; 234 printf("Extent size reduced to %d\n", sblock.fs_maxbsize); 235 } else { 236 sblock.fs_maxbsize = maxbsize; 237 } 238 #endif 239 sblock.fs_maxcontig = maxcontig; 240 #ifndef __DragonFly__ 241 if (sblock.fs_maxcontig < sblock.fs_maxbsize / sblock.fs_bsize) { 242 sblock.fs_maxcontig = sblock.fs_maxbsize / sblock.fs_bsize; 243 printf("Maxcontig raised to %d\n", sblock.fs_maxbsize); 244 } 245 #endif 246 247 if (sblock.fs_maxcontig > 1) 248 sblock.fs_contigsumsize = MIN(sblock.fs_maxcontig,FS_MAXCONTIG); 249 250 sblock.fs_bmask = ~(sblock.fs_bsize - 1); 251 sblock.fs_fmask = ~(sblock.fs_fsize - 1); 252 sblock.fs_qbmask = ~sblock.fs_bmask; 253 sblock.fs_qfmask = ~sblock.fs_fmask; 254 for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1) 255 sblock.fs_bshift++; 256 for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1) 257 sblock.fs_fshift++; 258 sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize); 259 for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1) 260 sblock.fs_fragshift++; 261 if (sblock.fs_frag > MAXFRAG) { 262 printf("fragment size %d is too small, " 263 "minimum with block size %d is %d\n", 264 sblock.fs_fsize, sblock.fs_bsize, 265 sblock.fs_bsize / MAXFRAG); 266 exit(21); 267 } 268 sblock.fs_fsbtodb = ilog2(sblock.fs_fsize / sectorsize); 269 #ifndef __DragonFly__ 270 sblock.fs_size = sblock.fs_providersize = fssize = 271 dbtofsb(&sblock, fssize); 272 #else 273 sblock.fs_size = fssize = dbtofsb(&sblock, fssize); 274 #endif 275 276 if (Oflag <= 1) { 277 sblock.fs_magic = FS_UFS1_MAGIC; 278 #ifndef __DragonFly__ 279 sblock.fs_sblockloc = SBLOCK_UFS1; 280 #endif 281 sblock.fs_nindir = sblock.fs_bsize / sizeof(ufs1_daddr_t); 282 sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs1_dinode); 283 sblock.fs_maxsymlinklen = ((UFS_NDADDR + UFS_NIADDR) * 284 sizeof (ufs1_daddr_t)); 285 sblock.fs_old_inodefmt = FS_44INODEFMT; 286 sblock.fs_old_cgoffset = 0; 287 sblock.fs_old_cgmask = 0xffffffff; 288 #ifndef __DragonFly__ 289 sblock.fs_old_size = sblock.fs_size; 290 #endif 291 sblock.fs_old_rotdelay = 0; 292 sblock.fs_old_rps = 60; 293 sblock.fs_old_nspf = sblock.fs_fsize / sectorsize; 294 sblock.fs_old_cpg = 1; 295 sblock.fs_old_interleave = 1; 296 sblock.fs_old_trackskew = 0; 297 sblock.fs_old_cpc = 0; 298 sblock.fs_old_postblformat = 1; 299 sblock.fs_old_nrpos = 1; 300 #ifdef __DragonFly__ /* softupdates support */ 301 if (ffs_opts->softupdates == 1) 302 sblock.fs_flags |= FS_DOSOFTDEP; 303 #else /* XXX UFS2 */ 304 } else { 305 sblock.fs_magic = FS_UFS2_MAGIC; 306 sblock.fs_sblockloc = SBLOCK_UFS2; 307 sblock.fs_nindir = sblock.fs_bsize / sizeof(ufs2_daddr_t); 308 sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs2_dinode); 309 sblock.fs_maxsymlinklen = ((UFS_NDADDR + UFS_NIADDR) * 310 sizeof (ufs2_daddr_t)); 311 if (ffs_opts->softupdates == 1) 312 sblock.fs_flags |= FS_DOSOFTDEP; 313 #endif 314 } 315 316 sblock.fs_sblkno = 317 #ifndef __DragonFly__ 318 roundup(howmany(sblock.fs_sblockloc + SBLOCKSIZE, sblock.fs_fsize), 319 #else 320 roundup(howmany(8192 + SBLOCKSIZE, sblock.fs_fsize), 321 #endif 322 sblock.fs_frag); 323 sblock.fs_cblkno = (makefs_daddr_t)(sblock.fs_sblkno + 324 roundup(howmany(SBLOCKSIZE, sblock.fs_fsize), sblock.fs_frag)); 325 sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag; 326 sblock.fs_maxfilesize = sblock.fs_bsize * UFS_NDADDR - 1; 327 for (sizepb = sblock.fs_bsize, i = 0; i < UFS_NIADDR; i++) { 328 sizepb *= NINDIR(&sblock); 329 sblock.fs_maxfilesize += sizepb; 330 } 331 332 /* 333 * Calculate the number of blocks to put into each cylinder group. 334 * 335 * This algorithm selects the number of blocks per cylinder 336 * group. The first goal is to have at least enough data blocks 337 * in each cylinder group to meet the density requirement. Once 338 * this goal is achieved we try to expand to have at least 339 * 1 cylinder group. Once this goal is achieved, we pack as 340 * many blocks into each cylinder group map as will fit. 341 * 342 * We start by calculating the smallest number of blocks that we 343 * can put into each cylinder group. If this is too big, we reduce 344 * the density until it fits. 345 */ 346 origdensity = density; 347 for (;;) { 348 fragsperinode = MAX(numfrags(&sblock, density), 1); 349 minfpg = fragsperinode * INOPB(&sblock); 350 if (minfpg > sblock.fs_size) 351 minfpg = sblock.fs_size; 352 sblock.fs_ipg = INOPB(&sblock); 353 sblock.fs_fpg = roundup(sblock.fs_iblkno + 354 sblock.fs_ipg / INOPF(&sblock), sblock.fs_frag); 355 if (sblock.fs_fpg < minfpg) 356 sblock.fs_fpg = minfpg; 357 sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode), 358 INOPB(&sblock)); 359 sblock.fs_fpg = roundup(sblock.fs_iblkno + 360 sblock.fs_ipg / INOPF(&sblock), sblock.fs_frag); 361 if (sblock.fs_fpg < minfpg) 362 sblock.fs_fpg = minfpg; 363 sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode), 364 INOPB(&sblock)); 365 #ifndef __DragonFly__ 366 if (CGSIZE(&sblock) < (unsigned long)sblock.fs_bsize) 367 break; 368 #else 369 if (FBSD_CGSIZE(&sblock) < (unsigned long)sblock.fs_bsize) 370 break; 371 #endif 372 density -= sblock.fs_fsize; 373 } 374 if (density != origdensity) 375 printf("density reduced from %d to %d\n", origdensity, density); 376 377 if (maxblkspercg <= 0 || maxblkspercg >= fssize) 378 maxblkspercg = fssize - 1; 379 /* 380 * Start packing more blocks into the cylinder group until 381 * it cannot grow any larger, the number of cylinder groups 382 * drops below 1, or we reach the size requested. 383 */ 384 for ( ; sblock.fs_fpg < maxblkspercg; sblock.fs_fpg += sblock.fs_frag) { 385 sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode), 386 INOPB(&sblock)); 387 if (sblock.fs_size / sblock.fs_fpg < 1) 388 break; 389 #ifndef __DragonFly__ 390 if (CGSIZE(&sblock) < (unsigned long)sblock.fs_bsize) 391 continue; 392 if (CGSIZE(&sblock) == (unsigned long)sblock.fs_bsize) 393 break; 394 #else 395 if (FBSD_CGSIZE(&sblock) < (unsigned long)sblock.fs_bsize) 396 continue; 397 if (FBSD_CGSIZE(&sblock) == (unsigned long)sblock.fs_bsize) 398 break; 399 #endif 400 sblock.fs_fpg -= sblock.fs_frag; 401 sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode), 402 INOPB(&sblock)); 403 break; 404 } 405 /* 406 * Check to be sure that the last cylinder group has enough blocks 407 * to be viable. If it is too small, reduce the number of blocks 408 * per cylinder group which will have the effect of moving more 409 * blocks into the last cylinder group. 410 */ 411 optimalfpg = sblock.fs_fpg; 412 for (;;) { 413 sblock.fs_ncg = howmany(sblock.fs_size, sblock.fs_fpg); 414 lastminfpg = roundup(sblock.fs_iblkno + 415 sblock.fs_ipg / INOPF(&sblock), sblock.fs_frag); 416 if (sblock.fs_size < lastminfpg) { 417 printf("Filesystem size %lld < minimum size of %d\n", 418 (long long)sblock.fs_size, lastminfpg); 419 exit(28); 420 } 421 if (sblock.fs_size % sblock.fs_fpg >= lastminfpg || 422 sblock.fs_size % sblock.fs_fpg == 0) 423 break; 424 sblock.fs_fpg -= sblock.fs_frag; 425 sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode), 426 INOPB(&sblock)); 427 } 428 if (optimalfpg != sblock.fs_fpg) 429 printf("Reduced frags per cylinder group from %d to %d %s\n", 430 optimalfpg, sblock.fs_fpg, "to enlarge last cyl group"); 431 #ifndef __DragonFly__ 432 sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock)); 433 #else 434 sblock.fs_cgsize = fragroundup(&sblock, FBSD_CGSIZE(&sblock)); 435 #endif 436 sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock); 437 if (Oflag <= 1) { 438 sblock.fs_old_spc = sblock.fs_fpg * sblock.fs_old_nspf; 439 sblock.fs_old_nsect = sblock.fs_old_spc; 440 sblock.fs_old_npsect = sblock.fs_old_spc; 441 sblock.fs_old_ncyl = sblock.fs_ncg; 442 } 443 444 /* 445 * fill in remaining fields of the super block 446 */ 447 sblock.fs_csaddr = cgdmin(&sblock, 0); 448 sblock.fs_cssize = 449 fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum)); 450 451 /* 452 * Setup memory for temporary in-core cylgroup summaries. 453 * Cribbed from ffs_mountfs(). 454 */ 455 size = sblock.fs_cssize; 456 if (sblock.fs_contigsumsize > 0) 457 size += sblock.fs_ncg * sizeof(int32_t); 458 space = ecalloc(1, size); 459 sblock.fs_csp = space; 460 space = (char *)space + sblock.fs_cssize; 461 if (sblock.fs_contigsumsize > 0) { 462 int32_t *lp; 463 464 sblock.fs_maxcluster = lp = space; 465 for (i = 0; i < sblock.fs_ncg; i++) 466 *lp++ = sblock.fs_contigsumsize; 467 } 468 469 sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs)); 470 if (sblock.fs_sbsize > SBLOCKSIZE) 471 sblock.fs_sbsize = SBLOCKSIZE; 472 sblock.fs_minfree = minfree; 473 sblock.fs_maxcontig = maxcontig; 474 sblock.fs_maxbpg = maxbpg; 475 sblock.fs_optim = opt; 476 sblock.fs_cgrotor = 0; 477 sblock.fs_pendingblocks = 0; 478 sblock.fs_pendinginodes = 0; 479 sblock.fs_cstotal.cs_ndir = 0; 480 sblock.fs_cstotal.cs_nbfree = 0; 481 sblock.fs_cstotal.cs_nifree = 0; 482 sblock.fs_cstotal.cs_nffree = 0; 483 sblock.fs_fmod = 0; 484 sblock.fs_ronly = 0; 485 sblock.fs_state = 0; 486 sblock.fs_clean = FS_ISCLEAN; 487 sblock.fs_ronly = 0; 488 sblock.fs_id[0] = tstamp; 489 sblock.fs_id[1] = random(); 490 sblock.fs_fsmnt[0] = '\0'; 491 csfrags = howmany(sblock.fs_cssize, sblock.fs_fsize); 492 sblock.fs_dsize = sblock.fs_size - sblock.fs_sblkno - 493 sblock.fs_ncg * (sblock.fs_dblkno - sblock.fs_sblkno); 494 sblock.fs_cstotal.cs_nbfree = 495 fragstoblks(&sblock, sblock.fs_dsize) - 496 howmany(csfrags, sblock.fs_frag); 497 sblock.fs_cstotal.cs_nffree = 498 fragnum(&sblock, sblock.fs_size) + 499 (fragnum(&sblock, csfrags) > 0 ? 500 sblock.fs_frag - fragnum(&sblock, csfrags) : 0); 501 sblock.fs_cstotal.cs_nifree = 502 sblock.fs_ncg * sblock.fs_ipg - UFS_ROOTINO; 503 sblock.fs_cstotal.cs_ndir = 0; 504 sblock.fs_dsize -= csfrags; 505 sblock.fs_time = tstamp; 506 #ifndef __DragonFly__ 507 if (Oflag <= 1) { 508 sblock.fs_old_time = tstamp; 509 sblock.fs_old_dsize = sblock.fs_dsize; 510 sblock.fs_old_csaddr = sblock.fs_csaddr; 511 sblock.fs_old_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir; 512 sblock.fs_old_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree; 513 sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree; 514 sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree; 515 } 516 #endif 517 /* 518 * Dump out summary information about file system. 519 */ 520 #define B2MBFACTOR (1 / (1024.0 * 1024.0)) 521 printf("%s: %.1fMB (%lld sectors) block size %d, " 522 "fragment size %d\n", 523 fsys, (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR, 524 (long long)fsbtodb(&sblock, sblock.fs_size), 525 sblock.fs_bsize, sblock.fs_fsize); 526 printf("\tusing %d cylinder groups of %.2fMB, %d blks, " 527 "%d inodes.\n", 528 sblock.fs_ncg, 529 (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR, 530 sblock.fs_fpg / sblock.fs_frag, sblock.fs_ipg); 531 #undef B2MBFACTOR 532 /* 533 * Now determine how wide each column will be, and calculate how 534 * many columns will fit in a 76 char line. 76 is the width of the 535 * subwindows in sysinst. 536 */ 537 printcolwidth = count_digits( 538 fsbtodb(&sblock, cgsblock(&sblock, sblock.fs_ncg -1))); 539 nprintcols = 76 / (printcolwidth + 2); 540 541 /* 542 * allocate space for superblock, cylinder group map, and 543 * two sets of inode blocks. 544 */ 545 if (sblock.fs_bsize < SBLOCKSIZE) 546 iobufsize = SBLOCKSIZE + 3 * sblock.fs_bsize; 547 else 548 iobufsize = 4 * sblock.fs_bsize; 549 iobuf = ecalloc(1, iobufsize); 550 /* 551 * Make a copy of the superblock into the buffer that we will be 552 * writing out in each cylinder group. 553 */ 554 memcpy(writebuf, &sblock, sbsize); 555 if (fsopts->needswap) 556 ffs_sb_swap(&sblock, (struct fs*)writebuf); 557 memcpy(iobuf, writebuf, SBLOCKSIZE); 558 559 printf("super-block backups (for fsck -b #) at:"); 560 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) { 561 initcg(cylno, tstamp, fsopts); 562 if (cylno % nprintcols == 0) 563 printf("\n"); 564 printf(" %*lld,", printcolwidth, 565 (long long)fsbtodb(&sblock, cgsblock(&sblock, cylno))); 566 fflush(stdout); 567 } 568 printf("\n"); 569 570 /* 571 * Now construct the initial file system, 572 * then write out the super-block. 573 */ 574 sblock.fs_time = tstamp; 575 #ifndef __DragonFly__ 576 if (Oflag <= 1) { 577 sblock.fs_old_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir; 578 sblock.fs_old_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree; 579 sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree; 580 sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree; 581 } 582 #endif 583 if (fsopts->needswap) 584 sblock.fs_flags |= FS_SWAPPED; 585 ffs_write_superblock(&sblock, fsopts); 586 return (&sblock); 587 } 588 589 /* 590 * Write out the superblock and its duplicates, 591 * and the cylinder group summaries 592 */ 593 void 594 ffs_write_superblock(struct fs *fs, const fsinfo_t *fsopts) 595 { 596 int size, blks, i, saveflag; 597 uint32_t cylno; 598 void *space; 599 char *wrbuf; 600 601 saveflag = fs->fs_flags & FS_INTERNAL; 602 fs->fs_flags &= ~FS_INTERNAL; 603 604 memcpy(writebuf, &sblock, sbsize); 605 if (fsopts->needswap) 606 ffs_sb_swap(fs, (struct fs*)writebuf); 607 #ifndef __DragonFly__ 608 ffs_wtfs(fs->fs_sblockloc / sectorsize, sbsize, writebuf, fsopts); 609 #else 610 ffs_wtfs(8192 / sectorsize, sbsize, writebuf, fsopts); 611 #endif 612 613 /* Write out the duplicate super blocks */ 614 for (cylno = 0; cylno < fs->fs_ncg; cylno++) 615 ffs_wtfs(fsbtodb(fs, cgsblock(fs, cylno)), 616 sbsize, writebuf, fsopts); 617 618 /* Write out the cylinder group summaries */ 619 size = fs->fs_cssize; 620 blks = howmany(size, fs->fs_fsize); 621 space = (void *)fs->fs_csp; 622 wrbuf = emalloc(size); 623 for (i = 0; i < blks; i+= fs->fs_frag) { 624 size = fs->fs_bsize; 625 if (i + fs->fs_frag > blks) 626 size = (blks - i) * fs->fs_fsize; 627 if (fsopts->needswap) 628 ffs_csum_swap((struct csum *)space, 629 (struct csum *)wrbuf, size); 630 else 631 memcpy(wrbuf, space, (u_int)size); 632 ffs_wtfs(fsbtodb(fs, fs->fs_csaddr + i), size, wrbuf, fsopts); 633 space = (char *)space + size; 634 } 635 free(wrbuf); 636 fs->fs_flags |= saveflag; 637 } 638 639 /* 640 * Initialize a cylinder group. 641 */ 642 static void 643 initcg(uint32_t cylno, time_t utime, const fsinfo_t *fsopts) 644 { 645 makefs_daddr_t cbase, dmax; 646 int32_t blkno; 647 uint32_t i, j, d, dlower, dupper; 648 struct ufs1_dinode *dp1; 649 #ifndef __DragonFly__ /* XXX UFS2 */ 650 struct ufs2_dinode *dp2; 651 #endif 652 int start; 653 654 /* 655 * Determine block bounds for cylinder group. 656 * Allow space for super block summary information in first 657 * cylinder group. 658 */ 659 cbase = cgbase(&sblock, cylno); 660 dmax = cbase + sblock.fs_fpg; 661 if (dmax > sblock.fs_size) 662 dmax = sblock.fs_size; 663 dlower = cgsblock(&sblock, cylno) - cbase; 664 dupper = cgdmin(&sblock, cylno) - cbase; 665 if (cylno == 0) 666 dupper += howmany(sblock.fs_cssize, sblock.fs_fsize); 667 memset(&acg, 0, sblock.fs_cgsize); 668 acg.cg_time = utime; 669 acg.cg_magic = CG_MAGIC; 670 acg.cg_cgx = cylno; 671 acg.cg_niblk = sblock.fs_ipg; 672 #ifndef __DragonFly__ /* XXX UFS2 */ 673 acg.cg_initediblk = MIN(sblock.fs_ipg, 2 * INOPB(&sblock)); 674 #endif 675 acg.cg_ndblk = dmax - cbase; 676 if (sblock.fs_contigsumsize > 0) 677 acg.cg_nclusterblks = acg.cg_ndblk >> sblock.fs_fragshift; 678 start = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield); 679 #ifndef __DragonFly__ /* XXX UFS2 */ 680 if (Oflag == 2) { 681 acg.cg_iusedoff = start; 682 } else 683 #endif 684 { 685 if (cylno == sblock.fs_ncg - 1) 686 #ifndef __DragonFly__ 687 acg.cg_old_ncyl = howmany(acg.cg_ndblk, 688 sblock.fs_fpg / sblock.fs_old_cpg); 689 #else 690 acg.cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg; 691 #endif 692 else 693 acg.cg_old_ncyl = sblock.fs_old_cpg; 694 #ifndef __DragonFly__ 695 acg.cg_old_time = acg.cg_time; 696 acg.cg_time = 0; 697 acg.cg_old_niblk = acg.cg_niblk; 698 acg.cg_niblk = 0; 699 acg.cg_initediblk = 0; 700 #endif 701 acg.cg_old_btotoff = start; 702 acg.cg_old_boff = acg.cg_old_btotoff + 703 sblock.fs_old_cpg * sizeof(int32_t); 704 acg.cg_iusedoff = acg.cg_old_boff + 705 sblock.fs_old_cpg * sizeof(u_int16_t); 706 } 707 acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT); 708 if (sblock.fs_contigsumsize <= 0) { 709 acg.cg_nextfreeoff = acg.cg_freeoff + 710 howmany(sblock.fs_fpg, CHAR_BIT); 711 } else { 712 acg.cg_clustersumoff = acg.cg_freeoff + 713 howmany(sblock.fs_fpg, CHAR_BIT) - sizeof(int32_t); 714 acg.cg_clustersumoff = 715 roundup(acg.cg_clustersumoff, sizeof(int32_t)); 716 acg.cg_clusteroff = acg.cg_clustersumoff + 717 (sblock.fs_contigsumsize + 1) * sizeof(int32_t); 718 acg.cg_nextfreeoff = acg.cg_clusteroff + 719 howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT); 720 } 721 if (acg.cg_nextfreeoff > (uint32_t)sblock.fs_cgsize) { 722 printf("Panic: cylinder group too big\n"); 723 exit(37); 724 } 725 acg.cg_cs.cs_nifree += sblock.fs_ipg; 726 if (cylno == 0) 727 for (i = 0; i < UFS_ROOTINO; i++) { 728 setbit(cg_inosused_swap(&acg, 0), i); 729 acg.cg_cs.cs_nifree--; 730 } 731 if (cylno > 0) { 732 /* 733 * In cylno 0, beginning space is reserved 734 * for boot and super blocks. 735 */ 736 for (d = 0, blkno = 0; d < dlower;) { 737 ffs_setblock(&sblock, cg_blksfree_swap(&acg, 0), blkno); 738 if (sblock.fs_contigsumsize > 0) 739 setbit(cg_clustersfree_swap(&acg, 0), blkno); 740 acg.cg_cs.cs_nbfree++; 741 #ifdef __DragonFly__ /* XXX swildner: our fsck checks these */ 742 cg_blktot(&acg)[cbtocylno(&sblock, d)]++; 743 cg_blks(&sblock, &acg, cbtocylno(&sblock, d)) 744 [cbtorpos(&sblock, d)]++; 745 #endif 746 d += sblock.fs_frag; 747 blkno++; 748 } 749 } 750 if ((i = (dupper & (sblock.fs_frag - 1))) != 0) { 751 acg.cg_frsum[sblock.fs_frag - i]++; 752 for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) { 753 setbit(cg_blksfree_swap(&acg, 0), dupper); 754 acg.cg_cs.cs_nffree++; 755 } 756 } 757 for (d = dupper, blkno = dupper >> sblock.fs_fragshift; 758 d + sblock.fs_frag <= acg.cg_ndblk; ) { 759 ffs_setblock(&sblock, cg_blksfree_swap(&acg, 0), blkno); 760 if (sblock.fs_contigsumsize > 0) 761 setbit(cg_clustersfree_swap(&acg, 0), blkno); 762 acg.cg_cs.cs_nbfree++; 763 #ifdef __DragonFly__ /* XXX swildner: our fsck checks these */ 764 cg_blktot(&acg)[cbtocylno(&sblock, d)]++; 765 cg_blks(&sblock, &acg, cbtocylno(&sblock, d)) 766 [cbtorpos(&sblock, d)]++; 767 #endif 768 d += sblock.fs_frag; 769 blkno++; 770 } 771 if (d < acg.cg_ndblk) { 772 acg.cg_frsum[acg.cg_ndblk - d]++; 773 for (; d < acg.cg_ndblk; d++) { 774 setbit(cg_blksfree_swap(&acg, 0), d); 775 acg.cg_cs.cs_nffree++; 776 } 777 } 778 if (sblock.fs_contigsumsize > 0) { 779 int32_t *sump = cg_clustersum_swap(&acg, 0); 780 u_char *mapp = cg_clustersfree_swap(&acg, 0); 781 int map = *mapp++; 782 int bit = 1; 783 int run = 0; 784 785 for (i = 0; i < acg.cg_nclusterblks; i++) { 786 if ((map & bit) != 0) { 787 run++; 788 } else if (run != 0) { 789 if (run > sblock.fs_contigsumsize) 790 run = sblock.fs_contigsumsize; 791 sump[run]++; 792 run = 0; 793 } 794 if ((i & (CHAR_BIT - 1)) != (CHAR_BIT - 1)) { 795 bit <<= 1; 796 } else { 797 map = *mapp++; 798 bit = 1; 799 } 800 } 801 if (run != 0) { 802 if (run > sblock.fs_contigsumsize) 803 run = sblock.fs_contigsumsize; 804 sump[run]++; 805 } 806 } 807 sblock.fs_cs(&sblock, cylno) = acg.cg_cs; 808 /* 809 * Write out the duplicate super block, the cylinder group map 810 * and two blocks worth of inodes in a single write. 811 */ 812 start = MAX(sblock.fs_bsize, SBLOCKSIZE); 813 memcpy(&iobuf[start], &acg, sblock.fs_cgsize); 814 if (fsopts->needswap) 815 ffs_cg_swap(&acg, (struct cg*)&iobuf[start], &sblock); 816 start += sblock.fs_bsize; 817 dp1 = (struct ufs1_dinode *)(&iobuf[start]); 818 #ifndef __DragonFly__ /* XXX UFS2 */ 819 dp2 = (struct ufs2_dinode *)(&iobuf[start]); 820 for (i = 0; i < acg.cg_initediblk; i++) { 821 if (sblock.fs_magic == FS_UFS1_MAGIC) { 822 /* No need to swap, it'll stay random */ 823 dp1->di_gen = random(); 824 dp1++; 825 } else { 826 dp2->di_gen = random(); 827 dp2++; 828 } 829 } 830 #endif 831 ffs_wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)), iobufsize, iobuf, 832 fsopts); 833 /* 834 * For the old file system, we have to initialize all the inodes. 835 */ 836 if (Oflag <= 1) { 837 for (i = 2 * sblock.fs_frag; 838 i < sblock.fs_ipg / INOPF(&sblock); 839 i += sblock.fs_frag) { 840 dp1 = (struct ufs1_dinode *)(&iobuf[start]); 841 for (j = 0; j < INOPB(&sblock); j++) { 842 dp1->di_gen = random(); 843 dp1++; 844 } 845 ffs_wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i), 846 sblock.fs_bsize, &iobuf[start], fsopts); 847 } 848 } 849 } 850 851 /* 852 * read a block from the file system 853 */ 854 void 855 ffs_rdfs(makefs_daddr_t bno, int size, void *bf, const fsinfo_t *fsopts) 856 { 857 int n; 858 off_t offset; 859 860 offset = bno * fsopts->sectorsize + fsopts->offset; 861 if (lseek(fsopts->fd, offset, SEEK_SET) < 0) 862 err(1, "%s: seek error for sector %lld", __func__, 863 (long long)bno); 864 n = read(fsopts->fd, bf, size); 865 if (n == -1) { 866 abort(); 867 err(1, "%s: read error bno %lld size %d", __func__, 868 (long long)bno, size); 869 } 870 else if (n != size) 871 errx(1, "%s: read error for sector %lld", __func__, 872 (long long)bno); 873 } 874 875 /* 876 * write a block to the file system 877 */ 878 void 879 ffs_wtfs(makefs_daddr_t bno, int size, void *bf, const fsinfo_t *fsopts) 880 { 881 int n; 882 off_t offset; 883 884 offset = bno * fsopts->sectorsize + fsopts->offset; 885 if (lseek(fsopts->fd, offset, SEEK_SET) < 0) 886 err(1, "%s: seek error for sector %lld", __func__, 887 (long long)bno); 888 n = write(fsopts->fd, bf, size); 889 if (n == -1) 890 err(1, "%s: write error for sector %lld", __func__, 891 (long long)bno); 892 else if (n != size) 893 errx(1, "%s: write error for sector %lld", __func__, 894 (long long)bno); 895 } 896 897 898 /* Determine how many digits are needed to print a given integer */ 899 static int 900 count_digits(int num) 901 { 902 int ndig; 903 904 for(ndig = 1; num > 9; num /=10, ndig++); 905 906 return (ndig); 907 } 908 909 static int 910 ilog2(int val) 911 { 912 u_int n; 913 914 for (n = 0; n < sizeof(n) * CHAR_BIT; n++) 915 if (1 << n == val) 916 return (n); 917 errx(1, "%s: %d is not a power of 2", __func__, val); 918 } 919