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