Home | History | Annotate | Line # | Download | only in newfs
mkfs.c revision 1.38.4.5
      1  1.38.4.5        he /*	$NetBSD: mkfs.c,v 1.38.4.5 2001/11/25 19:56:41 he Exp $	*/
      2      1.19       cgd 
      3       1.1       cgd /*
      4       1.9   mycroft  * Copyright (c) 1980, 1989, 1993
      5       1.9   mycroft  *	The Regents of the University of California.  All rights reserved.
      6       1.1       cgd  *
      7       1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8       1.1       cgd  * modification, are permitted provided that the following conditions
      9       1.1       cgd  * are met:
     10       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15       1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16       1.1       cgd  *    must display the following acknowledgement:
     17       1.1       cgd  *	This product includes software developed by the University of
     18       1.1       cgd  *	California, Berkeley and its contributors.
     19       1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20       1.1       cgd  *    may be used to endorse or promote products derived from this software
     21       1.1       cgd  *    without specific prior written permission.
     22       1.1       cgd  *
     23       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33       1.1       cgd  * SUCH DAMAGE.
     34       1.1       cgd  */
     35       1.1       cgd 
     36      1.26  christos #include <sys/cdefs.h>
     37       1.1       cgd #ifndef lint
     38      1.19       cgd #if 0
     39      1.27     lukem static char sccsid[] = "@(#)mkfs.c	8.11 (Berkeley) 5/3/95";
     40      1.19       cgd #else
     41  1.38.4.5        he __RCSID("$NetBSD: mkfs.c,v 1.38.4.5 2001/11/25 19:56:41 he Exp $");
     42      1.19       cgd #endif
     43       1.1       cgd #endif /* not lint */
     44       1.1       cgd 
     45       1.1       cgd #include <sys/param.h>
     46       1.1       cgd #include <sys/time.h>
     47       1.1       cgd #include <sys/resource.h>
     48       1.9   mycroft #include <ufs/ufs/dinode.h>
     49       1.9   mycroft #include <ufs/ufs/dir.h>
     50      1.30    bouyer #include <ufs/ufs/ufs_bswap.h>
     51       1.9   mycroft #include <ufs/ffs/fs.h>
     52      1.30    bouyer #include <ufs/ffs/ffs_extern.h>
     53       1.1       cgd #include <sys/disklabel.h>
     54       1.9   mycroft 
     55      1.14       cgd #include <string.h>
     56      1.14       cgd #include <unistd.h>
     57      1.26  christos #include <stdlib.h>
     58      1.14       cgd 
     59       1.9   mycroft #ifndef STANDALONE
     60       1.9   mycroft #include <stdio.h>
     61       1.9   mycroft #endif
     62       1.1       cgd 
     63  1.38.4.1        he #include "extern.h"
     64      1.26  christos 
     65      1.27     lukem 
     66  1.38.4.1        he static void initcg(int, time_t);
     67  1.38.4.1        he static void fsinit(time_t);
     68  1.38.4.1        he static int makedir(struct direct *, int);
     69  1.38.4.1        he static daddr_t alloc(int, int);
     70  1.38.4.1        he static void iput(struct dinode *, ino_t);
     71  1.38.4.1        he static void rdfs(daddr_t, int, void *);
     72  1.38.4.1        he static void wtfs(daddr_t, int, void *);
     73  1.38.4.1        he static int isblock(struct fs *, unsigned char *, int);
     74  1.38.4.1        he static void clrblock(struct fs *, unsigned char *, int);
     75  1.38.4.1        he static void setblock(struct fs *, unsigned char *, int);
     76  1.38.4.1        he static int32_t calcipg(int32_t, int32_t, off_t *);
     77  1.38.4.1        he static void swap_cg(struct cg *, struct cg *);
     78  1.38.4.1        he 
     79  1.38.4.1        he static int count_digits(int);
     80      1.36  wrstuden 
     81       1.1       cgd /*
     82       1.1       cgd  * make file system for cylinder-group style file systems
     83       1.1       cgd  */
     84       1.1       cgd 
     85       1.1       cgd /*
     86       1.1       cgd  * We limit the size of the inode map to be no more than a
     87       1.1       cgd  * third of the cylinder group space, since we must leave at
     88       1.1       cgd  * least an equal amount of space for the block map.
     89       1.1       cgd  *
     90       1.1       cgd  * N.B.: MAXIPG must be a multiple of INOPB(fs).
     91       1.1       cgd  */
     92       1.1       cgd #define MAXIPG(fs)	roundup((fs)->fs_bsize * NBBY / 3, INOPB(fs))
     93       1.1       cgd 
     94       1.1       cgd #define UMASK		0755
     95      1.32   thorpej #define MAXINOPB	(MAXBSIZE / DINODE_SIZE)
     96       1.1       cgd #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
     97       1.1       cgd 
     98       1.1       cgd union {
     99       1.1       cgd 	struct fs fs;
    100       1.1       cgd 	char pad[SBSIZE];
    101       1.1       cgd } fsun;
    102       1.1       cgd #define	sblock	fsun.fs
    103       1.1       cgd struct	csum *fscs;
    104       1.1       cgd 
    105       1.1       cgd union {
    106       1.1       cgd 	struct cg cg;
    107       1.1       cgd 	char pad[MAXBSIZE];
    108       1.1       cgd } cgun;
    109       1.1       cgd #define	acg	cgun.cg
    110       1.1       cgd 
    111      1.32   thorpej struct dinode zino[MAXBSIZE / DINODE_SIZE];
    112       1.1       cgd 
    113      1.30    bouyer char writebuf[MAXBSIZE];
    114      1.30    bouyer 
    115       1.1       cgd int	fsi, fso;
    116       1.1       cgd 
    117      1.26  christos void
    118  1.38.4.1        he mkfs(struct partition *pp, char *fsys, int fi, int fo)
    119       1.1       cgd {
    120      1.27     lukem 	int32_t i, mincpc, mincpg, inospercg;
    121      1.27     lukem 	int32_t cylno, rpos, blk, j, warn = 0;
    122      1.27     lukem 	int32_t used, mincpgcnt, bpcg;
    123      1.27     lukem 	off_t usedb;
    124      1.27     lukem 	int32_t mapcramped, inodecramped;
    125      1.27     lukem 	int32_t postblsize, rotblsize, totalsbsize;
    126       1.1       cgd 	time_t utime;
    127       1.9   mycroft 	quad_t sizepb;
    128      1.34  wrstuden 	char *writebuf2;		/* dynamic buffer */
    129      1.36  wrstuden 	int nprintcols, printcolwidth;
    130       1.1       cgd 
    131       1.1       cgd #ifndef STANDALONE
    132       1.1       cgd 	time(&utime);
    133       1.1       cgd #endif
    134       1.1       cgd 	if (mfs) {
    135       1.1       cgd 		(void)malloc(0);
    136       1.1       cgd 		if (fssize * sectorsize > memleft)
    137       1.1       cgd 			fssize = (memleft - 16384) / sectorsize;
    138       1.1       cgd 		if ((membase = malloc(fssize * sectorsize)) == 0)
    139       1.1       cgd 			exit(12);
    140       1.1       cgd 	}
    141       1.1       cgd 	fsi = fi;
    142       1.1       cgd 	fso = fo;
    143       1.9   mycroft 	if (Oflag) {
    144       1.9   mycroft 		sblock.fs_inodefmt = FS_42INODEFMT;
    145       1.9   mycroft 		sblock.fs_maxsymlinklen = 0;
    146       1.9   mycroft 	} else {
    147       1.9   mycroft 		sblock.fs_inodefmt = FS_44INODEFMT;
    148       1.9   mycroft 		sblock.fs_maxsymlinklen = MAXSYMLINKLEN;
    149       1.9   mycroft 	}
    150       1.1       cgd 	/*
    151       1.1       cgd 	 * Validate the given file system size.
    152       1.1       cgd 	 * Verify that its last block can actually be accessed.
    153       1.1       cgd 	 */
    154       1.1       cgd 	if (fssize <= 0)
    155       1.1       cgd 		printf("preposterous size %d\n", fssize), exit(13);
    156       1.1       cgd 	wtfs(fssize - 1, sectorsize, (char *)&sblock);
    157      1.30    bouyer 
    158       1.1       cgd 	/*
    159       1.1       cgd 	 * collect and verify the sector and track info
    160       1.1       cgd 	 */
    161       1.1       cgd 	sblock.fs_nsect = nsectors;
    162       1.1       cgd 	sblock.fs_ntrak = ntracks;
    163       1.1       cgd 	if (sblock.fs_ntrak <= 0)
    164       1.1       cgd 		printf("preposterous ntrak %d\n", sblock.fs_ntrak), exit(14);
    165       1.1       cgd 	if (sblock.fs_nsect <= 0)
    166       1.1       cgd 		printf("preposterous nsect %d\n", sblock.fs_nsect), exit(15);
    167  1.38.4.5        he 	/*
    168  1.38.4.5        he 	 * collect and verify the filesystem density info
    169  1.38.4.5        he 	 */
    170  1.38.4.5        he 	sblock.fs_avgfilesize = avgfilesize;
    171  1.38.4.5        he 	sblock.fs_avgfpdir = avgfpdir;
    172  1.38.4.5        he 	if (sblock.fs_avgfilesize <= 0)
    173  1.38.4.5        he 		printf("illegal expected average file size %d\n",
    174  1.38.4.5        he 		    sblock.fs_avgfilesize), exit(14);
    175  1.38.4.5        he 	if (sblock.fs_avgfpdir <= 0)
    176  1.38.4.5        he 		printf("illegal expected number of files per directory %d\n",
    177  1.38.4.5        he 		    sblock.fs_avgfpdir), exit(15);
    178       1.1       cgd 	/*
    179       1.1       cgd 	 * collect and verify the block and fragment sizes
    180       1.1       cgd 	 */
    181       1.1       cgd 	sblock.fs_bsize = bsize;
    182       1.1       cgd 	sblock.fs_fsize = fsize;
    183       1.1       cgd 	if (!POWEROF2(sblock.fs_bsize)) {
    184       1.1       cgd 		printf("block size must be a power of 2, not %d\n",
    185       1.1       cgd 		    sblock.fs_bsize);
    186       1.1       cgd 		exit(16);
    187       1.1       cgd 	}
    188       1.1       cgd 	if (!POWEROF2(sblock.fs_fsize)) {
    189       1.1       cgd 		printf("fragment size must be a power of 2, not %d\n",
    190       1.1       cgd 		    sblock.fs_fsize);
    191       1.1       cgd 		exit(17);
    192       1.1       cgd 	}
    193       1.1       cgd 	if (sblock.fs_fsize < sectorsize) {
    194       1.1       cgd 		printf("fragment size %d is too small, minimum is %d\n",
    195       1.1       cgd 		    sblock.fs_fsize, sectorsize);
    196       1.1       cgd 		exit(18);
    197       1.1       cgd 	}
    198       1.1       cgd 	if (sblock.fs_bsize < MINBSIZE) {
    199       1.1       cgd 		printf("block size %d is too small, minimum is %d\n",
    200       1.1       cgd 		    sblock.fs_bsize, MINBSIZE);
    201       1.1       cgd 		exit(19);
    202       1.1       cgd 	}
    203       1.1       cgd 	if (sblock.fs_bsize < sblock.fs_fsize) {
    204       1.1       cgd 		printf("block size (%d) cannot be smaller than fragment size (%d)\n",
    205       1.1       cgd 		    sblock.fs_bsize, sblock.fs_fsize);
    206       1.1       cgd 		exit(20);
    207       1.1       cgd 	}
    208       1.1       cgd 	sblock.fs_bmask = ~(sblock.fs_bsize - 1);
    209       1.1       cgd 	sblock.fs_fmask = ~(sblock.fs_fsize - 1);
    210       1.9   mycroft 	sblock.fs_qbmask = ~sblock.fs_bmask;
    211       1.9   mycroft 	sblock.fs_qfmask = ~sblock.fs_fmask;
    212       1.1       cgd 	for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1)
    213       1.1       cgd 		sblock.fs_bshift++;
    214       1.1       cgd 	for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1)
    215       1.1       cgd 		sblock.fs_fshift++;
    216       1.1       cgd 	sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize);
    217       1.1       cgd 	for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1)
    218       1.1       cgd 		sblock.fs_fragshift++;
    219       1.1       cgd 	if (sblock.fs_frag > MAXFRAG) {
    220      1.30    bouyer 		printf("fragment size %d is too small, "
    221      1.30    bouyer 			"minimum with block size %d is %d\n",
    222       1.1       cgd 		    sblock.fs_fsize, sblock.fs_bsize,
    223       1.1       cgd 		    sblock.fs_bsize / MAXFRAG);
    224       1.1       cgd 		exit(21);
    225       1.1       cgd 	}
    226       1.1       cgd 	sblock.fs_nrpos = nrpos;
    227       1.1       cgd 	sblock.fs_nindir = sblock.fs_bsize / sizeof(daddr_t);
    228      1.32   thorpej 	sblock.fs_inopb = sblock.fs_bsize / DINODE_SIZE;
    229       1.1       cgd 	sblock.fs_nspf = sblock.fs_fsize / sectorsize;
    230       1.1       cgd 	for (sblock.fs_fsbtodb = 0, i = NSPF(&sblock); i > 1; i >>= 1)
    231       1.1       cgd 		sblock.fs_fsbtodb++;
    232       1.1       cgd 	sblock.fs_sblkno =
    233       1.1       cgd 	    roundup(howmany(bbsize + sbsize, sblock.fs_fsize), sblock.fs_frag);
    234       1.1       cgd 	sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno +
    235       1.1       cgd 	    roundup(howmany(sbsize, sblock.fs_fsize), sblock.fs_frag));
    236       1.1       cgd 	sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag;
    237       1.1       cgd 	sblock.fs_cgoffset = roundup(
    238       1.1       cgd 	    howmany(sblock.fs_nsect, NSPF(&sblock)), sblock.fs_frag);
    239       1.1       cgd 	for (sblock.fs_cgmask = 0xffffffff, i = sblock.fs_ntrak; i > 1; i >>= 1)
    240       1.1       cgd 		sblock.fs_cgmask <<= 1;
    241       1.1       cgd 	if (!POWEROF2(sblock.fs_ntrak))
    242       1.1       cgd 		sblock.fs_cgmask <<= 1;
    243       1.9   mycroft 	sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
    244       1.9   mycroft 	for (sizepb = sblock.fs_bsize, i = 0; i < NIADDR; i++) {
    245       1.9   mycroft 		sizepb *= NINDIR(&sblock);
    246       1.9   mycroft 		sblock.fs_maxfilesize += sizepb;
    247       1.9   mycroft 	}
    248       1.1       cgd 	/*
    249       1.1       cgd 	 * Validate specified/determined secpercyl
    250       1.1       cgd 	 * and calculate minimum cylinders per group.
    251       1.1       cgd 	 */
    252       1.1       cgd 	sblock.fs_spc = secpercyl;
    253       1.1       cgd 	for (sblock.fs_cpc = NSPB(&sblock), i = sblock.fs_spc;
    254       1.1       cgd 	     sblock.fs_cpc > 1 && (i & 1) == 0;
    255       1.1       cgd 	     sblock.fs_cpc >>= 1, i >>= 1)
    256       1.1       cgd 		/* void */;
    257       1.1       cgd 	mincpc = sblock.fs_cpc;
    258       1.1       cgd 	bpcg = sblock.fs_spc * sectorsize;
    259      1.32   thorpej 	inospercg = roundup(bpcg / DINODE_SIZE, INOPB(&sblock));
    260       1.1       cgd 	if (inospercg > MAXIPG(&sblock))
    261       1.1       cgd 		inospercg = MAXIPG(&sblock);
    262       1.1       cgd 	used = (sblock.fs_iblkno + inospercg / INOPF(&sblock)) * NSPF(&sblock);
    263       1.1       cgd 	mincpgcnt = howmany(sblock.fs_cgoffset * (~sblock.fs_cgmask) + used,
    264       1.1       cgd 	    sblock.fs_spc);
    265       1.1       cgd 	mincpg = roundup(mincpgcnt, mincpc);
    266       1.1       cgd 	/*
    267       1.9   mycroft 	 * Ensure that cylinder group with mincpg has enough space
    268       1.9   mycroft 	 * for block maps.
    269       1.1       cgd 	 */
    270       1.1       cgd 	sblock.fs_cpg = mincpg;
    271       1.1       cgd 	sblock.fs_ipg = inospercg;
    272       1.9   mycroft 	if (maxcontig > 1)
    273       1.9   mycroft 		sblock.fs_contigsumsize = MIN(maxcontig, FS_MAXCONTIG);
    274       1.1       cgd 	mapcramped = 0;
    275       1.1       cgd 	while (CGSIZE(&sblock) > sblock.fs_bsize) {
    276       1.1       cgd 		mapcramped = 1;
    277       1.1       cgd 		if (sblock.fs_bsize < MAXBSIZE) {
    278       1.1       cgd 			sblock.fs_bsize <<= 1;
    279       1.1       cgd 			if ((i & 1) == 0) {
    280       1.1       cgd 				i >>= 1;
    281       1.1       cgd 			} else {
    282       1.1       cgd 				sblock.fs_cpc <<= 1;
    283       1.1       cgd 				mincpc <<= 1;
    284       1.1       cgd 				mincpg = roundup(mincpgcnt, mincpc);
    285       1.1       cgd 				sblock.fs_cpg = mincpg;
    286       1.1       cgd 			}
    287       1.1       cgd 			sblock.fs_frag <<= 1;
    288       1.1       cgd 			sblock.fs_fragshift += 1;
    289       1.1       cgd 			if (sblock.fs_frag <= MAXFRAG)
    290       1.1       cgd 				continue;
    291       1.1       cgd 		}
    292       1.1       cgd 		if (sblock.fs_fsize == sblock.fs_bsize) {
    293       1.1       cgd 			printf("There is no block size that");
    294       1.1       cgd 			printf(" can support this disk\n");
    295       1.1       cgd 			exit(22);
    296       1.1       cgd 		}
    297       1.1       cgd 		sblock.fs_frag >>= 1;
    298       1.1       cgd 		sblock.fs_fragshift -= 1;
    299       1.1       cgd 		sblock.fs_fsize <<= 1;
    300       1.1       cgd 		sblock.fs_nspf <<= 1;
    301       1.1       cgd 	}
    302       1.1       cgd 	/*
    303       1.9   mycroft 	 * Ensure that cylinder group with mincpg has enough space for inodes.
    304       1.1       cgd 	 */
    305       1.1       cgd 	inodecramped = 0;
    306      1.27     lukem 	inospercg = calcipg(mincpg, bpcg, &usedb);
    307       1.1       cgd 	sblock.fs_ipg = inospercg;
    308       1.1       cgd 	while (inospercg > MAXIPG(&sblock)) {
    309       1.1       cgd 		inodecramped = 1;
    310       1.1       cgd 		if (mincpc == 1 || sblock.fs_frag == 1 ||
    311       1.1       cgd 		    sblock.fs_bsize == MINBSIZE)
    312       1.1       cgd 			break;
    313      1.27     lukem 		printf("With a block size of %d %s %d\n", sblock.fs_bsize,
    314      1.27     lukem 		       "minimum bytes per inode is",
    315      1.27     lukem 		       (int)((mincpg * (off_t)bpcg - usedb)
    316      1.27     lukem 			     / MAXIPG(&sblock) + 1));
    317       1.1       cgd 		sblock.fs_bsize >>= 1;
    318       1.1       cgd 		sblock.fs_frag >>= 1;
    319       1.1       cgd 		sblock.fs_fragshift -= 1;
    320       1.1       cgd 		mincpc >>= 1;
    321       1.1       cgd 		sblock.fs_cpg = roundup(mincpgcnt, mincpc);
    322       1.1       cgd 		if (CGSIZE(&sblock) > sblock.fs_bsize) {
    323       1.1       cgd 			sblock.fs_bsize <<= 1;
    324       1.1       cgd 			break;
    325       1.1       cgd 		}
    326       1.1       cgd 		mincpg = sblock.fs_cpg;
    327      1.27     lukem 		inospercg = calcipg(mincpg, bpcg, &usedb);
    328       1.1       cgd 		sblock.fs_ipg = inospercg;
    329       1.1       cgd 	}
    330       1.1       cgd 	if (inodecramped) {
    331       1.1       cgd 		if (inospercg > MAXIPG(&sblock)) {
    332      1.27     lukem 			printf("Minimum bytes per inode is %d\n",
    333      1.27     lukem 			       (int)((mincpg * (off_t)bpcg - usedb)
    334      1.27     lukem 				     / MAXIPG(&sblock) + 1));
    335       1.1       cgd 		} else if (!mapcramped) {
    336       1.1       cgd 			printf("With %d bytes per inode, ", density);
    337      1.27     lukem 			printf("minimum cylinders per group is %d\n", mincpg);
    338       1.1       cgd 		}
    339       1.1       cgd 	}
    340       1.1       cgd 	if (mapcramped) {
    341       1.1       cgd 		printf("With %d sectors per cylinder, ", sblock.fs_spc);
    342      1.27     lukem 		printf("minimum cylinders per group is %d\n", mincpg);
    343       1.1       cgd 	}
    344       1.1       cgd 	if (inodecramped || mapcramped) {
    345       1.1       cgd 		if (sblock.fs_bsize != bsize)
    346       1.1       cgd 			printf("%s to be changed from %d to %d\n",
    347       1.1       cgd 			    "This requires the block size",
    348       1.1       cgd 			    bsize, sblock.fs_bsize);
    349       1.1       cgd 		if (sblock.fs_fsize != fsize)
    350       1.1       cgd 			printf("\t%s to be changed from %d to %d\n",
    351       1.1       cgd 			    "and the fragment size",
    352       1.1       cgd 			    fsize, sblock.fs_fsize);
    353       1.1       cgd 		exit(23);
    354       1.1       cgd 	}
    355       1.1       cgd 	/*
    356       1.1       cgd 	 * Calculate the number of cylinders per group
    357       1.1       cgd 	 */
    358       1.1       cgd 	sblock.fs_cpg = cpg;
    359       1.1       cgd 	if (sblock.fs_cpg % mincpc != 0) {
    360      1.27     lukem 		printf("%s groups must have a multiple of %d cylinders\n",
    361       1.1       cgd 			cpgflg ? "Cylinder" : "Warning: cylinder", mincpc);
    362       1.1       cgd 		sblock.fs_cpg = roundup(sblock.fs_cpg, mincpc);
    363       1.1       cgd 		if (!cpgflg)
    364       1.1       cgd 			cpg = sblock.fs_cpg;
    365       1.1       cgd 	}
    366       1.1       cgd 	/*
    367       1.9   mycroft 	 * Must ensure there is enough space for inodes.
    368       1.1       cgd 	 */
    369      1.27     lukem 	sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
    370       1.1       cgd 	while (sblock.fs_ipg > MAXIPG(&sblock)) {
    371       1.1       cgd 		inodecramped = 1;
    372       1.1       cgd 		sblock.fs_cpg -= mincpc;
    373      1.27     lukem 		sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
    374       1.1       cgd 	}
    375       1.1       cgd 	/*
    376       1.9   mycroft 	 * Must ensure there is enough space to hold block map.
    377       1.1       cgd 	 */
    378       1.1       cgd 	while (CGSIZE(&sblock) > sblock.fs_bsize) {
    379       1.1       cgd 		mapcramped = 1;
    380       1.1       cgd 		sblock.fs_cpg -= mincpc;
    381      1.27     lukem 		sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
    382       1.1       cgd 	}
    383       1.1       cgd 	sblock.fs_fpg = (sblock.fs_cpg * sblock.fs_spc) / NSPF(&sblock);
    384       1.1       cgd 	if ((sblock.fs_cpg * sblock.fs_spc) % NSPB(&sblock) != 0) {
    385      1.26  christos 		printf("panic (fs_cpg * fs_spc) %% NSPF != 0");
    386       1.1       cgd 		exit(24);
    387       1.1       cgd 	}
    388       1.1       cgd 	if (sblock.fs_cpg < mincpg) {
    389      1.27     lukem 		printf("cylinder groups must have at least %d cylinders\n",
    390       1.1       cgd 			mincpg);
    391       1.1       cgd 		exit(25);
    392       1.1       cgd 	} else if (sblock.fs_cpg != cpg) {
    393       1.1       cgd 		if (!cpgflg)
    394       1.1       cgd 			printf("Warning: ");
    395       1.1       cgd 		else if (!mapcramped && !inodecramped)
    396       1.1       cgd 			exit(26);
    397       1.1       cgd 		if (mapcramped && inodecramped)
    398       1.1       cgd 			printf("Block size and bytes per inode restrict");
    399       1.1       cgd 		else if (mapcramped)
    400       1.1       cgd 			printf("Block size restricts");
    401       1.1       cgd 		else
    402       1.1       cgd 			printf("Bytes per inode restrict");
    403       1.1       cgd 		printf(" cylinders per group to %d.\n", sblock.fs_cpg);
    404       1.1       cgd 		if (cpgflg)
    405       1.1       cgd 			exit(27);
    406       1.1       cgd 	}
    407       1.1       cgd 	sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock));
    408       1.1       cgd 	/*
    409       1.1       cgd 	 * Now have size for file system and nsect and ntrak.
    410       1.1       cgd 	 * Determine number of cylinders and blocks in the file system.
    411       1.1       cgd 	 */
    412       1.1       cgd 	sblock.fs_size = fssize = dbtofsb(&sblock, fssize);
    413       1.1       cgd 	sblock.fs_ncyl = fssize * NSPF(&sblock) / sblock.fs_spc;
    414       1.1       cgd 	if (fssize * NSPF(&sblock) > sblock.fs_ncyl * sblock.fs_spc) {
    415       1.1       cgd 		sblock.fs_ncyl++;
    416       1.1       cgd 		warn = 1;
    417       1.1       cgd 	}
    418       1.1       cgd 	if (sblock.fs_ncyl < 1) {
    419       1.1       cgd 		printf("file systems must have at least one cylinder\n");
    420       1.1       cgd 		exit(28);
    421       1.1       cgd 	}
    422       1.1       cgd 	/*
    423       1.1       cgd 	 * Determine feasability/values of rotational layout tables.
    424       1.1       cgd 	 *
    425       1.1       cgd 	 * The size of the rotational layout tables is limited by the
    426       1.1       cgd 	 * size of the superblock, SBSIZE. The amount of space available
    427       1.1       cgd 	 * for tables is calculated as (SBSIZE - sizeof (struct fs)).
    428       1.1       cgd 	 * The size of these tables is inversely proportional to the block
    429       1.1       cgd 	 * size of the file system. The size increases if sectors per track
    430       1.1       cgd 	 * are not powers of two, because more cylinders must be described
    431       1.1       cgd 	 * by the tables before the rotational pattern repeats (fs_cpc).
    432       1.1       cgd 	 */
    433       1.1       cgd 	sblock.fs_interleave = interleave;
    434       1.1       cgd 	sblock.fs_trackskew = trackskew;
    435       1.1       cgd 	sblock.fs_npsect = nphyssectors;
    436       1.1       cgd 	sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
    437       1.1       cgd 	sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs));
    438       1.1       cgd 	if (sblock.fs_ntrak == 1) {
    439       1.1       cgd 		sblock.fs_cpc = 0;
    440       1.1       cgd 		goto next;
    441       1.1       cgd 	}
    442      1.20       cgd 	postblsize = sblock.fs_nrpos * sblock.fs_cpc * sizeof(int16_t);
    443       1.1       cgd 	rotblsize = sblock.fs_cpc * sblock.fs_spc / NSPB(&sblock);
    444       1.1       cgd 	totalsbsize = sizeof(struct fs) + rotblsize;
    445       1.1       cgd 	if (sblock.fs_nrpos == 8 && sblock.fs_cpc <= 16) {
    446       1.1       cgd 		/* use old static table space */
    447       1.1       cgd 		sblock.fs_postbloff = (char *)(&sblock.fs_opostbl[0][0]) -
    448      1.15       cgd 		    (char *)(&sblock.fs_firstfield);
    449       1.1       cgd 		sblock.fs_rotbloff = &sblock.fs_space[0] -
    450      1.15       cgd 		    (u_char *)(&sblock.fs_firstfield);
    451       1.1       cgd 	} else {
    452       1.1       cgd 		/* use dynamic table space */
    453       1.1       cgd 		sblock.fs_postbloff = &sblock.fs_space[0] -
    454      1.15       cgd 		    (u_char *)(&sblock.fs_firstfield);
    455       1.1       cgd 		sblock.fs_rotbloff = sblock.fs_postbloff + postblsize;
    456       1.1       cgd 		totalsbsize += postblsize;
    457       1.1       cgd 	}
    458       1.1       cgd 	if (totalsbsize > SBSIZE ||
    459       1.1       cgd 	    sblock.fs_nsect > (1 << NBBY) * NSPB(&sblock)) {
    460       1.1       cgd 		printf("%s %s %d %s %d.%s",
    461       1.1       cgd 		    "Warning: insufficient space in super block for\n",
    462       1.1       cgd 		    "rotational layout tables with nsect", sblock.fs_nsect,
    463       1.1       cgd 		    "and ntrak", sblock.fs_ntrak,
    464       1.1       cgd 		    "\nFile system performance may be impaired.\n");
    465       1.1       cgd 		sblock.fs_cpc = 0;
    466       1.1       cgd 		goto next;
    467       1.1       cgd 	}
    468       1.1       cgd 	sblock.fs_sbsize = fragroundup(&sblock, totalsbsize);
    469       1.1       cgd 	/*
    470       1.1       cgd 	 * calculate the available blocks for each rotational position
    471       1.1       cgd 	 */
    472       1.1       cgd 	for (cylno = 0; cylno < sblock.fs_cpc; cylno++)
    473       1.1       cgd 		for (rpos = 0; rpos < sblock.fs_nrpos; rpos++)
    474       1.1       cgd 			fs_postbl(&sblock, cylno)[rpos] = -1;
    475       1.1       cgd 	for (i = (rotblsize - 1) * sblock.fs_frag;
    476       1.1       cgd 	     i >= 0; i -= sblock.fs_frag) {
    477       1.1       cgd 		cylno = cbtocylno(&sblock, i);
    478       1.1       cgd 		rpos = cbtorpos(&sblock, i);
    479       1.1       cgd 		blk = fragstoblks(&sblock, i);
    480       1.1       cgd 		if (fs_postbl(&sblock, cylno)[rpos] == -1)
    481       1.1       cgd 			fs_rotbl(&sblock)[blk] = 0;
    482       1.1       cgd 		else
    483      1.30    bouyer 			fs_rotbl(&sblock)[blk] = fs_postbl(&sblock, cylno)[rpos] - blk;
    484       1.1       cgd 		fs_postbl(&sblock, cylno)[rpos] = blk;
    485       1.1       cgd 	}
    486       1.1       cgd next:
    487       1.1       cgd 	/*
    488       1.1       cgd 	 * Compute/validate number of cylinder groups.
    489       1.1       cgd 	 */
    490       1.1       cgd 	sblock.fs_ncg = sblock.fs_ncyl / sblock.fs_cpg;
    491       1.1       cgd 	if (sblock.fs_ncyl % sblock.fs_cpg)
    492       1.1       cgd 		sblock.fs_ncg++;
    493       1.1       cgd 	sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock);
    494       1.1       cgd 	i = MIN(~sblock.fs_cgmask, sblock.fs_ncg - 1);
    495       1.1       cgd 	if (cgdmin(&sblock, i) - cgbase(&sblock, i) >= sblock.fs_fpg) {
    496      1.27     lukem 		printf("inode blocks/cyl group (%d) >= data blocks (%d)\n",
    497       1.1       cgd 		    cgdmin(&sblock, i) - cgbase(&sblock, i) / sblock.fs_frag,
    498       1.1       cgd 		    sblock.fs_fpg / sblock.fs_frag);
    499       1.1       cgd 		printf("number of cylinders per cylinder group (%d) %s.\n",
    500       1.1       cgd 		    sblock.fs_cpg, "must be increased");
    501       1.1       cgd 		exit(29);
    502       1.1       cgd 	}
    503       1.1       cgd 	j = sblock.fs_ncg - 1;
    504       1.1       cgd 	if ((i = fssize - j * sblock.fs_fpg) < sblock.fs_fpg &&
    505       1.1       cgd 	    cgdmin(&sblock, j) - cgbase(&sblock, j) > i) {
    506       1.1       cgd 		if (j == 0) {
    507  1.38.4.1        he 			printf("File system must have at least %d sectors\n",
    508       1.1       cgd 			    NSPF(&sblock) *
    509       1.1       cgd 			    (cgdmin(&sblock, 0) + 3 * sblock.fs_frag));
    510       1.1       cgd 			exit(30);
    511       1.1       cgd 		}
    512      1.30    bouyer 		printf("Warning: inode blocks/cyl group (%d) >= "
    513      1.30    bouyer 			"data blocks (%d) in last\n",
    514       1.1       cgd 		    (cgdmin(&sblock, j) - cgbase(&sblock, j)) / sblock.fs_frag,
    515       1.1       cgd 		    i / sblock.fs_frag);
    516      1.30    bouyer 		printf("    cylinder group. This implies %d sector(s) "
    517      1.30    bouyer 			"cannot be allocated.\n",
    518       1.1       cgd 		    i * NSPF(&sblock));
    519       1.1       cgd 		sblock.fs_ncg--;
    520       1.1       cgd 		sblock.fs_ncyl -= sblock.fs_ncyl % sblock.fs_cpg;
    521       1.1       cgd 		sblock.fs_size = fssize = sblock.fs_ncyl * sblock.fs_spc /
    522       1.1       cgd 		    NSPF(&sblock);
    523       1.1       cgd 		warn = 0;
    524       1.1       cgd 	}
    525       1.1       cgd 	if (warn && !mfs) {
    526       1.1       cgd 		printf("Warning: %d sector(s) in last cylinder unallocated\n",
    527       1.1       cgd 		    sblock.fs_spc -
    528       1.1       cgd 		    (fssize * NSPF(&sblock) - (sblock.fs_ncyl - 1)
    529       1.1       cgd 		    * sblock.fs_spc));
    530       1.1       cgd 	}
    531       1.1       cgd 	/*
    532       1.1       cgd 	 * fill in remaining fields of the super block
    533       1.1       cgd 	 */
    534       1.1       cgd 	sblock.fs_csaddr = cgdmin(&sblock, 0);
    535       1.1       cgd 	sblock.fs_cssize =
    536       1.1       cgd 	    fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
    537  1.38.4.4        he 	/*
    538  1.38.4.4        he 	 * The superblock fields 'fs_csmask' and 'fs_csshift' are no
    539  1.38.4.4        he 	 * longer used. However, we still initialise them so that the
    540  1.38.4.4        he 	 * filesystem remains compatible with old kernels.
    541  1.38.4.4        he 	 */
    542       1.1       cgd 	i = sblock.fs_bsize / sizeof(struct csum);
    543       1.1       cgd 	sblock.fs_csmask = ~(i - 1);
    544       1.1       cgd 	for (sblock.fs_csshift = 0; i > 1; i >>= 1)
    545       1.1       cgd 		sblock.fs_csshift++;
    546       1.1       cgd 	fscs = (struct csum *)calloc(1, sblock.fs_cssize);
    547  1.38.4.1        he 	if (fscs == NULL)
    548  1.38.4.1        he 		exit(39);
    549       1.1       cgd 	sblock.fs_magic = FS_MAGIC;
    550       1.1       cgd 	sblock.fs_rotdelay = rotdelay;
    551       1.1       cgd 	sblock.fs_minfree = minfree;
    552       1.1       cgd 	sblock.fs_maxcontig = maxcontig;
    553       1.1       cgd 	sblock.fs_maxbpg = maxbpg;
    554       1.1       cgd 	sblock.fs_rps = rpm / 60;
    555       1.1       cgd 	sblock.fs_optim = opt;
    556       1.1       cgd 	sblock.fs_cgrotor = 0;
    557       1.1       cgd 	sblock.fs_cstotal.cs_ndir = 0;
    558       1.1       cgd 	sblock.fs_cstotal.cs_nbfree = 0;
    559       1.1       cgd 	sblock.fs_cstotal.cs_nifree = 0;
    560       1.1       cgd 	sblock.fs_cstotal.cs_nffree = 0;
    561       1.1       cgd 	sblock.fs_fmod = 0;
    562      1.21   mycroft 	sblock.fs_clean = FS_ISCLEAN;
    563       1.1       cgd 	sblock.fs_ronly = 0;
    564       1.1       cgd 	/*
    565       1.1       cgd 	 * Dump out summary information about file system.
    566       1.1       cgd 	 */
    567       1.1       cgd 	if (!mfs) {
    568       1.1       cgd 		printf("%s:\t%d sectors in %d %s of %d tracks, %d sectors\n",
    569       1.1       cgd 		    fsys, sblock.fs_size * NSPF(&sblock), sblock.fs_ncyl,
    570       1.1       cgd 		    "cylinders", sblock.fs_ntrak, sblock.fs_nsect);
    571       1.9   mycroft #define B2MBFACTOR (1 / (1024.0 * 1024.0))
    572       1.1       cgd 		printf("\t%.1fMB in %d cyl groups (%d c/g, %.2fMB/g, %d i/g)\n",
    573       1.9   mycroft 		    (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
    574       1.1       cgd 		    sblock.fs_ncg, sblock.fs_cpg,
    575       1.9   mycroft 		    (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
    576       1.1       cgd 		    sblock.fs_ipg);
    577       1.9   mycroft #undef B2MBFACTOR
    578       1.1       cgd 	}
    579       1.1       cgd 	/*
    580      1.36  wrstuden 	 * Now determine how wide each column will be, and calculate how
    581      1.37  wrstuden 	 * many columns will fit in a 76 char line. 76 is the width of the
    582      1.37  wrstuden 	 * subwindows in sysinst.
    583      1.36  wrstuden 	 */
    584      1.36  wrstuden 	printcolwidth = count_digits(
    585      1.36  wrstuden 			fsbtodb(&sblock, cgsblock(&sblock, sblock.fs_ncg -1)));
    586      1.37  wrstuden 	nprintcols = 76 / (printcolwidth + 2);
    587      1.36  wrstuden 	/*
    588       1.1       cgd 	 * Now build the cylinders group blocks and
    589       1.1       cgd 	 * then print out indices of cylinder groups.
    590       1.1       cgd 	 */
    591       1.1       cgd 	if (!mfs)
    592       1.1       cgd 		printf("super-block backups (for fsck -b #) at:");
    593       1.1       cgd 	for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
    594       1.1       cgd 		initcg(cylno, utime);
    595       1.1       cgd 		if (mfs)
    596       1.1       cgd 			continue;
    597      1.36  wrstuden 		if (cylno % nprintcols == 0)
    598       1.1       cgd 			printf("\n");
    599      1.36  wrstuden 		printf(" %*d,", printcolwidth,
    600      1.36  wrstuden 				fsbtodb(&sblock, cgsblock(&sblock, cylno)));
    601      1.22       jtc 		fflush(stdout);
    602       1.1       cgd 	}
    603       1.1       cgd 	if (!mfs)
    604       1.1       cgd 		printf("\n");
    605       1.1       cgd 	if (Nflag && !mfs)
    606       1.1       cgd 		exit(0);
    607       1.1       cgd 	/*
    608       1.1       cgd 	 * Now construct the initial file system,
    609       1.1       cgd 	 * then write out the super-block.
    610       1.1       cgd 	 */
    611       1.1       cgd 	fsinit(utime);
    612       1.1       cgd 	sblock.fs_time = utime;
    613      1.30    bouyer 	memcpy(writebuf, &sblock, sbsize);
    614      1.30    bouyer 	if (needswap)
    615  1.38.4.2        he 		ffs_sb_swap(&sblock, (struct fs*)writebuf);
    616      1.30    bouyer 	wtfs((int)SBOFF / sectorsize, sbsize, writebuf);
    617       1.1       cgd 	/*
    618       1.1       cgd 	 * Write out the duplicate super blocks
    619       1.1       cgd 	 */
    620       1.1       cgd 	for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
    621       1.1       cgd 		wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)),
    622      1.30    bouyer 		    sbsize, writebuf);
    623      1.34  wrstuden 
    624      1.34  wrstuden 	/*
    625      1.34  wrstuden 	 * if we need to swap, create a buffer for the cylinder summaries
    626      1.34  wrstuden 	 * to get swapped to.
    627      1.34  wrstuden 	 */
    628      1.34  wrstuden 	if (needswap) {
    629      1.34  wrstuden 		if ((writebuf2=malloc(sblock.fs_cssize)) == NULL)
    630      1.34  wrstuden 			exit(12);
    631      1.34  wrstuden 		ffs_csum_swap(fscs, (struct csum*)writebuf2, sblock.fs_cssize);
    632      1.34  wrstuden 	} else
    633      1.34  wrstuden 		writebuf2 = (char *)fscs;
    634      1.34  wrstuden 
    635      1.30    bouyer 	for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize)
    636      1.30    bouyer 		wtfs(fsbtodb(&sblock, sblock.fs_csaddr + numfrags(&sblock, i)),
    637      1.30    bouyer 			sblock.fs_cssize - i < sblock.fs_bsize ?
    638      1.30    bouyer 			    sblock.fs_cssize - i : sblock.fs_bsize,
    639      1.34  wrstuden 			((char *)writebuf2) + i);
    640      1.34  wrstuden 	if (writebuf2 != (char *)fscs)
    641      1.34  wrstuden 		free(writebuf2);
    642      1.34  wrstuden 
    643       1.1       cgd 	/*
    644       1.1       cgd 	 * Update information about this partion in pack
    645       1.1       cgd 	 * label, to that it may be updated on disk.
    646       1.1       cgd 	 */
    647       1.1       cgd 	pp->p_fstype = FS_BSDFFS;
    648       1.1       cgd 	pp->p_fsize = sblock.fs_fsize;
    649       1.1       cgd 	pp->p_frag = sblock.fs_frag;
    650       1.1       cgd 	pp->p_cpg = sblock.fs_cpg;
    651       1.1       cgd }
    652       1.1       cgd 
    653       1.1       cgd /*
    654       1.1       cgd  * Initialize a cylinder group.
    655       1.1       cgd  */
    656      1.26  christos void
    657  1.38.4.1        he initcg(int cylno, time_t utime)
    658       1.1       cgd {
    659       1.9   mycroft 	daddr_t cbase, d, dlower, dupper, dmax, blkno;
    660      1.27     lukem 	int32_t i;
    661      1.26  christos 	struct csum *cs;
    662       1.1       cgd 
    663       1.1       cgd 	/*
    664       1.1       cgd 	 * Determine block bounds for cylinder group.
    665       1.1       cgd 	 * Allow space for super block summary information in first
    666       1.1       cgd 	 * cylinder group.
    667       1.1       cgd 	 */
    668       1.1       cgd 	cbase = cgbase(&sblock, cylno);
    669       1.1       cgd 	dmax = cbase + sblock.fs_fpg;
    670       1.1       cgd 	if (dmax > sblock.fs_size)
    671       1.1       cgd 		dmax = sblock.fs_size;
    672       1.1       cgd 	dlower = cgsblock(&sblock, cylno) - cbase;
    673       1.1       cgd 	dupper = cgdmin(&sblock, cylno) - cbase;
    674       1.1       cgd 	if (cylno == 0)
    675       1.1       cgd 		dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
    676       1.1       cgd 	cs = fscs + cylno;
    677      1.12   mycroft 	memset(&acg, 0, sblock.fs_cgsize);
    678       1.1       cgd 	acg.cg_time = utime;
    679       1.1       cgd 	acg.cg_magic = CG_MAGIC;
    680       1.1       cgd 	acg.cg_cgx = cylno;
    681       1.1       cgd 	if (cylno == sblock.fs_ncg - 1)
    682       1.1       cgd 		acg.cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg;
    683       1.1       cgd 	else
    684       1.1       cgd 		acg.cg_ncyl = sblock.fs_cpg;
    685       1.1       cgd 	acg.cg_niblk = sblock.fs_ipg;
    686       1.1       cgd 	acg.cg_ndblk = dmax - cbase;
    687       1.9   mycroft 	if (sblock.fs_contigsumsize > 0)
    688       1.9   mycroft 		acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag;
    689      1.15       cgd 	acg.cg_btotoff = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield);
    690      1.14       cgd 	acg.cg_boff = acg.cg_btotoff + sblock.fs_cpg * sizeof(int32_t);
    691       1.1       cgd 	acg.cg_iusedoff = acg.cg_boff +
    692      1.20       cgd 		sblock.fs_cpg * sblock.fs_nrpos * sizeof(int16_t);
    693       1.1       cgd 	acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, NBBY);
    694       1.9   mycroft 	if (sblock.fs_contigsumsize <= 0) {
    695       1.9   mycroft 		acg.cg_nextfreeoff = acg.cg_freeoff +
    696       1.9   mycroft 		   howmany(sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY);
    697       1.9   mycroft 	} else {
    698       1.9   mycroft 		acg.cg_clustersumoff = acg.cg_freeoff + howmany
    699       1.9   mycroft 		    (sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY) -
    700      1.14       cgd 		    sizeof(int32_t);
    701       1.9   mycroft 		acg.cg_clustersumoff =
    702      1.14       cgd 		    roundup(acg.cg_clustersumoff, sizeof(int32_t));
    703       1.9   mycroft 		acg.cg_clusteroff = acg.cg_clustersumoff +
    704      1.14       cgd 		    (sblock.fs_contigsumsize + 1) * sizeof(int32_t);
    705       1.9   mycroft 		acg.cg_nextfreeoff = acg.cg_clusteroff + howmany
    706       1.9   mycroft 		    (sblock.fs_cpg * sblock.fs_spc / NSPB(&sblock), NBBY);
    707       1.9   mycroft 	}
    708  1.38.4.1        he 	if (acg.cg_nextfreeoff > sblock.fs_cgsize) {
    709       1.9   mycroft 		printf("Panic: cylinder group too big\n");
    710       1.9   mycroft 		exit(37);
    711       1.1       cgd 	}
    712       1.1       cgd 	acg.cg_cs.cs_nifree += sblock.fs_ipg;
    713       1.1       cgd 	if (cylno == 0)
    714       1.1       cgd 		for (i = 0; i < ROOTINO; i++) {
    715      1.30    bouyer 			setbit(cg_inosused(&acg, 0), i);
    716       1.1       cgd 			acg.cg_cs.cs_nifree--;
    717       1.1       cgd 		}
    718       1.1       cgd 	for (i = 0; i < sblock.fs_ipg / INOPF(&sblock); i += sblock.fs_frag)
    719       1.1       cgd 		wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
    720       1.1       cgd 		    sblock.fs_bsize, (char *)zino);
    721       1.1       cgd 	if (cylno > 0) {
    722       1.1       cgd 		/*
    723       1.1       cgd 		 * In cylno 0, beginning space is reserved
    724       1.1       cgd 		 * for boot and super blocks.
    725       1.1       cgd 		 */
    726       1.1       cgd 		for (d = 0; d < dlower; d += sblock.fs_frag) {
    727       1.9   mycroft 			blkno = d / sblock.fs_frag;
    728      1.30    bouyer 			setblock(&sblock, cg_blksfree(&acg, 0), blkno);
    729       1.9   mycroft 			if (sblock.fs_contigsumsize > 0)
    730      1.30    bouyer 				setbit(cg_clustersfree(&acg, 0), blkno);
    731       1.1       cgd 			acg.cg_cs.cs_nbfree++;
    732      1.30    bouyer 			cg_blktot(&acg, 0)[cbtocylno(&sblock, d)]++;
    733      1.30    bouyer 			cg_blks(&sblock, &acg, cbtocylno(&sblock, d), 0)
    734       1.1       cgd 			    [cbtorpos(&sblock, d)]++;
    735       1.1       cgd 		}
    736       1.1       cgd 		sblock.fs_dsize += dlower;
    737       1.1       cgd 	}
    738       1.1       cgd 	sblock.fs_dsize += acg.cg_ndblk - dupper;
    739      1.26  christos 	if ((i = (dupper % sblock.fs_frag)) != 0) {
    740       1.1       cgd 		acg.cg_frsum[sblock.fs_frag - i]++;
    741       1.1       cgd 		for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
    742      1.30    bouyer 			setbit(cg_blksfree(&acg, 0), dupper);
    743       1.1       cgd 			acg.cg_cs.cs_nffree++;
    744       1.1       cgd 		}
    745       1.1       cgd 	}
    746       1.1       cgd 	for (d = dupper; d + sblock.fs_frag <= dmax - cbase; ) {
    747       1.9   mycroft 		blkno = d / sblock.fs_frag;
    748      1.30    bouyer 		setblock(&sblock, cg_blksfree(&acg, 0), blkno);
    749       1.9   mycroft 		if (sblock.fs_contigsumsize > 0)
    750      1.30    bouyer 			setbit(cg_clustersfree(&acg, 0), blkno);
    751       1.1       cgd 		acg.cg_cs.cs_nbfree++;
    752      1.30    bouyer 		cg_blktot(&acg, 0)[cbtocylno(&sblock, d)]++;
    753      1.30    bouyer 		cg_blks(&sblock, &acg, cbtocylno(&sblock, d), 0)
    754       1.1       cgd 		    [cbtorpos(&sblock, d)]++;
    755       1.1       cgd 		d += sblock.fs_frag;
    756       1.1       cgd 	}
    757       1.1       cgd 	if (d < dmax - cbase) {
    758       1.1       cgd 		acg.cg_frsum[dmax - cbase - d]++;
    759       1.1       cgd 		for (; d < dmax - cbase; d++) {
    760      1.30    bouyer 			setbit(cg_blksfree(&acg, 0), d);
    761       1.1       cgd 			acg.cg_cs.cs_nffree++;
    762       1.1       cgd 		}
    763       1.1       cgd 	}
    764       1.9   mycroft 	if (sblock.fs_contigsumsize > 0) {
    765      1.30    bouyer 		int32_t *sump = cg_clustersum(&acg, 0);
    766      1.30    bouyer 		u_char *mapp = cg_clustersfree(&acg, 0);
    767       1.9   mycroft 		int map = *mapp++;
    768       1.9   mycroft 		int bit = 1;
    769       1.9   mycroft 		int run = 0;
    770       1.9   mycroft 
    771       1.9   mycroft 		for (i = 0; i < acg.cg_nclusterblks; i++) {
    772       1.9   mycroft 			if ((map & bit) != 0) {
    773       1.9   mycroft 				run++;
    774       1.9   mycroft 			} else if (run != 0) {
    775       1.9   mycroft 				if (run > sblock.fs_contigsumsize)
    776       1.9   mycroft 					run = sblock.fs_contigsumsize;
    777       1.9   mycroft 				sump[run]++;
    778       1.9   mycroft 				run = 0;
    779       1.9   mycroft 			}
    780       1.9   mycroft 			if ((i & (NBBY - 1)) != (NBBY - 1)) {
    781       1.9   mycroft 				bit <<= 1;
    782       1.9   mycroft 			} else {
    783       1.9   mycroft 				map = *mapp++;
    784       1.9   mycroft 				bit = 1;
    785       1.9   mycroft 			}
    786       1.9   mycroft 		}
    787       1.9   mycroft 		if (run != 0) {
    788       1.9   mycroft 			if (run > sblock.fs_contigsumsize)
    789       1.9   mycroft 				run = sblock.fs_contigsumsize;
    790       1.9   mycroft 			sump[run]++;
    791       1.9   mycroft 		}
    792       1.9   mycroft 	}
    793       1.1       cgd 	sblock.fs_cstotal.cs_ndir += acg.cg_cs.cs_ndir;
    794       1.1       cgd 	sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree;
    795       1.1       cgd 	sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree;
    796       1.1       cgd 	sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree;
    797       1.1       cgd 	*cs = acg.cg_cs;
    798      1.30    bouyer 	memcpy(writebuf, &acg, sblock.fs_bsize);
    799      1.30    bouyer 	if (needswap)
    800      1.30    bouyer 		swap_cg(&acg, (struct cg*)writebuf);
    801       1.1       cgd 	wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
    802      1.30    bouyer 		sblock.fs_bsize, writebuf);
    803       1.1       cgd }
    804       1.1       cgd 
    805       1.1       cgd /*
    806       1.1       cgd  * initialize the file system
    807       1.1       cgd  */
    808       1.1       cgd struct dinode node;
    809       1.1       cgd 
    810       1.1       cgd #ifdef LOSTDIR
    811       1.1       cgd #define PREDEFDIR 3
    812       1.1       cgd #else
    813       1.1       cgd #define PREDEFDIR 2
    814       1.1       cgd #endif
    815       1.1       cgd 
    816       1.1       cgd struct direct root_dir[] = {
    817       1.9   mycroft 	{ ROOTINO, sizeof(struct direct), DT_DIR, 1, "." },
    818       1.9   mycroft 	{ ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
    819       1.9   mycroft #ifdef LOSTDIR
    820       1.9   mycroft 	{ LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 10, "lost+found" },
    821       1.9   mycroft #endif
    822       1.9   mycroft };
    823       1.9   mycroft struct odirect {
    824      1.14       cgd 	u_int32_t d_ino;
    825      1.14       cgd 	u_int16_t d_reclen;
    826      1.14       cgd 	u_int16_t d_namlen;
    827       1.9   mycroft 	u_char	d_name[MAXNAMLEN + 1];
    828       1.9   mycroft } oroot_dir[] = {
    829       1.1       cgd 	{ ROOTINO, sizeof(struct direct), 1, "." },
    830       1.1       cgd 	{ ROOTINO, sizeof(struct direct), 2, ".." },
    831       1.1       cgd #ifdef LOSTDIR
    832       1.1       cgd 	{ LOSTFOUNDINO, sizeof(struct direct), 10, "lost+found" },
    833       1.1       cgd #endif
    834       1.1       cgd };
    835       1.1       cgd #ifdef LOSTDIR
    836       1.1       cgd struct direct lost_found_dir[] = {
    837       1.9   mycroft 	{ LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 1, "." },
    838       1.9   mycroft 	{ ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
    839       1.9   mycroft 	{ 0, DIRBLKSIZ, 0, 0, 0 },
    840       1.9   mycroft };
    841       1.9   mycroft struct odirect olost_found_dir[] = {
    842       1.1       cgd 	{ LOSTFOUNDINO, sizeof(struct direct), 1, "." },
    843       1.1       cgd 	{ ROOTINO, sizeof(struct direct), 2, ".." },
    844       1.1       cgd 	{ 0, DIRBLKSIZ, 0, 0 },
    845       1.1       cgd };
    846       1.1       cgd #endif
    847       1.1       cgd char buf[MAXBSIZE];
    848  1.38.4.1        he static void copy_dir(struct direct *, struct direct *);
    849       1.1       cgd 
    850      1.26  christos void
    851  1.38.4.1        he fsinit(time_t utime)
    852       1.1       cgd {
    853      1.26  christos #ifdef LOSTDIR
    854       1.1       cgd 	int i;
    855      1.26  christos #endif
    856       1.1       cgd 
    857       1.1       cgd 	/*
    858       1.1       cgd 	 * initialize the node
    859       1.1       cgd 	 */
    860      1.33    simonb 	memset(&node, 0, sizeof(node));
    861      1.25       cgd 	node.di_atime = utime;
    862      1.25       cgd 	node.di_mtime = utime;
    863      1.25       cgd 	node.di_ctime = utime;
    864      1.30    bouyer 
    865       1.1       cgd #ifdef LOSTDIR
    866       1.1       cgd 	/*
    867       1.1       cgd 	 * create the lost+found directory
    868       1.1       cgd 	 */
    869       1.9   mycroft 	if (Oflag) {
    870       1.9   mycroft 		(void)makedir((struct direct *)olost_found_dir, 2);
    871       1.9   mycroft 		for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ)
    872      1.30    bouyer 			copy_dir((struct direct*)&olost_found_dir[2],
    873      1.30    bouyer 				(struct direct*)&buf[i]);
    874       1.9   mycroft 	} else {
    875       1.9   mycroft 		(void)makedir(lost_found_dir, 2);
    876       1.9   mycroft 		for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ)
    877      1.30    bouyer 			copy_dir(&lost_found_dir[2], (struct direct*)&buf[i]);
    878       1.9   mycroft 	}
    879       1.1       cgd 	node.di_mode = IFDIR | UMASK;
    880       1.1       cgd 	node.di_nlink = 2;
    881       1.1       cgd 	node.di_size = sblock.fs_bsize;
    882       1.1       cgd 	node.di_db[0] = alloc(node.di_size, node.di_mode);
    883       1.1       cgd 	node.di_blocks = btodb(fragroundup(&sblock, node.di_size));
    884      1.35  wrstuden 	node.di_uid = geteuid();
    885      1.35  wrstuden 	node.di_gid = getegid();
    886       1.1       cgd 	wtfs(fsbtodb(&sblock, node.di_db[0]), node.di_size, buf);
    887       1.1       cgd 	iput(&node, LOSTFOUNDINO);
    888       1.1       cgd #endif
    889       1.1       cgd 	/*
    890       1.1       cgd 	 * create the root directory
    891       1.1       cgd 	 */
    892       1.1       cgd 	if (mfs)
    893       1.1       cgd 		node.di_mode = IFDIR | 01777;
    894       1.1       cgd 	else
    895       1.1       cgd 		node.di_mode = IFDIR | UMASK;
    896       1.1       cgd 	node.di_nlink = PREDEFDIR;
    897       1.9   mycroft 	if (Oflag)
    898       1.9   mycroft 		node.di_size = makedir((struct direct *)oroot_dir, PREDEFDIR);
    899       1.9   mycroft 	else
    900       1.9   mycroft 		node.di_size = makedir(root_dir, PREDEFDIR);
    901       1.1       cgd 	node.di_db[0] = alloc(sblock.fs_fsize, node.di_mode);
    902       1.1       cgd 	node.di_blocks = btodb(fragroundup(&sblock, node.di_size));
    903      1.35  wrstuden 	node.di_uid = geteuid();
    904      1.35  wrstuden 	node.di_gid = getegid();
    905       1.1       cgd 	wtfs(fsbtodb(&sblock, node.di_db[0]), sblock.fs_fsize, buf);
    906       1.1       cgd 	iput(&node, ROOTINO);
    907       1.1       cgd }
    908       1.1       cgd 
    909       1.1       cgd /*
    910       1.1       cgd  * construct a set of directory entries in "buf".
    911       1.1       cgd  * return size of directory.
    912       1.1       cgd  */
    913      1.26  christos int
    914  1.38.4.1        he makedir(struct direct *protodir, int entries)
    915       1.1       cgd {
    916       1.1       cgd 	char *cp;
    917       1.1       cgd 	int i, spcleft;
    918       1.1       cgd 
    919       1.1       cgd 	spcleft = DIRBLKSIZ;
    920       1.1       cgd 	for (cp = buf, i = 0; i < entries - 1; i++) {
    921      1.30    bouyer 		protodir[i].d_reclen = DIRSIZ(Oflag, &protodir[i], 0);
    922      1.30    bouyer 		copy_dir(&protodir[i], (struct direct*)cp);
    923       1.1       cgd 		cp += protodir[i].d_reclen;
    924       1.1       cgd 		spcleft -= protodir[i].d_reclen;
    925       1.1       cgd 	}
    926       1.1       cgd 	protodir[i].d_reclen = spcleft;
    927      1.30    bouyer 	copy_dir(&protodir[i], (struct direct*)cp);
    928       1.1       cgd 	return (DIRBLKSIZ);
    929       1.1       cgd }
    930       1.1       cgd 
    931       1.1       cgd /*
    932       1.1       cgd  * allocate a block or frag
    933       1.1       cgd  */
    934       1.1       cgd daddr_t
    935  1.38.4.1        he alloc(int size, int mode)
    936       1.1       cgd {
    937       1.1       cgd 	int i, frag;
    938       1.9   mycroft 	daddr_t d, blkno;
    939       1.1       cgd 
    940      1.26  christos 	rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
    941      1.30    bouyer 	/* fs -> host byte order */
    942      1.30    bouyer 	if (needswap)
    943      1.30    bouyer 		swap_cg(&acg, &acg);
    944       1.1       cgd 	if (acg.cg_magic != CG_MAGIC) {
    945       1.1       cgd 		printf("cg 0: bad magic number\n");
    946       1.1       cgd 		return (0);
    947       1.1       cgd 	}
    948       1.1       cgd 	if (acg.cg_cs.cs_nbfree == 0) {
    949       1.1       cgd 		printf("first cylinder group ran out of space\n");
    950       1.1       cgd 		return (0);
    951       1.1       cgd 	}
    952       1.1       cgd 	for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag)
    953      1.30    bouyer 		if (isblock(&sblock, cg_blksfree(&acg, 0), d / sblock.fs_frag))
    954       1.1       cgd 			goto goth;
    955       1.1       cgd 	printf("internal error: can't find block in cyl 0\n");
    956       1.1       cgd 	return (0);
    957       1.1       cgd goth:
    958       1.9   mycroft 	blkno = fragstoblks(&sblock, d);
    959      1.30    bouyer 	clrblock(&sblock, cg_blksfree(&acg, 0), blkno);
    960      1.10       cgd 	if (sblock.fs_contigsumsize > 0)
    961      1.30    bouyer 		clrbit(cg_clustersfree(&acg, 0), blkno);
    962       1.1       cgd 	acg.cg_cs.cs_nbfree--;
    963       1.1       cgd 	sblock.fs_cstotal.cs_nbfree--;
    964       1.1       cgd 	fscs[0].cs_nbfree--;
    965       1.1       cgd 	if (mode & IFDIR) {
    966       1.1       cgd 		acg.cg_cs.cs_ndir++;
    967       1.1       cgd 		sblock.fs_cstotal.cs_ndir++;
    968       1.1       cgd 		fscs[0].cs_ndir++;
    969       1.1       cgd 	}
    970      1.30    bouyer 	cg_blktot(&acg, 0)[cbtocylno(&sblock, d)]--;
    971      1.30    bouyer 	cg_blks(&sblock, &acg, cbtocylno(&sblock, d), 0)[cbtorpos(&sblock, d)]--;
    972       1.1       cgd 	if (size != sblock.fs_bsize) {
    973       1.1       cgd 		frag = howmany(size, sblock.fs_fsize);
    974       1.1       cgd 		fscs[0].cs_nffree += sblock.fs_frag - frag;
    975       1.1       cgd 		sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag;
    976       1.1       cgd 		acg.cg_cs.cs_nffree += sblock.fs_frag - frag;
    977       1.1       cgd 		acg.cg_frsum[sblock.fs_frag - frag]++;
    978       1.1       cgd 		for (i = frag; i < sblock.fs_frag; i++)
    979      1.30    bouyer 			setbit(cg_blksfree(&acg, 0), d + i);
    980       1.1       cgd 	}
    981      1.30    bouyer 	/* host -> fs byte order */
    982      1.30    bouyer 	if (needswap)
    983      1.30    bouyer 		swap_cg(&acg, &acg);
    984       1.1       cgd 	wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
    985       1.1       cgd 	    (char *)&acg);
    986       1.1       cgd 	return (d);
    987       1.1       cgd }
    988       1.1       cgd 
    989       1.1       cgd /*
    990      1.27     lukem  * Calculate number of inodes per group.
    991      1.27     lukem  */
    992      1.27     lukem int32_t
    993  1.38.4.1        he calcipg(int32_t cylpg, int32_t bpcg, off_t *usedbp)
    994      1.27     lukem {
    995      1.27     lukem 	int i;
    996      1.27     lukem 	int32_t ipg, new_ipg, ncg, ncyl;
    997      1.27     lukem 	off_t usedb;
    998      1.28    jeremy #if __GNUC__ /* XXX work around gcc 2.7.2 initialization bug */
    999      1.28    jeremy 	(void)&usedb;
   1000      1.28    jeremy #endif
   1001      1.27     lukem 
   1002      1.27     lukem 	/*
   1003      1.27     lukem 	 * Prepare to scale by fssize / (number of sectors in cylinder groups).
   1004  1.38.4.1        he 	 * Note that fssize is still in sectors, not file system blocks.
   1005      1.27     lukem 	 */
   1006      1.27     lukem 	ncyl = howmany(fssize, secpercyl);
   1007  1.38.4.1        he 	ncg = howmany(ncyl, cylpg);
   1008      1.27     lukem 	/*
   1009      1.27     lukem 	 * Iterate a few times to allow for ipg depending on itself.
   1010      1.27     lukem 	 */
   1011      1.27     lukem 	ipg = 0;
   1012      1.27     lukem 	for (i = 0; i < 10; i++) {
   1013      1.27     lukem 		usedb = (sblock.fs_iblkno + ipg / INOPF(&sblock))
   1014      1.27     lukem 			* NSPF(&sblock) * (off_t)sectorsize;
   1015  1.38.4.1        he 		new_ipg = (cylpg * (quad_t)bpcg - usedb) / density * fssize
   1016  1.38.4.1        he 			  / ncg / secpercyl / cylpg;
   1017      1.27     lukem 		new_ipg = roundup(new_ipg, INOPB(&sblock));
   1018      1.27     lukem 		if (new_ipg == ipg)
   1019      1.27     lukem 			break;
   1020      1.27     lukem 		ipg = new_ipg;
   1021      1.27     lukem 	}
   1022      1.27     lukem 	*usedbp = usedb;
   1023      1.27     lukem 	return (ipg);
   1024      1.27     lukem }
   1025      1.27     lukem 
   1026      1.27     lukem /*
   1027       1.1       cgd  * Allocate an inode on the disk
   1028       1.1       cgd  */
   1029      1.26  christos static void
   1030  1.38.4.1        he iput(struct dinode *ip, ino_t ino)
   1031       1.1       cgd {
   1032  1.38.4.1        he 	struct dinode ibuf[MAXINOPB];
   1033       1.1       cgd 	daddr_t d;
   1034      1.30    bouyer 	int c, i;
   1035       1.1       cgd 
   1036       1.9   mycroft 	c = ino_to_cg(&sblock, ino);
   1037      1.26  christos 	rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
   1038      1.30    bouyer 	/* fs -> host byte order */
   1039      1.30    bouyer 	if (needswap)
   1040      1.30    bouyer 		swap_cg(&acg, &acg);
   1041       1.1       cgd 	if (acg.cg_magic != CG_MAGIC) {
   1042       1.1       cgd 		printf("cg 0: bad magic number\n");
   1043       1.1       cgd 		exit(31);
   1044       1.1       cgd 	}
   1045       1.1       cgd 	acg.cg_cs.cs_nifree--;
   1046      1.30    bouyer 	setbit(cg_inosused(&acg, 0), ino);
   1047      1.30    bouyer 	/* host -> fs byte order */
   1048      1.30    bouyer 	if (needswap)
   1049      1.30    bouyer 		swap_cg(&acg, &acg);
   1050       1.1       cgd 	wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
   1051       1.1       cgd 	    (char *)&acg);
   1052       1.1       cgd 	sblock.fs_cstotal.cs_nifree--;
   1053       1.1       cgd 	fscs[0].cs_nifree--;
   1054       1.1       cgd 	if (ino >= sblock.fs_ipg * sblock.fs_ncg) {
   1055       1.1       cgd 		printf("fsinit: inode value out of range (%d).\n", ino);
   1056       1.1       cgd 		exit(32);
   1057       1.1       cgd 	}
   1058       1.9   mycroft 	d = fsbtodb(&sblock, ino_to_fsba(&sblock, ino));
   1059  1.38.4.1        he 	rdfs(d, sblock.fs_bsize, ibuf);
   1060      1.30    bouyer 	if (needswap) {
   1061  1.38.4.1        he 		ffs_dinode_swap(ip, &ibuf[ino_to_fsbo(&sblock, ino)]);
   1062      1.30    bouyer 		/* ffs_dinode_swap() doesn't swap blocks addrs */
   1063      1.30    bouyer 		for (i=0; i<NDADDR + NIADDR; i++)
   1064  1.38.4.1        he 			(&ibuf[ino_to_fsbo(&sblock, ino)])->di_db[i] =
   1065      1.30    bouyer 				bswap32(ip->di_db[i]);
   1066      1.30    bouyer 	} else
   1067  1.38.4.1        he 		ibuf[ino_to_fsbo(&sblock, ino)] = *ip;
   1068  1.38.4.1        he 	wtfs(d, sblock.fs_bsize, ibuf);
   1069       1.1       cgd }
   1070       1.1       cgd 
   1071       1.1       cgd /*
   1072       1.1       cgd  * Replace libc function with one suited to our needs.
   1073       1.1       cgd  */
   1074      1.26  christos void *
   1075  1.38.4.1        he malloc(size_t size)
   1076       1.1       cgd {
   1077  1.38.4.1        he 	void *p;
   1078       1.9   mycroft 	char *base, *i;
   1079       1.1       cgd 	static u_long pgsz;
   1080       1.1       cgd 	struct rlimit rlp;
   1081       1.1       cgd 
   1082       1.1       cgd 	if (pgsz == 0) {
   1083       1.1       cgd 		base = sbrk(0);
   1084       1.1       cgd 		pgsz = getpagesize() - 1;
   1085       1.9   mycroft 		i = (char *)((u_long)(base + pgsz) &~ pgsz);
   1086       1.1       cgd 		base = sbrk(i - base);
   1087       1.1       cgd 		if (getrlimit(RLIMIT_DATA, &rlp) < 0)
   1088       1.1       cgd 			perror("getrlimit");
   1089       1.1       cgd 		rlp.rlim_cur = rlp.rlim_max;
   1090       1.1       cgd 		if (setrlimit(RLIMIT_DATA, &rlp) < 0)
   1091       1.1       cgd 			perror("setrlimit");
   1092       1.9   mycroft 		memleft = rlp.rlim_max - (u_long)base;
   1093       1.1       cgd 	}
   1094       1.1       cgd 	size = (size + pgsz) &~ pgsz;
   1095       1.1       cgd 	if (size > memleft)
   1096       1.1       cgd 		size = memleft;
   1097       1.1       cgd 	memleft -= size;
   1098       1.1       cgd 	if (size == 0)
   1099  1.38.4.1        he 		return (NULL);
   1100  1.38.4.1        he 	p = sbrk(size);
   1101  1.38.4.1        he 	if (p == (void *)-1)
   1102  1.38.4.1        he 		p = NULL;
   1103  1.38.4.1        he 	return (p);
   1104       1.1       cgd }
   1105       1.1       cgd 
   1106       1.1       cgd /*
   1107       1.1       cgd  * Replace libc function with one suited to our needs.
   1108       1.1       cgd  */
   1109      1.26  christos void *
   1110  1.38.4.1        he realloc(void *ptr, size_t size)
   1111       1.1       cgd {
   1112       1.9   mycroft 	void *p;
   1113       1.1       cgd 
   1114       1.9   mycroft 	if ((p = malloc(size)) == NULL)
   1115       1.9   mycroft 		return (NULL);
   1116      1.27     lukem 	memmove(p, ptr, size);
   1117       1.9   mycroft 	free(ptr);
   1118       1.9   mycroft 	return (p);
   1119       1.1       cgd }
   1120       1.1       cgd 
   1121       1.1       cgd /*
   1122       1.1       cgd  * Replace libc function with one suited to our needs.
   1123       1.1       cgd  */
   1124      1.26  christos void *
   1125  1.38.4.1        he calloc(size_t size, size_t numelm)
   1126       1.1       cgd {
   1127      1.26  christos 	void *base;
   1128       1.1       cgd 
   1129       1.1       cgd 	size *= numelm;
   1130       1.1       cgd 	base = malloc(size);
   1131  1.38.4.1        he 	if (base == NULL)
   1132  1.38.4.1        he 		return (NULL);
   1133      1.12   mycroft 	memset(base, 0, size);
   1134      1.27     lukem 	return (base);
   1135       1.1       cgd }
   1136       1.1       cgd 
   1137       1.1       cgd /*
   1138       1.1       cgd  * Replace libc function with one suited to our needs.
   1139       1.1       cgd  */
   1140      1.26  christos void
   1141  1.38.4.1        he free(void *ptr)
   1142       1.1       cgd {
   1143       1.1       cgd 
   1144       1.1       cgd 	/* do not worry about it for now */
   1145       1.1       cgd }
   1146       1.1       cgd 
   1147       1.1       cgd /*
   1148       1.1       cgd  * read a block from the file system
   1149       1.1       cgd  */
   1150      1.26  christos void
   1151  1.38.4.1        he rdfs(daddr_t bno, int size, void *bf)
   1152       1.1       cgd {
   1153       1.1       cgd 	int n;
   1154      1.18       cgd 	off_t offset;
   1155       1.1       cgd 
   1156       1.1       cgd 	if (mfs) {
   1157      1.27     lukem 		memmove(bf, membase + bno * sectorsize, size);
   1158       1.1       cgd 		return;
   1159       1.1       cgd 	}
   1160      1.18       cgd 	offset = bno;
   1161      1.18       cgd 	offset *= sectorsize;
   1162      1.18       cgd 	if (lseek(fsi, offset, SEEK_SET) < 0) {
   1163      1.26  christos 		printf("seek error: %d\n", bno);
   1164       1.1       cgd 		perror("rdfs");
   1165       1.1       cgd 		exit(33);
   1166       1.1       cgd 	}
   1167       1.1       cgd 	n = read(fsi, bf, size);
   1168       1.9   mycroft 	if (n != size) {
   1169      1.26  christos 		printf("read error: %d\n", bno);
   1170       1.1       cgd 		perror("rdfs");
   1171       1.1       cgd 		exit(34);
   1172       1.1       cgd 	}
   1173       1.1       cgd }
   1174       1.1       cgd 
   1175       1.1       cgd /*
   1176       1.1       cgd  * write a block to the file system
   1177       1.1       cgd  */
   1178      1.26  christos void
   1179  1.38.4.1        he wtfs(daddr_t bno, int size, void *bf)
   1180       1.1       cgd {
   1181       1.1       cgd 	int n;
   1182      1.18       cgd 	off_t offset;
   1183       1.1       cgd 
   1184       1.1       cgd 	if (mfs) {
   1185      1.27     lukem 		memmove(membase + bno * sectorsize, bf, size);
   1186       1.1       cgd 		return;
   1187       1.1       cgd 	}
   1188       1.1       cgd 	if (Nflag)
   1189       1.1       cgd 		return;
   1190      1.18       cgd 	offset = bno;
   1191      1.18       cgd 	offset *= sectorsize;
   1192      1.18       cgd 	if (lseek(fso, offset, SEEK_SET) < 0) {
   1193      1.26  christos 		printf("seek error: %d\n", bno);
   1194       1.1       cgd 		perror("wtfs");
   1195       1.1       cgd 		exit(35);
   1196       1.1       cgd 	}
   1197       1.1       cgd 	n = write(fso, bf, size);
   1198       1.9   mycroft 	if (n != size) {
   1199      1.26  christos 		printf("write error: %d\n", bno);
   1200       1.1       cgd 		perror("wtfs");
   1201       1.1       cgd 		exit(36);
   1202       1.1       cgd 	}
   1203       1.1       cgd }
   1204       1.1       cgd 
   1205       1.1       cgd /*
   1206       1.1       cgd  * check if a block is available
   1207       1.1       cgd  */
   1208      1.26  christos int
   1209  1.38.4.1        he isblock(struct fs *fs, unsigned char *cp, int h)
   1210       1.1       cgd {
   1211       1.1       cgd 	unsigned char mask;
   1212       1.1       cgd 
   1213       1.1       cgd 	switch (fs->fs_frag) {
   1214       1.1       cgd 	case 8:
   1215       1.1       cgd 		return (cp[h] == 0xff);
   1216       1.1       cgd 	case 4:
   1217       1.1       cgd 		mask = 0x0f << ((h & 0x1) << 2);
   1218       1.1       cgd 		return ((cp[h >> 1] & mask) == mask);
   1219       1.1       cgd 	case 2:
   1220       1.1       cgd 		mask = 0x03 << ((h & 0x3) << 1);
   1221       1.1       cgd 		return ((cp[h >> 2] & mask) == mask);
   1222       1.1       cgd 	case 1:
   1223       1.1       cgd 		mask = 0x01 << (h & 0x7);
   1224       1.1       cgd 		return ((cp[h >> 3] & mask) == mask);
   1225       1.1       cgd 	default:
   1226       1.1       cgd #ifdef STANDALONE
   1227       1.1       cgd 		printf("isblock bad fs_frag %d\n", fs->fs_frag);
   1228       1.1       cgd #else
   1229       1.1       cgd 		fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag);
   1230       1.1       cgd #endif
   1231       1.1       cgd 		return (0);
   1232       1.1       cgd 	}
   1233       1.1       cgd }
   1234       1.1       cgd 
   1235       1.1       cgd /*
   1236       1.1       cgd  * take a block out of the map
   1237       1.1       cgd  */
   1238      1.26  christos void
   1239  1.38.4.1        he clrblock(struct fs *fs, unsigned char *cp, int h)
   1240       1.1       cgd {
   1241       1.1       cgd 	switch ((fs)->fs_frag) {
   1242       1.1       cgd 	case 8:
   1243       1.1       cgd 		cp[h] = 0;
   1244       1.1       cgd 		return;
   1245       1.1       cgd 	case 4:
   1246       1.1       cgd 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
   1247       1.1       cgd 		return;
   1248       1.1       cgd 	case 2:
   1249       1.1       cgd 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
   1250       1.1       cgd 		return;
   1251       1.1       cgd 	case 1:
   1252       1.1       cgd 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
   1253       1.1       cgd 		return;
   1254       1.1       cgd 	default:
   1255       1.1       cgd #ifdef STANDALONE
   1256       1.1       cgd 		printf("clrblock bad fs_frag %d\n", fs->fs_frag);
   1257       1.1       cgd #else
   1258       1.1       cgd 		fprintf(stderr, "clrblock bad fs_frag %d\n", fs->fs_frag);
   1259       1.1       cgd #endif
   1260       1.1       cgd 		return;
   1261       1.1       cgd 	}
   1262       1.1       cgd }
   1263       1.1       cgd 
   1264       1.1       cgd /*
   1265       1.1       cgd  * put a block into the map
   1266       1.1       cgd  */
   1267      1.26  christos void
   1268  1.38.4.1        he setblock(struct fs *fs, unsigned char *cp, int h)
   1269       1.1       cgd {
   1270       1.1       cgd 	switch (fs->fs_frag) {
   1271       1.1       cgd 	case 8:
   1272       1.1       cgd 		cp[h] = 0xff;
   1273       1.1       cgd 		return;
   1274       1.1       cgd 	case 4:
   1275       1.1       cgd 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
   1276       1.1       cgd 		return;
   1277       1.1       cgd 	case 2:
   1278       1.1       cgd 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
   1279       1.1       cgd 		return;
   1280       1.1       cgd 	case 1:
   1281       1.1       cgd 		cp[h >> 3] |= (0x01 << (h & 0x7));
   1282       1.1       cgd 		return;
   1283       1.1       cgd 	default:
   1284       1.1       cgd #ifdef STANDALONE
   1285       1.1       cgd 		printf("setblock bad fs_frag %d\n", fs->fs_frag);
   1286       1.1       cgd #else
   1287       1.1       cgd 		fprintf(stderr, "setblock bad fs_frag %d\n", fs->fs_frag);
   1288       1.1       cgd #endif
   1289       1.1       cgd 		return;
   1290      1.30    bouyer 	}
   1291      1.30    bouyer }
   1292      1.30    bouyer 
   1293      1.30    bouyer /* swap byte order of cylinder group */
   1294      1.30    bouyer static void
   1295  1.38.4.1        he swap_cg(struct cg *o, struct cg *n)
   1296      1.30    bouyer {
   1297      1.30    bouyer 	int i, btotsize, fbsize;
   1298      1.30    bouyer 	u_int32_t *n32, *o32;
   1299      1.30    bouyer 	u_int16_t *n16, *o16;
   1300      1.30    bouyer 
   1301      1.30    bouyer 	n->cg_firstfield = bswap32(o->cg_firstfield);
   1302      1.30    bouyer 	n->cg_magic = bswap32(o->cg_magic);
   1303      1.30    bouyer 	n->cg_time = bswap32(o->cg_time);
   1304      1.30    bouyer 	n->cg_cgx = bswap32(o->cg_cgx);
   1305      1.30    bouyer 	n->cg_ncyl = bswap16(o->cg_ncyl);
   1306      1.30    bouyer 	n->cg_niblk = bswap16(o->cg_niblk);
   1307      1.30    bouyer 	n->cg_ndblk = bswap32(o->cg_ndblk);
   1308      1.30    bouyer 	n->cg_cs.cs_ndir = bswap32(o->cg_cs.cs_ndir);
   1309      1.30    bouyer 	n->cg_cs.cs_nbfree = bswap32(o->cg_cs.cs_nbfree);
   1310      1.30    bouyer 	n->cg_cs.cs_nifree = bswap32(o->cg_cs.cs_nifree);
   1311      1.30    bouyer 	n->cg_cs.cs_nffree = bswap32(o->cg_cs.cs_nffree);
   1312      1.30    bouyer 	n->cg_rotor = bswap32(o->cg_rotor);
   1313      1.30    bouyer 	n->cg_frotor = bswap32(o->cg_frotor);
   1314      1.30    bouyer 	n->cg_irotor = bswap32(o->cg_irotor);
   1315      1.30    bouyer 	n->cg_btotoff = bswap32(o->cg_btotoff);
   1316      1.30    bouyer 	n->cg_boff = bswap32(o->cg_boff);
   1317      1.30    bouyer 	n->cg_iusedoff = bswap32(o->cg_iusedoff);
   1318      1.30    bouyer 	n->cg_freeoff = bswap32(o->cg_freeoff);
   1319      1.30    bouyer 	n->cg_nextfreeoff = bswap32(o->cg_nextfreeoff);
   1320      1.30    bouyer 	n->cg_clustersumoff = bswap32(o->cg_clustersumoff);
   1321      1.30    bouyer 	n->cg_clusteroff = bswap32(o->cg_clusteroff);
   1322      1.30    bouyer 	n->cg_nclusterblks = bswap32(o->cg_nclusterblks);
   1323      1.30    bouyer 	for (i=0; i < MAXFRAG; i++)
   1324      1.30    bouyer 		n->cg_frsum[i] = bswap32(o->cg_frsum[i]);
   1325      1.30    bouyer 
   1326      1.30    bouyer 	/* alays new format */
   1327      1.30    bouyer 	if (n->cg_magic == CG_MAGIC) {
   1328      1.30    bouyer 		btotsize = n->cg_boff - n->cg_btotoff;
   1329      1.30    bouyer 		fbsize = n->cg_iusedoff - n->cg_boff;
   1330      1.30    bouyer 		n32 = (u_int32_t*)((u_int8_t*)n + n->cg_btotoff);
   1331      1.30    bouyer 		o32 = (u_int32_t*)((u_int8_t*)o + n->cg_btotoff);
   1332      1.30    bouyer 		n16 = (u_int16_t*)((u_int8_t*)n + n->cg_boff);
   1333      1.30    bouyer 		o16 = (u_int16_t*)((u_int8_t*)o + n->cg_boff);
   1334      1.30    bouyer 	} else {
   1335      1.30    bouyer 		btotsize = bswap32(n->cg_boff) - bswap32(n->cg_btotoff);
   1336      1.30    bouyer 		fbsize = bswap32(n->cg_iusedoff) - bswap32(n->cg_boff);
   1337      1.30    bouyer 		n32 = (u_int32_t*)((u_int8_t*)n + bswap32(n->cg_btotoff));
   1338      1.30    bouyer 		o32 = (u_int32_t*)((u_int8_t*)o + bswap32(n->cg_btotoff));
   1339      1.30    bouyer 		n16 = (u_int16_t*)((u_int8_t*)n + bswap32(n->cg_boff));
   1340      1.30    bouyer 		o16 = (u_int16_t*)((u_int8_t*)o + bswap32(n->cg_boff));
   1341      1.30    bouyer 	}
   1342      1.30    bouyer 	for (i=0; i < btotsize / sizeof(u_int32_t); i++)
   1343      1.30    bouyer 		n32[i] = bswap32(o32[i]);
   1344      1.30    bouyer 
   1345      1.30    bouyer 	for (i=0; i < fbsize/sizeof(u_int16_t); i++)
   1346      1.30    bouyer 		n16[i] = bswap16(o16[i]);
   1347      1.30    bouyer 
   1348      1.30    bouyer 	if (n->cg_magic == CG_MAGIC) {
   1349      1.30    bouyer 		n32 = (u_int32_t*)((u_int8_t*)n + n->cg_clustersumoff);
   1350      1.30    bouyer 		o32 = (u_int32_t*)((u_int8_t*)o + n->cg_clustersumoff);
   1351      1.30    bouyer 	} else {
   1352      1.30    bouyer 		n32 = (u_int32_t*)((u_int8_t*)n + bswap32(n->cg_clustersumoff));
   1353      1.30    bouyer 		o32 = (u_int32_t*)((u_int8_t*)o + bswap32(n->cg_clustersumoff));
   1354      1.30    bouyer 	}
   1355  1.38.4.1        he 	for (i = 1; i < sblock.fs_contigsumsize + 1; i++)
   1356      1.30    bouyer 		n32[i] = bswap32(o32[i]);
   1357      1.30    bouyer }
   1358      1.30    bouyer 
   1359      1.30    bouyer /* copy a direntry to a buffer, in fs byte order */
   1360      1.30    bouyer static void
   1361  1.38.4.1        he copy_dir(struct direct *dir, struct direct *dbuf)
   1362      1.30    bouyer {
   1363      1.30    bouyer 	memcpy(dbuf, dir, DIRSIZ(Oflag, dir, 0));
   1364      1.30    bouyer 	if (needswap) {
   1365      1.30    bouyer 		dbuf->d_ino = bswap32(dir->d_ino);
   1366      1.30    bouyer 		dbuf->d_reclen = bswap16(dir->d_reclen);
   1367      1.30    bouyer 		if (Oflag)
   1368      1.30    bouyer 			((struct odirect*)dbuf)->d_namlen =
   1369      1.30    bouyer 				bswap16(((struct odirect*)dir)->d_namlen);
   1370       1.1       cgd 	}
   1371      1.36  wrstuden }
   1372      1.36  wrstuden 
   1373      1.36  wrstuden /* Determine how many digits are needed to print a given integer */
   1374      1.36  wrstuden static int
   1375  1.38.4.1        he count_digits(int num)
   1376      1.36  wrstuden {
   1377      1.36  wrstuden 	int ndig;
   1378      1.36  wrstuden 
   1379      1.36  wrstuden 	for(ndig = 1; num > 9; num /=10, ndig++);
   1380      1.36  wrstuden 
   1381      1.36  wrstuden 	return (ndig);
   1382       1.1       cgd }
   1383