Home | History | Annotate | Line # | Download | only in ffs
mkfs.c revision 1.7
      1  1.7  lukem /*	$NetBSD: mkfs.c,v 1.7 2002/01/18 08:32:34 lukem Exp $	*/
      2  1.7  lukem /* From NetBSD: mkfs.c,v 1.59 2001/12/31 07:07:58 lukem Exp $	*/
      3  1.1  lukem 
      4  1.1  lukem /*
      5  1.1  lukem  * Copyright (c) 1980, 1989, 1993
      6  1.1  lukem  *	The Regents of the University of California.  All rights reserved.
      7  1.1  lukem  *
      8  1.1  lukem  * Redistribution and use in source and binary forms, with or without
      9  1.1  lukem  * modification, are permitted provided that the following conditions
     10  1.1  lukem  * are met:
     11  1.1  lukem  * 1. Redistributions of source code must retain the above copyright
     12  1.1  lukem  *    notice, this list of conditions and the following disclaimer.
     13  1.1  lukem  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  lukem  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  lukem  *    documentation and/or other materials provided with the distribution.
     16  1.1  lukem  * 3. All advertising materials mentioning features or use of this software
     17  1.1  lukem  *    must display the following acknowledgement:
     18  1.1  lukem  *	This product includes software developed by the University of
     19  1.1  lukem  *	California, Berkeley and its contributors.
     20  1.1  lukem  * 4. Neither the name of the University nor the names of its contributors
     21  1.1  lukem  *    may be used to endorse or promote products derived from this software
     22  1.1  lukem  *    without specific prior written permission.
     23  1.1  lukem  *
     24  1.1  lukem  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1  lukem  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1  lukem  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1  lukem  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1  lukem  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1  lukem  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1  lukem  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1  lukem  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1  lukem  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1  lukem  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1  lukem  * SUCH DAMAGE.
     35  1.1  lukem  */
     36  1.1  lukem 
     37  1.1  lukem #include <sys/cdefs.h>
     38  1.1  lukem #ifndef lint
     39  1.1  lukem #if 0
     40  1.1  lukem static char sccsid[] = "@(#)mkfs.c	8.11 (Berkeley) 5/3/95";
     41  1.1  lukem #else
     42  1.7  lukem __RCSID("$NetBSD: mkfs.c,v 1.7 2002/01/18 08:32:34 lukem Exp $");
     43  1.1  lukem #endif
     44  1.1  lukem #endif /* not lint */
     45  1.1  lukem 
     46  1.1  lukem #include <sys/param.h>
     47  1.1  lukem #include <sys/time.h>
     48  1.1  lukem #include <sys/resource.h>
     49  1.1  lukem 
     50  1.1  lukem #include <err.h>
     51  1.1  lukem #include <stdio.h>
     52  1.1  lukem #include <stdlib.h>
     53  1.1  lukem #include <string.h>
     54  1.1  lukem #include <unistd.h>
     55  1.1  lukem 
     56  1.3  lukem #include "makefs.h"
     57  1.3  lukem 
     58  1.6  lukem #include <ufs/ufs/dinode.h>
     59  1.4  lukem #include <ufs/ufs/dir.h>
     60  1.4  lukem #include <ufs/ufs/ufs_bswap.h>
     61  1.4  lukem #include <ufs/ffs/fs.h>
     62  1.1  lukem 
     63  1.5  lukem #include "ffs/ufs_inode.h"
     64  1.1  lukem #include "ffs/ffs_extern.h"
     65  1.1  lukem #include "ffs/newfs_extern.h"
     66  1.1  lukem 
     67  1.1  lukem static void initcg(int, time_t, const fsinfo_t *);
     68  1.1  lukem static int32_t calcipg(int32_t, int32_t, off_t *);
     69  1.1  lukem static void swap_cg(struct cg *, struct cg *);
     70  1.1  lukem 
     71  1.1  lukem static int count_digits(int);
     72  1.1  lukem 
     73  1.1  lukem /*
     74  1.1  lukem  * make file system for cylinder-group style file systems
     75  1.1  lukem  */
     76  1.1  lukem 
     77  1.1  lukem /*
     78  1.1  lukem  * We limit the size of the inode map to be no more than a
     79  1.1  lukem  * third of the cylinder group space, since we must leave at
     80  1.1  lukem  * least an equal amount of space for the block map.
     81  1.1  lukem  *
     82  1.1  lukem  * N.B.: MAXIPG must be a multiple of INOPB(fs).
     83  1.1  lukem  */
     84  1.1  lukem #define MAXIPG(fs)	roundup((fs)->fs_bsize * NBBY / 3, INOPB(fs))
     85  1.1  lukem 
     86  1.1  lukem #define UMASK		0755
     87  1.1  lukem #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
     88  1.1  lukem 
     89  1.1  lukem union {
     90  1.1  lukem 	struct fs fs;
     91  1.1  lukem 	char pad[SBSIZE];
     92  1.1  lukem } fsun;
     93  1.1  lukem #define	sblock	fsun.fs
     94  1.1  lukem 
     95  1.1  lukem union {
     96  1.1  lukem 	struct cg cg;
     97  1.1  lukem 	char pad[MAXBSIZE];
     98  1.1  lukem } cgun;
     99  1.1  lukem #define	acg	cgun.cg
    100  1.1  lukem 
    101  1.1  lukem struct dinode zino[MAXBSIZE / DINODE_SIZE];
    102  1.1  lukem 
    103  1.1  lukem char writebuf[MAXBSIZE];
    104  1.1  lukem 
    105  1.1  lukem static	int	Oflag;		/* format as an 4.3BSD file system */
    106  1.1  lukem static	int	fssize;		/* file system size */
    107  1.1  lukem static	int	ntracks;	/* # tracks/cylinder */
    108  1.1  lukem static	int	nsectors;	/* # sectors/track */
    109  1.1  lukem static	int	nphyssectors;	/* # sectors/track including spares */
    110  1.1  lukem static	int	secpercyl;	/* sectors per cylinder */
    111  1.1  lukem static	int	sectorsize;	/* bytes/sector */
    112  1.1  lukem static	int	rpm;		/* revolutions/minute of drive */
    113  1.1  lukem static	int	interleave;	/* hardware sector interleave */
    114  1.1  lukem static	int	trackskew;	/* sector 0 skew, per track */
    115  1.1  lukem static	int	fsize;		/* fragment size */
    116  1.1  lukem static	int	bsize;		/* block size */
    117  1.1  lukem static	int	cpg;		/* cylinders/cylinder group */
    118  1.1  lukem static	int	cpgflg;		/* cylinders/cylinder group flag was given */
    119  1.1  lukem static	int	minfree;	/* free space threshold */
    120  1.1  lukem static	int	opt;		/* optimization preference (space or time) */
    121  1.1  lukem static	int	density;	/* number of bytes per inode */
    122  1.1  lukem static	int	maxcontig;	/* max contiguous blocks to allocate */
    123  1.1  lukem static	int	rotdelay;	/* rotational delay between blocks */
    124  1.1  lukem static	int	maxbpg;		/* maximum blocks per file in a cyl group */
    125  1.1  lukem static	int	nrpos;		/* # of distinguished rotational positions */
    126  1.1  lukem static	int	bbsize;		/* boot block size */
    127  1.1  lukem static	int	sbsize;		/* superblock size */
    128  1.1  lukem static	int	avgfilesize;	/* expected average file size */
    129  1.1  lukem static	int	avgfpdir;	/* expected number of files per directory */
    130  1.1  lukem 
    131  1.1  lukem 
    132  1.1  lukem struct fs *
    133  1.1  lukem ffs_mkfs(const char *fsys, const fsinfo_t *fsopts)
    134  1.1  lukem {
    135  1.1  lukem 	int32_t i, mincpc, mincpg, inospercg;
    136  1.1  lukem 	int32_t cylno, rpos, blk, j, warned = 0;
    137  1.1  lukem 	int32_t used, mincpgcnt, bpcg;
    138  1.1  lukem 	off_t usedb;
    139  1.1  lukem 	int32_t mapcramped, inodecramped;
    140  1.1  lukem 	int32_t postblsize, rotblsize, totalsbsize;
    141  1.1  lukem 	long long sizepb;
    142  1.1  lukem 	void *space;
    143  1.1  lukem 	int size, blks;
    144  1.1  lukem 	int nprintcols, printcolwidth;
    145  1.1  lukem 
    146  1.1  lukem 	Oflag = 0;
    147  1.1  lukem 	fssize =	fsopts->size / fsopts->sectorsize;
    148  1.1  lukem 	ntracks =	fsopts->ntracks;
    149  1.1  lukem 	nsectors =	fsopts->nsectors;
    150  1.1  lukem 	nphyssectors =	fsopts->nsectors;	/* XXX: no trackspares */
    151  1.1  lukem 	secpercyl =	nsectors * ntracks;
    152  1.1  lukem 	sectorsize =	fsopts->sectorsize;
    153  1.1  lukem 	rpm =		fsopts->rpm;
    154  1.1  lukem 	interleave =	1;				/* XXX: HCD */
    155  1.1  lukem 	trackskew =	0;				/* XXX: HCD */
    156  1.1  lukem 	fsize =		fsopts->fsize;
    157  1.1  lukem 	bsize =		fsopts->bsize;
    158  1.1  lukem 	cpg =		fsopts->cpg;
    159  1.1  lukem 	cpgflg =	1;
    160  1.1  lukem 	minfree =	fsopts->minfree;
    161  1.1  lukem 	opt =		fsopts->optimization;
    162  1.1  lukem 	density =	fsopts->density;
    163  1.1  lukem 	maxcontig =	fsopts->maxcontig;
    164  1.1  lukem 	rotdelay =	fsopts->rotdelay;
    165  1.1  lukem 	maxbpg =	fsopts->maxbpg;
    166  1.1  lukem 	nrpos =		fsopts->nrpos;
    167  1.1  lukem 	bbsize =	BBSIZE;
    168  1.1  lukem 	sbsize =	SBSIZE;
    169  1.1  lukem 	avgfilesize = 	fsopts->avgfilesize;
    170  1.1  lukem 	avgfpdir = 	fsopts->avgfpdir;
    171  1.1  lukem 
    172  1.1  lukem 	if (Oflag) {
    173  1.1  lukem 		sblock.fs_inodefmt = FS_42INODEFMT;
    174  1.1  lukem 		sblock.fs_maxsymlinklen = 0;
    175  1.1  lukem 	} else {
    176  1.1  lukem 		sblock.fs_inodefmt = FS_44INODEFMT;
    177  1.1  lukem 		sblock.fs_maxsymlinklen = MAXSYMLINKLEN;
    178  1.1  lukem 	}
    179  1.1  lukem 	/*
    180  1.1  lukem 	 * Validate the given file system size.
    181  1.1  lukem 	 * Verify that its last block can actually be accessed.
    182  1.1  lukem 	 */
    183  1.1  lukem 	if (fssize <= 0)
    184  1.1  lukem 		printf("preposterous size %d\n", fssize), exit(13);
    185  1.1  lukem 	ffs_wtfs(fssize - 1, sectorsize, (char *)&sblock, fsopts);
    186  1.1  lukem 
    187  1.1  lukem 	/*
    188  1.1  lukem 	 * collect and verify the sector and track info
    189  1.1  lukem 	 */
    190  1.1  lukem 	sblock.fs_nsect = nsectors;
    191  1.1  lukem 	sblock.fs_ntrak = ntracks;
    192  1.1  lukem 	if (sblock.fs_ntrak <= 0)
    193  1.1  lukem 		printf("preposterous ntrak %d\n", sblock.fs_ntrak), exit(14);
    194  1.1  lukem 	if (sblock.fs_nsect <= 0)
    195  1.1  lukem 		printf("preposterous nsect %d\n", sblock.fs_nsect), exit(15);
    196  1.1  lukem 	/*
    197  1.1  lukem 	 * collect and verify the filesystem density info
    198  1.1  lukem 	 */
    199  1.1  lukem 	sblock.fs_avgfilesize = avgfilesize;
    200  1.1  lukem 	sblock.fs_avgfpdir = avgfpdir;
    201  1.1  lukem 	if (sblock.fs_avgfilesize <= 0)
    202  1.1  lukem 		printf("illegal expected average file size %d\n",
    203  1.1  lukem 		    sblock.fs_avgfilesize), exit(14);
    204  1.1  lukem 	if (sblock.fs_avgfpdir <= 0)
    205  1.1  lukem 		printf("illegal expected number of files per directory %d\n",
    206  1.1  lukem 		    sblock.fs_avgfpdir), exit(15);
    207  1.1  lukem 	/*
    208  1.1  lukem 	 * collect and verify the block and fragment sizes
    209  1.1  lukem 	 */
    210  1.1  lukem 	sblock.fs_bsize = bsize;
    211  1.1  lukem 	sblock.fs_fsize = fsize;
    212  1.1  lukem 	if (!POWEROF2(sblock.fs_bsize)) {
    213  1.1  lukem 		printf("block size must be a power of 2, not %d\n",
    214  1.1  lukem 		    sblock.fs_bsize);
    215  1.1  lukem 		exit(16);
    216  1.1  lukem 	}
    217  1.1  lukem 	if (!POWEROF2(sblock.fs_fsize)) {
    218  1.1  lukem 		printf("fragment size must be a power of 2, not %d\n",
    219  1.1  lukem 		    sblock.fs_fsize);
    220  1.1  lukem 		exit(17);
    221  1.1  lukem 	}
    222  1.1  lukem 	if (sblock.fs_fsize < sectorsize) {
    223  1.1  lukem 		printf("fragment size %d is too small, minimum is %d\n",
    224  1.1  lukem 		    sblock.fs_fsize, sectorsize);
    225  1.1  lukem 		exit(18);
    226  1.1  lukem 	}
    227  1.7  lukem 	if (sblock.fs_bsize > MAXBSIZE) {
    228  1.7  lukem 		printf("block size %d is too large, maximum is %d\n",
    229  1.7  lukem 		    sblock.fs_bsize, MAXBSIZE);
    230  1.7  lukem 		exit(19);
    231  1.7  lukem 	}
    232  1.1  lukem 	if (sblock.fs_bsize < MINBSIZE) {
    233  1.1  lukem 		printf("block size %d is too small, minimum is %d\n",
    234  1.1  lukem 		    sblock.fs_bsize, MINBSIZE);
    235  1.1  lukem 		exit(19);
    236  1.1  lukem 	}
    237  1.1  lukem 	if (sblock.fs_bsize < sblock.fs_fsize) {
    238  1.1  lukem 		printf("block size (%d) cannot be smaller than fragment size (%d)\n",
    239  1.1  lukem 		    sblock.fs_bsize, sblock.fs_fsize);
    240  1.1  lukem 		exit(20);
    241  1.1  lukem 	}
    242  1.1  lukem 	sblock.fs_bmask = ~(sblock.fs_bsize - 1);
    243  1.1  lukem 	sblock.fs_fmask = ~(sblock.fs_fsize - 1);
    244  1.1  lukem 	sblock.fs_qbmask = ~sblock.fs_bmask;
    245  1.1  lukem 	sblock.fs_qfmask = ~sblock.fs_fmask;
    246  1.1  lukem 	for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1)
    247  1.1  lukem 		sblock.fs_bshift++;
    248  1.1  lukem 	for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1)
    249  1.1  lukem 		sblock.fs_fshift++;
    250  1.1  lukem 	sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize);
    251  1.1  lukem 	for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1)
    252  1.1  lukem 		sblock.fs_fragshift++;
    253  1.1  lukem 	if (sblock.fs_frag > MAXFRAG) {
    254  1.1  lukem 		printf("fragment size %d is too small, "
    255  1.1  lukem 			"minimum with block size %d is %d\n",
    256  1.1  lukem 		    sblock.fs_fsize, sblock.fs_bsize,
    257  1.1  lukem 		    sblock.fs_bsize / MAXFRAG);
    258  1.1  lukem 		exit(21);
    259  1.1  lukem 	}
    260  1.1  lukem 	sblock.fs_nrpos = nrpos;
    261  1.1  lukem 	sblock.fs_nindir = sblock.fs_bsize / sizeof(daddr_t);
    262  1.1  lukem 	sblock.fs_inopb = sblock.fs_bsize / DINODE_SIZE;
    263  1.1  lukem 	sblock.fs_nspf = sblock.fs_fsize / sectorsize;
    264  1.1  lukem 	for (sblock.fs_fsbtodb = 0, i = NSPF(&sblock); i > 1; i >>= 1)
    265  1.1  lukem 		sblock.fs_fsbtodb++;
    266  1.1  lukem 	sblock.fs_sblkno =
    267  1.1  lukem 	    roundup(howmany(bbsize + sbsize, sblock.fs_fsize), sblock.fs_frag);
    268  1.1  lukem 	sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno +
    269  1.1  lukem 	    roundup(howmany(sbsize, sblock.fs_fsize), sblock.fs_frag));
    270  1.1  lukem 	sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag;
    271  1.1  lukem 	sblock.fs_cgoffset = roundup(
    272  1.1  lukem 	    howmany(sblock.fs_nsect, NSPF(&sblock)), sblock.fs_frag);
    273  1.1  lukem 	for (sblock.fs_cgmask = 0xffffffff, i = sblock.fs_ntrak; i > 1; i >>= 1)
    274  1.1  lukem 		sblock.fs_cgmask <<= 1;
    275  1.1  lukem 	if (!POWEROF2(sblock.fs_ntrak))
    276  1.1  lukem 		sblock.fs_cgmask <<= 1;
    277  1.1  lukem 	sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
    278  1.1  lukem 	for (sizepb = sblock.fs_bsize, i = 0; i < NIADDR; i++) {
    279  1.1  lukem 		sizepb *= NINDIR(&sblock);
    280  1.1  lukem 		sblock.fs_maxfilesize += sizepb;
    281  1.1  lukem 	}
    282  1.1  lukem 	/*
    283  1.1  lukem 	 * Validate specified/determined secpercyl
    284  1.1  lukem 	 * and calculate minimum cylinders per group.
    285  1.1  lukem 	 */
    286  1.1  lukem 	sblock.fs_spc = secpercyl;
    287  1.1  lukem 	for (sblock.fs_cpc = NSPB(&sblock), i = sblock.fs_spc;
    288  1.1  lukem 	     sblock.fs_cpc > 1 && (i & 1) == 0;
    289  1.1  lukem 	     sblock.fs_cpc >>= 1, i >>= 1)
    290  1.1  lukem 		/* void */;
    291  1.1  lukem 	mincpc = sblock.fs_cpc;
    292  1.1  lukem 	bpcg = sblock.fs_spc * sectorsize;
    293  1.1  lukem 	inospercg = roundup(bpcg / DINODE_SIZE, INOPB(&sblock));
    294  1.1  lukem 	if (inospercg > MAXIPG(&sblock))
    295  1.1  lukem 		inospercg = MAXIPG(&sblock);
    296  1.1  lukem 	used = (sblock.fs_iblkno + inospercg / INOPF(&sblock)) * NSPF(&sblock);
    297  1.1  lukem 	mincpgcnt = howmany(sblock.fs_cgoffset * (~sblock.fs_cgmask) + used,
    298  1.1  lukem 	    sblock.fs_spc);
    299  1.1  lukem 	mincpg = roundup(mincpgcnt, mincpc);
    300  1.1  lukem 	/*
    301  1.1  lukem 	 * Ensure that cylinder group with mincpg has enough space
    302  1.1  lukem 	 * for block maps.
    303  1.1  lukem 	 */
    304  1.1  lukem 	sblock.fs_cpg = mincpg;
    305  1.1  lukem 	sblock.fs_ipg = inospercg;
    306  1.1  lukem 	if (maxcontig > 1)
    307  1.1  lukem 		sblock.fs_contigsumsize = MIN(maxcontig, FS_MAXCONTIG);
    308  1.1  lukem 	mapcramped = 0;
    309  1.1  lukem 	while (CGSIZE(&sblock) > sblock.fs_bsize) {
    310  1.1  lukem 		mapcramped = 1;
    311  1.1  lukem 		if (sblock.fs_bsize < MAXBSIZE) {
    312  1.1  lukem 			sblock.fs_bsize <<= 1;
    313  1.1  lukem 			if ((i & 1) == 0) {
    314  1.1  lukem 				i >>= 1;
    315  1.1  lukem 			} else {
    316  1.1  lukem 				sblock.fs_cpc <<= 1;
    317  1.1  lukem 				mincpc <<= 1;
    318  1.1  lukem 				mincpg = roundup(mincpgcnt, mincpc);
    319  1.1  lukem 				sblock.fs_cpg = mincpg;
    320  1.1  lukem 			}
    321  1.1  lukem 			sblock.fs_frag <<= 1;
    322  1.1  lukem 			sblock.fs_fragshift += 1;
    323  1.1  lukem 			if (sblock.fs_frag <= MAXFRAG)
    324  1.1  lukem 				continue;
    325  1.1  lukem 		}
    326  1.1  lukem 		if (sblock.fs_fsize == sblock.fs_bsize) {
    327  1.1  lukem 			printf("There is no block size that");
    328  1.1  lukem 			printf(" can support this disk\n");
    329  1.1  lukem 			exit(22);
    330  1.1  lukem 		}
    331  1.1  lukem 		sblock.fs_frag >>= 1;
    332  1.1  lukem 		sblock.fs_fragshift -= 1;
    333  1.1  lukem 		sblock.fs_fsize <<= 1;
    334  1.1  lukem 		sblock.fs_nspf <<= 1;
    335  1.1  lukem 	}
    336  1.1  lukem 	/*
    337  1.1  lukem 	 * Ensure that cylinder group with mincpg has enough space for inodes.
    338  1.1  lukem 	 */
    339  1.1  lukem 	inodecramped = 0;
    340  1.1  lukem 	inospercg = calcipg(mincpg, bpcg, &usedb);
    341  1.1  lukem 	sblock.fs_ipg = inospercg;
    342  1.1  lukem 	while (inospercg > MAXIPG(&sblock)) {
    343  1.1  lukem 		inodecramped = 1;
    344  1.1  lukem 		if (mincpc == 1 || sblock.fs_frag == 1 ||
    345  1.1  lukem 		    sblock.fs_bsize == MINBSIZE)
    346  1.1  lukem 			break;
    347  1.1  lukem 		printf("With a block size of %d %s %d\n", sblock.fs_bsize,
    348  1.1  lukem 		       "minimum bytes per inode is",
    349  1.1  lukem 		       (int)((mincpg * (off_t)bpcg - usedb)
    350  1.1  lukem 			     / MAXIPG(&sblock) + 1));
    351  1.1  lukem 		sblock.fs_bsize >>= 1;
    352  1.1  lukem 		sblock.fs_frag >>= 1;
    353  1.1  lukem 		sblock.fs_fragshift -= 1;
    354  1.1  lukem 		mincpc >>= 1;
    355  1.1  lukem 		sblock.fs_cpg = roundup(mincpgcnt, mincpc);
    356  1.1  lukem 		if (CGSIZE(&sblock) > sblock.fs_bsize) {
    357  1.1  lukem 			sblock.fs_bsize <<= 1;
    358  1.1  lukem 			break;
    359  1.1  lukem 		}
    360  1.1  lukem 		mincpg = sblock.fs_cpg;
    361  1.1  lukem 		inospercg = calcipg(mincpg, bpcg, &usedb);
    362  1.1  lukem 		sblock.fs_ipg = inospercg;
    363  1.1  lukem 	}
    364  1.1  lukem 	if (inodecramped) {
    365  1.1  lukem 		if (inospercg > MAXIPG(&sblock)) {
    366  1.1  lukem 			printf("Minimum bytes per inode is %d\n",
    367  1.1  lukem 			       (int)((mincpg * (off_t)bpcg - usedb)
    368  1.1  lukem 				     / MAXIPG(&sblock) + 1));
    369  1.1  lukem 		} else if (!mapcramped) {
    370  1.1  lukem 			printf("With %d bytes per inode, ", density);
    371  1.1  lukem 			printf("minimum cylinders per group is %d\n", mincpg);
    372  1.1  lukem 		}
    373  1.1  lukem 	}
    374  1.1  lukem 	if (mapcramped) {
    375  1.1  lukem 		printf("With %d sectors per cylinder, ", sblock.fs_spc);
    376  1.1  lukem 		printf("minimum cylinders per group is %d\n", mincpg);
    377  1.1  lukem 	}
    378  1.1  lukem 	if (inodecramped || mapcramped) {
    379  1.1  lukem 		if (sblock.fs_bsize != bsize)
    380  1.1  lukem 			printf("%s to be changed from %d to %d\n",
    381  1.1  lukem 			    "This requires the block size",
    382  1.1  lukem 			    bsize, sblock.fs_bsize);
    383  1.1  lukem 		if (sblock.fs_fsize != fsize)
    384  1.1  lukem 			printf("\t%s to be changed from %d to %d\n",
    385  1.1  lukem 			    "and the fragment size",
    386  1.1  lukem 			    fsize, sblock.fs_fsize);
    387  1.1  lukem 		exit(23);
    388  1.1  lukem 	}
    389  1.1  lukem 	/*
    390  1.1  lukem 	 * Calculate the number of cylinders per group
    391  1.1  lukem 	 */
    392  1.1  lukem 	sblock.fs_cpg = cpg;
    393  1.1  lukem 	if (sblock.fs_cpg % mincpc != 0) {
    394  1.1  lukem 		printf("%s groups must have a multiple of %d cylinders\n",
    395  1.1  lukem 			cpgflg ? "Cylinder" : "Warning: cylinder", mincpc);
    396  1.1  lukem 		sblock.fs_cpg = roundup(sblock.fs_cpg, mincpc);
    397  1.1  lukem 		if (!cpgflg)
    398  1.1  lukem 			cpg = sblock.fs_cpg;
    399  1.1  lukem 	}
    400  1.1  lukem 	/*
    401  1.1  lukem 	 * Must ensure there is enough space for inodes.
    402  1.1  lukem 	 */
    403  1.1  lukem 	sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
    404  1.1  lukem 	while (sblock.fs_ipg > MAXIPG(&sblock)) {
    405  1.1  lukem 		inodecramped = 1;
    406  1.1  lukem 		sblock.fs_cpg -= mincpc;
    407  1.1  lukem 		sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
    408  1.1  lukem 	}
    409  1.1  lukem 	/*
    410  1.1  lukem 	 * Must ensure there is enough space to hold block map.
    411  1.1  lukem 	 */
    412  1.1  lukem 	while (CGSIZE(&sblock) > sblock.fs_bsize) {
    413  1.1  lukem 		mapcramped = 1;
    414  1.1  lukem 		sblock.fs_cpg -= mincpc;
    415  1.1  lukem 		sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
    416  1.1  lukem 	}
    417  1.1  lukem 	sblock.fs_fpg = (sblock.fs_cpg * sblock.fs_spc) / NSPF(&sblock);
    418  1.1  lukem 	if ((sblock.fs_cpg * sblock.fs_spc) % NSPB(&sblock) != 0) {
    419  1.1  lukem 		printf("panic (fs_cpg * fs_spc) %% NSPF != 0");
    420  1.1  lukem 		exit(24);
    421  1.1  lukem 	}
    422  1.1  lukem 	if (sblock.fs_cpg < mincpg) {
    423  1.1  lukem 		printf("cylinder groups must have at least %d cylinders\n",
    424  1.1  lukem 			mincpg);
    425  1.1  lukem 		exit(25);
    426  1.7  lukem 	} else if (sblock.fs_cpg != cpg && cpgflg) {
    427  1.7  lukem 		if (!mapcramped && !inodecramped)
    428  1.1  lukem 			exit(26);
    429  1.1  lukem 		if (mapcramped && inodecramped)
    430  1.1  lukem 			printf("Block size and bytes per inode restrict");
    431  1.1  lukem 		else if (mapcramped)
    432  1.1  lukem 			printf("Block size restricts");
    433  1.1  lukem 		else
    434  1.1  lukem 			printf("Bytes per inode restrict");
    435  1.1  lukem 		printf(" cylinders per group to %d.\n", sblock.fs_cpg);
    436  1.7  lukem 		exit(27);
    437  1.1  lukem 	}
    438  1.1  lukem 	sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock));
    439  1.1  lukem 	/*
    440  1.1  lukem 	 * Now have size for file system and nsect and ntrak.
    441  1.1  lukem 	 * Determine number of cylinders and blocks in the file system.
    442  1.1  lukem 	 */
    443  1.1  lukem 	sblock.fs_size = fssize = dbtofsb(&sblock, fssize);
    444  1.1  lukem 	sblock.fs_ncyl = fssize * NSPF(&sblock) / sblock.fs_spc;
    445  1.1  lukem 	if (fssize * NSPF(&sblock) > sblock.fs_ncyl * sblock.fs_spc) {
    446  1.1  lukem 		sblock.fs_ncyl++;
    447  1.1  lukem 		warned = 1;
    448  1.1  lukem 	}
    449  1.1  lukem 	if (sblock.fs_ncyl < 1) {
    450  1.1  lukem 		printf("file systems must have at least one cylinder\n");
    451  1.1  lukem 		exit(28);
    452  1.1  lukem 	}
    453  1.1  lukem 	/*
    454  1.1  lukem 	 * Determine feasability/values of rotational layout tables.
    455  1.1  lukem 	 *
    456  1.1  lukem 	 * The size of the rotational layout tables is limited by the
    457  1.1  lukem 	 * size of the superblock, SBSIZE. The amount of space available
    458  1.1  lukem 	 * for tables is calculated as (SBSIZE - sizeof (struct fs)).
    459  1.1  lukem 	 * The size of these tables is inversely proportional to the block
    460  1.1  lukem 	 * size of the file system. The size increases if sectors per track
    461  1.1  lukem 	 * are not powers of two, because more cylinders must be described
    462  1.1  lukem 	 * by the tables before the rotational pattern repeats (fs_cpc).
    463  1.1  lukem 	 */
    464  1.1  lukem 	sblock.fs_interleave = interleave;
    465  1.1  lukem 	sblock.fs_trackskew = trackskew;
    466  1.1  lukem 	sblock.fs_npsect = nphyssectors;
    467  1.1  lukem 	sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
    468  1.1  lukem 	sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs));
    469  1.1  lukem 	if (sblock.fs_ntrak == 1) {
    470  1.1  lukem 		sblock.fs_cpc = 0;
    471  1.1  lukem 		goto next;
    472  1.1  lukem 	}
    473  1.1  lukem 	postblsize = sblock.fs_nrpos * sblock.fs_cpc * sizeof(int16_t);
    474  1.1  lukem 	rotblsize = sblock.fs_cpc * sblock.fs_spc / NSPB(&sblock);
    475  1.1  lukem 	totalsbsize = sizeof(struct fs) + rotblsize;
    476  1.1  lukem 	if (sblock.fs_nrpos == 8 && sblock.fs_cpc <= 16) {
    477  1.1  lukem 		/* use old static table space */
    478  1.1  lukem 		sblock.fs_postbloff = (char *)(&sblock.fs_opostbl[0][0]) -
    479  1.1  lukem 		    (char *)(&sblock.fs_firstfield);
    480  1.1  lukem 		sblock.fs_rotbloff = &sblock.fs_space[0] -
    481  1.1  lukem 		    (u_char *)(&sblock.fs_firstfield);
    482  1.1  lukem 	} else {
    483  1.1  lukem 		/* use dynamic table space */
    484  1.1  lukem 		sblock.fs_postbloff = &sblock.fs_space[0] -
    485  1.1  lukem 		    (u_char *)(&sblock.fs_firstfield);
    486  1.1  lukem 		sblock.fs_rotbloff = sblock.fs_postbloff + postblsize;
    487  1.1  lukem 		totalsbsize += postblsize;
    488  1.1  lukem 	}
    489  1.1  lukem 	if (totalsbsize > SBSIZE ||
    490  1.1  lukem 	    sblock.fs_nsect > (1 << NBBY) * NSPB(&sblock)) {
    491  1.1  lukem 		printf("%s %s %d %s %d.%s",
    492  1.1  lukem 		    "Warning: insufficient space in super block for\n",
    493  1.1  lukem 		    "rotational layout tables with nsect", sblock.fs_nsect,
    494  1.1  lukem 		    "and ntrak", sblock.fs_ntrak,
    495  1.1  lukem 		    "\nFile system performance may be impaired.\n");
    496  1.1  lukem 		sblock.fs_cpc = 0;
    497  1.1  lukem 		goto next;
    498  1.1  lukem 	}
    499  1.1  lukem 	sblock.fs_sbsize = fragroundup(&sblock, totalsbsize);
    500  1.1  lukem 	/*
    501  1.1  lukem 	 * calculate the available blocks for each rotational position
    502  1.1  lukem 	 */
    503  1.1  lukem 	for (cylno = 0; cylno < sblock.fs_cpc; cylno++)
    504  1.1  lukem 		for (rpos = 0; rpos < sblock.fs_nrpos; rpos++)
    505  1.1  lukem 			fs_postbl(&sblock, cylno)[rpos] = -1;
    506  1.1  lukem 	for (i = (rotblsize - 1) * sblock.fs_frag;
    507  1.1  lukem 	     i >= 0; i -= sblock.fs_frag) {
    508  1.1  lukem 		cylno = cbtocylno(&sblock, i);
    509  1.1  lukem 		rpos = cbtorpos(&sblock, i);
    510  1.1  lukem 		blk = fragstoblks(&sblock, i);
    511  1.1  lukem 		if (fs_postbl(&sblock, cylno)[rpos] == -1)
    512  1.1  lukem 			fs_rotbl(&sblock)[blk] = 0;
    513  1.1  lukem 		else
    514  1.1  lukem 			fs_rotbl(&sblock)[blk] = fs_postbl(&sblock, cylno)[rpos] - blk;
    515  1.1  lukem 		fs_postbl(&sblock, cylno)[rpos] = blk;
    516  1.1  lukem 	}
    517  1.1  lukem next:
    518  1.1  lukem 	/*
    519  1.1  lukem 	 * Compute/validate number of cylinder groups.
    520  1.1  lukem 	 */
    521  1.1  lukem 	sblock.fs_ncg = sblock.fs_ncyl / sblock.fs_cpg;
    522  1.1  lukem 	if (sblock.fs_ncyl % sblock.fs_cpg)
    523  1.1  lukem 		sblock.fs_ncg++;
    524  1.1  lukem 	sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock);
    525  1.1  lukem 	i = MIN(~sblock.fs_cgmask, sblock.fs_ncg - 1);
    526  1.1  lukem 	if (cgdmin(&sblock, i) - cgbase(&sblock, i) >= sblock.fs_fpg) {
    527  1.1  lukem 		printf("inode blocks/cyl group (%d) >= data blocks (%d)\n",
    528  1.1  lukem 		    cgdmin(&sblock, i) - cgbase(&sblock, i) / sblock.fs_frag,
    529  1.1  lukem 		    sblock.fs_fpg / sblock.fs_frag);
    530  1.1  lukem 		printf("number of cylinders per cylinder group (%d) %s.\n",
    531  1.1  lukem 		    sblock.fs_cpg, "must be increased");
    532  1.1  lukem 		exit(29);
    533  1.1  lukem 	}
    534  1.1  lukem 	j = sblock.fs_ncg - 1;
    535  1.1  lukem 	if ((i = fssize - j * sblock.fs_fpg) < sblock.fs_fpg &&
    536  1.1  lukem 	    cgdmin(&sblock, j) - cgbase(&sblock, j) > i) {
    537  1.1  lukem 		if (j == 0) {
    538  1.1  lukem 			printf("File system must have at least %d sectors\n",
    539  1.1  lukem 			    NSPF(&sblock) *
    540  1.1  lukem 			    (cgdmin(&sblock, 0) + 3 * sblock.fs_frag));
    541  1.1  lukem 			exit(30);
    542  1.1  lukem 		}
    543  1.1  lukem 		printf("Warning: inode blocks/cyl group (%d) >= "
    544  1.1  lukem 			"data blocks (%d) in last\n",
    545  1.1  lukem 		    (cgdmin(&sblock, j) - cgbase(&sblock, j)) / sblock.fs_frag,
    546  1.1  lukem 		    i / sblock.fs_frag);
    547  1.1  lukem 		printf("    cylinder group. This implies %d sector(s) "
    548  1.1  lukem 			"cannot be allocated.\n",
    549  1.1  lukem 		    i * NSPF(&sblock));
    550  1.1  lukem 		sblock.fs_ncg--;
    551  1.1  lukem 		sblock.fs_ncyl -= sblock.fs_ncyl % sblock.fs_cpg;
    552  1.1  lukem 		sblock.fs_size = fssize = sblock.fs_ncyl * sblock.fs_spc /
    553  1.1  lukem 		    NSPF(&sblock);
    554  1.1  lukem 		warned = 0;
    555  1.1  lukem 	}
    556  1.1  lukem 	if (warned) {
    557  1.1  lukem 		printf("Warning: %d sector(s) in last cylinder unallocated\n",
    558  1.1  lukem 		    sblock.fs_spc -
    559  1.1  lukem 		    (fssize * NSPF(&sblock) - (sblock.fs_ncyl - 1)
    560  1.1  lukem 		    * sblock.fs_spc));
    561  1.1  lukem 	}
    562  1.1  lukem 	/*
    563  1.1  lukem 	 * fill in remaining fields of the super block
    564  1.1  lukem 	 */
    565  1.1  lukem 	sblock.fs_csaddr = cgdmin(&sblock, 0);
    566  1.1  lukem 	sblock.fs_cssize =
    567  1.1  lukem 	    fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
    568  1.1  lukem 	/*
    569  1.1  lukem 	 * The superblock fields 'fs_csmask' and 'fs_csshift' are no
    570  1.1  lukem 	 * longer used. However, we still initialise them so that the
    571  1.1  lukem 	 * filesystem remains compatible with old kernels.
    572  1.1  lukem 	 */
    573  1.1  lukem 	i = sblock.fs_bsize / sizeof(struct csum);
    574  1.1  lukem 	sblock.fs_csmask = ~(i - 1);
    575  1.1  lukem 	for (sblock.fs_csshift = 0; i > 1; i >>= 1)
    576  1.1  lukem 		sblock.fs_csshift++;
    577  1.1  lukem 
    578  1.1  lukem 	/*
    579  1.1  lukem 	 * Setup memory for temporary in-core cylgroup summaries.
    580  1.1  lukem 	 * Cribbed from ffs_mountfs().
    581  1.1  lukem 	 */
    582  1.1  lukem 	size = sblock.fs_cssize;
    583  1.1  lukem 	blks = howmany(size, sblock.fs_fsize);
    584  1.1  lukem 	if (sblock.fs_contigsumsize > 0)
    585  1.1  lukem 		size += sblock.fs_ncg * sizeof(int32_t);
    586  1.1  lukem 	if ((space = (char *)calloc(1, size)) == NULL)
    587  1.1  lukem 		err(1, "memory allocation error for cg summaries");
    588  1.1  lukem 	sblock.fs_csp = space;
    589  1.1  lukem 	space = (char *)space + sblock.fs_cssize;
    590  1.1  lukem 	if (sblock.fs_contigsumsize > 0) {
    591  1.1  lukem 		int32_t *lp;
    592  1.1  lukem 
    593  1.1  lukem 		sblock.fs_maxcluster = lp = space;
    594  1.1  lukem 		for (i = 0; i < sblock.fs_ncg; i++)
    595  1.1  lukem 			*lp++ = sblock.fs_contigsumsize;
    596  1.1  lukem 	}
    597  1.1  lukem 
    598  1.1  lukem 	sblock.fs_magic = FS_MAGIC;
    599  1.1  lukem 	sblock.fs_rotdelay = rotdelay;
    600  1.1  lukem 	sblock.fs_minfree = minfree;
    601  1.1  lukem 	sblock.fs_maxcontig = maxcontig;
    602  1.1  lukem 	sblock.fs_maxbpg = maxbpg;
    603  1.1  lukem 	sblock.fs_rps = rpm / 60;
    604  1.1  lukem 	sblock.fs_optim = opt;
    605  1.1  lukem 	sblock.fs_cgrotor = 0;
    606  1.1  lukem 	sblock.fs_cstotal.cs_ndir = 0;
    607  1.1  lukem 	sblock.fs_cstotal.cs_nbfree = 0;
    608  1.1  lukem 	sblock.fs_cstotal.cs_nifree = 0;
    609  1.1  lukem 	sblock.fs_cstotal.cs_nffree = 0;
    610  1.1  lukem 	sblock.fs_fmod = 0;
    611  1.1  lukem 	sblock.fs_clean = FS_ISCLEAN;
    612  1.1  lukem 	sblock.fs_ronly = 0;
    613  1.1  lukem 
    614  1.1  lukem 	/*
    615  1.1  lukem 	 * Dump out summary information about file system.
    616  1.1  lukem 	 */
    617  1.1  lukem 	printf("%s:\t%d sectors in %d %s of %d tracks, %d sectors\n",
    618  1.1  lukem 		    fsys, sblock.fs_size * NSPF(&sblock), sblock.fs_ncyl,
    619  1.1  lukem 		    "cylinders", sblock.fs_ntrak, sblock.fs_nsect);
    620  1.1  lukem #define B2MBFACTOR (1 / (1024.0 * 1024.0))
    621  1.1  lukem 	printf("\t%.1fMB in %d cyl groups (%d c/g, %.2fMB/g, %d i/g)\n",
    622  1.1  lukem 		    (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
    623  1.1  lukem 		    sblock.fs_ncg, sblock.fs_cpg,
    624  1.1  lukem 		    (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
    625  1.1  lukem 		    sblock.fs_ipg);
    626  1.1  lukem #undef B2MBFACTOR
    627  1.1  lukem 	/*
    628  1.1  lukem 	 * Now determine how wide each column will be, and calculate how
    629  1.1  lukem 	 * many columns will fit in a 76 char line. 76 is the width of the
    630  1.1  lukem 	 * subwindows in sysinst.
    631  1.1  lukem 	 */
    632  1.1  lukem 	printcolwidth = count_digits(
    633  1.1  lukem 			fsbtodb(&sblock, cgsblock(&sblock, sblock.fs_ncg -1)));
    634  1.1  lukem 	nprintcols = 76 / (printcolwidth + 2);
    635  1.1  lukem 	/*
    636  1.1  lukem 	 * Now build the cylinders group blocks and
    637  1.1  lukem 	 * then print out indices of cylinder groups.
    638  1.1  lukem 	 */
    639  1.1  lukem 		printf("super-block backups (for fsck -b #) at:");
    640  1.1  lukem 	for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
    641  1.1  lukem 		initcg(cylno, start_time.tv_sec, fsopts);
    642  1.1  lukem 		if (cylno % nprintcols == 0)
    643  1.1  lukem 			printf("\n");
    644  1.1  lukem 		printf(" %*d,", printcolwidth,
    645  1.1  lukem 				fsbtodb(&sblock, cgsblock(&sblock, cylno)));
    646  1.1  lukem 		fflush(stdout);
    647  1.1  lukem 	}
    648  1.1  lukem 	printf("\n");
    649  1.1  lukem 
    650  1.1  lukem 	/*
    651  1.1  lukem 	 * Now construct the initial file system,
    652  1.1  lukem 	 * then write out the super-block.
    653  1.1  lukem 	 */
    654  1.1  lukem 	sblock.fs_time = start_time.tv_sec;
    655  1.1  lukem 	if (fsopts->needswap)
    656  1.1  lukem 		sblock.fs_flags |= FS_SWAPPED;
    657  1.1  lukem 	ffs_write_superblock(&sblock, fsopts);
    658  1.1  lukem 	return (&sblock);
    659  1.1  lukem }
    660  1.1  lukem 
    661  1.1  lukem /*
    662  1.1  lukem  * Write out the superblock and its duplicates,
    663  1.1  lukem  * and the cylinder group summaries
    664  1.1  lukem  */
    665  1.1  lukem void
    666  1.1  lukem ffs_write_superblock(struct fs *fs, const fsinfo_t *fsopts)
    667  1.1  lukem {
    668  1.1  lukem 	int	cylno, size, blks, i, saveflag;
    669  1.1  lukem 	void	*space;
    670  1.1  lukem 	char	*wrbuf;
    671  1.1  lukem 
    672  1.1  lukem 	saveflag = fs->fs_flags & FS_INTERNAL;
    673  1.1  lukem 	fs->fs_flags &= ~FS_INTERNAL;
    674  1.1  lukem 
    675  1.1  lukem 			/* Write out the master super block */
    676  1.1  lukem 	memcpy(writebuf, fs, sbsize);
    677  1.1  lukem 	if (fsopts->needswap)
    678  1.1  lukem 		ffs_sb_swap(fs, (struct fs*)writebuf);
    679  1.1  lukem 	ffs_wtfs((int)SBOFF / sectorsize, sbsize, writebuf, fsopts);
    680  1.1  lukem 
    681  1.1  lukem 			/* Write out the duplicate super blocks */
    682  1.1  lukem 	for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
    683  1.1  lukem 		ffs_wtfs(fsbtodb(fs, cgsblock(fs, cylno)),
    684  1.1  lukem 		    sbsize, writebuf, fsopts);
    685  1.1  lukem 
    686  1.1  lukem 			/* Write out the cylinder group summaries */
    687  1.1  lukem 	size = fs->fs_cssize;
    688  1.1  lukem 	blks = howmany(size, fs->fs_fsize);
    689  1.1  lukem 	space = (void *)fs->fs_csp;
    690  1.1  lukem 	if ((wrbuf = malloc(size)) == NULL)
    691  1.1  lukem 		err(1, "ffs_write_superblock: malloc %d", size);
    692  1.1  lukem 	for (i = 0; i < blks; i+= fs->fs_frag) {
    693  1.1  lukem 		size = fs->fs_bsize;
    694  1.1  lukem 		if (i + fs->fs_frag > blks)
    695  1.1  lukem 			size = (blks - i) * fs->fs_fsize;
    696  1.1  lukem 		if (fsopts->needswap)
    697  1.1  lukem 			ffs_csum_swap((struct csum *)space,
    698  1.1  lukem 			    (struct csum *)wrbuf, size);
    699  1.1  lukem 		else
    700  1.1  lukem 			memcpy(wrbuf, space, (u_int)size);
    701  1.1  lukem 		ffs_wtfs(fsbtodb(fs, fs->fs_csaddr + i), size, wrbuf, fsopts);
    702  1.1  lukem 		space = (char *)space + size;
    703  1.1  lukem 	}
    704  1.1  lukem 	free(wrbuf);
    705  1.1  lukem 	fs->fs_flags |= saveflag;
    706  1.1  lukem }
    707  1.1  lukem 
    708  1.1  lukem 
    709  1.1  lukem /*
    710  1.1  lukem  * Initialize a cylinder group.
    711  1.1  lukem  */
    712  1.1  lukem static void
    713  1.1  lukem initcg(int cylno, time_t utime, const fsinfo_t *fsopts)
    714  1.1  lukem {
    715  1.1  lukem 	daddr_t cbase, d, dlower, dupper, dmax, blkno;
    716  1.1  lukem 	int32_t i;
    717  1.1  lukem 
    718  1.1  lukem 	/*
    719  1.1  lukem 	 * Determine block bounds for cylinder group.
    720  1.1  lukem 	 * Allow space for super block summary information in first
    721  1.1  lukem 	 * cylinder group.
    722  1.1  lukem 	 */
    723  1.1  lukem 	cbase = cgbase(&sblock, cylno);
    724  1.1  lukem 	dmax = cbase + sblock.fs_fpg;
    725  1.1  lukem 	if (dmax > sblock.fs_size)
    726  1.1  lukem 		dmax = sblock.fs_size;
    727  1.1  lukem 	dlower = cgsblock(&sblock, cylno) - cbase;
    728  1.1  lukem 	dupper = cgdmin(&sblock, cylno) - cbase;
    729  1.1  lukem 	if (cylno == 0)
    730  1.1  lukem 		dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
    731  1.1  lukem 	memset(&acg, 0, sblock.fs_cgsize);
    732  1.1  lukem 	acg.cg_time = utime;
    733  1.1  lukem 	acg.cg_magic = CG_MAGIC;
    734  1.1  lukem 	acg.cg_cgx = cylno;
    735  1.1  lukem 	if (cylno == sblock.fs_ncg - 1)
    736  1.1  lukem 		acg.cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg;
    737  1.1  lukem 	else
    738  1.1  lukem 		acg.cg_ncyl = sblock.fs_cpg;
    739  1.1  lukem 	acg.cg_niblk = sblock.fs_ipg;
    740  1.1  lukem 	acg.cg_ndblk = dmax - cbase;
    741  1.1  lukem 	if (sblock.fs_contigsumsize > 0)
    742  1.1  lukem 		acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag;
    743  1.1  lukem 	acg.cg_btotoff = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield);
    744  1.1  lukem 	acg.cg_boff = acg.cg_btotoff + sblock.fs_cpg * sizeof(int32_t);
    745  1.1  lukem 	acg.cg_iusedoff = acg.cg_boff +
    746  1.1  lukem 		sblock.fs_cpg * sblock.fs_nrpos * sizeof(int16_t);
    747  1.1  lukem 	acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, NBBY);
    748  1.1  lukem 	if (sblock.fs_contigsumsize <= 0) {
    749  1.1  lukem 		acg.cg_nextfreeoff = acg.cg_freeoff +
    750  1.1  lukem 		   howmany(sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY);
    751  1.1  lukem 	} else {
    752  1.1  lukem 		acg.cg_clustersumoff = acg.cg_freeoff + howmany
    753  1.1  lukem 		    (sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY) -
    754  1.1  lukem 		    sizeof(int32_t);
    755  1.1  lukem 		acg.cg_clustersumoff =
    756  1.1  lukem 		    roundup(acg.cg_clustersumoff, sizeof(int32_t));
    757  1.1  lukem 		acg.cg_clusteroff = acg.cg_clustersumoff +
    758  1.1  lukem 		    (sblock.fs_contigsumsize + 1) * sizeof(int32_t);
    759  1.1  lukem 		acg.cg_nextfreeoff = acg.cg_clusteroff + howmany
    760  1.1  lukem 		    (sblock.fs_cpg * sblock.fs_spc / NSPB(&sblock), NBBY);
    761  1.1  lukem 	}
    762  1.1  lukem 	if (acg.cg_nextfreeoff > sblock.fs_cgsize) {
    763  1.1  lukem 		printf("Panic: cylinder group too big\n");
    764  1.1  lukem 		exit(37);
    765  1.1  lukem 	}
    766  1.1  lukem 	acg.cg_cs.cs_nifree += sblock.fs_ipg;
    767  1.1  lukem 	if (cylno == 0)
    768  1.1  lukem 		for (i = 0; i < ROOTINO; i++) {
    769  1.1  lukem 			setbit(cg_inosused(&acg, 0), i);
    770  1.1  lukem 			acg.cg_cs.cs_nifree--;
    771  1.1  lukem 		}
    772  1.1  lukem 	for (i = 0; i < sblock.fs_ipg / INOPF(&sblock); i += sblock.fs_frag)
    773  1.1  lukem 		ffs_wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
    774  1.1  lukem 		    sblock.fs_bsize, (char *)zino, fsopts);
    775  1.1  lukem 	if (cylno > 0) {
    776  1.1  lukem 		/*
    777  1.1  lukem 		 * In cylno 0, beginning space is reserved
    778  1.1  lukem 		 * for boot and super blocks.
    779  1.1  lukem 		 */
    780  1.1  lukem 		for (d = 0; d < dlower; d += sblock.fs_frag) {
    781  1.1  lukem 			blkno = d / sblock.fs_frag;
    782  1.1  lukem 			ffs_setblock(&sblock, cg_blksfree(&acg, 0), blkno);
    783  1.1  lukem 			if (sblock.fs_contigsumsize > 0)
    784  1.1  lukem 				setbit(cg_clustersfree(&acg, 0), blkno);
    785  1.1  lukem 			acg.cg_cs.cs_nbfree++;
    786  1.1  lukem 			cg_blktot(&acg, 0)[cbtocylno(&sblock, d)]++;
    787  1.1  lukem 			cg_blks(&sblock, &acg, cbtocylno(&sblock, d), 0)
    788  1.1  lukem 			    [cbtorpos(&sblock, d)]++;
    789  1.1  lukem 		}
    790  1.1  lukem 		sblock.fs_dsize += dlower;
    791  1.1  lukem 	}
    792  1.1  lukem 	sblock.fs_dsize += acg.cg_ndblk - dupper;
    793  1.1  lukem 	if ((i = (dupper % sblock.fs_frag)) != 0) {
    794  1.1  lukem 		acg.cg_frsum[sblock.fs_frag - i]++;
    795  1.1  lukem 		for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
    796  1.1  lukem 			setbit(cg_blksfree(&acg, 0), dupper);
    797  1.1  lukem 			acg.cg_cs.cs_nffree++;
    798  1.1  lukem 		}
    799  1.1  lukem 	}
    800  1.1  lukem 	for (d = dupper; d + sblock.fs_frag <= dmax - cbase; ) {
    801  1.1  lukem 		blkno = d / sblock.fs_frag;
    802  1.1  lukem 		ffs_setblock(&sblock, cg_blksfree(&acg, 0), blkno);
    803  1.1  lukem 		if (sblock.fs_contigsumsize > 0)
    804  1.1  lukem 			setbit(cg_clustersfree(&acg, 0), blkno);
    805  1.1  lukem 		acg.cg_cs.cs_nbfree++;
    806  1.1  lukem 		cg_blktot(&acg, 0)[cbtocylno(&sblock, d)]++;
    807  1.1  lukem 		cg_blks(&sblock, &acg, cbtocylno(&sblock, d), 0)
    808  1.1  lukem 		    [cbtorpos(&sblock, d)]++;
    809  1.1  lukem 		d += sblock.fs_frag;
    810  1.1  lukem 	}
    811  1.1  lukem 	if (d < dmax - cbase) {
    812  1.1  lukem 		acg.cg_frsum[dmax - cbase - d]++;
    813  1.1  lukem 		for (; d < dmax - cbase; d++) {
    814  1.1  lukem 			setbit(cg_blksfree(&acg, 0), d);
    815  1.1  lukem 			acg.cg_cs.cs_nffree++;
    816  1.1  lukem 		}
    817  1.1  lukem 	}
    818  1.1  lukem 	if (sblock.fs_contigsumsize > 0) {
    819  1.1  lukem 		int32_t *sump = cg_clustersum(&acg, 0);
    820  1.1  lukem 		u_char *mapp = cg_clustersfree(&acg, 0);
    821  1.1  lukem 		int map = *mapp++;
    822  1.1  lukem 		int bit = 1;
    823  1.1  lukem 		int run = 0;
    824  1.1  lukem 
    825  1.1  lukem 		for (i = 0; i < acg.cg_nclusterblks; i++) {
    826  1.1  lukem 			if ((map & bit) != 0) {
    827  1.1  lukem 				run++;
    828  1.1  lukem 			} else if (run != 0) {
    829  1.1  lukem 				if (run > sblock.fs_contigsumsize)
    830  1.1  lukem 					run = sblock.fs_contigsumsize;
    831  1.1  lukem 				sump[run]++;
    832  1.1  lukem 				run = 0;
    833  1.1  lukem 			}
    834  1.1  lukem 			if ((i & (NBBY - 1)) != (NBBY - 1)) {
    835  1.1  lukem 				bit <<= 1;
    836  1.1  lukem 			} else {
    837  1.1  lukem 				map = *mapp++;
    838  1.1  lukem 				bit = 1;
    839  1.1  lukem 			}
    840  1.1  lukem 		}
    841  1.1  lukem 		if (run != 0) {
    842  1.1  lukem 			if (run > sblock.fs_contigsumsize)
    843  1.1  lukem 				run = sblock.fs_contigsumsize;
    844  1.1  lukem 			sump[run]++;
    845  1.1  lukem 		}
    846  1.1  lukem 	}
    847  1.1  lukem 	sblock.fs_cstotal.cs_ndir += acg.cg_cs.cs_ndir;
    848  1.1  lukem 	sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree;
    849  1.1  lukem 	sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree;
    850  1.1  lukem 	sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree;
    851  1.1  lukem 	sblock.fs_cs(&sblock, cylno) = acg.cg_cs;
    852  1.1  lukem 	memcpy(writebuf, &acg, sblock.fs_bsize);
    853  1.1  lukem 	if (fsopts->needswap)
    854  1.1  lukem 		swap_cg(&acg, (struct cg*)writebuf);
    855  1.1  lukem 	ffs_wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
    856  1.1  lukem 		sblock.fs_bsize,
    857  1.1  lukem 		writebuf, fsopts);
    858  1.1  lukem }
    859  1.1  lukem 
    860  1.1  lukem /*
    861  1.1  lukem  * Calculate number of inodes per group.
    862  1.1  lukem  */
    863  1.1  lukem static int32_t
    864  1.1  lukem calcipg(int32_t cylpg, int32_t bpcg, off_t *usedbp)
    865  1.1  lukem {
    866  1.1  lukem 	int i;
    867  1.1  lukem 	int32_t ipg, new_ipg, ncg, ncyl;
    868  1.1  lukem 	off_t usedb;
    869  1.1  lukem 
    870  1.1  lukem 	/*
    871  1.1  lukem 	 * Prepare to scale by fssize / (number of sectors in cylinder groups).
    872  1.1  lukem 	 * Note that fssize is still in sectors, not file system blocks.
    873  1.1  lukem 	 */
    874  1.1  lukem 	ncyl = howmany(fssize, secpercyl);
    875  1.1  lukem 	ncg = howmany(ncyl, cylpg);
    876  1.1  lukem 	/*
    877  1.1  lukem 	 * Iterate a few times to allow for ipg depending on itself.
    878  1.1  lukem 	 */
    879  1.1  lukem 	ipg = 0;
    880  1.1  lukem 	for (i = 0; i < 10; i++) {
    881  1.1  lukem 		usedb = (sblock.fs_iblkno + ipg / INOPF(&sblock))
    882  1.1  lukem 			* NSPF(&sblock) * (off_t)sectorsize;
    883  1.1  lukem 		if (cylpg * (long long)bpcg < usedb) {
    884  1.1  lukem 			warnx("Too many inodes per cyl group!");
    885  1.1  lukem 			return (MAXIPG(&sblock)+1);
    886  1.1  lukem 		}
    887  1.1  lukem 		new_ipg = (cylpg * (long long)bpcg - usedb) /
    888  1.7  lukem 		    (long long)density * fssize / (ncg * secpercyl * cylpg);
    889  1.1  lukem 		if (new_ipg <= 0)
    890  1.1  lukem 			new_ipg = 1;		/* ensure ipg > 0 */
    891  1.1  lukem 		new_ipg = roundup(new_ipg, INOPB(&sblock));
    892  1.1  lukem 		if (new_ipg == ipg)
    893  1.1  lukem 			break;
    894  1.1  lukem 		ipg = new_ipg;
    895  1.1  lukem 	}
    896  1.1  lukem 	*usedbp = usedb;
    897  1.1  lukem 	return (ipg);
    898  1.1  lukem }
    899  1.1  lukem 
    900  1.1  lukem 
    901  1.1  lukem /*
    902  1.1  lukem  * read a block from the file system
    903  1.1  lukem  */
    904  1.1  lukem void
    905  1.1  lukem ffs_rdfs(daddr_t bno, int size, void *bf, const fsinfo_t *fsopts)
    906  1.1  lukem {
    907  1.1  lukem 	int n;
    908  1.1  lukem 	off_t offset;
    909  1.1  lukem 
    910  1.1  lukem 	offset = bno;
    911  1.1  lukem 	offset *= fsopts->sectorsize;
    912  1.1  lukem 	if (lseek(fsopts->fd, offset, SEEK_SET) < 0)
    913  1.1  lukem 		err(1, "ffs_rdfs: seek error: %d\n", bno);
    914  1.1  lukem 	n = read(fsopts->fd, bf, size);
    915  1.1  lukem 	if (n == -1)
    916  1.1  lukem 		err(1, "ffs_rdfs: read error bno %d size %d\n", bno, size);
    917  1.1  lukem 	else if (n != size)
    918  1.1  lukem 		errx(1,
    919  1.1  lukem 		    "ffs_rdfs: read error bno %d size %d: short read of %d\n",
    920  1.1  lukem 		    bno, size, n);
    921  1.1  lukem }
    922  1.1  lukem 
    923  1.1  lukem /*
    924  1.1  lukem  * write a block to the file system
    925  1.1  lukem  */
    926  1.1  lukem void
    927  1.1  lukem ffs_wtfs(daddr_t bno, int size, void *bf, const fsinfo_t *fsopts)
    928  1.1  lukem {
    929  1.1  lukem 	int n;
    930  1.1  lukem 	off_t offset;
    931  1.1  lukem 
    932  1.1  lukem 	offset = bno;
    933  1.1  lukem 	offset *= fsopts->sectorsize;
    934  1.1  lukem 	if (lseek(fsopts->fd, offset, SEEK_SET) < 0)
    935  1.1  lukem 		err(1, "ffs_wtfs: seek error: %d\n", bno);
    936  1.1  lukem 	n = write(fsopts->fd, bf, size);
    937  1.1  lukem 	if (n == -1)
    938  1.1  lukem 		err(1, "ffs_wtfs: write error bno %d size %d\n", bno, size);
    939  1.1  lukem 	else if (n != size)
    940  1.1  lukem 		errx(1,
    941  1.1  lukem 		    "ffs_wtfs: write error bno %d size %d: short write of %d\n",
    942  1.1  lukem 		    bno, size, n);
    943  1.1  lukem }
    944  1.1  lukem 
    945  1.1  lukem /* swap byte order of cylinder group */
    946  1.1  lukem static void
    947  1.1  lukem swap_cg(struct cg *o, struct cg *n)
    948  1.1  lukem {
    949  1.1  lukem 	int i, btotsize, fbsize;
    950  1.1  lukem 	u_int32_t *n32, *o32;
    951  1.1  lukem 	u_int16_t *n16, *o16;
    952  1.1  lukem 
    953  1.1  lukem 	n->cg_firstfield = bswap32(o->cg_firstfield);
    954  1.1  lukem 	n->cg_magic = bswap32(o->cg_magic);
    955  1.1  lukem 	n->cg_time = bswap32(o->cg_time);
    956  1.1  lukem 	n->cg_cgx = bswap32(o->cg_cgx);
    957  1.1  lukem 	n->cg_ncyl = bswap16(o->cg_ncyl);
    958  1.1  lukem 	n->cg_niblk = bswap16(o->cg_niblk);
    959  1.1  lukem 	n->cg_ndblk = bswap32(o->cg_ndblk);
    960  1.1  lukem 	n->cg_cs.cs_ndir = bswap32(o->cg_cs.cs_ndir);
    961  1.1  lukem 	n->cg_cs.cs_nbfree = bswap32(o->cg_cs.cs_nbfree);
    962  1.1  lukem 	n->cg_cs.cs_nifree = bswap32(o->cg_cs.cs_nifree);
    963  1.1  lukem 	n->cg_cs.cs_nffree = bswap32(o->cg_cs.cs_nffree);
    964  1.1  lukem 	n->cg_rotor = bswap32(o->cg_rotor);
    965  1.1  lukem 	n->cg_frotor = bswap32(o->cg_frotor);
    966  1.1  lukem 	n->cg_irotor = bswap32(o->cg_irotor);
    967  1.1  lukem 	n->cg_btotoff = bswap32(o->cg_btotoff);
    968  1.1  lukem 	n->cg_boff = bswap32(o->cg_boff);
    969  1.1  lukem 	n->cg_iusedoff = bswap32(o->cg_iusedoff);
    970  1.1  lukem 	n->cg_freeoff = bswap32(o->cg_freeoff);
    971  1.1  lukem 	n->cg_nextfreeoff = bswap32(o->cg_nextfreeoff);
    972  1.1  lukem 	n->cg_clustersumoff = bswap32(o->cg_clustersumoff);
    973  1.1  lukem 	n->cg_clusteroff = bswap32(o->cg_clusteroff);
    974  1.1  lukem 	n->cg_nclusterblks = bswap32(o->cg_nclusterblks);
    975  1.1  lukem 	for (i=0; i < MAXFRAG; i++)
    976  1.1  lukem 		n->cg_frsum[i] = bswap32(o->cg_frsum[i]);
    977  1.1  lukem 
    978  1.1  lukem 	/* alays new format */
    979  1.1  lukem 	if (n->cg_magic == CG_MAGIC) {
    980  1.1  lukem 		btotsize = n->cg_boff - n->cg_btotoff;
    981  1.1  lukem 		fbsize = n->cg_iusedoff - n->cg_boff;
    982  1.1  lukem 		n32 = (u_int32_t*)((u_int8_t*)n + n->cg_btotoff);
    983  1.1  lukem 		o32 = (u_int32_t*)((u_int8_t*)o + n->cg_btotoff);
    984  1.1  lukem 		n16 = (u_int16_t*)((u_int8_t*)n + n->cg_boff);
    985  1.1  lukem 		o16 = (u_int16_t*)((u_int8_t*)o + n->cg_boff);
    986  1.1  lukem 	} else {
    987  1.1  lukem 		btotsize = bswap32(n->cg_boff) - bswap32(n->cg_btotoff);
    988  1.1  lukem 		fbsize = bswap32(n->cg_iusedoff) - bswap32(n->cg_boff);
    989  1.1  lukem 		n32 = (u_int32_t*)((u_int8_t*)n + bswap32(n->cg_btotoff));
    990  1.1  lukem 		o32 = (u_int32_t*)((u_int8_t*)o + bswap32(n->cg_btotoff));
    991  1.1  lukem 		n16 = (u_int16_t*)((u_int8_t*)n + bswap32(n->cg_boff));
    992  1.1  lukem 		o16 = (u_int16_t*)((u_int8_t*)o + bswap32(n->cg_boff));
    993  1.1  lukem 	}
    994  1.1  lukem 	for (i=0; i < btotsize / sizeof(u_int32_t); i++)
    995  1.1  lukem 		n32[i] = bswap32(o32[i]);
    996  1.1  lukem 
    997  1.1  lukem 	for (i=0; i < fbsize/sizeof(u_int16_t); i++)
    998  1.1  lukem 		n16[i] = bswap16(o16[i]);
    999  1.1  lukem 
   1000  1.1  lukem 	if (n->cg_magic == CG_MAGIC) {
   1001  1.1  lukem 		n32 = (u_int32_t*)((u_int8_t*)n + n->cg_clustersumoff);
   1002  1.1  lukem 		o32 = (u_int32_t*)((u_int8_t*)o + n->cg_clustersumoff);
   1003  1.1  lukem 	} else {
   1004  1.1  lukem 		n32 = (u_int32_t*)((u_int8_t*)n + bswap32(n->cg_clustersumoff));
   1005  1.1  lukem 		o32 = (u_int32_t*)((u_int8_t*)o + bswap32(n->cg_clustersumoff));
   1006  1.1  lukem 	}
   1007  1.1  lukem 	for (i = 1; i < sblock.fs_contigsumsize + 1; i++)
   1008  1.1  lukem 		n32[i] = bswap32(o32[i]);
   1009  1.1  lukem }
   1010  1.1  lukem 
   1011  1.1  lukem /* Determine how many digits are needed to print a given integer */
   1012  1.1  lukem static int
   1013  1.1  lukem count_digits(int num)
   1014  1.1  lukem {
   1015  1.1  lukem 	int ndig;
   1016  1.1  lukem 
   1017  1.1  lukem 	for(ndig = 1; num > 9; num /=10, ndig++);
   1018  1.1  lukem 
   1019  1.1  lukem 	return (ndig);
   1020  1.1  lukem }
   1021