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 $
43 * make file system for cylinder-group style file systems
47 * We limit the size of the inode map to be no more than a
48 * third of the cylinder group space, since we must leave at
49 * least an equal amount of space for the block map.
51 * N.B.: MAXIPG must be a multiple of INOPB(fs).
53 #define MAXIPG(fs) roundup((fs)->fs_bsize * NBBY / 3, INOPB(fs))
56 #define MAXINOPB (MAXBSIZE / sizeof(struct ufs1_dinode))
57 #define POWEROF2(num) (((num) & ((num) - 1)) == 0)
60 #error "mkfs.c: STANDALONE compilation no longer supported"
64 * variables set up by front end.
66 extern int mfs; /* run as the memory based filesystem */
67 extern char *mfs_mtpt; /* mount point for mfs */
68 extern struct stat mfs_mtstat; /* stat prior to mount */
69 extern int Lflag; /* add a volume label */
70 extern int Nflag; /* run mkfs without writing file system */
71 extern int Oflag; /* format as an 4.3BSD file system */
72 extern int Uflag; /* enable soft updates for file system */
73 extern u_long fssize; /* file system size */
74 extern int ntracks; /* # tracks/cylinder */
75 extern int nsectors; /* # sectors/track */
76 extern int nphyssectors; /* # sectors/track including spares */
77 extern int secpercyl; /* sectors per cylinder */
78 extern int sectorsize; /* bytes/sector */
79 extern int realsectorsize; /* bytes/sector in hardware*/
80 extern int rpm; /* revolutions/minute of drive */
81 extern int interleave; /* hardware sector interleave */
82 extern int trackskew; /* sector 0 skew, per track */
83 extern int fsize; /* fragment size */
84 extern int bsize; /* block size */
85 extern int cpg; /* cylinders/cylinder group */
86 extern int cpgflg; /* cylinders/cylinder group flag was given */
87 extern int minfree; /* free space threshold */
88 extern int opt; /* optimization preference (space or time) */
89 extern int density; /* number of bytes per inode */
90 extern int maxcontig; /* max contiguous blocks to allocate */
91 extern int rotdelay; /* rotational delay between blocks */
92 extern int maxbpg; /* maximum blocks per file in a cyl group */
93 extern int nrpos; /* # of distinguished rotational positions */
94 extern int bbsize; /* boot block size */
95 extern int sbsize; /* superblock size */
96 extern int avgfilesize; /* expected average file size */
97 extern int avgfilesperdir; /* expected number of files per directory */
98 extern caddr_t membase; /* start address of memory based filesystem */
99 extern char * filename;
100 extern u_char *volumelabel; /* volume label for filesystem */
101 extern struct disktab geom;
103 extern void fatal(const char *fmt, ...);
109 #define sblock fsun.fs
118 struct ufs1_dinode zino[MAXBSIZE / sizeof(struct ufs1_dinode)];
121 static fsnode_t copyroot;
122 static fsnode_t copyhlinks;
126 daddr_t alloc(int, int);
127 long calcipg(long, long, off_t *);
128 static int charsperline(void);
129 void clrblock(struct fs *, unsigned char *, int);
131 void initcg(int, time_t);
132 int isblock(struct fs *, unsigned char *, int);
133 void iput(struct ufs1_dinode *, ino_t);
134 int makedir(struct direct *, int);
135 void parentready(int);
136 void rdfs(daddr_t, int, char *);
137 void setblock(struct fs *, unsigned char *, int);
139 void wtfs(daddr_t, int, char *);
140 void wtfsflush(void);
143 int parentready_signalled;
146 mkfs(char *fsys, int fi, int fo, const char *mfscopy)
148 long i, mincpc, mincpg, inospercg;
149 long cylno, rpos, blk, j, emitwarn = 0;
150 long used, mincpgcnt, bpcg;
152 long mapcramped, inodecramped;
153 long postblsize, rotblsize, totalsbsize;
158 char tmpbuf[100]; /* XXX this will break in about 2,500 years */
172 signal(SIGUSR1, parentready);
173 if ((child = fork()) != 0) {
180 copyroot = FSCopy(©hlinks, mfscopy);
181 signal(SIGUSR1, started);
182 kill(child, SIGUSR1);
183 while (waitpid(child, &status, 0) != child)
185 exit(WEXITSTATUS(status));
192 omask = sigblock(sigmask(SIGUSR1));
193 while (parentready_signalled == 0)
196 if (filename != NULL) {
197 unsigned char buf[BUFSIZ];
201 fd = open(filename, O_RDWR|O_TRUNC|O_CREAT, 0644);
203 err(12, "%s", filename);
204 l1 = fssize * sectorsize;
207 for (l = 0; l < fssize * (u_long)sectorsize; l += l1) {
208 w = write(fd, buf, l1);
209 if (w < 0 || (u_long)w != l1)
210 err(12, "%s", filename);
212 membase = mmap(NULL, fssize * sectorsize,
213 PROT_READ|PROT_WRITE,
215 if (membase == MAP_FAILED)
219 membase = mmap(NULL, fssize * sectorsize,
220 PROT_READ|PROT_WRITE,
221 MAP_SHARED|MAP_ANON, -1, 0);
222 if (membase == MAP_FAILED)
223 errx(13, "mmap (anonymous memory) failed");
229 sblock.fs_inodefmt = FS_42INODEFMT;
230 sblock.fs_maxsymlinklen = 0;
232 sblock.fs_inodefmt = FS_44INODEFMT;
233 sblock.fs_maxsymlinklen = MAXSYMLINKLEN;
236 sblock.fs_flags |= FS_DOSOFTDEP;
238 strlcpy(sblock.fs_volname, volumelabel, MAXVOLLEN);
240 * Validate the given file system size.
241 * Verify that its last block can actually be accessed.
244 printf("preposterous size %lu\n", fssize), exit(13);
245 wtfs(fssize - (realsectorsize / DEV_BSIZE), realsectorsize,
248 * collect and verify the sector and track info
250 sblock.fs_nsect = nsectors;
251 sblock.fs_ntrak = ntracks;
252 if (sblock.fs_ntrak <= 0)
253 printf("preposterous ntrak %d\n", sblock.fs_ntrak), exit(14);
254 if (sblock.fs_nsect <= 0)
255 printf("preposterous nsect %d\n", sblock.fs_nsect), exit(15);
257 * collect and verify the filesystem density info
259 sblock.fs_avgfilesize = avgfilesize;
260 sblock.fs_avgfpdir = avgfilesperdir;
261 if (sblock.fs_avgfilesize <= 0)
262 printf("illegal expected average file size %d\n",
263 sblock.fs_avgfilesize), exit(14);
264 if (sblock.fs_avgfpdir <= 0)
265 printf("illegal expected number of files per directory %d\n",
266 sblock.fs_avgfpdir), exit(15);
268 * collect and verify the block and fragment sizes
270 sblock.fs_bsize = bsize;
271 sblock.fs_fsize = fsize;
272 if (!POWEROF2(sblock.fs_bsize)) {
273 printf("block size must be a power of 2, not %d\n",
277 if (!POWEROF2(sblock.fs_fsize)) {
278 printf("fragment size must be a power of 2, not %d\n",
282 if (sblock.fs_fsize < sectorsize) {
283 printf("fragment size %d is too small, minimum is %d\n",
284 sblock.fs_fsize, sectorsize);
287 if (sblock.fs_bsize < MINBSIZE) {
288 printf("block size %d is too small, minimum is %d\n",
289 sblock.fs_bsize, MINBSIZE);
292 if (sblock.fs_bsize < sblock.fs_fsize) {
293 printf("block size (%d) cannot be smaller than fragment size (%d)\n",
294 sblock.fs_bsize, sblock.fs_fsize);
297 sblock.fs_bmask = ~(sblock.fs_bsize - 1);
298 sblock.fs_fmask = ~(sblock.fs_fsize - 1);
299 sblock.fs_qbmask = ~sblock.fs_bmask;
300 sblock.fs_qfmask = ~sblock.fs_fmask;
301 for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1)
303 for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1)
305 sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize);
306 for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1)
307 sblock.fs_fragshift++;
308 if (sblock.fs_frag > MAXFRAG) {
309 printf("fragment size %d is too small, minimum with block size %d is %d\n",
310 sblock.fs_fsize, sblock.fs_bsize,
311 sblock.fs_bsize / MAXFRAG);
314 sblock.fs_nrpos = nrpos;
315 sblock.fs_nindir = sblock.fs_bsize / sizeof(daddr_t);
316 sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs1_dinode);
317 sblock.fs_nspf = sblock.fs_fsize / sectorsize;
318 for (sblock.fs_fsbtodb = 0, i = NSPF(&sblock); i > 1; i >>= 1)
321 roundup(howmany(bbsize + sbsize, sblock.fs_fsize), sblock.fs_frag);
322 sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno +
323 roundup(howmany(sbsize, sblock.fs_fsize), sblock.fs_frag));
324 sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag;
325 sblock.fs_cgoffset = roundup(
326 howmany(sblock.fs_nsect, NSPF(&sblock)), sblock.fs_frag);
327 for (sblock.fs_cgmask = 0xffffffff, i = sblock.fs_ntrak; i > 1; i >>= 1)
328 sblock.fs_cgmask <<= 1;
329 if (!POWEROF2(sblock.fs_ntrak))
330 sblock.fs_cgmask <<= 1;
331 sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
332 for (sizepb = sblock.fs_bsize, i = 0; i < NIADDR; i++) {
333 sizepb *= NINDIR(&sblock);
334 sblock.fs_maxfilesize += sizepb;
337 * Validate specified/determined secpercyl
338 * and calculate minimum cylinders per group.
340 sblock.fs_spc = secpercyl;
341 for (sblock.fs_cpc = NSPB(&sblock), i = sblock.fs_spc;
342 sblock.fs_cpc > 1 && (i & 1) == 0;
343 sblock.fs_cpc >>= 1, i >>= 1)
345 mincpc = sblock.fs_cpc;
346 bpcg = sblock.fs_spc * sectorsize;
347 inospercg = roundup(bpcg / sizeof(struct ufs1_dinode), INOPB(&sblock));
348 if (inospercg > MAXIPG(&sblock))
349 inospercg = MAXIPG(&sblock);
350 used = (sblock.fs_iblkno + inospercg / INOPF(&sblock)) * NSPF(&sblock);
351 mincpgcnt = howmany(sblock.fs_cgoffset * (~sblock.fs_cgmask) + used,
353 mincpg = roundup(mincpgcnt, mincpc);
355 * Ensure that cylinder group with mincpg has enough space
358 sblock.fs_cpg = mincpg;
359 sblock.fs_ipg = inospercg;
361 sblock.fs_contigsumsize = MIN(maxcontig, FS_MAXCONTIG);
363 while (CGSIZE(&sblock) > (uint32_t)sblock.fs_bsize) {
365 if (sblock.fs_bsize < MAXBSIZE) {
366 sblock.fs_bsize <<= 1;
372 mincpg = roundup(mincpgcnt, mincpc);
373 sblock.fs_cpg = mincpg;
375 sblock.fs_frag <<= 1;
376 sblock.fs_fragshift += 1;
377 if (sblock.fs_frag <= MAXFRAG)
380 if (sblock.fs_fsize == sblock.fs_bsize) {
381 printf("There is no block size that");
382 printf(" can support this disk\n");
385 sblock.fs_frag >>= 1;
386 sblock.fs_fragshift -= 1;
387 sblock.fs_fsize <<= 1;
388 sblock.fs_nspf <<= 1;
391 * Ensure that cylinder group with mincpg has enough space for inodes.
394 inospercg = calcipg(mincpg, bpcg, &usedb);
395 sblock.fs_ipg = inospercg;
396 while (inospercg > MAXIPG(&sblock)) {
398 if (mincpc == 1 || sblock.fs_frag == 1 ||
399 sblock.fs_bsize == MINBSIZE)
401 printf("With a block size of %d %s %d\n", sblock.fs_bsize,
402 "minimum bytes per inode is",
403 (int)((mincpg * (off_t)bpcg - usedb)
404 / MAXIPG(&sblock) + 1));
405 sblock.fs_bsize >>= 1;
406 sblock.fs_frag >>= 1;
407 sblock.fs_fragshift -= 1;
409 sblock.fs_cpg = roundup(mincpgcnt, mincpc);
410 if (CGSIZE(&sblock) > (uint32_t)sblock.fs_bsize) {
411 sblock.fs_bsize <<= 1;
414 mincpg = sblock.fs_cpg;
415 inospercg = calcipg(mincpg, bpcg, &usedb);
416 sblock.fs_ipg = inospercg;
419 if (inospercg > MAXIPG(&sblock)) {
420 printf("Minimum bytes per inode is %d\n",
421 (int)((mincpg * (off_t)bpcg - usedb)
422 / MAXIPG(&sblock) + 1));
423 } else if (!mapcramped) {
424 printf("With %d bytes per inode, ", density);
425 printf("minimum cylinders per group is %ld\n", mincpg);
429 printf("With %d sectors per cylinder, ", sblock.fs_spc);
430 printf("minimum cylinders per group is %ld\n", mincpg);
432 if (inodecramped || mapcramped) {
433 if (sblock.fs_bsize != bsize)
434 printf("%s to be changed from %d to %d\n",
435 "This requires the block size",
436 bsize, sblock.fs_bsize);
437 if (sblock.fs_fsize != fsize)
438 printf("\t%s to be changed from %d to %d\n",
439 "and the fragment size",
440 fsize, sblock.fs_fsize);
444 * Calculate the number of cylinders per group
447 if (sblock.fs_cpg % mincpc != 0) {
448 printf("%s groups must have a multiple of %ld cylinders\n",
449 cpgflg ? "Cylinder" : "Warning: cylinder", mincpc);
450 sblock.fs_cpg = roundup(sblock.fs_cpg, mincpc);
455 * Must ensure there is enough space for inodes.
457 sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
458 while (sblock.fs_ipg > MAXIPG(&sblock)) {
460 sblock.fs_cpg -= mincpc;
461 sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
464 * Must ensure there is enough space to hold block map.
466 while (CGSIZE(&sblock) > (uint32_t)sblock.fs_bsize) {
468 sblock.fs_cpg -= mincpc;
469 sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
471 sblock.fs_fpg = (sblock.fs_cpg * sblock.fs_spc) / NSPF(&sblock);
472 if ((sblock.fs_cpg * sblock.fs_spc) % NSPB(&sblock) != 0) {
473 printf("panic (fs_cpg * fs_spc) %% NSPF != 0");
476 if (sblock.fs_cpg < mincpg) {
477 printf("cylinder groups must have at least %ld cylinders\n",
480 } else if (sblock.fs_cpg != cpg) {
483 else if (!mapcramped && !inodecramped)
486 if (mapcramped && inodecramped)
487 printf("Block size and bytes per inode restrict");
489 printf("Block size restricts");
491 printf("Bytes per inode restrict");
492 printf(" cylinders per group to %d.\n", sblock.fs_cpg);
497 sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock));
499 * Now have size for file system and nsect and ntrak.
500 * Determine number of cylinders and blocks in the file system.
502 sblock.fs_size = fssize = dbtofsb(&sblock, fssize);
503 sblock.fs_ncyl = fssize * NSPF(&sblock) / sblock.fs_spc;
504 if ((long)fssize * NSPF(&sblock) > sblock.fs_ncyl * sblock.fs_spc) {
508 if (sblock.fs_ncyl < 1) {
509 printf("file systems must have at least one cylinder\n");
513 * Determine feasability/values of rotational layout tables.
515 * The size of the rotational layout tables is limited by the
516 * size of the superblock, SBSIZE. The amount of space available
517 * for tables is calculated as (SBSIZE - sizeof (struct fs)).
518 * The size of these tables is inversely proportional to the block
519 * size of the file system. The size increases if sectors per track
520 * are not powers of two, because more cylinders must be described
521 * by the tables before the rotational pattern repeats (fs_cpc).
523 sblock.fs_interleave = interleave;
524 sblock.fs_trackskew = trackskew;
525 sblock.fs_npsect = nphyssectors;
526 sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
527 sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs));
528 if (sblock.fs_sbsize > SBSIZE)
529 sblock.fs_sbsize = SBSIZE;
530 if (sblock.fs_ntrak == 1) {
534 postblsize = sblock.fs_nrpos * sblock.fs_cpc * sizeof(int16_t);
535 rotblsize = sblock.fs_cpc * sblock.fs_spc / NSPB(&sblock);
536 totalsbsize = sizeof(struct fs) + rotblsize;
537 if (sblock.fs_nrpos == 8 && sblock.fs_cpc <= 16) {
538 /* use old static table space */
539 sblock.fs_postbloff = (char *)(&sblock.fs_opostbl[0][0]) -
540 (char *)(&sblock.fs_firstfield);
541 sblock.fs_rotbloff = &sblock.fs_space[0] -
542 (u_char *)(&sblock.fs_firstfield);
544 /* use dynamic table space */
545 sblock.fs_postbloff = &sblock.fs_space[0] -
546 (u_char *)(&sblock.fs_firstfield);
547 sblock.fs_rotbloff = sblock.fs_postbloff + postblsize;
548 totalsbsize += postblsize;
550 if (totalsbsize > SBSIZE ||
551 sblock.fs_nsect > (1 << NBBY) * NSPB(&sblock)) {
552 printf("%s %s %d %s %d.%s",
553 "Warning: insufficient space in super block for\n",
554 "rotational layout tables with nsect", sblock.fs_nsect,
555 "and ntrak", sblock.fs_ntrak,
556 "\nFile system performance may be impaired.\n");
560 sblock.fs_sbsize = fragroundup(&sblock, totalsbsize);
561 if (sblock.fs_sbsize > SBSIZE)
562 sblock.fs_sbsize = SBSIZE;
564 * calculate the available blocks for each rotational position
566 for (cylno = 0; cylno < sblock.fs_cpc; cylno++)
567 for (rpos = 0; rpos < sblock.fs_nrpos; rpos++)
568 fs_postbl(&sblock, cylno)[rpos] = -1;
569 for (i = (rotblsize - 1) * sblock.fs_frag;
570 i >= 0; i -= sblock.fs_frag) {
571 cylno = cbtocylno(&sblock, i);
572 rpos = cbtorpos(&sblock, i);
573 blk = fragstoblks(&sblock, i);
574 if (fs_postbl(&sblock, cylno)[rpos] == -1)
575 fs_rotbl(&sblock)[blk] = 0;
577 fs_rotbl(&sblock)[blk] =
578 fs_postbl(&sblock, cylno)[rpos] - blk;
579 fs_postbl(&sblock, cylno)[rpos] = blk;
583 * Compute/validate number of cylinder groups.
585 sblock.fs_ncg = sblock.fs_ncyl / sblock.fs_cpg;
586 if (sblock.fs_ncyl % sblock.fs_cpg)
588 sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock);
589 i = MIN(~sblock.fs_cgmask, sblock.fs_ncg - 1);
590 if (cgdmin(&sblock, i) - cgbase(&sblock, i) >= sblock.fs_fpg) {
591 printf("inode blocks/cyl group (%ld) >= data blocks (%ld)\n",
592 cgdmin(&sblock, i) - cgbase(&sblock, i) / sblock.fs_frag,
593 (long)(sblock.fs_fpg / sblock.fs_frag));
594 printf("number of cylinders per cylinder group (%d) %s.\n",
595 sblock.fs_cpg, "must be increased");
598 j = sblock.fs_ncg - 1;
599 if ((i = fssize - j * sblock.fs_fpg) < sblock.fs_fpg &&
600 cgdmin(&sblock, j) - cgbase(&sblock, j) > i) {
602 printf("Filesystem must have at least %d sectors\n",
604 (cgdmin(&sblock, 0) + 3 * sblock.fs_frag));
608 "Warning: inode blocks/cyl group (%ld) >= data blocks (%ld) in last\n",
609 (cgdmin(&sblock, j) - cgbase(&sblock, j)) / sblock.fs_frag,
612 " cylinder group. This implies %ld sector(s) cannot be allocated.\n",
615 sblock.fs_ncyl -= sblock.fs_ncyl % sblock.fs_cpg;
616 sblock.fs_size = fssize = sblock.fs_ncyl * sblock.fs_spc /
620 if (emitwarn && !mfs) {
621 printf("Warning: %lu sector(s) in last cylinder unallocated\n",
623 (fssize * NSPF(&sblock) - (sblock.fs_ncyl - 1)
627 * fill in remaining fields of the super block
629 sblock.fs_csaddr = cgdmin(&sblock, 0);
631 fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
633 * The superblock fields 'fs_csmask' and 'fs_csshift' are no
634 * longer used. However, we still initialise them so that the
635 * filesystem remains compatible with old kernels.
637 i = sblock.fs_bsize / sizeof(struct csum);
638 sblock.fs_csmask = ~(i - 1);
639 for (sblock.fs_csshift = 0; i > 1; i >>= 1)
641 fscs = (struct csum *)calloc(1, sblock.fs_cssize);
643 errx(31, "calloc failed");
644 sblock.fs_magic = FS_MAGIC;
645 sblock.fs_rotdelay = rotdelay;
646 sblock.fs_minfree = minfree;
647 sblock.fs_maxcontig = maxcontig;
648 sblock.fs_maxbpg = maxbpg;
649 sblock.fs_rps = rpm / 60;
650 sblock.fs_optim = opt;
651 sblock.fs_cgrotor = 0;
652 sblock.fs_cstotal.cs_ndir = 0;
653 sblock.fs_cstotal.cs_nbfree = 0;
654 sblock.fs_cstotal.cs_nifree = 0;
655 sblock.fs_cstotal.cs_nffree = 0;
660 sblock.fs_id[0] = (long)utime;
661 sblock.fs_id[1] = random();
665 * Dump out summary information about file system.
668 printf("%s:\t%d sectors in %d %s of %d tracks, %d sectors\n",
669 fsys, sblock.fs_size * NSPF(&sblock), sblock.fs_ncyl,
670 "cylinders", sblock.fs_ntrak, sblock.fs_nsect);
671 #define B2MBFACTOR (1 / (1024.0 * 1024.0))
672 printf("\t%.1fMB in %d cyl groups (%d c/g, %.2fMB/g, %d i/g)%s\n",
673 (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
674 sblock.fs_ncg, sblock.fs_cpg,
675 (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
677 sblock.fs_flags & FS_DOSOFTDEP ? " SOFTUPDATES" : "");
681 * Now build the cylinders group blocks and
682 * then print out indices of cylinder groups.
685 printf("super-block backups (for fsck -b #) at:\n");
687 width = charsperline();
688 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
689 initcg(cylno, utime);
692 j = snprintf(tmpbuf, sizeof(tmpbuf), " %ld%s",
693 fsbtodb(&sblock, cgsblock(&sblock, cylno)),
694 cylno < (sblock.fs_ncg-1) ? "," : "" );
695 if (i + j >= width) {
700 printf("%s", tmpbuf);
708 * Now construct the initial file system,
709 * then write out the super-block.
712 sblock.fs_time = utime;
713 wtfs((int)SBOFF / sectorsize, sbsize, (char *)&sblock);
714 for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize)
715 wtfs(fsbtodb(&sblock, sblock.fs_csaddr + numfrags(&sblock, i)),
716 sblock.fs_cssize - i < sblock.fs_bsize ?
717 sblock.fs_cssize - i : sblock.fs_bsize,
720 * Write out the duplicate super blocks
722 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
723 wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)),
724 sbsize, (char *)&sblock);
728 * NOTE: we no longer update information in the disklabel
732 * Notify parent process of success.
733 * Dissociate from session and tty.
735 * NOTE: We are the child and may receive a SIGINT due
736 * to losing the tty session? XXX
740 kill(mfs_ppid, SIGUSR1);
746 /* returns to mount_mfs (newfs) and issues the mount */
751 * Initialize a cylinder group.
754 initcg(int cylno, time_t utime)
756 daddr_t cbase, d, dlower, dupper, dmax, blkno;
765 * Determine block bounds for cylinder group.
766 * Allow space for super block summary information in first
769 cbase = cgbase(&sblock, cylno);
770 dmax = cbase + sblock.fs_fpg;
771 if (dmax > sblock.fs_size)
772 dmax = sblock.fs_size;
773 dlower = cgsblock(&sblock, cylno) - cbase;
774 dupper = cgdmin(&sblock, cylno) - cbase;
776 dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
778 memset(&acg, 0, sblock.fs_cgsize);
780 acg.cg_magic = CG_MAGIC;
782 if (cylno == sblock.fs_ncg - 1)
783 acg.cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg;
785 acg.cg_ncyl = sblock.fs_cpg;
786 acg.cg_niblk = sblock.fs_ipg;
787 acg.cg_ndblk = dmax - cbase;
788 if (sblock.fs_contigsumsize > 0)
789 acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag;
790 acg.cg_btotoff = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield);
791 acg.cg_boff = acg.cg_btotoff + sblock.fs_cpg * sizeof(int32_t);
792 acg.cg_iusedoff = acg.cg_boff +
793 sblock.fs_cpg * sblock.fs_nrpos * sizeof(u_int16_t);
794 acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, NBBY);
795 if (sblock.fs_contigsumsize <= 0) {
796 acg.cg_nextfreeoff = acg.cg_freeoff +
797 howmany(sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY);
799 acg.cg_clustersumoff = acg.cg_freeoff + howmany
800 (sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY) -
802 acg.cg_clustersumoff =
803 roundup(acg.cg_clustersumoff, sizeof(u_int32_t));
804 acg.cg_clusteroff = acg.cg_clustersumoff +
805 (sblock.fs_contigsumsize + 1) * sizeof(u_int32_t);
806 acg.cg_nextfreeoff = acg.cg_clusteroff + howmany
807 (sblock.fs_cpg * sblock.fs_spc / NSPB(&sblock), NBBY);
809 if (acg.cg_nextfreeoff - (long)(&acg.cg_firstfield) > sblock.fs_cgsize) {
810 printf("Panic: cylinder group too big\n");
813 acg.cg_cs.cs_nifree += sblock.fs_ipg;
815 for (k = 0; k < ROOTINO; k++) {
816 setbit(cg_inosused(&acg), k);
817 acg.cg_cs.cs_nifree--;
820 for (i = 0; i < sblock.fs_ipg / INOPF(&sblock); i += sblock.fs_frag) {
823 j < sblock.fs_bsize / sizeof(struct ufs1_dinode);
825 zino[j].di_gen = random();
828 wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
829 sblock.fs_bsize, (char *)zino);
833 * In cylno 0, beginning space is reserved
834 * for boot and super blocks.
836 for (d = 0; d < dlower; d += sblock.fs_frag) {
837 blkno = d / sblock.fs_frag;
838 setblock(&sblock, cg_blksfree(&acg), blkno);
839 if (sblock.fs_contigsumsize > 0)
840 setbit(cg_clustersfree(&acg), blkno);
841 acg.cg_cs.cs_nbfree++;
842 cg_blktot(&acg)[cbtocylno(&sblock, d)]++;
843 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))
844 [cbtorpos(&sblock, d)]++;
846 sblock.fs_dsize += dlower;
848 sblock.fs_dsize += acg.cg_ndblk - dupper;
849 if ((i = dupper % sblock.fs_frag)) {
850 acg.cg_frsum[sblock.fs_frag - i]++;
851 for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
852 setbit(cg_blksfree(&acg), dupper);
853 acg.cg_cs.cs_nffree++;
856 for (d = dupper; d + sblock.fs_frag <= dmax - cbase; ) {
857 blkno = d / sblock.fs_frag;
858 setblock(&sblock, cg_blksfree(&acg), blkno);
859 if (sblock.fs_contigsumsize > 0)
860 setbit(cg_clustersfree(&acg), blkno);
861 acg.cg_cs.cs_nbfree++;
862 cg_blktot(&acg)[cbtocylno(&sblock, d)]++;
863 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))
864 [cbtorpos(&sblock, d)]++;
867 if (d < dmax - cbase) {
868 acg.cg_frsum[dmax - cbase - d]++;
869 for (; d < dmax - cbase; d++) {
870 setbit(cg_blksfree(&acg), d);
871 acg.cg_cs.cs_nffree++;
874 if (sblock.fs_contigsumsize > 0) {
875 int32_t *sump = cg_clustersum(&acg);
876 u_char *mapp = cg_clustersfree(&acg);
881 for (i = 0; i < acg.cg_nclusterblks; i++) {
882 if ((map & bit) != 0) {
884 } else if (run != 0) {
885 if (run > sblock.fs_contigsumsize)
886 run = sblock.fs_contigsumsize;
890 if ((i & (NBBY - 1)) != (NBBY - 1)) {
898 if (run > sblock.fs_contigsumsize)
899 run = sblock.fs_contigsumsize;
903 sblock.fs_cstotal.cs_ndir += acg.cg_cs.cs_ndir;
904 sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree;
905 sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree;
906 sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree;
908 wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
909 sblock.fs_bsize, (char *)&acg);
913 * initialize the file system
915 struct ufs1_dinode node;
923 struct direct root_dir[] = {
924 { ROOTINO, sizeof(struct direct), DT_DIR, 1, "." },
925 { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
927 { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 10, "lost+found" },
934 u_char d_name[MAXNAMLEN + 1];
936 { ROOTINO, sizeof(struct direct), 1, "." },
937 { ROOTINO, sizeof(struct direct), 2, ".." },
939 { LOSTFOUNDINO, sizeof(struct direct), 10, "lost+found" },
943 struct direct lost_found_dir[] = {
944 { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 1, "." },
945 { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
946 { 0, DIRBLKSIZ, 0, 0, 0 },
948 struct odirect olost_found_dir[] = {
949 { LOSTFOUNDINO, sizeof(struct direct), 1, "." },
950 { ROOTINO, sizeof(struct direct), 2, ".." },
951 { 0, DIRBLKSIZ, 0, 0 },
964 * initialize the node
966 node.di_atime = utime;
967 node.di_mtime = utime;
968 node.di_ctime = utime;
971 * create the lost+found directory
974 makedir((struct direct *)olost_found_dir, 2);
975 for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ)
976 memmove(&buf[i], &olost_found_dir[2],
977 DIRSIZ(0, &olost_found_dir[2]));
979 makedir(lost_found_dir, 2);
980 for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ)
981 memmove(&buf[i], &lost_found_dir[2],
982 DIRSIZ(0, &lost_found_dir[2]));
984 node.di_mode = IFDIR | UMASK;
986 node.di_size = sblock.fs_bsize;
987 node.di_db[0] = alloc(node.di_size, node.di_mode);
988 node.di_blocks = btodb(fragroundup(&sblock, node.di_size));
989 wtfs(fsbtodb(&sblock, node.di_db[0]), node.di_size, buf);
990 iput(&node, LOSTFOUNDINO);
993 * create the root directory
996 node.di_mode = IFDIR | 01777;
998 node.di_mode = IFDIR | UMASK;
999 node.di_nlink = PREDEFDIR;
1001 node.di_size = makedir((struct direct *)oroot_dir, PREDEFDIR);
1003 node.di_size = makedir(root_dir, PREDEFDIR);
1004 node.di_db[0] = alloc(sblock.fs_fsize, node.di_mode);
1005 node.di_blocks = btodb(fragroundup(&sblock, node.di_size));
1006 wtfs(fsbtodb(&sblock, node.di_db[0]), sblock.fs_fsize, buf);
1007 iput(&node, ROOTINO);
1011 * construct a set of directory entries in "buf".
1012 * return size of directory.
1015 makedir(struct direct *protodir, int entries)
1020 spcleft = DIRBLKSIZ;
1021 for (cp = buf, i = 0; i < entries - 1; i++) {
1022 protodir[i].d_reclen = DIRSIZ(0, &protodir[i]);
1023 memmove(cp, &protodir[i], protodir[i].d_reclen);
1024 cp += protodir[i].d_reclen;
1025 spcleft -= protodir[i].d_reclen;
1027 protodir[i].d_reclen = spcleft;
1028 memmove(cp, &protodir[i], DIRSIZ(0, &protodir[i]));
1033 * allocate a block or frag
1036 alloc(int size, int mode)
1041 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
1043 if (acg.cg_magic != CG_MAGIC) {
1044 printf("cg 0: bad magic number\n");
1047 if (acg.cg_cs.cs_nbfree == 0) {
1048 printf("first cylinder group ran out of space\n");
1051 for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag)
1052 if (isblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag))
1054 printf("internal error: can't find block in cyl 0\n");
1057 blkno = fragstoblks(&sblock, d);
1058 clrblock(&sblock, cg_blksfree(&acg), blkno);
1059 if (sblock.fs_contigsumsize > 0)
1060 clrbit(cg_clustersfree(&acg), blkno);
1061 acg.cg_cs.cs_nbfree--;
1062 sblock.fs_cstotal.cs_nbfree--;
1063 fscs[0].cs_nbfree--;
1065 acg.cg_cs.cs_ndir++;
1066 sblock.fs_cstotal.cs_ndir++;
1069 cg_blktot(&acg)[cbtocylno(&sblock, d)]--;
1070 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))[cbtorpos(&sblock, d)]--;
1071 if (size != sblock.fs_bsize) {
1072 frag = howmany(size, sblock.fs_fsize);
1073 fscs[0].cs_nffree += sblock.fs_frag - frag;
1074 sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag;
1075 acg.cg_cs.cs_nffree += sblock.fs_frag - frag;
1076 acg.cg_frsum[sblock.fs_frag - frag]++;
1077 for (i = frag; i < sblock.fs_frag; i++)
1078 setbit(cg_blksfree(&acg), d + i);
1080 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
1086 * Calculate number of inodes per group.
1089 calcipg(long cylspg, long bpcg, off_t *usedbp)
1092 long ipg, new_ipg, ncg, ncyl;
1096 * Prepare to scale by fssize / (number of sectors in cylinder groups).
1097 * Note that fssize is still in sectors, not filesystem blocks.
1099 ncyl = howmany(fssize, (u_int)secpercyl);
1100 ncg = howmany(ncyl, cylspg);
1102 * Iterate a few times to allow for ipg depending on itself.
1105 for (i = 0; i < 10; i++) {
1106 usedb = (sblock.fs_iblkno + ipg / INOPF(&sblock))
1107 * NSPF(&sblock) * (off_t)sectorsize;
1108 new_ipg = (cylspg * (quad_t)bpcg - usedb) / density * fssize
1109 / ncg / secpercyl / cylspg;
1110 new_ipg = roundup(new_ipg, INOPB(&sblock));
1120 * Allocate an inode on the disk
1123 iput(struct ufs1_dinode *ip, ino_t ino)
1125 struct ufs1_dinode inobuf[MAXINOPB];
1130 ip->di_gen = random();
1132 c = ino_to_cg(&sblock, ino);
1133 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
1135 if (acg.cg_magic != CG_MAGIC) {
1136 printf("cg 0: bad magic number\n");
1139 acg.cg_cs.cs_nifree--;
1140 setbit(cg_inosused(&acg), ino);
1141 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
1143 sblock.fs_cstotal.cs_nifree--;
1144 fscs[0].cs_nifree--;
1145 if (ino >= (uint32_t)sblock.fs_ipg * (uint32_t)sblock.fs_ncg) {
1146 printf("fsinit: inode value out of range (%ju).\n",
1150 d = fsbtodb(&sblock, ino_to_fsba(&sblock, ino));
1151 rdfs(d, sblock.fs_bsize, (char *)inobuf);
1152 inobuf[ino_to_fsbo(&sblock, ino)] = *ip;
1153 wtfs(d, sblock.fs_bsize, (char *)inobuf);
1157 * Parent notifies child that it can proceed with the newfs and mount
1158 * operation (occurs after parent has copied the underlying filesystem
1159 * if the -C option was specified (for MFS), or immediately after the
1160 * parent forked the child otherwise).
1163 parentready(__unused int signo)
1165 parentready_signalled = 1;
1169 * Notify parent process that the filesystem has created itself successfully.
1171 * We have to wait until the mount has actually completed!
1174 started(__unused int signo)
1176 int retry = 100; /* 10 seconds, 100ms */
1178 while (mfs_ppid && retry) {
1182 stat(mfs_mtpt, &st) < 0 ||
1183 st.st_dev != mfs_mtstat.st_dev
1191 fatal("mfs mount failed waiting for mount to go active");
1192 } else if (copyroot) {
1193 FSPaste(mfs_mtpt, copyroot, copyhlinks);
1199 * read a block from the file system
1202 rdfs(daddr_t bno, int size, char *bf)
1208 memmove(bf, membase + bno * sectorsize, size);
1211 if (lseek(fsi, (off_t)bno * sectorsize, 0) < 0) {
1212 printf("seek error: %ld\n", (long)bno);
1215 n = read(fsi, bf, size);
1217 printf("read error: %ld\n", (long)bno);
1222 #define WCSIZE (128 * 1024)
1223 daddr_t wc_sect; /* units of sectorsize */
1224 int wc_end; /* bytes */
1225 static char wc[WCSIZE]; /* bytes */
1228 * Flush dirty write behind buffer.
1235 if (lseek(fso, (off_t)wc_sect * sectorsize, SEEK_SET) < 0) {
1236 printf("seek error: %ld\n", (long)wc_sect);
1237 err(35, "wtfs - writecombine");
1239 n = write(fso, wc, wc_end);
1241 printf("write error: %ld\n", (long)wc_sect);
1242 err(36, "wtfs - writecombine");
1249 * write a block to the file system
1252 wtfs(daddr_t bno, int size, char *bf)
1258 memmove(membase + bno * sectorsize, bf, size);
1264 if (wc_end == 0 && size <= WCSIZE) {
1266 bcopy(bf, wc, size);
1268 if (wc_end < WCSIZE)
1272 if ((off_t)wc_sect * sectorsize + wc_end == (off_t)bno * sectorsize &&
1273 wc_end + size <= WCSIZE) {
1274 bcopy(bf, wc + wc_end, size);
1276 if (wc_end < WCSIZE)
1283 if (lseek(fso, (off_t)bno * sectorsize, SEEK_SET) < 0) {
1284 printf("seek error: %ld\n", (long)bno);
1287 n = write(fso, bf, size);
1289 printf("write error: fso %d blk %ld %d/%d\n",
1290 fso, (long)bno, n, size);
1296 * check if a block is available
1299 isblock(struct fs *fs, unsigned char *cp, int h)
1303 switch (fs->fs_frag) {
1305 return (cp[h] == 0xff);
1307 mask = 0x0f << ((h & 0x1) << 2);
1308 return ((cp[h >> 1] & mask) == mask);
1310 mask = 0x03 << ((h & 0x3) << 1);
1311 return ((cp[h >> 2] & mask) == mask);
1313 mask = 0x01 << (h & 0x7);
1314 return ((cp[h >> 3] & mask) == mask);
1316 fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag);
1322 * take a block out of the map
1325 clrblock(struct fs *fs, unsigned char *cp, int h)
1327 switch ((fs)->fs_frag) {
1332 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
1335 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
1338 cp[h >> 3] &= ~(0x01 << (h & 0x7));
1341 fprintf(stderr, "clrblock bad fs_frag %d\n", fs->fs_frag);
1347 * put a block into the map
1350 setblock(struct fs *fs, unsigned char *cp, int h)
1352 switch (fs->fs_frag) {
1357 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
1360 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
1363 cp[h >> 3] |= (0x01 << (h & 0x7));
1366 fprintf(stderr, "setblock bad fs_frag %d\n", fs->fs_frag);
1372 * Determine the number of characters in a
1384 if (ioctl(0, TIOCGWINSZ, &ws) != -1)
1385 columns = ws.ws_col;
1386 if (columns == 0 && (cp = getenv("COLUMNS")))
1389 columns = 80; /* last resort */