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