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