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