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