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