1 /* $NetBSD: mkfs.c,v 1.42 2023/01/07 19:41:30 chs 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.42 2023/01/07 19:41:30 chs 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 = ((UFS_NDADDR + UFS_NIADDR) * 287 sizeof (int32_t)); 288 sblock.fs_old_inodefmt = FS_44INODEFMT; 289 sblock.fs_old_cgoffset = 0; 290 sblock.fs_old_cgmask = 0xffffffff; 291 sblock.fs_old_size = sblock.fs_size; 292 sblock.fs_old_rotdelay = 0; 293 sblock.fs_old_rps = 60; 294 sblock.fs_old_nspf = sblock.fs_fsize / sectorsize; 295 sblock.fs_old_cpg = 1; 296 sblock.fs_old_interleave = 1; 297 sblock.fs_old_trackskew = 0; 298 sblock.fs_old_cpc = 0; 299 sblock.fs_old_postblformat = 1; 300 sblock.fs_old_nrpos = 1; 301 } else { 302 if (extattr) 303 sblock.fs_magic = FS_UFS2EA_MAGIC; 304 else 305 sblock.fs_magic = FS_UFS2_MAGIC; 306 #if 0 /* XXX makefs is used for small filesystems. */ 307 sblock.fs_sblockloc = SBLOCK_UFS2; 308 #else 309 sblock.fs_sblockloc = SBLOCK_UFS1; 310 #endif 311 sblock.fs_nindir = sblock.fs_bsize / sizeof(int64_t); 312 sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs2_dinode); 313 sblock.fs_maxsymlinklen = ((UFS_NDADDR + UFS_NIADDR) * 314 sizeof (int64_t)); 315 } 316 317 sblock.fs_sblkno = 318 roundup(howmany(sblock.fs_sblockloc + SBLOCKSIZE, sblock.fs_fsize), 319 sblock.fs_frag); 320 sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno + 321 roundup(howmany(SBLOCKSIZE, sblock.fs_fsize), sblock.fs_frag)); 322 sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag; 323 sblock.fs_maxfilesize = sblock.fs_bsize * UFS_NDADDR - 1; 324 for (sizepb = sblock.fs_bsize, i = 0; i < UFS_NIADDR; i++) { 325 sizepb *= FFS_NINDIR(&sblock); 326 sblock.fs_maxfilesize += sizepb; 327 } 328 329 /* 330 * Calculate the number of blocks to put into each cylinder group. 331 * 332 * This algorithm selects the number of blocks per cylinder 333 * group. The first goal is to have at least enough data blocks 334 * in each cylinder group to meet the density requirement. Once 335 * this goal is achieved we try to expand to have at least 336 * 1 cylinder group. Once this goal is achieved, we pack as 337 * many blocks into each cylinder group map as will fit. 338 * 339 * We start by calculating the smallest number of blocks that we 340 * can put into each cylinder group. If this is too big, we reduce 341 * the density until it fits. 342 */ 343 origdensity = density; 344 for (;;) { 345 fragsperinode = MAX(ffs_numfrags(&sblock, density), 1); 346 minfpg = fragsperinode * FFS_INOPB(&sblock); 347 if (minfpg > sblock.fs_size) 348 minfpg = sblock.fs_size; 349 sblock.fs_ipg = FFS_INOPB(&sblock); 350 sblock.fs_fpg = roundup(sblock.fs_iblkno + 351 sblock.fs_ipg / FFS_INOPF(&sblock), sblock.fs_frag); 352 if (sblock.fs_fpg < minfpg) 353 sblock.fs_fpg = minfpg; 354 sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode), 355 FFS_INOPB(&sblock)); 356 sblock.fs_fpg = roundup(sblock.fs_iblkno + 357 sblock.fs_ipg / FFS_INOPF(&sblock), sblock.fs_frag); 358 if (sblock.fs_fpg < minfpg) 359 sblock.fs_fpg = minfpg; 360 sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode), 361 FFS_INOPB(&sblock)); 362 if (CGSIZE(&sblock) < (unsigned long)sblock.fs_bsize) 363 break; 364 density -= sblock.fs_fsize; 365 } 366 if (density != origdensity) 367 printf("density reduced from %d to %d\n", origdensity, density); 368 369 if (maxblkspercg <= 0 || maxblkspercg >= fssize) 370 maxblkspercg = fssize - 1; 371 /* 372 * Start packing more blocks into the cylinder group until 373 * it cannot grow any larger, the number of cylinder groups 374 * drops below 1, or we reach the size requested. 375 */ 376 for ( ; sblock.fs_fpg < maxblkspercg; sblock.fs_fpg += sblock.fs_frag) { 377 sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode), 378 FFS_INOPB(&sblock)); 379 if (sblock.fs_size / sblock.fs_fpg < 1) 380 break; 381 if (CGSIZE(&sblock) < (unsigned long)sblock.fs_bsize) 382 continue; 383 if (CGSIZE(&sblock) == (unsigned long)sblock.fs_bsize) 384 break; 385 sblock.fs_fpg -= sblock.fs_frag; 386 sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode), 387 FFS_INOPB(&sblock)); 388 break; 389 } 390 /* 391 * Check to be sure that the last cylinder group has enough blocks 392 * to be viable. If it is too small, reduce the number of blocks 393 * per cylinder group which will have the effect of moving more 394 * blocks into the last cylinder group. 395 */ 396 optimalfpg = sblock.fs_fpg; 397 for (;;) { 398 sblock.fs_ncg = howmany(sblock.fs_size, sblock.fs_fpg); 399 lastminfpg = roundup(sblock.fs_iblkno + 400 sblock.fs_ipg / FFS_INOPF(&sblock), sblock.fs_frag); 401 if (sblock.fs_size < lastminfpg) { 402 printf("Filesystem size %lld < minimum size of %d\n", 403 (long long)sblock.fs_size, lastminfpg); 404 exit(28); 405 } 406 if (sblock.fs_size % sblock.fs_fpg >= lastminfpg || 407 sblock.fs_size % sblock.fs_fpg == 0) 408 break; 409 sblock.fs_fpg -= sblock.fs_frag; 410 sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode), 411 FFS_INOPB(&sblock)); 412 } 413 if (optimalfpg != sblock.fs_fpg) 414 printf("Reduced frags per cylinder group from %d to %d %s\n", 415 optimalfpg, sblock.fs_fpg, "to enlarge last cyl group"); 416 sblock.fs_cgsize = ffs_fragroundup(&sblock, CGSIZE(&sblock)); 417 sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / FFS_INOPF(&sblock); 418 if (Oflag <= 1) { 419 sblock.fs_old_spc = sblock.fs_fpg * sblock.fs_old_nspf; 420 sblock.fs_old_nsect = sblock.fs_old_spc; 421 sblock.fs_old_npsect = sblock.fs_old_spc; 422 sblock.fs_old_ncyl = sblock.fs_ncg; 423 } 424 425 /* 426 * fill in remaining fields of the super block 427 */ 428 sblock.fs_csaddr = cgdmin(&sblock, 0); 429 sblock.fs_cssize = 430 ffs_fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum)); 431 432 /* 433 * Setup memory for temporary in-core cylgroup summaries. 434 * Cribbed from ffs_mountfs(). 435 */ 436 size = sblock.fs_cssize; 437 if (sblock.fs_contigsumsize > 0) 438 size += sblock.fs_ncg * sizeof(int32_t); 439 space = ecalloc(1, size); 440 sblock.fs_csp = space; 441 space = (char *)space + sblock.fs_cssize; 442 if (sblock.fs_contigsumsize > 0) { 443 int32_t *lp; 444 445 sblock.fs_maxcluster = lp = space; 446 for (i = 0; i < sblock.fs_ncg; i++) 447 *lp++ = sblock.fs_contigsumsize; 448 } 449 450 sblock.fs_sbsize = ffs_fragroundup(&sblock, sizeof(struct fs)); 451 if (sblock.fs_sbsize > SBLOCKSIZE) 452 sblock.fs_sbsize = SBLOCKSIZE; 453 sblock.fs_minfree = minfree; 454 sblock.fs_maxcontig = maxcontig; 455 sblock.fs_maxbpg = maxbpg; 456 sblock.fs_optim = opt; 457 sblock.fs_cgrotor = 0; 458 sblock.fs_pendingblocks = 0; 459 sblock.fs_pendinginodes = 0; 460 sblock.fs_cstotal.cs_ndir = 0; 461 sblock.fs_cstotal.cs_nbfree = 0; 462 sblock.fs_cstotal.cs_nifree = 0; 463 sblock.fs_cstotal.cs_nffree = 0; 464 sblock.fs_fmod = 0; 465 sblock.fs_ronly = 0; 466 sblock.fs_state = 0; 467 sblock.fs_clean = FS_ISCLEAN; 468 sblock.fs_ronly = 0; 469 sblock.fs_id[0] = tstamp; 470 sblock.fs_id[1] = random(); 471 sblock.fs_fsmnt[0] = '\0'; 472 csfrags = howmany(sblock.fs_cssize, sblock.fs_fsize); 473 sblock.fs_dsize = sblock.fs_size - sblock.fs_sblkno - 474 sblock.fs_ncg * (sblock.fs_dblkno - sblock.fs_sblkno); 475 sblock.fs_cstotal.cs_nbfree = 476 ffs_fragstoblks(&sblock, sblock.fs_dsize) - 477 howmany(csfrags, sblock.fs_frag); 478 sblock.fs_cstotal.cs_nffree = 479 ffs_fragnum(&sblock, sblock.fs_size) + 480 (ffs_fragnum(&sblock, csfrags) > 0 ? 481 sblock.fs_frag - ffs_fragnum(&sblock, csfrags) : 0); 482 sblock.fs_cstotal.cs_nifree = sblock.fs_ncg * sblock.fs_ipg - UFS_ROOTINO; 483 sblock.fs_cstotal.cs_ndir = 0; 484 sblock.fs_dsize -= csfrags; 485 sblock.fs_time = tstamp; 486 if (Oflag <= 1) { 487 sblock.fs_old_time = tstamp; 488 sblock.fs_old_dsize = sblock.fs_dsize; 489 sblock.fs_old_csaddr = sblock.fs_csaddr; 490 sblock.fs_old_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir; 491 sblock.fs_old_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree; 492 sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree; 493 sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree; 494 } 495 /* 496 * Dump out summary information about file system. 497 */ 498 #define B2MBFACTOR (1 / (1024.0 * 1024.0)) 499 printf("%s: %.1fMB (%lld sectors) block size %d, " 500 "fragment size %d\n", 501 fsys, (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR, 502 (long long)FFS_FSBTODB(&sblock, sblock.fs_size), 503 sblock.fs_bsize, sblock.fs_fsize); 504 printf("\tusing %d cylinder groups of %.2fMB, %d blks, " 505 "%d inodes.\n", 506 sblock.fs_ncg, 507 (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR, 508 sblock.fs_fpg / sblock.fs_frag, sblock.fs_ipg); 509 #undef B2MBFACTOR 510 /* 511 * Now determine how wide each column will be, and calculate how 512 * many columns will fit in a 76 char line. 76 is the width of the 513 * subwindows in sysinst. 514 */ 515 printcolwidth = count_digits( 516 FFS_FSBTODB(&sblock, cgsblock(&sblock, sblock.fs_ncg -1))); 517 nprintcols = 76 / (printcolwidth + 2); 518 519 /* 520 * allocate space for superblock, cylinder group map, and 521 * two sets of inode blocks. 522 */ 523 if (sblock.fs_bsize < SBLOCKSIZE) 524 iobufsize = SBLOCKSIZE + 3 * sblock.fs_bsize; 525 else 526 iobufsize = 4 * sblock.fs_bsize; 527 iobuf = ecalloc(1, iobufsize); 528 /* 529 * Make a copy of the superblock into the buffer that we will be 530 * writing out in each cylinder group. 531 */ 532 ffs_sb_copy(&wb.fs, &sblock, sbsize, fsopts); 533 memcpy(iobuf, writebuf, SBLOCKSIZE); 534 535 printf("super-block backups (for fsck -b #) at:"); 536 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) { 537 initcg(cylno, tstamp, fsopts); 538 if (cylno % nprintcols == 0) 539 printf("\n"); 540 printf(" %*lld,", printcolwidth, 541 (long long)FFS_FSBTODB(&sblock, cgsblock(&sblock, cylno))); 542 fflush(stdout); 543 } 544 printf("\n"); 545 546 /* 547 * Now construct the initial file system, 548 * then write out the super-block. 549 */ 550 sblock.fs_time = tstamp; 551 if (Oflag <= 1) { 552 sblock.fs_old_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir; 553 sblock.fs_old_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree; 554 sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree; 555 sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree; 556 } 557 if (fsopts->needswap) 558 sblock.fs_flags |= FS_SWAPPED; 559 ffs_write_superblock(&sblock, fsopts); 560 return (&sblock); 561 } 562 563 /* 564 * Write out the superblock and its duplicates, 565 * and the cylinder group summaries 566 */ 567 void 568 ffs_write_superblock(struct fs *fs, const fsinfo_t *fsopts) 569 { 570 int size, blks, i, saveflag; 571 uint32_t cylno; 572 void *space; 573 char *wrbuf; 574 575 saveflag = fs->fs_flags & FS_INTERNAL; 576 fs->fs_flags &= ~FS_INTERNAL; 577 578 ffs_sb_copy(&wb.fs, &sblock, sbsize, fsopts); 579 ffs_wtfs(fs->fs_sblockloc / sectorsize, sbsize, writebuf, fsopts); 580 581 /* Write out the duplicate super blocks */ 582 for (cylno = 0; cylno < fs->fs_ncg; cylno++) 583 ffs_wtfs(FFS_FSBTODB(fs, cgsblock(fs, cylno)), 584 sbsize, writebuf, fsopts); 585 586 /* Write out the cylinder group summaries */ 587 size = fs->fs_cssize; 588 blks = howmany(size, fs->fs_fsize); 589 space = (void *)fs->fs_csp; 590 wrbuf = emalloc(size); 591 for (i = 0; i < blks; i+= fs->fs_frag) { 592 size = fs->fs_bsize; 593 if (i + fs->fs_frag > blks) 594 size = (blks - i) * fs->fs_fsize; 595 if (fsopts->needswap) 596 ffs_csum_swap((struct csum *)space, 597 (struct csum *)wrbuf, size); 598 else 599 memcpy(wrbuf, space, (u_int)size); 600 ffs_wtfs(FFS_FSBTODB(fs, fs->fs_csaddr + i), size, wrbuf, fsopts); 601 space = (char *)space + size; 602 } 603 free(wrbuf); 604 fs->fs_flags |= saveflag; 605 } 606 607 /* 608 * Initialize a cylinder group. 609 */ 610 static void 611 initcg(uint32_t cylno, time_t utime, const fsinfo_t *fsopts) 612 { 613 daddr_t cbase, dmax; 614 uint32_t i, j, d, dlower, dupper, blkno; 615 struct ufs1_dinode *dp1; 616 struct ufs2_dinode *dp2; 617 int start; 618 619 /* 620 * Determine block bounds for cylinder group. 621 * Allow space for super block summary information in first 622 * cylinder group. 623 */ 624 cbase = cgbase(&sblock, cylno); 625 dmax = cbase + sblock.fs_fpg; 626 if (dmax > sblock.fs_size) 627 dmax = sblock.fs_size; 628 dlower = cgsblock(&sblock, cylno) - cbase; 629 dupper = cgdmin(&sblock, cylno) - cbase; 630 if (cylno == 0) 631 dupper += howmany(sblock.fs_cssize, sblock.fs_fsize); 632 memset(&acg, 0, sblock.fs_cgsize); 633 acg.cg_time = utime; 634 acg.cg_magic = CG_MAGIC; 635 acg.cg_cgx = cylno; 636 acg.cg_niblk = sblock.fs_ipg; 637 acg.cg_initediblk = sblock.fs_ipg < 2 * FFS_INOPB(&sblock) ? 638 sblock.fs_ipg : 2 * FFS_INOPB(&sblock); 639 acg.cg_ndblk = dmax - cbase; 640 if (sblock.fs_contigsumsize > 0) 641 acg.cg_nclusterblks = acg.cg_ndblk >> sblock.fs_fragshift; 642 start = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield); 643 if (Oflag == 2) { 644 acg.cg_iusedoff = start; 645 } else { 646 if (cylno == sblock.fs_ncg - 1) 647 acg.cg_old_ncyl = howmany(acg.cg_ndblk, 648 sblock.fs_fpg / sblock.fs_old_cpg); 649 else 650 acg.cg_old_ncyl = sblock.fs_old_cpg; 651 acg.cg_old_time = acg.cg_time; 652 acg.cg_time = 0; 653 acg.cg_old_niblk = acg.cg_niblk; 654 acg.cg_niblk = 0; 655 acg.cg_initediblk = 0; 656 acg.cg_old_btotoff = start; 657 acg.cg_old_boff = acg.cg_old_btotoff + 658 sblock.fs_old_cpg * sizeof(int32_t); 659 acg.cg_iusedoff = acg.cg_old_boff + 660 sblock.fs_old_cpg * sizeof(u_int16_t); 661 } 662 acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT); 663 if (sblock.fs_contigsumsize <= 0) { 664 acg.cg_nextfreeoff = acg.cg_freeoff + 665 howmany(sblock.fs_fpg, CHAR_BIT); 666 } else { 667 acg.cg_clustersumoff = acg.cg_freeoff + 668 howmany(sblock.fs_fpg, CHAR_BIT) - sizeof(int32_t); 669 acg.cg_clustersumoff = 670 roundup(acg.cg_clustersumoff, sizeof(int32_t)); 671 acg.cg_clusteroff = acg.cg_clustersumoff + 672 (sblock.fs_contigsumsize + 1) * sizeof(int32_t); 673 acg.cg_nextfreeoff = acg.cg_clusteroff + 674 howmany(ffs_fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT); 675 } 676 if (acg.cg_nextfreeoff > (unsigned)sblock.fs_cgsize) { 677 printf("Panic: cylinder group too big\n"); 678 exit(37); 679 } 680 acg.cg_cs.cs_nifree += sblock.fs_ipg; 681 if (cylno == 0) { 682 size_t r; 683 684 for (r = 0; r < UFS_ROOTINO; r++) { 685 setbit(cg_inosused(&acg, 0), r); 686 acg.cg_cs.cs_nifree--; 687 } 688 } 689 if (cylno > 0) { 690 /* 691 * In cylno 0, beginning space is reserved 692 * for boot and super blocks. 693 */ 694 for (d = 0, blkno = 0; d < dlower;) { 695 ffs_setblock(&sblock, cg_blksfree(&acg, 0), blkno); 696 if (sblock.fs_contigsumsize > 0) 697 setbit(cg_clustersfree(&acg, 0), blkno); 698 acg.cg_cs.cs_nbfree++; 699 d += sblock.fs_frag; 700 blkno++; 701 } 702 } 703 if ((i = (dupper & (sblock.fs_frag - 1))) != 0) { 704 acg.cg_frsum[sblock.fs_frag - i]++; 705 for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) { 706 setbit(cg_blksfree(&acg, 0), dupper); 707 acg.cg_cs.cs_nffree++; 708 } 709 } 710 for (d = dupper, blkno = dupper >> sblock.fs_fragshift; 711 d + sblock.fs_frag <= acg.cg_ndblk; ) { 712 ffs_setblock(&sblock, cg_blksfree(&acg, 0), blkno); 713 if (sblock.fs_contigsumsize > 0) 714 setbit(cg_clustersfree(&acg, 0), blkno); 715 acg.cg_cs.cs_nbfree++; 716 d += sblock.fs_frag; 717 blkno++; 718 } 719 if (d < acg.cg_ndblk) { 720 acg.cg_frsum[acg.cg_ndblk - d]++; 721 for (; d < acg.cg_ndblk; d++) { 722 setbit(cg_blksfree(&acg, 0), d); 723 acg.cg_cs.cs_nffree++; 724 } 725 } 726 if (sblock.fs_contigsumsize > 0) { 727 int32_t *sump = cg_clustersum(&acg, 0); 728 u_char *mapp = cg_clustersfree(&acg, 0); 729 int map = *mapp++; 730 int bit = 1; 731 int run = 0; 732 733 for (i = 0; i < acg.cg_nclusterblks; i++) { 734 if ((map & bit) != 0) { 735 run++; 736 } else if (run != 0) { 737 if (run > sblock.fs_contigsumsize) 738 run = sblock.fs_contigsumsize; 739 sump[run]++; 740 run = 0; 741 } 742 if ((i & (CHAR_BIT - 1)) != (CHAR_BIT - 1)) { 743 bit <<= 1; 744 } else { 745 map = *mapp++; 746 bit = 1; 747 } 748 } 749 if (run != 0) { 750 if (run > sblock.fs_contigsumsize) 751 run = sblock.fs_contigsumsize; 752 sump[run]++; 753 } 754 } 755 sblock.fs_cs(&sblock, cylno) = acg.cg_cs; 756 /* 757 * Write out the duplicate super block, the cylinder group map 758 * and two blocks worth of inodes in a single write. 759 */ 760 start = sblock.fs_bsize > SBLOCKSIZE ? sblock.fs_bsize : SBLOCKSIZE; 761 memcpy(&iobuf[start], &acg, sblock.fs_cgsize); 762 if (fsopts->needswap) 763 ffs_cg_swap(&acg, (struct cg*)&iobuf[start], &sblock); 764 start += sblock.fs_bsize; 765 dp1 = (struct ufs1_dinode *)(&iobuf[start]); 766 dp2 = (struct ufs2_dinode *)(&iobuf[start]); 767 for (i = 0; i < acg.cg_initediblk; i++) { 768 if (sblock.fs_magic == FS_UFS1_MAGIC) { 769 /* No need to swap, it'll stay random */ 770 dp1->di_gen = random(); 771 dp1++; 772 } else { 773 dp2->di_gen = random(); 774 dp2++; 775 } 776 } 777 ffs_wtfs(FFS_FSBTODB(&sblock, cgsblock(&sblock, cylno)), iobufsize, iobuf, 778 fsopts); 779 /* 780 * For the old file system, we have to initialize all the inodes. 781 */ 782 if (Oflag <= 1) { 783 for (i = 2 * sblock.fs_frag; 784 i < sblock.fs_ipg / FFS_INOPF(&sblock); 785 i += sblock.fs_frag) { 786 dp1 = (struct ufs1_dinode *)(&iobuf[start]); 787 for (j = 0; j < FFS_INOPB(&sblock); j++) { 788 dp1->di_gen = random(); 789 dp1++; 790 } 791 ffs_wtfs(FFS_FSBTODB(&sblock, cgimin(&sblock, cylno) + i), 792 sblock.fs_bsize, &iobuf[start], fsopts); 793 } 794 } 795 } 796 797 /* 798 * read a block from the file system 799 */ 800 void 801 ffs_rdfs(daddr_t bno, int size, void *bf, const fsinfo_t *fsopts) 802 { 803 int n; 804 off_t offset; 805 806 offset = bno * (off_t)fsopts->sectorsize + fsopts->offset; 807 if (lseek(fsopts->fd, offset, SEEK_SET) < 0) 808 err(EXIT_FAILURE, "%s: seek error for sector %lld", __func__, 809 (long long)bno); 810 n = read(fsopts->fd, bf, size); 811 if (n == -1) { 812 err(EXIT_FAILURE, "%s: read error bno %lld size %d", __func__, 813 (long long)bno, size); 814 } 815 else if (n != size) 816 errx(EXIT_FAILURE, "%s: short read error for sector %lld", __func__, 817 (long long)bno); 818 } 819 820 /* 821 * write a block to the file system 822 */ 823 void 824 ffs_wtfs(daddr_t bno, int size, void *bf, const fsinfo_t *fsopts) 825 { 826 int n; 827 off_t offset; 828 829 offset = bno * (off_t)fsopts->sectorsize + fsopts->offset; 830 if (lseek(fsopts->fd, offset, SEEK_SET) == -1) 831 err(EXIT_FAILURE, "%s: seek error @%jd for sector %jd", 832 __func__, (intmax_t)offset, (intmax_t)bno); 833 n = write(fsopts->fd, bf, size); 834 if (n == -1) 835 err(EXIT_FAILURE, "%s: write error for sector %jd", __func__, 836 (intmax_t)bno); 837 else if (n != size) 838 errx(EXIT_FAILURE, "%s: short write error for sector %jd", 839 __func__, (intmax_t)bno); 840 } 841 842 843 /* Determine how many digits are needed to print a given integer */ 844 static int 845 count_digits(int num) 846 { 847 int ndig; 848 849 for(ndig = 1; num > 9; num /=10, ndig++); 850 851 return (ndig); 852 } 853 854 static int 855 ilog2(int val) 856 { 857 u_int n; 858 859 for (n = 0; n < sizeof(n) * CHAR_BIT; n++) 860 if (1 << n == val) 861 return (n); 862 errx(EXIT_FAILURE, "%s: %d is not a power of 2", __func__, val); 863 } 864