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