Home | History | Annotate | Line # | Download | only in newfs
mkfs.c revision 1.88.2.1
      1  1.88.2.1      tron /*	$NetBSD: mkfs.c,v 1.88.2.1 2005/11/06 13:27:32 tron Exp $	*/
      2      1.71       agc 
      3      1.71       agc /*
      4      1.71       agc  * Copyright (c) 1980, 1989, 1993
      5      1.71       agc  *	The Regents of the University of California.  All rights reserved.
      6      1.71       agc  *
      7      1.71       agc  * Redistribution and use in source and binary forms, with or without
      8      1.71       agc  * modification, are permitted provided that the following conditions
      9      1.71       agc  * are met:
     10      1.71       agc  * 1. Redistributions of source code must retain the above copyright
     11      1.71       agc  *    notice, this list of conditions and the following disclaimer.
     12      1.71       agc  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.71       agc  *    notice, this list of conditions and the following disclaimer in the
     14      1.71       agc  *    documentation and/or other materials provided with the distribution.
     15      1.71       agc  * 3. Neither the name of the University nor the names of its contributors
     16      1.71       agc  *    may be used to endorse or promote products derived from this software
     17      1.71       agc  *    without specific prior written permission.
     18      1.71       agc  *
     19      1.71       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20      1.71       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21      1.71       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22      1.71       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23      1.71       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24      1.71       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25      1.71       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26      1.71       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27      1.71       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28      1.71       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29      1.71       agc  * SUCH DAMAGE.
     30      1.71       agc  */
     31      1.19       cgd 
     32       1.1       cgd /*
     33      1.68      fvdl  * Copyright (c) 2002 Networks Associates Technology, Inc.
     34      1.68      fvdl  * All rights reserved.
     35      1.68      fvdl  *
     36      1.68      fvdl  * This software was developed for the FreeBSD Project by Marshall
     37      1.68      fvdl  * Kirk McKusick and Network Associates Laboratories, the Security
     38      1.68      fvdl  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
     39      1.68      fvdl  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
     40      1.68      fvdl  * research program
     41      1.68      fvdl  *
     42       1.1       cgd  * Redistribution and use in source and binary forms, with or without
     43       1.1       cgd  * modification, are permitted provided that the following conditions
     44       1.1       cgd  * are met:
     45       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     46       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     47       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     48       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     49       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     50       1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     51       1.1       cgd  *    must display the following acknowledgement:
     52       1.1       cgd  *	This product includes software developed by the University of
     53       1.1       cgd  *	California, Berkeley and its contributors.
     54       1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     55       1.1       cgd  *    may be used to endorse or promote products derived from this software
     56       1.1       cgd  *    without specific prior written permission.
     57       1.1       cgd  *
     58       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68       1.1       cgd  * SUCH DAMAGE.
     69       1.1       cgd  */
     70       1.1       cgd 
     71      1.26  christos #include <sys/cdefs.h>
     72       1.1       cgd #ifndef lint
     73      1.19       cgd #if 0
     74      1.27     lukem static char sccsid[] = "@(#)mkfs.c	8.11 (Berkeley) 5/3/95";
     75      1.19       cgd #else
     76  1.88.2.1      tron __RCSID("$NetBSD: mkfs.c,v 1.88.2.1 2005/11/06 13:27:32 tron Exp $");
     77      1.19       cgd #endif
     78       1.1       cgd #endif /* not lint */
     79       1.1       cgd 
     80       1.1       cgd #include <sys/param.h>
     81      1.60    simonb #include <sys/mman.h>
     82       1.1       cgd #include <sys/time.h>
     83       1.1       cgd #include <sys/resource.h>
     84       1.9   mycroft #include <ufs/ufs/dinode.h>
     85       1.9   mycroft #include <ufs/ufs/dir.h>
     86      1.30    bouyer #include <ufs/ufs/ufs_bswap.h>
     87       1.9   mycroft #include <ufs/ffs/fs.h>
     88      1.30    bouyer #include <ufs/ffs/ffs_extern.h>
     89       1.1       cgd #include <sys/disklabel.h>
     90       1.9   mycroft 
     91      1.60    simonb #include <err.h>
     92      1.57     lukem #include <errno.h>
     93      1.14       cgd #include <string.h>
     94      1.14       cgd #include <unistd.h>
     95      1.26  christos #include <stdlib.h>
     96      1.80       dsl #include <stddef.h>
     97      1.14       cgd 
     98       1.9   mycroft #ifndef STANDALONE
     99       1.9   mycroft #include <stdio.h>
    100       1.9   mycroft #endif
    101      1.40    simonb 
    102      1.40    simonb #include "extern.h"
    103       1.1       cgd 
    104      1.68      fvdl union dinode {
    105      1.68      fvdl 	struct ufs1_dinode dp1;
    106      1.68      fvdl 	struct ufs2_dinode dp2;
    107      1.68      fvdl };
    108      1.68      fvdl 
    109      1.70    atatat static void initcg(int, const struct timeval *);
    110      1.70    atatat static int fsinit(const struct timeval *, mode_t, uid_t, gid_t);
    111      1.39    simonb static int makedir(struct direct *, int);
    112      1.39    simonb static daddr_t alloc(int, int);
    113      1.68      fvdl static void iput(union dinode *, ino_t);
    114      1.39    simonb static void rdfs(daddr_t, int, void *);
    115      1.39    simonb static void wtfs(daddr_t, int, void *);
    116      1.39    simonb static int isblock(struct fs *, unsigned char *, int);
    117      1.39    simonb static void clrblock(struct fs *, unsigned char *, int);
    118      1.39    simonb static void setblock(struct fs *, unsigned char *, int);
    119      1.68      fvdl static int ilog2(int);
    120      1.80       dsl static void zap_old_sblock(int);
    121      1.61     lukem #ifdef MFS
    122      1.60    simonb static void calc_memfree(void);
    123      1.60    simonb static void *mkfs_malloc(size_t size);
    124      1.61     lukem #endif
    125      1.27     lukem 
    126      1.72       dsl static int count_digits(uint64_t);
    127      1.36  wrstuden 
    128       1.1       cgd /*
    129       1.1       cgd  * make file system for cylinder-group style file systems
    130       1.1       cgd  */
    131      1.60    simonb #define	UMASK		0755
    132      1.60    simonb #define	POWEROF2(num)	(((num) & ((num) - 1)) == 0)
    133       1.1       cgd 
    134       1.1       cgd union {
    135       1.1       cgd 	struct fs fs;
    136      1.68      fvdl 	char pad[SBLOCKSIZE];
    137       1.1       cgd } fsun;
    138       1.1       cgd #define	sblock	fsun.fs
    139      1.73       dsl 
    140      1.73       dsl struct	csum *fscs_0;		/* first block of cylinder summaries */
    141      1.73       dsl struct	csum *fscs_next;	/* place for next summary */
    142      1.73       dsl struct	csum *fscs_end;		/* end of summary buffer */
    143      1.73       dsl struct	csum *fscs_reset;	/* place for next summary after write */
    144      1.73       dsl uint	fs_csaddr;		/* fragment number to write to */
    145       1.1       cgd 
    146       1.1       cgd union {
    147       1.1       cgd 	struct cg cg;
    148       1.1       cgd 	char pad[MAXBSIZE];
    149       1.1       cgd } cgun;
    150       1.1       cgd #define	acg	cgun.cg
    151       1.1       cgd 
    152      1.68      fvdl #define DIP(dp, field) \
    153      1.68      fvdl 	((sblock.fs_magic == FS_UFS1_MAGIC) ? \
    154      1.68      fvdl 	(dp)->dp1.di_##field : (dp)->dp2.di_##field)
    155      1.68      fvdl 
    156      1.68      fvdl char *iobuf;
    157      1.87       dsl int iobufsize;			/* size to end of 2nd inode block */
    158      1.87       dsl int iobuf_memsize;		/* Actual buffer size */
    159       1.1       cgd 
    160       1.1       cgd int	fsi, fso;
    161       1.1       cgd 
    162      1.26  christos void
    163      1.60    simonb mkfs(struct partition *pp, const char *fsys, int fi, int fo,
    164      1.60    simonb     mode_t mfsmode, uid_t mfsuid, gid_t mfsgid)
    165       1.1       cgd {
    166      1.74       dsl 	uint fragsperinodeblk, ncg;
    167      1.74       dsl 	uint cgzero;
    168      1.74       dsl 	uint64_t inodeblks, cgall;
    169      1.68      fvdl 	int32_t cylno, i, csfrags;
    170      1.70    atatat 	struct timeval tv;
    171      1.52     lukem 	long long sizepb;
    172      1.36  wrstuden 	int nprintcols, printcolwidth;
    173       1.1       cgd 
    174       1.1       cgd #ifndef STANDALONE
    175      1.70    atatat 	gettimeofday(&tv, NULL);
    176       1.1       cgd #endif
    177      1.61     lukem #ifdef MFS
    178      1.86       dsl 	if (mfs && !Nflag) {
    179      1.60    simonb 		calc_memfree();
    180       1.1       cgd 		if (fssize * sectorsize > memleft)
    181      1.60    simonb 			fssize = memleft / sectorsize;
    182      1.60    simonb 		if ((membase = mkfs_malloc(fssize * sectorsize)) == 0)
    183       1.1       cgd 			exit(12);
    184       1.1       cgd 	}
    185      1.61     lukem #endif
    186       1.1       cgd 	fsi = fi;
    187       1.1       cgd 	fso = fo;
    188      1.68      fvdl 	if (Oflag == 0) {
    189      1.68      fvdl 		sblock.fs_old_inodefmt = FS_42INODEFMT;
    190       1.9   mycroft 		sblock.fs_maxsymlinklen = 0;
    191      1.68      fvdl 		sblock.fs_old_flags = 0;
    192       1.9   mycroft 	} else {
    193      1.68      fvdl 		sblock.fs_old_inodefmt = FS_44INODEFMT;
    194      1.68      fvdl 		sblock.fs_maxsymlinklen = (Oflag == 1 ? MAXSYMLINKLEN_UFS1 :
    195      1.68      fvdl 		    MAXSYMLINKLEN_UFS2);
    196      1.68      fvdl 		sblock.fs_old_flags = FS_FLAGS_UPDATED;
    197      1.88       dbj 		if (isappleufs)
    198      1.88       dbj 			sblock.fs_old_flags = 0;
    199      1.68      fvdl 		sblock.fs_flags = 0;
    200       1.9   mycroft 	}
    201      1.65       dbj 
    202       1.1       cgd 	/*
    203      1.55     lukem 	 * collect and verify the filesystem density info
    204      1.55     lukem 	 */
    205      1.55     lukem 	sblock.fs_avgfilesize = avgfilesize;
    206      1.55     lukem 	sblock.fs_avgfpdir = avgfpdir;
    207      1.72       dsl 	if (sblock.fs_avgfilesize <= 0) {
    208      1.55     lukem 		printf("illegal expected average file size %d\n",
    209      1.72       dsl 		    sblock.fs_avgfilesize);
    210      1.72       dsl 		exit(14);
    211      1.72       dsl 	}
    212      1.72       dsl 	if (sblock.fs_avgfpdir <= 0) {
    213      1.55     lukem 		printf("illegal expected number of files per directory %d\n",
    214      1.72       dsl 		    sblock.fs_avgfpdir);
    215      1.72       dsl 		exit(15);
    216      1.72       dsl 	}
    217       1.1       cgd 	/*
    218       1.1       cgd 	 * collect and verify the block and fragment sizes
    219       1.1       cgd 	 */
    220       1.1       cgd 	sblock.fs_bsize = bsize;
    221       1.1       cgd 	sblock.fs_fsize = fsize;
    222       1.1       cgd 	if (!POWEROF2(sblock.fs_bsize)) {
    223       1.1       cgd 		printf("block size must be a power of 2, not %d\n",
    224       1.1       cgd 		    sblock.fs_bsize);
    225       1.1       cgd 		exit(16);
    226       1.1       cgd 	}
    227       1.1       cgd 	if (!POWEROF2(sblock.fs_fsize)) {
    228       1.1       cgd 		printf("fragment size must be a power of 2, not %d\n",
    229       1.1       cgd 		    sblock.fs_fsize);
    230       1.1       cgd 		exit(17);
    231       1.1       cgd 	}
    232       1.1       cgd 	if (sblock.fs_fsize < sectorsize) {
    233       1.1       cgd 		printf("fragment size %d is too small, minimum is %d\n",
    234       1.1       cgd 		    sblock.fs_fsize, sectorsize);
    235       1.1       cgd 		exit(18);
    236       1.1       cgd 	}
    237       1.1       cgd 	if (sblock.fs_bsize < MINBSIZE) {
    238       1.1       cgd 		printf("block size %d is too small, minimum is %d\n",
    239       1.1       cgd 		    sblock.fs_bsize, MINBSIZE);
    240      1.58     lukem 		exit(19);
    241      1.58     lukem 	}
    242      1.58     lukem 	if (sblock.fs_bsize > MAXBSIZE) {
    243      1.58     lukem 		printf("block size %d is too large, maximum is %d\n",
    244      1.58     lukem 		    sblock.fs_bsize, MAXBSIZE);
    245       1.1       cgd 		exit(19);
    246       1.1       cgd 	}
    247       1.1       cgd 	if (sblock.fs_bsize < sblock.fs_fsize) {
    248       1.1       cgd 		printf("block size (%d) cannot be smaller than fragment size (%d)\n",
    249       1.1       cgd 		    sblock.fs_bsize, sblock.fs_fsize);
    250       1.1       cgd 		exit(20);
    251       1.1       cgd 	}
    252      1.68      fvdl 
    253      1.68      fvdl 	if (maxbsize < bsize || !POWEROF2(maxbsize)) {
    254      1.68      fvdl 		sblock.fs_maxbsize = sblock.fs_bsize;
    255      1.68      fvdl 	} else if (sblock.fs_maxbsize > FS_MAXCONTIG * sblock.fs_bsize) {
    256      1.68      fvdl 		sblock.fs_maxbsize = FS_MAXCONTIG * sblock.fs_bsize;
    257      1.68      fvdl 	} else {
    258      1.68      fvdl 		sblock.fs_maxbsize = maxbsize;
    259      1.68      fvdl 	}
    260      1.68      fvdl 	sblock.fs_maxcontig = maxcontig;
    261      1.68      fvdl 	if (sblock.fs_maxcontig < sblock.fs_maxbsize / sblock.fs_bsize) {
    262      1.68      fvdl 		sblock.fs_maxcontig = sblock.fs_maxbsize / sblock.fs_bsize;
    263      1.68      fvdl 		printf("Maxcontig raised to %d\n", sblock.fs_maxbsize);
    264      1.68      fvdl 	}
    265      1.68      fvdl 	if (sblock.fs_maxcontig > 1)
    266      1.68      fvdl 		sblock.fs_contigsumsize = MIN(sblock.fs_maxcontig,FS_MAXCONTIG);
    267      1.68      fvdl 
    268       1.1       cgd 	sblock.fs_bmask = ~(sblock.fs_bsize - 1);
    269       1.1       cgd 	sblock.fs_fmask = ~(sblock.fs_fsize - 1);
    270       1.9   mycroft 	sblock.fs_qbmask = ~sblock.fs_bmask;
    271       1.9   mycroft 	sblock.fs_qfmask = ~sblock.fs_fmask;
    272       1.1       cgd 	for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1)
    273       1.1       cgd 		sblock.fs_bshift++;
    274       1.1       cgd 	for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1)
    275       1.1       cgd 		sblock.fs_fshift++;
    276       1.1       cgd 	sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize);
    277       1.1       cgd 	for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1)
    278       1.1       cgd 		sblock.fs_fragshift++;
    279       1.1       cgd 	if (sblock.fs_frag > MAXFRAG) {
    280      1.30    bouyer 		printf("fragment size %d is too small, "
    281      1.30    bouyer 			"minimum with block size %d is %d\n",
    282       1.1       cgd 		    sblock.fs_fsize, sblock.fs_bsize,
    283       1.1       cgd 		    sblock.fs_bsize / MAXFRAG);
    284       1.1       cgd 		exit(21);
    285       1.1       cgd 	}
    286      1.68      fvdl 	sblock.fs_fsbtodb = ilog2(sblock.fs_fsize / sectorsize);
    287      1.74       dsl 	sblock.fs_size = dbtofsb(&sblock, fssize);
    288      1.68      fvdl 	if (Oflag <= 1) {
    289      1.72       dsl 		if (sblock.fs_size >= 1ull << 31) {
    290      1.72       dsl 			printf("Too many fragments (0x%" PRIx64
    291      1.72       dsl 			    ") for a UFS1 filesystem\n", sblock.fs_size);
    292      1.72       dsl 			exit(22);
    293      1.72       dsl 		}
    294      1.68      fvdl 		sblock.fs_magic = FS_UFS1_MAGIC;
    295      1.68      fvdl 		sblock.fs_sblockloc = SBLOCK_UFS1;
    296      1.68      fvdl 		sblock.fs_nindir = sblock.fs_bsize / sizeof(int32_t);
    297      1.68      fvdl 		sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs1_dinode);
    298      1.68      fvdl 		sblock.fs_old_cgoffset = 0;
    299      1.68      fvdl 		sblock.fs_old_cgmask = 0xffffffff;
    300      1.68      fvdl 		sblock.fs_old_size = sblock.fs_size;
    301      1.68      fvdl 		sblock.fs_old_rotdelay = 0;
    302      1.68      fvdl 		sblock.fs_old_rps = 60;
    303      1.68      fvdl 		sblock.fs_old_nspf = sblock.fs_fsize / sectorsize;
    304      1.68      fvdl 		sblock.fs_old_cpg = 1;
    305      1.68      fvdl 		sblock.fs_old_interleave = 1;
    306      1.68      fvdl 		sblock.fs_old_trackskew = 0;
    307      1.68      fvdl 		sblock.fs_old_cpc = 0;
    308      1.72       dsl 		sblock.fs_old_postblformat = FS_DYNAMICPOSTBLFMT;
    309      1.68      fvdl 		sblock.fs_old_nrpos = 1;
    310      1.68      fvdl 	} else {
    311      1.68      fvdl 		sblock.fs_magic = FS_UFS2_MAGIC;
    312      1.68      fvdl 		sblock.fs_sblockloc = SBLOCK_UFS2;
    313      1.68      fvdl 		sblock.fs_nindir = sblock.fs_bsize / sizeof(int64_t);
    314      1.68      fvdl 		sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs2_dinode);
    315      1.68      fvdl 	}
    316      1.68      fvdl 
    317       1.1       cgd 	sblock.fs_sblkno =
    318      1.68      fvdl 	    roundup(howmany(sblock.fs_sblockloc + SBLOCKSIZE, sblock.fs_fsize),
    319      1.68      fvdl 		sblock.fs_frag);
    320       1.1       cgd 	sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno +
    321      1.68      fvdl 	    roundup(howmany(SBLOCKSIZE, sblock.fs_fsize), sblock.fs_frag));
    322       1.1       cgd 	sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag;
    323       1.9   mycroft 	sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
    324       1.9   mycroft 	for (sizepb = sblock.fs_bsize, i = 0; i < NIADDR; i++) {
    325       1.9   mycroft 		sizepb *= NINDIR(&sblock);
    326       1.9   mycroft 		sblock.fs_maxfilesize += sizepb;
    327       1.9   mycroft 	}
    328      1.68      fvdl 
    329       1.1       cgd 	/*
    330      1.68      fvdl 	 * Calculate the number of blocks to put into each cylinder group.
    331      1.68      fvdl 	 *
    332      1.74       dsl 	 * The cylinder group size is limited because the data structure
    333      1.74       dsl 	 * must fit into a single block.
    334      1.74       dsl 	 * We try to have as few cylinder groups as possible, with a proviso
    335      1.74       dsl 	 * that we create at least MINCYLGRPS (==4) except for small
    336      1.74       dsl 	 * filesystems.
    337      1.68      fvdl 	 *
    338      1.74       dsl 	 * This algorithm works out how many blocks of inodes would be
    339      1.74       dsl 	 * needed to fill the entire volume at the specified density.
    340      1.74       dsl 	 * It then looks at how big the 'cylinder block' would have to
    341      1.74       dsl 	 * be and, assuming that it is linearly related to the number
    342      1.74       dsl 	 * of inodes and blocks how many cylinder groups are needed to
    343      1.74       dsl 	 * keep the cylinder block below the filesystem block size.
    344      1.74       dsl 	 *
    345      1.74       dsl 	 * The cylinder groups are then all created with the average size.
    346      1.74       dsl 	 *
    347      1.74       dsl 	 * Space taken by the red tape on cylinder groups other than the
    348      1.74       dsl 	 * first is ignored.
    349      1.68      fvdl 	 */
    350      1.74       dsl 
    351      1.74       dsl 	/* There must be space for 1 inode block and 2 data blocks */
    352      1.74       dsl 	if (sblock.fs_size < sblock.fs_iblkno + 3 * sblock.fs_frag) {
    353      1.74       dsl 		printf("Filesystem size %lld < minimum size of %d\n",
    354      1.74       dsl 		    (long long)sblock.fs_size, sblock.fs_iblkno + 3 * sblock.fs_frag);
    355      1.74       dsl 		exit(23);
    356       1.1       cgd 	}
    357      1.81       dsl 	if (num_inodes != 0)
    358      1.81       dsl 		inodeblks = howmany(num_inodes, INOPB(&sblock));
    359      1.81       dsl 	else {
    360      1.81       dsl 		/*
    361      1.81       dsl 		 * Calculate 'per inode block' so we can allocate less than
    362      1.81       dsl 		 * 1 fragment per inode - useful for /dev.
    363      1.81       dsl 		 */
    364      1.81       dsl 		fragsperinodeblk = MAX(numfrags(&sblock,
    365      1.81       dsl 					density * INOPB(&sblock)), 1);
    366      1.81       dsl 		inodeblks = (sblock.fs_size - sblock.fs_iblkno) /
    367      1.81       dsl 			(sblock.fs_frag + fragsperinodeblk);
    368      1.81       dsl 	}
    369      1.74       dsl 	if (inodeblks == 0)
    370      1.74       dsl 		inodeblks = 1;
    371      1.81       dsl 	/* Ensure that there are at least 2 data blocks (or we fail below) */
    372      1.81       dsl 	if (inodeblks > (sblock.fs_size - sblock.fs_iblkno)/sblock.fs_frag - 2)
    373      1.81       dsl 		inodeblks = (sblock.fs_size-sblock.fs_iblkno)/sblock.fs_frag-2;
    374      1.74       dsl 	/* Even UFS2 limits number of inodes to 2^31 (fs_ipg is int32_t) */
    375      1.74       dsl 	if (inodeblks * INOPB(&sblock) >= 1ull << 31)
    376      1.74       dsl 		inodeblks = ((1ull << 31) - NBBY) / INOPB(&sblock);
    377      1.74       dsl 	/*
    378      1.74       dsl 	 * See what would happen if we tried to use 1 cylinder group.
    379      1.74       dsl 	 * Assume space linear, so work out number of cylinder groups needed.
    380      1.74       dsl 	 * Subtract one from the allowed size to compensate for rounding
    381      1.74       dsl 	 * a number of bits up to a complete byte.
    382      1.68      fvdl 	 */
    383      1.74       dsl 	cgzero = CGSIZE_IF(&sblock, 0, 0);
    384      1.74       dsl 	cgall = CGSIZE_IF(&sblock, inodeblks * INOPB(&sblock), sblock.fs_size);
    385      1.74       dsl 	ncg = howmany(cgall - cgzero, sblock.fs_bsize - cgzero - 1);
    386      1.74       dsl 	if (ncg < MINCYLGRPS) {
    387      1.74       dsl 		/*
    388      1.74       dsl 		 * We would like to allocate MINCLYGRPS cylinder groups,
    389      1.74       dsl 		 * but for small file sytems (especially ones with a lot
    390      1.74       dsl 		 * of inodes) this is not desirable (or possible).
    391      1.74       dsl 		 */
    392      1.74       dsl 		i = sblock.fs_size / 2 / (sblock.fs_iblkno +
    393      1.74       dsl 						inodeblks * sblock.fs_frag);
    394      1.74       dsl 		if (i > ncg)
    395      1.74       dsl 			ncg = i;
    396      1.74       dsl 		if (ncg > MINCYLGRPS)
    397      1.74       dsl 			ncg = MINCYLGRPS;
    398      1.74       dsl 		if (ncg > inodeblks)
    399      1.74       dsl 			ncg = inodeblks;
    400      1.68      fvdl 	}
    401      1.68      fvdl 	/*
    402      1.74       dsl 	 * Put an equal number of blocks in each cylinder group.
    403      1.74       dsl 	 * Round up so we don't have more fragments in the last CG than
    404      1.74       dsl 	 * the earlier ones (does that matter?), but kill a block if the
    405      1.74       dsl 	 * CGSIZE becomes too big (only happens if there are a lot of CGs).
    406      1.68      fvdl 	 */
    407      1.74       dsl 	sblock.fs_fpg = roundup(howmany(sblock.fs_size, ncg), sblock.fs_frag);
    408      1.74       dsl 	i = CGSIZE_IF(&sblock, inodeblks * INOPB(&sblock) / ncg, sblock.fs_fpg);
    409      1.74       dsl 	if (i > sblock.fs_bsize)
    410      1.74       dsl 		sblock.fs_fpg -= (i - sblock.fs_bsize) * NBBY;
    411      1.74       dsl 	/* ... and recalculate how many cylinder groups we now need */
    412      1.74       dsl 	ncg = howmany(sblock.fs_size, sblock.fs_fpg);
    413      1.74       dsl 	inodeblks /= ncg;
    414      1.74       dsl 	if (inodeblks == 0)
    415      1.74       dsl 		inodeblks = 1;
    416      1.74       dsl 	sblock.fs_ipg = inodeblks * INOPB(&sblock);
    417      1.74       dsl 	/* Sanity check on our sums... */
    418      1.74       dsl 	if (CGSIZE(&sblock) > sblock.fs_bsize) {
    419      1.74       dsl 		printf("CGSIZE miscalculated %d > %d\n",
    420      1.74       dsl 		    (int)CGSIZE(&sblock), sblock.fs_bsize);
    421      1.74       dsl 		exit(24);
    422      1.74       dsl 	}
    423      1.74       dsl 	/* Check that the last cylinder group has enough space for the inodes */
    424      1.74       dsl 	i = sblock.fs_size - sblock.fs_fpg * (ncg - 1ull);
    425      1.74       dsl 	if (i < sblock.fs_iblkno + inodeblks * sblock.fs_frag) {
    426      1.74       dsl 		/*
    427      1.74       dsl 		 * Since we make all the cylinder groups the same size, the
    428      1.74       dsl 		 * last will only be small if there are a large number of
    429      1.74       dsl 		 * cylinder groups. If we pull even a fragment from each
    430      1.74       dsl 		 * of the other groups then the last CG will be overfull.
    431      1.74       dsl 		 * So we just kill the last CG.
    432      1.74       dsl 		 */
    433      1.74       dsl 		ncg--;
    434      1.74       dsl 		sblock.fs_size -= i;
    435      1.74       dsl 	}
    436      1.74       dsl 	sblock.fs_ncg = ncg;
    437      1.74       dsl 
    438       1.1       cgd 	sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock));
    439       1.1       cgd 	sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock);
    440      1.68      fvdl 	if (Oflag <= 1) {
    441      1.68      fvdl 		sblock.fs_old_spc = sblock.fs_fpg * sblock.fs_old_nspf;
    442      1.68      fvdl 		sblock.fs_old_nsect = sblock.fs_old_spc;
    443      1.68      fvdl 		sblock.fs_old_npsect = sblock.fs_old_spc;
    444      1.68      fvdl 		sblock.fs_old_ncyl = sblock.fs_ncg;
    445       1.1       cgd 	}
    446      1.68      fvdl 
    447       1.1       cgd 	/*
    448      1.73       dsl 	 * Cylinder group summary information for each cylinder is written
    449      1.73       dsl 	 * into the first cylinder group.
    450      1.73       dsl 	 * Write this fragment by fragment, but doing the first CG last
    451      1.73       dsl 	 * (after we've taken stuff off for the structure itself and the
    452      1.73       dsl 	 * root directory.
    453       1.1       cgd 	 */
    454       1.1       cgd 	sblock.fs_csaddr = cgdmin(&sblock, 0);
    455       1.1       cgd 	sblock.fs_cssize =
    456       1.1       cgd 	    fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
    457      1.73       dsl 	if (512 % sizeof *fscs_0)
    458      1.73       dsl 		errx(1, "cylinder group summary doesn't fit in sectors");
    459      1.87       dsl 	fscs_0 = mmap(0, 2 * sblock.fs_fsize, PROT_READ|PROT_WRITE,
    460      1.87       dsl 			MAP_ANON|MAP_PRIVATE, -1, 0);
    461      1.73       dsl 	if (fscs_0 == NULL)
    462      1.44     lukem 		exit(39);
    463      1.87       dsl 	memset(fscs_0, 0, 2 * sblock.fs_fsize);
    464      1.73       dsl 	fs_csaddr = sblock.fs_csaddr;
    465      1.73       dsl 	fscs_next = fscs_0;
    466      1.73       dsl 	fscs_end = (void *)((char *)fscs_0 + 2 * sblock.fs_fsize);
    467      1.73       dsl 	fscs_reset = (void *)((char *)fscs_0 + sblock.fs_fsize);
    468      1.73       dsl 	/*
    469      1.73       dsl 	 * fill in remaining fields of the super block
    470      1.73       dsl 	 */
    471      1.68      fvdl 	sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs));
    472      1.68      fvdl 	if (sblock.fs_sbsize > SBLOCKSIZE)
    473      1.68      fvdl 		sblock.fs_sbsize = SBLOCKSIZE;
    474       1.1       cgd 	sblock.fs_minfree = minfree;
    475       1.1       cgd 	sblock.fs_maxcontig = maxcontig;
    476       1.1       cgd 	sblock.fs_maxbpg = maxbpg;
    477       1.1       cgd 	sblock.fs_optim = opt;
    478       1.1       cgd 	sblock.fs_cgrotor = 0;
    479      1.68      fvdl 	sblock.fs_pendingblocks = 0;
    480      1.68      fvdl 	sblock.fs_pendinginodes = 0;
    481       1.1       cgd 	sblock.fs_cstotal.cs_ndir = 0;
    482       1.1       cgd 	sblock.fs_cstotal.cs_nbfree = 0;
    483       1.1       cgd 	sblock.fs_cstotal.cs_nifree = 0;
    484       1.1       cgd 	sblock.fs_cstotal.cs_nffree = 0;
    485       1.1       cgd 	sblock.fs_fmod = 0;
    486      1.68      fvdl 	sblock.fs_ronly = 0;
    487      1.68      fvdl 	sblock.fs_state = 0;
    488      1.21   mycroft 	sblock.fs_clean = FS_ISCLEAN;
    489       1.1       cgd 	sblock.fs_ronly = 0;
    490      1.70    atatat 	sblock.fs_id[0] = (long)tv.tv_sec;	/* XXXfvdl huh? */
    491      1.78    itojun 	sblock.fs_id[1] = arc4random() & INT32_MAX;
    492      1.68      fvdl 	sblock.fs_fsmnt[0] = '\0';
    493      1.68      fvdl 	csfrags = howmany(sblock.fs_cssize, sblock.fs_fsize);
    494      1.68      fvdl 	sblock.fs_dsize = sblock.fs_size - sblock.fs_sblkno -
    495      1.68      fvdl 	    sblock.fs_ncg * (sblock.fs_dblkno - sblock.fs_sblkno);
    496      1.68      fvdl 	sblock.fs_cstotal.cs_nbfree =
    497      1.68      fvdl 	    fragstoblks(&sblock, sblock.fs_dsize) -
    498      1.68      fvdl 	    howmany(csfrags, sblock.fs_frag);
    499      1.68      fvdl 	sblock.fs_cstotal.cs_nffree =
    500      1.68      fvdl 	    fragnum(&sblock, sblock.fs_size) +
    501      1.68      fvdl 	    (fragnum(&sblock, csfrags) > 0 ?
    502      1.68      fvdl 	    sblock.fs_frag - fragnum(&sblock, csfrags) : 0);
    503      1.68      fvdl 	sblock.fs_cstotal.cs_nifree = sblock.fs_ncg * sblock.fs_ipg - ROOTINO;
    504      1.68      fvdl 	sblock.fs_cstotal.cs_ndir = 0;
    505      1.68      fvdl 	sblock.fs_dsize -= csfrags;
    506      1.70    atatat 	sblock.fs_time = tv.tv_sec;
    507      1.68      fvdl 	if (Oflag <= 1) {
    508      1.70    atatat 		sblock.fs_old_time = tv.tv_sec;
    509      1.68      fvdl 		sblock.fs_old_dsize = sblock.fs_dsize;
    510      1.68      fvdl 		sblock.fs_old_csaddr = sblock.fs_csaddr;
    511      1.68      fvdl 		sblock.fs_old_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir;
    512      1.68      fvdl 		sblock.fs_old_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree;
    513      1.68      fvdl 		sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree;
    514      1.68      fvdl 		sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree;
    515      1.68      fvdl 	}
    516       1.1       cgd 	/*
    517       1.1       cgd 	 * Dump out summary information about file system.
    518       1.1       cgd 	 */
    519      1.86       dsl 	if (!mfs || Nflag) {
    520      1.60    simonb #define	B2MBFACTOR (1 / (1024.0 * 1024.0))
    521      1.68      fvdl 		printf("%s: %.1fMB (%lld sectors) block size %d, "
    522      1.68      fvdl 		       "fragment size %d\n",
    523      1.68      fvdl 		    fsys, (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
    524      1.68      fvdl 		    (long long)fsbtodb(&sblock, sblock.fs_size),
    525      1.68      fvdl 		    sblock.fs_bsize, sblock.fs_fsize);
    526      1.68      fvdl 		printf("\tusing %d cylinder groups of %.2fMB, %d blks, "
    527      1.68      fvdl 		       "%d inodes.\n",
    528      1.68      fvdl 		    sblock.fs_ncg,
    529       1.9   mycroft 		    (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
    530      1.68      fvdl 		    sblock.fs_fpg / sblock.fs_frag, sblock.fs_ipg);
    531       1.9   mycroft #undef B2MBFACTOR
    532       1.1       cgd 	}
    533       1.1       cgd 	/*
    534      1.36  wrstuden 	 * Now determine how wide each column will be, and calculate how
    535      1.72       dsl 	 * many columns will fit in a 80 char line.
    536      1.36  wrstuden 	 */
    537      1.36  wrstuden 	printcolwidth = count_digits(
    538      1.36  wrstuden 			fsbtodb(&sblock, cgsblock(&sblock, sblock.fs_ncg -1)));
    539      1.72       dsl 	nprintcols = 80 / (printcolwidth + 2);
    540      1.68      fvdl 
    541      1.68      fvdl 	/*
    542      1.68      fvdl 	 * allocate space for superblock, cylinder group map, and
    543      1.68      fvdl 	 * two sets of inode blocks.
    544      1.68      fvdl 	 */
    545      1.68      fvdl 	if (sblock.fs_bsize < SBLOCKSIZE)
    546      1.68      fvdl 		iobufsize = SBLOCKSIZE + 3 * sblock.fs_bsize;
    547      1.68      fvdl 	else
    548      1.68      fvdl 		iobufsize = 4 * sblock.fs_bsize;
    549      1.87       dsl 	iobuf_memsize = iobufsize;
    550      1.87       dsl 	if (!mfs && sblock.fs_magic == FS_UFS1_MAGIC) {
    551      1.87       dsl 		/* A larger buffer so we can write multiple inode blks */
    552      1.87       dsl 		iobuf_memsize += 14 * sblock.fs_bsize;
    553      1.87       dsl 	}
    554      1.87       dsl 	for (;;) {
    555      1.87       dsl 		iobuf = mmap(0, iobuf_memsize, PROT_READ|PROT_WRITE,
    556      1.87       dsl 				MAP_ANON|MAP_PRIVATE, -1, 0);
    557      1.87       dsl 		if (iobuf != NULL)
    558      1.87       dsl 			break;
    559      1.87       dsl 		if (iobuf_memsize != iobufsize) {
    560      1.87       dsl 			/* Try again with the smaller size */
    561      1.87       dsl 			iobuf_memsize = iobufsize;
    562      1.87       dsl 			continue;
    563      1.87       dsl 		}
    564      1.68      fvdl 		printf("Cannot allocate I/O buffer\n");
    565      1.68      fvdl 		exit(38);
    566      1.68      fvdl 	}
    567      1.87       dsl 	memset(iobuf, 0, iobuf_memsize);
    568      1.80       dsl 
    569      1.80       dsl 	/*
    570      1.80       dsl 	 * We now start writing to the filesystem
    571      1.80       dsl 	 */
    572      1.80       dsl 
    573      1.80       dsl 	/*
    574      1.80       dsl 	 * Validate the given file system size.
    575      1.80       dsl 	 * Verify that its last block can actually be accessed.
    576      1.80       dsl 	 * Convert to file system fragment sized units.
    577      1.80       dsl 	 */
    578      1.80       dsl 	if (fssize <= 0) {
    579      1.80       dsl 		printf("preposterous size %lld\n", (long long)fssize);
    580      1.80       dsl 		exit(13);
    581      1.80       dsl 	}
    582      1.80       dsl 	wtfs(fssize - 1, sectorsize, iobuf);
    583      1.80       dsl 
    584      1.80       dsl 	/*
    585      1.80       dsl 	 * Ensure there is nothing that looks like a filesystem
    586      1.80       dsl 	 * superbock anywhere other than where ours will be.
    587      1.80       dsl 	 * If fsck finds the wrong one all hell breaks loose!
    588      1.80       dsl 	 */
    589      1.80       dsl 	for (i = 0; ; i++) {
    590      1.80       dsl 		static const int sblocklist[] = SBLOCKSEARCH;
    591      1.80       dsl 		int sblkoff = sblocklist[i];
    592      1.80       dsl 		int sz;
    593      1.80       dsl 		if (sblkoff == -1)
    594      1.80       dsl 			break;
    595      1.80       dsl 		/* Remove main superblock */
    596      1.80       dsl 		zap_old_sblock(sblkoff);
    597      1.80       dsl 		/* and all possible locations for the first alternate */
    598      1.80       dsl 		sblkoff += SBLOCKSIZE;
    599      1.80       dsl 		for (sz = SBLOCKSIZE; sz <= 0x10000; sz <<= 1)
    600      1.80       dsl 			zap_old_sblock(roundup(sblkoff, sz));
    601      1.80       dsl 	}
    602      1.80       dsl 
    603      1.80       dsl 	if (isappleufs) {
    604      1.80       dsl 		struct appleufslabel appleufs;
    605      1.85       dbj 		ffs_appleufs_set(&appleufs, appleufs_volname, tv.tv_sec, 0);
    606      1.80       dsl 		wtfs(APPLEUFS_LABEL_OFFSET/sectorsize, APPLEUFS_LABEL_SIZE,
    607      1.80       dsl 		    &appleufs);
    608      1.85       dbj 	} else {
    609      1.85       dbj 		struct appleufslabel appleufs;
    610      1.85       dbj 		/* Look for and zap any existing valid apple ufs labels */
    611      1.85       dbj 		rdfs(APPLEUFS_LABEL_OFFSET/sectorsize, APPLEUFS_LABEL_SIZE,
    612      1.85       dbj 		    &appleufs);
    613      1.85       dbj 		if (ffs_appleufs_validate(fsys, &appleufs, NULL) == 0) {
    614      1.85       dbj 			memset(&appleufs, 0, sizeof(appleufs));
    615      1.85       dbj 			wtfs(APPLEUFS_LABEL_OFFSET/sectorsize, APPLEUFS_LABEL_SIZE,
    616      1.85       dbj 			    &appleufs);
    617      1.85       dbj 		}
    618      1.80       dsl 	}
    619      1.80       dsl 
    620      1.36  wrstuden 	/*
    621      1.68      fvdl 	 * Make a copy of the superblock into the buffer that we will be
    622      1.68      fvdl 	 * writing out in each cylinder group.
    623       1.1       cgd 	 */
    624      1.76       dsl 	memcpy(iobuf, &sblock, sizeof sblock);
    625      1.68      fvdl 	if (needswap)
    626      1.76       dsl 		ffs_sb_swap(&sblock, (struct fs *)iobuf);
    627      1.88       dbj 	if ((sblock.fs_old_flags & FS_FLAGS_UPDATED) == 0)
    628      1.88       dbj 		memset(iobuf + offsetof(struct fs, fs_old_postbl_start),
    629      1.88       dbj 		    0xff, 256);
    630      1.68      fvdl 
    631      1.86       dsl 	if (!mfs || Nflag)
    632       1.1       cgd 		printf("super-block backups (for fsck -b #) at:");
    633       1.1       cgd 	for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
    634      1.70    atatat 		initcg(cylno, &tv);
    635      1.86       dsl 		if (mfs && !Nflag)
    636       1.1       cgd 			continue;
    637      1.36  wrstuden 		if (cylno % nprintcols == 0)
    638       1.1       cgd 			printf("\n");
    639      1.66      fvdl 		printf(" %*lld,", printcolwidth,
    640      1.66      fvdl 			(long long)fsbtodb(&sblock, cgsblock(&sblock, cylno)));
    641      1.22       jtc 		fflush(stdout);
    642       1.1       cgd 	}
    643      1.86       dsl 	if (!mfs || Nflag)
    644       1.1       cgd 		printf("\n");
    645      1.86       dsl 	if (Nflag)
    646       1.1       cgd 		exit(0);
    647      1.68      fvdl 
    648       1.1       cgd 	/*
    649       1.1       cgd 	 * Now construct the initial file system,
    650       1.1       cgd 	 */
    651      1.70    atatat 	if (fsinit(&tv, mfsmode, mfsuid, mfsgid) == 0 && mfs)
    652      1.60    simonb 		errx(1, "Error making filesystem");
    653      1.70    atatat 	sblock.fs_time = tv.tv_sec;
    654      1.68      fvdl 	if (Oflag <= 1) {
    655      1.68      fvdl 		sblock.fs_old_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir;
    656      1.68      fvdl 		sblock.fs_old_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree;
    657      1.68      fvdl 		sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree;
    658      1.68      fvdl 		sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree;
    659      1.68      fvdl 	}
    660      1.76       dsl 	/*
    661      1.76       dsl 	 * Write out the super-block and zeros until the first cg info
    662      1.76       dsl 	 */
    663      1.87       dsl 	i = cgsblock(&sblock, 0) * sblock.fs_fsize - sblock.fs_sblockloc,
    664      1.87       dsl 	memset(iobuf, 0, i);
    665      1.87       dsl 	memcpy(iobuf, &sblock, sizeof sblock);
    666      1.30    bouyer 	if (needswap)
    667      1.76       dsl 		ffs_sb_swap(&sblock, (struct fs *)iobuf);
    668      1.88       dbj 	if ((sblock.fs_old_flags & FS_FLAGS_UPDATED) == 0)
    669      1.88       dbj 		memset(iobuf + offsetof(struct fs, fs_old_postbl_start),
    670      1.88       dbj 		    0xff, 256);
    671      1.87       dsl 	wtfs(sblock.fs_sblockloc / sectorsize, i, iobuf);
    672      1.34  wrstuden 
    673      1.73       dsl 	/* Write out first and last cylinder summary sectors */
    674      1.73       dsl 	if (needswap)
    675      1.73       dsl 		ffs_csum_swap(fscs_0, fscs_0, sblock.fs_fsize);
    676      1.73       dsl 	wtfs(fsbtodb(&sblock, sblock.fs_csaddr), sblock.fs_fsize, fscs_0);
    677      1.73       dsl 
    678      1.73       dsl 	if (fscs_next > fscs_reset) {
    679      1.73       dsl 		if (needswap)
    680      1.73       dsl 			ffs_csum_swap(fscs_reset, fscs_reset, sblock.fs_fsize);
    681      1.73       dsl 		fs_csaddr++;
    682      1.73       dsl 		wtfs(fsbtodb(&sblock, fs_csaddr), sblock.fs_fsize, fscs_reset);
    683      1.73       dsl 	}
    684      1.34  wrstuden 
    685      1.87       dsl 	/* mfs doesn't need these permanently allocated */
    686      1.87       dsl 	munmap(iobuf, iobuf_memsize);
    687      1.87       dsl 	munmap(fscs_0, 2 * sblock.fs_fsize);
    688      1.87       dsl 
    689       1.1       cgd 	/*
    690       1.1       cgd 	 * Update information about this partion in pack
    691       1.1       cgd 	 * label, to that it may be updated on disk.
    692       1.1       cgd 	 */
    693      1.83       dsl 	if (pp == NULL)
    694      1.83       dsl 		return;
    695      1.65       dbj 	if (isappleufs)
    696      1.65       dbj 		pp->p_fstype = FS_APPLEUFS;
    697      1.65       dbj 	else
    698      1.65       dbj 		pp->p_fstype = FS_BSDFFS;
    699       1.1       cgd 	pp->p_fsize = sblock.fs_fsize;
    700       1.1       cgd 	pp->p_frag = sblock.fs_frag;
    701      1.68      fvdl 	pp->p_cpg = sblock.fs_fpg;
    702       1.1       cgd }
    703       1.1       cgd 
    704       1.1       cgd /*
    705       1.1       cgd  * Initialize a cylinder group.
    706       1.1       cgd  */
    707      1.26  christos void
    708      1.70    atatat initcg(int cylno, const struct timeval *tv)
    709       1.1       cgd {
    710      1.68      fvdl 	daddr_t cbase, dmax;
    711      1.87       dsl 	int32_t i, d, dlower, dupper, blkno;
    712      1.68      fvdl 	struct ufs1_dinode *dp1;
    713      1.68      fvdl 	struct ufs2_dinode *dp2;
    714      1.68      fvdl 	int start;
    715       1.1       cgd 
    716       1.1       cgd 	/*
    717       1.1       cgd 	 * Determine block bounds for cylinder group.
    718       1.1       cgd 	 * Allow space for super block summary information in first
    719       1.1       cgd 	 * cylinder group.
    720       1.1       cgd 	 */
    721       1.1       cgd 	cbase = cgbase(&sblock, cylno);
    722       1.1       cgd 	dmax = cbase + sblock.fs_fpg;
    723       1.1       cgd 	if (dmax > sblock.fs_size)
    724       1.1       cgd 		dmax = sblock.fs_size;
    725       1.1       cgd 	dlower = cgsblock(&sblock, cylno) - cbase;
    726       1.1       cgd 	dupper = cgdmin(&sblock, cylno) - cbase;
    727      1.72       dsl 	if (cylno == 0) {
    728       1.1       cgd 		dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
    729      1.72       dsl 		if (dupper >= cgstart(&sblock, cylno + 1)) {
    730      1.72       dsl 			printf("\rToo many cylinder groups to fit summary "
    731      1.72       dsl 				"information into first cylinder group\n");
    732      1.72       dsl 			exit(40);
    733      1.72       dsl 		}
    734      1.72       dsl 	}
    735      1.12   mycroft 	memset(&acg, 0, sblock.fs_cgsize);
    736       1.1       cgd 	acg.cg_magic = CG_MAGIC;
    737       1.1       cgd 	acg.cg_cgx = cylno;
    738       1.1       cgd 	acg.cg_ndblk = dmax - cbase;
    739       1.9   mycroft 	if (sblock.fs_contigsumsize > 0)
    740      1.62   mycroft 		acg.cg_nclusterblks = acg.cg_ndblk >> sblock.fs_fragshift;
    741      1.68      fvdl 	start = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield);
    742      1.68      fvdl 	if (Oflag == 2) {
    743      1.75       dsl 		acg.cg_time = tv->tv_sec;
    744      1.75       dsl 		acg.cg_niblk = sblock.fs_ipg;
    745      1.75       dsl 		acg.cg_initediblk = sblock.fs_ipg < 2 * INOPB(&sblock) ?
    746      1.75       dsl 		    sblock.fs_ipg : 2 * INOPB(&sblock);
    747      1.68      fvdl 		acg.cg_iusedoff = start;
    748      1.68      fvdl 	} else {
    749      1.68      fvdl 		acg.cg_old_ncyl = sblock.fs_old_cpg;
    750      1.88       dbj 		if ((sblock.fs_old_flags & FS_FLAGS_UPDATED) == 0 &&
    751      1.88       dbj 		    (cylno == sblock.fs_ncg - 1))
    752      1.88       dbj 			acg.cg_old_ncyl =
    753      1.88       dbj 			    sblock.fs_old_ncyl % sblock.fs_old_cpg;
    754      1.75       dsl 		acg.cg_old_time = tv->tv_sec;
    755      1.75       dsl 		acg.cg_old_niblk = sblock.fs_ipg;
    756      1.68      fvdl 		acg.cg_old_btotoff = start;
    757      1.68      fvdl 		acg.cg_old_boff = acg.cg_old_btotoff +
    758      1.68      fvdl 		    sblock.fs_old_cpg * sizeof(int32_t);
    759      1.68      fvdl 		acg.cg_iusedoff = acg.cg_old_boff +
    760      1.68      fvdl 		    sblock.fs_old_cpg * sizeof(u_int16_t);
    761      1.68      fvdl 	}
    762      1.68      fvdl 	acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT);
    763       1.9   mycroft 	if (sblock.fs_contigsumsize <= 0) {
    764       1.9   mycroft 		acg.cg_nextfreeoff = acg.cg_freeoff +
    765      1.68      fvdl 		   howmany(sblock.fs_fpg, CHAR_BIT);
    766       1.9   mycroft 	} else {
    767      1.62   mycroft 		acg.cg_clustersumoff = acg.cg_freeoff +
    768      1.68      fvdl 		    howmany(sblock.fs_fpg, CHAR_BIT) - sizeof(int32_t);
    769      1.65       dbj 		if (isappleufs) {
    770      1.65       dbj 			/* Apple PR2216969 gives rationale for this change.
    771      1.65       dbj 			 * I believe they were mistaken, but we need to
    772      1.67     grant 			 * duplicate it for compatibility.  -- dbj (at) NetBSD.org
    773      1.65       dbj 			 */
    774      1.65       dbj 			acg.cg_clustersumoff += sizeof(int32_t);
    775      1.65       dbj 		}
    776       1.9   mycroft 		acg.cg_clustersumoff =
    777      1.14       cgd 		    roundup(acg.cg_clustersumoff, sizeof(int32_t));
    778       1.9   mycroft 		acg.cg_clusteroff = acg.cg_clustersumoff +
    779      1.14       cgd 		    (sblock.fs_contigsumsize + 1) * sizeof(int32_t);
    780      1.62   mycroft 		acg.cg_nextfreeoff = acg.cg_clusteroff +
    781      1.68      fvdl 		    howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT);
    782       1.9   mycroft 	}
    783      1.41       scw 	if (acg.cg_nextfreeoff > sblock.fs_cgsize) {
    784       1.9   mycroft 		printf("Panic: cylinder group too big\n");
    785       1.9   mycroft 		exit(37);
    786       1.1       cgd 	}
    787       1.1       cgd 	acg.cg_cs.cs_nifree += sblock.fs_ipg;
    788       1.1       cgd 	if (cylno == 0)
    789       1.1       cgd 		for (i = 0; i < ROOTINO; i++) {
    790      1.30    bouyer 			setbit(cg_inosused(&acg, 0), i);
    791       1.1       cgd 			acg.cg_cs.cs_nifree--;
    792       1.1       cgd 		}
    793       1.1       cgd 	if (cylno > 0) {
    794       1.1       cgd 		/*
    795       1.1       cgd 		 * In cylno 0, beginning space is reserved
    796       1.1       cgd 		 * for boot and super blocks.
    797       1.1       cgd 		 */
    798      1.68      fvdl 		for (d = 0, blkno = 0; d < dlower;) {
    799      1.30    bouyer 			setblock(&sblock, cg_blksfree(&acg, 0), blkno);
    800       1.9   mycroft 			if (sblock.fs_contigsumsize > 0)
    801      1.30    bouyer 				setbit(cg_clustersfree(&acg, 0), blkno);
    802       1.1       cgd 			acg.cg_cs.cs_nbfree++;
    803      1.88       dbj 			if (Oflag <= 1) {
    804      1.88       dbj 				int cn = old_cbtocylno(&sblock, d);
    805      1.88       dbj 				old_cg_blktot(&acg, 0)[cn]++;
    806      1.88       dbj 				old_cg_blks(&sblock, &acg,
    807      1.88       dbj 				    cn, 0)[old_cbtorpos(&sblock, d)]++;
    808      1.88       dbj 			}
    809      1.64   mycroft 			d += sblock.fs_frag;
    810      1.64   mycroft 			blkno++;
    811       1.1       cgd 		}
    812       1.1       cgd 	}
    813      1.62   mycroft 	if ((i = (dupper & (sblock.fs_frag - 1))) != 0) {
    814       1.1       cgd 		acg.cg_frsum[sblock.fs_frag - i]++;
    815       1.1       cgd 		for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
    816      1.30    bouyer 			setbit(cg_blksfree(&acg, 0), dupper);
    817       1.1       cgd 			acg.cg_cs.cs_nffree++;
    818       1.1       cgd 		}
    819       1.1       cgd 	}
    820      1.64   mycroft 	for (d = dupper, blkno = dupper >> sblock.fs_fragshift;
    821      1.68      fvdl 	     d + sblock.fs_frag <= acg.cg_ndblk; ) {
    822      1.30    bouyer 		setblock(&sblock, cg_blksfree(&acg, 0), blkno);
    823       1.9   mycroft 		if (sblock.fs_contigsumsize > 0)
    824      1.30    bouyer 			setbit(cg_clustersfree(&acg, 0), blkno);
    825       1.1       cgd 		acg.cg_cs.cs_nbfree++;
    826      1.88       dbj 		if (Oflag <= 1) {
    827      1.88       dbj 			int cn = old_cbtocylno(&sblock, d);
    828      1.88       dbj 			old_cg_blktot(&acg, 0)[cn]++;
    829      1.88       dbj 			old_cg_blks(&sblock, &acg,
    830      1.88       dbj 			    cn, 0)[old_cbtorpos(&sblock, d)]++;
    831      1.88       dbj 		}
    832       1.1       cgd 		d += sblock.fs_frag;
    833      1.64   mycroft 		blkno++;
    834       1.1       cgd 	}
    835      1.68      fvdl 	if (d < acg.cg_ndblk) {
    836      1.68      fvdl 		acg.cg_frsum[acg.cg_ndblk - d]++;
    837      1.68      fvdl 		for (; d < acg.cg_ndblk; d++) {
    838      1.30    bouyer 			setbit(cg_blksfree(&acg, 0), d);
    839       1.1       cgd 			acg.cg_cs.cs_nffree++;
    840       1.1       cgd 		}
    841       1.1       cgd 	}
    842       1.9   mycroft 	if (sblock.fs_contigsumsize > 0) {
    843      1.30    bouyer 		int32_t *sump = cg_clustersum(&acg, 0);
    844      1.30    bouyer 		u_char *mapp = cg_clustersfree(&acg, 0);
    845       1.9   mycroft 		int map = *mapp++;
    846       1.9   mycroft 		int bit = 1;
    847       1.9   mycroft 		int run = 0;
    848       1.9   mycroft 
    849       1.9   mycroft 		for (i = 0; i < acg.cg_nclusterblks; i++) {
    850       1.9   mycroft 			if ((map & bit) != 0) {
    851       1.9   mycroft 				run++;
    852       1.9   mycroft 			} else if (run != 0) {
    853       1.9   mycroft 				if (run > sblock.fs_contigsumsize)
    854       1.9   mycroft 					run = sblock.fs_contigsumsize;
    855       1.9   mycroft 				sump[run]++;
    856       1.9   mycroft 				run = 0;
    857       1.9   mycroft 			}
    858      1.68      fvdl 			if ((i & (CHAR_BIT - 1)) != (CHAR_BIT - 1)) {
    859       1.9   mycroft 				bit <<= 1;
    860       1.9   mycroft 			} else {
    861       1.9   mycroft 				map = *mapp++;
    862       1.9   mycroft 				bit = 1;
    863       1.9   mycroft 			}
    864       1.9   mycroft 		}
    865       1.9   mycroft 		if (run != 0) {
    866       1.9   mycroft 			if (run > sblock.fs_contigsumsize)
    867       1.9   mycroft 				run = sblock.fs_contigsumsize;
    868       1.9   mycroft 			sump[run]++;
    869       1.9   mycroft 		}
    870       1.9   mycroft 	}
    871      1.73       dsl 	*fscs_next++ = acg.cg_cs;
    872      1.73       dsl 	if (fscs_next == fscs_end) {
    873      1.87       dsl 		/* write block of cylinder group summary info into cyl 0 */
    874      1.73       dsl 		if (needswap)
    875      1.73       dsl 			ffs_csum_swap(fscs_reset, fscs_reset, sblock.fs_fsize);
    876      1.73       dsl 		fs_csaddr++;
    877      1.73       dsl 		wtfs(fsbtodb(&sblock, fs_csaddr), sblock.fs_fsize, fscs_reset);
    878      1.73       dsl 		fscs_next = fscs_reset;
    879      1.73       dsl 		memset(fscs_next, 0, sblock.fs_fsize);
    880      1.73       dsl 	}
    881      1.68      fvdl 	/*
    882      1.68      fvdl 	 * Write out the duplicate super block, the cylinder group map
    883      1.68      fvdl 	 * and two blocks worth of inodes in a single write.
    884      1.68      fvdl 	 */
    885      1.68      fvdl 	start = sblock.fs_bsize > SBLOCKSIZE ? sblock.fs_bsize : SBLOCKSIZE;
    886      1.68      fvdl 	memcpy(&iobuf[start], &acg, sblock.fs_cgsize);
    887      1.30    bouyer 	if (needswap)
    888      1.68      fvdl 		ffs_cg_swap(&acg, (struct cg*)&iobuf[start], &sblock);
    889      1.68      fvdl 	start += sblock.fs_bsize;
    890      1.68      fvdl 	dp1 = (struct ufs1_dinode *)(&iobuf[start]);
    891      1.68      fvdl 	dp2 = (struct ufs2_dinode *)(&iobuf[start]);
    892      1.75       dsl 	for (i = MIN(sblock.fs_ipg, 2) * INOPB(&sblock); i != 0; i--) {
    893      1.68      fvdl 		if (sblock.fs_magic == FS_UFS1_MAGIC) {
    894      1.68      fvdl 			/* No need to swap, it'll stay random */
    895      1.78    itojun 			dp1->di_gen = arc4random() & INT32_MAX;
    896      1.68      fvdl 			dp1++;
    897      1.68      fvdl 		} else {
    898      1.78    itojun 			dp2->di_gen = arc4random() & INT32_MAX;
    899      1.68      fvdl 			dp2++;
    900      1.68      fvdl 		}
    901      1.68      fvdl 	}
    902      1.68      fvdl 	wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)), iobufsize, iobuf);
    903      1.68      fvdl 	/*
    904      1.68      fvdl 	 * For the old file system, we have to initialize all the inodes.
    905      1.68      fvdl 	 */
    906      1.87       dsl 	if (sblock.fs_magic != FS_UFS1_MAGIC)
    907      1.87       dsl 		return;
    908      1.87       dsl 
    909      1.87       dsl 	/* Write 'd' (usually 16 * fs_frag) file-system fragments at once */
    910      1.87       dsl 	d = (iobuf_memsize - start) / sblock.fs_bsize * sblock.fs_frag;
    911      1.87       dsl 	dupper = sblock.fs_ipg / INOPF(&sblock);
    912      1.87       dsl 	for (i = 2 * sblock.fs_frag; i < dupper; i += d) {
    913      1.87       dsl 		if (d > dupper - i)
    914      1.87       dsl 			d = dupper - i;
    915      1.87       dsl 		dp1 = (struct ufs1_dinode *)(&iobuf[start]);
    916      1.87       dsl 		do
    917      1.87       dsl 			dp1->di_gen = arc4random() & INT32_MAX;
    918      1.87       dsl 		while ((char *)++dp1 < &iobuf[iobuf_memsize]);
    919      1.87       dsl 		wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
    920      1.87       dsl 		    d * sblock.fs_bsize / sblock.fs_frag, &iobuf[start]);
    921      1.68      fvdl 	}
    922       1.1       cgd }
    923       1.1       cgd 
    924       1.1       cgd /*
    925       1.1       cgd  * initialize the file system
    926       1.1       cgd  */
    927       1.1       cgd 
    928       1.1       cgd #ifdef LOSTDIR
    929      1.60    simonb #define	PREDEFDIR 3
    930       1.1       cgd #else
    931      1.60    simonb #define	PREDEFDIR 2
    932       1.1       cgd #endif
    933       1.1       cgd 
    934       1.1       cgd struct direct root_dir[] = {
    935       1.9   mycroft 	{ ROOTINO, sizeof(struct direct), DT_DIR, 1, "." },
    936       1.9   mycroft 	{ ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
    937       1.9   mycroft #ifdef LOSTDIR
    938       1.9   mycroft 	{ LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 10, "lost+found" },
    939       1.9   mycroft #endif
    940       1.9   mycroft };
    941       1.9   mycroft struct odirect {
    942      1.14       cgd 	u_int32_t d_ino;
    943      1.14       cgd 	u_int16_t d_reclen;
    944      1.14       cgd 	u_int16_t d_namlen;
    945       1.9   mycroft 	u_char	d_name[MAXNAMLEN + 1];
    946       1.9   mycroft } oroot_dir[] = {
    947       1.1       cgd 	{ ROOTINO, sizeof(struct direct), 1, "." },
    948       1.1       cgd 	{ ROOTINO, sizeof(struct direct), 2, ".." },
    949       1.1       cgd #ifdef LOSTDIR
    950       1.1       cgd 	{ LOSTFOUNDINO, sizeof(struct direct), 10, "lost+found" },
    951       1.1       cgd #endif
    952       1.1       cgd };
    953       1.1       cgd #ifdef LOSTDIR
    954       1.1       cgd struct direct lost_found_dir[] = {
    955       1.9   mycroft 	{ LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 1, "." },
    956       1.9   mycroft 	{ ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
    957       1.9   mycroft 	{ 0, DIRBLKSIZ, 0, 0, 0 },
    958       1.9   mycroft };
    959       1.9   mycroft struct odirect olost_found_dir[] = {
    960       1.1       cgd 	{ LOSTFOUNDINO, sizeof(struct direct), 1, "." },
    961       1.1       cgd 	{ ROOTINO, sizeof(struct direct), 2, ".." },
    962       1.1       cgd 	{ 0, DIRBLKSIZ, 0, 0 },
    963       1.1       cgd };
    964       1.1       cgd #endif
    965       1.1       cgd char buf[MAXBSIZE];
    966      1.39    simonb static void copy_dir(struct direct *, struct direct *);
    967       1.1       cgd 
    968      1.60    simonb int
    969      1.70    atatat fsinit(const struct timeval *tv, mode_t mfsmode, uid_t mfsuid, gid_t mfsgid)
    970       1.1       cgd {
    971      1.75       dsl 	union dinode node;
    972      1.26  christos #ifdef LOSTDIR
    973       1.1       cgd 	int i;
    974      1.65       dbj 	int dirblksiz = DIRBLKSIZ;
    975      1.65       dbj 	if (isappleufs)
    976      1.65       dbj 		dirblksiz = APPLEUFS_DIRBLKSIZ;
    977      1.26  christos #endif
    978       1.1       cgd 
    979       1.1       cgd 	/*
    980       1.1       cgd 	 * initialize the node
    981       1.1       cgd 	 */
    982      1.30    bouyer 
    983       1.1       cgd #ifdef LOSTDIR
    984       1.1       cgd 	/*
    985       1.1       cgd 	 * create the lost+found directory
    986       1.1       cgd 	 */
    987      1.75       dsl 	memset(&node, 0, sizeof(node));
    988      1.68      fvdl 	if (Oflag == 0) {
    989       1.9   mycroft 		(void)makedir((struct direct *)olost_found_dir, 2);
    990      1.65       dbj 		for (i = dirblksiz; i < sblock.fs_bsize; i += dirblksiz)
    991      1.30    bouyer 			copy_dir((struct direct*)&olost_found_dir[2],
    992      1.30    bouyer 				(struct direct*)&buf[i]);
    993       1.9   mycroft 	} else {
    994       1.9   mycroft 		(void)makedir(lost_found_dir, 2);
    995      1.65       dbj 		for (i = dirblksiz; i < sblock.fs_bsize; i += dirblksiz)
    996      1.30    bouyer 			copy_dir(&lost_found_dir[2], (struct direct*)&buf[i]);
    997       1.9   mycroft 	}
    998      1.68      fvdl 	if (sblock.fs_magic == FS_UFS1_MAGIC) {
    999      1.70    atatat 		node.dp1.di_atime = tv->tv_sec;
   1000      1.70    atatat 		node.dp1.di_atimensec = tv->tv_usec * 1000;
   1001      1.70    atatat 		node.dp1.di_mtime = tv->tv_sec;
   1002      1.70    atatat 		node.dp1.di_mtimensec = tv->tv_usec * 1000;
   1003      1.70    atatat 		node.dp1.di_ctime = tv->tv_sec;
   1004      1.70    atatat 		node.dp1.di_ctimensec = tv->tv_usec * 1000;
   1005      1.68      fvdl 		node.dp1.di_mode = IFDIR | UMASK;
   1006      1.68      fvdl 		node.dp1.di_nlink = 2;
   1007      1.68      fvdl 		node.dp1.di_size = sblock.fs_bsize;
   1008      1.68      fvdl 		node.dp1.di_db[0] = alloc(node.dp1.di_size, node.dp1.di_mode);
   1009      1.69  christos 		if (node.dp1.di_db[0] == 0)
   1010      1.69  christos 			return (0);
   1011      1.68      fvdl 		node.dp1.di_blocks = btodb(fragroundup(&sblock,
   1012      1.68      fvdl 		    node.dp1.di_size));
   1013      1.68      fvdl 		node.dp1.di_uid = geteuid();
   1014      1.68      fvdl 		node.dp1.di_gid = getegid();
   1015      1.68      fvdl 		wtfs(fsbtodb(&sblock, node.dp1.di_db[0]), node.dp1.di_size,
   1016      1.68      fvdl 		    buf);
   1017      1.68      fvdl 	} else {
   1018      1.70    atatat 		node.dp2.di_atime = tv->tv_sec;
   1019      1.70    atatat 		node.dp2.di_atimensec = tv->tv_usec * 1000;
   1020      1.70    atatat 		node.dp2.di_mtime = tv->tv_sec;
   1021      1.70    atatat 		node.dp2.di_mtimensec = tv->tv_usec * 1000;
   1022      1.70    atatat 		node.dp2.di_ctime = tv->tv_sec;
   1023      1.70    atatat 		node.dp2.di_ctimensec = tv->tv_usec * 1000;
   1024      1.70    atatat 		node.dp2.di_birthtime = tv->tv_sec;
   1025      1.70    atatat 		node.dp2.di_birthnsec = tv->tv_usec * 1000;
   1026      1.68      fvdl 		node.dp2.di_mode = IFDIR | UMASK;
   1027      1.68      fvdl 		node.dp2.di_nlink = 2;
   1028      1.68      fvdl 		node.dp2.di_size = sblock.fs_bsize;
   1029      1.68      fvdl 		node.dp2.di_db[0] = alloc(node.dp2.di_size, node.dp2.di_mode);
   1030      1.69  christos 		if (node.dp2.di_db[0] == 0)
   1031      1.69  christos 			return (0);
   1032      1.68      fvdl 		node.dp2.di_blocks = btodb(fragroundup(&sblock,
   1033      1.68      fvdl 		    node.dp2.di_size));
   1034      1.68      fvdl 		node.dp2.di_uid = geteuid();
   1035      1.68      fvdl 		node.dp2.di_gid = getegid();
   1036      1.68      fvdl 		wtfs(fsbtodb(&sblock, node.dp2.di_db[0]), node.dp2.di_size,
   1037      1.68      fvdl 		    buf);
   1038      1.68      fvdl 	}
   1039       1.1       cgd 	iput(&node, LOSTFOUNDINO);
   1040       1.1       cgd #endif
   1041       1.1       cgd 	/*
   1042       1.1       cgd 	 * create the root directory
   1043       1.1       cgd 	 */
   1044      1.75       dsl 	memset(&node, 0, sizeof(node));
   1045      1.68      fvdl 	if (Oflag <= 1) {
   1046      1.68      fvdl 		if (mfs) {
   1047      1.68      fvdl 			node.dp1.di_mode = IFDIR | mfsmode;
   1048      1.68      fvdl 			node.dp1.di_uid = mfsuid;
   1049      1.68      fvdl 			node.dp1.di_gid = mfsgid;
   1050      1.68      fvdl 		} else {
   1051      1.68      fvdl 			node.dp1.di_mode = IFDIR | UMASK;
   1052      1.68      fvdl 			node.dp1.di_uid = geteuid();
   1053      1.68      fvdl 			node.dp1.di_gid = getegid();
   1054      1.68      fvdl 		}
   1055      1.68      fvdl 		node.dp1.di_nlink = PREDEFDIR;
   1056      1.68      fvdl 		if (Oflag == 0)
   1057      1.68      fvdl 			node.dp1.di_size = makedir((struct direct *)oroot_dir,
   1058      1.68      fvdl 			    PREDEFDIR);
   1059      1.68      fvdl 		else
   1060      1.68      fvdl 			node.dp1.di_size = makedir(root_dir, PREDEFDIR);
   1061      1.68      fvdl 		node.dp1.di_db[0] = alloc(sblock.fs_fsize, node.dp1.di_mode);
   1062      1.68      fvdl 		if (node.dp1.di_db[0] == 0)
   1063      1.68      fvdl 			return (0);
   1064      1.68      fvdl 		node.dp1.di_blocks = btodb(fragroundup(&sblock,
   1065      1.68      fvdl 		    node.dp1.di_size));
   1066      1.68      fvdl 		wtfs(fsbtodb(&sblock, node.dp1.di_db[0]), sblock.fs_fsize, buf);
   1067      1.60    simonb 	} else {
   1068      1.68      fvdl 		if (mfs) {
   1069      1.68      fvdl 			node.dp2.di_mode = IFDIR | mfsmode;
   1070      1.68      fvdl 			node.dp2.di_uid = mfsuid;
   1071      1.68      fvdl 			node.dp2.di_gid = mfsgid;
   1072      1.68      fvdl 		} else {
   1073      1.68      fvdl 			node.dp2.di_mode = IFDIR | UMASK;
   1074      1.68      fvdl 			node.dp2.di_uid = geteuid();
   1075      1.68      fvdl 			node.dp2.di_gid = getegid();
   1076      1.68      fvdl 		}
   1077      1.70    atatat 		node.dp2.di_atime = tv->tv_sec;
   1078      1.70    atatat 		node.dp2.di_atimensec = tv->tv_usec * 1000;
   1079      1.70    atatat 		node.dp2.di_mtime = tv->tv_sec;
   1080      1.70    atatat 		node.dp2.di_mtimensec = tv->tv_usec * 1000;
   1081      1.70    atatat 		node.dp2.di_ctime = tv->tv_sec;
   1082      1.70    atatat 		node.dp2.di_ctimensec = tv->tv_usec * 1000;
   1083      1.70    atatat 		node.dp2.di_birthtime = tv->tv_sec;
   1084      1.70    atatat 		node.dp2.di_birthnsec = tv->tv_usec * 1000;
   1085      1.68      fvdl 		node.dp2.di_nlink = PREDEFDIR;
   1086      1.68      fvdl 		node.dp2.di_size = makedir(root_dir, PREDEFDIR);
   1087      1.68      fvdl 		node.dp2.di_db[0] = alloc(sblock.fs_fsize, node.dp2.di_mode);
   1088      1.68      fvdl 		if (node.dp2.di_db[0] == 0)
   1089      1.68      fvdl 			return (0);
   1090      1.68      fvdl 		node.dp2.di_blocks = btodb(fragroundup(&sblock,
   1091      1.68      fvdl 		    node.dp2.di_size));
   1092      1.68      fvdl 		wtfs(fsbtodb(&sblock, node.dp2.di_db[0]), sblock.fs_fsize, buf);
   1093      1.68      fvdl 	}
   1094       1.1       cgd 	iput(&node, ROOTINO);
   1095      1.60    simonb 	return (1);
   1096       1.1       cgd }
   1097       1.1       cgd 
   1098       1.1       cgd /*
   1099       1.1       cgd  * construct a set of directory entries in "buf".
   1100       1.1       cgd  * return size of directory.
   1101       1.1       cgd  */
   1102      1.26  christos int
   1103      1.39    simonb makedir(struct direct *protodir, int entries)
   1104       1.1       cgd {
   1105       1.1       cgd 	char *cp;
   1106       1.1       cgd 	int i, spcleft;
   1107      1.65       dbj 	int dirblksiz = DIRBLKSIZ;
   1108      1.65       dbj 	if (isappleufs)
   1109      1.65       dbj 		dirblksiz = APPLEUFS_DIRBLKSIZ;
   1110       1.1       cgd 
   1111      1.68      fvdl 	memset(buf, 0, DIRBLKSIZ);
   1112      1.65       dbj 	spcleft = dirblksiz;
   1113       1.1       cgd 	for (cp = buf, i = 0; i < entries - 1; i++) {
   1114      1.68      fvdl 		protodir[i].d_reclen = DIRSIZ(Oflag == 0, &protodir[i], 0);
   1115      1.30    bouyer 		copy_dir(&protodir[i], (struct direct*)cp);
   1116       1.1       cgd 		cp += protodir[i].d_reclen;
   1117       1.1       cgd 		spcleft -= protodir[i].d_reclen;
   1118       1.1       cgd 	}
   1119       1.1       cgd 	protodir[i].d_reclen = spcleft;
   1120      1.30    bouyer 	copy_dir(&protodir[i], (struct direct*)cp);
   1121      1.65       dbj 	return (dirblksiz);
   1122       1.1       cgd }
   1123       1.1       cgd 
   1124       1.1       cgd /*
   1125       1.1       cgd  * allocate a block or frag
   1126       1.1       cgd  */
   1127       1.1       cgd daddr_t
   1128      1.39    simonb alloc(int size, int mode)
   1129       1.1       cgd {
   1130       1.1       cgd 	int i, frag;
   1131       1.9   mycroft 	daddr_t d, blkno;
   1132       1.1       cgd 
   1133      1.26  christos 	rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
   1134      1.30    bouyer 	/* fs -> host byte order */
   1135      1.30    bouyer 	if (needswap)
   1136      1.68      fvdl 		ffs_cg_swap(&acg, &acg, &sblock);
   1137       1.1       cgd 	if (acg.cg_magic != CG_MAGIC) {
   1138       1.1       cgd 		printf("cg 0: bad magic number\n");
   1139       1.1       cgd 		return (0);
   1140       1.1       cgd 	}
   1141       1.1       cgd 	if (acg.cg_cs.cs_nbfree == 0) {
   1142       1.1       cgd 		printf("first cylinder group ran out of space\n");
   1143       1.1       cgd 		return (0);
   1144       1.1       cgd 	}
   1145       1.1       cgd 	for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag)
   1146      1.62   mycroft 		if (isblock(&sblock, cg_blksfree(&acg, 0),
   1147      1.62   mycroft 		    d >> sblock.fs_fragshift))
   1148       1.1       cgd 			goto goth;
   1149       1.1       cgd 	printf("internal error: can't find block in cyl 0\n");
   1150       1.1       cgd 	return (0);
   1151       1.1       cgd goth:
   1152       1.9   mycroft 	blkno = fragstoblks(&sblock, d);
   1153      1.30    bouyer 	clrblock(&sblock, cg_blksfree(&acg, 0), blkno);
   1154      1.10       cgd 	if (sblock.fs_contigsumsize > 0)
   1155      1.30    bouyer 		clrbit(cg_clustersfree(&acg, 0), blkno);
   1156       1.1       cgd 	acg.cg_cs.cs_nbfree--;
   1157       1.1       cgd 	sblock.fs_cstotal.cs_nbfree--;
   1158      1.73       dsl 	fscs_0->cs_nbfree--;
   1159       1.1       cgd 	if (mode & IFDIR) {
   1160       1.1       cgd 		acg.cg_cs.cs_ndir++;
   1161       1.1       cgd 		sblock.fs_cstotal.cs_ndir++;
   1162      1.73       dsl 		fscs_0->cs_ndir++;
   1163       1.1       cgd 	}
   1164      1.88       dbj 	if (Oflag <= 1) {
   1165      1.88       dbj 		int cn = old_cbtocylno(&sblock, d);
   1166      1.88       dbj 		old_cg_blktot(&acg, 0)[cn]--;
   1167      1.88       dbj 		old_cg_blks(&sblock, &acg,
   1168      1.88       dbj 		    cn, 0)[old_cbtorpos(&sblock, d)]--;
   1169      1.88       dbj 	}
   1170       1.1       cgd 	if (size != sblock.fs_bsize) {
   1171       1.1       cgd 		frag = howmany(size, sblock.fs_fsize);
   1172      1.73       dsl 		fscs_0->cs_nffree += sblock.fs_frag - frag;
   1173       1.1       cgd 		sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag;
   1174       1.1       cgd 		acg.cg_cs.cs_nffree += sblock.fs_frag - frag;
   1175       1.1       cgd 		acg.cg_frsum[sblock.fs_frag - frag]++;
   1176       1.1       cgd 		for (i = frag; i < sblock.fs_frag; i++)
   1177      1.30    bouyer 			setbit(cg_blksfree(&acg, 0), d + i);
   1178       1.1       cgd 	}
   1179      1.30    bouyer 	/* host -> fs byte order */
   1180      1.30    bouyer 	if (needswap)
   1181      1.68      fvdl 		ffs_cg_swap(&acg, &acg, &sblock);
   1182      1.72       dsl 	wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
   1183       1.1       cgd 	return (d);
   1184       1.1       cgd }
   1185       1.1       cgd 
   1186       1.1       cgd /*
   1187       1.1       cgd  * Allocate an inode on the disk
   1188       1.1       cgd  */
   1189      1.26  christos static void
   1190      1.68      fvdl iput(union dinode *ip, ino_t ino)
   1191       1.1       cgd {
   1192       1.1       cgd 	daddr_t d;
   1193      1.30    bouyer 	int c, i;
   1194      1.68      fvdl 	struct ufs1_dinode *dp1;
   1195      1.68      fvdl 	struct ufs2_dinode *dp2;
   1196       1.1       cgd 
   1197       1.9   mycroft 	c = ino_to_cg(&sblock, ino);
   1198      1.26  christos 	rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
   1199      1.30    bouyer 	/* fs -> host byte order */
   1200      1.30    bouyer 	if (needswap)
   1201      1.68      fvdl 		ffs_cg_swap(&acg, &acg, &sblock);
   1202       1.1       cgd 	if (acg.cg_magic != CG_MAGIC) {
   1203       1.1       cgd 		printf("cg 0: bad magic number\n");
   1204       1.1       cgd 		exit(31);
   1205       1.1       cgd 	}
   1206       1.1       cgd 	acg.cg_cs.cs_nifree--;
   1207      1.30    bouyer 	setbit(cg_inosused(&acg, 0), ino);
   1208      1.30    bouyer 	/* host -> fs byte order */
   1209      1.30    bouyer 	if (needswap)
   1210      1.68      fvdl 		ffs_cg_swap(&acg, &acg, &sblock);
   1211      1.72       dsl 	wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
   1212       1.1       cgd 	sblock.fs_cstotal.cs_nifree--;
   1213      1.73       dsl 	fscs_0->cs_nifree--;
   1214       1.1       cgd 	if (ino >= sblock.fs_ipg * sblock.fs_ncg) {
   1215       1.1       cgd 		printf("fsinit: inode value out of range (%d).\n", ino);
   1216       1.1       cgd 		exit(32);
   1217       1.1       cgd 	}
   1218       1.9   mycroft 	d = fsbtodb(&sblock, ino_to_fsba(&sblock, ino));
   1219      1.68      fvdl 	rdfs(d, sblock.fs_bsize, (char *)iobuf);
   1220      1.68      fvdl 	if (sblock.fs_magic == FS_UFS1_MAGIC) {
   1221      1.68      fvdl 		dp1 = (struct ufs1_dinode *)iobuf;
   1222      1.75       dsl 		dp1 += ino_to_fsbo(&sblock, ino);
   1223      1.68      fvdl 		if (needswap) {
   1224      1.75       dsl 			ffs_dinode1_swap(&ip->dp1, dp1);
   1225      1.68      fvdl 			/* ffs_dinode1_swap() doesn't swap blocks addrs */
   1226      1.68      fvdl 			for (i=0; i<NDADDR + NIADDR; i++)
   1227      1.75       dsl 			    dp1->di_db[i] = bswap32(ip->dp1.di_db[i]);
   1228      1.68      fvdl 		} else
   1229      1.75       dsl 			*dp1 = ip->dp1;
   1230      1.79    itojun 		dp1->di_gen = arc4random() & INT32_MAX;
   1231      1.68      fvdl 	} else {
   1232      1.68      fvdl 		dp2 = (struct ufs2_dinode *)iobuf;
   1233      1.75       dsl 		dp2 += ino_to_fsbo(&sblock, ino);
   1234      1.68      fvdl 		if (needswap) {
   1235      1.75       dsl 			ffs_dinode2_swap(&ip->dp2, dp2);
   1236      1.68      fvdl 			for (i=0; i<NDADDR + NIADDR; i++)
   1237      1.75       dsl 			    dp2->di_db[i] = bswap32(ip->dp2.di_db[i]);
   1238      1.68      fvdl 		} else
   1239      1.75       dsl 			*dp2 = ip->dp2;
   1240      1.79    itojun 		dp2->di_gen = arc4random() & INT32_MAX;
   1241      1.68      fvdl 	}
   1242      1.68      fvdl 	wtfs(d, sblock.fs_bsize, iobuf);
   1243       1.1       cgd }
   1244       1.1       cgd 
   1245       1.1       cgd /*
   1246       1.1       cgd  * read a block from the file system
   1247       1.1       cgd  */
   1248      1.26  christos void
   1249      1.39    simonb rdfs(daddr_t bno, int size, void *bf)
   1250       1.1       cgd {
   1251       1.1       cgd 	int n;
   1252      1.18       cgd 	off_t offset;
   1253       1.1       cgd 
   1254      1.61     lukem #ifdef MFS
   1255       1.1       cgd 	if (mfs) {
   1256      1.86       dsl 		if (Nflag)
   1257      1.86       dsl 			memset(bf, 0, size);
   1258      1.86       dsl 		else
   1259      1.86       dsl 			memmove(bf, membase + bno * sectorsize, size);
   1260       1.1       cgd 		return;
   1261       1.1       cgd 	}
   1262      1.61     lukem #endif
   1263      1.18       cgd 	offset = bno;
   1264      1.72       dsl 	n = pread(fsi, bf, size, offset * sectorsize);
   1265       1.9   mycroft 	if (n != size) {
   1266      1.66      fvdl 		printf("rdfs: read error for sector %lld: %s\n",
   1267      1.66      fvdl 		    (long long)bno, strerror(errno));
   1268       1.1       cgd 		exit(34);
   1269       1.1       cgd 	}
   1270       1.1       cgd }
   1271       1.1       cgd 
   1272       1.1       cgd /*
   1273       1.1       cgd  * write a block to the file system
   1274       1.1       cgd  */
   1275      1.26  christos void
   1276      1.39    simonb wtfs(daddr_t bno, int size, void *bf)
   1277       1.1       cgd {
   1278       1.1       cgd 	int n;
   1279      1.18       cgd 	off_t offset;
   1280       1.1       cgd 
   1281      1.86       dsl 	if (Nflag)
   1282      1.86       dsl 		return;
   1283      1.61     lukem #ifdef MFS
   1284       1.1       cgd 	if (mfs) {
   1285      1.27     lukem 		memmove(membase + bno * sectorsize, bf, size);
   1286       1.1       cgd 		return;
   1287       1.1       cgd 	}
   1288      1.61     lukem #endif
   1289      1.18       cgd 	offset = bno;
   1290      1.72       dsl 	n = pwrite(fso, bf, size, offset * sectorsize);
   1291       1.9   mycroft 	if (n != size) {
   1292      1.66      fvdl 		printf("wtfs: write error for sector %lld: %s\n",
   1293      1.66      fvdl 		    (long long)bno, strerror(errno));
   1294       1.1       cgd 		exit(36);
   1295       1.1       cgd 	}
   1296       1.1       cgd }
   1297       1.1       cgd 
   1298       1.1       cgd /*
   1299       1.1       cgd  * check if a block is available
   1300       1.1       cgd  */
   1301      1.26  christos int
   1302      1.39    simonb isblock(struct fs *fs, unsigned char *cp, int h)
   1303       1.1       cgd {
   1304       1.1       cgd 	unsigned char mask;
   1305       1.1       cgd 
   1306      1.62   mycroft 	switch (fs->fs_fragshift) {
   1307      1.62   mycroft 	case 3:
   1308       1.1       cgd 		return (cp[h] == 0xff);
   1309      1.62   mycroft 	case 2:
   1310       1.1       cgd 		mask = 0x0f << ((h & 0x1) << 2);
   1311       1.1       cgd 		return ((cp[h >> 1] & mask) == mask);
   1312      1.62   mycroft 	case 1:
   1313       1.1       cgd 		mask = 0x03 << ((h & 0x3) << 1);
   1314       1.1       cgd 		return ((cp[h >> 2] & mask) == mask);
   1315      1.62   mycroft 	case 0:
   1316       1.1       cgd 		mask = 0x01 << (h & 0x7);
   1317       1.1       cgd 		return ((cp[h >> 3] & mask) == mask);
   1318       1.1       cgd 	default:
   1319       1.1       cgd #ifdef STANDALONE
   1320      1.62   mycroft 		printf("isblock bad fs_fragshift %d\n", fs->fs_fragshift);
   1321       1.1       cgd #else
   1322      1.62   mycroft 		fprintf(stderr, "isblock bad fs_fragshift %d\n",
   1323      1.62   mycroft 		    fs->fs_fragshift);
   1324       1.1       cgd #endif
   1325       1.1       cgd 		return (0);
   1326       1.1       cgd 	}
   1327       1.1       cgd }
   1328       1.1       cgd 
   1329       1.1       cgd /*
   1330       1.1       cgd  * take a block out of the map
   1331       1.1       cgd  */
   1332      1.26  christos void
   1333      1.39    simonb clrblock(struct fs *fs, unsigned char *cp, int h)
   1334       1.1       cgd {
   1335      1.62   mycroft 	switch ((fs)->fs_fragshift) {
   1336      1.62   mycroft 	case 3:
   1337       1.1       cgd 		cp[h] = 0;
   1338       1.1       cgd 		return;
   1339      1.62   mycroft 	case 2:
   1340       1.1       cgd 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
   1341       1.1       cgd 		return;
   1342      1.62   mycroft 	case 1:
   1343       1.1       cgd 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
   1344       1.1       cgd 		return;
   1345      1.62   mycroft 	case 0:
   1346       1.1       cgd 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
   1347       1.1       cgd 		return;
   1348       1.1       cgd 	default:
   1349       1.1       cgd #ifdef STANDALONE
   1350      1.62   mycroft 		printf("clrblock bad fs_fragshift %d\n", fs->fs_fragshift);
   1351       1.1       cgd #else
   1352      1.62   mycroft 		fprintf(stderr, "clrblock bad fs_fragshift %d\n",
   1353      1.62   mycroft 		    fs->fs_fragshift);
   1354       1.1       cgd #endif
   1355       1.1       cgd 		return;
   1356       1.1       cgd 	}
   1357       1.1       cgd }
   1358       1.1       cgd 
   1359       1.1       cgd /*
   1360       1.1       cgd  * put a block into the map
   1361       1.1       cgd  */
   1362      1.26  christos void
   1363      1.39    simonb setblock(struct fs *fs, unsigned char *cp, int h)
   1364       1.1       cgd {
   1365      1.62   mycroft 	switch (fs->fs_fragshift) {
   1366      1.62   mycroft 	case 3:
   1367       1.1       cgd 		cp[h] = 0xff;
   1368       1.1       cgd 		return;
   1369      1.62   mycroft 	case 2:
   1370       1.1       cgd 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
   1371       1.1       cgd 		return;
   1372      1.62   mycroft 	case 1:
   1373       1.1       cgd 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
   1374       1.1       cgd 		return;
   1375      1.62   mycroft 	case 0:
   1376       1.1       cgd 		cp[h >> 3] |= (0x01 << (h & 0x7));
   1377       1.1       cgd 		return;
   1378       1.1       cgd 	default:
   1379       1.1       cgd #ifdef STANDALONE
   1380      1.62   mycroft 		printf("setblock bad fs_frag %d\n", fs->fs_fragshift);
   1381       1.1       cgd #else
   1382      1.62   mycroft 		fprintf(stderr, "setblock bad fs_fragshift %d\n",
   1383      1.62   mycroft 		    fs->fs_fragshift);
   1384       1.1       cgd #endif
   1385       1.1       cgd 		return;
   1386      1.30    bouyer 	}
   1387      1.30    bouyer }
   1388      1.30    bouyer 
   1389      1.30    bouyer /* copy a direntry to a buffer, in fs byte order */
   1390      1.30    bouyer static void
   1391      1.39    simonb copy_dir(struct direct *dir, struct direct *dbuf)
   1392      1.30    bouyer {
   1393      1.68      fvdl 	memcpy(dbuf, dir, DIRSIZ(Oflag == 0, dir, 0));
   1394      1.30    bouyer 	if (needswap) {
   1395      1.30    bouyer 		dbuf->d_ino = bswap32(dir->d_ino);
   1396      1.30    bouyer 		dbuf->d_reclen = bswap16(dir->d_reclen);
   1397      1.68      fvdl 		if (Oflag == 0)
   1398      1.30    bouyer 			((struct odirect*)dbuf)->d_namlen =
   1399      1.30    bouyer 				bswap16(((struct odirect*)dir)->d_namlen);
   1400       1.1       cgd 	}
   1401      1.36  wrstuden }
   1402      1.36  wrstuden 
   1403      1.36  wrstuden /* Determine how many digits are needed to print a given integer */
   1404      1.36  wrstuden static int
   1405      1.72       dsl count_digits(uint64_t num)
   1406      1.36  wrstuden {
   1407      1.36  wrstuden 	int ndig;
   1408      1.36  wrstuden 
   1409      1.72       dsl 	for (ndig = 1; num > 9; num /= 10, ndig++);
   1410      1.36  wrstuden 
   1411      1.36  wrstuden 	return (ndig);
   1412      1.60    simonb }
   1413      1.68      fvdl 
   1414      1.68      fvdl static int
   1415      1.68      fvdl ilog2(int val)
   1416      1.68      fvdl {
   1417      1.68      fvdl 	u_int n;
   1418      1.68      fvdl 
   1419      1.68      fvdl 	for (n = 0; n < sizeof(n) * CHAR_BIT; n++)
   1420      1.68      fvdl 		if (1 << n == val)
   1421      1.68      fvdl 			return (n);
   1422      1.68      fvdl 	errx(1, "ilog2: %d is not a power of 2\n", val);
   1423      1.80       dsl }
   1424      1.80       dsl 
   1425      1.80       dsl static void
   1426      1.80       dsl zap_old_sblock(int sblkoff)
   1427      1.80       dsl {
   1428      1.80       dsl 	static int cg0_data;
   1429      1.80       dsl 	uint32_t oldfs[SBLOCKSIZE / 4];
   1430      1.80       dsl 	static const struct fsm {
   1431      1.80       dsl 		uint32_t	offset;
   1432      1.80       dsl 		uint32_t	magic;
   1433      1.80       dsl 		uint32_t	mask;
   1434      1.80       dsl 	} fs_magics[] = {
   1435      1.80       dsl 		{offsetof(struct fs, fs_magic)/4, FS_UFS1_MAGIC, ~0u},
   1436      1.80       dsl 		{offsetof(struct fs, fs_magic)/4, FS_UFS2_MAGIC, ~0u},
   1437      1.80       dsl 		{0, 0x70162, ~0u},		/* LFS_MAGIC */
   1438      1.80       dsl 		{14, 0xef53, 0xffff},		/* EXT2FS (little) */
   1439      1.80       dsl 		{14, 0xef530000, 0xffff0000},	/* EXT2FS (big) */
   1440      1.80       dsl 		{~0u},
   1441      1.80       dsl 	};
   1442      1.80       dsl 	const struct fsm *fsm;
   1443      1.81       dsl 
   1444      1.81       dsl 	if (Nflag)
   1445      1.84     lukem 		return;
   1446      1.84     lukem 
   1447      1.84     lukem 	if (sblkoff == 0)	/* Why did UFS2 add support for this?  sigh. */
   1448      1.81       dsl 		return;
   1449      1.80       dsl 
   1450      1.80       dsl 	if (cg0_data == 0)
   1451      1.80       dsl 		/* For FFSv1 this could include all the inodes. */
   1452      1.80       dsl 		cg0_data = cgsblock(&sblock, 0) * sblock.fs_fsize + iobufsize;
   1453      1.80       dsl 
   1454      1.80       dsl 	/* Ignore anything that is beyond our filesystem */
   1455      1.80       dsl 	if ((sblkoff + SBLOCKSIZE)/sectorsize >= fssize)
   1456      1.80       dsl 		return;
   1457      1.80       dsl 	/* Zero anything inside our filesystem... */
   1458      1.80       dsl 	if (sblkoff >= sblock.fs_sblockloc) {
   1459      1.80       dsl 		/* ...unless we will write that area anyway */
   1460      1.80       dsl 		if (sblkoff >= cg0_data)
   1461      1.82     enami 			wtfs(sblkoff / sectorsize,
   1462      1.82     enami 			    roundup(sizeof sblock, sectorsize), iobuf);
   1463      1.80       dsl 		return;
   1464      1.80       dsl 	}
   1465      1.80       dsl 
   1466      1.80       dsl 	/* The sector might contain boot code, so we must validate it */
   1467      1.80       dsl 	rdfs(sblkoff/sectorsize, sizeof oldfs, &oldfs);
   1468      1.80       dsl 	for (fsm = fs_magics; ; fsm++) {
   1469      1.80       dsl 		uint32_t v;
   1470      1.80       dsl 		if (fsm->mask == 0)
   1471      1.80       dsl 			return;
   1472      1.80       dsl 		v = oldfs[fsm->offset];
   1473      1.80       dsl 		if ((v & fsm->mask) == fsm->magic ||
   1474      1.80       dsl 		    (bswap32(v) & fsm->mask) == fsm->magic)
   1475      1.80       dsl 			break;
   1476      1.80       dsl 	}
   1477      1.80       dsl 
   1478      1.80       dsl 	/* Just zap the magic number */
   1479      1.80       dsl 	oldfs[fsm->offset] = 0;
   1480      1.80       dsl 	wtfs(sblkoff/sectorsize, sizeof oldfs, &oldfs);
   1481      1.68      fvdl }
   1482      1.68      fvdl 
   1483      1.60    simonb 
   1484      1.61     lukem #ifdef MFS
   1485      1.60    simonb /*
   1486      1.60    simonb  * XXX!
   1487      1.60    simonb  * Attempt to guess how much more space is available for process data.  The
   1488      1.60    simonb  * heuristic we use is
   1489      1.60    simonb  *
   1490      1.60    simonb  *	max_data_limit - (sbrk(0) - etext) - 128kB
   1491      1.60    simonb  *
   1492      1.60    simonb  * etext approximates that start address of the data segment, and the 128kB
   1493      1.60    simonb  * allows some slop for both segment gap between text and data, and for other
   1494      1.60    simonb  * (libc) malloc usage.
   1495      1.60    simonb  */
   1496      1.60    simonb static void
   1497      1.60    simonb calc_memfree(void)
   1498      1.60    simonb {
   1499      1.60    simonb 	extern char etext;
   1500      1.60    simonb 	struct rlimit rlp;
   1501      1.60    simonb 	u_long base;
   1502      1.60    simonb 
   1503      1.60    simonb 	base = (u_long)sbrk(0) - (u_long)&etext;
   1504      1.60    simonb 	if (getrlimit(RLIMIT_DATA, &rlp) < 0)
   1505      1.60    simonb 		perror("getrlimit");
   1506      1.60    simonb 	rlp.rlim_cur = rlp.rlim_max;
   1507      1.60    simonb 	if (setrlimit(RLIMIT_DATA, &rlp) < 0)
   1508      1.60    simonb 		perror("setrlimit");
   1509      1.60    simonb 	memleft = rlp.rlim_max - base - (128 * 1024);
   1510      1.60    simonb }
   1511      1.60    simonb 
   1512      1.60    simonb /*
   1513      1.60    simonb  * Internal version of malloc that trims the requested size if not enough
   1514      1.60    simonb  * memory is available.
   1515      1.60    simonb  */
   1516      1.60    simonb static void *
   1517      1.60    simonb mkfs_malloc(size_t size)
   1518      1.60    simonb {
   1519      1.60    simonb 	u_long pgsz;
   1520      1.60    simonb 
   1521      1.60    simonb 	if (size == 0)
   1522      1.60    simonb 		return (NULL);
   1523      1.60    simonb 	if (memleft == 0)
   1524      1.60    simonb 		calc_memfree();
   1525      1.60    simonb 
   1526      1.60    simonb 	pgsz = getpagesize() - 1;
   1527      1.60    simonb 	size = (size + pgsz) &~ pgsz;
   1528      1.60    simonb 	if (size > memleft)
   1529      1.60    simonb 		size = memleft;
   1530      1.60    simonb 	memleft -= size;
   1531      1.60    simonb 	return (mmap(0, size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
   1532      1.60    simonb 	    -1, 0));
   1533       1.1       cgd }
   1534      1.61     lukem #endif	/* MFS */
   1535