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 43 #include <sys/cdefs.h> 44 #include <sys/param.h> 45 #include <sys/time.h> 46 #include <sys/resource.h> 47 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <unistd.h> 52 #include <errno.h> 53 #include <util.h> 54 55 #include "makefs.h" 56 #include "ffs.h" 57 58 #include <ufs/ufs/dinode.h> 59 #include <ufs/ffs/fs.h> 60 61 #include "ffs/ufs_bswap.h" 62 #include "ffs/ufs_inode.h" 63 #include "ffs/ffs_extern.h" 64 #include "ffs/newfs_extern.h" 65 66 #ifndef BBSIZE 67 #define BBSIZE 8192 /* size of boot area, with label */ 68 #endif 69 70 static void initcg(uint32_t, time_t, const fsinfo_t *); 71 static int ilog2(int); 72 73 static int count_digits(int); 74 75 /* 76 * make file system for cylinder-group style file systems 77 */ 78 #define UMASK 0755 79 #define POWEROF2(num) (((num) & ((num) - 1)) == 0) 80 81 /* 82 * The definition of "struct cg" used to contain an extra field at the end 83 * to represent the variable-length data that followed the fixed structure. 84 * This had the effect of artificially limiting the number of blocks that 85 * newfs would put in a CG, since newfs thought that the fixed-size header 86 * was bigger than it really was. When we started validating that the CG 87 * header data actually fit into one fs block, the placeholder field caused 88 * a problem because it caused struct cg to be a different size depending on 89 * platform. The placeholder field was later removed, but this caused a 90 * backward compatibility problem with older binaries that still thought 91 * struct cg was larger, and a new file system could fail validation if 92 * viewed by the older binaries. To avoid this compatibility problem, we 93 * now artificially reduce the amount of space that the variable-length data 94 * can use such that new file systems will pass validation by older binaries. 95 */ 96 #define CGSIZEFUDGE 8 97 98 static union { 99 struct fs fs; 100 char pad[SBLOCKSIZE]; 101 } fsun; 102 #define sblock fsun.fs 103 104 static union { 105 struct cg cg; 106 char pad[FFS_MAXBSIZE]; 107 } cgun; 108 #define acg cgun.cg 109 110 static char *iobuf; 111 static int iobufsize; 112 113 static char writebuf[FFS_MAXBSIZE]; 114 115 static int Oflag; /* format as an 4.3BSD file system */ 116 static int64_t fssize; /* file system size */ 117 static int sectorsize; /* bytes/sector */ 118 static int fsize; /* fragment size */ 119 static int bsize; /* block size */ 120 static int maxbsize; /* maximum clustering */ 121 static int maxblkspercg; 122 static int minfree; /* free space threshold */ 123 static int opt; /* optimization preference (space or time) */ 124 static int density; /* number of bytes per inode */ 125 static int maxcontig; /* max contiguous blocks to allocate */ 126 static int maxbpg; /* maximum blocks per file in a cyl group */ 127 static int bbsize; /* boot block size */ 128 static int sbsize; /* superblock size */ 129 static int avgfilesize; /* expected average file size */ 130 static int avgfpdir; /* expected number of files per directory */ 131 132 struct fs * 133 ffs_mkfs(const char *fsys, const fsinfo_t *fsopts, time_t tstamp) 134 { 135 int fragsperinode, optimalfpg, origdensity, mindensity; 136 int minfpg, lastminfpg; 137 int32_t csfrags; 138 uint32_t i, cylno; 139 long long sizepb; 140 ino_t maxinum; 141 int minfragsperinode; /* minimum ratio of frags to inodes */ 142 void *space; 143 int size; 144 int nprintcols, printcolwidth; 145 ffs_opt_t *ffs_opts = fsopts->fs_specific; 146 147 Oflag = ffs_opts->version; 148 fssize = fsopts->size / fsopts->sectorsize; 149 sectorsize = fsopts->sectorsize; 150 fsize = ffs_opts->fsize; 151 bsize = ffs_opts->bsize; 152 maxbsize = ffs_opts->maxbsize; 153 maxblkspercg = ffs_opts->maxblkspercg; 154 minfree = ffs_opts->minfree; 155 opt = ffs_opts->optimization; 156 density = ffs_opts->density; 157 maxcontig = ffs_opts->maxcontig; 158 maxbpg = ffs_opts->maxbpg; 159 avgfilesize = ffs_opts->avgfilesize; 160 avgfpdir = ffs_opts->avgfpdir; 161 bbsize = BBSIZE; 162 sbsize = SBLOCKSIZE; 163 164 strlcpy((char *)sblock.fs_volname, ffs_opts->label, 165 sizeof(sblock.fs_volname)); 166 167 if (Oflag == 0) { 168 sblock.fs_old_inodefmt = FS_42INODEFMT; 169 sblock.fs_maxsymlinklen = 0; 170 sblock.fs_old_flags = 0; 171 } else { 172 sblock.fs_old_inodefmt = FS_44INODEFMT; 173 sblock.fs_maxsymlinklen = (Oflag == 1 ? UFS1_MAXSYMLINKLEN : 174 UFS2_MAXSYMLINKLEN); 175 sblock.fs_old_flags = FS_FLAGS_UPDATED; 176 sblock.fs_flags = 0; 177 } 178 /* 179 * Validate the given file system size. 180 * Verify that its last block can actually be accessed. 181 * Convert to file system fragment sized units. 182 */ 183 if (fssize <= 0) { 184 printf("preposterous size %lld\n", (long long)fssize); 185 exit(13); 186 } 187 ffs_wtfs(fssize - 1, sectorsize, (char *)&sblock, fsopts); 188 189 /* 190 * collect and verify the filesystem density info 191 */ 192 sblock.fs_avgfilesize = avgfilesize; 193 sblock.fs_avgfpdir = avgfpdir; 194 if (sblock.fs_avgfilesize <= 0) 195 printf("illegal expected average file size %d\n", 196 sblock.fs_avgfilesize), exit(14); 197 if (sblock.fs_avgfpdir <= 0) 198 printf("illegal expected number of files per directory %d\n", 199 sblock.fs_avgfpdir), exit(15); 200 /* 201 * collect and verify the block and fragment sizes 202 */ 203 sblock.fs_bsize = bsize; 204 sblock.fs_fsize = fsize; 205 if (!POWEROF2(sblock.fs_bsize)) { 206 printf("block size must be a power of 2, not %d\n", 207 sblock.fs_bsize); 208 exit(16); 209 } 210 if (!POWEROF2(sblock.fs_fsize)) { 211 printf("fragment size must be a power of 2, not %d\n", 212 sblock.fs_fsize); 213 exit(17); 214 } 215 if (sblock.fs_fsize < sectorsize) { 216 printf("fragment size %d is too small, minimum is %d\n", 217 sblock.fs_fsize, sectorsize); 218 exit(18); 219 } 220 if (sblock.fs_bsize < MINBSIZE) { 221 printf("block size %d is too small, minimum is %d\n", 222 sblock.fs_bsize, MINBSIZE); 223 exit(19); 224 } 225 if (sblock.fs_bsize > FFS_MAXBSIZE) { 226 printf("block size %d is too large, maximum is %d\n", 227 sblock.fs_bsize, FFS_MAXBSIZE); 228 exit(19); 229 } 230 if (sblock.fs_bsize < sblock.fs_fsize) { 231 printf("block size (%d) cannot be smaller than fragment size (%d)\n", 232 sblock.fs_bsize, sblock.fs_fsize); 233 exit(20); 234 } 235 236 if (maxbsize < bsize || !POWEROF2(maxbsize)) { 237 sblock.fs_maxbsize = sblock.fs_bsize; 238 printf("Extent size set to %d\n", sblock.fs_maxbsize); 239 } else if (sblock.fs_maxbsize > FS_MAXCONTIG * sblock.fs_bsize) { 240 sblock.fs_maxbsize = FS_MAXCONTIG * sblock.fs_bsize; 241 printf("Extent size reduced to %d\n", sblock.fs_maxbsize); 242 } else { 243 sblock.fs_maxbsize = maxbsize; 244 } 245 sblock.fs_maxcontig = maxcontig; 246 if (sblock.fs_maxcontig < sblock.fs_maxbsize / sblock.fs_bsize) { 247 sblock.fs_maxcontig = sblock.fs_maxbsize / sblock.fs_bsize; 248 printf("Maxcontig raised to %d\n", sblock.fs_maxbsize); 249 } 250 251 if (sblock.fs_maxcontig > 1) 252 sblock.fs_contigsumsize = MIN(sblock.fs_maxcontig,FS_MAXCONTIG); 253 254 sblock.fs_bmask = ~(sblock.fs_bsize - 1); 255 sblock.fs_fmask = ~(sblock.fs_fsize - 1); 256 sblock.fs_qbmask = ~sblock.fs_bmask; 257 sblock.fs_qfmask = ~sblock.fs_fmask; 258 for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1) 259 sblock.fs_bshift++; 260 for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1) 261 sblock.fs_fshift++; 262 sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize); 263 for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1) 264 sblock.fs_fragshift++; 265 if (sblock.fs_frag > MAXFRAG) { 266 printf("fragment size %d is too small, " 267 "minimum with block size %d is %d\n", 268 sblock.fs_fsize, sblock.fs_bsize, 269 sblock.fs_bsize / MAXFRAG); 270 exit(21); 271 } 272 sblock.fs_fsbtodb = ilog2(sblock.fs_fsize / sectorsize); 273 sblock.fs_size = sblock.fs_providersize = fssize = 274 dbtofsb(&sblock, fssize); 275 276 if (Oflag <= 1) { 277 sblock.fs_magic = FS_UFS1_MAGIC; 278 sblock.fs_sblockloc = SBLOCK_UFS1; 279 sblock.fs_nindir = sblock.fs_bsize / sizeof(ufs1_daddr_t); 280 sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs1_dinode); 281 sblock.fs_maxsymlinklen = ((UFS_NDADDR + UFS_NIADDR) * 282 sizeof (ufs1_daddr_t)); 283 sblock.fs_old_inodefmt = FS_44INODEFMT; 284 sblock.fs_old_cgoffset = 0; 285 sblock.fs_old_cgmask = 0xffffffff; 286 sblock.fs_old_size = sblock.fs_size; 287 sblock.fs_old_rotdelay = 0; 288 sblock.fs_old_rps = 60; 289 sblock.fs_old_nspf = sblock.fs_fsize / sectorsize; 290 sblock.fs_old_cpg = 1; 291 sblock.fs_old_interleave = 1; 292 sblock.fs_old_trackskew = 0; 293 sblock.fs_old_cpc = 0; 294 sblock.fs_old_postblformat = 1; 295 sblock.fs_old_nrpos = 1; 296 } else { 297 sblock.fs_magic = FS_UFS2_MAGIC; 298 sblock.fs_sblockloc = SBLOCK_UFS2; 299 sblock.fs_nindir = sblock.fs_bsize / sizeof(ufs2_daddr_t); 300 sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs2_dinode); 301 sblock.fs_maxsymlinklen = ((UFS_NDADDR + UFS_NIADDR) * 302 sizeof (ufs2_daddr_t)); 303 if (ffs_opts->softupdates == 1) 304 sblock.fs_flags |= FS_DOSOFTDEP; 305 } 306 307 sblock.fs_sblkno = 308 roundup(howmany(sblock.fs_sblockloc + SBLOCKSIZE, sblock.fs_fsize), 309 sblock.fs_frag); 310 sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno + 311 roundup(howmany(SBLOCKSIZE, sblock.fs_fsize), sblock.fs_frag)); 312 sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag; 313 sblock.fs_maxfilesize = sblock.fs_bsize * UFS_NDADDR - 1; 314 for (sizepb = sblock.fs_bsize, i = 0; i < UFS_NIADDR; i++) { 315 sizepb *= NINDIR(&sblock); 316 sblock.fs_maxfilesize += sizepb; 317 } 318 319 /* 320 * Calculate the number of blocks to put into each cylinder group. 321 * 322 * This algorithm selects the number of blocks per cylinder 323 * group. The first goal is to have at least enough data blocks 324 * in each cylinder group to meet the density requirement. Once 325 * this goal is achieved we try to expand to have at least 326 * 1 cylinder group. Once this goal is achieved, we pack as 327 * many blocks into each cylinder group map as will fit. 328 * 329 * We start by calculating the smallest number of blocks that we 330 * can put into each cylinder group. If this is too big, we reduce 331 * the density until it fits. 332 */ 333 maxinum = (((int64_t)(1)) << 32) - INOPB(&sblock); 334 minfragsperinode = 1 + fssize / maxinum; 335 mindensity = minfragsperinode * fsize; 336 if (density == 0) 337 density = MAX(2, minfragsperinode) * fsize; 338 if (density < mindensity) { 339 origdensity = density; 340 density = mindensity; 341 fprintf(stderr, "density increased from %d to %d\n", 342 origdensity, density); 343 } 344 origdensity = density; 345 if (!ffs_opts->min_inodes) 346 density = MIN(density, MAX(2, minfragsperinode) * fsize); 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 if (CGSIZE(&sblock) < (unsigned long)sblock.fs_bsize - 366 CGSIZEFUDGE) 367 break; 368 density -= sblock.fs_fsize; 369 } 370 if (density != origdensity) 371 printf("density reduced from %d to %d\n", origdensity, density); 372 373 if (maxblkspercg <= 0 || maxblkspercg >= fssize) 374 maxblkspercg = fssize - 1; 375 /* 376 * Start packing more blocks into the cylinder group until 377 * it cannot grow any larger, the number of cylinder groups 378 * drops below 1, or we reach the size requested. 379 */ 380 for ( ; sblock.fs_fpg < maxblkspercg; sblock.fs_fpg += sblock.fs_frag) { 381 sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode), 382 INOPB(&sblock)); 383 if (sblock.fs_size / sblock.fs_fpg < 1) 384 break; 385 if (CGSIZE(&sblock) < (unsigned long)sblock.fs_bsize - 386 CGSIZEFUDGE) 387 continue; 388 if (CGSIZE(&sblock) == (unsigned long)sblock.fs_bsize - 389 CGSIZEFUDGE) 390 break; 391 sblock.fs_fpg -= sblock.fs_frag; 392 sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode), 393 INOPB(&sblock)); 394 break; 395 } 396 /* 397 * Check to be sure that the last cylinder group has enough blocks 398 * to be viable. If it is too small, reduce the number of blocks 399 * per cylinder group which will have the effect of moving more 400 * blocks into the last cylinder group. 401 */ 402 optimalfpg = sblock.fs_fpg; 403 for (;;) { 404 sblock.fs_ncg = howmany(sblock.fs_size, sblock.fs_fpg); 405 lastminfpg = roundup(sblock.fs_iblkno + 406 sblock.fs_ipg / INOPF(&sblock), sblock.fs_frag); 407 if (sblock.fs_size < lastminfpg) { 408 printf("Filesystem size %lld < minimum size of %d\n", 409 (long long)sblock.fs_size, lastminfpg); 410 exit(28); 411 } 412 if (sblock.fs_size % sblock.fs_fpg >= lastminfpg || 413 sblock.fs_size % sblock.fs_fpg == 0) 414 break; 415 sblock.fs_fpg -= sblock.fs_frag; 416 sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode), 417 INOPB(&sblock)); 418 } 419 if (optimalfpg != sblock.fs_fpg) 420 printf("Reduced frags per cylinder group from %d to %d %s\n", 421 optimalfpg, sblock.fs_fpg, "to enlarge last cyl group"); 422 sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock)); 423 sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock); 424 if (Oflag <= 1) { 425 sblock.fs_old_spc = sblock.fs_fpg * sblock.fs_old_nspf; 426 sblock.fs_old_nsect = sblock.fs_old_spc; 427 sblock.fs_old_npsect = sblock.fs_old_spc; 428 sblock.fs_old_ncyl = sblock.fs_ncg; 429 } 430 431 /* 432 * fill in remaining fields of the super block 433 */ 434 sblock.fs_csaddr = cgdmin(&sblock, 0); 435 sblock.fs_cssize = 436 fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum)); 437 438 /* 439 * Setup memory for temporary in-core cylgroup summaries. 440 * Cribbed from ffs_mountfs(). 441 */ 442 size = sblock.fs_cssize; 443 if (sblock.fs_contigsumsize > 0) 444 size += sblock.fs_ncg * sizeof(int32_t); 445 space = ecalloc(1, size); 446 sblock.fs_si = ecalloc(1, sizeof(struct fs_summary_info)); 447 sblock.fs_csp = space; 448 space = (char *)space + sblock.fs_cssize; 449 if (sblock.fs_contigsumsize > 0) { 450 int32_t *lp; 451 452 sblock.fs_maxcluster = lp = space; 453 for (i = 0; i < sblock.fs_ncg; i++) 454 *lp++ = sblock.fs_contigsumsize; 455 } 456 457 sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs)); 458 if (sblock.fs_sbsize > SBLOCKSIZE) 459 sblock.fs_sbsize = SBLOCKSIZE; 460 sblock.fs_minfree = minfree; 461 sblock.fs_maxcontig = maxcontig; 462 sblock.fs_maxbpg = maxbpg; 463 sblock.fs_optim = opt; 464 sblock.fs_cgrotor = 0; 465 sblock.fs_pendingblocks = 0; 466 sblock.fs_pendinginodes = 0; 467 sblock.fs_cstotal.cs_ndir = 0; 468 sblock.fs_cstotal.cs_nbfree = 0; 469 sblock.fs_cstotal.cs_nifree = 0; 470 sblock.fs_cstotal.cs_nffree = 0; 471 sblock.fs_fmod = 0; 472 sblock.fs_ronly = 0; 473 sblock.fs_state = 0; 474 sblock.fs_clean = FS_ISCLEAN; 475 sblock.fs_ronly = 0; 476 sblock.fs_id[0] = tstamp; 477 sblock.fs_id[1] = random(); 478 sblock.fs_fsmnt[0] = '\0'; 479 csfrags = howmany(sblock.fs_cssize, sblock.fs_fsize); 480 sblock.fs_dsize = sblock.fs_size - sblock.fs_sblkno - 481 sblock.fs_ncg * (sblock.fs_dblkno - sblock.fs_sblkno); 482 sblock.fs_cstotal.cs_nbfree = 483 fragstoblks(&sblock, sblock.fs_dsize) - 484 howmany(csfrags, sblock.fs_frag); 485 sblock.fs_cstotal.cs_nffree = 486 fragnum(&sblock, sblock.fs_size) + 487 (fragnum(&sblock, csfrags) > 0 ? 488 sblock.fs_frag - fragnum(&sblock, csfrags) : 0); 489 sblock.fs_cstotal.cs_nifree = 490 sblock.fs_ncg * sblock.fs_ipg - UFS_ROOTINO; 491 sblock.fs_cstotal.cs_ndir = 0; 492 sblock.fs_dsize -= csfrags; 493 sblock.fs_time = tstamp; 494 if (Oflag <= 1) { 495 sblock.fs_old_time = tstamp; 496 sblock.fs_old_dsize = sblock.fs_dsize; 497 sblock.fs_old_csaddr = sblock.fs_csaddr; 498 sblock.fs_old_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir; 499 sblock.fs_old_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree; 500 sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree; 501 sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree; 502 } 503 /* 504 * Dump out summary information about file system. 505 */ 506 #define B2MBFACTOR (1 / (1024.0 * 1024.0)) 507 printf("%s: %.1fMB (%lld sectors) block size %d, " 508 "fragment size %d\n", 509 fsys, (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR, 510 (long long)fsbtodb(&sblock, sblock.fs_size), 511 sblock.fs_bsize, sblock.fs_fsize); 512 printf("\tusing %d cylinder groups of %.2fMB, %d blks, " 513 "%d inodes.\n", 514 sblock.fs_ncg, 515 (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR, 516 sblock.fs_fpg / sblock.fs_frag, sblock.fs_ipg); 517 #undef B2MBFACTOR 518 /* 519 * Now determine how wide each column will be, and calculate how 520 * many columns will fit in a 76 char line. 76 is the width of the 521 * subwindows in sysinst. 522 */ 523 printcolwidth = count_digits( 524 fsbtodb(&sblock, cgsblock(&sblock, sblock.fs_ncg -1))); 525 nprintcols = 76 / (printcolwidth + 2); 526 527 /* 528 * allocate space for superblock, cylinder group map, and 529 * two sets of inode blocks. 530 */ 531 if (sblock.fs_bsize < SBLOCKSIZE) 532 iobufsize = SBLOCKSIZE + 3 * sblock.fs_bsize; 533 else 534 iobufsize = 4 * sblock.fs_bsize; 535 iobuf = ecalloc(1, iobufsize); 536 /* 537 * Make a copy of the superblock into the buffer that we will be 538 * writing out in each cylinder group. 539 */ 540 memcpy(writebuf, &sblock, sbsize); 541 if (fsopts->needswap) 542 ffs_sb_swap(&sblock, (struct fs*)writebuf); 543 memcpy(iobuf, writebuf, SBLOCKSIZE); 544 545 printf("super-block backups (for fsck -b #) at:"); 546 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) { 547 initcg(cylno, tstamp, fsopts); 548 if (cylno % nprintcols == 0) 549 printf("\n"); 550 printf(" %*lld%s", printcolwidth, 551 (long long)fsbtodb(&sblock, cgsblock(&sblock, cylno)), 552 cylno == sblock.fs_ncg - 1 ? "" : ","); 553 fflush(stdout); 554 } 555 printf("\n"); 556 557 /* 558 * Now construct the initial file system, 559 * then write out the super-block. 560 */ 561 sblock.fs_time = tstamp; 562 if (Oflag <= 1) { 563 sblock.fs_old_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir; 564 sblock.fs_old_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree; 565 sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree; 566 sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree; 567 } 568 if (fsopts->needswap) 569 sblock.fs_flags |= FS_SWAPPED; 570 ffs_write_superblock(&sblock, fsopts); 571 return (&sblock); 572 } 573 574 /* 575 * Write out the superblock and its duplicates, 576 * and the cylinder group summaries 577 */ 578 void 579 ffs_write_superblock(struct fs *fs, const fsinfo_t *fsopts) 580 { 581 int size, blks, i, saveflag; 582 uint32_t cylno; 583 void *space; 584 char *wrbuf; 585 586 saveflag = fs->fs_flags & FS_INTERNAL; 587 fs->fs_flags &= ~FS_INTERNAL; 588 589 memcpy(writebuf, &sblock, sbsize); 590 if (fsopts->needswap) 591 ffs_sb_swap(fs, (struct fs*)writebuf); 592 ffs_wtfs(fs->fs_sblockloc / sectorsize, sbsize, writebuf, fsopts); 593 594 /* Write out the duplicate super blocks */ 595 for (cylno = 0; cylno < fs->fs_ncg; cylno++) 596 ffs_wtfs(fsbtodb(fs, cgsblock(fs, cylno)), 597 sbsize, writebuf, fsopts); 598 599 /* Write out the cylinder group summaries */ 600 size = fs->fs_cssize; 601 blks = howmany(size, fs->fs_fsize); 602 space = (void *)fs->fs_csp; 603 wrbuf = emalloc(size); 604 for (i = 0; i < blks; i+= fs->fs_frag) { 605 size = fs->fs_bsize; 606 if (i + fs->fs_frag > blks) 607 size = (blks - i) * fs->fs_fsize; 608 if (fsopts->needswap) 609 ffs_csum_swap((struct csum *)space, 610 (struct csum *)wrbuf, size); 611 else 612 memcpy(wrbuf, space, (u_int)size); 613 ffs_wtfs(fsbtodb(fs, fs->fs_csaddr + i), size, wrbuf, fsopts); 614 space = (char *)space + size; 615 } 616 free(wrbuf); 617 fs->fs_flags |= saveflag; 618 } 619 620 /* 621 * Initialize a cylinder group. 622 */ 623 static void 624 initcg(uint32_t cylno, time_t utime, const fsinfo_t *fsopts) 625 { 626 daddr_t cbase, dmax; 627 int32_t blkno; 628 uint32_t i, j, d, dlower, dupper; 629 struct ufs1_dinode *dp1; 630 struct ufs2_dinode *dp2; 631 int start; 632 633 /* 634 * Determine block bounds for cylinder group. 635 * Allow space for super block summary information in first 636 * cylinder group. 637 */ 638 cbase = cgbase(&sblock, cylno); 639 dmax = cbase + sblock.fs_fpg; 640 if (dmax > sblock.fs_size) 641 dmax = sblock.fs_size; 642 dlower = cgsblock(&sblock, cylno) - cbase; 643 dupper = cgdmin(&sblock, cylno) - cbase; 644 if (cylno == 0) 645 dupper += howmany(sblock.fs_cssize, sblock.fs_fsize); 646 memset(&acg, 0, sblock.fs_cgsize); 647 acg.cg_time = utime; 648 acg.cg_magic = CG_MAGIC; 649 acg.cg_cgx = cylno; 650 acg.cg_niblk = sblock.fs_ipg; 651 acg.cg_initediblk = MIN(sblock.fs_ipg, 2 * INOPB(&sblock)); 652 acg.cg_ndblk = dmax - cbase; 653 if (sblock.fs_contigsumsize > 0) 654 acg.cg_nclusterblks = acg.cg_ndblk >> sblock.fs_fragshift; 655 start = sizeof(acg); 656 if (Oflag == 2) { 657 acg.cg_iusedoff = start; 658 } else { 659 if (cylno == sblock.fs_ncg - 1) 660 acg.cg_old_ncyl = howmany(acg.cg_ndblk, 661 sblock.fs_fpg / sblock.fs_old_cpg); 662 else 663 acg.cg_old_ncyl = sblock.fs_old_cpg; 664 acg.cg_old_time = acg.cg_time; 665 acg.cg_time = 0; 666 acg.cg_old_niblk = acg.cg_niblk; 667 acg.cg_niblk = 0; 668 acg.cg_initediblk = 0; 669 acg.cg_old_btotoff = start; 670 acg.cg_old_boff = acg.cg_old_btotoff + 671 sblock.fs_old_cpg * sizeof(int32_t); 672 acg.cg_iusedoff = acg.cg_old_boff + 673 sblock.fs_old_cpg * sizeof(u_int16_t); 674 } 675 acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT); 676 if (sblock.fs_contigsumsize <= 0) { 677 acg.cg_nextfreeoff = acg.cg_freeoff + 678 howmany(sblock.fs_fpg, CHAR_BIT); 679 } else { 680 acg.cg_clustersumoff = acg.cg_freeoff + 681 howmany(sblock.fs_fpg, CHAR_BIT) - sizeof(int32_t); 682 acg.cg_clustersumoff = 683 roundup(acg.cg_clustersumoff, sizeof(int32_t)); 684 acg.cg_clusteroff = acg.cg_clustersumoff + 685 (sblock.fs_contigsumsize + 1) * sizeof(int32_t); 686 acg.cg_nextfreeoff = acg.cg_clusteroff + 687 howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT); 688 } 689 if (acg.cg_nextfreeoff > (uint32_t)sblock.fs_cgsize) { 690 printf("Panic: cylinder group too big\n"); 691 exit(37); 692 } 693 acg.cg_cs.cs_nifree += sblock.fs_ipg; 694 if (cylno == 0) 695 for (i = 0; i < UFS_ROOTINO; i++) { 696 setbit(cg_inosused_swap(&acg, 0), i); 697 acg.cg_cs.cs_nifree--; 698 } 699 if (cylno > 0) { 700 /* 701 * In cylno 0, beginning space is reserved 702 * for boot and super blocks. 703 */ 704 for (d = 0, blkno = 0; d < dlower;) { 705 ffs_setblock(&sblock, cg_blksfree_swap(&acg, 0), blkno); 706 if (sblock.fs_contigsumsize > 0) 707 setbit(cg_clustersfree_swap(&acg, 0), blkno); 708 acg.cg_cs.cs_nbfree++; 709 d += sblock.fs_frag; 710 blkno++; 711 } 712 } 713 if ((i = (dupper & (sblock.fs_frag - 1))) != 0) { 714 acg.cg_frsum[sblock.fs_frag - i]++; 715 for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) { 716 setbit(cg_blksfree_swap(&acg, 0), dupper); 717 acg.cg_cs.cs_nffree++; 718 } 719 } 720 for (d = dupper, blkno = dupper >> sblock.fs_fragshift; 721 d + sblock.fs_frag <= acg.cg_ndblk; ) { 722 ffs_setblock(&sblock, cg_blksfree_swap(&acg, 0), blkno); 723 if (sblock.fs_contigsumsize > 0) 724 setbit(cg_clustersfree_swap(&acg, 0), blkno); 725 acg.cg_cs.cs_nbfree++; 726 d += sblock.fs_frag; 727 blkno++; 728 } 729 if (d < acg.cg_ndblk) { 730 acg.cg_frsum[acg.cg_ndblk - d]++; 731 for (; d < acg.cg_ndblk; d++) { 732 setbit(cg_blksfree_swap(&acg, 0), d); 733 acg.cg_cs.cs_nffree++; 734 } 735 } 736 if (sblock.fs_contigsumsize > 0) { 737 int32_t *sump = cg_clustersum_swap(&acg, 0); 738 u_char *mapp = cg_clustersfree_swap(&acg, 0); 739 int map = *mapp++; 740 int bit = 1; 741 int run = 0; 742 743 for (i = 0; i < acg.cg_nclusterblks; i++) { 744 if ((map & bit) != 0) { 745 run++; 746 } else if (run != 0) { 747 if (run > sblock.fs_contigsumsize) 748 run = sblock.fs_contigsumsize; 749 sump[run]++; 750 run = 0; 751 } 752 if ((i & (CHAR_BIT - 1)) != (CHAR_BIT - 1)) { 753 bit <<= 1; 754 } else { 755 map = *mapp++; 756 bit = 1; 757 } 758 } 759 if (run != 0) { 760 if (run > sblock.fs_contigsumsize) 761 run = sblock.fs_contigsumsize; 762 sump[run]++; 763 } 764 } 765 sblock.fs_cs(&sblock, cylno) = acg.cg_cs; 766 /* 767 * Write out the duplicate super block, the cylinder group map 768 * and two blocks worth of inodes in a single write. 769 */ 770 start = MAX(sblock.fs_bsize, SBLOCKSIZE); 771 memcpy(&iobuf[start], &acg, sblock.fs_cgsize); 772 if (fsopts->needswap) 773 ffs_cg_swap(&acg, (struct cg*)&iobuf[start], &sblock); 774 start += sblock.fs_bsize; 775 dp1 = (struct ufs1_dinode *)(&iobuf[start]); 776 dp2 = (struct ufs2_dinode *)(&iobuf[start]); 777 for (i = 0; i < acg.cg_initediblk; i++) { 778 if (sblock.fs_magic == FS_UFS1_MAGIC) { 779 /* No need to swap, it'll stay random */ 780 dp1->di_gen = random(); 781 dp1++; 782 } else { 783 dp2->di_gen = random(); 784 dp2++; 785 } 786 } 787 ffs_wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)), iobufsize, iobuf, 788 fsopts); 789 /* 790 * For the old file system, we have to initialize all the inodes. 791 */ 792 if (Oflag <= 1) { 793 for (i = 2 * sblock.fs_frag; 794 i < sblock.fs_ipg / INOPF(&sblock); 795 i += sblock.fs_frag) { 796 dp1 = (struct ufs1_dinode *)(&iobuf[start]); 797 for (j = 0; j < INOPB(&sblock); j++) { 798 dp1->di_gen = random(); 799 dp1++; 800 } 801 ffs_wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i), 802 sblock.fs_bsize, &iobuf[start], fsopts); 803 } 804 } 805 } 806 807 /* 808 * read a block from the file system 809 */ 810 void 811 ffs_rdfs(daddr_t bno, int size, void *bf, const fsinfo_t *fsopts) 812 { 813 int n; 814 off_t offset; 815 816 offset = (off_t)bno * fsopts->sectorsize + fsopts->offset; 817 if (lseek(fsopts->fd, offset, SEEK_SET) < 0) 818 err(1, "%s: seek error for sector %lld", __func__, 819 (long long)bno); 820 n = read(fsopts->fd, bf, size); 821 if (n == -1) { 822 abort(); 823 err(1, "%s: read error bno %lld size %d", __func__, 824 (long long)bno, size); 825 } 826 else if (n != size) 827 errx(1, "%s: read error for sector %lld", __func__, 828 (long long)bno); 829 } 830 831 /* 832 * write a block to the file system 833 */ 834 void 835 ffs_wtfs(daddr_t bno, int size, void *bf, const fsinfo_t *fsopts) 836 { 837 int n; 838 off_t offset; 839 840 offset = (off_t)bno * fsopts->sectorsize + fsopts->offset; 841 if (lseek(fsopts->fd, offset, SEEK_SET) < 0) 842 err(1, "%s: seek error for sector %lld", __func__, 843 (long long)bno); 844 n = write(fsopts->fd, bf, size); 845 if (n == -1) 846 err(1, "%s: write error for sector %lld", __func__, 847 (long long)bno); 848 else if (n != size) 849 errx(1, "%s: write error for sector %lld", __func__, 850 (long long)bno); 851 } 852 853 854 /* Determine how many digits are needed to print a given integer */ 855 static int 856 count_digits(int num) 857 { 858 int ndig; 859 860 for(ndig = 1; num > 9; num /=10, ndig++); 861 862 return (ndig); 863 } 864 865 static int 866 ilog2(int val) 867 { 868 u_int n; 869 870 for (n = 0; n < sizeof(n) * CHAR_BIT; n++) 871 if (1 << n == val) 872 return (n); 873 errx(1, "%s: %d is not a power of 2", __func__, val); 874 } 875