1 /* 2 * Copyright (c) 1980, 1989, 1993 3 * The Regents of the University of California. 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[] = "@(#)mkfs.c 8.11 (Berkeley) 5/3/95"; 36 #endif /* not lint */ 37 38 #include <unistd.h> 39 #include <sys/param.h> 40 #include <sys/time.h> 41 #include <sys/wait.h> 42 #include <sys/resource.h> 43 #include <ufs/ufs/dinode.h> 44 #include <ufs/ufs/dir.h> 45 #include <ufs/ffs/fs.h> 46 #include <sys/disklabel.h> 47 #include <sys/file.h> 48 #include <sys/mman.h> 49 #include <sys/ioctl.h> 50 51 #ifndef STANDALONE 52 #include <a.out.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #endif 56 57 /* 58 * make file system for cylinder-group style file systems 59 */ 60 61 /* 62 * We limit the size of the inode map to be no more than a 63 * third of the cylinder group space, since we must leave at 64 * least an equal amount of space for the block map. 65 * 66 * N.B.: MAXIPG must be a multiple of INOPB(fs). 67 */ 68 #define MAXIPG(fs) roundup((fs)->fs_bsize * NBBY / 3, INOPB(fs)) 69 70 #define UMASK 0755 71 #define MAXINOPB (MAXBSIZE / sizeof(struct dinode)) 72 #define POWEROF2(num) (((num) & ((num) - 1)) == 0) 73 74 /* 75 * variables set up by front end. 76 */ 77 extern int mfs; /* run as the memory based filesystem */ 78 extern int Nflag; /* run mkfs without writing file system */ 79 extern int Oflag; /* format as an 4.3BSD file system */ 80 extern int fssize; /* file system size */ 81 extern int ntracks; /* # tracks/cylinder */ 82 extern int nsectors; /* # sectors/track */ 83 extern int nphyssectors; /* # sectors/track including spares */ 84 extern int secpercyl; /* sectors per cylinder */ 85 extern int sectorsize; /* bytes/sector */ 86 extern int realsectorsize; /* bytes/sector in hardware*/ 87 extern int rpm; /* revolutions/minute of drive */ 88 extern int interleave; /* hardware sector interleave */ 89 extern int trackskew; /* sector 0 skew, per track */ 90 extern int fsize; /* fragment size */ 91 extern int bsize; /* block size */ 92 extern int cpg; /* cylinders/cylinder group */ 93 extern int cpgflg; /* cylinders/cylinder group flag was given */ 94 extern int minfree; /* free space threshold */ 95 extern int opt; /* optimization preference (space or time) */ 96 extern int density; /* number of bytes per inode */ 97 extern int maxcontig; /* max contiguous blocks to allocate */ 98 extern int rotdelay; /* rotational delay between blocks */ 99 extern int maxbpg; /* maximum blocks per file in a cyl group */ 100 extern int nrpos; /* # of distinguished rotational positions */ 101 extern int bbsize; /* boot block size */ 102 extern int sbsize; /* superblock size */ 103 extern u_long memleft; /* virtual memory available */ 104 extern caddr_t membase; /* start address of memory based filesystem */ 105 #ifdef STANDALONE 106 extern caddr_t malloc(), calloc(); 107 #endif 108 extern char * filename; 109 110 union { 111 struct fs fs; 112 char pad[SBSIZE]; 113 } fsun; 114 #define sblock fsun.fs 115 struct csum *fscs; 116 117 union { 118 struct cg cg; 119 char pad[MAXBSIZE]; 120 } cgun; 121 #define acg cgun.cg 122 123 struct dinode zino[MAXBSIZE / sizeof(struct dinode)]; 124 125 int fsi, fso; 126 #ifdef FSIRAND 127 int randinit; 128 #endif 129 daddr_t alloc(); 130 long calcipg(); 131 static int charsperline(); 132 133 mkfs(pp, fsys, fi, fo) 134 struct partition *pp; 135 char *fsys; 136 int fi, fo; 137 { 138 register long i, mincpc, mincpg, inospercg; 139 long cylno, rpos, blk, j, warn = 0; 140 long used, mincpgcnt, bpcg; 141 off_t usedb; 142 long mapcramped, inodecramped; 143 long postblsize, rotblsize, totalsbsize; 144 int ppid, status, fd; 145 time_t utime; 146 quad_t sizepb; 147 void started(); 148 int width; 149 char tmpbuf[100]; /* XXX this will break in about 2,500 years */ 150 151 #ifndef STANDALONE 152 time(&utime); 153 #endif 154 #ifdef FSIRAND 155 if (!randinit) { 156 randinit = 1; 157 srandomdev(); 158 } 159 #endif 160 if (mfs) { 161 ppid = getpid(); 162 (void) signal(SIGUSR1, started); 163 if (i = fork()) { 164 if (i == -1) { 165 perror("mfs"); 166 exit(10); 167 } 168 if (waitpid(i, &status, 0) != -1 && WIFEXITED(status)) 169 exit(WEXITSTATUS(status)); 170 exit(11); 171 /* NOTREACHED */ 172 } 173 #ifdef STANDALONE 174 (void)malloc(0); 175 #else 176 raise_data_limit(); 177 #endif 178 if(filename) { 179 unsigned char buf[BUFSIZ]; 180 unsigned long l,l1; 181 fd = open(filename,O_RDWR|O_TRUNC|O_CREAT,0644); 182 if(fd < 0) { 183 perror(filename); 184 exit(12); 185 } 186 for(l=0;l< fssize * sectorsize;l += l1) { 187 l1 = fssize * sectorsize; 188 if (BUFSIZ < l1) 189 l1 = BUFSIZ; 190 if (l1 != write(fd,buf,l1)) { 191 perror(filename); 192 exit(12); 193 } 194 } 195 membase = mmap( 196 0, 197 fssize * sectorsize, 198 PROT_READ|PROT_WRITE, 199 MAP_SHARED, 200 fd, 201 0); 202 if(membase == MAP_FAILED) { 203 perror("mmap"); 204 exit(12); 205 } 206 close(fd); 207 } else { 208 #ifndef STANDALONE 209 get_memleft(); 210 #endif 211 if (fssize * sectorsize > (memleft - 16384)) 212 fssize = (memleft - 16384) / sectorsize; 213 if ((membase = malloc(fssize * sectorsize)) == 0) { 214 perror("malloc"); 215 exit(13); 216 } 217 } 218 } 219 fsi = fi; 220 fso = fo; 221 if (Oflag) { 222 sblock.fs_inodefmt = FS_42INODEFMT; 223 sblock.fs_maxsymlinklen = 0; 224 } else { 225 sblock.fs_inodefmt = FS_44INODEFMT; 226 sblock.fs_maxsymlinklen = MAXSYMLINKLEN; 227 } 228 /* 229 * Validate the given file system size. 230 * Verify that its last block can actually be accessed. 231 */ 232 if (fssize <= 0) 233 printf("preposterous size %d\n", fssize), exit(13); 234 wtfs(fssize - (realsectorsize / DEV_BSIZE), realsectorsize, 235 (char *)&sblock); 236 /* 237 * collect and verify the sector and track info 238 */ 239 sblock.fs_nsect = nsectors; 240 sblock.fs_ntrak = ntracks; 241 if (sblock.fs_ntrak <= 0) 242 printf("preposterous ntrak %d\n", sblock.fs_ntrak), exit(14); 243 if (sblock.fs_nsect <= 0) 244 printf("preposterous nsect %d\n", sblock.fs_nsect), exit(15); 245 /* 246 * collect and verify the block and fragment sizes 247 */ 248 sblock.fs_bsize = bsize; 249 sblock.fs_fsize = fsize; 250 if (!POWEROF2(sblock.fs_bsize)) { 251 printf("block size must be a power of 2, not %d\n", 252 sblock.fs_bsize); 253 exit(16); 254 } 255 if (!POWEROF2(sblock.fs_fsize)) { 256 printf("fragment size must be a power of 2, not %d\n", 257 sblock.fs_fsize); 258 exit(17); 259 } 260 if (sblock.fs_fsize < sectorsize) { 261 printf("fragment size %d is too small, minimum is %d\n", 262 sblock.fs_fsize, sectorsize); 263 exit(18); 264 } 265 if (sblock.fs_bsize < MINBSIZE) { 266 printf("block size %d is too small, minimum is %d\n", 267 sblock.fs_bsize, MINBSIZE); 268 exit(19); 269 } 270 if (sblock.fs_bsize < sblock.fs_fsize) { 271 printf("block size (%d) cannot be smaller than fragment size (%d)\n", 272 sblock.fs_bsize, sblock.fs_fsize); 273 exit(20); 274 } 275 sblock.fs_bmask = ~(sblock.fs_bsize - 1); 276 sblock.fs_fmask = ~(sblock.fs_fsize - 1); 277 sblock.fs_qbmask = ~sblock.fs_bmask; 278 sblock.fs_qfmask = ~sblock.fs_fmask; 279 for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1) 280 sblock.fs_bshift++; 281 for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1) 282 sblock.fs_fshift++; 283 sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize); 284 for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1) 285 sblock.fs_fragshift++; 286 if (sblock.fs_frag > MAXFRAG) { 287 printf("fragment size %d is too small, minimum with block size %d is %d\n", 288 sblock.fs_fsize, sblock.fs_bsize, 289 sblock.fs_bsize / MAXFRAG); 290 exit(21); 291 } 292 sblock.fs_nrpos = nrpos; 293 sblock.fs_nindir = sblock.fs_bsize / sizeof(daddr_t); 294 sblock.fs_inopb = sblock.fs_bsize / sizeof(struct dinode); 295 sblock.fs_nspf = sblock.fs_fsize / sectorsize; 296 for (sblock.fs_fsbtodb = 0, i = NSPF(&sblock); i > 1; i >>= 1) 297 sblock.fs_fsbtodb++; 298 sblock.fs_sblkno = 299 roundup(howmany(bbsize + sbsize, sblock.fs_fsize), sblock.fs_frag); 300 sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno + 301 roundup(howmany(sbsize, sblock.fs_fsize), sblock.fs_frag)); 302 sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag; 303 sblock.fs_cgoffset = roundup( 304 howmany(sblock.fs_nsect, NSPF(&sblock)), sblock.fs_frag); 305 for (sblock.fs_cgmask = 0xffffffff, i = sblock.fs_ntrak; i > 1; i >>= 1) 306 sblock.fs_cgmask <<= 1; 307 if (!POWEROF2(sblock.fs_ntrak)) 308 sblock.fs_cgmask <<= 1; 309 sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1; 310 for (sizepb = sblock.fs_bsize, i = 0; i < NIADDR; i++) { 311 sizepb *= NINDIR(&sblock); 312 sblock.fs_maxfilesize += sizepb; 313 } 314 /* 315 * Validate specified/determined secpercyl 316 * and calculate minimum cylinders per group. 317 */ 318 sblock.fs_spc = secpercyl; 319 for (sblock.fs_cpc = NSPB(&sblock), i = sblock.fs_spc; 320 sblock.fs_cpc > 1 && (i & 1) == 0; 321 sblock.fs_cpc >>= 1, i >>= 1) 322 /* void */; 323 mincpc = sblock.fs_cpc; 324 bpcg = sblock.fs_spc * sectorsize; 325 inospercg = roundup(bpcg / sizeof(struct dinode), INOPB(&sblock)); 326 if (inospercg > MAXIPG(&sblock)) 327 inospercg = MAXIPG(&sblock); 328 used = (sblock.fs_iblkno + inospercg / INOPF(&sblock)) * NSPF(&sblock); 329 mincpgcnt = howmany(sblock.fs_cgoffset * (~sblock.fs_cgmask) + used, 330 sblock.fs_spc); 331 mincpg = roundup(mincpgcnt, mincpc); 332 /* 333 * Ensure that cylinder group with mincpg has enough space 334 * for block maps. 335 */ 336 sblock.fs_cpg = mincpg; 337 sblock.fs_ipg = inospercg; 338 if (maxcontig > 1) 339 sblock.fs_contigsumsize = MIN(maxcontig, FS_MAXCONTIG); 340 mapcramped = 0; 341 while (CGSIZE(&sblock) > sblock.fs_bsize) { 342 mapcramped = 1; 343 if (sblock.fs_bsize < MAXBSIZE) { 344 sblock.fs_bsize <<= 1; 345 if ((i & 1) == 0) { 346 i >>= 1; 347 } else { 348 sblock.fs_cpc <<= 1; 349 mincpc <<= 1; 350 mincpg = roundup(mincpgcnt, mincpc); 351 sblock.fs_cpg = mincpg; 352 } 353 sblock.fs_frag <<= 1; 354 sblock.fs_fragshift += 1; 355 if (sblock.fs_frag <= MAXFRAG) 356 continue; 357 } 358 if (sblock.fs_fsize == sblock.fs_bsize) { 359 printf("There is no block size that"); 360 printf(" can support this disk\n"); 361 exit(22); 362 } 363 sblock.fs_frag >>= 1; 364 sblock.fs_fragshift -= 1; 365 sblock.fs_fsize <<= 1; 366 sblock.fs_nspf <<= 1; 367 } 368 /* 369 * Ensure that cylinder group with mincpg has enough space for inodes. 370 */ 371 inodecramped = 0; 372 inospercg = calcipg(mincpg, bpcg, &usedb); 373 sblock.fs_ipg = inospercg; 374 while (inospercg > MAXIPG(&sblock)) { 375 inodecramped = 1; 376 if (mincpc == 1 || sblock.fs_frag == 1 || 377 sblock.fs_bsize == MINBSIZE) 378 break; 379 printf("With a block size of %d %s %d\n", sblock.fs_bsize, 380 "minimum bytes per inode is", 381 (int)((mincpg * (off_t)bpcg - usedb) 382 / MAXIPG(&sblock) + 1)); 383 sblock.fs_bsize >>= 1; 384 sblock.fs_frag >>= 1; 385 sblock.fs_fragshift -= 1; 386 mincpc >>= 1; 387 sblock.fs_cpg = roundup(mincpgcnt, mincpc); 388 if (CGSIZE(&sblock) > sblock.fs_bsize) { 389 sblock.fs_bsize <<= 1; 390 break; 391 } 392 mincpg = sblock.fs_cpg; 393 inospercg = calcipg(mincpg, bpcg, &usedb); 394 sblock.fs_ipg = inospercg; 395 } 396 if (inodecramped) { 397 if (inospercg > MAXIPG(&sblock)) { 398 printf("Minimum bytes per inode is %d\n", 399 (int)((mincpg * (off_t)bpcg - usedb) 400 / MAXIPG(&sblock) + 1)); 401 } else if (!mapcramped) { 402 printf("With %d bytes per inode, ", density); 403 printf("minimum cylinders per group is %d\n", mincpg); 404 } 405 } 406 if (mapcramped) { 407 printf("With %d sectors per cylinder, ", sblock.fs_spc); 408 printf("minimum cylinders per group is %d\n", mincpg); 409 } 410 if (inodecramped || mapcramped) { 411 if (sblock.fs_bsize != bsize) 412 printf("%s to be changed from %d to %d\n", 413 "This requires the block size", 414 bsize, sblock.fs_bsize); 415 if (sblock.fs_fsize != fsize) 416 printf("\t%s to be changed from %d to %d\n", 417 "and the fragment size", 418 fsize, sblock.fs_fsize); 419 exit(23); 420 } 421 /* 422 * Calculate the number of cylinders per group 423 */ 424 sblock.fs_cpg = cpg; 425 if (sblock.fs_cpg % mincpc != 0) { 426 printf("%s groups must have a multiple of %d cylinders\n", 427 cpgflg ? "Cylinder" : "Warning: cylinder", mincpc); 428 sblock.fs_cpg = roundup(sblock.fs_cpg, mincpc); 429 if (!cpgflg) 430 cpg = sblock.fs_cpg; 431 } 432 /* 433 * Must ensure there is enough space for inodes. 434 */ 435 sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb); 436 while (sblock.fs_ipg > MAXIPG(&sblock)) { 437 inodecramped = 1; 438 sblock.fs_cpg -= mincpc; 439 sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb); 440 } 441 /* 442 * Must ensure there is enough space to hold block map. 443 */ 444 while (CGSIZE(&sblock) > sblock.fs_bsize) { 445 mapcramped = 1; 446 sblock.fs_cpg -= mincpc; 447 sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb); 448 } 449 sblock.fs_fpg = (sblock.fs_cpg * sblock.fs_spc) / NSPF(&sblock); 450 if ((sblock.fs_cpg * sblock.fs_spc) % NSPB(&sblock) != 0) { 451 printf("panic (fs_cpg * fs_spc) % NSPF != 0"); 452 exit(24); 453 } 454 if (sblock.fs_cpg < mincpg) { 455 printf("cylinder groups must have at least %d cylinders\n", 456 mincpg); 457 exit(25); 458 } else if (sblock.fs_cpg != cpg) { 459 if (!cpgflg) 460 printf("Warning: "); 461 else if (!mapcramped && !inodecramped) 462 exit(26); 463 if (mapcramped && inodecramped) 464 printf("Block size and bytes per inode restrict"); 465 else if (mapcramped) 466 printf("Block size restricts"); 467 else 468 printf("Bytes per inode restrict"); 469 printf(" cylinders per group to %d.\n", sblock.fs_cpg); 470 if (cpgflg) 471 exit(27); 472 } 473 sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock)); 474 /* 475 * Now have size for file system and nsect and ntrak. 476 * Determine number of cylinders and blocks in the file system. 477 */ 478 sblock.fs_size = fssize = dbtofsb(&sblock, fssize); 479 sblock.fs_ncyl = fssize * NSPF(&sblock) / sblock.fs_spc; 480 if (fssize * NSPF(&sblock) > sblock.fs_ncyl * sblock.fs_spc) { 481 sblock.fs_ncyl++; 482 warn = 1; 483 } 484 if (sblock.fs_ncyl < 1) { 485 printf("file systems must have at least one cylinder\n"); 486 exit(28); 487 } 488 /* 489 * Determine feasability/values of rotational layout tables. 490 * 491 * The size of the rotational layout tables is limited by the 492 * size of the superblock, SBSIZE. The amount of space available 493 * for tables is calculated as (SBSIZE - sizeof (struct fs)). 494 * The size of these tables is inversely proportional to the block 495 * size of the file system. The size increases if sectors per track 496 * are not powers of two, because more cylinders must be described 497 * by the tables before the rotational pattern repeats (fs_cpc). 498 */ 499 sblock.fs_interleave = interleave; 500 sblock.fs_trackskew = trackskew; 501 sblock.fs_npsect = nphyssectors; 502 sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT; 503 sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs)); 504 if (sblock.fs_ntrak == 1) { 505 sblock.fs_cpc = 0; 506 goto next; 507 } 508 postblsize = sblock.fs_nrpos * sblock.fs_cpc * sizeof(short); 509 rotblsize = sblock.fs_cpc * sblock.fs_spc / NSPB(&sblock); 510 totalsbsize = sizeof(struct fs) + rotblsize; 511 if (sblock.fs_nrpos == 8 && sblock.fs_cpc <= 16) { 512 /* use old static table space */ 513 sblock.fs_postbloff = (char *)(&sblock.fs_opostbl[0][0]) - 514 (char *)(&sblock.fs_firstfield); 515 sblock.fs_rotbloff = &sblock.fs_space[0] - 516 (u_char *)(&sblock.fs_firstfield); 517 } else { 518 /* use dynamic table space */ 519 sblock.fs_postbloff = &sblock.fs_space[0] - 520 (u_char *)(&sblock.fs_firstfield); 521 sblock.fs_rotbloff = sblock.fs_postbloff + postblsize; 522 totalsbsize += postblsize; 523 } 524 if (totalsbsize > SBSIZE || 525 sblock.fs_nsect > (1 << NBBY) * NSPB(&sblock)) { 526 printf("%s %s %d %s %d.%s", 527 "Warning: insufficient space in super block for\n", 528 "rotational layout tables with nsect", sblock.fs_nsect, 529 "and ntrak", sblock.fs_ntrak, 530 "\nFile system performance may be impaired.\n"); 531 sblock.fs_cpc = 0; 532 goto next; 533 } 534 sblock.fs_sbsize = fragroundup(&sblock, totalsbsize); 535 /* 536 * calculate the available blocks for each rotational position 537 */ 538 for (cylno = 0; cylno < sblock.fs_cpc; cylno++) 539 for (rpos = 0; rpos < sblock.fs_nrpos; rpos++) 540 fs_postbl(&sblock, cylno)[rpos] = -1; 541 for (i = (rotblsize - 1) * sblock.fs_frag; 542 i >= 0; i -= sblock.fs_frag) { 543 cylno = cbtocylno(&sblock, i); 544 rpos = cbtorpos(&sblock, i); 545 blk = fragstoblks(&sblock, i); 546 if (fs_postbl(&sblock, cylno)[rpos] == -1) 547 fs_rotbl(&sblock)[blk] = 0; 548 else 549 fs_rotbl(&sblock)[blk] = 550 fs_postbl(&sblock, cylno)[rpos] - blk; 551 fs_postbl(&sblock, cylno)[rpos] = blk; 552 } 553 next: 554 /* 555 * Compute/validate number of cylinder groups. 556 */ 557 sblock.fs_ncg = sblock.fs_ncyl / sblock.fs_cpg; 558 if (sblock.fs_ncyl % sblock.fs_cpg) 559 sblock.fs_ncg++; 560 sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock); 561 i = MIN(~sblock.fs_cgmask, sblock.fs_ncg - 1); 562 if (cgdmin(&sblock, i) - cgbase(&sblock, i) >= sblock.fs_fpg) { 563 printf("inode blocks/cyl group (%d) >= data blocks (%d)\n", 564 cgdmin(&sblock, i) - cgbase(&sblock, i) / sblock.fs_frag, 565 sblock.fs_fpg / sblock.fs_frag); 566 printf("number of cylinders per cylinder group (%d) %s.\n", 567 sblock.fs_cpg, "must be increased"); 568 exit(29); 569 } 570 j = sblock.fs_ncg - 1; 571 if ((i = fssize - j * sblock.fs_fpg) < sblock.fs_fpg && 572 cgdmin(&sblock, j) - cgbase(&sblock, j) > i) { 573 if (j == 0) { 574 printf("Filesystem must have at least %d sectors\n", 575 NSPF(&sblock) * 576 (cgdmin(&sblock, 0) + 3 * sblock.fs_frag)); 577 exit(30); 578 } 579 printf("Warning: inode blocks/cyl group (%d) >= data blocks (%d) in last\n", 580 (cgdmin(&sblock, j) - cgbase(&sblock, j)) / sblock.fs_frag, 581 i / sblock.fs_frag); 582 printf(" cylinder group. This implies %d sector(s) cannot be allocated.\n", 583 i * NSPF(&sblock)); 584 sblock.fs_ncg--; 585 sblock.fs_ncyl -= sblock.fs_ncyl % sblock.fs_cpg; 586 sblock.fs_size = fssize = sblock.fs_ncyl * sblock.fs_spc / 587 NSPF(&sblock); 588 warn = 0; 589 } 590 if (warn && !mfs) { 591 printf("Warning: %d sector(s) in last cylinder unallocated\n", 592 sblock.fs_spc - 593 (fssize * NSPF(&sblock) - (sblock.fs_ncyl - 1) 594 * sblock.fs_spc)); 595 } 596 /* 597 * fill in remaining fields of the super block 598 */ 599 sblock.fs_csaddr = cgdmin(&sblock, 0); 600 sblock.fs_cssize = 601 fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum)); 602 i = sblock.fs_bsize / sizeof(struct csum); 603 sblock.fs_csmask = ~(i - 1); 604 for (sblock.fs_csshift = 0; i > 1; i >>= 1) 605 sblock.fs_csshift++; 606 fscs = (struct csum *)calloc(1, sblock.fs_cssize); 607 if (fscs == NULL) { 608 perror("calloc"); 609 exit(31); 610 } 611 sblock.fs_magic = FS_MAGIC; 612 sblock.fs_rotdelay = rotdelay; 613 sblock.fs_minfree = minfree; 614 sblock.fs_maxcontig = maxcontig; 615 sblock.fs_maxbpg = maxbpg; 616 sblock.fs_rps = rpm / 60; 617 sblock.fs_optim = opt; 618 sblock.fs_cgrotor = 0; 619 sblock.fs_cstotal.cs_ndir = 0; 620 sblock.fs_cstotal.cs_nbfree = 0; 621 sblock.fs_cstotal.cs_nifree = 0; 622 sblock.fs_cstotal.cs_nffree = 0; 623 sblock.fs_fmod = 0; 624 sblock.fs_ronly = 0; 625 sblock.fs_clean = 1; 626 #ifdef FSIRAND 627 sblock.fs_id[0] = (long)utime; 628 sblock.fs_id[1] = random(); 629 #endif 630 631 /* 632 * Dump out summary information about file system. 633 */ 634 if (!mfs) { 635 printf("%s:\t%d sectors in %d %s of %d tracks, %d sectors\n", 636 fsys, sblock.fs_size * NSPF(&sblock), sblock.fs_ncyl, 637 "cylinders", sblock.fs_ntrak, sblock.fs_nsect); 638 #define B2MBFACTOR (1 / (1024.0 * 1024.0)) 639 printf("\t%.1fMB in %d cyl groups (%d c/g, %.2fMB/g, %d i/g)\n", 640 (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR, 641 sblock.fs_ncg, sblock.fs_cpg, 642 (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR, 643 sblock.fs_ipg); 644 #undef B2MBFACTOR 645 } 646 /* 647 * Now build the cylinders group blocks and 648 * then print out indices of cylinder groups. 649 */ 650 if (!mfs) 651 printf("super-block backups (for fsck -b #) at:\n"); 652 i = 0; 653 width = charsperline(); 654 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) { 655 initcg(cylno, utime); 656 if (mfs) 657 continue; 658 j = sprintf(tmpbuf, " %d,", 659 fsbtodb(&sblock, cgsblock(&sblock, cylno))); 660 if (i+j >= width) { 661 printf("\n"); 662 i = 0; 663 } 664 i += j; 665 printf("%s", tmpbuf); 666 fflush(stdout); 667 } 668 if (!mfs) 669 printf("\n"); 670 if (Nflag && !mfs) 671 exit(0); 672 /* 673 * Now construct the initial file system, 674 * then write out the super-block. 675 */ 676 fsinit(utime); 677 sblock.fs_time = utime; 678 wtfs((int)SBOFF / sectorsize, sbsize, (char *)&sblock); 679 for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize) 680 wtfs(fsbtodb(&sblock, sblock.fs_csaddr + numfrags(&sblock, i)), 681 sblock.fs_cssize - i < sblock.fs_bsize ? 682 sblock.fs_cssize - i : sblock.fs_bsize, 683 ((char *)fscs) + i); 684 /* 685 * Write out the duplicate super blocks 686 */ 687 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) 688 wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)), 689 sbsize, (char *)&sblock); 690 /* 691 * Update information about this partion in pack 692 * label, to that it may be updated on disk. 693 */ 694 pp->p_fstype = FS_BSDFFS; 695 pp->p_fsize = sblock.fs_fsize; 696 pp->p_frag = sblock.fs_frag; 697 pp->p_cpg = sblock.fs_cpg; 698 /* 699 * Notify parent process of success. 700 * Dissociate from session and tty. 701 */ 702 if (mfs) { 703 kill(ppid, SIGUSR1); 704 (void) setsid(); 705 (void) close(0); 706 (void) close(1); 707 (void) close(2); 708 (void) chdir("/"); 709 } 710 } 711 712 /* 713 * Initialize a cylinder group. 714 */ 715 initcg(cylno, utime) 716 int cylno; 717 time_t utime; 718 { 719 daddr_t cbase, d, dlower, dupper, dmax, blkno; 720 long i, j, s; 721 register struct csum *cs; 722 723 /* 724 * Determine block bounds for cylinder group. 725 * Allow space for super block summary information in first 726 * cylinder group. 727 */ 728 cbase = cgbase(&sblock, cylno); 729 dmax = cbase + sblock.fs_fpg; 730 if (dmax > sblock.fs_size) 731 dmax = sblock.fs_size; 732 dlower = cgsblock(&sblock, cylno) - cbase; 733 dupper = cgdmin(&sblock, cylno) - cbase; 734 if (cylno == 0) 735 dupper += howmany(sblock.fs_cssize, sblock.fs_fsize); 736 cs = fscs + cylno; 737 memset(&acg, 0, sblock.fs_cgsize); 738 acg.cg_time = utime; 739 acg.cg_magic = CG_MAGIC; 740 acg.cg_cgx = cylno; 741 if (cylno == sblock.fs_ncg - 1) 742 acg.cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg; 743 else 744 acg.cg_ncyl = sblock.fs_cpg; 745 acg.cg_niblk = sblock.fs_ipg; 746 acg.cg_ndblk = dmax - cbase; 747 if (sblock.fs_contigsumsize > 0) 748 acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag; 749 acg.cg_btotoff = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield); 750 acg.cg_boff = acg.cg_btotoff + sblock.fs_cpg * sizeof(long); 751 acg.cg_iusedoff = acg.cg_boff + 752 sblock.fs_cpg * sblock.fs_nrpos * sizeof(short); 753 acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, NBBY); 754 if (sblock.fs_contigsumsize <= 0) { 755 acg.cg_nextfreeoff = acg.cg_freeoff + 756 howmany(sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY); 757 } else { 758 acg.cg_clustersumoff = acg.cg_freeoff + howmany 759 (sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY) - 760 sizeof(long); 761 acg.cg_clustersumoff = 762 roundup(acg.cg_clustersumoff, sizeof(long)); 763 acg.cg_clusteroff = acg.cg_clustersumoff + 764 (sblock.fs_contigsumsize + 1) * sizeof(long); 765 acg.cg_nextfreeoff = acg.cg_clusteroff + howmany 766 (sblock.fs_cpg * sblock.fs_spc / NSPB(&sblock), NBBY); 767 } 768 if (acg.cg_nextfreeoff - (long)(&acg.cg_firstfield) > sblock.fs_cgsize) { 769 printf("Panic: cylinder group too big\n"); 770 exit(37); 771 } 772 acg.cg_cs.cs_nifree += sblock.fs_ipg; 773 if (cylno == 0) 774 for (i = 0; i < ROOTINO; i++) { 775 setbit(cg_inosused(&acg), i); 776 acg.cg_cs.cs_nifree--; 777 } 778 for (i = 0; i < sblock.fs_ipg / INOPF(&sblock); i += sblock.fs_frag) { 779 #ifdef FSIRAND 780 for (j = 0; j < sblock.fs_bsize / sizeof(struct dinode); j++) 781 zino[j].di_gen = random(); 782 #endif 783 wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i), 784 sblock.fs_bsize, (char *)zino); 785 } 786 if (cylno > 0) { 787 /* 788 * In cylno 0, beginning space is reserved 789 * for boot and super blocks. 790 */ 791 for (d = 0; d < dlower; d += sblock.fs_frag) { 792 blkno = d / sblock.fs_frag; 793 setblock(&sblock, cg_blksfree(&acg), blkno); 794 if (sblock.fs_contigsumsize > 0) 795 setbit(cg_clustersfree(&acg), blkno); 796 acg.cg_cs.cs_nbfree++; 797 cg_blktot(&acg)[cbtocylno(&sblock, d)]++; 798 cg_blks(&sblock, &acg, cbtocylno(&sblock, d)) 799 [cbtorpos(&sblock, d)]++; 800 } 801 sblock.fs_dsize += dlower; 802 } 803 sblock.fs_dsize += acg.cg_ndblk - dupper; 804 if (i = dupper % sblock.fs_frag) { 805 acg.cg_frsum[sblock.fs_frag - i]++; 806 for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) { 807 setbit(cg_blksfree(&acg), dupper); 808 acg.cg_cs.cs_nffree++; 809 } 810 } 811 for (d = dupper; d + sblock.fs_frag <= dmax - cbase; ) { 812 blkno = d / sblock.fs_frag; 813 setblock(&sblock, cg_blksfree(&acg), blkno); 814 if (sblock.fs_contigsumsize > 0) 815 setbit(cg_clustersfree(&acg), blkno); 816 acg.cg_cs.cs_nbfree++; 817 cg_blktot(&acg)[cbtocylno(&sblock, d)]++; 818 cg_blks(&sblock, &acg, cbtocylno(&sblock, d)) 819 [cbtorpos(&sblock, d)]++; 820 d += sblock.fs_frag; 821 } 822 if (d < dmax - cbase) { 823 acg.cg_frsum[dmax - cbase - d]++; 824 for (; d < dmax - cbase; d++) { 825 setbit(cg_blksfree(&acg), d); 826 acg.cg_cs.cs_nffree++; 827 } 828 } 829 if (sblock.fs_contigsumsize > 0) { 830 int32_t *sump = cg_clustersum(&acg); 831 u_char *mapp = cg_clustersfree(&acg); 832 int map = *mapp++; 833 int bit = 1; 834 int run = 0; 835 836 for (i = 0; i < acg.cg_nclusterblks; i++) { 837 if ((map & bit) != 0) { 838 run++; 839 } else if (run != 0) { 840 if (run > sblock.fs_contigsumsize) 841 run = sblock.fs_contigsumsize; 842 sump[run]++; 843 run = 0; 844 } 845 if ((i & (NBBY - 1)) != (NBBY - 1)) { 846 bit <<= 1; 847 } else { 848 map = *mapp++; 849 bit = 1; 850 } 851 } 852 if (run != 0) { 853 if (run > sblock.fs_contigsumsize) 854 run = sblock.fs_contigsumsize; 855 sump[run]++; 856 } 857 } 858 sblock.fs_cstotal.cs_ndir += acg.cg_cs.cs_ndir; 859 sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree; 860 sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree; 861 sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree; 862 *cs = acg.cg_cs; 863 wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)), 864 sblock.fs_bsize, (char *)&acg); 865 } 866 867 /* 868 * initialize the file system 869 */ 870 struct dinode node; 871 872 #ifdef LOSTDIR 873 #define PREDEFDIR 3 874 #else 875 #define PREDEFDIR 2 876 #endif 877 878 struct direct root_dir[] = { 879 { ROOTINO, sizeof(struct direct), DT_DIR, 1, "." }, 880 { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." }, 881 #ifdef LOSTDIR 882 { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 10, "lost+found" }, 883 #endif 884 }; 885 struct odirect { 886 u_long d_ino; 887 u_short d_reclen; 888 u_short d_namlen; 889 u_char d_name[MAXNAMLEN + 1]; 890 } oroot_dir[] = { 891 { ROOTINO, sizeof(struct direct), 1, "." }, 892 { ROOTINO, sizeof(struct direct), 2, ".." }, 893 #ifdef LOSTDIR 894 { LOSTFOUNDINO, sizeof(struct direct), 10, "lost+found" }, 895 #endif 896 }; 897 #ifdef LOSTDIR 898 struct direct lost_found_dir[] = { 899 { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 1, "." }, 900 { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." }, 901 { 0, DIRBLKSIZ, 0, 0, 0 }, 902 }; 903 struct odirect olost_found_dir[] = { 904 { LOSTFOUNDINO, sizeof(struct direct), 1, "." }, 905 { ROOTINO, sizeof(struct direct), 2, ".." }, 906 { 0, DIRBLKSIZ, 0, 0 }, 907 }; 908 #endif 909 char buf[MAXBSIZE]; 910 911 fsinit(utime) 912 time_t utime; 913 { 914 int i; 915 916 /* 917 * initialize the node 918 */ 919 node.di_atime = utime; 920 node.di_mtime = utime; 921 node.di_ctime = utime; 922 #ifdef LOSTDIR 923 /* 924 * create the lost+found directory 925 */ 926 if (Oflag) { 927 (void)makedir((struct direct *)olost_found_dir, 2); 928 for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ) 929 memmove(&buf[i], &olost_found_dir[2], 930 DIRSIZ(0, &olost_found_dir[2])); 931 } else { 932 (void)makedir(lost_found_dir, 2); 933 for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ) 934 memmove(&buf[i], &lost_found_dir[2], 935 DIRSIZ(0, &lost_found_dir[2])); 936 } 937 node.di_mode = IFDIR | UMASK; 938 node.di_nlink = 2; 939 node.di_size = sblock.fs_bsize; 940 node.di_db[0] = alloc(node.di_size, node.di_mode); 941 node.di_blocks = btodb(fragroundup(&sblock, node.di_size)); 942 wtfs(fsbtodb(&sblock, node.di_db[0]), node.di_size, buf); 943 iput(&node, LOSTFOUNDINO); 944 #endif 945 /* 946 * create the root directory 947 */ 948 if (mfs) 949 node.di_mode = IFDIR | 01777; 950 else 951 node.di_mode = IFDIR | UMASK; 952 node.di_nlink = PREDEFDIR; 953 if (Oflag) 954 node.di_size = makedir((struct direct *)oroot_dir, PREDEFDIR); 955 else 956 node.di_size = makedir(root_dir, PREDEFDIR); 957 node.di_db[0] = alloc(sblock.fs_fsize, node.di_mode); 958 node.di_blocks = btodb(fragroundup(&sblock, node.di_size)); 959 wtfs(fsbtodb(&sblock, node.di_db[0]), sblock.fs_fsize, buf); 960 iput(&node, ROOTINO); 961 } 962 963 /* 964 * construct a set of directory entries in "buf". 965 * return size of directory. 966 */ 967 makedir(protodir, entries) 968 register struct direct *protodir; 969 int entries; 970 { 971 char *cp; 972 int i, spcleft; 973 974 spcleft = DIRBLKSIZ; 975 for (cp = buf, i = 0; i < entries - 1; i++) { 976 protodir[i].d_reclen = DIRSIZ(0, &protodir[i]); 977 memmove(cp, &protodir[i], protodir[i].d_reclen); 978 cp += protodir[i].d_reclen; 979 spcleft -= protodir[i].d_reclen; 980 } 981 protodir[i].d_reclen = spcleft; 982 memmove(cp, &protodir[i], DIRSIZ(0, &protodir[i])); 983 return (DIRBLKSIZ); 984 } 985 986 /* 987 * allocate a block or frag 988 */ 989 daddr_t 990 alloc(size, mode) 991 int size; 992 int mode; 993 { 994 int i, frag; 995 daddr_t d, blkno; 996 997 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, 998 (char *)&acg); 999 if (acg.cg_magic != CG_MAGIC) { 1000 printf("cg 0: bad magic number\n"); 1001 return (0); 1002 } 1003 if (acg.cg_cs.cs_nbfree == 0) { 1004 printf("first cylinder group ran out of space\n"); 1005 return (0); 1006 } 1007 for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag) 1008 if (isblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag)) 1009 goto goth; 1010 printf("internal error: can't find block in cyl 0\n"); 1011 return (0); 1012 goth: 1013 blkno = fragstoblks(&sblock, d); 1014 clrblock(&sblock, cg_blksfree(&acg), blkno); 1015 if (sblock.fs_contigsumsize > 0) 1016 clrbit(cg_clustersfree(&acg), blkno); 1017 acg.cg_cs.cs_nbfree--; 1018 sblock.fs_cstotal.cs_nbfree--; 1019 fscs[0].cs_nbfree--; 1020 if (mode & IFDIR) { 1021 acg.cg_cs.cs_ndir++; 1022 sblock.fs_cstotal.cs_ndir++; 1023 fscs[0].cs_ndir++; 1024 } 1025 cg_blktot(&acg)[cbtocylno(&sblock, d)]--; 1026 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))[cbtorpos(&sblock, d)]--; 1027 if (size != sblock.fs_bsize) { 1028 frag = howmany(size, sblock.fs_fsize); 1029 fscs[0].cs_nffree += sblock.fs_frag - frag; 1030 sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag; 1031 acg.cg_cs.cs_nffree += sblock.fs_frag - frag; 1032 acg.cg_frsum[sblock.fs_frag - frag]++; 1033 for (i = frag; i < sblock.fs_frag; i++) 1034 setbit(cg_blksfree(&acg), d + i); 1035 } 1036 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, 1037 (char *)&acg); 1038 return (d); 1039 } 1040 1041 /* 1042 * Calculate number of inodes per group. 1043 */ 1044 long 1045 calcipg(cpg, bpcg, usedbp) 1046 long cpg; 1047 long bpcg; 1048 off_t *usedbp; 1049 { 1050 int i; 1051 long ipg, new_ipg, ncg, ncyl; 1052 off_t usedb; 1053 1054 /* 1055 * Prepare to scale by fssize / (number of sectors in cylinder groups). 1056 * Note that fssize is still in sectors, not filesystem blocks. 1057 */ 1058 ncyl = howmany(fssize, secpercyl); 1059 ncg = howmany(ncyl, cpg); 1060 /* 1061 * Iterate a few times to allow for ipg depending on itself. 1062 */ 1063 ipg = 0; 1064 for (i = 0; i < 10; i++) { 1065 usedb = (sblock.fs_iblkno + ipg / INOPF(&sblock)) 1066 * NSPF(&sblock) * (off_t)sectorsize; 1067 new_ipg = (cpg * (quad_t)bpcg - usedb) / density * fssize 1068 / ncg / secpercyl / cpg; 1069 new_ipg = roundup(new_ipg, INOPB(&sblock)); 1070 if (new_ipg == ipg) 1071 break; 1072 ipg = new_ipg; 1073 } 1074 *usedbp = usedb; 1075 return (ipg); 1076 } 1077 1078 /* 1079 * Allocate an inode on the disk 1080 */ 1081 iput(ip, ino) 1082 register struct dinode *ip; 1083 register ino_t ino; 1084 { 1085 struct dinode buf[MAXINOPB]; 1086 daddr_t d; 1087 int c; 1088 1089 #ifdef FSIRAND 1090 ip->di_gen = random(); 1091 #endif 1092 c = ino_to_cg(&sblock, ino); 1093 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, 1094 (char *)&acg); 1095 if (acg.cg_magic != CG_MAGIC) { 1096 printf("cg 0: bad magic number\n"); 1097 exit(31); 1098 } 1099 acg.cg_cs.cs_nifree--; 1100 setbit(cg_inosused(&acg), ino); 1101 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, 1102 (char *)&acg); 1103 sblock.fs_cstotal.cs_nifree--; 1104 fscs[0].cs_nifree--; 1105 if (ino >= sblock.fs_ipg * sblock.fs_ncg) { 1106 printf("fsinit: inode value out of range (%d).\n", ino); 1107 exit(32); 1108 } 1109 d = fsbtodb(&sblock, ino_to_fsba(&sblock, ino)); 1110 rdfs(d, sblock.fs_bsize, buf); 1111 buf[ino_to_fsbo(&sblock, ino)] = *ip; 1112 wtfs(d, sblock.fs_bsize, buf); 1113 } 1114 1115 /* 1116 * Notify parent process that the filesystem has created itself successfully. 1117 */ 1118 void 1119 started() 1120 { 1121 1122 exit(0); 1123 } 1124 1125 #ifdef STANDALONE 1126 /* 1127 * Replace libc function with one suited to our needs. 1128 */ 1129 caddr_t 1130 malloc(size) 1131 register u_long size; 1132 { 1133 char *base, *i; 1134 static u_long pgsz; 1135 struct rlimit rlp; 1136 1137 if (pgsz == 0) { 1138 base = sbrk(0); 1139 pgsz = getpagesize() - 1; 1140 i = (char *)((u_long)(base + pgsz) &~ pgsz); 1141 base = sbrk(i - base); 1142 if (getrlimit(RLIMIT_DATA, &rlp) < 0) 1143 perror("getrlimit"); 1144 rlp.rlim_cur = rlp.rlim_max; 1145 if (setrlimit(RLIMIT_DATA, &rlp) < 0) 1146 perror("setrlimit"); 1147 memleft = rlp.rlim_max - (u_long)base; 1148 } 1149 size = (size + pgsz) &~ pgsz; 1150 if (size > memleft) 1151 size = memleft; 1152 memleft -= size; 1153 if (size == 0) 1154 return (0); 1155 return ((caddr_t)sbrk(size)); 1156 } 1157 1158 /* 1159 * Replace libc function with one suited to our needs. 1160 */ 1161 caddr_t 1162 realloc(ptr, size) 1163 char *ptr; 1164 u_long size; 1165 { 1166 void *p; 1167 1168 if ((p = malloc(size)) == NULL) 1169 return (NULL); 1170 memmove(p, ptr, size); 1171 free(ptr); 1172 return (p); 1173 } 1174 1175 /* 1176 * Replace libc function with one suited to our needs. 1177 */ 1178 char * 1179 calloc(size, numelm) 1180 u_long size, numelm; 1181 { 1182 caddr_t base; 1183 1184 size *= numelm; 1185 base = malloc(size); 1186 memset(base, 0, size); 1187 return (base); 1188 } 1189 1190 /* 1191 * Replace libc function with one suited to our needs. 1192 */ 1193 free(ptr) 1194 char *ptr; 1195 { 1196 1197 /* do not worry about it for now */ 1198 } 1199 1200 #else /* !STANDALONE */ 1201 1202 raise_data_limit() 1203 { 1204 struct rlimit rlp; 1205 1206 if (getrlimit(RLIMIT_DATA, &rlp) < 0) 1207 perror("getrlimit"); 1208 rlp.rlim_cur = rlp.rlim_max; 1209 if (setrlimit(RLIMIT_DATA, &rlp) < 0) 1210 perror("setrlimit"); 1211 } 1212 1213 get_memleft() 1214 { 1215 char *base; 1216 static u_long pgsz, i; 1217 struct rlimit rlp; 1218 1219 base = sbrk(0); 1220 pgsz = getpagesize() - 1; 1221 i = ((u_long)(base + pgsz) &~ pgsz); 1222 if (getrlimit(RLIMIT_DATA, &rlp) < 0) 1223 perror("getrlimit"); 1224 memleft = rlp.rlim_cur - (u_long)base - i; 1225 } 1226 #endif /* STANDALONE */ 1227 1228 /* 1229 * read a block from the file system 1230 */ 1231 rdfs(bno, size, bf) 1232 daddr_t bno; 1233 int size; 1234 char *bf; 1235 { 1236 int n; 1237 1238 if (mfs) { 1239 memmove(bf, membase + bno * sectorsize, size); 1240 return; 1241 } 1242 if (lseek(fsi, (off_t)bno * sectorsize, 0) < 0) { 1243 printf("seek error: %ld\n", bno); 1244 perror("rdfs"); 1245 exit(33); 1246 } 1247 n = read(fsi, bf, size); 1248 if (n != size) { 1249 printf("read error: %ld\n", bno); 1250 perror("rdfs"); 1251 exit(34); 1252 } 1253 } 1254 1255 /* 1256 * write a block to the file system 1257 */ 1258 wtfs(bno, size, bf) 1259 daddr_t bno; 1260 int size; 1261 char *bf; 1262 { 1263 int n; 1264 1265 if (mfs) { 1266 memmove(membase + bno * sectorsize, bf, size); 1267 return; 1268 } 1269 if (Nflag) 1270 return; 1271 if (lseek(fso, (off_t)bno * sectorsize, SEEK_SET) < 0) { 1272 printf("seek error: %ld\n", bno); 1273 perror("wtfs"); 1274 exit(35); 1275 } 1276 n = write(fso, bf, size); 1277 if (n != size) { 1278 printf("write error: %ld\n", bno); 1279 perror("wtfs"); 1280 exit(36); 1281 } 1282 } 1283 1284 /* 1285 * check if a block is available 1286 */ 1287 isblock(fs, cp, h) 1288 struct fs *fs; 1289 unsigned char *cp; 1290 int h; 1291 { 1292 unsigned char mask; 1293 1294 switch (fs->fs_frag) { 1295 case 8: 1296 return (cp[h] == 0xff); 1297 case 4: 1298 mask = 0x0f << ((h & 0x1) << 2); 1299 return ((cp[h >> 1] & mask) == mask); 1300 case 2: 1301 mask = 0x03 << ((h & 0x3) << 1); 1302 return ((cp[h >> 2] & mask) == mask); 1303 case 1: 1304 mask = 0x01 << (h & 0x7); 1305 return ((cp[h >> 3] & mask) == mask); 1306 default: 1307 #ifdef STANDALONE 1308 printf("isblock bad fs_frag %d\n", fs->fs_frag); 1309 #else 1310 fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag); 1311 #endif 1312 return (0); 1313 } 1314 } 1315 1316 /* 1317 * take a block out of the map 1318 */ 1319 clrblock(fs, cp, h) 1320 struct fs *fs; 1321 unsigned char *cp; 1322 int h; 1323 { 1324 switch ((fs)->fs_frag) { 1325 case 8: 1326 cp[h] = 0; 1327 return; 1328 case 4: 1329 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2)); 1330 return; 1331 case 2: 1332 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1)); 1333 return; 1334 case 1: 1335 cp[h >> 3] &= ~(0x01 << (h & 0x7)); 1336 return; 1337 default: 1338 #ifdef STANDALONE 1339 printf("clrblock bad fs_frag %d\n", fs->fs_frag); 1340 #else 1341 fprintf(stderr, "clrblock bad fs_frag %d\n", fs->fs_frag); 1342 #endif 1343 return; 1344 } 1345 } 1346 1347 /* 1348 * put a block into the map 1349 */ 1350 setblock(fs, cp, h) 1351 struct fs *fs; 1352 unsigned char *cp; 1353 int h; 1354 { 1355 switch (fs->fs_frag) { 1356 case 8: 1357 cp[h] = 0xff; 1358 return; 1359 case 4: 1360 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2)); 1361 return; 1362 case 2: 1363 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1)); 1364 return; 1365 case 1: 1366 cp[h >> 3] |= (0x01 << (h & 0x7)); 1367 return; 1368 default: 1369 #ifdef STANDALONE 1370 printf("setblock bad fs_frag %d\n", fs->fs_frag); 1371 #else 1372 fprintf(stderr, "setblock bad fs_frag %d\n", fs->fs_frag); 1373 #endif 1374 return; 1375 } 1376 } 1377 1378 /* 1379 * Determine the number of characters in a 1380 * single line. 1381 */ 1382 1383 static int 1384 charsperline() 1385 { 1386 int columns; 1387 char *cp; 1388 struct winsize ws; 1389 extern char *getenv(); 1390 1391 columns = 0; 1392 if (ioctl(0, TIOCGWINSZ, &ws) != -1) 1393 columns = ws.ws_col; 1394 if (columns == 0 && (cp = getenv("COLUMNS"))) 1395 columns = atoi(cp); 1396 if (columns == 0) 1397 columns = 80; /* last resort */ 1398 return columns; 1399 } 1400