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