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