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.8 1994/04/25 18:28:36 cgd 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((daddr_t)(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.ts_sec = utime; 796 node.di_atime.ts_nsec = 0; 797 node.di_mtime.ts_sec = utime; 798 node.di_mtime.ts_nsec = 0; 799 node.di_ctime.ts_sec = utime; 800 node.di_ctime.ts_nsec = 0; 801 #ifdef LOSTDIR 802 /* 803 * create the lost+found directory 804 */ 805 (void)makedir(lost_found_dir, 2); 806 for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ) 807 bcopy(&lost_found_dir[2], &buf[i], DIRSIZ(&lost_found_dir[2])); 808 node.di_mode = IFDIR | UMASK; 809 node.di_nlink = 2; 810 node.di_size = sblock.fs_bsize; 811 node.di_db[0] = alloc(node.di_size, node.di_mode); 812 node.di_blocks = btodb(fragroundup(&sblock, node.di_size)); 813 wtfs(fsbtodb(&sblock, node.di_db[0]), node.di_size, buf); 814 iput(&node, LOSTFOUNDINO); 815 #endif 816 /* 817 * create the root directory 818 */ 819 if (mfs) 820 node.di_mode = IFDIR | 01777; 821 else 822 node.di_mode = IFDIR | UMASK; 823 node.di_nlink = PREDEFDIR; 824 node.di_size = makedir(root_dir, PREDEFDIR); 825 node.di_db[0] = alloc(sblock.fs_fsize, node.di_mode); 826 node.di_blocks = btodb(fragroundup(&sblock, node.di_size)); 827 wtfs(fsbtodb(&sblock, node.di_db[0]), sblock.fs_fsize, buf); 828 iput(&node, ROOTINO); 829 } 830 831 /* 832 * construct a set of directory entries in "buf". 833 * return size of directory. 834 */ 835 makedir(protodir, entries) 836 register struct direct *protodir; 837 int entries; 838 { 839 char *cp; 840 int i, spcleft; 841 842 spcleft = DIRBLKSIZ; 843 for (cp = buf, i = 0; i < entries - 1; i++) { 844 protodir[i].d_reclen = DIRSIZ(&protodir[i]); 845 bcopy(&protodir[i], cp, protodir[i].d_reclen); 846 cp += protodir[i].d_reclen; 847 spcleft -= protodir[i].d_reclen; 848 } 849 protodir[i].d_reclen = spcleft; 850 bcopy(&protodir[i], cp, DIRSIZ(&protodir[i])); 851 return (DIRBLKSIZ); 852 } 853 854 /* 855 * allocate a block or frag 856 */ 857 daddr_t 858 alloc(size, mode) 859 int size; 860 int mode; 861 { 862 int i, frag; 863 daddr_t d; 864 865 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, 866 (char *)&acg); 867 if (acg.cg_magic != CG_MAGIC) { 868 printf("cg 0: bad magic number\n"); 869 return (0); 870 } 871 if (acg.cg_cs.cs_nbfree == 0) { 872 printf("first cylinder group ran out of space\n"); 873 return (0); 874 } 875 for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag) 876 if (isblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag)) 877 goto goth; 878 printf("internal error: can't find block in cyl 0\n"); 879 return (0); 880 goth: 881 clrblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag); 882 acg.cg_cs.cs_nbfree--; 883 sblock.fs_cstotal.cs_nbfree--; 884 fscs[0].cs_nbfree--; 885 if (mode & IFDIR) { 886 acg.cg_cs.cs_ndir++; 887 sblock.fs_cstotal.cs_ndir++; 888 fscs[0].cs_ndir++; 889 } 890 cg_blktot(&acg)[cbtocylno(&sblock, d)]--; 891 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))[cbtorpos(&sblock, d)]--; 892 if (size != sblock.fs_bsize) { 893 frag = howmany(size, sblock.fs_fsize); 894 fscs[0].cs_nffree += sblock.fs_frag - frag; 895 sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag; 896 acg.cg_cs.cs_nffree += sblock.fs_frag - frag; 897 acg.cg_frsum[sblock.fs_frag - frag]++; 898 for (i = frag; i < sblock.fs_frag; i++) 899 setbit(cg_blksfree(&acg), d + i); 900 } 901 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, 902 (char *)&acg); 903 return (d); 904 } 905 906 /* 907 * Allocate an inode on the disk 908 */ 909 iput(ip, ino) 910 register struct dinode *ip; 911 register ino_t ino; 912 { 913 struct dinode buf[MAXINOPB]; 914 daddr_t d; 915 int c; 916 917 c = itog(&sblock, ino); 918 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, 919 (char *)&acg); 920 if (acg.cg_magic != CG_MAGIC) { 921 printf("cg 0: bad magic number\n"); 922 exit(31); 923 } 924 acg.cg_cs.cs_nifree--; 925 setbit(cg_inosused(&acg), ino); 926 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, 927 (char *)&acg); 928 sblock.fs_cstotal.cs_nifree--; 929 fscs[0].cs_nifree--; 930 if (ino >= sblock.fs_ipg * sblock.fs_ncg) { 931 printf("fsinit: inode value out of range (%d).\n", ino); 932 exit(32); 933 } 934 d = fsbtodb(&sblock, itod(&sblock, ino)); 935 rdfs(d, sblock.fs_bsize, buf); 936 buf[itoo(&sblock, ino)] = *ip; 937 wtfs(d, sblock.fs_bsize, buf); 938 } 939 940 /* 941 * Notify parent process that the filesystem has created itself successfully. 942 */ 943 void 944 started() 945 { 946 947 exit(0); 948 } 949 950 /* 951 * Replace libc function with one suited to our needs. 952 */ 953 caddr_t 954 malloc(size) 955 register u_long size; 956 { 957 u_long base, i; 958 static u_long pgsz; 959 struct rlimit rlp; 960 961 if (pgsz == 0) { 962 base = sbrk(0); 963 pgsz = getpagesize() - 1; 964 i = (base + pgsz) &~ pgsz; 965 base = sbrk(i - base); 966 if (getrlimit(RLIMIT_DATA, &rlp) < 0) 967 perror("getrlimit"); 968 rlp.rlim_cur = rlp.rlim_max; 969 if (setrlimit(RLIMIT_DATA, &rlp) < 0) 970 perror("setrlimit"); 971 memleft = rlp.rlim_max - base; 972 } 973 size = (size + pgsz) &~ pgsz; 974 if (size > memleft) 975 size = memleft; 976 memleft -= size; 977 if (size == 0) 978 return (0); 979 return ((caddr_t)sbrk(size)); 980 } 981 982 /* 983 * Replace libc function with one suited to our needs. 984 */ 985 caddr_t 986 realloc(ptr, size) 987 char *ptr; 988 u_long size; 989 { 990 991 /* always fail for now */ 992 return ((caddr_t)0); 993 } 994 995 /* 996 * Replace libc function with one suited to our needs. 997 */ 998 char * 999 calloc(size, numelm) 1000 u_long size, numelm; 1001 { 1002 caddr_t base; 1003 1004 size *= numelm; 1005 base = malloc(size); 1006 bzero(base, size); 1007 return (base); 1008 } 1009 1010 /* 1011 * Replace libc function with one suited to our needs. 1012 */ 1013 free(ptr) 1014 char *ptr; 1015 { 1016 1017 /* do not worry about it for now */ 1018 } 1019 1020 /* 1021 * read a block from the file system 1022 */ 1023 rdfs(bno, size, bf) 1024 daddr_t bno; 1025 int size; 1026 char *bf; 1027 { 1028 int n; 1029 1030 if (mfs) { 1031 bcopy(membase + bno * sectorsize, bf, size); 1032 return; 1033 } 1034 if (lseek(fsi, bno * sectorsize, 0) < 0) { 1035 printf("seek error: %ld\n", bno); 1036 perror("rdfs"); 1037 exit(33); 1038 } 1039 n = read(fsi, bf, size); 1040 if(n != size) { 1041 printf("read error: %ld\n", bno); 1042 perror("rdfs"); 1043 exit(34); 1044 } 1045 } 1046 1047 /* 1048 * write a block to the file system 1049 */ 1050 wtfs(bno, size, bf) 1051 daddr_t bno; 1052 int size; 1053 char *bf; 1054 { 1055 int n; 1056 1057 if (mfs) { 1058 bcopy(bf, membase + bno * sectorsize, size); 1059 return; 1060 } 1061 if (Nflag) 1062 return; 1063 if (lseek(fso, bno * sectorsize, 0) < 0) { 1064 printf("seek error: %ld\n", bno); 1065 perror("wtfs"); 1066 exit(35); 1067 } 1068 n = write(fso, bf, size); 1069 if(n != size) { 1070 printf("write error: %ld\n", bno); 1071 perror("wtfs"); 1072 exit(36); 1073 } 1074 } 1075 1076 /* 1077 * check if a block is available 1078 */ 1079 isblock(fs, cp, h) 1080 struct fs *fs; 1081 unsigned char *cp; 1082 int h; 1083 { 1084 unsigned char mask; 1085 1086 switch (fs->fs_frag) { 1087 case 8: 1088 return (cp[h] == 0xff); 1089 case 4: 1090 mask = 0x0f << ((h & 0x1) << 2); 1091 return ((cp[h >> 1] & mask) == mask); 1092 case 2: 1093 mask = 0x03 << ((h & 0x3) << 1); 1094 return ((cp[h >> 2] & mask) == mask); 1095 case 1: 1096 mask = 0x01 << (h & 0x7); 1097 return ((cp[h >> 3] & mask) == mask); 1098 default: 1099 #ifdef STANDALONE 1100 printf("isblock bad fs_frag %d\n", fs->fs_frag); 1101 #else 1102 fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag); 1103 #endif 1104 return (0); 1105 } 1106 } 1107 1108 /* 1109 * take a block out of the map 1110 */ 1111 clrblock(fs, cp, h) 1112 struct fs *fs; 1113 unsigned char *cp; 1114 int h; 1115 { 1116 switch ((fs)->fs_frag) { 1117 case 8: 1118 cp[h] = 0; 1119 return; 1120 case 4: 1121 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2)); 1122 return; 1123 case 2: 1124 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1)); 1125 return; 1126 case 1: 1127 cp[h >> 3] &= ~(0x01 << (h & 0x7)); 1128 return; 1129 default: 1130 #ifdef STANDALONE 1131 printf("clrblock bad fs_frag %d\n", fs->fs_frag); 1132 #else 1133 fprintf(stderr, "clrblock bad fs_frag %d\n", fs->fs_frag); 1134 #endif 1135 return; 1136 } 1137 } 1138 1139 /* 1140 * put a block into the map 1141 */ 1142 setblock(fs, cp, h) 1143 struct fs *fs; 1144 unsigned char *cp; 1145 int h; 1146 { 1147 switch (fs->fs_frag) { 1148 case 8: 1149 cp[h] = 0xff; 1150 return; 1151 case 4: 1152 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2)); 1153 return; 1154 case 2: 1155 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1)); 1156 return; 1157 case 1: 1158 cp[h >> 3] |= (0x01 << (h & 0x7)); 1159 return; 1160 default: 1161 #ifdef STANDALONE 1162 printf("setblock bad fs_frag %d\n", fs->fs_frag); 1163 #else 1164 fprintf(stderr, "setblock bad fs_frag %d\n", fs->fs_frag); 1165 #endif 1166 return; 1167 } 1168 } 1169