2 * Copyright (c) 1980, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
36 static char sccsid[] = "@(#)mkfs.c 8.11 (Berkeley) 5/3/95";
38 static const char rcsid[] =
47 #include <sys/param.h>
49 #include <sys/types.h>
51 #include <sys/resource.h>
53 #include <ufs/ufs/dinode.h>
54 #include <ufs/ufs/dir.h>
55 #include <ufs/ffs/fs.h>
56 #include <sys/disklabel.h>
59 #include <sys/ioctl.h>
64 extern int atoi __P((char *));
65 extern char * getenv __P((char *));
69 extern long random __P((void));
70 extern void srandomdev __P((void));
74 * make file system for cylinder-group style file systems
78 * We limit the size of the inode map to be no more than a
79 * third of the cylinder group space, since we must leave at
80 * least an equal amount of space for the block map.
82 * N.B.: MAXIPG must be a multiple of INOPB(fs).
84 #define MAXIPG(fs) roundup((fs)->fs_bsize * NBBY / 3, INOPB(fs))
87 #define MAXINOPB (MAXBSIZE / sizeof(struct dinode))
88 #define POWEROF2(num) (((num) & ((num) - 1)) == 0)
91 * variables set up by front end.
93 extern int mfs; /* run as the memory based filesystem */
94 extern char *mfs_mtpt; /* mount point for mfs */
95 extern struct stat mfs_mtstat; /* stat prior to mount */
96 extern int Nflag; /* run mkfs without writing file system */
97 extern int Oflag; /* format as an 4.3BSD file system */
98 extern int fssize; /* file system size */
99 extern int ntracks; /* # tracks/cylinder */
100 extern int nsectors; /* # sectors/track */
101 extern int nphyssectors; /* # sectors/track including spares */
102 extern int secpercyl; /* sectors per cylinder */
103 extern int sectorsize; /* bytes/sector */
104 extern int realsectorsize; /* bytes/sector in hardware*/
105 extern int rpm; /* revolutions/minute of drive */
106 extern int interleave; /* hardware sector interleave */
107 extern int trackskew; /* sector 0 skew, per track */
108 extern int fsize; /* fragment size */
109 extern int bsize; /* block size */
110 extern int cpg; /* cylinders/cylinder group */
111 extern int cpgflg; /* cylinders/cylinder group flag was given */
112 extern int minfree; /* free space threshold */
113 extern int opt; /* optimization preference (space or time) */
114 extern int density; /* number of bytes per inode */
115 extern int maxcontig; /* max contiguous blocks to allocate */
116 extern int rotdelay; /* rotational delay between blocks */
117 extern int maxbpg; /* maximum blocks per file in a cyl group */
118 extern int nrpos; /* # of distinguished rotational positions */
119 extern int bbsize; /* boot block size */
120 extern int sbsize; /* superblock size */
121 extern u_long memleft; /* virtual memory available */
122 extern caddr_t membase; /* start address of memory based filesystem */
123 extern char * filename;
129 #define sblock fsun.fs
138 struct dinode zino[MAXBSIZE / sizeof(struct dinode)];
146 static int charsperline();
147 void clrblock __P((struct fs *, unsigned char *, int));
148 void fsinit __P((time_t));
149 void initcg __P((int, time_t));
150 int isblock __P((struct fs *, unsigned char *, int));
151 void iput __P((struct dinode *, ino_t));
152 int makedir __P((struct direct *, int));
153 void rdfs __P((daddr_t, int, char *));
154 void setblock __P((struct fs *, unsigned char *, int));
155 void wtfs __P((daddr_t, int, char *));
158 void get_memleft __P((void));
159 void raise_data_limit __P((void));
161 void free __P((char *));
162 char * calloc __P((u_long, u_long));
163 caddr_t malloc __P((u_long));
164 caddr_t realloc __P((char *, u_long));
170 mkfs(pp, fsys, fi, fo)
171 struct partition *pp;
175 register long i, mincpc, mincpg, inospercg;
176 long cylno, rpos, blk, j, warn = 0;
177 long used, mincpgcnt, bpcg;
179 long mapcramped, inodecramped;
180 long postblsize, rotblsize, totalsbsize;
186 char tmpbuf[100]; /* XXX this will break in about 2,500 years */
199 (void) signal(SIGUSR1, started);
203 if (waitpid(i, &status, 0) != -1 && WIFEXITED(status))
204 exit(WEXITSTATUS(status));
214 unsigned char buf[BUFSIZ];
216 fd = open(filename,O_RDWR|O_TRUNC|O_CREAT,0644);
218 err(12, "%s", filename);
219 for(l=0;l< fssize * sectorsize;l += l1) {
220 l1 = fssize * sectorsize;
223 if (l1 != write(fd,buf,l1))
224 err(12, "%s", filename);
229 PROT_READ|PROT_WRITE,
233 if(membase == MAP_FAILED)
240 if (fssize * sectorsize > (memleft - 131072))
241 fssize = (memleft - 131072) / sectorsize;
242 if ((membase = malloc(fssize * sectorsize)) == NULL)
243 errx(13, "malloc failed");
249 sblock.fs_inodefmt = FS_42INODEFMT;
250 sblock.fs_maxsymlinklen = 0;
252 sblock.fs_inodefmt = FS_44INODEFMT;
253 sblock.fs_maxsymlinklen = MAXSYMLINKLEN;
256 * Validate the given file system size.
257 * Verify that its last block can actually be accessed.
260 printf("preposterous size %d\n", fssize), exit(13);
261 wtfs(fssize - (realsectorsize / DEV_BSIZE), realsectorsize,
264 * collect and verify the sector and track info
266 sblock.fs_nsect = nsectors;
267 sblock.fs_ntrak = ntracks;
268 if (sblock.fs_ntrak <= 0)
269 printf("preposterous ntrak %d\n", sblock.fs_ntrak), exit(14);
270 if (sblock.fs_nsect <= 0)
271 printf("preposterous nsect %d\n", sblock.fs_nsect), exit(15);
273 * collect and verify the block and fragment sizes
275 sblock.fs_bsize = bsize;
276 sblock.fs_fsize = fsize;
277 if (!POWEROF2(sblock.fs_bsize)) {
278 printf("block size must be a power of 2, not %d\n",
282 if (!POWEROF2(sblock.fs_fsize)) {
283 printf("fragment size must be a power of 2, not %d\n",
287 if (sblock.fs_fsize < sectorsize) {
288 printf("fragment size %d is too small, minimum is %d\n",
289 sblock.fs_fsize, sectorsize);
292 if (sblock.fs_bsize < MINBSIZE) {
293 printf("block size %d is too small, minimum is %d\n",
294 sblock.fs_bsize, MINBSIZE);
297 if (sblock.fs_bsize < sblock.fs_fsize) {
298 printf("block size (%d) cannot be smaller than fragment size (%d)\n",
299 sblock.fs_bsize, sblock.fs_fsize);
302 sblock.fs_bmask = ~(sblock.fs_bsize - 1);
303 sblock.fs_fmask = ~(sblock.fs_fsize - 1);
304 sblock.fs_qbmask = ~sblock.fs_bmask;
305 sblock.fs_qfmask = ~sblock.fs_fmask;
306 for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1)
308 for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1)
310 sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize);
311 for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1)
312 sblock.fs_fragshift++;
313 if (sblock.fs_frag > MAXFRAG) {
314 printf("fragment size %d is too small, minimum with block size %d is %d\n",
315 sblock.fs_fsize, sblock.fs_bsize,
316 sblock.fs_bsize / MAXFRAG);
319 sblock.fs_nrpos = nrpos;
320 sblock.fs_nindir = sblock.fs_bsize / sizeof(daddr_t);
321 sblock.fs_inopb = sblock.fs_bsize / sizeof(struct dinode);
322 sblock.fs_nspf = sblock.fs_fsize / sectorsize;
323 for (sblock.fs_fsbtodb = 0, i = NSPF(&sblock); i > 1; i >>= 1)
326 roundup(howmany(bbsize + sbsize, sblock.fs_fsize), sblock.fs_frag);
327 sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno +
328 roundup(howmany(sbsize, sblock.fs_fsize), sblock.fs_frag));
329 sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag;
330 sblock.fs_cgoffset = roundup(
331 howmany(sblock.fs_nsect, NSPF(&sblock)), sblock.fs_frag);
332 for (sblock.fs_cgmask = 0xffffffff, i = sblock.fs_ntrak; i > 1; i >>= 1)
333 sblock.fs_cgmask <<= 1;
334 if (!POWEROF2(sblock.fs_ntrak))
335 sblock.fs_cgmask <<= 1;
336 sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
337 for (sizepb = sblock.fs_bsize, i = 0; i < NIADDR; i++) {
338 sizepb *= NINDIR(&sblock);
339 sblock.fs_maxfilesize += sizepb;
342 * Validate specified/determined secpercyl
343 * and calculate minimum cylinders per group.
345 sblock.fs_spc = secpercyl;
346 for (sblock.fs_cpc = NSPB(&sblock), i = sblock.fs_spc;
347 sblock.fs_cpc > 1 && (i & 1) == 0;
348 sblock.fs_cpc >>= 1, i >>= 1)
350 mincpc = sblock.fs_cpc;
351 bpcg = sblock.fs_spc * sectorsize;
352 inospercg = roundup(bpcg / sizeof(struct dinode), INOPB(&sblock));
353 if (inospercg > MAXIPG(&sblock))
354 inospercg = MAXIPG(&sblock);
355 used = (sblock.fs_iblkno + inospercg / INOPF(&sblock)) * NSPF(&sblock);
356 mincpgcnt = howmany(sblock.fs_cgoffset * (~sblock.fs_cgmask) + used,
358 mincpg = roundup(mincpgcnt, mincpc);
360 * Ensure that cylinder group with mincpg has enough space
363 sblock.fs_cpg = mincpg;
364 sblock.fs_ipg = inospercg;
366 sblock.fs_contigsumsize = MIN(maxcontig, FS_MAXCONTIG);
368 while (CGSIZE(&sblock) > sblock.fs_bsize) {
370 if (sblock.fs_bsize < MAXBSIZE) {
371 sblock.fs_bsize <<= 1;
377 mincpg = roundup(mincpgcnt, mincpc);
378 sblock.fs_cpg = mincpg;
380 sblock.fs_frag <<= 1;
381 sblock.fs_fragshift += 1;
382 if (sblock.fs_frag <= MAXFRAG)
385 if (sblock.fs_fsize == sblock.fs_bsize) {
386 printf("There is no block size that");
387 printf(" can support this disk\n");
390 sblock.fs_frag >>= 1;
391 sblock.fs_fragshift -= 1;
392 sblock.fs_fsize <<= 1;
393 sblock.fs_nspf <<= 1;
396 * Ensure that cylinder group with mincpg has enough space for inodes.
399 inospercg = calcipg(mincpg, bpcg, &usedb);
400 sblock.fs_ipg = inospercg;
401 while (inospercg > MAXIPG(&sblock)) {
403 if (mincpc == 1 || sblock.fs_frag == 1 ||
404 sblock.fs_bsize == MINBSIZE)
406 printf("With a block size of %d %s %d\n", sblock.fs_bsize,
407 "minimum bytes per inode is",
408 (int)((mincpg * (off_t)bpcg - usedb)
409 / MAXIPG(&sblock) + 1));
410 sblock.fs_bsize >>= 1;
411 sblock.fs_frag >>= 1;
412 sblock.fs_fragshift -= 1;
414 sblock.fs_cpg = roundup(mincpgcnt, mincpc);
415 if (CGSIZE(&sblock) > sblock.fs_bsize) {
416 sblock.fs_bsize <<= 1;
419 mincpg = sblock.fs_cpg;
420 inospercg = calcipg(mincpg, bpcg, &usedb);
421 sblock.fs_ipg = inospercg;
424 if (inospercg > MAXIPG(&sblock)) {
425 printf("Minimum bytes per inode is %d\n",
426 (int)((mincpg * (off_t)bpcg - usedb)
427 / MAXIPG(&sblock) + 1));
428 } else if (!mapcramped) {
429 printf("With %d bytes per inode, ", density);
430 printf("minimum cylinders per group is %ld\n", mincpg);
434 printf("With %d sectors per cylinder, ", sblock.fs_spc);
435 printf("minimum cylinders per group is %ld\n", mincpg);
437 if (inodecramped || mapcramped) {
438 if (sblock.fs_bsize != bsize)
439 printf("%s to be changed from %d to %d\n",
440 "This requires the block size",
441 bsize, sblock.fs_bsize);
442 if (sblock.fs_fsize != fsize)
443 printf("\t%s to be changed from %d to %d\n",
444 "and the fragment size",
445 fsize, sblock.fs_fsize);
449 * Calculate the number of cylinders per group
452 if (sblock.fs_cpg % mincpc != 0) {
453 printf("%s groups must have a multiple of %ld cylinders\n",
454 cpgflg ? "Cylinder" : "Warning: cylinder", mincpc);
455 sblock.fs_cpg = roundup(sblock.fs_cpg, mincpc);
460 * Must ensure there is enough space for inodes.
462 sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
463 while (sblock.fs_ipg > MAXIPG(&sblock)) {
465 sblock.fs_cpg -= mincpc;
466 sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
469 * Must ensure there is enough space to hold block map.
471 while (CGSIZE(&sblock) > sblock.fs_bsize) {
473 sblock.fs_cpg -= mincpc;
474 sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
476 sblock.fs_fpg = (sblock.fs_cpg * sblock.fs_spc) / NSPF(&sblock);
477 if ((sblock.fs_cpg * sblock.fs_spc) % NSPB(&sblock) != 0) {
478 printf("panic (fs_cpg * fs_spc) %% NSPF != 0");
481 if (sblock.fs_cpg < mincpg) {
482 printf("cylinder groups must have at least %ld cylinders\n",
485 } else if (sblock.fs_cpg != cpg) {
488 else if (!mapcramped && !inodecramped)
490 if (mapcramped && inodecramped)
491 printf("Block size and bytes per inode restrict");
493 printf("Block size restricts");
495 printf("Bytes per inode restrict");
496 printf(" cylinders per group to %d.\n", sblock.fs_cpg);
500 sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock));
502 * Now have size for file system and nsect and ntrak.
503 * Determine number of cylinders and blocks in the file system.
505 sblock.fs_size = fssize = dbtofsb(&sblock, fssize);
506 sblock.fs_ncyl = fssize * NSPF(&sblock) / sblock.fs_spc;
507 if (fssize * NSPF(&sblock) > sblock.fs_ncyl * sblock.fs_spc) {
511 if (sblock.fs_ncyl < 1) {
512 printf("file systems must have at least one cylinder\n");
516 * Determine feasability/values of rotational layout tables.
518 * The size of the rotational layout tables is limited by the
519 * size of the superblock, SBSIZE. The amount of space available
520 * for tables is calculated as (SBSIZE - sizeof (struct fs)).
521 * The size of these tables is inversely proportional to the block
522 * size of the file system. The size increases if sectors per track
523 * are not powers of two, because more cylinders must be described
524 * by the tables before the rotational pattern repeats (fs_cpc).
526 sblock.fs_interleave = interleave;
527 sblock.fs_trackskew = trackskew;
528 sblock.fs_npsect = nphyssectors;
529 sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
530 sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs));
531 if (sblock.fs_sbsize > SBSIZE)
532 sblock.fs_sbsize = SBSIZE;
533 if (sblock.fs_ntrak == 1) {
537 postblsize = sblock.fs_nrpos * sblock.fs_cpc * sizeof(int16_t);
538 rotblsize = sblock.fs_cpc * sblock.fs_spc / NSPB(&sblock);
539 totalsbsize = sizeof(struct fs) + rotblsize;
540 if (sblock.fs_nrpos == 8 && sblock.fs_cpc <= 16) {
541 /* use old static table space */
542 sblock.fs_postbloff = (char *)(&sblock.fs_opostbl[0][0]) -
543 (char *)(&sblock.fs_firstfield);
544 sblock.fs_rotbloff = &sblock.fs_space[0] -
545 (u_char *)(&sblock.fs_firstfield);
547 /* use dynamic table space */
548 sblock.fs_postbloff = &sblock.fs_space[0] -
549 (u_char *)(&sblock.fs_firstfield);
550 sblock.fs_rotbloff = sblock.fs_postbloff + postblsize;
551 totalsbsize += postblsize;
553 if (totalsbsize > SBSIZE ||
554 sblock.fs_nsect > (1 << NBBY) * NSPB(&sblock)) {
555 printf("%s %s %d %s %d.%s",
556 "Warning: insufficient space in super block for\n",
557 "rotational layout tables with nsect", sblock.fs_nsect,
558 "and ntrak", sblock.fs_ntrak,
559 "\nFile system performance may be impaired.\n");
563 sblock.fs_sbsize = fragroundup(&sblock, totalsbsize);
564 if (sblock.fs_sbsize > SBSIZE)
565 sblock.fs_sbsize = SBSIZE;
567 * calculate the available blocks for each rotational position
569 for (cylno = 0; cylno < sblock.fs_cpc; cylno++)
570 for (rpos = 0; rpos < sblock.fs_nrpos; rpos++)
571 fs_postbl(&sblock, cylno)[rpos] = -1;
572 for (i = (rotblsize - 1) * sblock.fs_frag;
573 i >= 0; i -= sblock.fs_frag) {
574 cylno = cbtocylno(&sblock, i);
575 rpos = cbtorpos(&sblock, i);
576 blk = fragstoblks(&sblock, i);
577 if (fs_postbl(&sblock, cylno)[rpos] == -1)
578 fs_rotbl(&sblock)[blk] = 0;
580 fs_rotbl(&sblock)[blk] =
581 fs_postbl(&sblock, cylno)[rpos] - blk;
582 fs_postbl(&sblock, cylno)[rpos] = blk;
586 * Compute/validate number of cylinder groups.
588 sblock.fs_ncg = sblock.fs_ncyl / sblock.fs_cpg;
589 if (sblock.fs_ncyl % sblock.fs_cpg)
591 sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock);
592 i = MIN(~sblock.fs_cgmask, sblock.fs_ncg - 1);
593 if (cgdmin(&sblock, i) - cgbase(&sblock, i) >= sblock.fs_fpg) {
594 printf("inode blocks/cyl group (%ld) >= data blocks (%ld)\n",
595 cgdmin(&sblock, i) - cgbase(&sblock, i) / sblock.fs_frag,
596 (long)(sblock.fs_fpg / sblock.fs_frag));
597 printf("number of cylinders per cylinder group (%d) %s.\n",
598 sblock.fs_cpg, "must be increased");
601 j = sblock.fs_ncg - 1;
602 if ((i = fssize - j * sblock.fs_fpg) < sblock.fs_fpg &&
603 cgdmin(&sblock, j) - cgbase(&sblock, j) > i) {
605 printf("Filesystem must have at least %d sectors\n",
607 (cgdmin(&sblock, 0) + 3 * sblock.fs_frag));
611 "Warning: inode blocks/cyl group (%ld) >= data blocks (%ld) in last\n",
612 (cgdmin(&sblock, j) - cgbase(&sblock, j)) / sblock.fs_frag,
615 " cylinder group. This implies %ld sector(s) cannot be allocated.\n",
618 sblock.fs_ncyl -= sblock.fs_ncyl % sblock.fs_cpg;
619 sblock.fs_size = fssize = sblock.fs_ncyl * sblock.fs_spc /
624 printf("Warning: %d sector(s) in last cylinder unallocated\n",
626 (fssize * NSPF(&sblock) - (sblock.fs_ncyl - 1)
630 * fill in remaining fields of the super block
632 sblock.fs_csaddr = cgdmin(&sblock, 0);
634 fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
635 i = sblock.fs_bsize / sizeof(struct csum);
636 sblock.fs_csmask = ~(i - 1);
637 for (sblock.fs_csshift = 0; i > 1; i >>= 1)
639 fscs = (struct csum *)calloc(1, sblock.fs_cssize);
641 errx(31, "calloc failed");
642 sblock.fs_magic = FS_MAGIC;
643 sblock.fs_rotdelay = rotdelay;
644 sblock.fs_minfree = minfree;
645 sblock.fs_maxcontig = maxcontig;
646 sblock.fs_maxbpg = maxbpg;
647 sblock.fs_rps = rpm / 60;
648 sblock.fs_optim = opt;
649 sblock.fs_cgrotor = 0;
650 sblock.fs_cstotal.cs_ndir = 0;
651 sblock.fs_cstotal.cs_nbfree = 0;
652 sblock.fs_cstotal.cs_nifree = 0;
653 sblock.fs_cstotal.cs_nffree = 0;
658 sblock.fs_id[0] = (long)utime;
659 sblock.fs_id[1] = random();
663 * Dump out summary information about file system.
666 printf("%s:\t%d sectors in %d %s of %d tracks, %d sectors\n",
667 fsys, sblock.fs_size * NSPF(&sblock), sblock.fs_ncyl,
668 "cylinders", sblock.fs_ntrak, sblock.fs_nsect);
669 #define B2MBFACTOR (1 / (1024.0 * 1024.0))
670 printf("\t%.1fMB in %d cyl groups (%d c/g, %.2fMB/g, %d i/g)\n",
671 (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
672 sblock.fs_ncg, sblock.fs_cpg,
673 (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
678 * Now build the cylinders group blocks and
679 * then print out indices of cylinder groups.
682 printf("super-block backups (for fsck -b #) at:\n");
684 width = charsperline();
685 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
686 initcg(cylno, utime);
689 j = sprintf(tmpbuf, " %ld%s",
690 fsbtodb(&sblock, cgsblock(&sblock, cylno)),
691 cylno < (sblock.fs_ncg-1) ? "," : "" );
692 if (i + j >= width) {
697 printf("%s", tmpbuf);
705 * Now construct the initial file system,
706 * then write out the super-block.
709 sblock.fs_time = utime;
710 wtfs((int)SBOFF / sectorsize, sbsize, (char *)&sblock);
711 for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize)
712 wtfs(fsbtodb(&sblock, sblock.fs_csaddr + numfrags(&sblock, i)),
713 sblock.fs_cssize - i < sblock.fs_bsize ?
714 sblock.fs_cssize - i : sblock.fs_bsize,
717 * Write out the duplicate super blocks
719 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
720 wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)),
721 sbsize, (char *)&sblock);
723 * Update information about this partion in pack
724 * label, to that it may be updated on disk.
726 pp->p_fstype = FS_BSDFFS;
727 pp->p_fsize = sblock.fs_fsize;
728 pp->p_frag = sblock.fs_frag;
729 pp->p_cpg = sblock.fs_cpg;
731 * Notify parent process of success.
732 * Dissociate from session and tty.
735 kill(mfs_ppid, SIGUSR1);
745 * Initialize a cylinder group.
752 daddr_t cbase, d, dlower, dupper, dmax, blkno;
754 register struct csum *cs;
760 * Determine block bounds for cylinder group.
761 * Allow space for super block summary information in first
764 cbase = cgbase(&sblock, cylno);
765 dmax = cbase + sblock.fs_fpg;
766 if (dmax > sblock.fs_size)
767 dmax = sblock.fs_size;
768 dlower = cgsblock(&sblock, cylno) - cbase;
769 dupper = cgdmin(&sblock, cylno) - cbase;
771 dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
773 memset(&acg, 0, sblock.fs_cgsize);
775 acg.cg_magic = CG_MAGIC;
777 if (cylno == sblock.fs_ncg - 1)
778 acg.cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg;
780 acg.cg_ncyl = sblock.fs_cpg;
781 acg.cg_niblk = sblock.fs_ipg;
782 acg.cg_ndblk = dmax - cbase;
783 if (sblock.fs_contigsumsize > 0)
784 acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag;
785 acg.cg_btotoff = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield);
786 acg.cg_boff = acg.cg_btotoff + sblock.fs_cpg * sizeof(int32_t);
787 acg.cg_iusedoff = acg.cg_boff +
788 sblock.fs_cpg * sblock.fs_nrpos * sizeof(u_int16_t);
789 acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, NBBY);
790 if (sblock.fs_contigsumsize <= 0) {
791 acg.cg_nextfreeoff = acg.cg_freeoff +
792 howmany(sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY);
794 acg.cg_clustersumoff = acg.cg_freeoff + howmany
795 (sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY) -
797 acg.cg_clustersumoff =
798 roundup(acg.cg_clustersumoff, sizeof(u_int32_t));
799 acg.cg_clusteroff = acg.cg_clustersumoff +
800 (sblock.fs_contigsumsize + 1) * sizeof(u_int32_t);
801 acg.cg_nextfreeoff = acg.cg_clusteroff + howmany
802 (sblock.fs_cpg * sblock.fs_spc / NSPB(&sblock), NBBY);
804 if (acg.cg_nextfreeoff - (long)(&acg.cg_firstfield) > sblock.fs_cgsize) {
805 printf("Panic: cylinder group too big\n");
808 acg.cg_cs.cs_nifree += sblock.fs_ipg;
810 for (i = 0; i < ROOTINO; i++) {
811 setbit(cg_inosused(&acg), i);
812 acg.cg_cs.cs_nifree--;
814 for (i = 0; i < sblock.fs_ipg / INOPF(&sblock); i += sblock.fs_frag) {
816 for (j = 0; j < sblock.fs_bsize / sizeof(struct dinode); j++)
817 zino[j].di_gen = random();
819 wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
820 sblock.fs_bsize, (char *)zino);
824 * In cylno 0, beginning space is reserved
825 * for boot and super blocks.
827 for (d = 0; d < dlower; d += sblock.fs_frag) {
828 blkno = d / sblock.fs_frag;
829 setblock(&sblock, cg_blksfree(&acg), blkno);
830 if (sblock.fs_contigsumsize > 0)
831 setbit(cg_clustersfree(&acg), blkno);
832 acg.cg_cs.cs_nbfree++;
833 cg_blktot(&acg)[cbtocylno(&sblock, d)]++;
834 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))
835 [cbtorpos(&sblock, d)]++;
837 sblock.fs_dsize += dlower;
839 sblock.fs_dsize += acg.cg_ndblk - dupper;
840 if ((i = dupper % sblock.fs_frag)) {
841 acg.cg_frsum[sblock.fs_frag - i]++;
842 for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
843 setbit(cg_blksfree(&acg), dupper);
844 acg.cg_cs.cs_nffree++;
847 for (d = dupper; d + sblock.fs_frag <= dmax - cbase; ) {
848 blkno = d / sblock.fs_frag;
849 setblock(&sblock, cg_blksfree(&acg), blkno);
850 if (sblock.fs_contigsumsize > 0)
851 setbit(cg_clustersfree(&acg), blkno);
852 acg.cg_cs.cs_nbfree++;
853 cg_blktot(&acg)[cbtocylno(&sblock, d)]++;
854 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))
855 [cbtorpos(&sblock, d)]++;
858 if (d < dmax - cbase) {
859 acg.cg_frsum[dmax - cbase - d]++;
860 for (; d < dmax - cbase; d++) {
861 setbit(cg_blksfree(&acg), d);
862 acg.cg_cs.cs_nffree++;
865 if (sblock.fs_contigsumsize > 0) {
866 int32_t *sump = cg_clustersum(&acg);
867 u_char *mapp = cg_clustersfree(&acg);
872 for (i = 0; i < acg.cg_nclusterblks; i++) {
873 if ((map & bit) != 0) {
875 } else if (run != 0) {
876 if (run > sblock.fs_contigsumsize)
877 run = sblock.fs_contigsumsize;
881 if ((i & (NBBY - 1)) != (NBBY - 1)) {
889 if (run > sblock.fs_contigsumsize)
890 run = sblock.fs_contigsumsize;
894 sblock.fs_cstotal.cs_ndir += acg.cg_cs.cs_ndir;
895 sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree;
896 sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree;
897 sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree;
899 wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
900 sblock.fs_bsize, (char *)&acg);
904 * initialize the file system
914 struct direct root_dir[] = {
915 { ROOTINO, sizeof(struct direct), DT_DIR, 1, "." },
916 { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
918 { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 10, "lost+found" },
925 u_char d_name[MAXNAMLEN + 1];
927 { ROOTINO, sizeof(struct direct), 1, "." },
928 { ROOTINO, sizeof(struct direct), 2, ".." },
930 { LOSTFOUNDINO, sizeof(struct direct), 10, "lost+found" },
934 struct direct lost_found_dir[] = {
935 { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 1, "." },
936 { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
937 { 0, DIRBLKSIZ, 0, 0, 0 },
939 struct odirect olost_found_dir[] = {
940 { LOSTFOUNDINO, sizeof(struct direct), 1, "." },
941 { ROOTINO, sizeof(struct direct), 2, ".." },
942 { 0, DIRBLKSIZ, 0, 0 },
956 * initialize the node
958 node.di_atime = utime;
959 node.di_mtime = utime;
960 node.di_ctime = utime;
963 * create the lost+found directory
966 (void)makedir((struct direct *)olost_found_dir, 2);
967 for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ)
968 memmove(&buf[i], &olost_found_dir[2],
969 DIRSIZ(0, &olost_found_dir[2]));
971 (void)makedir(lost_found_dir, 2);
972 for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ)
973 memmove(&buf[i], &lost_found_dir[2],
974 DIRSIZ(0, &lost_found_dir[2]));
976 node.di_mode = IFDIR | UMASK;
978 node.di_size = sblock.fs_bsize;
979 node.di_db[0] = alloc(node.di_size, node.di_mode);
980 node.di_blocks = btodb(fragroundup(&sblock, node.di_size));
981 wtfs(fsbtodb(&sblock, node.di_db[0]), node.di_size, buf);
982 iput(&node, LOSTFOUNDINO);
985 * create the root directory
988 node.di_mode = IFDIR | 01777;
990 node.di_mode = IFDIR | UMASK;
991 node.di_nlink = PREDEFDIR;
993 node.di_size = makedir((struct direct *)oroot_dir, PREDEFDIR);
995 node.di_size = makedir(root_dir, PREDEFDIR);
996 node.di_db[0] = alloc(sblock.fs_fsize, node.di_mode);
997 node.di_blocks = btodb(fragroundup(&sblock, node.di_size));
998 wtfs(fsbtodb(&sblock, node.di_db[0]), sblock.fs_fsize, buf);
999 iput(&node, ROOTINO);
1003 * construct a set of directory entries in "buf".
1004 * return size of directory.
1007 makedir(protodir, entries)
1008 register struct direct *protodir;
1014 spcleft = DIRBLKSIZ;
1015 for (cp = buf, i = 0; i < entries - 1; i++) {
1016 protodir[i].d_reclen = DIRSIZ(0, &protodir[i]);
1017 memmove(cp, &protodir[i], protodir[i].d_reclen);
1018 cp += protodir[i].d_reclen;
1019 spcleft -= protodir[i].d_reclen;
1021 protodir[i].d_reclen = spcleft;
1022 memmove(cp, &protodir[i], DIRSIZ(0, &protodir[i]));
1027 * allocate a block or frag
1037 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
1039 if (acg.cg_magic != CG_MAGIC) {
1040 printf("cg 0: bad magic number\n");
1043 if (acg.cg_cs.cs_nbfree == 0) {
1044 printf("first cylinder group ran out of space\n");
1047 for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag)
1048 if (isblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag))
1050 printf("internal error: can't find block in cyl 0\n");
1053 blkno = fragstoblks(&sblock, d);
1054 clrblock(&sblock, cg_blksfree(&acg), blkno);
1055 if (sblock.fs_contigsumsize > 0)
1056 clrbit(cg_clustersfree(&acg), blkno);
1057 acg.cg_cs.cs_nbfree--;
1058 sblock.fs_cstotal.cs_nbfree--;
1059 fscs[0].cs_nbfree--;
1061 acg.cg_cs.cs_ndir++;
1062 sblock.fs_cstotal.cs_ndir++;
1065 cg_blktot(&acg)[cbtocylno(&sblock, d)]--;
1066 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))[cbtorpos(&sblock, d)]--;
1067 if (size != sblock.fs_bsize) {
1068 frag = howmany(size, sblock.fs_fsize);
1069 fscs[0].cs_nffree += sblock.fs_frag - frag;
1070 sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag;
1071 acg.cg_cs.cs_nffree += sblock.fs_frag - frag;
1072 acg.cg_frsum[sblock.fs_frag - frag]++;
1073 for (i = frag; i < sblock.fs_frag; i++)
1074 setbit(cg_blksfree(&acg), d + i);
1076 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
1082 * Calculate number of inodes per group.
1085 calcipg(cpg, bpcg, usedbp)
1091 long ipg, new_ipg, ncg, ncyl;
1095 * Prepare to scale by fssize / (number of sectors in cylinder groups).
1096 * Note that fssize is still in sectors, not filesystem blocks.
1098 ncyl = howmany(fssize, (u_int)secpercyl);
1099 ncg = howmany(ncyl, cpg);
1101 * Iterate a few times to allow for ipg depending on itself.
1104 for (i = 0; i < 10; i++) {
1105 usedb = (sblock.fs_iblkno + ipg / INOPF(&sblock))
1106 * NSPF(&sblock) * (off_t)sectorsize;
1107 new_ipg = (cpg * (quad_t)bpcg - usedb) / density * fssize
1108 / ncg / secpercyl / cpg;
1109 new_ipg = roundup(new_ipg, INOPB(&sblock));
1119 * Allocate an inode on the disk
1123 register struct dinode *ip;
1126 struct dinode buf[MAXINOPB];
1131 ip->di_gen = random();
1133 c = ino_to_cg(&sblock, ino);
1134 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
1136 if (acg.cg_magic != CG_MAGIC) {
1137 printf("cg 0: bad magic number\n");
1140 acg.cg_cs.cs_nifree--;
1141 setbit(cg_inosused(&acg), ino);
1142 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
1144 sblock.fs_cstotal.cs_nifree--;
1145 fscs[0].cs_nifree--;
1146 if (ino >= sblock.fs_ipg * sblock.fs_ncg) {
1147 printf("fsinit: inode value out of range (%d).\n", ino);
1150 d = fsbtodb(&sblock, ino_to_fsba(&sblock, ino));
1151 rdfs(d, sblock.fs_bsize, (char *)buf);
1152 buf[ino_to_fsbo(&sblock, ino)] = *ip;
1153 wtfs(d, sblock.fs_bsize, (char *)buf);
1157 * Notify parent process that the filesystem has created itself successfully.
1159 * We have to wait until the mount has actually completed!
1164 int retry = 100; /* 10 seconds, 100ms */
1166 while (mfs_ppid && retry) {
1170 stat(mfs_mtpt, &st) < 0 ||
1171 st.st_dev != mfs_mtstat.st_dev
1179 fatal("mfs mount failed waiting for mount to go active");
1186 * Replace libc function with one suited to our needs.
1190 register u_long size;
1198 pgsz = getpagesize() - 1;
1199 i = (char *)((u_long)(base + pgsz) &~ pgsz);
1200 base = sbrk(i - base);
1201 if (getrlimit(RLIMIT_DATA, &rlp) < 0)
1203 rlp.rlim_cur = rlp.rlim_max;
1204 if (setrlimit(RLIMIT_DATA, &rlp) < 0)
1206 memleft = rlp.rlim_max - (u_long)base;
1208 size = (size + pgsz) &~ pgsz;
1214 return ((caddr_t)sbrk(size));
1218 * Replace libc function with one suited to our needs.
1227 if ((p = malloc(size)) == NULL)
1229 memmove(p, ptr, size);
1235 * Replace libc function with one suited to our needs.
1238 calloc(size, numelm)
1239 u_long size, numelm;
1244 if ((base = malloc(size)) == NULL)
1246 memset(base, 0, size);
1251 * Replace libc function with one suited to our needs.
1258 /* do not worry about it for now */
1261 #else /* !STANDALONE */
1268 if (getrlimit(RLIMIT_DATA, &rlp) < 0)
1270 rlp.rlim_cur = rlp.rlim_max;
1271 if (setrlimit(RLIMIT_DATA, &rlp) < 0)
1276 extern char *_etext;
1277 #define etext _etext
1291 pgsz = getpagesize() - 1;
1292 dstart = ((u_long)&etext) &~ pgsz;
1293 freestart = ((u_long)(sbrk(0) + pgsz) &~ pgsz);
1294 if (getrlimit(RLIMIT_DATA, &rlp) < 0)
1296 memused = freestart - dstart;
1297 memleft = rlp.rlim_cur - memused;
1299 #endif /* STANDALONE */
1302 * read a block from the file system
1313 memmove(bf, membase + bno * sectorsize, size);
1316 if (lseek(fsi, (off_t)bno * sectorsize, 0) < 0) {
1317 printf("seek error: %ld\n", (long)bno);
1320 n = read(fsi, bf, size);
1322 printf("read error: %ld\n", (long)bno);
1328 * write a block to the file system
1339 memmove(membase + bno * sectorsize, bf, size);
1344 if (lseek(fso, (off_t)bno * sectorsize, SEEK_SET) < 0) {
1345 printf("seek error: %ld\n", (long)bno);
1348 n = write(fso, bf, size);
1350 printf("write error: %ld\n", (long)bno);
1356 * check if a block is available
1366 switch (fs->fs_frag) {
1368 return (cp[h] == 0xff);
1370 mask = 0x0f << ((h & 0x1) << 2);
1371 return ((cp[h >> 1] & mask) == mask);
1373 mask = 0x03 << ((h & 0x3) << 1);
1374 return ((cp[h >> 2] & mask) == mask);
1376 mask = 0x01 << (h & 0x7);
1377 return ((cp[h >> 3] & mask) == mask);
1380 printf("isblock bad fs_frag %d\n", fs->fs_frag);
1382 fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag);
1389 * take a block out of the map
1397 switch ((fs)->fs_frag) {
1402 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
1405 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
1408 cp[h >> 3] &= ~(0x01 << (h & 0x7));
1412 printf("clrblock bad fs_frag %d\n", fs->fs_frag);
1414 fprintf(stderr, "clrblock bad fs_frag %d\n", fs->fs_frag);
1421 * put a block into the map
1429 switch (fs->fs_frag) {
1434 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
1437 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
1440 cp[h >> 3] |= (0x01 << (h & 0x7));
1444 printf("setblock bad fs_frag %d\n", fs->fs_frag);
1446 fprintf(stderr, "setblock bad fs_frag %d\n", fs->fs_frag);
1453 * Determine the number of characters in a
1465 if (ioctl(0, TIOCGWINSZ, &ws) != -1)
1466 columns = ws.ws_col;
1467 if (columns == 0 && (cp = getenv("COLUMNS")))
1470 columns = 80; /* last resort */