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