mkfs.c revision 92712
11558Srgrimes/*
21558Srgrimes * Copyright (c) 1980, 1989, 1993
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes *
51558Srgrimes * Redistribution and use in source and binary forms, with or without
61558Srgrimes * modification, are permitted provided that the following conditions
71558Srgrimes * are met:
81558Srgrimes * 1. Redistributions of source code must retain the above copyright
91558Srgrimes *    notice, this list of conditions and the following disclaimer.
101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111558Srgrimes *    notice, this list of conditions and the following disclaimer in the
121558Srgrimes *    documentation and/or other materials provided with the distribution.
131558Srgrimes * 3. All advertising materials mentioning features or use of this software
141558Srgrimes *    must display the following acknowledgement:
151558Srgrimes *	This product includes software developed by the University of
161558Srgrimes *	California, Berkeley and its contributors.
171558Srgrimes * 4. Neither the name of the University nor the names of its contributors
181558Srgrimes *    may be used to endorse or promote products derived from this software
191558Srgrimes *    without specific prior written permission.
201558Srgrimes *
211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311558Srgrimes * SUCH DAMAGE.
321558Srgrimes */
331558Srgrimes
341558Srgrimes#ifndef lint
3537664Scharnier#if 0
3623682Speterstatic char sccsid[] = "@(#)mkfs.c	8.11 (Berkeley) 5/3/95";
3737664Scharnier#endif
3837664Scharnierstatic const char rcsid[] =
3950476Speter  "$FreeBSD: head/sbin/newfs/mkfs.c 92712 2002-03-19 17:39:01Z iedowse $";
401558Srgrimes#endif /* not lint */
411558Srgrimes
4237664Scharnier#include <err.h>
4337664Scharnier#include <signal.h>
4492709Siedowse#include <stdlib.h>
4537664Scharnier#include <string.h>
4637664Scharnier#include <stdio.h>
471558Srgrimes#include <unistd.h>
481558Srgrimes#include <sys/param.h>
491558Srgrimes#include <sys/time.h>
5037664Scharnier#include <sys/types.h>
511558Srgrimes#include <sys/wait.h>
521558Srgrimes#include <sys/resource.h>
5343804Sdillon#include <sys/stat.h>
541558Srgrimes#include <ufs/ufs/dinode.h>
551558Srgrimes#include <ufs/ufs/dir.h>
561558Srgrimes#include <ufs/ffs/fs.h>
571558Srgrimes#include <sys/disklabel.h>
583550Sphk#include <sys/file.h>
593550Sphk#include <sys/mman.h>
6013637Sjoerg#include <sys/ioctl.h>
611558Srgrimes
621558Srgrimes/*
631558Srgrimes * make file system for cylinder-group style file systems
641558Srgrimes */
651558Srgrimes
661558Srgrimes/*
671558Srgrimes * We limit the size of the inode map to be no more than a
681558Srgrimes * third of the cylinder group space, since we must leave at
691558Srgrimes * least an equal amount of space for the block map.
701558Srgrimes *
711558Srgrimes * N.B.: MAXIPG must be a multiple of INOPB(fs).
721558Srgrimes */
731558Srgrimes#define MAXIPG(fs)	roundup((fs)->fs_bsize * NBBY / 3, INOPB(fs))
741558Srgrimes
751558Srgrimes#define UMASK		0755
761558Srgrimes#define MAXINOPB	(MAXBSIZE / sizeof(struct dinode))
771558Srgrimes#define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
781558Srgrimes
791558Srgrimes/*
801558Srgrimes * variables set up by front end.
811558Srgrimes */
821558Srgrimesextern int	Nflag;		/* run mkfs without writing file system */
831558Srgrimesextern int	Oflag;		/* format as an 4.3BSD file system */
8475078Sobrienextern int	Uflag;		/* enable soft updates for file system */
851558Srgrimesextern int	fssize;		/* file system size */
861558Srgrimesextern int	ntracks;	/* # tracks/cylinder */
871558Srgrimesextern int	nsectors;	/* # sectors/track */
881558Srgrimesextern int	nphyssectors;	/* # sectors/track including spares */
891558Srgrimesextern int	secpercyl;	/* sectors per cylinder */
901558Srgrimesextern int	sectorsize;	/* bytes/sector */
9120061Ssosextern int	realsectorsize;	/* bytes/sector in hardware*/
921558Srgrimesextern int	rpm;		/* revolutions/minute of drive */
931558Srgrimesextern int	interleave;	/* hardware sector interleave */
941558Srgrimesextern int	trackskew;	/* sector 0 skew, per track */
951558Srgrimesextern int	fsize;		/* fragment size */
961558Srgrimesextern int	bsize;		/* block size */
971558Srgrimesextern int	cpg;		/* cylinders/cylinder group */
981558Srgrimesextern int	cpgflg;		/* cylinders/cylinder group flag was given */
991558Srgrimesextern int	minfree;	/* free space threshold */
1001558Srgrimesextern int	opt;		/* optimization preference (space or time) */
1011558Srgrimesextern int	density;	/* number of bytes per inode */
1021558Srgrimesextern int	maxcontig;	/* max contiguous blocks to allocate */
1031558Srgrimesextern int	rotdelay;	/* rotational delay between blocks */
1041558Srgrimesextern int	maxbpg;		/* maximum blocks per file in a cyl group */
1051558Srgrimesextern int	nrpos;		/* # of distinguished rotational positions */
1061558Srgrimesextern int	bbsize;		/* boot block size */
1071558Srgrimesextern int	sbsize;		/* superblock size */
10875377Smckusickextern int	avgfilesize;	/* expected average file size */
10975377Smckusickextern int	avgfilesperdir;	/* expected number of files per directory */
1101558Srgrimes
1111558Srgrimesunion {
1121558Srgrimes	struct fs fs;
1131558Srgrimes	char pad[SBSIZE];
1141558Srgrimes} fsun;
1151558Srgrimes#define	sblock	fsun.fs
1161558Srgrimesstruct	csum *fscs;
1171558Srgrimes
1181558Srgrimesunion {
1191558Srgrimes	struct cg cg;
1201558Srgrimes	char pad[MAXBSIZE];
1211558Srgrimes} cgun;
1221558Srgrimes#define	acg	cgun.cg
1231558Srgrimes
1241558Srgrimesstruct dinode zino[MAXBSIZE / sizeof(struct dinode)];
1251558Srgrimes
12624215Sacheint	fsi, fso;
12724215Sacheint     randinit;
1281558Srgrimesdaddr_t	alloc();
12927372Sbdelong	calcipg();
13013769Sjoergstatic int charsperline();
13192483Sphkvoid clrblock (struct fs *, unsigned char *, int);
13292483Sphkvoid fsinit (time_t);
13392712Siedowsestatic int ilog2(int);
13492483Sphkvoid initcg (int, time_t);
13592483Sphkint isblock (struct fs *, unsigned char *, int);
13692483Sphkvoid iput (struct dinode *, ino_t);
13792483Sphkint makedir (struct direct *, int);
13892483Sphkvoid rdfs (daddr_t, int, char *);
13992483Sphkvoid setblock (struct fs *, unsigned char *, int);
14092483Sphkvoid wtfs (daddr_t, int, char *);
14192483Sphkvoid wtfsflush (void);
1421558Srgrimes
14337664Scharniervoid
14492711Siedowsemkfs(struct partition *pp, char *fsys, int fi, int fo)
1451558Srgrimes{
14692483Sphk	long i, mincpc, mincpg, inospercg;
1471558Srgrimes	long cylno, rpos, blk, j, warn = 0;
1481558Srgrimes	long used, mincpgcnt, bpcg;
14923682Speter	off_t usedb;
1501558Srgrimes	long mapcramped, inodecramped;
1511558Srgrimes	long postblsize, rotblsize, totalsbsize;
1521558Srgrimes	time_t utime;
1531558Srgrimes	quad_t sizepb;
15413769Sjoerg	int width;
15513769Sjoerg	char tmpbuf[100];	/* XXX this will break in about 2,500 years */
1561558Srgrimes
1571558Srgrimes	time(&utime);
15824215Sache	if (!randinit) {
15924215Sache		randinit = 1;
16026625Sache		srandomdev();
16124215Sache	}
1621558Srgrimes	fsi = fi;
1631558Srgrimes	fso = fo;
1641558Srgrimes	if (Oflag) {
1651558Srgrimes		sblock.fs_inodefmt = FS_42INODEFMT;
1661558Srgrimes		sblock.fs_maxsymlinklen = 0;
1671558Srgrimes	} else {
1681558Srgrimes		sblock.fs_inodefmt = FS_44INODEFMT;
1691558Srgrimes		sblock.fs_maxsymlinklen = MAXSYMLINKLEN;
1701558Srgrimes	}
17175124Sbde	if (Uflag)
17275078Sobrien		sblock.fs_flags |= FS_DOSOFTDEP;
1731558Srgrimes	/*
1741558Srgrimes	 * Validate the given file system size.
1751558Srgrimes	 * Verify that its last block can actually be accessed.
1761558Srgrimes	 */
1771558Srgrimes	if (fssize <= 0)
1781558Srgrimes		printf("preposterous size %d\n", fssize), exit(13);
17920061Ssos	wtfs(fssize - (realsectorsize / DEV_BSIZE), realsectorsize,
18092532Sbde	    (char *)&sblock);
1811558Srgrimes	/*
1821558Srgrimes	 * collect and verify the sector and track info
1831558Srgrimes	 */
1841558Srgrimes	sblock.fs_nsect = nsectors;
1851558Srgrimes	sblock.fs_ntrak = ntracks;
1861558Srgrimes	if (sblock.fs_ntrak <= 0)
1871558Srgrimes		printf("preposterous ntrak %d\n", sblock.fs_ntrak), exit(14);
1881558Srgrimes	if (sblock.fs_nsect <= 0)
1891558Srgrimes		printf("preposterous nsect %d\n", sblock.fs_nsect), exit(15);
1901558Srgrimes	/*
19175377Smckusick	 * collect and verify the filesystem density info
19275377Smckusick	 */
19375377Smckusick	sblock.fs_avgfilesize = avgfilesize;
19475377Smckusick	sblock.fs_avgfpdir = avgfilesperdir;
19575377Smckusick	if (sblock.fs_avgfilesize <= 0)
19675377Smckusick		printf("illegal expected average file size %d\n",
19775377Smckusick		    sblock.fs_avgfilesize), exit(14);
19875377Smckusick	if (sblock.fs_avgfpdir <= 0)
19975377Smckusick		printf("illegal expected number of files per directory %d\n",
20075377Smckusick		    sblock.fs_avgfpdir), exit(15);
20175377Smckusick	/*
2021558Srgrimes	 * collect and verify the block and fragment sizes
2031558Srgrimes	 */
2041558Srgrimes	sblock.fs_bsize = bsize;
2051558Srgrimes	sblock.fs_fsize = fsize;
2061558Srgrimes	if (!POWEROF2(sblock.fs_bsize)) {
2071558Srgrimes		printf("block size must be a power of 2, not %d\n",
2081558Srgrimes		    sblock.fs_bsize);
2091558Srgrimes		exit(16);
2101558Srgrimes	}
2111558Srgrimes	if (!POWEROF2(sblock.fs_fsize)) {
2121558Srgrimes		printf("fragment size must be a power of 2, not %d\n",
2131558Srgrimes		    sblock.fs_fsize);
2141558Srgrimes		exit(17);
2151558Srgrimes	}
2161558Srgrimes	if (sblock.fs_fsize < sectorsize) {
2171558Srgrimes		printf("fragment size %d is too small, minimum is %d\n",
2181558Srgrimes		    sblock.fs_fsize, sectorsize);
2191558Srgrimes		exit(18);
2201558Srgrimes	}
2211558Srgrimes	if (sblock.fs_bsize < MINBSIZE) {
2221558Srgrimes		printf("block size %d is too small, minimum is %d\n",
2231558Srgrimes		    sblock.fs_bsize, MINBSIZE);
2241558Srgrimes		exit(19);
2251558Srgrimes	}
2261558Srgrimes	if (sblock.fs_bsize < sblock.fs_fsize) {
22792532Sbde		printf(
22892532Sbde		"block size (%d) cannot be smaller than fragment size (%d)\n",
22992532Sbde		    sblock.fs_bsize, sblock.fs_fsize);
2301558Srgrimes		exit(20);
2311558Srgrimes	}
2321558Srgrimes	sblock.fs_bmask = ~(sblock.fs_bsize - 1);
2331558Srgrimes	sblock.fs_fmask = ~(sblock.fs_fsize - 1);
2341558Srgrimes	sblock.fs_qbmask = ~sblock.fs_bmask;
2351558Srgrimes	sblock.fs_qfmask = ~sblock.fs_fmask;
23692712Siedowse	sblock.fs_bshift = ilog2(sblock.fs_bsize);
23792712Siedowse	sblock.fs_fshift = ilog2(sblock.fs_fsize);
2381558Srgrimes	sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize);
23992712Siedowse	sblock.fs_fragshift = ilog2(sblock.fs_frag);
2401558Srgrimes	if (sblock.fs_frag > MAXFRAG) {
24192532Sbde		printf(
24292532Sbde	"fragment size %d is too small, minimum with block size %d is %d\n",
24392532Sbde		    sblock.fs_fsize, sblock.fs_bsize,
2441558Srgrimes		    sblock.fs_bsize / MAXFRAG);
2451558Srgrimes		exit(21);
2461558Srgrimes	}
2471558Srgrimes	sblock.fs_nrpos = nrpos;
2481558Srgrimes	sblock.fs_nindir = sblock.fs_bsize / sizeof(daddr_t);
2491558Srgrimes	sblock.fs_inopb = sblock.fs_bsize / sizeof(struct dinode);
2501558Srgrimes	sblock.fs_nspf = sblock.fs_fsize / sectorsize;
25192712Siedowse	sblock.fs_fsbtodb = ilog2(NSPF(&sblock));
2521558Srgrimes	sblock.fs_sblkno =
2531558Srgrimes	    roundup(howmany(bbsize + sbsize, sblock.fs_fsize), sblock.fs_frag);
2541558Srgrimes	sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno +
2551558Srgrimes	    roundup(howmany(sbsize, sblock.fs_fsize), sblock.fs_frag));
2561558Srgrimes	sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag;
25785860Sphk	sblock.fs_cgoffset =
25885860Sphk	    roundup(howmany(sblock.fs_nsect, NSPF(&sblock)), sblock.fs_frag);
2591558Srgrimes	for (sblock.fs_cgmask = 0xffffffff, i = sblock.fs_ntrak; i > 1; i >>= 1)
2601558Srgrimes		sblock.fs_cgmask <<= 1;
2611558Srgrimes	if (!POWEROF2(sblock.fs_ntrak))
2621558Srgrimes		sblock.fs_cgmask <<= 1;
2631558Srgrimes	sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
2641558Srgrimes	for (sizepb = sblock.fs_bsize, i = 0; i < NIADDR; i++) {
2651558Srgrimes		sizepb *= NINDIR(&sblock);
2661558Srgrimes		sblock.fs_maxfilesize += sizepb;
2671558Srgrimes	}
2681558Srgrimes	/*
2691558Srgrimes	 * Validate specified/determined secpercyl
2701558Srgrimes	 * and calculate minimum cylinders per group.
2711558Srgrimes	 */
2721558Srgrimes	sblock.fs_spc = secpercyl;
2731558Srgrimes	for (sblock.fs_cpc = NSPB(&sblock), i = sblock.fs_spc;
2741558Srgrimes	     sblock.fs_cpc > 1 && (i & 1) == 0;
2751558Srgrimes	     sblock.fs_cpc >>= 1, i >>= 1)
2761558Srgrimes		/* void */;
2771558Srgrimes	mincpc = sblock.fs_cpc;
2781558Srgrimes	bpcg = sblock.fs_spc * sectorsize;
2791558Srgrimes	inospercg = roundup(bpcg / sizeof(struct dinode), INOPB(&sblock));
2801558Srgrimes	if (inospercg > MAXIPG(&sblock))
2811558Srgrimes		inospercg = MAXIPG(&sblock);
2821558Srgrimes	used = (sblock.fs_iblkno + inospercg / INOPF(&sblock)) * NSPF(&sblock);
2831558Srgrimes	mincpgcnt = howmany(sblock.fs_cgoffset * (~sblock.fs_cgmask) + used,
2841558Srgrimes	    sblock.fs_spc);
2851558Srgrimes	mincpg = roundup(mincpgcnt, mincpc);
2861558Srgrimes	/*
2871558Srgrimes	 * Ensure that cylinder group with mincpg has enough space
2881558Srgrimes	 * for block maps.
2891558Srgrimes	 */
2901558Srgrimes	sblock.fs_cpg = mincpg;
2911558Srgrimes	sblock.fs_ipg = inospercg;
2921558Srgrimes	if (maxcontig > 1)
2931558Srgrimes		sblock.fs_contigsumsize = MIN(maxcontig, FS_MAXCONTIG);
2941558Srgrimes	mapcramped = 0;
2951558Srgrimes	while (CGSIZE(&sblock) > sblock.fs_bsize) {
2961558Srgrimes		mapcramped = 1;
2971558Srgrimes		if (sblock.fs_bsize < MAXBSIZE) {
2981558Srgrimes			sblock.fs_bsize <<= 1;
29985860Sphk			if ((i & 1) == 0)
3001558Srgrimes				i >>= 1;
30185860Sphk			else {
3021558Srgrimes				sblock.fs_cpc <<= 1;
3031558Srgrimes				mincpc <<= 1;
3041558Srgrimes				mincpg = roundup(mincpgcnt, mincpc);
3051558Srgrimes				sblock.fs_cpg = mincpg;
3061558Srgrimes			}
3071558Srgrimes			sblock.fs_frag <<= 1;
3081558Srgrimes			sblock.fs_fragshift += 1;
3091558Srgrimes			if (sblock.fs_frag <= MAXFRAG)
3101558Srgrimes				continue;
3111558Srgrimes		}
3121558Srgrimes		if (sblock.fs_fsize == sblock.fs_bsize) {
3131558Srgrimes			printf("There is no block size that");
3141558Srgrimes			printf(" can support this disk\n");
3151558Srgrimes			exit(22);
3161558Srgrimes		}
3171558Srgrimes		sblock.fs_frag >>= 1;
3181558Srgrimes		sblock.fs_fragshift -= 1;
3191558Srgrimes		sblock.fs_fsize <<= 1;
3201558Srgrimes		sblock.fs_nspf <<= 1;
3211558Srgrimes	}
3221558Srgrimes	/*
3231558Srgrimes	 * Ensure that cylinder group with mincpg has enough space for inodes.
3241558Srgrimes	 */
3251558Srgrimes	inodecramped = 0;
32623682Speter	inospercg = calcipg(mincpg, bpcg, &usedb);
3271558Srgrimes	sblock.fs_ipg = inospercg;
3281558Srgrimes	while (inospercg > MAXIPG(&sblock)) {
3291558Srgrimes		inodecramped = 1;
3301558Srgrimes		if (mincpc == 1 || sblock.fs_frag == 1 ||
3311558Srgrimes		    sblock.fs_bsize == MINBSIZE)
3321558Srgrimes			break;
3331558Srgrimes		printf("With a block size of %d %s %d\n", sblock.fs_bsize,
33485860Sphk		    "minimum bytes per inode is",
33585860Sphk		    (int)((mincpg * (off_t)bpcg - usedb) /
33685860Sphk		    MAXIPG(&sblock) + 1));
3371558Srgrimes		sblock.fs_bsize >>= 1;
3381558Srgrimes		sblock.fs_frag >>= 1;
3391558Srgrimes		sblock.fs_fragshift -= 1;
3401558Srgrimes		mincpc >>= 1;
3411558Srgrimes		sblock.fs_cpg = roundup(mincpgcnt, mincpc);
3421558Srgrimes		if (CGSIZE(&sblock) > sblock.fs_bsize) {
3431558Srgrimes			sblock.fs_bsize <<= 1;
3441558Srgrimes			break;
3451558Srgrimes		}
3461558Srgrimes		mincpg = sblock.fs_cpg;
34723682Speter		inospercg = calcipg(mincpg, bpcg, &usedb);
3481558Srgrimes		sblock.fs_ipg = inospercg;
3491558Srgrimes	}
3501558Srgrimes	if (inodecramped) {
3511558Srgrimes		if (inospercg > MAXIPG(&sblock)) {
3521558Srgrimes			printf("Minimum bytes per inode is %d\n",
35385860Sphk			    (int)((mincpg * (off_t)bpcg - usedb) /
35485860Sphk			    MAXIPG(&sblock) + 1));
3551558Srgrimes		} else if (!mapcramped) {
3561558Srgrimes			printf("With %d bytes per inode, ", density);
35737239Sbde			printf("minimum cylinders per group is %ld\n", mincpg);
3581558Srgrimes		}
3591558Srgrimes	}
3601558Srgrimes	if (mapcramped) {
3611558Srgrimes		printf("With %d sectors per cylinder, ", sblock.fs_spc);
36237239Sbde		printf("minimum cylinders per group is %ld\n", mincpg);
3631558Srgrimes	}
3641558Srgrimes	if (inodecramped || mapcramped) {
3651558Srgrimes		if (sblock.fs_bsize != bsize)
3661558Srgrimes			printf("%s to be changed from %d to %d\n",
3671558Srgrimes			    "This requires the block size",
3681558Srgrimes			    bsize, sblock.fs_bsize);
3691558Srgrimes		if (sblock.fs_fsize != fsize)
3701558Srgrimes			printf("\t%s to be changed from %d to %d\n",
37185860Sphk			    "and the fragment size", fsize, sblock.fs_fsize);
3721558Srgrimes		exit(23);
3731558Srgrimes	}
3748871Srgrimes	/*
3751558Srgrimes	 * Calculate the number of cylinders per group
3761558Srgrimes	 */
3771558Srgrimes	sblock.fs_cpg = cpg;
3781558Srgrimes	if (sblock.fs_cpg % mincpc != 0) {
37937239Sbde		printf("%s groups must have a multiple of %ld cylinders\n",
38085860Sphk		    cpgflg ? "Cylinder" : "Warning: cylinder", mincpc);
3811558Srgrimes		sblock.fs_cpg = roundup(sblock.fs_cpg, mincpc);
3821558Srgrimes		if (!cpgflg)
3831558Srgrimes			cpg = sblock.fs_cpg;
3841558Srgrimes	}
3851558Srgrimes	/*
3861558Srgrimes	 * Must ensure there is enough space for inodes.
3871558Srgrimes	 */
38823682Speter	sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
3891558Srgrimes	while (sblock.fs_ipg > MAXIPG(&sblock)) {
3901558Srgrimes		inodecramped = 1;
3911558Srgrimes		sblock.fs_cpg -= mincpc;
39223682Speter		sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
3931558Srgrimes	}
3941558Srgrimes	/*
3951558Srgrimes	 * Must ensure there is enough space to hold block map.
3961558Srgrimes	 */
3971558Srgrimes	while (CGSIZE(&sblock) > sblock.fs_bsize) {
3981558Srgrimes		mapcramped = 1;
3991558Srgrimes		sblock.fs_cpg -= mincpc;
40023682Speter		sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
4011558Srgrimes	}
4021558Srgrimes	sblock.fs_fpg = (sblock.fs_cpg * sblock.fs_spc) / NSPF(&sblock);
4031558Srgrimes	if ((sblock.fs_cpg * sblock.fs_spc) % NSPB(&sblock) != 0) {
40437239Sbde		printf("panic (fs_cpg * fs_spc) %% NSPF != 0");
4051558Srgrimes		exit(24);
4061558Srgrimes	}
4071558Srgrimes	if (sblock.fs_cpg < mincpg) {
40837239Sbde		printf("cylinder groups must have at least %ld cylinders\n",
4091558Srgrimes			mincpg);
4101558Srgrimes		exit(25);
4111558Srgrimes	} else if (sblock.fs_cpg != cpg) {
4121558Srgrimes		if (!cpgflg)
4131558Srgrimes			printf("Warning: ");
4141558Srgrimes		else if (!mapcramped && !inodecramped)
4151558Srgrimes			exit(26);
4161558Srgrimes		if (mapcramped && inodecramped)
4171558Srgrimes			printf("Block size and bytes per inode restrict");
4181558Srgrimes		else if (mapcramped)
4191558Srgrimes			printf("Block size restricts");
4201558Srgrimes		else
4211558Srgrimes			printf("Bytes per inode restrict");
4221558Srgrimes		printf(" cylinders per group to %d.\n", sblock.fs_cpg);
4231558Srgrimes		if (cpgflg)
4241558Srgrimes			exit(27);
4251558Srgrimes	}
4261558Srgrimes	sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock));
4271558Srgrimes	/*
4281558Srgrimes	 * Now have size for file system and nsect and ntrak.
4291558Srgrimes	 * Determine number of cylinders and blocks in the file system.
4301558Srgrimes	 */
4311558Srgrimes	sblock.fs_size = fssize = dbtofsb(&sblock, fssize);
4321558Srgrimes	sblock.fs_ncyl = fssize * NSPF(&sblock) / sblock.fs_spc;
4331558Srgrimes	if (fssize * NSPF(&sblock) > sblock.fs_ncyl * sblock.fs_spc) {
4341558Srgrimes		sblock.fs_ncyl++;
4351558Srgrimes		warn = 1;
4361558Srgrimes	}
4371558Srgrimes	if (sblock.fs_ncyl < 1) {
4381558Srgrimes		printf("file systems must have at least one cylinder\n");
4391558Srgrimes		exit(28);
4401558Srgrimes	}
4411558Srgrimes	/*
4421558Srgrimes	 * Determine feasability/values of rotational layout tables.
4431558Srgrimes	 *
4441558Srgrimes	 * The size of the rotational layout tables is limited by the
4451558Srgrimes	 * size of the superblock, SBSIZE. The amount of space available
4461558Srgrimes	 * for tables is calculated as (SBSIZE - sizeof (struct fs)).
4471558Srgrimes	 * The size of these tables is inversely proportional to the block
4481558Srgrimes	 * size of the file system. The size increases if sectors per track
4491558Srgrimes	 * are not powers of two, because more cylinders must be described
4501558Srgrimes	 * by the tables before the rotational pattern repeats (fs_cpc).
4511558Srgrimes	 */
4521558Srgrimes	sblock.fs_interleave = interleave;
4531558Srgrimes	sblock.fs_trackskew = trackskew;
4541558Srgrimes	sblock.fs_npsect = nphyssectors;
4551558Srgrimes	sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
4561558Srgrimes	sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs));
45732623Sbde	if (sblock.fs_sbsize > SBSIZE)
45832623Sbde		sblock.fs_sbsize = SBSIZE;
4593467Sdg	if (sblock.fs_ntrak == 1) {
4601558Srgrimes		sblock.fs_cpc = 0;
4611558Srgrimes		goto next;
4621558Srgrimes	}
46338579Sdfr	postblsize = sblock.fs_nrpos * sblock.fs_cpc * sizeof(int16_t);
4641558Srgrimes	rotblsize = sblock.fs_cpc * sblock.fs_spc / NSPB(&sblock);
4651558Srgrimes	totalsbsize = sizeof(struct fs) + rotblsize;
4661558Srgrimes	if (sblock.fs_nrpos == 8 && sblock.fs_cpc <= 16) {
4671558Srgrimes		/* use old static table space */
4681558Srgrimes		sblock.fs_postbloff = (char *)(&sblock.fs_opostbl[0][0]) -
46923682Speter		    (char *)(&sblock.fs_firstfield);
4701558Srgrimes		sblock.fs_rotbloff = &sblock.fs_space[0] -
47123682Speter		    (u_char *)(&sblock.fs_firstfield);
4721558Srgrimes	} else {
4731558Srgrimes		/* use dynamic table space */
4741558Srgrimes		sblock.fs_postbloff = &sblock.fs_space[0] -
47523682Speter		    (u_char *)(&sblock.fs_firstfield);
4761558Srgrimes		sblock.fs_rotbloff = sblock.fs_postbloff + postblsize;
4771558Srgrimes		totalsbsize += postblsize;
4781558Srgrimes	}
4791558Srgrimes	if (totalsbsize > SBSIZE ||
4801558Srgrimes	    sblock.fs_nsect > (1 << NBBY) * NSPB(&sblock)) {
4811558Srgrimes		printf("%s %s %d %s %d.%s",
4821558Srgrimes		    "Warning: insufficient space in super block for\n",
4831558Srgrimes		    "rotational layout tables with nsect", sblock.fs_nsect,
4841558Srgrimes		    "and ntrak", sblock.fs_ntrak,
4851558Srgrimes		    "\nFile system performance may be impaired.\n");
4861558Srgrimes		sblock.fs_cpc = 0;
4871558Srgrimes		goto next;
4881558Srgrimes	}
4891558Srgrimes	sblock.fs_sbsize = fragroundup(&sblock, totalsbsize);
49032623Sbde	if (sblock.fs_sbsize > SBSIZE)
49132623Sbde		sblock.fs_sbsize = SBSIZE;
4921558Srgrimes	/*
4931558Srgrimes	 * calculate the available blocks for each rotational position
4941558Srgrimes	 */
4951558Srgrimes	for (cylno = 0; cylno < sblock.fs_cpc; cylno++)
4961558Srgrimes		for (rpos = 0; rpos < sblock.fs_nrpos; rpos++)
4971558Srgrimes			fs_postbl(&sblock, cylno)[rpos] = -1;
4981558Srgrimes	for (i = (rotblsize - 1) * sblock.fs_frag;
4991558Srgrimes	     i >= 0; i -= sblock.fs_frag) {
5001558Srgrimes		cylno = cbtocylno(&sblock, i);
5011558Srgrimes		rpos = cbtorpos(&sblock, i);
5021558Srgrimes		blk = fragstoblks(&sblock, i);
5031558Srgrimes		if (fs_postbl(&sblock, cylno)[rpos] == -1)
5041558Srgrimes			fs_rotbl(&sblock)[blk] = 0;
5051558Srgrimes		else
5061558Srgrimes			fs_rotbl(&sblock)[blk] =
5071558Srgrimes			    fs_postbl(&sblock, cylno)[rpos] - blk;
5081558Srgrimes		fs_postbl(&sblock, cylno)[rpos] = blk;
5091558Srgrimes	}
5101558Srgrimesnext:
5111558Srgrimes	/*
5121558Srgrimes	 * Compute/validate number of cylinder groups.
5131558Srgrimes	 */
5141558Srgrimes	sblock.fs_ncg = sblock.fs_ncyl / sblock.fs_cpg;
5151558Srgrimes	if (sblock.fs_ncyl % sblock.fs_cpg)
5161558Srgrimes		sblock.fs_ncg++;
5171558Srgrimes	sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock);
5181558Srgrimes	i = MIN(~sblock.fs_cgmask, sblock.fs_ncg - 1);
5191558Srgrimes	if (cgdmin(&sblock, i) - cgbase(&sblock, i) >= sblock.fs_fpg) {
52037239Sbde		printf("inode blocks/cyl group (%ld) >= data blocks (%ld)\n",
5211558Srgrimes		    cgdmin(&sblock, i) - cgbase(&sblock, i) / sblock.fs_frag,
52237239Sbde		    (long)(sblock.fs_fpg / sblock.fs_frag));
5231558Srgrimes		printf("number of cylinders per cylinder group (%d) %s.\n",
5241558Srgrimes		    sblock.fs_cpg, "must be increased");
5251558Srgrimes		exit(29);
5261558Srgrimes	}
5271558Srgrimes	j = sblock.fs_ncg - 1;
5281558Srgrimes	if ((i = fssize - j * sblock.fs_fpg) < sblock.fs_fpg &&
5291558Srgrimes	    cgdmin(&sblock, j) - cgbase(&sblock, j) > i) {
5301558Srgrimes		if (j == 0) {
5311558Srgrimes			printf("Filesystem must have at least %d sectors\n",
5321558Srgrimes			    NSPF(&sblock) *
5331558Srgrimes			    (cgdmin(&sblock, 0) + 3 * sblock.fs_frag));
5341558Srgrimes			exit(30);
5351558Srgrimes		}
53692532Sbde		printf(
53792532Sbde"Warning: inode blocks/cyl group (%ld) >= data blocks (%ld) in last\n",
5381558Srgrimes		    (cgdmin(&sblock, j) - cgbase(&sblock, j)) / sblock.fs_frag,
5391558Srgrimes		    i / sblock.fs_frag);
54092532Sbde		printf(
54192532Sbde"    cylinder group. This implies %ld sector(s) cannot be allocated.\n",
54292532Sbde		    i * NSPF(&sblock));
5431558Srgrimes		sblock.fs_ncg--;
5441558Srgrimes		sblock.fs_ncyl -= sblock.fs_ncyl % sblock.fs_cpg;
5451558Srgrimes		sblock.fs_size = fssize = sblock.fs_ncyl * sblock.fs_spc /
5461558Srgrimes		    NSPF(&sblock);
5471558Srgrimes		warn = 0;
5481558Srgrimes	}
54977420Sphk	if (warn) {
5501558Srgrimes		printf("Warning: %d sector(s) in last cylinder unallocated\n",
5511558Srgrimes		    sblock.fs_spc -
55285860Sphk		    (fssize * NSPF(&sblock) - (sblock.fs_ncyl - 1) *
55385860Sphk		    sblock.fs_spc));
5541558Srgrimes	}
5551558Srgrimes	/*
5561558Srgrimes	 * fill in remaining fields of the super block
5571558Srgrimes	 */
5581558Srgrimes	sblock.fs_csaddr = cgdmin(&sblock, 0);
5591558Srgrimes	sblock.fs_cssize =
5601558Srgrimes	    fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
56171073Siedowse	/*
56271073Siedowse	 * The superblock fields 'fs_csmask' and 'fs_csshift' are no
56371073Siedowse	 * longer used. However, we still initialise them so that the
56471073Siedowse	 * filesystem remains compatible with old kernels.
56571073Siedowse	 */
5661558Srgrimes	i = sblock.fs_bsize / sizeof(struct csum);
5671558Srgrimes	sblock.fs_csmask = ~(i - 1);
56892712Siedowse	sblock.fs_csshift = ilog2(i);
5691558Srgrimes	fscs = (struct csum *)calloc(1, sblock.fs_cssize);
57037664Scharnier	if (fscs == NULL)
57137664Scharnier		errx(31, "calloc failed");
5721558Srgrimes	sblock.fs_magic = FS_MAGIC;
5731558Srgrimes	sblock.fs_rotdelay = rotdelay;
5741558Srgrimes	sblock.fs_minfree = minfree;
5751558Srgrimes	sblock.fs_maxcontig = maxcontig;
5761558Srgrimes	sblock.fs_maxbpg = maxbpg;
5771558Srgrimes	sblock.fs_rps = rpm / 60;
5781558Srgrimes	sblock.fs_optim = opt;
5791558Srgrimes	sblock.fs_cgrotor = 0;
5801558Srgrimes	sblock.fs_cstotal.cs_ndir = 0;
5811558Srgrimes	sblock.fs_cstotal.cs_nbfree = 0;
5821558Srgrimes	sblock.fs_cstotal.cs_nifree = 0;
5831558Srgrimes	sblock.fs_cstotal.cs_nffree = 0;
5841558Srgrimes	sblock.fs_fmod = 0;
5851558Srgrimes	sblock.fs_ronly = 0;
5862294Sdg	sblock.fs_clean = 1;
58724149Sguido	sblock.fs_id[0] = (long)utime;
58824215Sache	sblock.fs_id[1] = random();
58924149Sguido
5901558Srgrimes	/*
5911558Srgrimes	 * Dump out summary information about file system.
5921558Srgrimes	 */
59377420Sphk	printf("%s:\t%d sectors in %d %s of %d tracks, %d sectors\n",
59477420Sphk	    fsys, sblock.fs_size * NSPF(&sblock), sblock.fs_ncyl,
59577420Sphk	    "cylinders", sblock.fs_ntrak, sblock.fs_nsect);
5961558Srgrimes#define B2MBFACTOR (1 / (1024.0 * 1024.0))
59785860Sphk	printf("\t%.1fMB in %d cyl groups (%d c/g, %.2fMB/g, %d i/g)%s\n",
59877420Sphk	    (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
59977420Sphk	    sblock.fs_ncg, sblock.fs_cpg,
60077420Sphk	    (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
60177420Sphk	    sblock.fs_ipg,
60277420Sphk	    sblock.fs_flags & FS_DOSOFTDEP ? " SOFTUPDATES" : "");
6031558Srgrimes#undef B2MBFACTOR
6041558Srgrimes	/*
6051558Srgrimes	 * Now build the cylinders group blocks and
6061558Srgrimes	 * then print out indices of cylinder groups.
6071558Srgrimes	 */
60877420Sphk	printf("super-block backups (for fsck -b #) at:\n");
60913769Sjoerg	i = 0;
61013769Sjoerg	width = charsperline();
6111558Srgrimes	for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
6121558Srgrimes		initcg(cylno, utime);
61375904Skris		j = snprintf(tmpbuf, sizeof(tmpbuf), " %ld%s",
61450134Sbillf		    fsbtodb(&sblock, cgsblock(&sblock, cylno)),
61585860Sphk		    cylno < (sblock.fs_ncg-1) ? "," : "");
61681980Sbrian		if (j < 0)
61781980Sbrian			tmpbuf[j = 0] = '\0';
61837239Sbde		if (i + j >= width) {
6191558Srgrimes			printf("\n");
62013769Sjoerg			i = 0;
62113769Sjoerg		}
62213769Sjoerg		i += j;
62313769Sjoerg		printf("%s", tmpbuf);
6248218Sdg		fflush(stdout);
6251558Srgrimes	}
62677420Sphk	printf("\n");
62777420Sphk	if (Nflag)
6281558Srgrimes		exit(0);
6291558Srgrimes	/*
6301558Srgrimes	 * Now construct the initial file system,
6311558Srgrimes	 * then write out the super-block.
6321558Srgrimes	 */
6331558Srgrimes	fsinit(utime);
6341558Srgrimes	sblock.fs_time = utime;
6351558Srgrimes	wtfs((int)SBOFF / sectorsize, sbsize, (char *)&sblock);
6361558Srgrimes	for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize)
6371558Srgrimes		wtfs(fsbtodb(&sblock, sblock.fs_csaddr + numfrags(&sblock, i)),
6381558Srgrimes			sblock.fs_cssize - i < sblock.fs_bsize ?
63985860Sphk			sblock.fs_cssize - i : sblock.fs_bsize,
6401558Srgrimes			((char *)fscs) + i);
6418871Srgrimes	/*
6421558Srgrimes	 * Write out the duplicate super blocks
6431558Srgrimes	 */
6441558Srgrimes	for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
6451558Srgrimes		wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)),
6461558Srgrimes		    sbsize, (char *)&sblock);
64767478Sjwd	wtfsflush();
6481558Srgrimes	/*
6491558Srgrimes	 * Update information about this partion in pack
6501558Srgrimes	 * label, to that it may be updated on disk.
6511558Srgrimes	 */
6521558Srgrimes	pp->p_fstype = FS_BSDFFS;
6531558Srgrimes	pp->p_fsize = sblock.fs_fsize;
6541558Srgrimes	pp->p_frag = sblock.fs_frag;
6551558Srgrimes	pp->p_cpg = sblock.fs_cpg;
6561558Srgrimes}
6571558Srgrimes
6581558Srgrimes/*
6591558Srgrimes * Initialize a cylinder group.
6601558Srgrimes */
66137664Scharniervoid
66292711Siedowseinitcg(int cylno, time_t utime)
6631558Srgrimes{
6641558Srgrimes	daddr_t cbase, d, dlower, dupper, dmax, blkno;
66592483Sphk	struct csum *cs;
66692710Siedowse	long i, j;
6671558Srgrimes
6681558Srgrimes	/*
6691558Srgrimes	 * Determine block bounds for cylinder group.
6701558Srgrimes	 * Allow space for super block summary information in first
6711558Srgrimes	 * cylinder group.
6721558Srgrimes	 */
6731558Srgrimes	cbase = cgbase(&sblock, cylno);
6741558Srgrimes	dmax = cbase + sblock.fs_fpg;
6751558Srgrimes	if (dmax > sblock.fs_size)
6761558Srgrimes		dmax = sblock.fs_size;
6771558Srgrimes	dlower = cgsblock(&sblock, cylno) - cbase;
6781558Srgrimes	dupper = cgdmin(&sblock, cylno) - cbase;
6791558Srgrimes	if (cylno == 0)
6801558Srgrimes		dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
6811558Srgrimes	cs = fscs + cylno;
68223682Speter	memset(&acg, 0, sblock.fs_cgsize);
6831558Srgrimes	acg.cg_time = utime;
6841558Srgrimes	acg.cg_magic = CG_MAGIC;
6851558Srgrimes	acg.cg_cgx = cylno;
6861558Srgrimes	if (cylno == sblock.fs_ncg - 1)
6871558Srgrimes		acg.cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg;
6881558Srgrimes	else
6891558Srgrimes		acg.cg_ncyl = sblock.fs_cpg;
6901558Srgrimes	acg.cg_niblk = sblock.fs_ipg;
6911558Srgrimes	acg.cg_ndblk = dmax - cbase;
6921558Srgrimes	if (sblock.fs_contigsumsize > 0)
6931558Srgrimes		acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag;
69423682Speter	acg.cg_btotoff = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield);
69538579Sdfr	acg.cg_boff = acg.cg_btotoff + sblock.fs_cpg * sizeof(int32_t);
6968871Srgrimes	acg.cg_iusedoff = acg.cg_boff +
69738579Sdfr		sblock.fs_cpg * sblock.fs_nrpos * sizeof(u_int16_t);
6981558Srgrimes	acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, NBBY);
6991558Srgrimes	if (sblock.fs_contigsumsize <= 0) {
7001558Srgrimes		acg.cg_nextfreeoff = acg.cg_freeoff +
70192532Sbde		    howmany(sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock),
70292532Sbde		    NBBY);
7031558Srgrimes	} else {
7041558Srgrimes		acg.cg_clustersumoff = acg.cg_freeoff + howmany
7051558Srgrimes		    (sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY) -
70638579Sdfr		    sizeof(u_int32_t);
7071558Srgrimes		acg.cg_clustersumoff =
70838579Sdfr		    roundup(acg.cg_clustersumoff, sizeof(u_int32_t));
7091558Srgrimes		acg.cg_clusteroff = acg.cg_clustersumoff +
71038579Sdfr		    (sblock.fs_contigsumsize + 1) * sizeof(u_int32_t);
7111558Srgrimes		acg.cg_nextfreeoff = acg.cg_clusteroff + howmany
7121558Srgrimes		    (sblock.fs_cpg * sblock.fs_spc / NSPB(&sblock), NBBY);
7131558Srgrimes	}
71492532Sbde	if (acg.cg_nextfreeoff - (long)(&acg.cg_firstfield) >
71592532Sbde	    sblock.fs_cgsize) {
7161558Srgrimes		printf("Panic: cylinder group too big\n");
7171558Srgrimes		exit(37);
7181558Srgrimes	}
7191558Srgrimes	acg.cg_cs.cs_nifree += sblock.fs_ipg;
7201558Srgrimes	if (cylno == 0)
7211558Srgrimes		for (i = 0; i < ROOTINO; i++) {
7221558Srgrimes			setbit(cg_inosused(&acg), i);
7231558Srgrimes			acg.cg_cs.cs_nifree--;
7241558Srgrimes		}
72524149Sguido	for (i = 0; i < sblock.fs_ipg / INOPF(&sblock); i += sblock.fs_frag) {
72624149Sguido		for (j = 0; j < sblock.fs_bsize / sizeof(struct dinode); j++)
72724215Sache			zino[j].di_gen = random();
7281558Srgrimes		wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
7291558Srgrimes		    sblock.fs_bsize, (char *)zino);
73024149Sguido	}
7311558Srgrimes	if (cylno > 0) {
7321558Srgrimes		/*
7331558Srgrimes		 * In cylno 0, beginning space is reserved
7341558Srgrimes		 * for boot and super blocks.
7351558Srgrimes		 */
7361558Srgrimes		for (d = 0; d < dlower; d += sblock.fs_frag) {
7371558Srgrimes			blkno = d / sblock.fs_frag;
7381558Srgrimes			setblock(&sblock, cg_blksfree(&acg), blkno);
7391558Srgrimes			if (sblock.fs_contigsumsize > 0)
7401558Srgrimes				setbit(cg_clustersfree(&acg), blkno);
7411558Srgrimes			acg.cg_cs.cs_nbfree++;
7421558Srgrimes			cg_blktot(&acg)[cbtocylno(&sblock, d)]++;
7431558Srgrimes			cg_blks(&sblock, &acg, cbtocylno(&sblock, d))
7441558Srgrimes			    [cbtorpos(&sblock, d)]++;
7451558Srgrimes		}
7461558Srgrimes		sblock.fs_dsize += dlower;
7471558Srgrimes	}
7481558Srgrimes	sblock.fs_dsize += acg.cg_ndblk - dupper;
74937664Scharnier	if ((i = dupper % sblock.fs_frag)) {
7501558Srgrimes		acg.cg_frsum[sblock.fs_frag - i]++;
7511558Srgrimes		for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
7521558Srgrimes			setbit(cg_blksfree(&acg), dupper);
7531558Srgrimes			acg.cg_cs.cs_nffree++;
7541558Srgrimes		}
7551558Srgrimes	}
75685860Sphk	for (d = dupper; d + sblock.fs_frag <= dmax - cbase;) {
7571558Srgrimes		blkno = d / sblock.fs_frag;
7581558Srgrimes		setblock(&sblock, cg_blksfree(&acg), blkno);
7591558Srgrimes		if (sblock.fs_contigsumsize > 0)
7601558Srgrimes			setbit(cg_clustersfree(&acg), blkno);
7611558Srgrimes		acg.cg_cs.cs_nbfree++;
7621558Srgrimes		cg_blktot(&acg)[cbtocylno(&sblock, d)]++;
7631558Srgrimes		cg_blks(&sblock, &acg, cbtocylno(&sblock, d))
7641558Srgrimes		    [cbtorpos(&sblock, d)]++;
7651558Srgrimes		d += sblock.fs_frag;
7661558Srgrimes	}
7671558Srgrimes	if (d < dmax - cbase) {
7681558Srgrimes		acg.cg_frsum[dmax - cbase - d]++;
7691558Srgrimes		for (; d < dmax - cbase; d++) {
7701558Srgrimes			setbit(cg_blksfree(&acg), d);
7711558Srgrimes			acg.cg_cs.cs_nffree++;
7721558Srgrimes		}
7731558Srgrimes	}
7741558Srgrimes	if (sblock.fs_contigsumsize > 0) {
77523682Speter		int32_t *sump = cg_clustersum(&acg);
7761558Srgrimes		u_char *mapp = cg_clustersfree(&acg);
7771558Srgrimes		int map = *mapp++;
7781558Srgrimes		int bit = 1;
7791558Srgrimes		int run = 0;
7801558Srgrimes
7811558Srgrimes		for (i = 0; i < acg.cg_nclusterblks; i++) {
78285860Sphk			if ((map & bit) != 0)
7831558Srgrimes				run++;
78485860Sphk			else if (run != 0) {
7851558Srgrimes				if (run > sblock.fs_contigsumsize)
7861558Srgrimes					run = sblock.fs_contigsumsize;
7871558Srgrimes				sump[run]++;
7881558Srgrimes				run = 0;
7891558Srgrimes			}
79085860Sphk			if ((i & (NBBY - 1)) != NBBY - 1)
7911558Srgrimes				bit <<= 1;
79285860Sphk			else {
7931558Srgrimes				map = *mapp++;
7941558Srgrimes				bit = 1;
7951558Srgrimes			}
7961558Srgrimes		}
7971558Srgrimes		if (run != 0) {
7981558Srgrimes			if (run > sblock.fs_contigsumsize)
7991558Srgrimes				run = sblock.fs_contigsumsize;
8001558Srgrimes			sump[run]++;
8011558Srgrimes		}
8021558Srgrimes	}
8031558Srgrimes	sblock.fs_cstotal.cs_ndir += acg.cg_cs.cs_ndir;
8041558Srgrimes	sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree;
8051558Srgrimes	sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree;
8061558Srgrimes	sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree;
8071558Srgrimes	*cs = acg.cg_cs;
8081558Srgrimes	wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
8091558Srgrimes		sblock.fs_bsize, (char *)&acg);
8101558Srgrimes}
8111558Srgrimes
8121558Srgrimes/*
8131558Srgrimes * initialize the file system
8141558Srgrimes */
8151558Srgrimesstruct dinode node;
8161558Srgrimes
8171558Srgrimes#ifdef LOSTDIR
8181558Srgrimes#define PREDEFDIR 3
8191558Srgrimes#else
8201558Srgrimes#define PREDEFDIR 2
8211558Srgrimes#endif
8221558Srgrimes
8231558Srgrimesstruct direct root_dir[] = {
8241558Srgrimes	{ ROOTINO, sizeof(struct direct), DT_DIR, 1, "." },
8251558Srgrimes	{ ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
8261558Srgrimes#ifdef LOSTDIR
8271558Srgrimes	{ LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 10, "lost+found" },
8281558Srgrimes#endif
8291558Srgrimes};
8301558Srgrimesstruct odirect {
8311558Srgrimes	u_long	d_ino;
8321558Srgrimes	u_short	d_reclen;
8331558Srgrimes	u_short	d_namlen;
8341558Srgrimes	u_char	d_name[MAXNAMLEN + 1];
8351558Srgrimes} oroot_dir[] = {
8361558Srgrimes	{ ROOTINO, sizeof(struct direct), 1, "." },
8371558Srgrimes	{ ROOTINO, sizeof(struct direct), 2, ".." },
8381558Srgrimes#ifdef LOSTDIR
8391558Srgrimes	{ LOSTFOUNDINO, sizeof(struct direct), 10, "lost+found" },
8401558Srgrimes#endif
8411558Srgrimes};
8421558Srgrimes#ifdef LOSTDIR
8431558Srgrimesstruct direct lost_found_dir[] = {
8441558Srgrimes	{ LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 1, "." },
8451558Srgrimes	{ ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
8461558Srgrimes	{ 0, DIRBLKSIZ, 0, 0, 0 },
8471558Srgrimes};
8481558Srgrimesstruct odirect olost_found_dir[] = {
8491558Srgrimes	{ LOSTFOUNDINO, sizeof(struct direct), 1, "." },
8501558Srgrimes	{ ROOTINO, sizeof(struct direct), 2, ".." },
8511558Srgrimes	{ 0, DIRBLKSIZ, 0, 0 },
8521558Srgrimes};
8531558Srgrimes#endif
8541558Srgrimeschar buf[MAXBSIZE];
8551558Srgrimes
85637664Scharniervoid
85792711Siedowsefsinit(time_t utime)
8581558Srgrimes{
85937664Scharnier#ifdef LOSTDIR
8601558Srgrimes	int i;
86137664Scharnier#endif
8621558Srgrimes
8631558Srgrimes	/*
8641558Srgrimes	 * initialize the node
8651558Srgrimes	 */
86623682Speter	node.di_atime = utime;
86723682Speter	node.di_mtime = utime;
86823682Speter	node.di_ctime = utime;
8691558Srgrimes#ifdef LOSTDIR
8701558Srgrimes	/*
8711558Srgrimes	 * create the lost+found directory
8721558Srgrimes	 */
8731558Srgrimes	if (Oflag) {
8741558Srgrimes		(void)makedir((struct direct *)olost_found_dir, 2);
8751558Srgrimes		for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ)
87623682Speter			memmove(&buf[i], &olost_found_dir[2],
8771558Srgrimes			    DIRSIZ(0, &olost_found_dir[2]));
8781558Srgrimes	} else {
8791558Srgrimes		(void)makedir(lost_found_dir, 2);
8801558Srgrimes		for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ)
88123682Speter			memmove(&buf[i], &lost_found_dir[2],
8821558Srgrimes			    DIRSIZ(0, &lost_found_dir[2]));
8831558Srgrimes	}
8841558Srgrimes	node.di_mode = IFDIR | UMASK;
8851558Srgrimes	node.di_nlink = 2;
8861558Srgrimes	node.di_size = sblock.fs_bsize;
8871558Srgrimes	node.di_db[0] = alloc(node.di_size, node.di_mode);
8881558Srgrimes	node.di_blocks = btodb(fragroundup(&sblock, node.di_size));
8891558Srgrimes	wtfs(fsbtodb(&sblock, node.di_db[0]), node.di_size, buf);
8901558Srgrimes	iput(&node, LOSTFOUNDINO);
8911558Srgrimes#endif
8921558Srgrimes	/*
8931558Srgrimes	 * create the root directory
8941558Srgrimes	 */
89577420Sphk	node.di_mode = IFDIR | UMASK;
8961558Srgrimes	node.di_nlink = PREDEFDIR;
8971558Srgrimes	if (Oflag)
8981558Srgrimes		node.di_size = makedir((struct direct *)oroot_dir, PREDEFDIR);
8991558Srgrimes	else
9001558Srgrimes		node.di_size = makedir(root_dir, PREDEFDIR);
9011558Srgrimes	node.di_db[0] = alloc(sblock.fs_fsize, node.di_mode);
9021558Srgrimes	node.di_blocks = btodb(fragroundup(&sblock, node.di_size));
9031558Srgrimes	wtfs(fsbtodb(&sblock, node.di_db[0]), sblock.fs_fsize, buf);
9041558Srgrimes	iput(&node, ROOTINO);
9051558Srgrimes}
9061558Srgrimes
9071558Srgrimes/*
9081558Srgrimes * construct a set of directory entries in "buf".
9091558Srgrimes * return size of directory.
9101558Srgrimes */
91137664Scharnierint
91292711Siedowsemakedir(struct direct *protodir, int entries)
9131558Srgrimes{
9141558Srgrimes	char *cp;
9151558Srgrimes	int i, spcleft;
9161558Srgrimes
9171558Srgrimes	spcleft = DIRBLKSIZ;
9181558Srgrimes	for (cp = buf, i = 0; i < entries - 1; i++) {
9191558Srgrimes		protodir[i].d_reclen = DIRSIZ(0, &protodir[i]);
92023682Speter		memmove(cp, &protodir[i], protodir[i].d_reclen);
9211558Srgrimes		cp += protodir[i].d_reclen;
9221558Srgrimes		spcleft -= protodir[i].d_reclen;
9231558Srgrimes	}
9241558Srgrimes	protodir[i].d_reclen = spcleft;
92523682Speter	memmove(cp, &protodir[i], DIRSIZ(0, &protodir[i]));
9261558Srgrimes	return (DIRBLKSIZ);
9271558Srgrimes}
9281558Srgrimes
9291558Srgrimes/*
9301558Srgrimes * allocate a block or frag
9311558Srgrimes */
9321558Srgrimesdaddr_t
93392711Siedowsealloc(int size, int mode)
9341558Srgrimes{
9351558Srgrimes	int i, frag;
9361558Srgrimes	daddr_t d, blkno;
9371558Srgrimes
9381558Srgrimes	rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
9391558Srgrimes	    (char *)&acg);
9401558Srgrimes	if (acg.cg_magic != CG_MAGIC) {
9411558Srgrimes		printf("cg 0: bad magic number\n");
9421558Srgrimes		return (0);
9431558Srgrimes	}
9441558Srgrimes	if (acg.cg_cs.cs_nbfree == 0) {
9451558Srgrimes		printf("first cylinder group ran out of space\n");
9461558Srgrimes		return (0);
9471558Srgrimes	}
9481558Srgrimes	for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag)
9491558Srgrimes		if (isblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag))
9501558Srgrimes			goto goth;
9511558Srgrimes	printf("internal error: can't find block in cyl 0\n");
9521558Srgrimes	return (0);
9531558Srgrimesgoth:
9541558Srgrimes	blkno = fragstoblks(&sblock, d);
9551558Srgrimes	clrblock(&sblock, cg_blksfree(&acg), blkno);
95623682Speter	if (sblock.fs_contigsumsize > 0)
95723682Speter		clrbit(cg_clustersfree(&acg), blkno);
9581558Srgrimes	acg.cg_cs.cs_nbfree--;
9591558Srgrimes	sblock.fs_cstotal.cs_nbfree--;
9601558Srgrimes	fscs[0].cs_nbfree--;
9611558Srgrimes	if (mode & IFDIR) {
9621558Srgrimes		acg.cg_cs.cs_ndir++;
9631558Srgrimes		sblock.fs_cstotal.cs_ndir++;
9641558Srgrimes		fscs[0].cs_ndir++;
9651558Srgrimes	}
9661558Srgrimes	cg_blktot(&acg)[cbtocylno(&sblock, d)]--;
9671558Srgrimes	cg_blks(&sblock, &acg, cbtocylno(&sblock, d))[cbtorpos(&sblock, d)]--;
9681558Srgrimes	if (size != sblock.fs_bsize) {
9691558Srgrimes		frag = howmany(size, sblock.fs_fsize);
9701558Srgrimes		fscs[0].cs_nffree += sblock.fs_frag - frag;
9711558Srgrimes		sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag;
9721558Srgrimes		acg.cg_cs.cs_nffree += sblock.fs_frag - frag;
9731558Srgrimes		acg.cg_frsum[sblock.fs_frag - frag]++;
9741558Srgrimes		for (i = frag; i < sblock.fs_frag; i++)
9751558Srgrimes			setbit(cg_blksfree(&acg), d + i);
9761558Srgrimes	}
9771558Srgrimes	wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
9781558Srgrimes	    (char *)&acg);
9791558Srgrimes	return (d);
9801558Srgrimes}
9811558Srgrimes
9821558Srgrimes/*
98323682Speter * Calculate number of inodes per group.
98423682Speter */
98523682Speterlong
98692711Siedowsecalcipg(long cpg, long bpcg, off_t *usedbp)
98723682Speter{
98823682Speter	int i;
98923682Speter	long ipg, new_ipg, ncg, ncyl;
99023682Speter	off_t usedb;
99123682Speter
99223682Speter	/*
99323682Speter	 * Prepare to scale by fssize / (number of sectors in cylinder groups).
99423682Speter	 * Note that fssize is still in sectors, not filesystem blocks.
99523682Speter	 */
99636498Sbde	ncyl = howmany(fssize, (u_int)secpercyl);
99723682Speter	ncg = howmany(ncyl, cpg);
99823682Speter	/*
99923682Speter	 * Iterate a few times to allow for ipg depending on itself.
100023682Speter	 */
100123682Speter	ipg = 0;
100223682Speter	for (i = 0; i < 10; i++) {
100385860Sphk		usedb = (sblock.fs_iblkno + ipg / INOPF(&sblock)) *
100485860Sphk		    NSPF(&sblock) * (off_t)sectorsize;
100585860Sphk		new_ipg = (cpg * (quad_t)bpcg - usedb) / density *
100685860Sphk		    fssize / ncg / secpercyl / cpg;
100723682Speter		new_ipg = roundup(new_ipg, INOPB(&sblock));
100823682Speter		if (new_ipg == ipg)
100923682Speter			break;
101023682Speter		ipg = new_ipg;
101123682Speter	}
101223682Speter	*usedbp = usedb;
101323682Speter	return (ipg);
101423682Speter}
101523682Speter
101623682Speter/*
10171558Srgrimes * Allocate an inode on the disk
10181558Srgrimes */
101937664Scharniervoid
102092711Siedowseiput(struct dinode *ip, ino_t ino)
10211558Srgrimes{
10221558Srgrimes	struct dinode buf[MAXINOPB];
10231558Srgrimes	daddr_t d;
10241558Srgrimes	int c;
10251558Srgrimes
102624215Sache	ip->di_gen = random();
10271558Srgrimes	c = ino_to_cg(&sblock, ino);
10281558Srgrimes	rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
10291558Srgrimes	    (char *)&acg);
10301558Srgrimes	if (acg.cg_magic != CG_MAGIC) {
10311558Srgrimes		printf("cg 0: bad magic number\n");
10321558Srgrimes		exit(31);
10331558Srgrimes	}
10341558Srgrimes	acg.cg_cs.cs_nifree--;
10351558Srgrimes	setbit(cg_inosused(&acg), ino);
10361558Srgrimes	wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
10371558Srgrimes	    (char *)&acg);
10381558Srgrimes	sblock.fs_cstotal.cs_nifree--;
10391558Srgrimes	fscs[0].cs_nifree--;
10401558Srgrimes	if (ino >= sblock.fs_ipg * sblock.fs_ncg) {
10411558Srgrimes		printf("fsinit: inode value out of range (%d).\n", ino);
10421558Srgrimes		exit(32);
10431558Srgrimes	}
10441558Srgrimes	d = fsbtodb(&sblock, ino_to_fsba(&sblock, ino));
104537664Scharnier	rdfs(d, sblock.fs_bsize, (char *)buf);
10461558Srgrimes	buf[ino_to_fsbo(&sblock, ino)] = *ip;
104737664Scharnier	wtfs(d, sblock.fs_bsize, (char *)buf);
10481558Srgrimes}
10491558Srgrimes
10501558Srgrimes/*
10511558Srgrimes * read a block from the file system
10521558Srgrimes */
105337664Scharniervoid
105492711Siedowserdfs(daddr_t bno, int size, char *bf)
10551558Srgrimes{
10561558Srgrimes	int n;
10571558Srgrimes
105867478Sjwd	wtfsflush();
10591558Srgrimes	if (lseek(fsi, (off_t)bno * sectorsize, 0) < 0) {
106037239Sbde		printf("seek error: %ld\n", (long)bno);
106137664Scharnier		err(33, "rdfs");
10621558Srgrimes	}
10631558Srgrimes	n = read(fsi, bf, size);
10641558Srgrimes	if (n != size) {
106537239Sbde		printf("read error: %ld\n", (long)bno);
106637664Scharnier		err(34, "rdfs");
10671558Srgrimes	}
10681558Srgrimes}
10691558Srgrimes
107067239Speter#define WCSIZE (128 * 1024)
107167239Speterdaddr_t wc_sect;		/* units of sectorsize */
107267239Speterint wc_end;			/* bytes */
107367239Speterstatic char wc[WCSIZE];		/* bytes */
107467239Speter
10751558Srgrimes/*
107667478Sjwd * Flush dirty write behind buffer.
107767478Sjwd */
107867478Sjwdvoid
107967478Sjwdwtfsflush()
108067478Sjwd{
108167478Sjwd	int n;
108267478Sjwd	if (wc_end) {
108367478Sjwd		if (lseek(fso, (off_t)wc_sect * sectorsize, SEEK_SET) < 0) {
108467478Sjwd			printf("seek error: %ld\n", (long)wc_sect);
108567478Sjwd			err(35, "wtfs - writecombine");
108667478Sjwd		}
108767478Sjwd		n = write(fso, wc, wc_end);
108867478Sjwd		if (n != wc_end) {
108967478Sjwd			printf("write error: %ld\n", (long)wc_sect);
109067478Sjwd			err(36, "wtfs - writecombine");
109167478Sjwd		}
109267478Sjwd		wc_end = 0;
109367478Sjwd	}
109467478Sjwd}
109567478Sjwd
109667478Sjwd/*
10971558Srgrimes * write a block to the file system
10981558Srgrimes */
109937664Scharniervoid
110092711Siedowsewtfs(daddr_t bno, int size, char *bf)
11011558Srgrimes{
110292711Siedowse	int done, n;
11031558Srgrimes
11041558Srgrimes	if (Nflag)
11051558Srgrimes		return;
110667239Speter	done = 0;
110767239Speter	if (wc_end == 0 && size <= WCSIZE) {
110867239Speter		wc_sect = bno;
110967239Speter		bcopy(bf, wc, size);
111067239Speter		wc_end = size;
111167239Speter		if (wc_end < WCSIZE)
111267239Speter			return;
111367239Speter		done = 1;
111467239Speter	}
111567479Sjwd	if ((off_t)wc_sect * sectorsize + wc_end == (off_t)bno * sectorsize &&
111667239Speter	    wc_end + size <= WCSIZE) {
111767239Speter		bcopy(bf, wc + wc_end, size);
111867239Speter		wc_end += size;
111967239Speter		if (wc_end < WCSIZE)
112067239Speter			return;
112167239Speter		done = 1;
112267239Speter	}
112367478Sjwd	wtfsflush();
112467239Speter	if (done)
112567239Speter		return;
11261558Srgrimes	if (lseek(fso, (off_t)bno * sectorsize, SEEK_SET) < 0) {
112737239Sbde		printf("seek error: %ld\n", (long)bno);
112837664Scharnier		err(35, "wtfs");
11291558Srgrimes	}
11301558Srgrimes	n = write(fso, bf, size);
11311558Srgrimes	if (n != size) {
113237239Sbde		printf("write error: %ld\n", (long)bno);
113337664Scharnier		err(36, "wtfs");
11341558Srgrimes	}
11351558Srgrimes}
11361558Srgrimes
11371558Srgrimes/*
11381558Srgrimes * check if a block is available
11391558Srgrimes */
114037664Scharnierint
114192711Siedowseisblock(struct fs *fs, unsigned char *cp, int h)
11421558Srgrimes{
11431558Srgrimes	unsigned char mask;
11441558Srgrimes
11451558Srgrimes	switch (fs->fs_frag) {
11461558Srgrimes	case 8:
11471558Srgrimes		return (cp[h] == 0xff);
11481558Srgrimes	case 4:
11491558Srgrimes		mask = 0x0f << ((h & 0x1) << 2);
11501558Srgrimes		return ((cp[h >> 1] & mask) == mask);
11511558Srgrimes	case 2:
11521558Srgrimes		mask = 0x03 << ((h & 0x3) << 1);
11531558Srgrimes		return ((cp[h >> 2] & mask) == mask);
11541558Srgrimes	case 1:
11551558Srgrimes		mask = 0x01 << (h & 0x7);
11561558Srgrimes		return ((cp[h >> 3] & mask) == mask);
11571558Srgrimes	default:
11581558Srgrimes		fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag);
11591558Srgrimes		return (0);
11601558Srgrimes	}
11611558Srgrimes}
11621558Srgrimes
11631558Srgrimes/*
11641558Srgrimes * take a block out of the map
11651558Srgrimes */
116637664Scharniervoid
116792711Siedowseclrblock(struct fs *fs, unsigned char *cp, int h)
11681558Srgrimes{
11691558Srgrimes	switch ((fs)->fs_frag) {
11701558Srgrimes	case 8:
11711558Srgrimes		cp[h] = 0;
11721558Srgrimes		return;
11731558Srgrimes	case 4:
11741558Srgrimes		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
11751558Srgrimes		return;
11761558Srgrimes	case 2:
11771558Srgrimes		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
11781558Srgrimes		return;
11791558Srgrimes	case 1:
11801558Srgrimes		cp[h >> 3] &= ~(0x01 << (h & 0x7));
11811558Srgrimes		return;
11821558Srgrimes	default:
11831558Srgrimes		fprintf(stderr, "clrblock bad fs_frag %d\n", fs->fs_frag);
11841558Srgrimes		return;
11851558Srgrimes	}
11861558Srgrimes}
11871558Srgrimes
11881558Srgrimes/*
11891558Srgrimes * put a block into the map
11901558Srgrimes */
119137664Scharniervoid
119292711Siedowsesetblock(struct fs *fs, unsigned char *cp, int h)
11931558Srgrimes{
11941558Srgrimes	switch (fs->fs_frag) {
11951558Srgrimes	case 8:
11961558Srgrimes		cp[h] = 0xff;
11971558Srgrimes		return;
11981558Srgrimes	case 4:
11991558Srgrimes		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
12001558Srgrimes		return;
12011558Srgrimes	case 2:
12021558Srgrimes		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
12031558Srgrimes		return;
12041558Srgrimes	case 1:
12051558Srgrimes		cp[h >> 3] |= (0x01 << (h & 0x7));
12061558Srgrimes		return;
12071558Srgrimes	default:
12081558Srgrimes		fprintf(stderr, "setblock bad fs_frag %d\n", fs->fs_frag);
12091558Srgrimes		return;
12101558Srgrimes	}
12111558Srgrimes}
121213637Sjoerg
121313637Sjoerg/*
121413769Sjoerg * Determine the number of characters in a
121513637Sjoerg * single line.
121613637Sjoerg */
121713637Sjoerg
121813637Sjoergstatic int
121913769Sjoergcharsperline()
122013637Sjoerg{
122113769Sjoerg	int columns;
122213637Sjoerg	char *cp;
122313637Sjoerg	struct winsize ws;
122413637Sjoerg
122513637Sjoerg	columns = 0;
122613637Sjoerg	if (ioctl(0, TIOCGWINSZ, &ws) != -1)
122713637Sjoerg		columns = ws.ws_col;
122813637Sjoerg	if (columns == 0 && (cp = getenv("COLUMNS")))
122913637Sjoerg		columns = atoi(cp);
123013637Sjoerg	if (columns == 0)
123113637Sjoerg		columns = 80;	/* last resort */
123285860Sphk	return (columns);
123313637Sjoerg}
123492712Siedowse
123592712Siedowsestatic int
123692712Siedowseilog2(int val)
123792712Siedowse{
123892712Siedowse	int n;
123992712Siedowse
124092712Siedowse	for (n = 0; n < sizeof(n) * NBBY; n++)
124192712Siedowse		if (1 << n == val)
124292712Siedowse			return (n);
124392712Siedowse	errx(1, "ilog2: %d is not a power of 2\n", val);
124492712Siedowse}
1245