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