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 " 258 "fragment size (%d)\n", 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 " 274 "block size %d is %d\n", 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 = 290 roundup(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", fsize, sblock.fs_fsize); 404 exit(23); 405 } 406 /* 407 * Calculate the number of cylinders per group 408 */ 409 sblock.fs_cpg = cpg; 410 if (sblock.fs_cpg % mincpc != 0) { 411 printf("%s groups must have a multiple of %ld cylinders\n", 412 cpgflg ? "Cylinder" : "Warning: cylinder", mincpc); 413 sblock.fs_cpg = roundup(sblock.fs_cpg, mincpc); 414 if (!cpgflg) 415 cpg = sblock.fs_cpg; 416 } 417 /* 418 * Must ensure there is enough space for inodes. 419 */ 420 sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb); 421 while (sblock.fs_ipg > MAXIPG(&sblock)) { 422 inodecramped = 1; 423 sblock.fs_cpg -= mincpc; 424 sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb); 425 } 426 /* 427 * Must ensure there is enough space to hold block map. 428 */ 429 while (CGSIZE(&sblock) > sblock.fs_bsize) { 430 mapcramped = 1; 431 sblock.fs_cpg -= mincpc; 432 sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb); 433 } 434 sblock.fs_fpg = (sblock.fs_cpg * sblock.fs_spc) / NSPF(&sblock); 435 if ((sblock.fs_cpg * sblock.fs_spc) % NSPB(&sblock) != 0) { 436 printf("panic (fs_cpg * fs_spc) %% NSPF != 0"); 437 exit(24); 438 } 439 if (sblock.fs_cpg < mincpg) { 440 printf("cylinder groups must have at least %ld cylinders\n", 441 mincpg); 442 exit(25); 443 } else if (sblock.fs_cpg != cpg) { 444 if (!cpgflg) 445 printf("Warning: "); 446 else if (!mapcramped && !inodecramped) 447 exit(26); 448 if (mapcramped && inodecramped) 449 printf("Block size and bytes per inode restrict"); 450 else if (mapcramped) 451 printf("Block size restricts"); 452 else 453 printf("Bytes per inode restrict"); 454 printf(" cylinders per group to %d.\n", sblock.fs_cpg); 455 if (cpgflg) 456 exit(27); 457 } 458 sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock)); 459 /* 460 * Now have size for file system and nsect and ntrak. 461 * Determine number of cylinders and blocks in the file system. 462 */ 463 sblock.fs_size = fssize = dbtofsb(&sblock, fssize); 464 sblock.fs_ncyl = fssize * NSPF(&sblock) / sblock.fs_spc; 465 if (fssize * NSPF(&sblock) > sblock.fs_ncyl * sblock.fs_spc) { 466 sblock.fs_ncyl++; 467 warn = 1; 468 } 469 if (sblock.fs_ncyl < 1) { 470 printf("file systems must have at least one cylinder\n"); 471 exit(28); 472 } 473 /* 474 * Determine feasability/values of rotational layout tables. 475 * 476 * The size of the rotational layout tables is limited by the 477 * size of the superblock, SBSIZE. The amount of space available 478 * for tables is calculated as (SBSIZE - sizeof (struct fs)). 479 * The size of these tables is inversely proportional to the block 480 * size of the file system. The size increases if sectors per track 481 * are not powers of two, because more cylinders must be described 482 * by the tables before the rotational pattern repeats (fs_cpc). 483 */ 484 sblock.fs_interleave = interleave; 485 sblock.fs_trackskew = trackskew; 486 sblock.fs_npsect = nphyssectors; 487 sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT; 488 sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs)); 489 if (sblock.fs_sbsize > SBSIZE) 490 sblock.fs_sbsize = SBSIZE; 491 if (sblock.fs_ntrak == 1) { 492 sblock.fs_cpc = 0; 493 goto next; 494 } 495 postblsize = sblock.fs_nrpos * sblock.fs_cpc * sizeof(int16_t); 496 rotblsize = sblock.fs_cpc * sblock.fs_spc / NSPB(&sblock); 497 totalsbsize = sizeof(struct fs) + rotblsize; 498 if (sblock.fs_nrpos == 8 && sblock.fs_cpc <= 16) { 499 /* use old static table space */ 500 sblock.fs_postbloff = (char *)(&sblock.fs_opostbl[0][0]) - 501 (char *)(&sblock.fs_firstfield); 502 sblock.fs_rotbloff = &sblock.fs_space[0] - 503 (u_char *)(&sblock.fs_firstfield); 504 } else { 505 /* use dynamic table space */ 506 sblock.fs_postbloff = &sblock.fs_space[0] - 507 (u_char *)(&sblock.fs_firstfield); 508 sblock.fs_rotbloff = sblock.fs_postbloff + postblsize; 509 totalsbsize += postblsize; 510 } 511 if (totalsbsize > SBSIZE || 512 sblock.fs_nsect > (1 << NBBY) * NSPB(&sblock)) { 513 printf("%s %s %d %s %d.%s", 514 "Warning: insufficient space in super block for\n", 515 "rotational layout tables with nsect", sblock.fs_nsect, 516 "and ntrak", sblock.fs_ntrak, 517 "\nFile system performance may be impaired.\n"); 518 sblock.fs_cpc = 0; 519 goto next; 520 } 521 sblock.fs_sbsize = fragroundup(&sblock, totalsbsize); 522 if (sblock.fs_sbsize > SBSIZE) 523 sblock.fs_sbsize = SBSIZE; 524 /* 525 * calculate the available blocks for each rotational position 526 */ 527 for (cylno = 0; cylno < sblock.fs_cpc; cylno++) 528 for (rpos = 0; rpos < sblock.fs_nrpos; rpos++) 529 fs_postbl(&sblock, cylno)[rpos] = -1; 530 for (i = (rotblsize - 1) * sblock.fs_frag; 531 i >= 0; i -= sblock.fs_frag) { 532 cylno = cbtocylno(&sblock, i); 533 rpos = cbtorpos(&sblock, i); 534 blk = fragstoblks(&sblock, i); 535 if (fs_postbl(&sblock, cylno)[rpos] == -1) 536 fs_rotbl(&sblock)[blk] = 0; 537 else 538 fs_rotbl(&sblock)[blk] = 539 fs_postbl(&sblock, cylno)[rpos] - blk; 540 fs_postbl(&sblock, cylno)[rpos] = blk; 541 } 542 next: 543 /* 544 * Compute/validate number of cylinder groups. 545 */ 546 sblock.fs_ncg = sblock.fs_ncyl / sblock.fs_cpg; 547 if (sblock.fs_ncyl % sblock.fs_cpg) 548 sblock.fs_ncg++; 549 sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock); 550 i = MIN(~sblock.fs_cgmask, sblock.fs_ncg - 1); 551 if (cgdmin(&sblock, i) - cgbase(&sblock, i) >= sblock.fs_fpg) { 552 printf("inode blocks/cyl group (%ld) >= data blocks (%ld)\n", 553 cgdmin(&sblock, i) - cgbase(&sblock, i) / sblock.fs_frag, 554 (long)(sblock.fs_fpg / sblock.fs_frag)); 555 printf("number of cylinders per cylinder group (%d) %s.\n", 556 sblock.fs_cpg, "must be increased"); 557 exit(29); 558 } 559 j = sblock.fs_ncg - 1; 560 if ((i = fssize - j * sblock.fs_fpg) < sblock.fs_fpg && 561 cgdmin(&sblock, j) - cgbase(&sblock, j) > i) { 562 if (j == 0) { 563 printf("Filesystem must have at least %d sectors\n", 564 NSPF(&sblock) * 565 (cgdmin(&sblock, 0) + 3 * sblock.fs_frag)); 566 exit(30); 567 } 568 printf("Warning: inode blocks/cyl group (%ld) >= " 569 "data blocks (%ld) in last\n", 570 (cgdmin(&sblock, j) - cgbase(&sblock, j)) / sblock.fs_frag, 571 i / sblock.fs_frag); 572 printf(" cylinder group. This implies %ld sector(s) " 573 "cannot be allocated.\n", i * NSPF(&sblock)); 574 sblock.fs_ncg--; 575 sblock.fs_ncyl -= sblock.fs_ncyl % sblock.fs_cpg; 576 sblock.fs_size = fssize = sblock.fs_ncyl * sblock.fs_spc / 577 NSPF(&sblock); 578 warn = 0; 579 } 580 if (warn) { 581 printf("Warning: %d sector(s) in last cylinder unallocated\n", 582 sblock.fs_spc - 583 (fssize * NSPF(&sblock) - (sblock.fs_ncyl - 1) * 584 sblock.fs_spc)); 585 } 586 /* 587 * fill in remaining fields of the super block 588 */ 589 sblock.fs_csaddr = cgdmin(&sblock, 0); 590 sblock.fs_cssize = 591 fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum)); 592 /* 593 * The superblock fields 'fs_csmask' and 'fs_csshift' are no 594 * longer used. However, we still initialise them so that the 595 * filesystem remains compatible with old kernels. 596 */ 597 i = sblock.fs_bsize / sizeof(struct csum); 598 sblock.fs_csmask = ~(i - 1); 599 for (sblock.fs_csshift = 0; i > 1; i >>= 1) 600 sblock.fs_csshift++; 601 fscs = (struct csum *)calloc(1, sblock.fs_cssize); 602 if (fscs == NULL) 603 errx(31, "calloc failed"); 604 sblock.fs_magic = FS_MAGIC; 605 sblock.fs_rotdelay = rotdelay; 606 sblock.fs_minfree = minfree; 607 sblock.fs_maxcontig = maxcontig; 608 sblock.fs_maxbpg = maxbpg; 609 sblock.fs_rps = rpm / 60; 610 sblock.fs_optim = opt; 611 sblock.fs_cgrotor = 0; 612 sblock.fs_cstotal.cs_ndir = 0; 613 sblock.fs_cstotal.cs_nbfree = 0; 614 sblock.fs_cstotal.cs_nifree = 0; 615 sblock.fs_cstotal.cs_nffree = 0; 616 sblock.fs_fmod = 0; 617 sblock.fs_ronly = 0; 618 sblock.fs_clean = 1; 619 #ifdef FSIRAND 620 sblock.fs_id[0] = (long)utime; 621 sblock.fs_id[1] = random(); 622 #endif 623 624 /* 625 * Dump out summary information about file system. 626 */ 627 printf("%s:\t%d sectors in %d %s of %d tracks, %d sectors\n", 628 fsys, sblock.fs_size * NSPF(&sblock), sblock.fs_ncyl, 629 "cylinders", sblock.fs_ntrak, sblock.fs_nsect); 630 #define B2MBFACTOR (1 / (1024.0 * 1024.0)) 631 printf("\t%.1fMB in %d cyl groups (%d c/g, %.2fMB/g, %d i/g)%s\n", 632 (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR, 633 sblock.fs_ncg, sblock.fs_cpg, 634 (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR, 635 sblock.fs_ipg, 636 sblock.fs_flags & FS_DOSOFTDEP ? " SOFTUPDATES" : ""); 637 #undef B2MBFACTOR 638 /* 639 * Now build the cylinders group blocks and 640 * then print out indices of cylinder groups. 641 */ 642 printf("super-block backups (for fsck -b #) at:\n"); 643 i = 0; 644 width = charsperline(); 645 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) { 646 initcg(cylno, utime); 647 j = snprintf(tmpbuf, sizeof(tmpbuf), " %ld%s", 648 fsbtodb(&sblock, cgsblock(&sblock, cylno)), 649 cylno < (sblock.fs_ncg-1) ? "," : ""); 650 if (j < 0) 651 tmpbuf[j = 0] = '\0'; 652 if (i + j >= width) { 653 printf("\n"); 654 i = 0; 655 } 656 i += j; 657 printf("%s", tmpbuf); 658 fflush(stdout); 659 } 660 printf("\n"); 661 if (Nflag) 662 exit(0); 663 /* 664 * Now construct the initial file system, 665 * then write out the super-block. 666 */ 667 fsinit(utime); 668 sblock.fs_time = utime; 669 wtfs((int)SBOFF / sectorsize, sbsize, (char *)&sblock); 670 for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize) 671 wtfs(fsbtodb(&sblock, sblock.fs_csaddr + numfrags(&sblock, i)), 672 sblock.fs_cssize - i < sblock.fs_bsize ? 673 sblock.fs_cssize - i : sblock.fs_bsize, 674 ((char *)fscs) + i); 675 /* 676 * Write out the duplicate super blocks 677 */ 678 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) 679 wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)), 680 sbsize, (char *)&sblock); 681 wtfsflush(); 682 /* 683 * Update information about this partion in pack 684 * label, to that it may be updated on disk. 685 */ 686 pp->p_fstype = FS_BSDFFS; 687 pp->p_fsize = sblock.fs_fsize; 688 pp->p_frag = sblock.fs_frag; 689 pp->p_cpg = sblock.fs_cpg; 690 } 691 692 /* 693 * Initialize a cylinder group. 694 */ 695 void 696 initcg(cylno, utime) 697 int cylno; 698 time_t utime; 699 { 700 daddr_t cbase, d, dlower, dupper, dmax, blkno; 701 long i; 702 register struct csum *cs; 703 #ifdef FSIRAND 704 long j; 705 #endif 706 707 /* 708 * Determine block bounds for cylinder group. 709 * Allow space for super block summary information in first 710 * cylinder group. 711 */ 712 cbase = cgbase(&sblock, cylno); 713 dmax = cbase + sblock.fs_fpg; 714 if (dmax > sblock.fs_size) 715 dmax = sblock.fs_size; 716 dlower = cgsblock(&sblock, cylno) - cbase; 717 dupper = cgdmin(&sblock, cylno) - cbase; 718 if (cylno == 0) 719 dupper += howmany(sblock.fs_cssize, sblock.fs_fsize); 720 cs = fscs + cylno; 721 memset(&acg, 0, sblock.fs_cgsize); 722 acg.cg_time = utime; 723 acg.cg_magic = CG_MAGIC; 724 acg.cg_cgx = cylno; 725 if (cylno == sblock.fs_ncg - 1) 726 acg.cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg; 727 else 728 acg.cg_ncyl = sblock.fs_cpg; 729 acg.cg_niblk = sblock.fs_ipg; 730 acg.cg_ndblk = dmax - cbase; 731 if (sblock.fs_contigsumsize > 0) 732 acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag; 733 acg.cg_btotoff = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield); 734 acg.cg_boff = acg.cg_btotoff + sblock.fs_cpg * sizeof(int32_t); 735 acg.cg_iusedoff = acg.cg_boff + 736 sblock.fs_cpg * sblock.fs_nrpos * sizeof(u_int16_t); 737 acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, NBBY); 738 if (sblock.fs_contigsumsize <= 0) { 739 acg.cg_nextfreeoff = acg.cg_freeoff + 740 howmany(sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY); 741 } else { 742 acg.cg_clustersumoff = acg.cg_freeoff + howmany 743 (sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY) - 744 sizeof(u_int32_t); 745 acg.cg_clustersumoff = 746 roundup(acg.cg_clustersumoff, sizeof(u_int32_t)); 747 acg.cg_clusteroff = acg.cg_clustersumoff + 748 (sblock.fs_contigsumsize + 1) * sizeof(u_int32_t); 749 acg.cg_nextfreeoff = acg.cg_clusteroff + howmany 750 (sblock.fs_cpg * sblock.fs_spc / NSPB(&sblock), NBBY); 751 } 752 if (acg.cg_nextfreeoff - (long)(&acg.cg_firstfield) > sblock.fs_cgsize) 753 { 754 printf("Panic: cylinder group too big\n"); 755 exit(37); 756 } 757 acg.cg_cs.cs_nifree += sblock.fs_ipg; 758 if (cylno == 0) 759 for (i = 0; i < ROOTINO; i++) { 760 setbit(cg_inosused(&acg), i); 761 acg.cg_cs.cs_nifree--; 762 } 763 for (i = 0; i < sblock.fs_ipg / INOPF(&sblock); i += sblock.fs_frag) { 764 #ifdef FSIRAND 765 for (j = 0; j < sblock.fs_bsize / sizeof(struct dinode); j++) 766 zino[j].di_gen = random(); 767 #endif 768 wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i), 769 sblock.fs_bsize, (char *)zino); 770 } 771 if (cylno > 0) { 772 /* 773 * In cylno 0, beginning space is reserved 774 * for boot and super blocks. 775 */ 776 for (d = 0; d < dlower; d += sblock.fs_frag) { 777 blkno = d / sblock.fs_frag; 778 setblock(&sblock, cg_blksfree(&acg), blkno); 779 if (sblock.fs_contigsumsize > 0) 780 setbit(cg_clustersfree(&acg), blkno); 781 acg.cg_cs.cs_nbfree++; 782 cg_blktot(&acg)[cbtocylno(&sblock, d)]++; 783 cg_blks(&sblock, &acg, cbtocylno(&sblock, d)) 784 [cbtorpos(&sblock, d)]++; 785 } 786 sblock.fs_dsize += dlower; 787 } 788 sblock.fs_dsize += acg.cg_ndblk - dupper; 789 if ((i = dupper % sblock.fs_frag)) { 790 acg.cg_frsum[sblock.fs_frag - i]++; 791 for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) { 792 setbit(cg_blksfree(&acg), dupper); 793 acg.cg_cs.cs_nffree++; 794 } 795 } 796 for (d = dupper; d + sblock.fs_frag <= dmax - cbase;) { 797 blkno = d / sblock.fs_frag; 798 setblock(&sblock, cg_blksfree(&acg), blkno); 799 if (sblock.fs_contigsumsize > 0) 800 setbit(cg_clustersfree(&acg), blkno); 801 acg.cg_cs.cs_nbfree++; 802 cg_blktot(&acg)[cbtocylno(&sblock, d)]++; 803 cg_blks(&sblock, &acg, cbtocylno(&sblock, d)) 804 [cbtorpos(&sblock, d)]++; 805 d += sblock.fs_frag; 806 } 807 if (d < dmax - cbase) { 808 acg.cg_frsum[dmax - cbase - d]++; 809 for (; d < dmax - cbase; d++) { 810 setbit(cg_blksfree(&acg), d); 811 acg.cg_cs.cs_nffree++; 812 } 813 } 814 if (sblock.fs_contigsumsize > 0) { 815 int32_t *sump = cg_clustersum(&acg); 816 u_char *mapp = cg_clustersfree(&acg); 817 int map = *mapp++; 818 int bit = 1; 819 int run = 0; 820 821 for (i = 0; i < acg.cg_nclusterblks; i++) { 822 if ((map & bit) != 0) 823 run++; 824 else if (run != 0) { 825 if (run > sblock.fs_contigsumsize) 826 run = sblock.fs_contigsumsize; 827 sump[run]++; 828 run = 0; 829 } 830 if ((i & (NBBY - 1)) != NBBY - 1) 831 bit <<= 1; 832 else { 833 map = *mapp++; 834 bit = 1; 835 } 836 } 837 if (run != 0) { 838 if (run > sblock.fs_contigsumsize) 839 run = sblock.fs_contigsumsize; 840 sump[run]++; 841 } 842 } 843 sblock.fs_cstotal.cs_ndir += acg.cg_cs.cs_ndir; 844 sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree; 845 sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree; 846 sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree; 847 *cs = acg.cg_cs; 848 wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)), 849 sblock.fs_bsize, (char *)&acg); 850 } 851 852 /* 853 * initialize the file system 854 */ 855 struct dinode node; 856 857 #ifdef LOSTDIR 858 #define PREDEFDIR 3 859 #else 860 #define PREDEFDIR 2 861 #endif 862 863 struct direct root_dir[] = { 864 { ROOTINO, sizeof(struct direct), DT_DIR, 1, "." }, 865 { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." }, 866 #ifdef LOSTDIR 867 { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 10, "lost+found" }, 868 #endif 869 }; 870 struct odirect { 871 u_long d_ino; 872 u_short d_reclen; 873 u_short d_namlen; 874 u_char d_name[MAXNAMLEN + 1]; 875 } oroot_dir[] = { 876 { ROOTINO, sizeof(struct direct), 1, "." }, 877 { ROOTINO, sizeof(struct direct), 2, ".." }, 878 #ifdef LOSTDIR 879 { LOSTFOUNDINO, sizeof(struct direct), 10, "lost+found" }, 880 #endif 881 }; 882 #ifdef LOSTDIR 883 struct direct lost_found_dir[] = { 884 { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 1, "." }, 885 { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." }, 886 { 0, DIRBLKSIZ, 0, 0, 0 }, 887 }; 888 struct odirect olost_found_dir[] = { 889 { LOSTFOUNDINO, sizeof(struct direct), 1, "." }, 890 { ROOTINO, sizeof(struct direct), 2, ".." }, 891 { 0, DIRBLKSIZ, 0, 0 }, 892 }; 893 #endif 894 char buf[MAXBSIZE]; 895 896 void 897 fsinit(utime) 898 time_t utime; 899 { 900 #ifdef LOSTDIR 901 int i; 902 #endif 903 904 /* 905 * initialize the node 906 */ 907 node.di_atime = utime; 908 node.di_mtime = utime; 909 node.di_ctime = utime; 910 #ifdef LOSTDIR 911 /* 912 * create the lost+found directory 913 */ 914 if (Oflag) { 915 (void)makedir((struct direct *)olost_found_dir, 2); 916 for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ) 917 memmove(&buf[i], &olost_found_dir[2], 918 DIRSIZ(0, &olost_found_dir[2])); 919 } else { 920 (void)makedir(lost_found_dir, 2); 921 for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ) 922 memmove(&buf[i], &lost_found_dir[2], 923 DIRSIZ(0, &lost_found_dir[2])); 924 } 925 node.di_mode = IFDIR | UMASK; 926 node.di_nlink = 2; 927 node.di_size = sblock.fs_bsize; 928 node.di_db[0] = alloc(node.di_size, node.di_mode); 929 node.di_blocks = btodb(fragroundup(&sblock, node.di_size)); 930 wtfs(fsbtodb(&sblock, node.di_db[0]), node.di_size, buf); 931 iput(&node, LOSTFOUNDINO); 932 #endif 933 /* 934 * create the root directory 935 */ 936 node.di_mode = IFDIR | UMASK; 937 node.di_nlink = PREDEFDIR; 938 if (Oflag) 939 node.di_size = makedir((struct direct *)oroot_dir, PREDEFDIR); 940 else 941 node.di_size = makedir(root_dir, PREDEFDIR); 942 node.di_db[0] = alloc(sblock.fs_fsize, node.di_mode); 943 node.di_blocks = btodb(fragroundup(&sblock, node.di_size)); 944 wtfs(fsbtodb(&sblock, node.di_db[0]), sblock.fs_fsize, buf); 945 iput(&node, ROOTINO); 946 } 947 948 /* 949 * construct a set of directory entries in "buf". 950 * return size of directory. 951 */ 952 int 953 makedir(protodir, entries) 954 register struct direct *protodir; 955 int entries; 956 { 957 char *cp; 958 int i, spcleft; 959 960 spcleft = DIRBLKSIZ; 961 for (cp = buf, i = 0; i < entries - 1; i++) { 962 protodir[i].d_reclen = DIRSIZ(0, &protodir[i]); 963 memmove(cp, &protodir[i], protodir[i].d_reclen); 964 cp += protodir[i].d_reclen; 965 spcleft -= protodir[i].d_reclen; 966 } 967 protodir[i].d_reclen = spcleft; 968 memmove(cp, &protodir[i], DIRSIZ(0, &protodir[i])); 969 return (DIRBLKSIZ); 970 } 971 972 /* 973 * allocate a block or frag 974 */ 975 daddr_t 976 alloc(size, mode) 977 int size; 978 int mode; 979 { 980 int i, frag; 981 daddr_t d, blkno; 982 983 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, 984 (char *)&acg); 985 if (acg.cg_magic != CG_MAGIC) { 986 printf("cg 0: bad magic number\n"); 987 return (0); 988 } 989 if (acg.cg_cs.cs_nbfree == 0) { 990 printf("first cylinder group ran out of space\n"); 991 return (0); 992 } 993 for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag) 994 if (isblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag)) 995 goto goth; 996 printf("internal error: can't find block in cyl 0\n"); 997 return (0); 998 goth: 999 blkno = fragstoblks(&sblock, d); 1000 clrblock(&sblock, cg_blksfree(&acg), blkno); 1001 if (sblock.fs_contigsumsize > 0) 1002 clrbit(cg_clustersfree(&acg), blkno); 1003 acg.cg_cs.cs_nbfree--; 1004 sblock.fs_cstotal.cs_nbfree--; 1005 fscs[0].cs_nbfree--; 1006 if (mode & IFDIR) { 1007 acg.cg_cs.cs_ndir++; 1008 sblock.fs_cstotal.cs_ndir++; 1009 fscs[0].cs_ndir++; 1010 } 1011 cg_blktot(&acg)[cbtocylno(&sblock, d)]--; 1012 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))[cbtorpos(&sblock, d)]--; 1013 if (size != sblock.fs_bsize) { 1014 frag = howmany(size, sblock.fs_fsize); 1015 fscs[0].cs_nffree += sblock.fs_frag - frag; 1016 sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag; 1017 acg.cg_cs.cs_nffree += sblock.fs_frag - frag; 1018 acg.cg_frsum[sblock.fs_frag - frag]++; 1019 for (i = frag; i < sblock.fs_frag; i++) 1020 setbit(cg_blksfree(&acg), d + i); 1021 } 1022 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, 1023 (char *)&acg); 1024 return (d); 1025 } 1026 1027 /* 1028 * Calculate number of inodes per group. 1029 */ 1030 long 1031 calcipg(cpg, bpcg, usedbp) 1032 long cpg; 1033 long bpcg; 1034 off_t *usedbp; 1035 { 1036 int i; 1037 long ipg, new_ipg, ncg, ncyl; 1038 off_t usedb; 1039 1040 /* 1041 * Prepare to scale by fssize / (number of sectors in cylinder groups). 1042 * Note that fssize is still in sectors, not filesystem blocks. 1043 */ 1044 ncyl = howmany(fssize, (u_int)secpercyl); 1045 ncg = howmany(ncyl, cpg); 1046 /* 1047 * Iterate a few times to allow for ipg depending on itself. 1048 */ 1049 ipg = 0; 1050 for (i = 0; i < 10; i++) { 1051 usedb = (sblock.fs_iblkno + ipg / INOPF(&sblock)) * 1052 NSPF(&sblock) * (off_t)sectorsize; 1053 new_ipg = (cpg * (quad_t)bpcg - usedb) / density * 1054 fssize / ncg / secpercyl / cpg; 1055 new_ipg = roundup(new_ipg, INOPB(&sblock)); 1056 if (new_ipg == ipg) 1057 break; 1058 ipg = new_ipg; 1059 } 1060 *usedbp = usedb; 1061 return (ipg); 1062 } 1063 1064 /* 1065 * Allocate an inode on the disk 1066 */ 1067 void 1068 iput(ip, ino) 1069 register struct dinode *ip; 1070 register ino_t ino; 1071 { 1072 struct dinode buf[MAXINOPB]; 1073 daddr_t d; 1074 int c; 1075 1076 #ifdef FSIRAND 1077 ip->di_gen = random(); 1078 #endif 1079 c = ino_to_cg(&sblock, ino); 1080 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, 1081 (char *)&acg); 1082 if (acg.cg_magic != CG_MAGIC) { 1083 printf("cg 0: bad magic number\n"); 1084 exit(31); 1085 } 1086 acg.cg_cs.cs_nifree--; 1087 setbit(cg_inosused(&acg), ino); 1088 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, 1089 (char *)&acg); 1090 sblock.fs_cstotal.cs_nifree--; 1091 fscs[0].cs_nifree--; 1092 if (ino >= sblock.fs_ipg * sblock.fs_ncg) { 1093 printf("fsinit: inode value out of range (%d).\n", ino); 1094 exit(32); 1095 } 1096 d = fsbtodb(&sblock, ino_to_fsba(&sblock, ino)); 1097 rdfs(d, sblock.fs_bsize, (char *)buf); 1098 buf[ino_to_fsbo(&sblock, ino)] = *ip; 1099 wtfs(d, sblock.fs_bsize, (char *)buf); 1100 } 1101 1102 #ifdef STANDALONE 1103 /* 1104 * Replace libc function with one suited to our needs. 1105 */ 1106 caddr_t 1107 malloc(size) 1108 register u_long size; 1109 { 1110 char *base, *i; 1111 static u_long pgsz; 1112 struct rlimit rlp; 1113 1114 if (pgsz == 0) { 1115 base = sbrk(0); 1116 pgsz = getpagesize() - 1; 1117 i = (char *)((u_long)(base + pgsz) & ~pgsz); 1118 base = sbrk(i - base); 1119 if (getrlimit(RLIMIT_DATA, &rlp) < 0) 1120 warn("getrlimit"); 1121 rlp.rlim_cur = rlp.rlim_max; 1122 if (setrlimit(RLIMIT_DATA, &rlp) < 0) 1123 warn("setrlimit"); 1124 memleft = rlp.rlim_max - (u_long)base; 1125 } 1126 size = (size + pgsz) & ~pgsz; 1127 if (size > memleft) 1128 size = memleft; 1129 memleft -= size; 1130 if (size == 0) 1131 return (0); 1132 return ((caddr_t)sbrk(size)); 1133 } 1134 1135 /* 1136 * Replace libc function with one suited to our needs. 1137 */ 1138 caddr_t 1139 realloc(ptr, size) 1140 char *ptr; 1141 u_long size; 1142 { 1143 void *p; 1144 1145 if ((p = malloc(size)) == NULL) 1146 return (NULL); 1147 memmove(p, ptr, size); 1148 free(ptr); 1149 return (p); 1150 } 1151 1152 /* 1153 * Replace libc function with one suited to our needs. 1154 */ 1155 char * 1156 calloc(size, numelm) 1157 u_long size, numelm; 1158 { 1159 caddr_t base; 1160 1161 size *= numelm; 1162 if ((base = malloc(size)) == NULL) 1163 return (NULL); 1164 memset(base, 0, size); 1165 return (base); 1166 } 1167 1168 /* 1169 * Replace libc function with one suited to our needs. 1170 */ 1171 void 1172 free(ptr) 1173 char *ptr; 1174 { 1175 1176 /* do not worry about it for now */ 1177 } 1178 1179 #else /* !STANDALONE */ 1180 1181 #ifdef __ELF__ 1182 extern char *_etext; 1183 #define etext _etext 1184 #else 1185 extern char *etext; 1186 #endif 1187 1188 #endif /* STANDALONE */ 1189 1190 /* 1191 * read a block from the file system 1192 */ 1193 void 1194 rdfs(bno, size, bf) 1195 daddr_t bno; 1196 int size; 1197 char *bf; 1198 { 1199 int n; 1200 1201 wtfsflush(); 1202 if (lseek(fsi, (off_t)bno * sectorsize, 0) < 0) { 1203 printf("seek error: %ld\n", (long)bno); 1204 err(33, "rdfs"); 1205 } 1206 n = read(fsi, bf, size); 1207 if (n != size) { 1208 printf("read error: %ld\n", (long)bno); 1209 err(34, "rdfs"); 1210 } 1211 } 1212 1213 #define WCSIZE (128 * 1024) 1214 daddr_t wc_sect; /* units of sectorsize */ 1215 int wc_end; /* bytes */ 1216 static char wc[WCSIZE]; /* bytes */ 1217 1218 /* 1219 * Flush dirty write behind buffer. 1220 */ 1221 void 1222 wtfsflush() 1223 { 1224 int n; 1225 if (wc_end) { 1226 if (lseek(fso, (off_t)wc_sect * sectorsize, SEEK_SET) < 0) { 1227 printf("seek error: %ld\n", (long)wc_sect); 1228 err(35, "wtfs - writecombine"); 1229 } 1230 n = write(fso, wc, wc_end); 1231 if (n != wc_end) { 1232 printf("write error: %ld\n", (long)wc_sect); 1233 err(36, "wtfs - writecombine"); 1234 } 1235 wc_end = 0; 1236 } 1237 } 1238 1239 /* 1240 * write a block to the file system 1241 */ 1242 void 1243 wtfs(bno, size, bf) 1244 daddr_t bno; 1245 int size; 1246 char *bf; 1247 { 1248 int n; 1249 int done; 1250 1251 if (Nflag) 1252 return; 1253 done = 0; 1254 if (wc_end == 0 && size <= WCSIZE) { 1255 wc_sect = bno; 1256 bcopy(bf, wc, size); 1257 wc_end = size; 1258 if (wc_end < WCSIZE) 1259 return; 1260 done = 1; 1261 } 1262 if ((off_t)wc_sect * sectorsize + wc_end == (off_t)bno * sectorsize && 1263 wc_end + size <= WCSIZE) { 1264 bcopy(bf, wc + wc_end, size); 1265 wc_end += size; 1266 if (wc_end < WCSIZE) 1267 return; 1268 done = 1; 1269 } 1270 wtfsflush(); 1271 if (done) 1272 return; 1273 if (lseek(fso, (off_t)bno * sectorsize, SEEK_SET) < 0) { 1274 printf("seek error: %ld\n", (long)bno); 1275 err(35, "wtfs"); 1276 } 1277 n = write(fso, bf, size); 1278 if (n != size) { 1279 printf("write error: %ld\n", (long)bno); 1280 err(36, "wtfs"); 1281 } 1282 } 1283 1284 /* 1285 * check if a block is available 1286 */ 1287 int 1288 isblock(fs, cp, h) 1289 struct fs *fs; 1290 unsigned char *cp; 1291 int h; 1292 { 1293 unsigned char mask; 1294 1295 switch (fs->fs_frag) { 1296 case 8: 1297 return (cp[h] == 0xff); 1298 case 4: 1299 mask = 0x0f << ((h & 0x1) << 2); 1300 return ((cp[h >> 1] & mask) == mask); 1301 case 2: 1302 mask = 0x03 << ((h & 0x3) << 1); 1303 return ((cp[h >> 2] & mask) == mask); 1304 case 1: 1305 mask = 0x01 << (h & 0x7); 1306 return ((cp[h >> 3] & mask) == mask); 1307 default: 1308 #ifdef STANDALONE 1309 printf("isblock bad fs_frag %d\n", fs->fs_frag); 1310 #else 1311 fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag); 1312 #endif 1313 return (0); 1314 } 1315 } 1316 1317 /* 1318 * take a block out of the map 1319 */ 1320 void 1321 clrblock(fs, cp, h) 1322 struct fs *fs; 1323 unsigned char *cp; 1324 int h; 1325 { 1326 switch ((fs)->fs_frag) { 1327 case 8: 1328 cp[h] = 0; 1329 return; 1330 case 4: 1331 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2)); 1332 return; 1333 case 2: 1334 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1)); 1335 return; 1336 case 1: 1337 cp[h >> 3] &= ~(0x01 << (h & 0x7)); 1338 return; 1339 default: 1340 #ifdef STANDALONE 1341 printf("clrblock bad fs_frag %d\n", fs->fs_frag); 1342 #else 1343 fprintf(stderr, "clrblock bad fs_frag %d\n", fs->fs_frag); 1344 #endif 1345 return; 1346 } 1347 } 1348 1349 /* 1350 * put a block into the map 1351 */ 1352 void 1353 setblock(fs, cp, h) 1354 struct fs *fs; 1355 unsigned char *cp; 1356 int h; 1357 { 1358 switch (fs->fs_frag) { 1359 case 8: 1360 cp[h] = 0xff; 1361 return; 1362 case 4: 1363 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2)); 1364 return; 1365 case 2: 1366 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1)); 1367 return; 1368 case 1: 1369 cp[h >> 3] |= (0x01 << (h & 0x7)); 1370 return; 1371 default: 1372 #ifdef STANDALONE 1373 printf("setblock bad fs_frag %d\n", fs->fs_frag); 1374 #else 1375 fprintf(stderr, "setblock bad fs_frag %d\n", fs->fs_frag); 1376 #endif 1377 return; 1378 } 1379 } 1380 1381 /* 1382 * Determine the number of characters in a 1383 * single line. 1384 */ 1385 1386 static int 1387 charsperline() 1388 { 1389 int columns; 1390 char *cp; 1391 struct winsize ws; 1392 1393 columns = 0; 1394 if (ioctl(0, TIOCGWINSZ, &ws) != -1) 1395 columns = ws.ws_col; 1396 if (columns == 0 && (cp = getenv("COLUMNS"))) 1397 columns = atoi(cp); 1398 if (columns == 0) 1399 columns = 80; /* last resort */ 1400 return (columns); 1401 } 1402