1 /* 2 * Copyright (c) 1980, 1989 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 /*static char sccsid[] = "from: @(#)mkfs.c 6.18 (Berkeley) 7/3/91";*/ 36 static char rcsid[] = "$Id: mkfs.c,v 1.6 1993/10/01 01:56:42 mycroft Exp $"; 37 #endif /* not lint */ 38 39 #ifndef STANDALONE 40 #include <stdio.h> 41 #include <a.out.h> 42 #endif 43 44 #include <sys/param.h> 45 #include <sys/time.h> 46 #include <sys/wait.h> 47 #include <sys/resource.h> 48 #include <ufs/dinode.h> 49 #include <ufs/fs.h> 50 #include <ufs/dir.h> 51 #include <sys/disklabel.h> 52 #include <machine/endian.h> 53 54 /* 55 * make file system for cylinder-group style file systems 56 */ 57 58 /* 59 * The size of a cylinder group is calculated by CGSIZE. The maximum size 60 * is limited by the fact that cylinder groups are at most one block. 61 * Its size is derived from the size of the maps maintained in the 62 * cylinder group and the (struct cg) size. 63 */ 64 #define CGSIZE(fs) \ 65 /* base cg */ (sizeof(struct cg) + \ 66 /* blktot size */ (fs)->fs_cpg * sizeof(long) + \ 67 /* blks size */ (fs)->fs_cpg * (fs)->fs_nrpos * sizeof(short) + \ 68 /* inode map */ howmany((fs)->fs_ipg, NBBY) + \ 69 /* block map */ howmany((fs)->fs_cpg * (fs)->fs_spc / NSPF(fs), NBBY)) 70 71 /* 72 * We limit the size of the inode map to be no more than a 73 * third of the cylinder group space, since we must leave at 74 * least an equal amount of space for the block map. 75 * 76 * N.B.: MAXIPG must be a multiple of INOPB(fs). 77 */ 78 #define MAXIPG(fs) roundup((fs)->fs_bsize * NBBY / 3, INOPB(fs)) 79 80 #define UMASK 0755 81 #define MAXINOPB (MAXBSIZE / sizeof(struct dinode)) 82 #define POWEROF2(num) (((num) & ((num) - 1)) == 0) 83 84 /* 85 * variables set up by front end. 86 */ 87 extern int mfs; /* run as the memory based filesystem */ 88 extern int Nflag; /* run mkfs without writing file system */ 89 extern int fssize; /* file system size */ 90 extern int ntracks; /* # tracks/cylinder */ 91 extern int nsectors; /* # sectors/track */ 92 extern int nphyssectors; /* # sectors/track including spares */ 93 extern int secpercyl; /* sectors per cylinder */ 94 extern int sectorsize; /* bytes/sector */ 95 extern int rpm; /* revolutions/minute of drive */ 96 extern int interleave; /* hardware sector interleave */ 97 extern int trackskew; /* sector 0 skew, per track */ 98 extern int headswitch; /* head switch time, usec */ 99 extern int trackseek; /* track-to-track seek, usec */ 100 extern int fsize; /* fragment size */ 101 extern int bsize; /* block size */ 102 extern int cpg; /* cylinders/cylinder group */ 103 extern int cpgflg; /* cylinders/cylinder group flag was given */ 104 extern int minfree; /* free space threshold */ 105 extern int opt; /* optimization preference (space or time) */ 106 extern int density; /* number of bytes per inode */ 107 extern int maxcontig; /* max contiguous blocks to allocate */ 108 extern int rotdelay; /* rotational delay between blocks */ 109 extern int maxbpg; /* maximum blocks per file in a cyl group */ 110 extern int nrpos; /* # of distinguished rotational positions */ 111 extern int bbsize; /* boot block size */ 112 extern int sbsize; /* superblock size */ 113 extern u_long memleft; /* virtual memory available */ 114 extern caddr_t membase; /* start address of memory based filesystem */ 115 extern caddr_t malloc(), calloc(); 116 117 union { 118 struct fs fs; 119 char pad[SBSIZE]; 120 } fsun; 121 #define sblock fsun.fs 122 struct csum *fscs; 123 124 union { 125 struct cg cg; 126 char pad[MAXBSIZE]; 127 } cgun; 128 #define acg cgun.cg 129 130 struct dinode zino[MAXBSIZE / sizeof(struct dinode)]; 131 132 int fsi, fso; 133 daddr_t alloc(); 134 135 mkfs(pp, fsys, fi, fo) 136 struct partition *pp; 137 char *fsys; 138 int fi, fo; 139 { 140 register long i, mincpc, mincpg, inospercg; 141 long cylno, rpos, blk, j, warn = 0; 142 long used, mincpgcnt, bpcg; 143 long mapcramped, inodecramped; 144 long postblsize, rotblsize, totalsbsize; 145 int ppid, status; 146 time_t utime; 147 void started(); 148 149 #ifndef STANDALONE 150 time(&utime); 151 #endif 152 if (mfs) { 153 ppid = getpid(); 154 (void) signal(SIGUSR1, started); 155 if (i = fork()) { 156 if (i == -1) { 157 perror("mount_mfs"); 158 exit(10); 159 } 160 if (waitpid(i, &status, 0) != -1 && WIFEXITED(status)) 161 exit(WEXITSTATUS(status)); 162 exit(11); 163 /* NOTREACHED */ 164 } 165 (void)malloc(0); 166 if (fssize * sectorsize > memleft) 167 fssize = (memleft - 16384) / sectorsize; 168 if ((membase = malloc(fssize * sectorsize)) == 0) 169 exit(12); 170 } 171 fsi = fi; 172 fso = fo; 173 /* 174 * Validate the given file system size. 175 * Verify that its last block can actually be accessed. 176 */ 177 if (fssize <= 0) 178 printf("preposterous size %d\n", fssize), exit(13); 179 wtfs(fssize - 1, sectorsize, (char *)&sblock); 180 /* 181 * collect and verify the sector and track info 182 */ 183 sblock.fs_nsect = nsectors; 184 sblock.fs_ntrak = ntracks; 185 if (sblock.fs_ntrak <= 0) 186 printf("preposterous ntrak %d\n", sblock.fs_ntrak), exit(14); 187 if (sblock.fs_nsect <= 0) 188 printf("preposterous nsect %d\n", sblock.fs_nsect), exit(15); 189 /* 190 * collect and verify the block and fragment sizes 191 */ 192 sblock.fs_bsize = bsize; 193 sblock.fs_fsize = fsize; 194 if (!POWEROF2(sblock.fs_bsize)) { 195 printf("block size must be a power of 2, not %d\n", 196 sblock.fs_bsize); 197 exit(16); 198 } 199 if (!POWEROF2(sblock.fs_fsize)) { 200 printf("fragment size must be a power of 2, not %d\n", 201 sblock.fs_fsize); 202 exit(17); 203 } 204 if (sblock.fs_fsize < sectorsize) { 205 printf("fragment size %d is too small, minimum is %d\n", 206 sblock.fs_fsize, sectorsize); 207 exit(18); 208 } 209 if (sblock.fs_bsize < MINBSIZE) { 210 printf("block size %d is too small, minimum is %d\n", 211 sblock.fs_bsize, MINBSIZE); 212 exit(19); 213 } 214 if (sblock.fs_bsize < sblock.fs_fsize) { 215 printf("block size (%d) cannot be smaller than fragment size (%d)\n", 216 sblock.fs_bsize, sblock.fs_fsize); 217 exit(20); 218 } 219 sblock.fs_bmask = ~(sblock.fs_bsize - 1); 220 sblock.fs_fmask = ~(sblock.fs_fsize - 1); 221 /* 222 * Planning now for future expansion. 223 */ 224 # if (BYTE_ORDER == BIG_ENDIAN) 225 sblock.fs_qbmask.val[0] = 0; 226 sblock.fs_qbmask.val[1] = ~sblock.fs_bmask; 227 sblock.fs_qfmask.val[0] = 0; 228 sblock.fs_qfmask.val[1] = ~sblock.fs_fmask; 229 # endif /* BIG_ENDIAN */ 230 # if (BYTE_ORDER == LITTLE_ENDIAN) 231 sblock.fs_qbmask.val[0] = ~sblock.fs_bmask; 232 sblock.fs_qbmask.val[1] = 0; 233 sblock.fs_qfmask.val[0] = ~sblock.fs_fmask; 234 sblock.fs_qfmask.val[1] = 0; 235 # endif /* LITTLE_ENDIAN */ 236 for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1) 237 sblock.fs_bshift++; 238 for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1) 239 sblock.fs_fshift++; 240 sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize); 241 for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1) 242 sblock.fs_fragshift++; 243 if (sblock.fs_frag > MAXFRAG) { 244 printf("fragment size %d is too small, minimum with block size %d is %d\n", 245 sblock.fs_fsize, sblock.fs_bsize, 246 sblock.fs_bsize / MAXFRAG); 247 exit(21); 248 } 249 sblock.fs_nrpos = nrpos; 250 sblock.fs_nindir = sblock.fs_bsize / sizeof(daddr_t); 251 sblock.fs_inopb = sblock.fs_bsize / sizeof(struct dinode); 252 sblock.fs_nspf = sblock.fs_fsize / sectorsize; 253 for (sblock.fs_fsbtodb = 0, i = NSPF(&sblock); i > 1; i >>= 1) 254 sblock.fs_fsbtodb++; 255 sblock.fs_sblkno = 256 roundup(howmany(bbsize + sbsize, sblock.fs_fsize), sblock.fs_frag); 257 sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno + 258 roundup(howmany(sbsize, sblock.fs_fsize), sblock.fs_frag)); 259 sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag; 260 sblock.fs_cgoffset = roundup( 261 howmany(sblock.fs_nsect, NSPF(&sblock)), sblock.fs_frag); 262 for (sblock.fs_cgmask = 0xffffffff, i = sblock.fs_ntrak; i > 1; i >>= 1) 263 sblock.fs_cgmask <<= 1; 264 if (!POWEROF2(sblock.fs_ntrak)) 265 sblock.fs_cgmask <<= 1; 266 /* 267 * Validate specified/determined secpercyl 268 * and calculate minimum cylinders per group. 269 */ 270 sblock.fs_spc = secpercyl; 271 for (sblock.fs_cpc = NSPB(&sblock), i = sblock.fs_spc; 272 sblock.fs_cpc > 1 && (i & 1) == 0; 273 sblock.fs_cpc >>= 1, i >>= 1) 274 /* void */; 275 mincpc = sblock.fs_cpc; 276 bpcg = sblock.fs_spc * sectorsize; 277 inospercg = roundup(bpcg / sizeof(struct dinode), INOPB(&sblock)); 278 if (inospercg > MAXIPG(&sblock)) 279 inospercg = MAXIPG(&sblock); 280 used = (sblock.fs_iblkno + inospercg / INOPF(&sblock)) * NSPF(&sblock); 281 mincpgcnt = howmany(sblock.fs_cgoffset * (~sblock.fs_cgmask) + used, 282 sblock.fs_spc); 283 mincpg = roundup(mincpgcnt, mincpc); 284 /* 285 * Insure that cylinder group with mincpg has enough space 286 * for block maps 287 */ 288 sblock.fs_cpg = mincpg; 289 sblock.fs_ipg = inospercg; 290 mapcramped = 0; 291 while (CGSIZE(&sblock) > sblock.fs_bsize) { 292 mapcramped = 1; 293 if (sblock.fs_bsize < MAXBSIZE) { 294 sblock.fs_bsize <<= 1; 295 if ((i & 1) == 0) { 296 i >>= 1; 297 } else { 298 sblock.fs_cpc <<= 1; 299 mincpc <<= 1; 300 mincpg = roundup(mincpgcnt, mincpc); 301 sblock.fs_cpg = mincpg; 302 } 303 sblock.fs_frag <<= 1; 304 sblock.fs_fragshift += 1; 305 if (sblock.fs_frag <= MAXFRAG) 306 continue; 307 } 308 if (sblock.fs_fsize == sblock.fs_bsize) { 309 printf("There is no block size that"); 310 printf(" can support this disk\n"); 311 exit(22); 312 } 313 sblock.fs_frag >>= 1; 314 sblock.fs_fragshift -= 1; 315 sblock.fs_fsize <<= 1; 316 sblock.fs_nspf <<= 1; 317 } 318 /* 319 * Insure that cylinder group with mincpg has enough space for inodes 320 */ 321 inodecramped = 0; 322 used *= sectorsize; 323 inospercg = roundup((mincpg * bpcg - used) / density, INOPB(&sblock)); 324 sblock.fs_ipg = inospercg; 325 while (inospercg > MAXIPG(&sblock)) { 326 inodecramped = 1; 327 if (mincpc == 1 || sblock.fs_frag == 1 || 328 sblock.fs_bsize == MINBSIZE) 329 break; 330 printf("With a block size of %d %s %d\n", sblock.fs_bsize, 331 "minimum bytes per inode is", 332 (mincpg * bpcg - used) / MAXIPG(&sblock) + 1); 333 sblock.fs_bsize >>= 1; 334 sblock.fs_frag >>= 1; 335 sblock.fs_fragshift -= 1; 336 mincpc >>= 1; 337 sblock.fs_cpg = roundup(mincpgcnt, mincpc); 338 if (CGSIZE(&sblock) > sblock.fs_bsize) { 339 sblock.fs_bsize <<= 1; 340 break; 341 } 342 mincpg = sblock.fs_cpg; 343 inospercg = 344 roundup((mincpg * bpcg - used) / density, INOPB(&sblock)); 345 sblock.fs_ipg = inospercg; 346 } 347 if (inodecramped) { 348 if (inospercg > MAXIPG(&sblock)) { 349 printf("Minimum bytes per inode is %d\n", 350 (mincpg * bpcg - used) / MAXIPG(&sblock) + 1); 351 } else if (!mapcramped) { 352 printf("With %d bytes per inode, ", density); 353 printf("minimum cylinders per group is %d\n", mincpg); 354 } 355 } 356 if (mapcramped) { 357 printf("With %d sectors per cylinder, ", sblock.fs_spc); 358 printf("minimum cylinders per group is %d\n", mincpg); 359 } 360 if (inodecramped || mapcramped) { 361 if (sblock.fs_bsize != bsize) 362 printf("%s to be changed from %d to %d\n", 363 "This requires the block size", 364 bsize, sblock.fs_bsize); 365 if (sblock.fs_fsize != fsize) 366 printf("\t%s to be changed from %d to %d\n", 367 "and the fragment size", 368 fsize, sblock.fs_fsize); 369 exit(23); 370 } 371 /* 372 * Calculate the number of cylinders per group 373 */ 374 sblock.fs_cpg = cpg; 375 if (sblock.fs_cpg % mincpc != 0) { 376 printf("%s groups must have a multiple of %d cylinders\n", 377 cpgflg ? "Cylinder" : "Warning: cylinder", mincpc); 378 sblock.fs_cpg = roundup(sblock.fs_cpg, mincpc); 379 if (!cpgflg) 380 cpg = sblock.fs_cpg; 381 } 382 /* 383 * Must insure there is enough space for inodes 384 */ 385 sblock.fs_ipg = roundup((sblock.fs_cpg * bpcg - used) / density, 386 INOPB(&sblock)); 387 while (sblock.fs_ipg > MAXIPG(&sblock)) { 388 inodecramped = 1; 389 sblock.fs_cpg -= mincpc; 390 sblock.fs_ipg = roundup((sblock.fs_cpg * bpcg - used) / density, 391 INOPB(&sblock)); 392 } 393 /* 394 * Must insure there is enough space to hold block map 395 */ 396 while (CGSIZE(&sblock) > sblock.fs_bsize) { 397 mapcramped = 1; 398 sblock.fs_cpg -= mincpc; 399 sblock.fs_ipg = roundup((sblock.fs_cpg * bpcg - used) / density, 400 INOPB(&sblock)); 401 } 402 sblock.fs_fpg = (sblock.fs_cpg * sblock.fs_spc) / NSPF(&sblock); 403 if ((sblock.fs_cpg * sblock.fs_spc) % NSPB(&sblock) != 0) { 404 printf("panic (fs_cpg * fs_spc) % NSPF != 0"); 405 exit(24); 406 } 407 if (sblock.fs_cpg < mincpg) { 408 printf("cylinder groups must have at least %d cylinders\n", 409 mincpg); 410 exit(25); 411 } else if (sblock.fs_cpg != cpg) { 412 if (!cpgflg) 413 printf("Warning: "); 414 else if (!mapcramped && !inodecramped) 415 exit(26); 416 if (mapcramped && inodecramped) 417 printf("Block size and bytes per inode restrict"); 418 else if (mapcramped) 419 printf("Block size restricts"); 420 else 421 printf("Bytes per inode restrict"); 422 printf(" cylinders per group to %d.\n", sblock.fs_cpg); 423 if (cpgflg) 424 exit(27); 425 } 426 sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock)); 427 /* 428 * Now have size for file system and nsect and ntrak. 429 * Determine number of cylinders and blocks in the file system. 430 */ 431 sblock.fs_size = fssize = dbtofsb(&sblock, fssize); 432 sblock.fs_ncyl = fssize * NSPF(&sblock) / sblock.fs_spc; 433 if (fssize * NSPF(&sblock) > sblock.fs_ncyl * sblock.fs_spc) { 434 sblock.fs_ncyl++; 435 warn = 1; 436 } 437 if (sblock.fs_ncyl < 1) { 438 printf("file systems must have at least one cylinder\n"); 439 exit(28); 440 } 441 /* 442 * Determine feasability/values of rotational layout tables. 443 * 444 * The size of the rotational layout tables is limited by the 445 * size of the superblock, SBSIZE. The amount of space available 446 * for tables is calculated as (SBSIZE - sizeof (struct fs)). 447 * The size of these tables is inversely proportional to the block 448 * size of the file system. The size increases if sectors per track 449 * are not powers of two, because more cylinders must be described 450 * by the tables before the rotational pattern repeats (fs_cpc). 451 */ 452 sblock.fs_interleave = interleave; 453 sblock.fs_trackskew = trackskew; 454 sblock.fs_npsect = nphyssectors; 455 sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT; 456 sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs)); 457 if (sblock.fs_ntrak == 1) { 458 sblock.fs_cpc = 0; 459 goto next; 460 } 461 postblsize = sblock.fs_nrpos * sblock.fs_cpc * sizeof(short); 462 rotblsize = sblock.fs_cpc * sblock.fs_spc / NSPB(&sblock); 463 totalsbsize = sizeof(struct fs) + rotblsize; 464 if (sblock.fs_nrpos == 8 && sblock.fs_cpc <= 16) { 465 /* use old static table space */ 466 sblock.fs_postbloff = (char *)(&sblock.fs_opostbl[0][0]) - 467 (char *)(&sblock.fs_link); 468 sblock.fs_rotbloff = &sblock.fs_space[0] - 469 (u_char *)(&sblock.fs_link); 470 } else { 471 /* use dynamic table space */ 472 sblock.fs_postbloff = &sblock.fs_space[0] - 473 (u_char *)(&sblock.fs_link); 474 sblock.fs_rotbloff = sblock.fs_postbloff + postblsize; 475 totalsbsize += postblsize; 476 } 477 if (totalsbsize > SBSIZE || 478 sblock.fs_nsect > (1 << NBBY) * NSPB(&sblock)) { 479 printf("%s %s %d %s %d.%s", 480 "Warning: insufficient space in super block for\n", 481 "rotational layout tables with nsect", sblock.fs_nsect, 482 "and ntrak", sblock.fs_ntrak, 483 "\nFile system performance may be impaired.\n"); 484 sblock.fs_cpc = 0; 485 goto next; 486 } 487 sblock.fs_sbsize = fragroundup(&sblock, totalsbsize); 488 /* 489 * calculate the available blocks for each rotational position 490 */ 491 for (cylno = 0; cylno < sblock.fs_cpc; cylno++) 492 for (rpos = 0; rpos < sblock.fs_nrpos; rpos++) 493 fs_postbl(&sblock, cylno)[rpos] = -1; 494 for (i = (rotblsize - 1) * sblock.fs_frag; 495 i >= 0; i -= sblock.fs_frag) { 496 cylno = cbtocylno(&sblock, i); 497 rpos = cbtorpos(&sblock, i); 498 blk = fragstoblks(&sblock, i); 499 if (fs_postbl(&sblock, cylno)[rpos] == -1) 500 fs_rotbl(&sblock)[blk] = 0; 501 else 502 fs_rotbl(&sblock)[blk] = 503 fs_postbl(&sblock, cylno)[rpos] - blk; 504 fs_postbl(&sblock, cylno)[rpos] = blk; 505 } 506 next: 507 /* 508 * Compute/validate number of cylinder groups. 509 */ 510 sblock.fs_ncg = sblock.fs_ncyl / sblock.fs_cpg; 511 if (sblock.fs_ncyl % sblock.fs_cpg) 512 sblock.fs_ncg++; 513 sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock); 514 i = MIN(~sblock.fs_cgmask, sblock.fs_ncg - 1); 515 if (cgdmin(&sblock, i) - cgbase(&sblock, i) >= sblock.fs_fpg) { 516 printf("inode blocks/cyl group (%d) >= data blocks (%d)\n", 517 cgdmin(&sblock, i) - cgbase(&sblock, i) / sblock.fs_frag, 518 sblock.fs_fpg / sblock.fs_frag); 519 printf("number of cylinders per cylinder group (%d) %s.\n", 520 sblock.fs_cpg, "must be increased"); 521 exit(29); 522 } 523 j = sblock.fs_ncg - 1; 524 if ((i = fssize - j * sblock.fs_fpg) < sblock.fs_fpg && 525 cgdmin(&sblock, j) - cgbase(&sblock, j) > i) { 526 if (j == 0) { 527 printf("Filesystem must have at least %d sectors\n", 528 NSPF(&sblock) * 529 (cgdmin(&sblock, 0) + 3 * sblock.fs_frag)); 530 exit(30); 531 } 532 printf("Warning: inode blocks/cyl group (%d) >= data blocks (%d) in last\n", 533 (cgdmin(&sblock, j) - cgbase(&sblock, j)) / sblock.fs_frag, 534 i / sblock.fs_frag); 535 printf(" cylinder group. This implies %d sector(s) cannot be allocated.\n", 536 i * NSPF(&sblock)); 537 sblock.fs_ncg--; 538 sblock.fs_ncyl -= sblock.fs_ncyl % sblock.fs_cpg; 539 sblock.fs_size = fssize = sblock.fs_ncyl * sblock.fs_spc / 540 NSPF(&sblock); 541 warn = 0; 542 } 543 if (warn && !mfs) { 544 printf("Warning: %d sector(s) in last cylinder unallocated\n", 545 sblock.fs_spc - 546 (fssize * NSPF(&sblock) - (sblock.fs_ncyl - 1) 547 * sblock.fs_spc)); 548 } 549 /* 550 * fill in remaining fields of the super block 551 */ 552 sblock.fs_csaddr = cgdmin(&sblock, 0); 553 sblock.fs_cssize = 554 fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum)); 555 i = sblock.fs_bsize / sizeof(struct csum); 556 sblock.fs_csmask = ~(i - 1); 557 for (sblock.fs_csshift = 0; i > 1; i >>= 1) 558 sblock.fs_csshift++; 559 fscs = (struct csum *)calloc(1, sblock.fs_cssize); 560 sblock.fs_magic = FS_MAGIC; 561 sblock.fs_rotdelay = rotdelay; 562 sblock.fs_minfree = minfree; 563 sblock.fs_maxcontig = maxcontig; 564 sblock.fs_headswitch = headswitch; 565 sblock.fs_trkseek = trackseek; 566 sblock.fs_maxbpg = maxbpg; 567 sblock.fs_rps = rpm / 60; 568 sblock.fs_optim = opt; 569 sblock.fs_cgrotor = 0; 570 sblock.fs_cstotal.cs_ndir = 0; 571 sblock.fs_cstotal.cs_nbfree = 0; 572 sblock.fs_cstotal.cs_nifree = 0; 573 sblock.fs_cstotal.cs_nffree = 0; 574 sblock.fs_fmod = 0; 575 sblock.fs_state = FSOKAY; 576 sblock.fs_clean = FS_CLEANFREQ; 577 sblock.fs_ronly = 0; 578 /* 579 * Dump out summary information about file system. 580 */ 581 if (!mfs) { 582 printf("%s:\t%d sectors in %d %s of %d tracks, %d sectors\n", 583 fsys, sblock.fs_size * NSPF(&sblock), sblock.fs_ncyl, 584 "cylinders", sblock.fs_ntrak, sblock.fs_nsect); 585 printf("\t%.1fMB in %d cyl groups (%d c/g, %.2fMB/g, %d i/g)\n", 586 (float)sblock.fs_size * sblock.fs_fsize * 1e-6, 587 sblock.fs_ncg, sblock.fs_cpg, 588 (float)sblock.fs_fpg * sblock.fs_fsize * 1e-6, 589 sblock.fs_ipg); 590 } 591 /* 592 * Now build the cylinders group blocks and 593 * then print out indices of cylinder groups. 594 */ 595 if (!mfs) 596 printf("super-block backups (for fsck -b #) at:"); 597 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) { 598 initcg(cylno, utime); 599 if (mfs) 600 continue; 601 if (cylno % 9 == 0) 602 printf("\n"); 603 printf(" %d,", fsbtodb(&sblock, cgsblock(&sblock, cylno))); 604 } 605 if (!mfs) 606 printf("\n"); 607 if (Nflag && !mfs) 608 exit(0); 609 /* 610 * Now construct the initial file system, 611 * then write out the super-block. 612 */ 613 fsinit(utime); 614 sblock.fs_time = utime; 615 wtfs(SBOFF / sectorsize, sbsize, (char *)&sblock); 616 for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize) 617 wtfs(fsbtodb(&sblock, sblock.fs_csaddr + numfrags(&sblock, i)), 618 sblock.fs_cssize - i < sblock.fs_bsize ? 619 sblock.fs_cssize - i : sblock.fs_bsize, 620 ((char *)fscs) + i); 621 /* 622 * Write out the duplicate super blocks 623 */ 624 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) 625 wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)), 626 sbsize, (char *)&sblock); 627 /* 628 * Update information about this partion in pack 629 * label, to that it may be updated on disk. 630 */ 631 pp->p_fstype = FS_BSDFFS; 632 pp->p_fsize = sblock.fs_fsize; 633 pp->p_frag = sblock.fs_frag; 634 pp->p_cpg = sblock.fs_cpg; 635 /* 636 * Notify parent process of success. 637 * Dissociate from session and tty. 638 */ 639 if (mfs) { 640 kill(ppid, SIGUSR1); 641 (void) setsid(); 642 (void) close(0); 643 (void) close(1); 644 (void) close(2); 645 (void) chdir("/"); 646 } 647 } 648 649 /* 650 * Initialize a cylinder group. 651 */ 652 initcg(cylno, utime) 653 int cylno; 654 time_t utime; 655 { 656 daddr_t cbase, d, dlower, dupper, dmax; 657 long i, j, s; 658 register struct csum *cs; 659 660 /* 661 * Determine block bounds for cylinder group. 662 * Allow space for super block summary information in first 663 * cylinder group. 664 */ 665 cbase = cgbase(&sblock, cylno); 666 dmax = cbase + sblock.fs_fpg; 667 if (dmax > sblock.fs_size) 668 dmax = sblock.fs_size; 669 dlower = cgsblock(&sblock, cylno) - cbase; 670 dupper = cgdmin(&sblock, cylno) - cbase; 671 if (cylno == 0) 672 dupper += howmany(sblock.fs_cssize, sblock.fs_fsize); 673 cs = fscs + cylno; 674 acg.cg_time = utime; 675 acg.cg_magic = CG_MAGIC; 676 acg.cg_cgx = cylno; 677 if (cylno == sblock.fs_ncg - 1) 678 acg.cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg; 679 else 680 acg.cg_ncyl = sblock.fs_cpg; 681 acg.cg_niblk = sblock.fs_ipg; 682 acg.cg_ndblk = dmax - cbase; 683 acg.cg_cs.cs_ndir = 0; 684 acg.cg_cs.cs_nffree = 0; 685 acg.cg_cs.cs_nbfree = 0; 686 acg.cg_cs.cs_nifree = 0; 687 acg.cg_rotor = 0; 688 acg.cg_frotor = 0; 689 acg.cg_irotor = 0; 690 acg.cg_btotoff = &acg.cg_space[0] - (u_char *)(&acg.cg_link); 691 acg.cg_boff = acg.cg_btotoff + sblock.fs_cpg * sizeof(long); 692 acg.cg_iusedoff = acg.cg_boff + 693 sblock.fs_cpg * sblock.fs_nrpos * sizeof(short); 694 acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, NBBY); 695 acg.cg_nextfreeoff = acg.cg_freeoff + 696 howmany(sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY); 697 for (i = 0; i < sblock.fs_frag; i++) { 698 acg.cg_frsum[i] = 0; 699 } 700 bzero((caddr_t)cg_inosused(&acg), acg.cg_freeoff - acg.cg_iusedoff); 701 acg.cg_cs.cs_nifree += sblock.fs_ipg; 702 if (cylno == 0) 703 for (i = 0; i < ROOTINO; i++) { 704 setbit(cg_inosused(&acg), i); 705 acg.cg_cs.cs_nifree--; 706 } 707 for (i = 0; i < sblock.fs_ipg / INOPF(&sblock); i += sblock.fs_frag) 708 wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i), 709 sblock.fs_bsize, (char *)zino); 710 bzero((caddr_t)cg_blktot(&acg), acg.cg_boff - acg.cg_btotoff); 711 bzero((caddr_t)cg_blks(&sblock, &acg, 0), 712 acg.cg_iusedoff - acg.cg_boff); 713 bzero((caddr_t)cg_blksfree(&acg), acg.cg_nextfreeoff - acg.cg_freeoff); 714 if (cylno > 0) { 715 /* 716 * In cylno 0, beginning space is reserved 717 * for boot and super blocks. 718 */ 719 for (d = 0; d < dlower; d += sblock.fs_frag) { 720 setblock(&sblock, cg_blksfree(&acg), d/sblock.fs_frag); 721 acg.cg_cs.cs_nbfree++; 722 cg_blktot(&acg)[cbtocylno(&sblock, d)]++; 723 cg_blks(&sblock, &acg, cbtocylno(&sblock, d)) 724 [cbtorpos(&sblock, d)]++; 725 } 726 sblock.fs_dsize += dlower; 727 } 728 sblock.fs_dsize += acg.cg_ndblk - dupper; 729 if (i = dupper % sblock.fs_frag) { 730 acg.cg_frsum[sblock.fs_frag - i]++; 731 for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) { 732 setbit(cg_blksfree(&acg), dupper); 733 acg.cg_cs.cs_nffree++; 734 } 735 } 736 for (d = dupper; d + sblock.fs_frag <= dmax - cbase; ) { 737 setblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag); 738 acg.cg_cs.cs_nbfree++; 739 cg_blktot(&acg)[cbtocylno(&sblock, d)]++; 740 cg_blks(&sblock, &acg, cbtocylno(&sblock, d)) 741 [cbtorpos(&sblock, d)]++; 742 d += sblock.fs_frag; 743 } 744 if (d < dmax - cbase) { 745 acg.cg_frsum[dmax - cbase - d]++; 746 for (; d < dmax - cbase; d++) { 747 setbit(cg_blksfree(&acg), d); 748 acg.cg_cs.cs_nffree++; 749 } 750 } 751 sblock.fs_cstotal.cs_ndir += acg.cg_cs.cs_ndir; 752 sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree; 753 sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree; 754 sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree; 755 *cs = acg.cg_cs; 756 wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)), 757 sblock.fs_bsize, (char *)&acg); 758 } 759 760 /* 761 * initialize the file system 762 */ 763 struct dinode node; 764 765 #ifdef LOSTDIR 766 #define PREDEFDIR 3 767 #else 768 #define PREDEFDIR 2 769 #endif 770 771 struct direct root_dir[] = { 772 { ROOTINO, sizeof(struct direct), 1, "." }, 773 { ROOTINO, sizeof(struct direct), 2, ".." }, 774 #ifdef LOSTDIR 775 { LOSTFOUNDINO, sizeof(struct direct), 10, "lost+found" }, 776 #endif 777 }; 778 #ifdef LOSTDIR 779 struct direct lost_found_dir[] = { 780 { LOSTFOUNDINO, sizeof(struct direct), 1, "." }, 781 { ROOTINO, sizeof(struct direct), 2, ".." }, 782 { 0, DIRBLKSIZ, 0, 0 }, 783 }; 784 #endif 785 char buf[MAXBSIZE]; 786 787 fsinit(utime) 788 time_t utime; 789 { 790 int i; 791 792 /* 793 * initialize the node 794 */ 795 node.di_atime = utime; 796 node.di_mtime = utime; 797 node.di_ctime = utime; 798 #ifdef LOSTDIR 799 /* 800 * create the lost+found directory 801 */ 802 (void)makedir(lost_found_dir, 2); 803 for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ) 804 bcopy(&lost_found_dir[2], &buf[i], DIRSIZ(&lost_found_dir[2])); 805 node.di_mode = IFDIR | UMASK; 806 node.di_nlink = 2; 807 node.di_size = sblock.fs_bsize; 808 node.di_db[0] = alloc(node.di_size, node.di_mode); 809 node.di_blocks = btodb(fragroundup(&sblock, node.di_size)); 810 wtfs(fsbtodb(&sblock, node.di_db[0]), node.di_size, buf); 811 iput(&node, LOSTFOUNDINO); 812 #endif 813 /* 814 * create the root directory 815 */ 816 if (mfs) 817 node.di_mode = IFDIR | 01777; 818 else 819 node.di_mode = IFDIR | UMASK; 820 node.di_nlink = PREDEFDIR; 821 node.di_size = makedir(root_dir, PREDEFDIR); 822 node.di_db[0] = alloc(sblock.fs_fsize, node.di_mode); 823 node.di_blocks = btodb(fragroundup(&sblock, node.di_size)); 824 wtfs(fsbtodb(&sblock, node.di_db[0]), sblock.fs_fsize, buf); 825 iput(&node, ROOTINO); 826 } 827 828 /* 829 * construct a set of directory entries in "buf". 830 * return size of directory. 831 */ 832 makedir(protodir, entries) 833 register struct direct *protodir; 834 int entries; 835 { 836 char *cp; 837 int i, spcleft; 838 839 spcleft = DIRBLKSIZ; 840 for (cp = buf, i = 0; i < entries - 1; i++) { 841 protodir[i].d_reclen = DIRSIZ(&protodir[i]); 842 bcopy(&protodir[i], cp, protodir[i].d_reclen); 843 cp += protodir[i].d_reclen; 844 spcleft -= protodir[i].d_reclen; 845 } 846 protodir[i].d_reclen = spcleft; 847 bcopy(&protodir[i], cp, DIRSIZ(&protodir[i])); 848 return (DIRBLKSIZ); 849 } 850 851 /* 852 * allocate a block or frag 853 */ 854 daddr_t 855 alloc(size, mode) 856 int size; 857 int mode; 858 { 859 int i, frag; 860 daddr_t d; 861 862 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, 863 (char *)&acg); 864 if (acg.cg_magic != CG_MAGIC) { 865 printf("cg 0: bad magic number\n"); 866 return (0); 867 } 868 if (acg.cg_cs.cs_nbfree == 0) { 869 printf("first cylinder group ran out of space\n"); 870 return (0); 871 } 872 for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag) 873 if (isblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag)) 874 goto goth; 875 printf("internal error: can't find block in cyl 0\n"); 876 return (0); 877 goth: 878 clrblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag); 879 acg.cg_cs.cs_nbfree--; 880 sblock.fs_cstotal.cs_nbfree--; 881 fscs[0].cs_nbfree--; 882 if (mode & IFDIR) { 883 acg.cg_cs.cs_ndir++; 884 sblock.fs_cstotal.cs_ndir++; 885 fscs[0].cs_ndir++; 886 } 887 cg_blktot(&acg)[cbtocylno(&sblock, d)]--; 888 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))[cbtorpos(&sblock, d)]--; 889 if (size != sblock.fs_bsize) { 890 frag = howmany(size, sblock.fs_fsize); 891 fscs[0].cs_nffree += sblock.fs_frag - frag; 892 sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag; 893 acg.cg_cs.cs_nffree += sblock.fs_frag - frag; 894 acg.cg_frsum[sblock.fs_frag - frag]++; 895 for (i = frag; i < sblock.fs_frag; i++) 896 setbit(cg_blksfree(&acg), d + i); 897 } 898 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, 899 (char *)&acg); 900 return (d); 901 } 902 903 /* 904 * Allocate an inode on the disk 905 */ 906 iput(ip, ino) 907 register struct dinode *ip; 908 register ino_t ino; 909 { 910 struct dinode buf[MAXINOPB]; 911 daddr_t d; 912 int c; 913 914 c = itog(&sblock, ino); 915 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, 916 (char *)&acg); 917 if (acg.cg_magic != CG_MAGIC) { 918 printf("cg 0: bad magic number\n"); 919 exit(31); 920 } 921 acg.cg_cs.cs_nifree--; 922 setbit(cg_inosused(&acg), ino); 923 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, 924 (char *)&acg); 925 sblock.fs_cstotal.cs_nifree--; 926 fscs[0].cs_nifree--; 927 if (ino >= sblock.fs_ipg * sblock.fs_ncg) { 928 printf("fsinit: inode value out of range (%d).\n", ino); 929 exit(32); 930 } 931 d = fsbtodb(&sblock, itod(&sblock, ino)); 932 rdfs(d, sblock.fs_bsize, buf); 933 buf[itoo(&sblock, ino)] = *ip; 934 wtfs(d, sblock.fs_bsize, buf); 935 } 936 937 /* 938 * Notify parent process that the filesystem has created itself successfully. 939 */ 940 void 941 started() 942 { 943 944 exit(0); 945 } 946 947 /* 948 * Replace libc function with one suited to our needs. 949 */ 950 caddr_t 951 malloc(size) 952 register u_long size; 953 { 954 u_long base, i; 955 static u_long pgsz; 956 struct rlimit rlp; 957 958 if (pgsz == 0) { 959 base = sbrk(0); 960 pgsz = getpagesize() - 1; 961 i = (base + pgsz) &~ pgsz; 962 base = sbrk(i - base); 963 if (getrlimit(RLIMIT_DATA, &rlp) < 0) 964 perror("getrlimit"); 965 rlp.rlim_cur = rlp.rlim_max; 966 if (setrlimit(RLIMIT_DATA, &rlp) < 0) 967 perror("setrlimit"); 968 memleft = rlp.rlim_max - base; 969 } 970 size = (size + pgsz) &~ pgsz; 971 if (size > memleft) 972 size = memleft; 973 memleft -= size; 974 if (size == 0) 975 return (0); 976 return ((caddr_t)sbrk(size)); 977 } 978 979 /* 980 * Replace libc function with one suited to our needs. 981 */ 982 caddr_t 983 realloc(ptr, size) 984 char *ptr; 985 u_long size; 986 { 987 988 /* always fail for now */ 989 return ((caddr_t)0); 990 } 991 992 /* 993 * Replace libc function with one suited to our needs. 994 */ 995 char * 996 calloc(size, numelm) 997 u_long size, numelm; 998 { 999 caddr_t base; 1000 1001 size *= numelm; 1002 base = malloc(size); 1003 bzero(base, size); 1004 return (base); 1005 } 1006 1007 /* 1008 * Replace libc function with one suited to our needs. 1009 */ 1010 free(ptr) 1011 char *ptr; 1012 { 1013 1014 /* do not worry about it for now */ 1015 } 1016 1017 /* 1018 * read a block from the file system 1019 */ 1020 rdfs(bno, size, bf) 1021 daddr_t bno; 1022 int size; 1023 char *bf; 1024 { 1025 int n; 1026 1027 if (mfs) { 1028 bcopy(membase + bno * sectorsize, bf, size); 1029 return; 1030 } 1031 if (lseek(fsi, bno * sectorsize, 0) < 0) { 1032 printf("seek error: %ld\n", bno); 1033 perror("rdfs"); 1034 exit(33); 1035 } 1036 n = read(fsi, bf, size); 1037 if(n != size) { 1038 printf("read error: %ld\n", bno); 1039 perror("rdfs"); 1040 exit(34); 1041 } 1042 } 1043 1044 /* 1045 * write a block to the file system 1046 */ 1047 wtfs(bno, size, bf) 1048 daddr_t bno; 1049 int size; 1050 char *bf; 1051 { 1052 int n; 1053 1054 if (mfs) { 1055 bcopy(bf, membase + bno * sectorsize, size); 1056 return; 1057 } 1058 if (Nflag) 1059 return; 1060 if (lseek(fso, bno * sectorsize, 0) < 0) { 1061 printf("seek error: %ld\n", bno); 1062 perror("wtfs"); 1063 exit(35); 1064 } 1065 n = write(fso, bf, size); 1066 if(n != size) { 1067 printf("write error: %ld\n", bno); 1068 perror("wtfs"); 1069 exit(36); 1070 } 1071 } 1072 1073 /* 1074 * check if a block is available 1075 */ 1076 isblock(fs, cp, h) 1077 struct fs *fs; 1078 unsigned char *cp; 1079 int h; 1080 { 1081 unsigned char mask; 1082 1083 switch (fs->fs_frag) { 1084 case 8: 1085 return (cp[h] == 0xff); 1086 case 4: 1087 mask = 0x0f << ((h & 0x1) << 2); 1088 return ((cp[h >> 1] & mask) == mask); 1089 case 2: 1090 mask = 0x03 << ((h & 0x3) << 1); 1091 return ((cp[h >> 2] & mask) == mask); 1092 case 1: 1093 mask = 0x01 << (h & 0x7); 1094 return ((cp[h >> 3] & mask) == mask); 1095 default: 1096 #ifdef STANDALONE 1097 printf("isblock bad fs_frag %d\n", fs->fs_frag); 1098 #else 1099 fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag); 1100 #endif 1101 return (0); 1102 } 1103 } 1104 1105 /* 1106 * take a block out of the map 1107 */ 1108 clrblock(fs, cp, h) 1109 struct fs *fs; 1110 unsigned char *cp; 1111 int h; 1112 { 1113 switch ((fs)->fs_frag) { 1114 case 8: 1115 cp[h] = 0; 1116 return; 1117 case 4: 1118 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2)); 1119 return; 1120 case 2: 1121 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1)); 1122 return; 1123 case 1: 1124 cp[h >> 3] &= ~(0x01 << (h & 0x7)); 1125 return; 1126 default: 1127 #ifdef STANDALONE 1128 printf("clrblock bad fs_frag %d\n", fs->fs_frag); 1129 #else 1130 fprintf(stderr, "clrblock bad fs_frag %d\n", fs->fs_frag); 1131 #endif 1132 return; 1133 } 1134 } 1135 1136 /* 1137 * put a block into the map 1138 */ 1139 setblock(fs, cp, h) 1140 struct fs *fs; 1141 unsigned char *cp; 1142 int h; 1143 { 1144 switch (fs->fs_frag) { 1145 case 8: 1146 cp[h] = 0xff; 1147 return; 1148 case 4: 1149 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2)); 1150 return; 1151 case 2: 1152 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1)); 1153 return; 1154 case 1: 1155 cp[h >> 3] |= (0x01 << (h & 0x7)); 1156 return; 1157 default: 1158 #ifdef STANDALONE 1159 printf("setblock bad fs_frag %d\n", fs->fs_frag); 1160 #else 1161 fprintf(stderr, "setblock bad fs_frag %d\n", fs->fs_frag); 1162 #endif 1163 return; 1164 } 1165 } 1166