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