Home | History | Annotate | Line # | Download | only in ffs
mkfs.c revision 1.12
      1  1.12   fvdl /*	$NetBSD: mkfs.c,v 1.12 2003/01/24 21:55:33 fvdl 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.9     tv #if defined(__RCSID) && !defined(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.12   fvdl __RCSID("$NetBSD: mkfs.c,v 1.12 2003/01/24 21:55:33 fvdl 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 <stdio.h>
     51   1.1  lukem #include <stdlib.h>
     52   1.1  lukem #include <string.h>
     53   1.1  lukem #include <unistd.h>
     54   1.1  lukem 
     55   1.3  lukem #include "makefs.h"
     56   1.3  lukem 
     57   1.6  lukem #include <ufs/ufs/dinode.h>
     58   1.4  lukem #include <ufs/ufs/ufs_bswap.h>
     59   1.4  lukem #include <ufs/ffs/fs.h>
     60   1.1  lukem 
     61   1.5  lukem #include "ffs/ufs_inode.h"
     62   1.1  lukem #include "ffs/ffs_extern.h"
     63   1.1  lukem #include "ffs/newfs_extern.h"
     64   1.1  lukem 
     65   1.1  lukem static void initcg(int, time_t, const fsinfo_t *);
     66   1.1  lukem static int32_t calcipg(int32_t, int32_t, off_t *);
     67   1.1  lukem static void swap_cg(struct cg *, struct cg *);
     68   1.1  lukem 
     69   1.1  lukem static int count_digits(int);
     70   1.1  lukem 
     71   1.1  lukem /*
     72   1.1  lukem  * make file system for cylinder-group style file systems
     73   1.1  lukem  */
     74   1.1  lukem 
     75   1.1  lukem /*
     76   1.1  lukem  * We limit the size of the inode map to be no more than a
     77   1.1  lukem  * third of the cylinder group space, since we must leave at
     78   1.1  lukem  * least an equal amount of space for the block map.
     79   1.1  lukem  *
     80   1.1  lukem  * N.B.: MAXIPG must be a multiple of INOPB(fs).
     81   1.1  lukem  */
     82   1.1  lukem #define MAXIPG(fs)	roundup((fs)->fs_bsize * NBBY / 3, INOPB(fs))
     83   1.1  lukem 
     84   1.1  lukem #define UMASK		0755
     85   1.1  lukem #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
     86   1.1  lukem 
     87   1.1  lukem union {
     88   1.1  lukem 	struct fs fs;
     89   1.1  lukem 	char pad[SBSIZE];
     90   1.1  lukem } fsun;
     91   1.1  lukem #define	sblock	fsun.fs
     92   1.1  lukem 
     93   1.1  lukem union {
     94   1.1  lukem 	struct cg cg;
     95   1.1  lukem 	char pad[MAXBSIZE];
     96   1.1  lukem } cgun;
     97   1.1  lukem #define	acg	cgun.cg
     98   1.1  lukem 
     99   1.1  lukem struct dinode zino[MAXBSIZE / DINODE_SIZE];
    100   1.1  lukem 
    101   1.1  lukem char writebuf[MAXBSIZE];
    102   1.1  lukem 
    103   1.1  lukem static	int	Oflag;		/* format as an 4.3BSD file system */
    104   1.1  lukem static	int	fssize;		/* file system size */
    105   1.1  lukem static	int	ntracks;	/* # tracks/cylinder */
    106   1.1  lukem static	int	nsectors;	/* # sectors/track */
    107   1.1  lukem static	int	nphyssectors;	/* # sectors/track including spares */
    108   1.1  lukem static	int	secpercyl;	/* sectors per cylinder */
    109   1.1  lukem static	int	sectorsize;	/* bytes/sector */
    110   1.1  lukem static	int	rpm;		/* revolutions/minute of drive */
    111   1.1  lukem static	int	interleave;	/* hardware sector interleave */
    112   1.1  lukem static	int	trackskew;	/* sector 0 skew, per track */
    113   1.1  lukem static	int	fsize;		/* fragment size */
    114   1.1  lukem static	int	bsize;		/* block size */
    115   1.1  lukem static	int	cpg;		/* cylinders/cylinder group */
    116   1.1  lukem static	int	cpgflg;		/* cylinders/cylinder group flag was given */
    117   1.1  lukem static	int	minfree;	/* free space threshold */
    118   1.1  lukem static	int	opt;		/* optimization preference (space or time) */
    119   1.1  lukem static	int	density;	/* number of bytes per inode */
    120   1.1  lukem static	int	maxcontig;	/* max contiguous blocks to allocate */
    121   1.1  lukem static	int	rotdelay;	/* rotational delay between blocks */
    122   1.1  lukem static	int	maxbpg;		/* maximum blocks per file in a cyl group */
    123   1.1  lukem static	int	nrpos;		/* # of distinguished rotational positions */
    124   1.1  lukem static	int	bbsize;		/* boot block size */
    125   1.1  lukem static	int	sbsize;		/* superblock size */
    126   1.1  lukem static	int	avgfilesize;	/* expected average file size */
    127   1.1  lukem static	int	avgfpdir;	/* expected number of files per directory */
    128   1.1  lukem 
    129   1.1  lukem 
    130   1.1  lukem struct fs *
    131   1.1  lukem ffs_mkfs(const char *fsys, const fsinfo_t *fsopts)
    132   1.1  lukem {
    133   1.1  lukem 	int32_t i, mincpc, mincpg, inospercg;
    134   1.1  lukem 	int32_t cylno, rpos, blk, j, warned = 0;
    135   1.1  lukem 	int32_t used, mincpgcnt, bpcg;
    136   1.1  lukem 	off_t usedb;
    137   1.1  lukem 	int32_t mapcramped, inodecramped;
    138   1.1  lukem 	int32_t postblsize, rotblsize, totalsbsize;
    139   1.1  lukem 	long long sizepb;
    140   1.1  lukem 	void *space;
    141   1.1  lukem 	int size, blks;
    142   1.1  lukem 	int nprintcols, printcolwidth;
    143   1.1  lukem 
    144   1.1  lukem 	Oflag = 0;
    145   1.1  lukem 	fssize =	fsopts->size / fsopts->sectorsize;
    146   1.1  lukem 	ntracks =	fsopts->ntracks;
    147   1.1  lukem 	nsectors =	fsopts->nsectors;
    148   1.1  lukem 	nphyssectors =	fsopts->nsectors;	/* XXX: no trackspares */
    149   1.1  lukem 	secpercyl =	nsectors * ntracks;
    150   1.1  lukem 	sectorsize =	fsopts->sectorsize;
    151   1.1  lukem 	rpm =		fsopts->rpm;
    152   1.8  lukem 	interleave =	1;
    153   1.8  lukem 	trackskew =	0;
    154   1.1  lukem 	fsize =		fsopts->fsize;
    155   1.1  lukem 	bsize =		fsopts->bsize;
    156   1.1  lukem 	cpg =		fsopts->cpg;
    157   1.8  lukem 	cpgflg =	fsopts->cpgflg;
    158   1.1  lukem 	minfree =	fsopts->minfree;
    159   1.1  lukem 	opt =		fsopts->optimization;
    160   1.1  lukem 	density =	fsopts->density;
    161   1.1  lukem 	maxcontig =	fsopts->maxcontig;
    162   1.1  lukem 	rotdelay =	fsopts->rotdelay;
    163   1.1  lukem 	maxbpg =	fsopts->maxbpg;
    164   1.1  lukem 	nrpos =		fsopts->nrpos;
    165   1.1  lukem 	bbsize =	BBSIZE;
    166   1.1  lukem 	sbsize =	SBSIZE;
    167   1.1  lukem 	avgfilesize = 	fsopts->avgfilesize;
    168   1.1  lukem 	avgfpdir = 	fsopts->avgfpdir;
    169   1.1  lukem 
    170   1.1  lukem 	if (Oflag) {
    171   1.1  lukem 		sblock.fs_inodefmt = FS_42INODEFMT;
    172   1.1  lukem 		sblock.fs_maxsymlinklen = 0;
    173   1.1  lukem 	} else {
    174   1.1  lukem 		sblock.fs_inodefmt = FS_44INODEFMT;
    175   1.1  lukem 		sblock.fs_maxsymlinklen = MAXSYMLINKLEN;
    176   1.1  lukem 	}
    177   1.1  lukem 	/*
    178   1.1  lukem 	 * Validate the given file system size.
    179   1.1  lukem 	 * Verify that its last block can actually be accessed.
    180   1.1  lukem 	 */
    181   1.1  lukem 	if (fssize <= 0)
    182   1.1  lukem 		printf("preposterous size %d\n", fssize), exit(13);
    183   1.1  lukem 	ffs_wtfs(fssize - 1, sectorsize, (char *)&sblock, fsopts);
    184   1.1  lukem 
    185   1.1  lukem 	/*
    186   1.1  lukem 	 * collect and verify the sector and track info
    187   1.1  lukem 	 */
    188   1.1  lukem 	sblock.fs_nsect = nsectors;
    189   1.1  lukem 	sblock.fs_ntrak = ntracks;
    190   1.1  lukem 	if (sblock.fs_ntrak <= 0)
    191   1.1  lukem 		printf("preposterous ntrak %d\n", sblock.fs_ntrak), exit(14);
    192   1.1  lukem 	if (sblock.fs_nsect <= 0)
    193   1.1  lukem 		printf("preposterous nsect %d\n", sblock.fs_nsect), exit(15);
    194   1.1  lukem 	/*
    195   1.1  lukem 	 * collect and verify the filesystem density info
    196   1.1  lukem 	 */
    197   1.1  lukem 	sblock.fs_avgfilesize = avgfilesize;
    198   1.1  lukem 	sblock.fs_avgfpdir = avgfpdir;
    199   1.1  lukem 	if (sblock.fs_avgfilesize <= 0)
    200   1.1  lukem 		printf("illegal expected average file size %d\n",
    201   1.1  lukem 		    sblock.fs_avgfilesize), exit(14);
    202   1.1  lukem 	if (sblock.fs_avgfpdir <= 0)
    203   1.1  lukem 		printf("illegal expected number of files per directory %d\n",
    204   1.1  lukem 		    sblock.fs_avgfpdir), exit(15);
    205   1.1  lukem 	/*
    206   1.1  lukem 	 * collect and verify the block and fragment sizes
    207   1.1  lukem 	 */
    208   1.1  lukem 	sblock.fs_bsize = bsize;
    209   1.1  lukem 	sblock.fs_fsize = fsize;
    210   1.1  lukem 	if (!POWEROF2(sblock.fs_bsize)) {
    211   1.1  lukem 		printf("block size must be a power of 2, not %d\n",
    212   1.1  lukem 		    sblock.fs_bsize);
    213   1.1  lukem 		exit(16);
    214   1.1  lukem 	}
    215   1.1  lukem 	if (!POWEROF2(sblock.fs_fsize)) {
    216   1.1  lukem 		printf("fragment size must be a power of 2, not %d\n",
    217   1.1  lukem 		    sblock.fs_fsize);
    218   1.1  lukem 		exit(17);
    219   1.1  lukem 	}
    220   1.1  lukem 	if (sblock.fs_fsize < sectorsize) {
    221   1.1  lukem 		printf("fragment size %d is too small, minimum is %d\n",
    222   1.1  lukem 		    sblock.fs_fsize, sectorsize);
    223   1.1  lukem 		exit(18);
    224   1.1  lukem 	}
    225   1.7  lukem 	if (sblock.fs_bsize > MAXBSIZE) {
    226   1.7  lukem 		printf("block size %d is too large, maximum is %d\n",
    227   1.7  lukem 		    sblock.fs_bsize, MAXBSIZE);
    228   1.7  lukem 		exit(19);
    229   1.7  lukem 	}
    230   1.1  lukem 	if (sblock.fs_bsize < MINBSIZE) {
    231   1.1  lukem 		printf("block size %d is too small, minimum is %d\n",
    232   1.1  lukem 		    sblock.fs_bsize, MINBSIZE);
    233   1.1  lukem 		exit(19);
    234   1.1  lukem 	}
    235   1.1  lukem 	if (sblock.fs_bsize < sblock.fs_fsize) {
    236   1.1  lukem 		printf("block size (%d) cannot be smaller than fragment size (%d)\n",
    237   1.1  lukem 		    sblock.fs_bsize, sblock.fs_fsize);
    238   1.1  lukem 		exit(20);
    239   1.1  lukem 	}
    240   1.1  lukem 	sblock.fs_bmask = ~(sblock.fs_bsize - 1);
    241   1.1  lukem 	sblock.fs_fmask = ~(sblock.fs_fsize - 1);
    242   1.1  lukem 	sblock.fs_qbmask = ~sblock.fs_bmask;
    243   1.1  lukem 	sblock.fs_qfmask = ~sblock.fs_fmask;
    244   1.1  lukem 	for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1)
    245   1.1  lukem 		sblock.fs_bshift++;
    246   1.1  lukem 	for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1)
    247   1.1  lukem 		sblock.fs_fshift++;
    248   1.1  lukem 	sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize);
    249   1.1  lukem 	for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1)
    250   1.1  lukem 		sblock.fs_fragshift++;
    251   1.1  lukem 	if (sblock.fs_frag > MAXFRAG) {
    252   1.1  lukem 		printf("fragment size %d is too small, "
    253   1.1  lukem 			"minimum with block size %d is %d\n",
    254   1.1  lukem 		    sblock.fs_fsize, sblock.fs_bsize,
    255   1.1  lukem 		    sblock.fs_bsize / MAXFRAG);
    256   1.1  lukem 		exit(21);
    257   1.1  lukem 	}
    258   1.1  lukem 	sblock.fs_nrpos = nrpos;
    259  1.12   fvdl 	/* XXX ondisk32 */
    260  1.12   fvdl 	sblock.fs_nindir = sblock.fs_bsize / sizeof(int32_t);
    261   1.1  lukem 	sblock.fs_inopb = sblock.fs_bsize / DINODE_SIZE;
    262   1.1  lukem 	sblock.fs_nspf = sblock.fs_fsize / sectorsize;
    263   1.1  lukem 	for (sblock.fs_fsbtodb = 0, i = NSPF(&sblock); i > 1; i >>= 1)
    264   1.1  lukem 		sblock.fs_fsbtodb++;
    265   1.1  lukem 	sblock.fs_sblkno =
    266   1.1  lukem 	    roundup(howmany(bbsize + sbsize, sblock.fs_fsize), sblock.fs_frag);
    267  1.12   fvdl 	/* XXX ondisk32 */
    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.12   fvdl 		printf("inode blocks/cyl group (%lld) >= data blocks (%d)\n",
    528  1.12   fvdl 		    (long long)cgdmin(&sblock, i) -
    529  1.12   fvdl 			cgbase(&sblock, i) / sblock.fs_frag,
    530   1.1  lukem 		    sblock.fs_fpg / sblock.fs_frag);
    531   1.1  lukem 		printf("number of cylinders per cylinder group (%d) %s.\n",
    532   1.1  lukem 		    sblock.fs_cpg, "must be increased");
    533   1.1  lukem 		exit(29);
    534   1.1  lukem 	}
    535   1.1  lukem 	j = sblock.fs_ncg - 1;
    536   1.1  lukem 	if ((i = fssize - j * sblock.fs_fpg) < sblock.fs_fpg &&
    537   1.1  lukem 	    cgdmin(&sblock, j) - cgbase(&sblock, j) > i) {
    538   1.1  lukem 		if (j == 0) {
    539  1.12   fvdl 			printf("File system must have at least %lld sectors\n",
    540  1.12   fvdl 			    (long long)NSPF(&sblock) *
    541   1.1  lukem 			    (cgdmin(&sblock, 0) + 3 * sblock.fs_frag));
    542   1.1  lukem 			exit(30);
    543   1.1  lukem 		}
    544  1.12   fvdl 		printf("Warning: inode blocks/cyl group (%lld) >= "
    545   1.1  lukem 			"data blocks (%d) in last\n",
    546  1.12   fvdl 		    (long long)(cgdmin(&sblock, j) - cgbase(&sblock, j))
    547  1.12   fvdl 			/ sblock.fs_frag,
    548   1.1  lukem 		    i / sblock.fs_frag);
    549   1.1  lukem 		printf("    cylinder group. This implies %d sector(s) "
    550   1.1  lukem 			"cannot be allocated.\n",
    551   1.1  lukem 		    i * NSPF(&sblock));
    552   1.1  lukem 		sblock.fs_ncg--;
    553   1.1  lukem 		sblock.fs_ncyl -= sblock.fs_ncyl % sblock.fs_cpg;
    554   1.1  lukem 		sblock.fs_size = fssize = sblock.fs_ncyl * sblock.fs_spc /
    555   1.1  lukem 		    NSPF(&sblock);
    556   1.1  lukem 		warned = 0;
    557   1.1  lukem 	}
    558   1.1  lukem 	if (warned) {
    559   1.1  lukem 		printf("Warning: %d sector(s) in last cylinder unallocated\n",
    560   1.1  lukem 		    sblock.fs_spc -
    561   1.1  lukem 		    (fssize * NSPF(&sblock) - (sblock.fs_ncyl - 1)
    562   1.1  lukem 		    * sblock.fs_spc));
    563   1.1  lukem 	}
    564   1.1  lukem 	/*
    565   1.1  lukem 	 * fill in remaining fields of the super block
    566   1.1  lukem 	 */
    567   1.1  lukem 	sblock.fs_csaddr = cgdmin(&sblock, 0);
    568   1.1  lukem 	sblock.fs_cssize =
    569   1.1  lukem 	    fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
    570   1.1  lukem 	/*
    571   1.1  lukem 	 * The superblock fields 'fs_csmask' and 'fs_csshift' are no
    572   1.1  lukem 	 * longer used. However, we still initialise them so that the
    573   1.1  lukem 	 * filesystem remains compatible with old kernels.
    574   1.1  lukem 	 */
    575   1.1  lukem 	i = sblock.fs_bsize / sizeof(struct csum);
    576   1.1  lukem 	sblock.fs_csmask = ~(i - 1);
    577   1.1  lukem 	for (sblock.fs_csshift = 0; i > 1; i >>= 1)
    578   1.1  lukem 		sblock.fs_csshift++;
    579   1.1  lukem 
    580   1.1  lukem 	/*
    581   1.1  lukem 	 * Setup memory for temporary in-core cylgroup summaries.
    582   1.1  lukem 	 * Cribbed from ffs_mountfs().
    583   1.1  lukem 	 */
    584   1.1  lukem 	size = sblock.fs_cssize;
    585   1.1  lukem 	blks = howmany(size, sblock.fs_fsize);
    586   1.1  lukem 	if (sblock.fs_contigsumsize > 0)
    587   1.1  lukem 		size += sblock.fs_ncg * sizeof(int32_t);
    588   1.1  lukem 	if ((space = (char *)calloc(1, size)) == NULL)
    589   1.1  lukem 		err(1, "memory allocation error for cg summaries");
    590   1.1  lukem 	sblock.fs_csp = space;
    591   1.1  lukem 	space = (char *)space + sblock.fs_cssize;
    592   1.1  lukem 	if (sblock.fs_contigsumsize > 0) {
    593   1.1  lukem 		int32_t *lp;
    594   1.1  lukem 
    595   1.1  lukem 		sblock.fs_maxcluster = lp = space;
    596   1.1  lukem 		for (i = 0; i < sblock.fs_ncg; i++)
    597   1.1  lukem 			*lp++ = sblock.fs_contigsumsize;
    598   1.1  lukem 	}
    599   1.1  lukem 
    600   1.1  lukem 	sblock.fs_magic = FS_MAGIC;
    601   1.1  lukem 	sblock.fs_rotdelay = rotdelay;
    602   1.1  lukem 	sblock.fs_minfree = minfree;
    603   1.1  lukem 	sblock.fs_maxcontig = maxcontig;
    604   1.1  lukem 	sblock.fs_maxbpg = maxbpg;
    605   1.1  lukem 	sblock.fs_rps = rpm / 60;
    606   1.1  lukem 	sblock.fs_optim = opt;
    607   1.1  lukem 	sblock.fs_cgrotor = 0;
    608   1.1  lukem 	sblock.fs_cstotal.cs_ndir = 0;
    609   1.1  lukem 	sblock.fs_cstotal.cs_nbfree = 0;
    610   1.1  lukem 	sblock.fs_cstotal.cs_nifree = 0;
    611   1.1  lukem 	sblock.fs_cstotal.cs_nffree = 0;
    612   1.1  lukem 	sblock.fs_fmod = 0;
    613   1.1  lukem 	sblock.fs_clean = FS_ISCLEAN;
    614   1.1  lukem 	sblock.fs_ronly = 0;
    615   1.1  lukem 
    616   1.1  lukem 	/*
    617   1.1  lukem 	 * Dump out summary information about file system.
    618   1.1  lukem 	 */
    619   1.1  lukem 	printf("%s:\t%d sectors in %d %s of %d tracks, %d sectors\n",
    620   1.1  lukem 		    fsys, sblock.fs_size * NSPF(&sblock), sblock.fs_ncyl,
    621   1.1  lukem 		    "cylinders", sblock.fs_ntrak, sblock.fs_nsect);
    622   1.1  lukem #define B2MBFACTOR (1 / (1024.0 * 1024.0))
    623   1.1  lukem 	printf("\t%.1fMB in %d cyl groups (%d c/g, %.2fMB/g, %d i/g)\n",
    624   1.1  lukem 		    (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
    625   1.1  lukem 		    sblock.fs_ncg, sblock.fs_cpg,
    626   1.1  lukem 		    (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
    627   1.1  lukem 		    sblock.fs_ipg);
    628   1.1  lukem #undef B2MBFACTOR
    629   1.1  lukem 	/*
    630   1.1  lukem 	 * Now determine how wide each column will be, and calculate how
    631   1.1  lukem 	 * many columns will fit in a 76 char line. 76 is the width of the
    632   1.1  lukem 	 * subwindows in sysinst.
    633   1.1  lukem 	 */
    634   1.1  lukem 	printcolwidth = count_digits(
    635   1.1  lukem 			fsbtodb(&sblock, cgsblock(&sblock, sblock.fs_ncg -1)));
    636   1.1  lukem 	nprintcols = 76 / (printcolwidth + 2);
    637   1.1  lukem 	/*
    638   1.1  lukem 	 * Now build the cylinders group blocks and
    639   1.1  lukem 	 * then print out indices of cylinder groups.
    640   1.1  lukem 	 */
    641   1.1  lukem 		printf("super-block backups (for fsck -b #) at:");
    642   1.1  lukem 	for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
    643   1.1  lukem 		initcg(cylno, start_time.tv_sec, fsopts);
    644   1.1  lukem 		if (cylno % nprintcols == 0)
    645   1.1  lukem 			printf("\n");
    646  1.12   fvdl 		printf(" %*lld,", printcolwidth,
    647  1.12   fvdl 			(long long)fsbtodb(&sblock, cgsblock(&sblock, cylno)));
    648   1.1  lukem 		fflush(stdout);
    649   1.1  lukem 	}
    650   1.1  lukem 	printf("\n");
    651   1.1  lukem 
    652   1.1  lukem 	/*
    653   1.1  lukem 	 * Now construct the initial file system,
    654   1.1  lukem 	 * then write out the super-block.
    655   1.1  lukem 	 */
    656   1.1  lukem 	sblock.fs_time = start_time.tv_sec;
    657   1.1  lukem 	if (fsopts->needswap)
    658   1.1  lukem 		sblock.fs_flags |= FS_SWAPPED;
    659   1.1  lukem 	ffs_write_superblock(&sblock, fsopts);
    660   1.1  lukem 	return (&sblock);
    661   1.1  lukem }
    662   1.1  lukem 
    663   1.1  lukem /*
    664   1.1  lukem  * Write out the superblock and its duplicates,
    665   1.1  lukem  * and the cylinder group summaries
    666   1.1  lukem  */
    667   1.1  lukem void
    668   1.1  lukem ffs_write_superblock(struct fs *fs, const fsinfo_t *fsopts)
    669   1.1  lukem {
    670   1.1  lukem 	int	cylno, size, blks, i, saveflag;
    671   1.1  lukem 	void	*space;
    672   1.1  lukem 	char	*wrbuf;
    673   1.1  lukem 
    674   1.1  lukem 	saveflag = fs->fs_flags & FS_INTERNAL;
    675   1.1  lukem 	fs->fs_flags &= ~FS_INTERNAL;
    676   1.1  lukem 
    677   1.1  lukem 			/* Write out the master super block */
    678   1.1  lukem 	memcpy(writebuf, fs, sbsize);
    679   1.1  lukem 	if (fsopts->needswap)
    680   1.1  lukem 		ffs_sb_swap(fs, (struct fs*)writebuf);
    681   1.1  lukem 	ffs_wtfs((int)SBOFF / sectorsize, sbsize, writebuf, fsopts);
    682   1.1  lukem 
    683   1.1  lukem 			/* Write out the duplicate super blocks */
    684   1.1  lukem 	for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
    685   1.1  lukem 		ffs_wtfs(fsbtodb(fs, cgsblock(fs, cylno)),
    686   1.1  lukem 		    sbsize, writebuf, fsopts);
    687   1.1  lukem 
    688   1.1  lukem 			/* Write out the cylinder group summaries */
    689   1.1  lukem 	size = fs->fs_cssize;
    690   1.1  lukem 	blks = howmany(size, fs->fs_fsize);
    691   1.1  lukem 	space = (void *)fs->fs_csp;
    692   1.1  lukem 	if ((wrbuf = malloc(size)) == NULL)
    693   1.1  lukem 		err(1, "ffs_write_superblock: malloc %d", size);
    694   1.1  lukem 	for (i = 0; i < blks; i+= fs->fs_frag) {
    695   1.1  lukem 		size = fs->fs_bsize;
    696   1.1  lukem 		if (i + fs->fs_frag > blks)
    697   1.1  lukem 			size = (blks - i) * fs->fs_fsize;
    698   1.1  lukem 		if (fsopts->needswap)
    699   1.1  lukem 			ffs_csum_swap((struct csum *)space,
    700   1.1  lukem 			    (struct csum *)wrbuf, size);
    701   1.1  lukem 		else
    702   1.1  lukem 			memcpy(wrbuf, space, (u_int)size);
    703   1.1  lukem 		ffs_wtfs(fsbtodb(fs, fs->fs_csaddr + i), size, wrbuf, fsopts);
    704   1.1  lukem 		space = (char *)space + size;
    705   1.1  lukem 	}
    706   1.1  lukem 	free(wrbuf);
    707   1.1  lukem 	fs->fs_flags |= saveflag;
    708   1.1  lukem }
    709   1.1  lukem 
    710   1.1  lukem 
    711   1.1  lukem /*
    712   1.1  lukem  * Initialize a cylinder group.
    713   1.1  lukem  */
    714   1.1  lukem static void
    715   1.1  lukem initcg(int cylno, time_t utime, const fsinfo_t *fsopts)
    716   1.1  lukem {
    717   1.1  lukem 	daddr_t cbase, d, dlower, dupper, dmax, blkno;
    718   1.1  lukem 	int32_t i;
    719   1.1  lukem 
    720   1.1  lukem 	/*
    721   1.1  lukem 	 * Determine block bounds for cylinder group.
    722   1.1  lukem 	 * Allow space for super block summary information in first
    723   1.1  lukem 	 * cylinder group.
    724   1.1  lukem 	 */
    725   1.1  lukem 	cbase = cgbase(&sblock, cylno);
    726   1.1  lukem 	dmax = cbase + sblock.fs_fpg;
    727   1.1  lukem 	if (dmax > sblock.fs_size)
    728   1.1  lukem 		dmax = sblock.fs_size;
    729   1.1  lukem 	dlower = cgsblock(&sblock, cylno) - cbase;
    730   1.1  lukem 	dupper = cgdmin(&sblock, cylno) - cbase;
    731   1.1  lukem 	if (cylno == 0)
    732   1.1  lukem 		dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
    733   1.1  lukem 	memset(&acg, 0, sblock.fs_cgsize);
    734   1.1  lukem 	acg.cg_time = utime;
    735   1.1  lukem 	acg.cg_magic = CG_MAGIC;
    736   1.1  lukem 	acg.cg_cgx = cylno;
    737   1.1  lukem 	if (cylno == sblock.fs_ncg - 1)
    738   1.1  lukem 		acg.cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg;
    739   1.1  lukem 	else
    740   1.1  lukem 		acg.cg_ncyl = sblock.fs_cpg;
    741   1.1  lukem 	acg.cg_niblk = sblock.fs_ipg;
    742   1.1  lukem 	acg.cg_ndblk = dmax - cbase;
    743   1.1  lukem 	if (sblock.fs_contigsumsize > 0)
    744   1.1  lukem 		acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag;
    745   1.1  lukem 	acg.cg_btotoff = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield);
    746   1.1  lukem 	acg.cg_boff = acg.cg_btotoff + sblock.fs_cpg * sizeof(int32_t);
    747   1.1  lukem 	acg.cg_iusedoff = acg.cg_boff +
    748   1.1  lukem 		sblock.fs_cpg * sblock.fs_nrpos * sizeof(int16_t);
    749   1.1  lukem 	acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, NBBY);
    750   1.1  lukem 	if (sblock.fs_contigsumsize <= 0) {
    751   1.1  lukem 		acg.cg_nextfreeoff = acg.cg_freeoff +
    752   1.1  lukem 		   howmany(sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY);
    753   1.1  lukem 	} else {
    754   1.1  lukem 		acg.cg_clustersumoff = acg.cg_freeoff + howmany
    755   1.1  lukem 		    (sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY) -
    756   1.1  lukem 		    sizeof(int32_t);
    757   1.1  lukem 		acg.cg_clustersumoff =
    758   1.1  lukem 		    roundup(acg.cg_clustersumoff, sizeof(int32_t));
    759   1.1  lukem 		acg.cg_clusteroff = acg.cg_clustersumoff +
    760   1.1  lukem 		    (sblock.fs_contigsumsize + 1) * sizeof(int32_t);
    761   1.1  lukem 		acg.cg_nextfreeoff = acg.cg_clusteroff + howmany
    762   1.1  lukem 		    (sblock.fs_cpg * sblock.fs_spc / NSPB(&sblock), NBBY);
    763   1.1  lukem 	}
    764   1.1  lukem 	if (acg.cg_nextfreeoff > sblock.fs_cgsize) {
    765   1.1  lukem 		printf("Panic: cylinder group too big\n");
    766   1.1  lukem 		exit(37);
    767   1.1  lukem 	}
    768   1.1  lukem 	acg.cg_cs.cs_nifree += sblock.fs_ipg;
    769   1.1  lukem 	if (cylno == 0)
    770   1.1  lukem 		for (i = 0; i < ROOTINO; i++) {
    771   1.1  lukem 			setbit(cg_inosused(&acg, 0), i);
    772   1.1  lukem 			acg.cg_cs.cs_nifree--;
    773   1.1  lukem 		}
    774   1.1  lukem 	for (i = 0; i < sblock.fs_ipg / INOPF(&sblock); i += sblock.fs_frag)
    775   1.1  lukem 		ffs_wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
    776   1.1  lukem 		    sblock.fs_bsize, (char *)zino, fsopts);
    777   1.1  lukem 	if (cylno > 0) {
    778   1.1  lukem 		/*
    779   1.1  lukem 		 * In cylno 0, beginning space is reserved
    780   1.1  lukem 		 * for boot and super blocks.
    781   1.1  lukem 		 */
    782   1.1  lukem 		for (d = 0; d < dlower; d += sblock.fs_frag) {
    783   1.1  lukem 			blkno = d / sblock.fs_frag;
    784   1.1  lukem 			ffs_setblock(&sblock, cg_blksfree(&acg, 0), blkno);
    785   1.1  lukem 			if (sblock.fs_contigsumsize > 0)
    786   1.1  lukem 				setbit(cg_clustersfree(&acg, 0), blkno);
    787   1.1  lukem 			acg.cg_cs.cs_nbfree++;
    788   1.1  lukem 			cg_blktot(&acg, 0)[cbtocylno(&sblock, d)]++;
    789   1.1  lukem 			cg_blks(&sblock, &acg, cbtocylno(&sblock, d), 0)
    790   1.1  lukem 			    [cbtorpos(&sblock, d)]++;
    791   1.1  lukem 		}
    792   1.1  lukem 		sblock.fs_dsize += dlower;
    793   1.1  lukem 	}
    794   1.1  lukem 	sblock.fs_dsize += acg.cg_ndblk - dupper;
    795   1.1  lukem 	if ((i = (dupper % sblock.fs_frag)) != 0) {
    796   1.1  lukem 		acg.cg_frsum[sblock.fs_frag - i]++;
    797   1.1  lukem 		for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
    798   1.1  lukem 			setbit(cg_blksfree(&acg, 0), dupper);
    799   1.1  lukem 			acg.cg_cs.cs_nffree++;
    800   1.1  lukem 		}
    801   1.1  lukem 	}
    802   1.1  lukem 	for (d = dupper; d + sblock.fs_frag <= dmax - cbase; ) {
    803   1.1  lukem 		blkno = d / sblock.fs_frag;
    804   1.1  lukem 		ffs_setblock(&sblock, cg_blksfree(&acg, 0), blkno);
    805   1.1  lukem 		if (sblock.fs_contigsumsize > 0)
    806   1.1  lukem 			setbit(cg_clustersfree(&acg, 0), blkno);
    807   1.1  lukem 		acg.cg_cs.cs_nbfree++;
    808   1.1  lukem 		cg_blktot(&acg, 0)[cbtocylno(&sblock, d)]++;
    809   1.1  lukem 		cg_blks(&sblock, &acg, cbtocylno(&sblock, d), 0)
    810   1.1  lukem 		    [cbtorpos(&sblock, d)]++;
    811   1.1  lukem 		d += sblock.fs_frag;
    812   1.1  lukem 	}
    813   1.1  lukem 	if (d < dmax - cbase) {
    814   1.1  lukem 		acg.cg_frsum[dmax - cbase - d]++;
    815   1.1  lukem 		for (; d < dmax - cbase; d++) {
    816   1.1  lukem 			setbit(cg_blksfree(&acg, 0), d);
    817   1.1  lukem 			acg.cg_cs.cs_nffree++;
    818   1.1  lukem 		}
    819   1.1  lukem 	}
    820   1.1  lukem 	if (sblock.fs_contigsumsize > 0) {
    821   1.1  lukem 		int32_t *sump = cg_clustersum(&acg, 0);
    822   1.1  lukem 		u_char *mapp = cg_clustersfree(&acg, 0);
    823   1.1  lukem 		int map = *mapp++;
    824   1.1  lukem 		int bit = 1;
    825   1.1  lukem 		int run = 0;
    826   1.1  lukem 
    827   1.1  lukem 		for (i = 0; i < acg.cg_nclusterblks; i++) {
    828   1.1  lukem 			if ((map & bit) != 0) {
    829   1.1  lukem 				run++;
    830   1.1  lukem 			} else if (run != 0) {
    831   1.1  lukem 				if (run > sblock.fs_contigsumsize)
    832   1.1  lukem 					run = sblock.fs_contigsumsize;
    833   1.1  lukem 				sump[run]++;
    834   1.1  lukem 				run = 0;
    835   1.1  lukem 			}
    836   1.1  lukem 			if ((i & (NBBY - 1)) != (NBBY - 1)) {
    837   1.1  lukem 				bit <<= 1;
    838   1.1  lukem 			} else {
    839   1.1  lukem 				map = *mapp++;
    840   1.1  lukem 				bit = 1;
    841   1.1  lukem 			}
    842   1.1  lukem 		}
    843   1.1  lukem 		if (run != 0) {
    844   1.1  lukem 			if (run > sblock.fs_contigsumsize)
    845   1.1  lukem 				run = sblock.fs_contigsumsize;
    846   1.1  lukem 			sump[run]++;
    847   1.1  lukem 		}
    848   1.1  lukem 	}
    849   1.1  lukem 	sblock.fs_cstotal.cs_ndir += acg.cg_cs.cs_ndir;
    850   1.1  lukem 	sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree;
    851   1.1  lukem 	sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree;
    852   1.1  lukem 	sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree;
    853   1.1  lukem 	sblock.fs_cs(&sblock, cylno) = acg.cg_cs;
    854   1.1  lukem 	memcpy(writebuf, &acg, sblock.fs_bsize);
    855   1.1  lukem 	if (fsopts->needswap)
    856   1.1  lukem 		swap_cg(&acg, (struct cg*)writebuf);
    857   1.1  lukem 	ffs_wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
    858   1.1  lukem 		sblock.fs_bsize,
    859   1.1  lukem 		writebuf, fsopts);
    860   1.1  lukem }
    861   1.1  lukem 
    862   1.1  lukem /*
    863   1.1  lukem  * Calculate number of inodes per group.
    864   1.1  lukem  */
    865   1.1  lukem static int32_t
    866   1.1  lukem calcipg(int32_t cylpg, int32_t bpcg, off_t *usedbp)
    867   1.1  lukem {
    868   1.1  lukem 	int i;
    869   1.1  lukem 	int32_t ipg, new_ipg, ncg, ncyl;
    870   1.1  lukem 	off_t usedb;
    871   1.1  lukem 
    872   1.1  lukem 	/*
    873   1.1  lukem 	 * Prepare to scale by fssize / (number of sectors in cylinder groups).
    874   1.1  lukem 	 * Note that fssize is still in sectors, not file system blocks.
    875   1.1  lukem 	 */
    876   1.1  lukem 	ncyl = howmany(fssize, secpercyl);
    877   1.1  lukem 	ncg = howmany(ncyl, cylpg);
    878   1.1  lukem 	/*
    879   1.1  lukem 	 * Iterate a few times to allow for ipg depending on itself.
    880   1.1  lukem 	 */
    881   1.1  lukem 	ipg = 0;
    882   1.1  lukem 	for (i = 0; i < 10; i++) {
    883   1.1  lukem 		usedb = (sblock.fs_iblkno + ipg / INOPF(&sblock))
    884   1.1  lukem 			* NSPF(&sblock) * (off_t)sectorsize;
    885   1.1  lukem 		if (cylpg * (long long)bpcg < usedb) {
    886   1.1  lukem 			warnx("Too many inodes per cyl group!");
    887   1.1  lukem 			return (MAXIPG(&sblock)+1);
    888   1.1  lukem 		}
    889   1.1  lukem 		new_ipg = (cylpg * (long long)bpcg - usedb) /
    890   1.7  lukem 		    (long long)density * fssize / (ncg * secpercyl * cylpg);
    891   1.1  lukem 		if (new_ipg <= 0)
    892   1.1  lukem 			new_ipg = 1;		/* ensure ipg > 0 */
    893   1.1  lukem 		new_ipg = roundup(new_ipg, INOPB(&sblock));
    894   1.1  lukem 		if (new_ipg == ipg)
    895   1.1  lukem 			break;
    896   1.1  lukem 		ipg = new_ipg;
    897   1.1  lukem 	}
    898   1.1  lukem 	*usedbp = usedb;
    899   1.1  lukem 	return (ipg);
    900   1.1  lukem }
    901   1.1  lukem 
    902   1.1  lukem 
    903   1.1  lukem /*
    904   1.1  lukem  * read a block from the file system
    905   1.1  lukem  */
    906   1.1  lukem void
    907   1.1  lukem ffs_rdfs(daddr_t bno, int size, void *bf, const fsinfo_t *fsopts)
    908   1.1  lukem {
    909   1.1  lukem 	int n;
    910   1.1  lukem 	off_t offset;
    911   1.1  lukem 
    912   1.1  lukem 	offset = bno;
    913   1.1  lukem 	offset *= fsopts->sectorsize;
    914   1.1  lukem 	if (lseek(fsopts->fd, offset, SEEK_SET) < 0)
    915  1.12   fvdl 		err(1, "ffs_rdfs: seek error: %lld", (long long)bno);
    916   1.1  lukem 	n = read(fsopts->fd, bf, size);
    917   1.1  lukem 	if (n == -1)
    918  1.12   fvdl 		err(1, "ffs_rdfs: read error bno %lld size %d", (long long)bno,
    919  1.12   fvdl 		    size);
    920   1.1  lukem 	else if (n != size)
    921   1.1  lukem 		errx(1,
    922  1.12   fvdl 		    "ffs_rdfs: read error bno %lld size %d: short read of %d",
    923  1.12   fvdl 		    (long long)bno, size, n);
    924   1.1  lukem }
    925   1.1  lukem 
    926   1.1  lukem /*
    927   1.1  lukem  * write a block to the file system
    928   1.1  lukem  */
    929   1.1  lukem void
    930   1.1  lukem ffs_wtfs(daddr_t bno, int size, void *bf, const fsinfo_t *fsopts)
    931   1.1  lukem {
    932   1.1  lukem 	int n;
    933   1.1  lukem 	off_t offset;
    934   1.1  lukem 
    935   1.1  lukem 	offset = bno;
    936   1.1  lukem 	offset *= fsopts->sectorsize;
    937   1.1  lukem 	if (lseek(fsopts->fd, offset, SEEK_SET) < 0)
    938  1.12   fvdl 		err(1, "ffs_wtfs: seek error: %lld", (long long)bno);
    939   1.1  lukem 	n = write(fsopts->fd, bf, size);
    940   1.1  lukem 	if (n == -1)
    941  1.12   fvdl 		err(1, "ffs_wtfs: write error bno %lld size %d", (long long)bno,		   size);
    942   1.1  lukem 	else if (n != size)
    943   1.1  lukem 		errx(1,
    944  1.12   fvdl 		    "ffs_wtfs: write error bno %lld size %d: short write of %d",
    945  1.12   fvdl 		    (long long)bno, size, n);
    946   1.1  lukem }
    947   1.1  lukem 
    948   1.1  lukem /* swap byte order of cylinder group */
    949   1.1  lukem static void
    950   1.1  lukem swap_cg(struct cg *o, struct cg *n)
    951   1.1  lukem {
    952   1.1  lukem 	int i, btotsize, fbsize;
    953   1.1  lukem 	u_int32_t *n32, *o32;
    954   1.1  lukem 	u_int16_t *n16, *o16;
    955   1.1  lukem 
    956   1.1  lukem 	n->cg_firstfield = bswap32(o->cg_firstfield);
    957   1.1  lukem 	n->cg_magic = bswap32(o->cg_magic);
    958   1.1  lukem 	n->cg_time = bswap32(o->cg_time);
    959   1.1  lukem 	n->cg_cgx = bswap32(o->cg_cgx);
    960   1.1  lukem 	n->cg_ncyl = bswap16(o->cg_ncyl);
    961   1.1  lukem 	n->cg_niblk = bswap16(o->cg_niblk);
    962   1.1  lukem 	n->cg_ndblk = bswap32(o->cg_ndblk);
    963   1.1  lukem 	n->cg_cs.cs_ndir = bswap32(o->cg_cs.cs_ndir);
    964   1.1  lukem 	n->cg_cs.cs_nbfree = bswap32(o->cg_cs.cs_nbfree);
    965   1.1  lukem 	n->cg_cs.cs_nifree = bswap32(o->cg_cs.cs_nifree);
    966   1.1  lukem 	n->cg_cs.cs_nffree = bswap32(o->cg_cs.cs_nffree);
    967   1.1  lukem 	n->cg_rotor = bswap32(o->cg_rotor);
    968   1.1  lukem 	n->cg_frotor = bswap32(o->cg_frotor);
    969   1.1  lukem 	n->cg_irotor = bswap32(o->cg_irotor);
    970   1.1  lukem 	n->cg_btotoff = bswap32(o->cg_btotoff);
    971   1.1  lukem 	n->cg_boff = bswap32(o->cg_boff);
    972   1.1  lukem 	n->cg_iusedoff = bswap32(o->cg_iusedoff);
    973   1.1  lukem 	n->cg_freeoff = bswap32(o->cg_freeoff);
    974   1.1  lukem 	n->cg_nextfreeoff = bswap32(o->cg_nextfreeoff);
    975   1.1  lukem 	n->cg_clustersumoff = bswap32(o->cg_clustersumoff);
    976   1.1  lukem 	n->cg_clusteroff = bswap32(o->cg_clusteroff);
    977   1.1  lukem 	n->cg_nclusterblks = bswap32(o->cg_nclusterblks);
    978   1.1  lukem 	for (i=0; i < MAXFRAG; i++)
    979   1.1  lukem 		n->cg_frsum[i] = bswap32(o->cg_frsum[i]);
    980   1.1  lukem 
    981   1.1  lukem 	/* alays new format */
    982   1.1  lukem 	if (n->cg_magic == CG_MAGIC) {
    983   1.1  lukem 		btotsize = n->cg_boff - n->cg_btotoff;
    984   1.1  lukem 		fbsize = n->cg_iusedoff - n->cg_boff;
    985   1.1  lukem 		n32 = (u_int32_t*)((u_int8_t*)n + n->cg_btotoff);
    986   1.1  lukem 		o32 = (u_int32_t*)((u_int8_t*)o + n->cg_btotoff);
    987   1.1  lukem 		n16 = (u_int16_t*)((u_int8_t*)n + n->cg_boff);
    988   1.1  lukem 		o16 = (u_int16_t*)((u_int8_t*)o + n->cg_boff);
    989   1.1  lukem 	} else {
    990   1.1  lukem 		btotsize = bswap32(n->cg_boff) - bswap32(n->cg_btotoff);
    991   1.1  lukem 		fbsize = bswap32(n->cg_iusedoff) - bswap32(n->cg_boff);
    992   1.1  lukem 		n32 = (u_int32_t*)((u_int8_t*)n + bswap32(n->cg_btotoff));
    993   1.1  lukem 		o32 = (u_int32_t*)((u_int8_t*)o + bswap32(n->cg_btotoff));
    994   1.1  lukem 		n16 = (u_int16_t*)((u_int8_t*)n + bswap32(n->cg_boff));
    995   1.1  lukem 		o16 = (u_int16_t*)((u_int8_t*)o + bswap32(n->cg_boff));
    996   1.1  lukem 	}
    997   1.1  lukem 	for (i=0; i < btotsize / sizeof(u_int32_t); i++)
    998   1.1  lukem 		n32[i] = bswap32(o32[i]);
    999   1.1  lukem 
   1000   1.1  lukem 	for (i=0; i < fbsize/sizeof(u_int16_t); i++)
   1001   1.1  lukem 		n16[i] = bswap16(o16[i]);
   1002   1.1  lukem 
   1003   1.1  lukem 	if (n->cg_magic == CG_MAGIC) {
   1004   1.1  lukem 		n32 = (u_int32_t*)((u_int8_t*)n + n->cg_clustersumoff);
   1005   1.1  lukem 		o32 = (u_int32_t*)((u_int8_t*)o + n->cg_clustersumoff);
   1006   1.1  lukem 	} else {
   1007   1.1  lukem 		n32 = (u_int32_t*)((u_int8_t*)n + bswap32(n->cg_clustersumoff));
   1008   1.1  lukem 		o32 = (u_int32_t*)((u_int8_t*)o + bswap32(n->cg_clustersumoff));
   1009   1.1  lukem 	}
   1010   1.1  lukem 	for (i = 1; i < sblock.fs_contigsumsize + 1; i++)
   1011   1.1  lukem 		n32[i] = bswap32(o32[i]);
   1012   1.1  lukem }
   1013   1.1  lukem 
   1014   1.1  lukem /* Determine how many digits are needed to print a given integer */
   1015   1.1  lukem static int
   1016   1.1  lukem count_digits(int num)
   1017   1.1  lukem {
   1018   1.1  lukem 	int ndig;
   1019   1.1  lukem 
   1020   1.1  lukem 	for(ndig = 1; num > 9; num /=10, ndig++);
   1021   1.1  lukem 
   1022   1.1  lukem 	return (ndig);
   1023   1.1  lukem }
   1024