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
33 * @(#)mkfs.c 8.11 (Berkeley) 5/3/95
34 * $FreeBSD: src/sbin/newfs/mkfs.c,v 1.29.2.6 2001/09/21 19:15:21 dillon Exp $
35 * $DragonFly: src/sbin/newfs/mkfs.c,v 1.14 2007/05/20 19:29:21 dillon Exp $
44 extern int atoi(char *);
45 extern char * getenv(char *);
48 extern long random(void);
49 extern void srandomdev(void);
52 #endif /* STANDALONE */
55 * make file system for cylinder-group style file systems
59 * We limit the size of the inode map to be no more than a
60 * third of the cylinder group space, since we must leave at
61 * least an equal amount of space for the block map.
63 * N.B.: MAXIPG must be a multiple of INOPB(fs).
65 #define MAXIPG(fs) roundup((fs)->fs_bsize * NBBY / 3, INOPB(fs))
68 #define MAXINOPB (MAXBSIZE / sizeof(struct ufs1_dinode))
69 #define POWEROF2(num) (((num) & ((num) - 1)) == 0)
72 * variables set up by front end.
74 extern int mfs; /* run as the memory based filesystem */
75 extern char *mfs_mtpt; /* mount point for mfs */
76 extern struct stat mfs_mtstat; /* stat prior to mount */
77 extern int Nflag; /* run mkfs without writing file system */
78 extern int Oflag; /* format as an 4.3BSD file system */
79 extern int Uflag; /* enable soft updates for file system */
80 extern int fssize; /* file system size */
81 extern int ntracks; /* # tracks/cylinder */
82 extern int nsectors; /* # sectors/track */
83 extern int nphyssectors; /* # sectors/track including spares */
84 extern int secpercyl; /* sectors per cylinder */
85 extern int sectorsize; /* bytes/sector */
86 extern int realsectorsize; /* bytes/sector in hardware*/
87 extern int rpm; /* revolutions/minute of drive */
88 extern int interleave; /* hardware sector interleave */
89 extern int trackskew; /* sector 0 skew, per track */
90 extern int fsize; /* fragment size */
91 extern int bsize; /* block size */
92 extern int cpg; /* cylinders/cylinder group */
93 extern int cpgflg; /* cylinders/cylinder group flag was given */
94 extern int minfree; /* free space threshold */
95 extern int opt; /* optimization preference (space or time) */
96 extern int density; /* number of bytes per inode */
97 extern int maxcontig; /* max contiguous blocks to allocate */
98 extern int rotdelay; /* rotational delay between blocks */
99 extern int maxbpg; /* maximum blocks per file in a cyl group */
100 extern int nrpos; /* # of distinguished rotational positions */
101 extern int bbsize; /* boot block size */
102 extern int sbsize; /* superblock size */
103 extern int avgfilesize; /* expected average file size */
104 extern int avgfilesperdir; /* expected number of files per directory */
105 extern u_long memleft; /* virtual memory available */
106 extern caddr_t membase; /* start address of memory based filesystem */
107 extern char * filename;
108 extern struct disktab geom;
110 extern void fatal(const char *fmt, ...);
116 #define sblock fsun.fs
125 struct ufs1_dinode zino[MAXBSIZE / sizeof(struct ufs1_dinode)];
128 static fsnode_t copyroot;
129 static fsnode_t copyhlinks;
133 daddr_t alloc(int, int);
134 long calcipg(long, long, off_t *);
135 static int charsperline(void);
136 void clrblock(struct fs *, unsigned char *, int);
138 void initcg(int, time_t);
139 int isblock(struct fs *, unsigned char *, int);
140 void iput(struct ufs1_dinode *, ino_t);
141 int makedir(struct direct *, int);
142 void parentready(int);
143 void rdfs(daddr_t, int, char *);
144 void setblock(struct fs *, unsigned char *, int);
146 void wtfs(daddr_t, int, char *);
147 void wtfsflush(void);
150 void get_memleft(void);
151 void raise_data_limit(void);
154 char * calloc(u_long, u_long);
155 caddr_t malloc(u_long);
156 caddr_t realloc(char *, u_long);
160 int parentready_signalled;
163 mkfs(char *fsys, int fi, int fo, const char *mfscopy)
165 long i, mincpc, mincpg, inospercg;
166 long cylno, rpos, blk, j, emitwarn = 0;
167 long used, mincpgcnt, bpcg;
169 long mapcramped, inodecramped;
170 long postblsize, rotblsize, totalsbsize;
175 char tmpbuf[100]; /* XXX this will break in about 2,500 years */
191 signal(SIGUSR1, parentready);
192 if ((child = fork()) != 0) {
199 copyroot = FSCopy(©hlinks, mfscopy);
200 signal(SIGUSR1, started);
201 kill(child, SIGUSR1);
202 while (waitpid(child, &status, 0) != child)
204 exit(WEXITSTATUS(status));
211 omask = sigblock(sigmask(SIGUSR1));
212 while (parentready_signalled == 0)
220 if (filename != NULL) {
221 unsigned char buf[BUFSIZ];
225 fd = open(filename, O_RDWR|O_TRUNC|O_CREAT, 0644);
227 err(12, "%s", filename);
228 l1 = fssize * sectorsize;
231 for (l = 0; l < (u_long)fssize * (u_long)sectorsize; l += l1) {
232 w = write(fd, buf, l1);
233 if (w < 0 || (u_long)w != l1)
234 err(12, "%s", filename);
239 PROT_READ|PROT_WRITE,
243 if(membase == MAP_FAILED)
250 if ((u_long)fssize * (u_long)sectorsize >
252 fssize = (memleft - 131072) / sectorsize;
253 if ((membase = malloc(fssize * sectorsize)) == NULL)
254 errx(13, "malloc failed");
260 sblock.fs_inodefmt = FS_42INODEFMT;
261 sblock.fs_maxsymlinklen = 0;
263 sblock.fs_inodefmt = FS_44INODEFMT;
264 sblock.fs_maxsymlinklen = MAXSYMLINKLEN;
267 sblock.fs_flags |= FS_DOSOFTDEP;
269 * Validate the given file system size.
270 * Verify that its last block can actually be accessed.
273 printf("preposterous size %d\n", fssize), exit(13);
274 wtfs(fssize - (realsectorsize / DEV_BSIZE), realsectorsize,
277 * collect and verify the sector and track info
279 sblock.fs_nsect = nsectors;
280 sblock.fs_ntrak = ntracks;
281 if (sblock.fs_ntrak <= 0)
282 printf("preposterous ntrak %d\n", sblock.fs_ntrak), exit(14);
283 if (sblock.fs_nsect <= 0)
284 printf("preposterous nsect %d\n", sblock.fs_nsect), exit(15);
286 * collect and verify the filesystem density info
288 sblock.fs_avgfilesize = avgfilesize;
289 sblock.fs_avgfpdir = avgfilesperdir;
290 if (sblock.fs_avgfilesize <= 0)
291 printf("illegal expected average file size %d\n",
292 sblock.fs_avgfilesize), exit(14);
293 if (sblock.fs_avgfpdir <= 0)
294 printf("illegal expected number of files per directory %d\n",
295 sblock.fs_avgfpdir), exit(15);
297 * collect and verify the block and fragment sizes
299 sblock.fs_bsize = bsize;
300 sblock.fs_fsize = fsize;
301 if (!POWEROF2(sblock.fs_bsize)) {
302 printf("block size must be a power of 2, not %d\n",
306 if (!POWEROF2(sblock.fs_fsize)) {
307 printf("fragment size must be a power of 2, not %d\n",
311 if (sblock.fs_fsize < sectorsize) {
312 printf("fragment size %d is too small, minimum is %d\n",
313 sblock.fs_fsize, sectorsize);
316 if (sblock.fs_bsize < MINBSIZE) {
317 printf("block size %d is too small, minimum is %d\n",
318 sblock.fs_bsize, MINBSIZE);
321 if (sblock.fs_bsize < sblock.fs_fsize) {
322 printf("block size (%d) cannot be smaller than fragment size (%d)\n",
323 sblock.fs_bsize, sblock.fs_fsize);
326 sblock.fs_bmask = ~(sblock.fs_bsize - 1);
327 sblock.fs_fmask = ~(sblock.fs_fsize - 1);
328 sblock.fs_qbmask = ~sblock.fs_bmask;
329 sblock.fs_qfmask = ~sblock.fs_fmask;
330 for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1)
332 for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1)
334 sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize);
335 for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1)
336 sblock.fs_fragshift++;
337 if (sblock.fs_frag > MAXFRAG) {
338 printf("fragment size %d is too small, minimum with block size %d is %d\n",
339 sblock.fs_fsize, sblock.fs_bsize,
340 sblock.fs_bsize / MAXFRAG);
343 sblock.fs_nrpos = nrpos;
344 sblock.fs_nindir = sblock.fs_bsize / sizeof(daddr_t);
345 sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs1_dinode);
346 sblock.fs_nspf = sblock.fs_fsize / sectorsize;
347 for (sblock.fs_fsbtodb = 0, i = NSPF(&sblock); i > 1; i >>= 1)
350 roundup(howmany(bbsize + sbsize, sblock.fs_fsize), sblock.fs_frag);
351 sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno +
352 roundup(howmany(sbsize, sblock.fs_fsize), sblock.fs_frag));
353 sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag;
354 sblock.fs_cgoffset = roundup(
355 howmany(sblock.fs_nsect, NSPF(&sblock)), sblock.fs_frag);
356 for (sblock.fs_cgmask = 0xffffffff, i = sblock.fs_ntrak; i > 1; i >>= 1)
357 sblock.fs_cgmask <<= 1;
358 if (!POWEROF2(sblock.fs_ntrak))
359 sblock.fs_cgmask <<= 1;
360 sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
361 for (sizepb = sblock.fs_bsize, i = 0; i < NIADDR; i++) {
362 sizepb *= NINDIR(&sblock);
363 sblock.fs_maxfilesize += sizepb;
366 * Validate specified/determined secpercyl
367 * and calculate minimum cylinders per group.
369 sblock.fs_spc = secpercyl;
370 for (sblock.fs_cpc = NSPB(&sblock), i = sblock.fs_spc;
371 sblock.fs_cpc > 1 && (i & 1) == 0;
372 sblock.fs_cpc >>= 1, i >>= 1)
374 mincpc = sblock.fs_cpc;
375 bpcg = sblock.fs_spc * sectorsize;
376 inospercg = roundup(bpcg / sizeof(struct ufs1_dinode), INOPB(&sblock));
377 if (inospercg > MAXIPG(&sblock))
378 inospercg = MAXIPG(&sblock);
379 used = (sblock.fs_iblkno + inospercg / INOPF(&sblock)) * NSPF(&sblock);
380 mincpgcnt = howmany(sblock.fs_cgoffset * (~sblock.fs_cgmask) + used,
382 mincpg = roundup(mincpgcnt, mincpc);
384 * Ensure that cylinder group with mincpg has enough space
387 sblock.fs_cpg = mincpg;
388 sblock.fs_ipg = inospercg;
390 sblock.fs_contigsumsize = MIN(maxcontig, FS_MAXCONTIG);
392 while (CGSIZE(&sblock) > (uint32_t)sblock.fs_bsize) {
394 if (sblock.fs_bsize < MAXBSIZE) {
395 sblock.fs_bsize <<= 1;
401 mincpg = roundup(mincpgcnt, mincpc);
402 sblock.fs_cpg = mincpg;
404 sblock.fs_frag <<= 1;
405 sblock.fs_fragshift += 1;
406 if (sblock.fs_frag <= MAXFRAG)
409 if (sblock.fs_fsize == sblock.fs_bsize) {
410 printf("There is no block size that");
411 printf(" can support this disk\n");
414 sblock.fs_frag >>= 1;
415 sblock.fs_fragshift -= 1;
416 sblock.fs_fsize <<= 1;
417 sblock.fs_nspf <<= 1;
420 * Ensure that cylinder group with mincpg has enough space for inodes.
423 inospercg = calcipg(mincpg, bpcg, &usedb);
424 sblock.fs_ipg = inospercg;
425 while (inospercg > MAXIPG(&sblock)) {
427 if (mincpc == 1 || sblock.fs_frag == 1 ||
428 sblock.fs_bsize == MINBSIZE)
430 printf("With a block size of %d %s %d\n", sblock.fs_bsize,
431 "minimum bytes per inode is",
432 (int)((mincpg * (off_t)bpcg - usedb)
433 / MAXIPG(&sblock) + 1));
434 sblock.fs_bsize >>= 1;
435 sblock.fs_frag >>= 1;
436 sblock.fs_fragshift -= 1;
438 sblock.fs_cpg = roundup(mincpgcnt, mincpc);
439 if (CGSIZE(&sblock) > (uint32_t)sblock.fs_bsize) {
440 sblock.fs_bsize <<= 1;
443 mincpg = sblock.fs_cpg;
444 inospercg = calcipg(mincpg, bpcg, &usedb);
445 sblock.fs_ipg = inospercg;
448 if (inospercg > MAXIPG(&sblock)) {
449 printf("Minimum bytes per inode is %d\n",
450 (int)((mincpg * (off_t)bpcg - usedb)
451 / MAXIPG(&sblock) + 1));
452 } else if (!mapcramped) {
453 printf("With %d bytes per inode, ", density);
454 printf("minimum cylinders per group is %ld\n", mincpg);
458 printf("With %d sectors per cylinder, ", sblock.fs_spc);
459 printf("minimum cylinders per group is %ld\n", mincpg);
461 if (inodecramped || mapcramped) {
462 if (sblock.fs_bsize != bsize)
463 printf("%s to be changed from %d to %d\n",
464 "This requires the block size",
465 bsize, sblock.fs_bsize);
466 if (sblock.fs_fsize != fsize)
467 printf("\t%s to be changed from %d to %d\n",
468 "and the fragment size",
469 fsize, sblock.fs_fsize);
473 * Calculate the number of cylinders per group
476 if (sblock.fs_cpg % mincpc != 0) {
477 printf("%s groups must have a multiple of %ld cylinders\n",
478 cpgflg ? "Cylinder" : "Warning: cylinder", mincpc);
479 sblock.fs_cpg = roundup(sblock.fs_cpg, mincpc);
484 * Must ensure there is enough space for inodes.
486 sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
487 while (sblock.fs_ipg > MAXIPG(&sblock)) {
489 sblock.fs_cpg -= mincpc;
490 sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
493 * Must ensure there is enough space to hold block map.
495 while (CGSIZE(&sblock) > (uint32_t)sblock.fs_bsize) {
497 sblock.fs_cpg -= mincpc;
498 sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
500 sblock.fs_fpg = (sblock.fs_cpg * sblock.fs_spc) / NSPF(&sblock);
501 if ((sblock.fs_cpg * sblock.fs_spc) % NSPB(&sblock) != 0) {
502 printf("panic (fs_cpg * fs_spc) %% NSPF != 0");
505 if (sblock.fs_cpg < mincpg) {
506 printf("cylinder groups must have at least %ld cylinders\n",
509 } else if (sblock.fs_cpg != cpg) {
512 else if (!mapcramped && !inodecramped)
515 if (mapcramped && inodecramped)
516 printf("Block size and bytes per inode restrict");
518 printf("Block size restricts");
520 printf("Bytes per inode restrict");
521 printf(" cylinders per group to %d.\n", sblock.fs_cpg);
526 sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock));
528 * Now have size for file system and nsect and ntrak.
529 * Determine number of cylinders and blocks in the file system.
531 sblock.fs_size = fssize = dbtofsb(&sblock, fssize);
532 sblock.fs_ncyl = fssize * NSPF(&sblock) / sblock.fs_spc;
533 if (fssize * NSPF(&sblock) > sblock.fs_ncyl * sblock.fs_spc) {
537 if (sblock.fs_ncyl < 1) {
538 printf("file systems must have at least one cylinder\n");
542 * Determine feasability/values of rotational layout tables.
544 * The size of the rotational layout tables is limited by the
545 * size of the superblock, SBSIZE. The amount of space available
546 * for tables is calculated as (SBSIZE - sizeof (struct fs)).
547 * The size of these tables is inversely proportional to the block
548 * size of the file system. The size increases if sectors per track
549 * are not powers of two, because more cylinders must be described
550 * by the tables before the rotational pattern repeats (fs_cpc).
552 sblock.fs_interleave = interleave;
553 sblock.fs_trackskew = trackskew;
554 sblock.fs_npsect = nphyssectors;
555 sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
556 sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs));
557 if (sblock.fs_sbsize > SBSIZE)
558 sblock.fs_sbsize = SBSIZE;
559 if (sblock.fs_ntrak == 1) {
563 postblsize = sblock.fs_nrpos * sblock.fs_cpc * sizeof(int16_t);
564 rotblsize = sblock.fs_cpc * sblock.fs_spc / NSPB(&sblock);
565 totalsbsize = sizeof(struct fs) + rotblsize;
566 if (sblock.fs_nrpos == 8 && sblock.fs_cpc <= 16) {
567 /* use old static table space */
568 sblock.fs_postbloff = (char *)(&sblock.fs_opostbl[0][0]) -
569 (char *)(&sblock.fs_firstfield);
570 sblock.fs_rotbloff = &sblock.fs_space[0] -
571 (u_char *)(&sblock.fs_firstfield);
573 /* use dynamic table space */
574 sblock.fs_postbloff = &sblock.fs_space[0] -
575 (u_char *)(&sblock.fs_firstfield);
576 sblock.fs_rotbloff = sblock.fs_postbloff + postblsize;
577 totalsbsize += postblsize;
579 if (totalsbsize > SBSIZE ||
580 sblock.fs_nsect > (1 << NBBY) * NSPB(&sblock)) {
581 printf("%s %s %d %s %d.%s",
582 "Warning: insufficient space in super block for\n",
583 "rotational layout tables with nsect", sblock.fs_nsect,
584 "and ntrak", sblock.fs_ntrak,
585 "\nFile system performance may be impaired.\n");
589 sblock.fs_sbsize = fragroundup(&sblock, totalsbsize);
590 if (sblock.fs_sbsize > SBSIZE)
591 sblock.fs_sbsize = SBSIZE;
593 * calculate the available blocks for each rotational position
595 for (cylno = 0; cylno < sblock.fs_cpc; cylno++)
596 for (rpos = 0; rpos < sblock.fs_nrpos; rpos++)
597 fs_postbl(&sblock, cylno)[rpos] = -1;
598 for (i = (rotblsize - 1) * sblock.fs_frag;
599 i >= 0; i -= sblock.fs_frag) {
600 cylno = cbtocylno(&sblock, i);
601 rpos = cbtorpos(&sblock, i);
602 blk = fragstoblks(&sblock, i);
603 if (fs_postbl(&sblock, cylno)[rpos] == -1)
604 fs_rotbl(&sblock)[blk] = 0;
606 fs_rotbl(&sblock)[blk] =
607 fs_postbl(&sblock, cylno)[rpos] - blk;
608 fs_postbl(&sblock, cylno)[rpos] = blk;
612 * Compute/validate number of cylinder groups.
614 sblock.fs_ncg = sblock.fs_ncyl / sblock.fs_cpg;
615 if (sblock.fs_ncyl % sblock.fs_cpg)
617 sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock);
618 i = MIN(~sblock.fs_cgmask, sblock.fs_ncg - 1);
619 if (cgdmin(&sblock, i) - cgbase(&sblock, i) >= sblock.fs_fpg) {
620 printf("inode blocks/cyl group (%ld) >= data blocks (%ld)\n",
621 cgdmin(&sblock, i) - cgbase(&sblock, i) / sblock.fs_frag,
622 (long)(sblock.fs_fpg / sblock.fs_frag));
623 printf("number of cylinders per cylinder group (%d) %s.\n",
624 sblock.fs_cpg, "must be increased");
627 j = sblock.fs_ncg - 1;
628 if ((i = fssize - j * sblock.fs_fpg) < sblock.fs_fpg &&
629 cgdmin(&sblock, j) - cgbase(&sblock, j) > i) {
631 printf("Filesystem must have at least %d sectors\n",
633 (cgdmin(&sblock, 0) + 3 * sblock.fs_frag));
637 "Warning: inode blocks/cyl group (%ld) >= data blocks (%ld) in last\n",
638 (cgdmin(&sblock, j) - cgbase(&sblock, j)) / sblock.fs_frag,
641 " cylinder group. This implies %ld sector(s) cannot be allocated.\n",
644 sblock.fs_ncyl -= sblock.fs_ncyl % sblock.fs_cpg;
645 sblock.fs_size = fssize = sblock.fs_ncyl * sblock.fs_spc /
649 if (emitwarn && !mfs) {
650 printf("Warning: %d sector(s) in last cylinder unallocated\n",
652 (fssize * NSPF(&sblock) - (sblock.fs_ncyl - 1)
656 * fill in remaining fields of the super block
658 sblock.fs_csaddr = cgdmin(&sblock, 0);
660 fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
662 * The superblock fields 'fs_csmask' and 'fs_csshift' are no
663 * longer used. However, we still initialise them so that the
664 * filesystem remains compatible with old kernels.
666 i = sblock.fs_bsize / sizeof(struct csum);
667 sblock.fs_csmask = ~(i - 1);
668 for (sblock.fs_csshift = 0; i > 1; i >>= 1)
670 fscs = (struct csum *)calloc(1, sblock.fs_cssize);
672 errx(31, "calloc failed");
673 sblock.fs_magic = FS_MAGIC;
674 sblock.fs_rotdelay = rotdelay;
675 sblock.fs_minfree = minfree;
676 sblock.fs_maxcontig = maxcontig;
677 sblock.fs_maxbpg = maxbpg;
678 sblock.fs_rps = rpm / 60;
679 sblock.fs_optim = opt;
680 sblock.fs_cgrotor = 0;
681 sblock.fs_cstotal.cs_ndir = 0;
682 sblock.fs_cstotal.cs_nbfree = 0;
683 sblock.fs_cstotal.cs_nifree = 0;
684 sblock.fs_cstotal.cs_nffree = 0;
689 sblock.fs_id[0] = (long)utime;
690 sblock.fs_id[1] = random();
694 * Dump out summary information about file system.
697 printf("%s:\t%d sectors in %d %s of %d tracks, %d sectors\n",
698 fsys, sblock.fs_size * NSPF(&sblock), sblock.fs_ncyl,
699 "cylinders", sblock.fs_ntrak, sblock.fs_nsect);
700 #define B2MBFACTOR (1 / (1024.0 * 1024.0))
701 printf("\t%.1fMB in %d cyl groups (%d c/g, %.2fMB/g, %d i/g)%s\n",
702 (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
703 sblock.fs_ncg, sblock.fs_cpg,
704 (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
706 sblock.fs_flags & FS_DOSOFTDEP ? " SOFTUPDATES" : "");
710 * Now build the cylinders group blocks and
711 * then print out indices of cylinder groups.
714 printf("super-block backups (for fsck -b #) at:\n");
716 width = charsperline();
717 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
718 initcg(cylno, utime);
721 j = snprintf(tmpbuf, sizeof(tmpbuf), " %ld%s",
722 fsbtodb(&sblock, cgsblock(&sblock, cylno)),
723 cylno < (sblock.fs_ncg-1) ? "," : "" );
724 if (i + j >= width) {
729 printf("%s", tmpbuf);
737 * Now construct the initial file system,
738 * then write out the super-block.
741 sblock.fs_time = utime;
742 wtfs((int)SBOFF / sectorsize, sbsize, (char *)&sblock);
743 for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize)
744 wtfs(fsbtodb(&sblock, sblock.fs_csaddr + numfrags(&sblock, i)),
745 sblock.fs_cssize - i < sblock.fs_bsize ?
746 sblock.fs_cssize - i : sblock.fs_bsize,
749 * Write out the duplicate super blocks
751 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
752 wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)),
753 sbsize, (char *)&sblock);
757 * NOTE: we no longer update information in the disklabel
761 * Notify parent process of success.
762 * Dissociate from session and tty.
764 * NOTE: We are the child and may receive a SIGINT due
765 * to losing the tty session? XXX
769 kill(mfs_ppid, SIGUSR1);
775 /* returns to mount_mfs (newfs) and issues the mount */
780 * Initialize a cylinder group.
783 initcg(int cylno, time_t utime)
785 daddr_t cbase, d, dlower, dupper, dmax, blkno;
794 * Determine block bounds for cylinder group.
795 * Allow space for super block summary information in first
798 cbase = cgbase(&sblock, cylno);
799 dmax = cbase + sblock.fs_fpg;
800 if (dmax > sblock.fs_size)
801 dmax = sblock.fs_size;
802 dlower = cgsblock(&sblock, cylno) - cbase;
803 dupper = cgdmin(&sblock, cylno) - cbase;
805 dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
807 memset(&acg, 0, sblock.fs_cgsize);
809 acg.cg_magic = CG_MAGIC;
811 if (cylno == sblock.fs_ncg - 1)
812 acg.cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg;
814 acg.cg_ncyl = sblock.fs_cpg;
815 acg.cg_niblk = sblock.fs_ipg;
816 acg.cg_ndblk = dmax - cbase;
817 if (sblock.fs_contigsumsize > 0)
818 acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag;
819 acg.cg_btotoff = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield);
820 acg.cg_boff = acg.cg_btotoff + sblock.fs_cpg * sizeof(int32_t);
821 acg.cg_iusedoff = acg.cg_boff +
822 sblock.fs_cpg * sblock.fs_nrpos * sizeof(u_int16_t);
823 acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, NBBY);
824 if (sblock.fs_contigsumsize <= 0) {
825 acg.cg_nextfreeoff = acg.cg_freeoff +
826 howmany(sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY);
828 acg.cg_clustersumoff = acg.cg_freeoff + howmany
829 (sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY) -
831 acg.cg_clustersumoff =
832 roundup(acg.cg_clustersumoff, sizeof(u_int32_t));
833 acg.cg_clusteroff = acg.cg_clustersumoff +
834 (sblock.fs_contigsumsize + 1) * sizeof(u_int32_t);
835 acg.cg_nextfreeoff = acg.cg_clusteroff + howmany
836 (sblock.fs_cpg * sblock.fs_spc / NSPB(&sblock), NBBY);
838 if (acg.cg_nextfreeoff - (long)(&acg.cg_firstfield) > sblock.fs_cgsize) {
839 printf("Panic: cylinder group too big\n");
842 acg.cg_cs.cs_nifree += sblock.fs_ipg;
844 for (k = 0; k < ROOTINO; k++) {
845 setbit(cg_inosused(&acg), k);
846 acg.cg_cs.cs_nifree--;
849 for (i = 0; i < sblock.fs_ipg / INOPF(&sblock); i += sblock.fs_frag) {
852 j < sblock.fs_bsize / sizeof(struct ufs1_dinode);
854 zino[j].di_gen = random();
857 wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
858 sblock.fs_bsize, (char *)zino);
862 * In cylno 0, beginning space is reserved
863 * for boot and super blocks.
865 for (d = 0; d < dlower; d += sblock.fs_frag) {
866 blkno = d / sblock.fs_frag;
867 setblock(&sblock, cg_blksfree(&acg), blkno);
868 if (sblock.fs_contigsumsize > 0)
869 setbit(cg_clustersfree(&acg), blkno);
870 acg.cg_cs.cs_nbfree++;
871 cg_blktot(&acg)[cbtocylno(&sblock, d)]++;
872 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))
873 [cbtorpos(&sblock, d)]++;
875 sblock.fs_dsize += dlower;
877 sblock.fs_dsize += acg.cg_ndblk - dupper;
878 if ((i = dupper % sblock.fs_frag)) {
879 acg.cg_frsum[sblock.fs_frag - i]++;
880 for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
881 setbit(cg_blksfree(&acg), dupper);
882 acg.cg_cs.cs_nffree++;
885 for (d = dupper; d + sblock.fs_frag <= dmax - cbase; ) {
886 blkno = d / sblock.fs_frag;
887 setblock(&sblock, cg_blksfree(&acg), blkno);
888 if (sblock.fs_contigsumsize > 0)
889 setbit(cg_clustersfree(&acg), blkno);
890 acg.cg_cs.cs_nbfree++;
891 cg_blktot(&acg)[cbtocylno(&sblock, d)]++;
892 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))
893 [cbtorpos(&sblock, d)]++;
896 if (d < dmax - cbase) {
897 acg.cg_frsum[dmax - cbase - d]++;
898 for (; d < dmax - cbase; d++) {
899 setbit(cg_blksfree(&acg), d);
900 acg.cg_cs.cs_nffree++;
903 if (sblock.fs_contigsumsize > 0) {
904 int32_t *sump = cg_clustersum(&acg);
905 u_char *mapp = cg_clustersfree(&acg);
910 for (i = 0; i < acg.cg_nclusterblks; i++) {
911 if ((map & bit) != 0) {
913 } else if (run != 0) {
914 if (run > sblock.fs_contigsumsize)
915 run = sblock.fs_contigsumsize;
919 if ((i & (NBBY - 1)) != (NBBY - 1)) {
927 if (run > sblock.fs_contigsumsize)
928 run = sblock.fs_contigsumsize;
932 sblock.fs_cstotal.cs_ndir += acg.cg_cs.cs_ndir;
933 sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree;
934 sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree;
935 sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree;
937 wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
938 sblock.fs_bsize, (char *)&acg);
942 * initialize the file system
944 struct ufs1_dinode node;
952 struct direct root_dir[] = {
953 { ROOTINO, sizeof(struct direct), DT_DIR, 1, "." },
954 { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
956 { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 10, "lost+found" },
963 u_char d_name[MAXNAMLEN + 1];
965 { ROOTINO, sizeof(struct direct), 1, "." },
966 { ROOTINO, sizeof(struct direct), 2, ".." },
968 { LOSTFOUNDINO, sizeof(struct direct), 10, "lost+found" },
972 struct direct lost_found_dir[] = {
973 { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 1, "." },
974 { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
975 { 0, DIRBLKSIZ, 0, 0, 0 },
977 struct odirect olost_found_dir[] = {
978 { LOSTFOUNDINO, sizeof(struct direct), 1, "." },
979 { ROOTINO, sizeof(struct direct), 2, ".." },
980 { 0, DIRBLKSIZ, 0, 0 },
993 * initialize the node
995 node.di_atime = utime;
996 node.di_mtime = utime;
997 node.di_ctime = utime;
1000 * create the lost+found directory
1003 makedir((struct direct *)olost_found_dir, 2);
1004 for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ)
1005 memmove(&buf[i], &olost_found_dir[2],
1006 DIRSIZ(0, &olost_found_dir[2]));
1008 makedir(lost_found_dir, 2);
1009 for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ)
1010 memmove(&buf[i], &lost_found_dir[2],
1011 DIRSIZ(0, &lost_found_dir[2]));
1013 node.di_mode = IFDIR | UMASK;
1015 node.di_size = sblock.fs_bsize;
1016 node.di_db[0] = alloc(node.di_size, node.di_mode);
1017 node.di_blocks = btodb(fragroundup(&sblock, node.di_size));
1018 wtfs(fsbtodb(&sblock, node.di_db[0]), node.di_size, buf);
1019 iput(&node, LOSTFOUNDINO);
1022 * create the root directory
1025 node.di_mode = IFDIR | 01777;
1027 node.di_mode = IFDIR | UMASK;
1028 node.di_nlink = PREDEFDIR;
1030 node.di_size = makedir((struct direct *)oroot_dir, PREDEFDIR);
1032 node.di_size = makedir(root_dir, PREDEFDIR);
1033 node.di_db[0] = alloc(sblock.fs_fsize, node.di_mode);
1034 node.di_blocks = btodb(fragroundup(&sblock, node.di_size));
1035 wtfs(fsbtodb(&sblock, node.di_db[0]), sblock.fs_fsize, buf);
1036 iput(&node, ROOTINO);
1040 * construct a set of directory entries in "buf".
1041 * return size of directory.
1044 makedir(struct direct *protodir, int entries)
1049 spcleft = DIRBLKSIZ;
1050 for (cp = buf, i = 0; i < entries - 1; i++) {
1051 protodir[i].d_reclen = DIRSIZ(0, &protodir[i]);
1052 memmove(cp, &protodir[i], protodir[i].d_reclen);
1053 cp += protodir[i].d_reclen;
1054 spcleft -= protodir[i].d_reclen;
1056 protodir[i].d_reclen = spcleft;
1057 memmove(cp, &protodir[i], DIRSIZ(0, &protodir[i]));
1062 * allocate a block or frag
1065 alloc(int size, int mode)
1070 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
1072 if (acg.cg_magic != CG_MAGIC) {
1073 printf("cg 0: bad magic number\n");
1076 if (acg.cg_cs.cs_nbfree == 0) {
1077 printf("first cylinder group ran out of space\n");
1080 for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag)
1081 if (isblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag))
1083 printf("internal error: can't find block in cyl 0\n");
1086 blkno = fragstoblks(&sblock, d);
1087 clrblock(&sblock, cg_blksfree(&acg), blkno);
1088 if (sblock.fs_contigsumsize > 0)
1089 clrbit(cg_clustersfree(&acg), blkno);
1090 acg.cg_cs.cs_nbfree--;
1091 sblock.fs_cstotal.cs_nbfree--;
1092 fscs[0].cs_nbfree--;
1094 acg.cg_cs.cs_ndir++;
1095 sblock.fs_cstotal.cs_ndir++;
1098 cg_blktot(&acg)[cbtocylno(&sblock, d)]--;
1099 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))[cbtorpos(&sblock, d)]--;
1100 if (size != sblock.fs_bsize) {
1101 frag = howmany(size, sblock.fs_fsize);
1102 fscs[0].cs_nffree += sblock.fs_frag - frag;
1103 sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag;
1104 acg.cg_cs.cs_nffree += sblock.fs_frag - frag;
1105 acg.cg_frsum[sblock.fs_frag - frag]++;
1106 for (i = frag; i < sblock.fs_frag; i++)
1107 setbit(cg_blksfree(&acg), d + i);
1109 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
1115 * Calculate number of inodes per group.
1118 calcipg(long cylspg, long bpcg, off_t *usedbp)
1121 long ipg, new_ipg, ncg, ncyl;
1125 * Prepare to scale by fssize / (number of sectors in cylinder groups).
1126 * Note that fssize is still in sectors, not filesystem blocks.
1128 ncyl = howmany(fssize, (u_int)secpercyl);
1129 ncg = howmany(ncyl, cylspg);
1131 * Iterate a few times to allow for ipg depending on itself.
1134 for (i = 0; i < 10; i++) {
1135 usedb = (sblock.fs_iblkno + ipg / INOPF(&sblock))
1136 * NSPF(&sblock) * (off_t)sectorsize;
1137 new_ipg = (cylspg * (quad_t)bpcg - usedb) / density * fssize
1138 / ncg / secpercyl / cylspg;
1139 new_ipg = roundup(new_ipg, INOPB(&sblock));
1149 * Allocate an inode on the disk
1152 iput(struct ufs1_dinode *ip, ino_t ino)
1154 struct ufs1_dinode inobuf[MAXINOPB];
1159 ip->di_gen = random();
1161 c = ino_to_cg(&sblock, ino);
1162 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
1164 if (acg.cg_magic != CG_MAGIC) {
1165 printf("cg 0: bad magic number\n");
1168 acg.cg_cs.cs_nifree--;
1169 setbit(cg_inosused(&acg), ino);
1170 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
1172 sblock.fs_cstotal.cs_nifree--;
1173 fscs[0].cs_nifree--;
1174 if (ino >= (uint32_t)sblock.fs_ipg * (uint32_t)sblock.fs_ncg) {
1175 printf("fsinit: inode value out of range (%ju).\n",
1179 d = fsbtodb(&sblock, ino_to_fsba(&sblock, ino));
1180 rdfs(d, sblock.fs_bsize, (char *)inobuf);
1181 inobuf[ino_to_fsbo(&sblock, ino)] = *ip;
1182 wtfs(d, sblock.fs_bsize, (char *)inobuf);
1186 * Parent notifies child that it can proceed with the newfs and mount
1187 * operation (occurs after parent has copied the underlying filesystem
1188 * if the -C option was specified (for MFS), or immediately after the
1189 * parent forked the child otherwise).
1192 parentready(__unused int signo)
1194 parentready_signalled = 1;
1198 * Notify parent process that the filesystem has created itself successfully.
1200 * We have to wait until the mount has actually completed!
1203 started(__unused int signo)
1205 int retry = 100; /* 10 seconds, 100ms */
1207 while (mfs_ppid && retry) {
1211 stat(mfs_mtpt, &st) < 0 ||
1212 st.st_dev != mfs_mtstat.st_dev
1220 fatal("mfs mount failed waiting for mount to go active");
1221 } else if (copyroot) {
1222 FSPaste(mfs_mtpt, copyroot, copyhlinks);
1229 * Replace libc function with one suited to our needs.
1240 pgsz = getpagesize() - 1;
1241 i = (char *)((u_long)(base + pgsz) &~ pgsz);
1242 base = sbrk(i - base);
1243 if (getrlimit(RLIMIT_DATA, &rlp) < 0)
1245 rlp.rlim_cur = rlp.rlim_max;
1246 if (setrlimit(RLIMIT_DATA, &rlp) < 0)
1248 memleft = rlp.rlim_max - (u_long)base;
1250 size = (size + pgsz) &~ pgsz;
1256 return ((caddr_t)sbrk(size));
1260 * Replace libc function with one suited to our needs.
1263 realloc(char *ptr, u_long size)
1267 if ((p = malloc(size)) == NULL)
1269 memmove(p, ptr, size);
1275 * Replace libc function with one suited to our needs.
1278 calloc(u_long size, u_long numelm)
1283 if ((base = malloc(size)) == NULL)
1285 memset(base, 0, size);
1290 * Replace libc function with one suited to our needs.
1296 /* do not worry about it for now */
1299 #else /* !STANDALONE */
1302 raise_data_limit(void)
1306 if (getrlimit(RLIMIT_DATA, &rlp) < 0)
1308 rlp.rlim_cur = rlp.rlim_max;
1309 if (setrlimit(RLIMIT_DATA, &rlp) < 0)
1314 extern char *_etext;
1315 #define etext _etext
1329 pgsz = getpagesize() - 1;
1330 dstart = ((u_long)&etext) &~ pgsz;
1331 freestart = ((u_long)((char *)sbrk(0) + pgsz) &~ pgsz);
1332 if (getrlimit(RLIMIT_DATA, &rlp) < 0)
1334 memused = freestart - dstart;
1335 memleft = rlp.rlim_cur - memused;
1337 #endif /* STANDALONE */
1340 * read a block from the file system
1343 rdfs(daddr_t bno, int size, char *bf)
1349 memmove(bf, membase + bno * sectorsize, size);
1352 if (lseek(fsi, (off_t)bno * sectorsize, 0) < 0) {
1353 printf("seek error: %ld\n", (long)bno);
1356 n = read(fsi, bf, size);
1358 printf("read error: %ld\n", (long)bno);
1363 #define WCSIZE (128 * 1024)
1364 daddr_t wc_sect; /* units of sectorsize */
1365 int wc_end; /* bytes */
1366 static char wc[WCSIZE]; /* bytes */
1369 * Flush dirty write behind buffer.
1376 if (lseek(fso, (off_t)wc_sect * sectorsize, SEEK_SET) < 0) {
1377 printf("seek error: %ld\n", (long)wc_sect);
1378 err(35, "wtfs - writecombine");
1380 n = write(fso, wc, wc_end);
1382 printf("write error: %ld\n", (long)wc_sect);
1383 err(36, "wtfs - writecombine");
1390 * write a block to the file system
1393 wtfs(daddr_t bno, int size, char *bf)
1399 memmove(membase + bno * sectorsize, bf, size);
1405 if (wc_end == 0 && size <= WCSIZE) {
1407 bcopy(bf, wc, size);
1409 if (wc_end < WCSIZE)
1413 if ((off_t)wc_sect * sectorsize + wc_end == (off_t)bno * sectorsize &&
1414 wc_end + size <= WCSIZE) {
1415 bcopy(bf, wc + wc_end, size);
1417 if (wc_end < WCSIZE)
1424 if (lseek(fso, (off_t)bno * sectorsize, SEEK_SET) < 0) {
1425 printf("seek error: %ld\n", (long)bno);
1428 n = write(fso, bf, size);
1430 printf("write error: fso %d blk %ld %d/%d\n",
1431 fso, (long)bno, n, size);
1437 * check if a block is available
1440 isblock(struct fs *fs, unsigned char *cp, int h)
1444 switch (fs->fs_frag) {
1446 return (cp[h] == 0xff);
1448 mask = 0x0f << ((h & 0x1) << 2);
1449 return ((cp[h >> 1] & mask) == mask);
1451 mask = 0x03 << ((h & 0x3) << 1);
1452 return ((cp[h >> 2] & mask) == mask);
1454 mask = 0x01 << (h & 0x7);
1455 return ((cp[h >> 3] & mask) == mask);
1458 printf("isblock bad fs_frag %d\n", fs->fs_frag);
1460 fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag);
1467 * take a block out of the map
1470 clrblock(struct fs *fs, unsigned char *cp, int h)
1472 switch ((fs)->fs_frag) {
1477 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
1480 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
1483 cp[h >> 3] &= ~(0x01 << (h & 0x7));
1487 printf("clrblock bad fs_frag %d\n", fs->fs_frag);
1489 fprintf(stderr, "clrblock bad fs_frag %d\n", fs->fs_frag);
1496 * put a block into the map
1499 setblock(struct fs *fs, unsigned char *cp, int h)
1501 switch (fs->fs_frag) {
1506 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
1509 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
1512 cp[h >> 3] |= (0x01 << (h & 0x7));
1516 printf("setblock bad fs_frag %d\n", fs->fs_frag);
1518 fprintf(stderr, "setblock bad fs_frag %d\n", fs->fs_frag);
1525 * Determine the number of characters in a
1537 if (ioctl(0, TIOCGWINSZ, &ws) != -1)
1538 columns = ws.ws_col;
1539 if (columns == 0 && (cp = getenv("COLUMNS")))
1542 columns = 80; /* last resort */