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