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