xref: /minix/usr.sbin/makefs/ffs/mkfs.c (revision 9f988b79)
1*9f988b79SJean-Baptiste Boric /*	$NetBSD: mkfs.c,v 1.32 2013/10/19 17:16:37 christos Exp $	*/
2*9f988b79SJean-Baptiste Boric 
3*9f988b79SJean-Baptiste Boric /*
4*9f988b79SJean-Baptiste Boric  * Copyright (c) 2002 Networks Associates Technology, Inc.
5*9f988b79SJean-Baptiste Boric  * All rights reserved.
6*9f988b79SJean-Baptiste Boric  *
7*9f988b79SJean-Baptiste Boric  * This software was developed for the FreeBSD Project by Marshall
8*9f988b79SJean-Baptiste Boric  * Kirk McKusick and Network Associates Laboratories, the Security
9*9f988b79SJean-Baptiste Boric  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
10*9f988b79SJean-Baptiste Boric  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
11*9f988b79SJean-Baptiste Boric  * research program
12*9f988b79SJean-Baptiste Boric  *
13*9f988b79SJean-Baptiste Boric  * Copyright (c) 1980, 1989, 1993
14*9f988b79SJean-Baptiste Boric  *	The Regents of the University of California.  All rights reserved.
15*9f988b79SJean-Baptiste Boric  *
16*9f988b79SJean-Baptiste Boric  * Redistribution and use in source and binary forms, with or without
17*9f988b79SJean-Baptiste Boric  * modification, are permitted provided that the following conditions
18*9f988b79SJean-Baptiste Boric  * are met:
19*9f988b79SJean-Baptiste Boric  * 1. Redistributions of source code must retain the above copyright
20*9f988b79SJean-Baptiste Boric  *    notice, this list of conditions and the following disclaimer.
21*9f988b79SJean-Baptiste Boric  * 2. Redistributions in binary form must reproduce the above copyright
22*9f988b79SJean-Baptiste Boric  *    notice, this list of conditions and the following disclaimer in the
23*9f988b79SJean-Baptiste Boric  *    documentation and/or other materials provided with the distribution.
24*9f988b79SJean-Baptiste Boric  * 3. Neither the name of the University nor the names of its contributors
25*9f988b79SJean-Baptiste Boric  *    may be used to endorse or promote products derived from this software
26*9f988b79SJean-Baptiste Boric  *    without specific prior written permission.
27*9f988b79SJean-Baptiste Boric  *
28*9f988b79SJean-Baptiste Boric  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29*9f988b79SJean-Baptiste Boric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30*9f988b79SJean-Baptiste Boric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31*9f988b79SJean-Baptiste Boric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32*9f988b79SJean-Baptiste Boric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33*9f988b79SJean-Baptiste Boric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34*9f988b79SJean-Baptiste Boric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35*9f988b79SJean-Baptiste Boric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36*9f988b79SJean-Baptiste Boric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37*9f988b79SJean-Baptiste Boric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38*9f988b79SJean-Baptiste Boric  * SUCH DAMAGE.
39*9f988b79SJean-Baptiste Boric  */
40*9f988b79SJean-Baptiste Boric 
41*9f988b79SJean-Baptiste Boric #if HAVE_NBTOOL_CONFIG_H
42*9f988b79SJean-Baptiste Boric #include "nbtool_config.h"
43*9f988b79SJean-Baptiste Boric #endif
44*9f988b79SJean-Baptiste Boric 
45*9f988b79SJean-Baptiste Boric #include <sys/cdefs.h>
46*9f988b79SJean-Baptiste Boric #ifndef lint
47*9f988b79SJean-Baptiste Boric #if 0
48*9f988b79SJean-Baptiste Boric static char sccsid[] = "@(#)mkfs.c	8.11 (Berkeley) 5/3/95";
49*9f988b79SJean-Baptiste Boric #else
50*9f988b79SJean-Baptiste Boric #ifdef __RCSID
51*9f988b79SJean-Baptiste Boric __RCSID("$NetBSD: mkfs.c,v 1.32 2013/10/19 17:16:37 christos Exp $");
52*9f988b79SJean-Baptiste Boric #endif
53*9f988b79SJean-Baptiste Boric #endif
54*9f988b79SJean-Baptiste Boric #endif /* not lint */
55*9f988b79SJean-Baptiste Boric 
56*9f988b79SJean-Baptiste Boric #include <sys/param.h>
57*9f988b79SJean-Baptiste Boric #include <sys/time.h>
58*9f988b79SJean-Baptiste Boric #include <sys/resource.h>
59*9f988b79SJean-Baptiste Boric 
60*9f988b79SJean-Baptiste Boric #include <stdio.h>
61*9f988b79SJean-Baptiste Boric #include <stdlib.h>
62*9f988b79SJean-Baptiste Boric #include <string.h>
63*9f988b79SJean-Baptiste Boric #include <unistd.h>
64*9f988b79SJean-Baptiste Boric #include <errno.h>
65*9f988b79SJean-Baptiste Boric #include <util.h>
66*9f988b79SJean-Baptiste Boric 
67*9f988b79SJean-Baptiste Boric #include "makefs.h"
68*9f988b79SJean-Baptiste Boric #include "ffs.h"
69*9f988b79SJean-Baptiste Boric 
70*9f988b79SJean-Baptiste Boric #include <ufs/ufs/dinode.h>
71*9f988b79SJean-Baptiste Boric #include <ufs/ufs/ufs_bswap.h>
72*9f988b79SJean-Baptiste Boric #include <ufs/ffs/fs.h>
73*9f988b79SJean-Baptiste Boric 
74*9f988b79SJean-Baptiste Boric #include "ffs/ufs_inode.h"
75*9f988b79SJean-Baptiste Boric #include "ffs/ffs_extern.h"
76*9f988b79SJean-Baptiste Boric #include "ffs/newfs_extern.h"
77*9f988b79SJean-Baptiste Boric 
78*9f988b79SJean-Baptiste Boric static void initcg(int, time_t, const fsinfo_t *);
79*9f988b79SJean-Baptiste Boric static int ilog2(int);
80*9f988b79SJean-Baptiste Boric 
81*9f988b79SJean-Baptiste Boric static int count_digits(int);
82*9f988b79SJean-Baptiste Boric 
83*9f988b79SJean-Baptiste Boric /*
84*9f988b79SJean-Baptiste Boric  * make file system for cylinder-group style file systems
85*9f988b79SJean-Baptiste Boric  */
86*9f988b79SJean-Baptiste Boric #define	UMASK		0755
87*9f988b79SJean-Baptiste Boric #define	POWEROF2(num)	(((num) & ((num) - 1)) == 0)
88*9f988b79SJean-Baptiste Boric 
89*9f988b79SJean-Baptiste Boric union {
90*9f988b79SJean-Baptiste Boric 	struct fs fs;
91*9f988b79SJean-Baptiste Boric 	char pad[SBLOCKSIZE];
92*9f988b79SJean-Baptiste Boric } fsun;
93*9f988b79SJean-Baptiste Boric #define	sblock	fsun.fs
94*9f988b79SJean-Baptiste Boric struct	csum *fscs;
95*9f988b79SJean-Baptiste Boric 
96*9f988b79SJean-Baptiste Boric union {
97*9f988b79SJean-Baptiste Boric 	struct cg cg;
98*9f988b79SJean-Baptiste Boric 	char pad[FFS_MAXBSIZE];
99*9f988b79SJean-Baptiste Boric } cgun;
100*9f988b79SJean-Baptiste Boric #define	acg	cgun.cg
101*9f988b79SJean-Baptiste Boric 
102*9f988b79SJean-Baptiste Boric char *iobuf;
103*9f988b79SJean-Baptiste Boric int iobufsize;
104*9f988b79SJean-Baptiste Boric 
105*9f988b79SJean-Baptiste Boric char writebuf[FFS_MAXBSIZE];
106*9f988b79SJean-Baptiste Boric 
107*9f988b79SJean-Baptiste Boric static int     Oflag;	   /* format as an 4.3BSD file system */
108*9f988b79SJean-Baptiste Boric static int64_t fssize;	   /* file system size */
109*9f988b79SJean-Baptiste Boric static int     sectorsize;	   /* bytes/sector */
110*9f988b79SJean-Baptiste Boric static int     fsize;	   /* fragment size */
111*9f988b79SJean-Baptiste Boric static int     bsize;	   /* block size */
112*9f988b79SJean-Baptiste Boric static int     maxbsize;   /* maximum clustering */
113*9f988b79SJean-Baptiste Boric static int     maxblkspercg;
114*9f988b79SJean-Baptiste Boric static int     minfree;	   /* free space threshold */
115*9f988b79SJean-Baptiste Boric static int     opt;		   /* optimization preference (space or time) */
116*9f988b79SJean-Baptiste Boric static int     density;	   /* number of bytes per inode */
117*9f988b79SJean-Baptiste Boric static int     maxcontig;	   /* max contiguous blocks to allocate */
118*9f988b79SJean-Baptiste Boric static int     maxbpg;	   /* maximum blocks per file in a cyl group */
119*9f988b79SJean-Baptiste Boric static int     bbsize;	   /* boot block size */
120*9f988b79SJean-Baptiste Boric static int     sbsize;	   /* superblock size */
121*9f988b79SJean-Baptiste Boric static int     avgfilesize;	   /* expected average file size */
122*9f988b79SJean-Baptiste Boric static int     avgfpdir;	   /* expected number of files per directory */
123*9f988b79SJean-Baptiste Boric 
124*9f988b79SJean-Baptiste Boric struct fs *
ffs_mkfs(const char * fsys,const fsinfo_t * fsopts)125*9f988b79SJean-Baptiste Boric ffs_mkfs(const char *fsys, const fsinfo_t *fsopts)
126*9f988b79SJean-Baptiste Boric {
127*9f988b79SJean-Baptiste Boric 	int fragsperinode, optimalfpg, origdensity, minfpg, lastminfpg;
128*9f988b79SJean-Baptiste Boric 	int32_t cylno, i, csfrags;
129*9f988b79SJean-Baptiste Boric 	long long sizepb;
130*9f988b79SJean-Baptiste Boric 	void *space;
131*9f988b79SJean-Baptiste Boric 	int size;
132*9f988b79SJean-Baptiste Boric 	int nprintcols, printcolwidth;
133*9f988b79SJean-Baptiste Boric 	ffs_opt_t	*ffs_opts = fsopts->fs_specific;
134*9f988b79SJean-Baptiste Boric 
135*9f988b79SJean-Baptiste Boric 	Oflag =		ffs_opts->version;
136*9f988b79SJean-Baptiste Boric 	fssize =        fsopts->size / fsopts->sectorsize;
137*9f988b79SJean-Baptiste Boric 	sectorsize =    fsopts->sectorsize;
138*9f988b79SJean-Baptiste Boric 	fsize =         ffs_opts->fsize;
139*9f988b79SJean-Baptiste Boric 	bsize =         ffs_opts->bsize;
140*9f988b79SJean-Baptiste Boric 	maxbsize =      ffs_opts->maxbsize;
141*9f988b79SJean-Baptiste Boric 	maxblkspercg =  ffs_opts->maxblkspercg;
142*9f988b79SJean-Baptiste Boric 	minfree =       ffs_opts->minfree;
143*9f988b79SJean-Baptiste Boric 	opt =           ffs_opts->optimization;
144*9f988b79SJean-Baptiste Boric 	density =       ffs_opts->density;
145*9f988b79SJean-Baptiste Boric 	maxcontig =     ffs_opts->maxcontig;
146*9f988b79SJean-Baptiste Boric 	maxbpg =        ffs_opts->maxbpg;
147*9f988b79SJean-Baptiste Boric 	avgfilesize =   ffs_opts->avgfilesize;
148*9f988b79SJean-Baptiste Boric 	avgfpdir =      ffs_opts->avgfpdir;
149*9f988b79SJean-Baptiste Boric 	bbsize =        BBSIZE;
150*9f988b79SJean-Baptiste Boric 	sbsize =        SBLOCKSIZE;
151*9f988b79SJean-Baptiste Boric 
152*9f988b79SJean-Baptiste Boric 	strlcpy((char *)sblock.fs_volname, ffs_opts->label,
153*9f988b79SJean-Baptiste Boric 	    sizeof(sblock.fs_volname));
154*9f988b79SJean-Baptiste Boric 
155*9f988b79SJean-Baptiste Boric 	if (Oflag == 0) {
156*9f988b79SJean-Baptiste Boric 		sblock.fs_old_inodefmt = FS_42INODEFMT;
157*9f988b79SJean-Baptiste Boric 		sblock.fs_maxsymlinklen = 0;
158*9f988b79SJean-Baptiste Boric 		sblock.fs_old_flags = 0;
159*9f988b79SJean-Baptiste Boric 	} else {
160*9f988b79SJean-Baptiste Boric 		sblock.fs_old_inodefmt = FS_44INODEFMT;
161*9f988b79SJean-Baptiste Boric 		sblock.fs_maxsymlinklen = (Oflag == 1 ? UFS1_MAXSYMLINKLEN :
162*9f988b79SJean-Baptiste Boric 		    UFS2_MAXSYMLINKLEN);
163*9f988b79SJean-Baptiste Boric 		sblock.fs_old_flags = FS_FLAGS_UPDATED;
164*9f988b79SJean-Baptiste Boric 		sblock.fs_flags = 0;
165*9f988b79SJean-Baptiste Boric 	}
166*9f988b79SJean-Baptiste Boric 	/*
167*9f988b79SJean-Baptiste Boric 	 * Validate the given file system size.
168*9f988b79SJean-Baptiste Boric 	 * Verify that its last block can actually be accessed.
169*9f988b79SJean-Baptiste Boric 	 * Convert to file system fragment sized units.
170*9f988b79SJean-Baptiste Boric 	 */
171*9f988b79SJean-Baptiste Boric 	if (fssize <= 0) {
172*9f988b79SJean-Baptiste Boric 		printf("preposterous size %lld\n", (long long)fssize);
173*9f988b79SJean-Baptiste Boric 		exit(13);
174*9f988b79SJean-Baptiste Boric 	}
175*9f988b79SJean-Baptiste Boric 	ffs_wtfs(fssize - 1, sectorsize, (char *)&sblock, fsopts);
176*9f988b79SJean-Baptiste Boric 
177*9f988b79SJean-Baptiste Boric 	/*
178*9f988b79SJean-Baptiste Boric 	 * collect and verify the filesystem density info
179*9f988b79SJean-Baptiste Boric 	 */
180*9f988b79SJean-Baptiste Boric 	sblock.fs_avgfilesize = avgfilesize;
181*9f988b79SJean-Baptiste Boric 	sblock.fs_avgfpdir = avgfpdir;
182*9f988b79SJean-Baptiste Boric 	if (sblock.fs_avgfilesize <= 0)
183*9f988b79SJean-Baptiste Boric 		printf("illegal expected average file size %d\n",
184*9f988b79SJean-Baptiste Boric 		    sblock.fs_avgfilesize), exit(14);
185*9f988b79SJean-Baptiste Boric 	if (sblock.fs_avgfpdir <= 0)
186*9f988b79SJean-Baptiste Boric 		printf("illegal expected number of files per directory %d\n",
187*9f988b79SJean-Baptiste Boric 		    sblock.fs_avgfpdir), exit(15);
188*9f988b79SJean-Baptiste Boric 	/*
189*9f988b79SJean-Baptiste Boric 	 * collect and verify the block and fragment sizes
190*9f988b79SJean-Baptiste Boric 	 */
191*9f988b79SJean-Baptiste Boric 	sblock.fs_bsize = bsize;
192*9f988b79SJean-Baptiste Boric 	sblock.fs_fsize = fsize;
193*9f988b79SJean-Baptiste Boric 	if (!POWEROF2(sblock.fs_bsize)) {
194*9f988b79SJean-Baptiste Boric 		printf("block size must be a power of 2, not %d\n",
195*9f988b79SJean-Baptiste Boric 		    sblock.fs_bsize);
196*9f988b79SJean-Baptiste Boric 		exit(16);
197*9f988b79SJean-Baptiste Boric 	}
198*9f988b79SJean-Baptiste Boric 	if (!POWEROF2(sblock.fs_fsize)) {
199*9f988b79SJean-Baptiste Boric 		printf("fragment size must be a power of 2, not %d\n",
200*9f988b79SJean-Baptiste Boric 		    sblock.fs_fsize);
201*9f988b79SJean-Baptiste Boric 		exit(17);
202*9f988b79SJean-Baptiste Boric 	}
203*9f988b79SJean-Baptiste Boric 	if (sblock.fs_fsize < sectorsize) {
204*9f988b79SJean-Baptiste Boric 		printf("fragment size %d is too small, minimum is %d\n",
205*9f988b79SJean-Baptiste Boric 		    sblock.fs_fsize, sectorsize);
206*9f988b79SJean-Baptiste Boric 		exit(18);
207*9f988b79SJean-Baptiste Boric 	}
208*9f988b79SJean-Baptiste Boric 	if (sblock.fs_bsize < MINBSIZE) {
209*9f988b79SJean-Baptiste Boric 		printf("block size %d is too small, minimum is %d\n",
210*9f988b79SJean-Baptiste Boric 		    sblock.fs_bsize, MINBSIZE);
211*9f988b79SJean-Baptiste Boric 		exit(19);
212*9f988b79SJean-Baptiste Boric 	}
213*9f988b79SJean-Baptiste Boric 	if (sblock.fs_bsize > FFS_MAXBSIZE) {
214*9f988b79SJean-Baptiste Boric 		printf("block size %d is too large, maximum is %d\n",
215*9f988b79SJean-Baptiste Boric 		    sblock.fs_bsize, FFS_MAXBSIZE);
216*9f988b79SJean-Baptiste Boric 		exit(19);
217*9f988b79SJean-Baptiste Boric 	}
218*9f988b79SJean-Baptiste Boric 	if (sblock.fs_bsize < sblock.fs_fsize) {
219*9f988b79SJean-Baptiste Boric 		printf("block size (%d) cannot be smaller than fragment size (%d)\n",
220*9f988b79SJean-Baptiste Boric 		    sblock.fs_bsize, sblock.fs_fsize);
221*9f988b79SJean-Baptiste Boric 		exit(20);
222*9f988b79SJean-Baptiste Boric 	}
223*9f988b79SJean-Baptiste Boric 
224*9f988b79SJean-Baptiste Boric 	if (maxbsize < bsize || !POWEROF2(maxbsize)) {
225*9f988b79SJean-Baptiste Boric 		sblock.fs_maxbsize = sblock.fs_bsize;
226*9f988b79SJean-Baptiste Boric 		printf("Extent size set to %d\n", sblock.fs_maxbsize);
227*9f988b79SJean-Baptiste Boric 	} else if (sblock.fs_maxbsize > FS_MAXCONTIG * sblock.fs_bsize) {
228*9f988b79SJean-Baptiste Boric 		sblock.fs_maxbsize = FS_MAXCONTIG * sblock.fs_bsize;
229*9f988b79SJean-Baptiste Boric 		printf("Extent size reduced to %d\n", sblock.fs_maxbsize);
230*9f988b79SJean-Baptiste Boric 	} else {
231*9f988b79SJean-Baptiste Boric 		sblock.fs_maxbsize = maxbsize;
232*9f988b79SJean-Baptiste Boric 	}
233*9f988b79SJean-Baptiste Boric 	sblock.fs_maxcontig = maxcontig;
234*9f988b79SJean-Baptiste Boric 	if (sblock.fs_maxcontig < sblock.fs_maxbsize / sblock.fs_bsize) {
235*9f988b79SJean-Baptiste Boric 		sblock.fs_maxcontig = sblock.fs_maxbsize / sblock.fs_bsize;
236*9f988b79SJean-Baptiste Boric 		printf("Maxcontig raised to %d\n", sblock.fs_maxbsize);
237*9f988b79SJean-Baptiste Boric 	}
238*9f988b79SJean-Baptiste Boric 
239*9f988b79SJean-Baptiste Boric 	if (sblock.fs_maxcontig > 1)
240*9f988b79SJean-Baptiste Boric 		sblock.fs_contigsumsize = MIN(sblock.fs_maxcontig,FS_MAXCONTIG);
241*9f988b79SJean-Baptiste Boric 
242*9f988b79SJean-Baptiste Boric 	sblock.fs_bmask = ~(sblock.fs_bsize - 1);
243*9f988b79SJean-Baptiste Boric 	sblock.fs_fmask = ~(sblock.fs_fsize - 1);
244*9f988b79SJean-Baptiste Boric 	sblock.fs_qbmask = ~sblock.fs_bmask;
245*9f988b79SJean-Baptiste Boric 	sblock.fs_qfmask = ~sblock.fs_fmask;
246*9f988b79SJean-Baptiste Boric 	for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1)
247*9f988b79SJean-Baptiste Boric 		sblock.fs_bshift++;
248*9f988b79SJean-Baptiste Boric 	for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1)
249*9f988b79SJean-Baptiste Boric 		sblock.fs_fshift++;
250*9f988b79SJean-Baptiste Boric 	sblock.fs_frag = ffs_numfrags(&sblock, sblock.fs_bsize);
251*9f988b79SJean-Baptiste Boric 	for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1)
252*9f988b79SJean-Baptiste Boric 		sblock.fs_fragshift++;
253*9f988b79SJean-Baptiste Boric 	if (sblock.fs_frag > MAXFRAG) {
254*9f988b79SJean-Baptiste Boric 		printf("fragment size %d is too small, "
255*9f988b79SJean-Baptiste Boric 			"minimum with block size %d is %d\n",
256*9f988b79SJean-Baptiste Boric 		    sblock.fs_fsize, sblock.fs_bsize,
257*9f988b79SJean-Baptiste Boric 		    sblock.fs_bsize / MAXFRAG);
258*9f988b79SJean-Baptiste Boric 		exit(21);
259*9f988b79SJean-Baptiste Boric 	}
260*9f988b79SJean-Baptiste Boric 	sblock.fs_fsbtodb = ilog2(sblock.fs_fsize / sectorsize);
261*9f988b79SJean-Baptiste Boric 	sblock.fs_size = fssize = FFS_DBTOFSB(&sblock, fssize);
262*9f988b79SJean-Baptiste Boric 
263*9f988b79SJean-Baptiste Boric 	if (Oflag <= 1) {
264*9f988b79SJean-Baptiste Boric 		sblock.fs_magic = FS_UFS1_MAGIC;
265*9f988b79SJean-Baptiste Boric 		sblock.fs_sblockloc = SBLOCK_UFS1;
266*9f988b79SJean-Baptiste Boric 		sblock.fs_nindir = sblock.fs_bsize / sizeof(int32_t);
267*9f988b79SJean-Baptiste Boric 		sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs1_dinode);
268*9f988b79SJean-Baptiste Boric 		sblock.fs_maxsymlinklen = ((UFS_NDADDR + UFS_NIADDR) *
269*9f988b79SJean-Baptiste Boric 		    sizeof (int32_t));
270*9f988b79SJean-Baptiste Boric 		sblock.fs_old_inodefmt = FS_44INODEFMT;
271*9f988b79SJean-Baptiste Boric 		sblock.fs_old_cgoffset = 0;
272*9f988b79SJean-Baptiste Boric 		sblock.fs_old_cgmask = 0xffffffff;
273*9f988b79SJean-Baptiste Boric 		sblock.fs_old_size = sblock.fs_size;
274*9f988b79SJean-Baptiste Boric 		sblock.fs_old_rotdelay = 0;
275*9f988b79SJean-Baptiste Boric 		sblock.fs_old_rps = 60;
276*9f988b79SJean-Baptiste Boric 		sblock.fs_old_nspf = sblock.fs_fsize / sectorsize;
277*9f988b79SJean-Baptiste Boric 		sblock.fs_old_cpg = 1;
278*9f988b79SJean-Baptiste Boric 		sblock.fs_old_interleave = 1;
279*9f988b79SJean-Baptiste Boric 		sblock.fs_old_trackskew = 0;
280*9f988b79SJean-Baptiste Boric 		sblock.fs_old_cpc = 0;
281*9f988b79SJean-Baptiste Boric 		sblock.fs_old_postblformat = 1;
282*9f988b79SJean-Baptiste Boric 		sblock.fs_old_nrpos = 1;
283*9f988b79SJean-Baptiste Boric 	} else {
284*9f988b79SJean-Baptiste Boric 		sblock.fs_magic = FS_UFS2_MAGIC;
285*9f988b79SJean-Baptiste Boric #if 0 /* XXX makefs is used for small filesystems. */
286*9f988b79SJean-Baptiste Boric 		sblock.fs_sblockloc = SBLOCK_UFS2;
287*9f988b79SJean-Baptiste Boric #else
288*9f988b79SJean-Baptiste Boric 		sblock.fs_sblockloc = SBLOCK_UFS1;
289*9f988b79SJean-Baptiste Boric #endif
290*9f988b79SJean-Baptiste Boric 		sblock.fs_nindir = sblock.fs_bsize / sizeof(int64_t);
291*9f988b79SJean-Baptiste Boric 		sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs2_dinode);
292*9f988b79SJean-Baptiste Boric 		sblock.fs_maxsymlinklen = ((UFS_NDADDR + UFS_NIADDR) *
293*9f988b79SJean-Baptiste Boric 		    sizeof (int64_t));
294*9f988b79SJean-Baptiste Boric 	}
295*9f988b79SJean-Baptiste Boric 
296*9f988b79SJean-Baptiste Boric 	sblock.fs_sblkno =
297*9f988b79SJean-Baptiste Boric 	    roundup(howmany(sblock.fs_sblockloc + SBLOCKSIZE, sblock.fs_fsize),
298*9f988b79SJean-Baptiste Boric 		sblock.fs_frag);
299*9f988b79SJean-Baptiste Boric 	sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno +
300*9f988b79SJean-Baptiste Boric 	    roundup(howmany(SBLOCKSIZE, sblock.fs_fsize), sblock.fs_frag));
301*9f988b79SJean-Baptiste Boric 	sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag;
302*9f988b79SJean-Baptiste Boric 	sblock.fs_maxfilesize = sblock.fs_bsize * UFS_NDADDR - 1;
303*9f988b79SJean-Baptiste Boric 	for (sizepb = sblock.fs_bsize, i = 0; i < UFS_NIADDR; i++) {
304*9f988b79SJean-Baptiste Boric 		sizepb *= FFS_NINDIR(&sblock);
305*9f988b79SJean-Baptiste Boric 		sblock.fs_maxfilesize += sizepb;
306*9f988b79SJean-Baptiste Boric 	}
307*9f988b79SJean-Baptiste Boric 
308*9f988b79SJean-Baptiste Boric 	/*
309*9f988b79SJean-Baptiste Boric 	 * Calculate the number of blocks to put into each cylinder group.
310*9f988b79SJean-Baptiste Boric 	 *
311*9f988b79SJean-Baptiste Boric 	 * This algorithm selects the number of blocks per cylinder
312*9f988b79SJean-Baptiste Boric 	 * group. The first goal is to have at least enough data blocks
313*9f988b79SJean-Baptiste Boric 	 * in each cylinder group to meet the density requirement. Once
314*9f988b79SJean-Baptiste Boric 	 * this goal is achieved we try to expand to have at least
315*9f988b79SJean-Baptiste Boric 	 * 1 cylinder group. Once this goal is achieved, we pack as
316*9f988b79SJean-Baptiste Boric 	 * many blocks into each cylinder group map as will fit.
317*9f988b79SJean-Baptiste Boric 	 *
318*9f988b79SJean-Baptiste Boric 	 * We start by calculating the smallest number of blocks that we
319*9f988b79SJean-Baptiste Boric 	 * can put into each cylinder group. If this is too big, we reduce
320*9f988b79SJean-Baptiste Boric 	 * the density until it fits.
321*9f988b79SJean-Baptiste Boric 	 */
322*9f988b79SJean-Baptiste Boric 	origdensity = density;
323*9f988b79SJean-Baptiste Boric 	for (;;) {
324*9f988b79SJean-Baptiste Boric 		fragsperinode = MAX(ffs_numfrags(&sblock, density), 1);
325*9f988b79SJean-Baptiste Boric 		minfpg = fragsperinode * FFS_INOPB(&sblock);
326*9f988b79SJean-Baptiste Boric 		if (minfpg > sblock.fs_size)
327*9f988b79SJean-Baptiste Boric 			minfpg = sblock.fs_size;
328*9f988b79SJean-Baptiste Boric 		sblock.fs_ipg = FFS_INOPB(&sblock);
329*9f988b79SJean-Baptiste Boric 		sblock.fs_fpg = roundup(sblock.fs_iblkno +
330*9f988b79SJean-Baptiste Boric 		    sblock.fs_ipg / FFS_INOPF(&sblock), sblock.fs_frag);
331*9f988b79SJean-Baptiste Boric 		if (sblock.fs_fpg < minfpg)
332*9f988b79SJean-Baptiste Boric 			sblock.fs_fpg = minfpg;
333*9f988b79SJean-Baptiste Boric 		sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode),
334*9f988b79SJean-Baptiste Boric 		    FFS_INOPB(&sblock));
335*9f988b79SJean-Baptiste Boric 		sblock.fs_fpg = roundup(sblock.fs_iblkno +
336*9f988b79SJean-Baptiste Boric 		    sblock.fs_ipg / FFS_INOPF(&sblock), sblock.fs_frag);
337*9f988b79SJean-Baptiste Boric 		if (sblock.fs_fpg < minfpg)
338*9f988b79SJean-Baptiste Boric 			sblock.fs_fpg = minfpg;
339*9f988b79SJean-Baptiste Boric 		sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode),
340*9f988b79SJean-Baptiste Boric 		    FFS_INOPB(&sblock));
341*9f988b79SJean-Baptiste Boric 		if (CGSIZE(&sblock) < (unsigned long)sblock.fs_bsize)
342*9f988b79SJean-Baptiste Boric 			break;
343*9f988b79SJean-Baptiste Boric 		density -= sblock.fs_fsize;
344*9f988b79SJean-Baptiste Boric 	}
345*9f988b79SJean-Baptiste Boric 	if (density != origdensity)
346*9f988b79SJean-Baptiste Boric 		printf("density reduced from %d to %d\n", origdensity, density);
347*9f988b79SJean-Baptiste Boric 
348*9f988b79SJean-Baptiste Boric 	if (maxblkspercg <= 0 || maxblkspercg >= fssize)
349*9f988b79SJean-Baptiste Boric 		maxblkspercg = fssize - 1;
350*9f988b79SJean-Baptiste Boric 	/*
351*9f988b79SJean-Baptiste Boric 	 * Start packing more blocks into the cylinder group until
352*9f988b79SJean-Baptiste Boric 	 * it cannot grow any larger, the number of cylinder groups
353*9f988b79SJean-Baptiste Boric 	 * drops below 1, or we reach the size requested.
354*9f988b79SJean-Baptiste Boric 	 */
355*9f988b79SJean-Baptiste Boric 	for ( ; sblock.fs_fpg < maxblkspercg; sblock.fs_fpg += sblock.fs_frag) {
356*9f988b79SJean-Baptiste Boric 		sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode),
357*9f988b79SJean-Baptiste Boric 		    FFS_INOPB(&sblock));
358*9f988b79SJean-Baptiste Boric 		if (sblock.fs_size / sblock.fs_fpg < 1)
359*9f988b79SJean-Baptiste Boric 			break;
360*9f988b79SJean-Baptiste Boric 		if (CGSIZE(&sblock) < (unsigned long)sblock.fs_bsize)
361*9f988b79SJean-Baptiste Boric 			continue;
362*9f988b79SJean-Baptiste Boric 		if (CGSIZE(&sblock) == (unsigned long)sblock.fs_bsize)
363*9f988b79SJean-Baptiste Boric 			break;
364*9f988b79SJean-Baptiste Boric 		sblock.fs_fpg -= sblock.fs_frag;
365*9f988b79SJean-Baptiste Boric 		sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode),
366*9f988b79SJean-Baptiste Boric 		    FFS_INOPB(&sblock));
367*9f988b79SJean-Baptiste Boric 		break;
368*9f988b79SJean-Baptiste Boric 	}
369*9f988b79SJean-Baptiste Boric 	/*
370*9f988b79SJean-Baptiste Boric 	 * Check to be sure that the last cylinder group has enough blocks
371*9f988b79SJean-Baptiste Boric 	 * to be viable. If it is too small, reduce the number of blocks
372*9f988b79SJean-Baptiste Boric 	 * per cylinder group which will have the effect of moving more
373*9f988b79SJean-Baptiste Boric 	 * blocks into the last cylinder group.
374*9f988b79SJean-Baptiste Boric 	 */
375*9f988b79SJean-Baptiste Boric 	optimalfpg = sblock.fs_fpg;
376*9f988b79SJean-Baptiste Boric 	for (;;) {
377*9f988b79SJean-Baptiste Boric 		sblock.fs_ncg = howmany(sblock.fs_size, sblock.fs_fpg);
378*9f988b79SJean-Baptiste Boric 		lastminfpg = roundup(sblock.fs_iblkno +
379*9f988b79SJean-Baptiste Boric 		    sblock.fs_ipg / FFS_INOPF(&sblock), sblock.fs_frag);
380*9f988b79SJean-Baptiste Boric 		if (sblock.fs_size < lastminfpg) {
381*9f988b79SJean-Baptiste Boric 			printf("Filesystem size %lld < minimum size of %d\n",
382*9f988b79SJean-Baptiste Boric 			    (long long)sblock.fs_size, lastminfpg);
383*9f988b79SJean-Baptiste Boric 			exit(28);
384*9f988b79SJean-Baptiste Boric 		}
385*9f988b79SJean-Baptiste Boric 		if (sblock.fs_size % sblock.fs_fpg >= lastminfpg ||
386*9f988b79SJean-Baptiste Boric 		    sblock.fs_size % sblock.fs_fpg == 0)
387*9f988b79SJean-Baptiste Boric 			break;
388*9f988b79SJean-Baptiste Boric 		sblock.fs_fpg -= sblock.fs_frag;
389*9f988b79SJean-Baptiste Boric 		sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode),
390*9f988b79SJean-Baptiste Boric 		    FFS_INOPB(&sblock));
391*9f988b79SJean-Baptiste Boric 	}
392*9f988b79SJean-Baptiste Boric 	if (optimalfpg != sblock.fs_fpg)
393*9f988b79SJean-Baptiste Boric 		printf("Reduced frags per cylinder group from %d to %d %s\n",
394*9f988b79SJean-Baptiste Boric 		   optimalfpg, sblock.fs_fpg, "to enlarge last cyl group");
395*9f988b79SJean-Baptiste Boric 	sblock.fs_cgsize = ffs_fragroundup(&sblock, CGSIZE(&sblock));
396*9f988b79SJean-Baptiste Boric 	sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / FFS_INOPF(&sblock);
397*9f988b79SJean-Baptiste Boric 	if (Oflag <= 1) {
398*9f988b79SJean-Baptiste Boric 		sblock.fs_old_spc = sblock.fs_fpg * sblock.fs_old_nspf;
399*9f988b79SJean-Baptiste Boric 		sblock.fs_old_nsect = sblock.fs_old_spc;
400*9f988b79SJean-Baptiste Boric 		sblock.fs_old_npsect = sblock.fs_old_spc;
401*9f988b79SJean-Baptiste Boric 		sblock.fs_old_ncyl = sblock.fs_ncg;
402*9f988b79SJean-Baptiste Boric 	}
403*9f988b79SJean-Baptiste Boric 
404*9f988b79SJean-Baptiste Boric 	/*
405*9f988b79SJean-Baptiste Boric 	 * fill in remaining fields of the super block
406*9f988b79SJean-Baptiste Boric 	 */
407*9f988b79SJean-Baptiste Boric 	sblock.fs_csaddr = cgdmin(&sblock, 0);
408*9f988b79SJean-Baptiste Boric 	sblock.fs_cssize =
409*9f988b79SJean-Baptiste Boric 	    ffs_fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
410*9f988b79SJean-Baptiste Boric 
411*9f988b79SJean-Baptiste Boric 	/*
412*9f988b79SJean-Baptiste Boric 	 * Setup memory for temporary in-core cylgroup summaries.
413*9f988b79SJean-Baptiste Boric 	 * Cribbed from ffs_mountfs().
414*9f988b79SJean-Baptiste Boric 	 */
415*9f988b79SJean-Baptiste Boric 	size = sblock.fs_cssize;
416*9f988b79SJean-Baptiste Boric 	if (sblock.fs_contigsumsize > 0)
417*9f988b79SJean-Baptiste Boric 		size += sblock.fs_ncg * sizeof(int32_t);
418*9f988b79SJean-Baptiste Boric 	space = ecalloc(1, size);
419*9f988b79SJean-Baptiste Boric 	sblock.fs_csp = space;
420*9f988b79SJean-Baptiste Boric 	space = (char *)space + sblock.fs_cssize;
421*9f988b79SJean-Baptiste Boric 	if (sblock.fs_contigsumsize > 0) {
422*9f988b79SJean-Baptiste Boric 		int32_t *lp;
423*9f988b79SJean-Baptiste Boric 
424*9f988b79SJean-Baptiste Boric 		sblock.fs_maxcluster = lp = space;
425*9f988b79SJean-Baptiste Boric 		for (i = 0; i < sblock.fs_ncg; i++)
426*9f988b79SJean-Baptiste Boric 		*lp++ = sblock.fs_contigsumsize;
427*9f988b79SJean-Baptiste Boric 	}
428*9f988b79SJean-Baptiste Boric 
429*9f988b79SJean-Baptiste Boric 	sblock.fs_sbsize = ffs_fragroundup(&sblock, sizeof(struct fs));
430*9f988b79SJean-Baptiste Boric 	if (sblock.fs_sbsize > SBLOCKSIZE)
431*9f988b79SJean-Baptiste Boric 		sblock.fs_sbsize = SBLOCKSIZE;
432*9f988b79SJean-Baptiste Boric 	sblock.fs_minfree = minfree;
433*9f988b79SJean-Baptiste Boric 	sblock.fs_maxcontig = maxcontig;
434*9f988b79SJean-Baptiste Boric 	sblock.fs_maxbpg = maxbpg;
435*9f988b79SJean-Baptiste Boric 	sblock.fs_optim = opt;
436*9f988b79SJean-Baptiste Boric 	sblock.fs_cgrotor = 0;
437*9f988b79SJean-Baptiste Boric 	sblock.fs_pendingblocks = 0;
438*9f988b79SJean-Baptiste Boric 	sblock.fs_pendinginodes = 0;
439*9f988b79SJean-Baptiste Boric 	sblock.fs_cstotal.cs_ndir = 0;
440*9f988b79SJean-Baptiste Boric 	sblock.fs_cstotal.cs_nbfree = 0;
441*9f988b79SJean-Baptiste Boric 	sblock.fs_cstotal.cs_nifree = 0;
442*9f988b79SJean-Baptiste Boric 	sblock.fs_cstotal.cs_nffree = 0;
443*9f988b79SJean-Baptiste Boric 	sblock.fs_fmod = 0;
444*9f988b79SJean-Baptiste Boric 	sblock.fs_ronly = 0;
445*9f988b79SJean-Baptiste Boric 	sblock.fs_state = 0;
446*9f988b79SJean-Baptiste Boric 	sblock.fs_clean = FS_ISCLEAN;
447*9f988b79SJean-Baptiste Boric 	sblock.fs_ronly = 0;
448*9f988b79SJean-Baptiste Boric 	sblock.fs_id[0] = start_time.tv_sec;
449*9f988b79SJean-Baptiste Boric 	sblock.fs_id[1] = random();
450*9f988b79SJean-Baptiste Boric 	sblock.fs_fsmnt[0] = '\0';
451*9f988b79SJean-Baptiste Boric 	csfrags = howmany(sblock.fs_cssize, sblock.fs_fsize);
452*9f988b79SJean-Baptiste Boric 	sblock.fs_dsize = sblock.fs_size - sblock.fs_sblkno -
453*9f988b79SJean-Baptiste Boric 	    sblock.fs_ncg * (sblock.fs_dblkno - sblock.fs_sblkno);
454*9f988b79SJean-Baptiste Boric 	sblock.fs_cstotal.cs_nbfree =
455*9f988b79SJean-Baptiste Boric 	    ffs_fragstoblks(&sblock, sblock.fs_dsize) -
456*9f988b79SJean-Baptiste Boric 	    howmany(csfrags, sblock.fs_frag);
457*9f988b79SJean-Baptiste Boric 	sblock.fs_cstotal.cs_nffree =
458*9f988b79SJean-Baptiste Boric 	    ffs_fragnum(&sblock, sblock.fs_size) +
459*9f988b79SJean-Baptiste Boric 	    (ffs_fragnum(&sblock, csfrags) > 0 ?
460*9f988b79SJean-Baptiste Boric 	    sblock.fs_frag - ffs_fragnum(&sblock, csfrags) : 0);
461*9f988b79SJean-Baptiste Boric 	sblock.fs_cstotal.cs_nifree = sblock.fs_ncg * sblock.fs_ipg - UFS_ROOTINO;
462*9f988b79SJean-Baptiste Boric 	sblock.fs_cstotal.cs_ndir = 0;
463*9f988b79SJean-Baptiste Boric 	sblock.fs_dsize -= csfrags;
464*9f988b79SJean-Baptiste Boric 	sblock.fs_time = start_time.tv_sec;
465*9f988b79SJean-Baptiste Boric 	if (Oflag <= 1) {
466*9f988b79SJean-Baptiste Boric 		sblock.fs_old_time = start_time.tv_sec;
467*9f988b79SJean-Baptiste Boric 		sblock.fs_old_dsize = sblock.fs_dsize;
468*9f988b79SJean-Baptiste Boric 		sblock.fs_old_csaddr = sblock.fs_csaddr;
469*9f988b79SJean-Baptiste Boric 		sblock.fs_old_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir;
470*9f988b79SJean-Baptiste Boric 		sblock.fs_old_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree;
471*9f988b79SJean-Baptiste Boric 		sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree;
472*9f988b79SJean-Baptiste Boric 		sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree;
473*9f988b79SJean-Baptiste Boric 	}
474*9f988b79SJean-Baptiste Boric 	/*
475*9f988b79SJean-Baptiste Boric 	 * Dump out summary information about file system.
476*9f988b79SJean-Baptiste Boric 	 */
477*9f988b79SJean-Baptiste Boric #define	B2MBFACTOR (1 / (1024.0 * 1024.0))
478*9f988b79SJean-Baptiste Boric 	printf("%s: %.1fMB (%lld sectors) block size %d, "
479*9f988b79SJean-Baptiste Boric 	       "fragment size %d\n",
480*9f988b79SJean-Baptiste Boric 	    fsys, (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
481*9f988b79SJean-Baptiste Boric 	    (long long)FFS_FSBTODB(&sblock, sblock.fs_size),
482*9f988b79SJean-Baptiste Boric 	    sblock.fs_bsize, sblock.fs_fsize);
483*9f988b79SJean-Baptiste Boric 	printf("\tusing %d cylinder groups of %.2fMB, %d blks, "
484*9f988b79SJean-Baptiste Boric 	       "%d inodes.\n",
485*9f988b79SJean-Baptiste Boric 	    sblock.fs_ncg,
486*9f988b79SJean-Baptiste Boric 	    (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
487*9f988b79SJean-Baptiste Boric 	    sblock.fs_fpg / sblock.fs_frag, sblock.fs_ipg);
488*9f988b79SJean-Baptiste Boric #undef B2MBFACTOR
489*9f988b79SJean-Baptiste Boric 	/*
490*9f988b79SJean-Baptiste Boric 	 * Now determine how wide each column will be, and calculate how
491*9f988b79SJean-Baptiste Boric 	 * many columns will fit in a 76 char line. 76 is the width of the
492*9f988b79SJean-Baptiste Boric 	 * subwindows in sysinst.
493*9f988b79SJean-Baptiste Boric 	 */
494*9f988b79SJean-Baptiste Boric 	printcolwidth = count_digits(
495*9f988b79SJean-Baptiste Boric 			FFS_FSBTODB(&sblock, cgsblock(&sblock, sblock.fs_ncg -1)));
496*9f988b79SJean-Baptiste Boric 	nprintcols = 76 / (printcolwidth + 2);
497*9f988b79SJean-Baptiste Boric 
498*9f988b79SJean-Baptiste Boric 	/*
499*9f988b79SJean-Baptiste Boric 	 * allocate space for superblock, cylinder group map, and
500*9f988b79SJean-Baptiste Boric 	 * two sets of inode blocks.
501*9f988b79SJean-Baptiste Boric 	 */
502*9f988b79SJean-Baptiste Boric 	if (sblock.fs_bsize < SBLOCKSIZE)
503*9f988b79SJean-Baptiste Boric 		iobufsize = SBLOCKSIZE + 3 * sblock.fs_bsize;
504*9f988b79SJean-Baptiste Boric 	else
505*9f988b79SJean-Baptiste Boric 		iobufsize = 4 * sblock.fs_bsize;
506*9f988b79SJean-Baptiste Boric 	iobuf = ecalloc(1, iobufsize);
507*9f988b79SJean-Baptiste Boric 	/*
508*9f988b79SJean-Baptiste Boric 	 * Make a copy of the superblock into the buffer that we will be
509*9f988b79SJean-Baptiste Boric 	 * writing out in each cylinder group.
510*9f988b79SJean-Baptiste Boric 	 */
511*9f988b79SJean-Baptiste Boric 	memcpy(writebuf, &sblock, sbsize);
512*9f988b79SJean-Baptiste Boric 	if (fsopts->needswap)
513*9f988b79SJean-Baptiste Boric 		ffs_sb_swap(&sblock, (struct fs*)writebuf);
514*9f988b79SJean-Baptiste Boric 	memcpy(iobuf, writebuf, SBLOCKSIZE);
515*9f988b79SJean-Baptiste Boric 
516*9f988b79SJean-Baptiste Boric 	printf("super-block backups (for fsck -b #) at:");
517*9f988b79SJean-Baptiste Boric 	for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
518*9f988b79SJean-Baptiste Boric 		initcg(cylno, start_time.tv_sec, fsopts);
519*9f988b79SJean-Baptiste Boric 		if (cylno % nprintcols == 0)
520*9f988b79SJean-Baptiste Boric 			printf("\n");
521*9f988b79SJean-Baptiste Boric 		printf(" %*lld,", printcolwidth,
522*9f988b79SJean-Baptiste Boric 			(long long)FFS_FSBTODB(&sblock, cgsblock(&sblock, cylno)));
523*9f988b79SJean-Baptiste Boric 		fflush(stdout);
524*9f988b79SJean-Baptiste Boric 	}
525*9f988b79SJean-Baptiste Boric 	printf("\n");
526*9f988b79SJean-Baptiste Boric 
527*9f988b79SJean-Baptiste Boric 	/*
528*9f988b79SJean-Baptiste Boric 	 * Now construct the initial file system,
529*9f988b79SJean-Baptiste Boric 	 * then write out the super-block.
530*9f988b79SJean-Baptiste Boric 	 */
531*9f988b79SJean-Baptiste Boric 	sblock.fs_time = start_time.tv_sec;
532*9f988b79SJean-Baptiste Boric 	if (Oflag <= 1) {
533*9f988b79SJean-Baptiste Boric 		sblock.fs_old_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir;
534*9f988b79SJean-Baptiste Boric 		sblock.fs_old_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree;
535*9f988b79SJean-Baptiste Boric 		sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree;
536*9f988b79SJean-Baptiste Boric 		sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree;
537*9f988b79SJean-Baptiste Boric 	}
538*9f988b79SJean-Baptiste Boric 	if (fsopts->needswap)
539*9f988b79SJean-Baptiste Boric 		sblock.fs_flags |= FS_SWAPPED;
540*9f988b79SJean-Baptiste Boric 	ffs_write_superblock(&sblock, fsopts);
541*9f988b79SJean-Baptiste Boric 	return (&sblock);
542*9f988b79SJean-Baptiste Boric }
543*9f988b79SJean-Baptiste Boric 
544*9f988b79SJean-Baptiste Boric /*
545*9f988b79SJean-Baptiste Boric  * Write out the superblock and its duplicates,
546*9f988b79SJean-Baptiste Boric  * and the cylinder group summaries
547*9f988b79SJean-Baptiste Boric  */
548*9f988b79SJean-Baptiste Boric void
ffs_write_superblock(struct fs * fs,const fsinfo_t * fsopts)549*9f988b79SJean-Baptiste Boric ffs_write_superblock(struct fs *fs, const fsinfo_t *fsopts)
550*9f988b79SJean-Baptiste Boric {
551*9f988b79SJean-Baptiste Boric 	int cylno, size, blks, i, saveflag;
552*9f988b79SJean-Baptiste Boric 	void *space;
553*9f988b79SJean-Baptiste Boric 	char *wrbuf;
554*9f988b79SJean-Baptiste Boric 
555*9f988b79SJean-Baptiste Boric 	saveflag = fs->fs_flags & FS_INTERNAL;
556*9f988b79SJean-Baptiste Boric 	fs->fs_flags &= ~FS_INTERNAL;
557*9f988b79SJean-Baptiste Boric 
558*9f988b79SJean-Baptiste Boric         memcpy(writebuf, &sblock, sbsize);
559*9f988b79SJean-Baptiste Boric 	if (fsopts->needswap)
560*9f988b79SJean-Baptiste Boric 		ffs_sb_swap(fs, (struct fs*)writebuf);
561*9f988b79SJean-Baptiste Boric 	ffs_wtfs(fs->fs_sblockloc / sectorsize, sbsize, writebuf, fsopts);
562*9f988b79SJean-Baptiste Boric 
563*9f988b79SJean-Baptiste Boric 	/* Write out the duplicate super blocks */
564*9f988b79SJean-Baptiste Boric 	for (cylno = 0; cylno < fs->fs_ncg; cylno++)
565*9f988b79SJean-Baptiste Boric 		ffs_wtfs(FFS_FSBTODB(fs, cgsblock(fs, cylno)),
566*9f988b79SJean-Baptiste Boric 		    sbsize, writebuf, fsopts);
567*9f988b79SJean-Baptiste Boric 
568*9f988b79SJean-Baptiste Boric 	/* Write out the cylinder group summaries */
569*9f988b79SJean-Baptiste Boric 	size = fs->fs_cssize;
570*9f988b79SJean-Baptiste Boric 	blks = howmany(size, fs->fs_fsize);
571*9f988b79SJean-Baptiste Boric 	space = (void *)fs->fs_csp;
572*9f988b79SJean-Baptiste Boric 	wrbuf = emalloc(size);
573*9f988b79SJean-Baptiste Boric 	for (i = 0; i < blks; i+= fs->fs_frag) {
574*9f988b79SJean-Baptiste Boric 		size = fs->fs_bsize;
575*9f988b79SJean-Baptiste Boric 		if (i + fs->fs_frag > blks)
576*9f988b79SJean-Baptiste Boric 			size = (blks - i) * fs->fs_fsize;
577*9f988b79SJean-Baptiste Boric 		if (fsopts->needswap)
578*9f988b79SJean-Baptiste Boric 			ffs_csum_swap((struct csum *)space,
579*9f988b79SJean-Baptiste Boric 			    (struct csum *)wrbuf, size);
580*9f988b79SJean-Baptiste Boric 		else
581*9f988b79SJean-Baptiste Boric 			memcpy(wrbuf, space, (u_int)size);
582*9f988b79SJean-Baptiste Boric 		ffs_wtfs(FFS_FSBTODB(fs, fs->fs_csaddr + i), size, wrbuf, fsopts);
583*9f988b79SJean-Baptiste Boric 		space = (char *)space + size;
584*9f988b79SJean-Baptiste Boric 	}
585*9f988b79SJean-Baptiste Boric 	free(wrbuf);
586*9f988b79SJean-Baptiste Boric 	fs->fs_flags |= saveflag;
587*9f988b79SJean-Baptiste Boric }
588*9f988b79SJean-Baptiste Boric 
589*9f988b79SJean-Baptiste Boric /*
590*9f988b79SJean-Baptiste Boric  * Initialize a cylinder group.
591*9f988b79SJean-Baptiste Boric  */
592*9f988b79SJean-Baptiste Boric static void
initcg(int cylno,time_t utime,const fsinfo_t * fsopts)593*9f988b79SJean-Baptiste Boric initcg(int cylno, time_t utime, const fsinfo_t *fsopts)
594*9f988b79SJean-Baptiste Boric {
595*9f988b79SJean-Baptiste Boric 	daddr_t cbase, dmax;
596*9f988b79SJean-Baptiste Boric 	int i, j, d, dlower, dupper, blkno;
597*9f988b79SJean-Baptiste Boric 	struct ufs1_dinode *dp1;
598*9f988b79SJean-Baptiste Boric 	struct ufs2_dinode *dp2;
599*9f988b79SJean-Baptiste Boric 	int start;
600*9f988b79SJean-Baptiste Boric 
601*9f988b79SJean-Baptiste Boric 	/*
602*9f988b79SJean-Baptiste Boric 	 * Determine block bounds for cylinder group.
603*9f988b79SJean-Baptiste Boric 	 * Allow space for super block summary information in first
604*9f988b79SJean-Baptiste Boric 	 * cylinder group.
605*9f988b79SJean-Baptiste Boric 	 */
606*9f988b79SJean-Baptiste Boric 	cbase = cgbase(&sblock, cylno);
607*9f988b79SJean-Baptiste Boric 	dmax = cbase + sblock.fs_fpg;
608*9f988b79SJean-Baptiste Boric 	if (dmax > sblock.fs_size)
609*9f988b79SJean-Baptiste Boric 		dmax = sblock.fs_size;
610*9f988b79SJean-Baptiste Boric 	dlower = cgsblock(&sblock, cylno) - cbase;
611*9f988b79SJean-Baptiste Boric 	dupper = cgdmin(&sblock, cylno) - cbase;
612*9f988b79SJean-Baptiste Boric 	if (cylno == 0)
613*9f988b79SJean-Baptiste Boric 		dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
614*9f988b79SJean-Baptiste Boric 	memset(&acg, 0, sblock.fs_cgsize);
615*9f988b79SJean-Baptiste Boric 	acg.cg_time = utime;
616*9f988b79SJean-Baptiste Boric 	acg.cg_magic = CG_MAGIC;
617*9f988b79SJean-Baptiste Boric 	acg.cg_cgx = cylno;
618*9f988b79SJean-Baptiste Boric 	acg.cg_niblk = sblock.fs_ipg;
619*9f988b79SJean-Baptiste Boric 	acg.cg_initediblk = sblock.fs_ipg < 2 * FFS_INOPB(&sblock) ?
620*9f988b79SJean-Baptiste Boric 	    sblock.fs_ipg : 2 * FFS_INOPB(&sblock);
621*9f988b79SJean-Baptiste Boric 	acg.cg_ndblk = dmax - cbase;
622*9f988b79SJean-Baptiste Boric 	if (sblock.fs_contigsumsize > 0)
623*9f988b79SJean-Baptiste Boric 		acg.cg_nclusterblks = acg.cg_ndblk >> sblock.fs_fragshift;
624*9f988b79SJean-Baptiste Boric 	start = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield);
625*9f988b79SJean-Baptiste Boric 	if (Oflag == 2) {
626*9f988b79SJean-Baptiste Boric 		acg.cg_iusedoff = start;
627*9f988b79SJean-Baptiste Boric 	} else {
628*9f988b79SJean-Baptiste Boric 		if (cylno == sblock.fs_ncg - 1)
629*9f988b79SJean-Baptiste Boric 			acg.cg_old_ncyl = howmany(acg.cg_ndblk,
630*9f988b79SJean-Baptiste Boric 			    sblock.fs_fpg / sblock.fs_old_cpg);
631*9f988b79SJean-Baptiste Boric 		else
632*9f988b79SJean-Baptiste Boric 			acg.cg_old_ncyl = sblock.fs_old_cpg;
633*9f988b79SJean-Baptiste Boric 		acg.cg_old_time = acg.cg_time;
634*9f988b79SJean-Baptiste Boric 		acg.cg_time = 0;
635*9f988b79SJean-Baptiste Boric 		acg.cg_old_niblk = acg.cg_niblk;
636*9f988b79SJean-Baptiste Boric 		acg.cg_niblk = 0;
637*9f988b79SJean-Baptiste Boric 		acg.cg_initediblk = 0;
638*9f988b79SJean-Baptiste Boric 		acg.cg_old_btotoff = start;
639*9f988b79SJean-Baptiste Boric 		acg.cg_old_boff = acg.cg_old_btotoff +
640*9f988b79SJean-Baptiste Boric 		    sblock.fs_old_cpg * sizeof(int32_t);
641*9f988b79SJean-Baptiste Boric 		acg.cg_iusedoff = acg.cg_old_boff +
642*9f988b79SJean-Baptiste Boric 		    sblock.fs_old_cpg * sizeof(u_int16_t);
643*9f988b79SJean-Baptiste Boric 	}
644*9f988b79SJean-Baptiste Boric 	acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT);
645*9f988b79SJean-Baptiste Boric 	if (sblock.fs_contigsumsize <= 0) {
646*9f988b79SJean-Baptiste Boric 		acg.cg_nextfreeoff = acg.cg_freeoff +
647*9f988b79SJean-Baptiste Boric 		   howmany(sblock.fs_fpg, CHAR_BIT);
648*9f988b79SJean-Baptiste Boric 	} else {
649*9f988b79SJean-Baptiste Boric 		acg.cg_clustersumoff = acg.cg_freeoff +
650*9f988b79SJean-Baptiste Boric 		    howmany(sblock.fs_fpg, CHAR_BIT) - sizeof(int32_t);
651*9f988b79SJean-Baptiste Boric 		acg.cg_clustersumoff =
652*9f988b79SJean-Baptiste Boric 		    roundup(acg.cg_clustersumoff, sizeof(int32_t));
653*9f988b79SJean-Baptiste Boric 		acg.cg_clusteroff = acg.cg_clustersumoff +
654*9f988b79SJean-Baptiste Boric 		    (sblock.fs_contigsumsize + 1) * sizeof(int32_t);
655*9f988b79SJean-Baptiste Boric 		acg.cg_nextfreeoff = acg.cg_clusteroff +
656*9f988b79SJean-Baptiste Boric 		    howmany(ffs_fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT);
657*9f988b79SJean-Baptiste Boric 	}
658*9f988b79SJean-Baptiste Boric 	if (acg.cg_nextfreeoff > sblock.fs_cgsize) {
659*9f988b79SJean-Baptiste Boric 		printf("Panic: cylinder group too big\n");
660*9f988b79SJean-Baptiste Boric 		exit(37);
661*9f988b79SJean-Baptiste Boric 	}
662*9f988b79SJean-Baptiste Boric 	acg.cg_cs.cs_nifree += sblock.fs_ipg;
663*9f988b79SJean-Baptiste Boric 	if (cylno == 0) {
664*9f988b79SJean-Baptiste Boric 		size_t r;
665*9f988b79SJean-Baptiste Boric 
666*9f988b79SJean-Baptiste Boric 		for (r = 0; r < UFS_ROOTINO; r++) {
667*9f988b79SJean-Baptiste Boric 			setbit(cg_inosused(&acg, 0), r);
668*9f988b79SJean-Baptiste Boric 			acg.cg_cs.cs_nifree--;
669*9f988b79SJean-Baptiste Boric 		}
670*9f988b79SJean-Baptiste Boric 	}
671*9f988b79SJean-Baptiste Boric 	if (cylno > 0) {
672*9f988b79SJean-Baptiste Boric 		/*
673*9f988b79SJean-Baptiste Boric 		 * In cylno 0, beginning space is reserved
674*9f988b79SJean-Baptiste Boric 		 * for boot and super blocks.
675*9f988b79SJean-Baptiste Boric 		 */
676*9f988b79SJean-Baptiste Boric 		for (d = 0, blkno = 0; d < dlower;) {
677*9f988b79SJean-Baptiste Boric 			ffs_setblock(&sblock, cg_blksfree(&acg, 0), blkno);
678*9f988b79SJean-Baptiste Boric 			if (sblock.fs_contigsumsize > 0)
679*9f988b79SJean-Baptiste Boric 				setbit(cg_clustersfree(&acg, 0), blkno);
680*9f988b79SJean-Baptiste Boric 			acg.cg_cs.cs_nbfree++;
681*9f988b79SJean-Baptiste Boric 			d += sblock.fs_frag;
682*9f988b79SJean-Baptiste Boric 			blkno++;
683*9f988b79SJean-Baptiste Boric 		}
684*9f988b79SJean-Baptiste Boric 	}
685*9f988b79SJean-Baptiste Boric 	if ((i = (dupper & (sblock.fs_frag - 1))) != 0) {
686*9f988b79SJean-Baptiste Boric 		acg.cg_frsum[sblock.fs_frag - i]++;
687*9f988b79SJean-Baptiste Boric 		for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
688*9f988b79SJean-Baptiste Boric 			setbit(cg_blksfree(&acg, 0), dupper);
689*9f988b79SJean-Baptiste Boric 			acg.cg_cs.cs_nffree++;
690*9f988b79SJean-Baptiste Boric 		}
691*9f988b79SJean-Baptiste Boric 	}
692*9f988b79SJean-Baptiste Boric 	for (d = dupper, blkno = dupper >> sblock.fs_fragshift;
693*9f988b79SJean-Baptiste Boric 	     d + sblock.fs_frag <= acg.cg_ndblk; ) {
694*9f988b79SJean-Baptiste Boric 		ffs_setblock(&sblock, cg_blksfree(&acg, 0), blkno);
695*9f988b79SJean-Baptiste Boric 		if (sblock.fs_contigsumsize > 0)
696*9f988b79SJean-Baptiste Boric 			setbit(cg_clustersfree(&acg, 0), blkno);
697*9f988b79SJean-Baptiste Boric 		acg.cg_cs.cs_nbfree++;
698*9f988b79SJean-Baptiste Boric 		d += sblock.fs_frag;
699*9f988b79SJean-Baptiste Boric 		blkno++;
700*9f988b79SJean-Baptiste Boric 	}
701*9f988b79SJean-Baptiste Boric 	if (d < acg.cg_ndblk) {
702*9f988b79SJean-Baptiste Boric 		acg.cg_frsum[acg.cg_ndblk - d]++;
703*9f988b79SJean-Baptiste Boric 		for (; d < acg.cg_ndblk; d++) {
704*9f988b79SJean-Baptiste Boric 			setbit(cg_blksfree(&acg, 0), d);
705*9f988b79SJean-Baptiste Boric 			acg.cg_cs.cs_nffree++;
706*9f988b79SJean-Baptiste Boric 		}
707*9f988b79SJean-Baptiste Boric 	}
708*9f988b79SJean-Baptiste Boric 	if (sblock.fs_contigsumsize > 0) {
709*9f988b79SJean-Baptiste Boric 		int32_t *sump = cg_clustersum(&acg, 0);
710*9f988b79SJean-Baptiste Boric 		u_char *mapp = cg_clustersfree(&acg, 0);
711*9f988b79SJean-Baptiste Boric 		int map = *mapp++;
712*9f988b79SJean-Baptiste Boric 		int bit = 1;
713*9f988b79SJean-Baptiste Boric 		int run = 0;
714*9f988b79SJean-Baptiste Boric 
715*9f988b79SJean-Baptiste Boric 		for (i = 0; i < acg.cg_nclusterblks; i++) {
716*9f988b79SJean-Baptiste Boric 			if ((map & bit) != 0) {
717*9f988b79SJean-Baptiste Boric 				run++;
718*9f988b79SJean-Baptiste Boric 			} else if (run != 0) {
719*9f988b79SJean-Baptiste Boric 				if (run > sblock.fs_contigsumsize)
720*9f988b79SJean-Baptiste Boric 					run = sblock.fs_contigsumsize;
721*9f988b79SJean-Baptiste Boric 				sump[run]++;
722*9f988b79SJean-Baptiste Boric 				run = 0;
723*9f988b79SJean-Baptiste Boric 			}
724*9f988b79SJean-Baptiste Boric 			if ((i & (CHAR_BIT - 1)) != (CHAR_BIT - 1)) {
725*9f988b79SJean-Baptiste Boric 				bit <<= 1;
726*9f988b79SJean-Baptiste Boric 			} else {
727*9f988b79SJean-Baptiste Boric 				map = *mapp++;
728*9f988b79SJean-Baptiste Boric 				bit = 1;
729*9f988b79SJean-Baptiste Boric 			}
730*9f988b79SJean-Baptiste Boric 		}
731*9f988b79SJean-Baptiste Boric 		if (run != 0) {
732*9f988b79SJean-Baptiste Boric 			if (run > sblock.fs_contigsumsize)
733*9f988b79SJean-Baptiste Boric 				run = sblock.fs_contigsumsize;
734*9f988b79SJean-Baptiste Boric 			sump[run]++;
735*9f988b79SJean-Baptiste Boric 		}
736*9f988b79SJean-Baptiste Boric 	}
737*9f988b79SJean-Baptiste Boric 	sblock.fs_cs(&sblock, cylno) = acg.cg_cs;
738*9f988b79SJean-Baptiste Boric 	/*
739*9f988b79SJean-Baptiste Boric 	 * Write out the duplicate super block, the cylinder group map
740*9f988b79SJean-Baptiste Boric 	 * and two blocks worth of inodes in a single write.
741*9f988b79SJean-Baptiste Boric 	 */
742*9f988b79SJean-Baptiste Boric 	start = sblock.fs_bsize > SBLOCKSIZE ? sblock.fs_bsize : SBLOCKSIZE;
743*9f988b79SJean-Baptiste Boric 	memcpy(&iobuf[start], &acg, sblock.fs_cgsize);
744*9f988b79SJean-Baptiste Boric 	if (fsopts->needswap)
745*9f988b79SJean-Baptiste Boric 		ffs_cg_swap(&acg, (struct cg*)&iobuf[start], &sblock);
746*9f988b79SJean-Baptiste Boric 	start += sblock.fs_bsize;
747*9f988b79SJean-Baptiste Boric 	dp1 = (struct ufs1_dinode *)(&iobuf[start]);
748*9f988b79SJean-Baptiste Boric 	dp2 = (struct ufs2_dinode *)(&iobuf[start]);
749*9f988b79SJean-Baptiste Boric 	for (i = 0; i < acg.cg_initediblk; i++) {
750*9f988b79SJean-Baptiste Boric 		if (sblock.fs_magic == FS_UFS1_MAGIC) {
751*9f988b79SJean-Baptiste Boric 			/* No need to swap, it'll stay random */
752*9f988b79SJean-Baptiste Boric 			dp1->di_gen = random();
753*9f988b79SJean-Baptiste Boric 			dp1++;
754*9f988b79SJean-Baptiste Boric 		} else {
755*9f988b79SJean-Baptiste Boric 			dp2->di_gen = random();
756*9f988b79SJean-Baptiste Boric 			dp2++;
757*9f988b79SJean-Baptiste Boric 		}
758*9f988b79SJean-Baptiste Boric 	}
759*9f988b79SJean-Baptiste Boric 	ffs_wtfs(FFS_FSBTODB(&sblock, cgsblock(&sblock, cylno)), iobufsize, iobuf,
760*9f988b79SJean-Baptiste Boric 	    fsopts);
761*9f988b79SJean-Baptiste Boric 	/*
762*9f988b79SJean-Baptiste Boric 	 * For the old file system, we have to initialize all the inodes.
763*9f988b79SJean-Baptiste Boric 	 */
764*9f988b79SJean-Baptiste Boric 	if (Oflag <= 1) {
765*9f988b79SJean-Baptiste Boric 		for (i = 2 * sblock.fs_frag;
766*9f988b79SJean-Baptiste Boric 		     i < sblock.fs_ipg / FFS_INOPF(&sblock);
767*9f988b79SJean-Baptiste Boric 		     i += sblock.fs_frag) {
768*9f988b79SJean-Baptiste Boric 			dp1 = (struct ufs1_dinode *)(&iobuf[start]);
769*9f988b79SJean-Baptiste Boric 			for (j = 0; j < FFS_INOPB(&sblock); j++) {
770*9f988b79SJean-Baptiste Boric 				dp1->di_gen = random();
771*9f988b79SJean-Baptiste Boric 				dp1++;
772*9f988b79SJean-Baptiste Boric 			}
773*9f988b79SJean-Baptiste Boric 			ffs_wtfs(FFS_FSBTODB(&sblock, cgimin(&sblock, cylno) + i),
774*9f988b79SJean-Baptiste Boric 			    sblock.fs_bsize, &iobuf[start], fsopts);
775*9f988b79SJean-Baptiste Boric 		}
776*9f988b79SJean-Baptiste Boric 	}
777*9f988b79SJean-Baptiste Boric }
778*9f988b79SJean-Baptiste Boric 
779*9f988b79SJean-Baptiste Boric /*
780*9f988b79SJean-Baptiste Boric  * read a block from the file system
781*9f988b79SJean-Baptiste Boric  */
782*9f988b79SJean-Baptiste Boric void
ffs_rdfs(daddr_t bno,int size,void * bf,const fsinfo_t * fsopts)783*9f988b79SJean-Baptiste Boric ffs_rdfs(daddr_t bno, int size, void *bf, const fsinfo_t *fsopts)
784*9f988b79SJean-Baptiste Boric {
785*9f988b79SJean-Baptiste Boric 	int n;
786*9f988b79SJean-Baptiste Boric 	off_t offset;
787*9f988b79SJean-Baptiste Boric 
788*9f988b79SJean-Baptiste Boric 	offset = bno * fsopts->sectorsize + fsopts->offset;
789*9f988b79SJean-Baptiste Boric 	if (lseek(fsopts->fd, offset, SEEK_SET) < 0)
790*9f988b79SJean-Baptiste Boric 		err(1, "%s: seek error for sector %lld", __func__,
791*9f988b79SJean-Baptiste Boric 		    (long long)bno);
792*9f988b79SJean-Baptiste Boric 	n = read(fsopts->fd, bf, size);
793*9f988b79SJean-Baptiste Boric 	if (n == -1) {
794*9f988b79SJean-Baptiste Boric 		err(1, "%s: read error bno %lld size %d", __func__,
795*9f988b79SJean-Baptiste Boric 		    (long long)bno, size);
796*9f988b79SJean-Baptiste Boric 	}
797*9f988b79SJean-Baptiste Boric 	else if (n != size)
798*9f988b79SJean-Baptiste Boric 		errx(1, "%s: short read error for sector %lld", __func__,
799*9f988b79SJean-Baptiste Boric 		    (long long)bno);
800*9f988b79SJean-Baptiste Boric }
801*9f988b79SJean-Baptiste Boric 
802*9f988b79SJean-Baptiste Boric /*
803*9f988b79SJean-Baptiste Boric  * write a block to the file system
804*9f988b79SJean-Baptiste Boric  */
805*9f988b79SJean-Baptiste Boric void
ffs_wtfs(daddr_t bno,int size,void * bf,const fsinfo_t * fsopts)806*9f988b79SJean-Baptiste Boric ffs_wtfs(daddr_t bno, int size, void *bf, const fsinfo_t *fsopts)
807*9f988b79SJean-Baptiste Boric {
808*9f988b79SJean-Baptiste Boric 	int n;
809*9f988b79SJean-Baptiste Boric 	off_t offset;
810*9f988b79SJean-Baptiste Boric 
811*9f988b79SJean-Baptiste Boric 	offset = bno * fsopts->sectorsize + fsopts->offset;
812*9f988b79SJean-Baptiste Boric 	if (lseek(fsopts->fd, offset, SEEK_SET) == -1)
813*9f988b79SJean-Baptiste Boric 		err(1, "%s: seek error for sector %lld", __func__,
814*9f988b79SJean-Baptiste Boric 		    (long long)bno);
815*9f988b79SJean-Baptiste Boric 	n = write(fsopts->fd, bf, size);
816*9f988b79SJean-Baptiste Boric 	if (n == -1)
817*9f988b79SJean-Baptiste Boric 		err(1, "%s: write error for sector %lld", __func__,
818*9f988b79SJean-Baptiste Boric 		    (long long)bno);
819*9f988b79SJean-Baptiste Boric 	else if (n != size)
820*9f988b79SJean-Baptiste Boric 		errx(1, "%s: short write error for sector %lld", __func__,
821*9f988b79SJean-Baptiste Boric 		    (long long)bno);
822*9f988b79SJean-Baptiste Boric }
823*9f988b79SJean-Baptiste Boric 
824*9f988b79SJean-Baptiste Boric 
825*9f988b79SJean-Baptiste Boric /* Determine how many digits are needed to print a given integer */
826*9f988b79SJean-Baptiste Boric static int
count_digits(int num)827*9f988b79SJean-Baptiste Boric count_digits(int num)
828*9f988b79SJean-Baptiste Boric {
829*9f988b79SJean-Baptiste Boric 	int ndig;
830*9f988b79SJean-Baptiste Boric 
831*9f988b79SJean-Baptiste Boric 	for(ndig = 1; num > 9; num /=10, ndig++);
832*9f988b79SJean-Baptiste Boric 
833*9f988b79SJean-Baptiste Boric 	return (ndig);
834*9f988b79SJean-Baptiste Boric }
835*9f988b79SJean-Baptiste Boric 
836*9f988b79SJean-Baptiste Boric static int
ilog2(int val)837*9f988b79SJean-Baptiste Boric ilog2(int val)
838*9f988b79SJean-Baptiste Boric {
839*9f988b79SJean-Baptiste Boric 	u_int n;
840*9f988b79SJean-Baptiste Boric 
841*9f988b79SJean-Baptiste Boric 	for (n = 0; n < sizeof(n) * CHAR_BIT; n++)
842*9f988b79SJean-Baptiste Boric 		if (1 << n == val)
843*9f988b79SJean-Baptiste Boric 			return (n);
844*9f988b79SJean-Baptiste Boric 	errx(1, "%s: %d is not a power of 2", __func__, val);
845*9f988b79SJean-Baptiste Boric }
846