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