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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)mkfs.c 8.11 (Berkeley) 5/3/95
30 * $FreeBSD: src/sbin/newfs/mkfs.c,v 1.29.2.6 2001/09/21 19:15:21 dillon Exp $
38 #include <bus/cam/scsi/scsi_daio.h>
41 * make file system for cylinder-group style file systems
45 * We limit the size of the inode map to be no more than a
46 * third of the cylinder group space, since we must leave at
47 * least an equal amount of space for the block map.
49 * N.B.: MAXIPG must be a multiple of INOPB(fs).
51 #define MAXIPG(fs) roundup((fs)->fs_bsize * NBBY / 3, INOPB(fs))
54 #define MAXINOPB (MAXBSIZE / sizeof(struct ufs1_dinode))
55 #define POWEROF2(num) (((num) & ((num) - 1)) == 0)
58 #error "mkfs.c: STANDALONE compilation no longer supported"
62 * variables set up by front end.
64 extern int mfs; /* run as the memory based filesystem */
65 extern char *mfs_mtpt; /* mount point for mfs */
66 extern struct stat mfs_mtstat; /* stat prior to mount */
67 extern int Lflag; /* add a volume label */
68 extern int Nflag; /* run mkfs without writing file system */
69 extern int Oflag; /* format as an 4.3BSD file system */
70 extern int Uflag; /* enable soft updates for file system */
71 extern int Eflag; /* erase contents using TRIM */
72 extern uint64_t slice_offset; /* Pysical device slice offset */
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);
138 void started(int) __dead2;
139 void erfs(off_t, off_t);
140 void wtfs(daddr_t, int, char *);
141 void wtfsflush(void);
144 int parentready_signalled;
147 mkfs(char *fsys, int fi, int fo, const char *mfscopy)
149 long i, mincpc, mincpg, inospercg;
150 long cylno, rpos, blk, j, emitwarn = 0;
151 long used, mincpgcnt, bpcg;
153 long mapcramped, inodecramped;
154 long postblsize, rotblsize, totalsbsize;
159 char tmpbuf[100]; /* XXX this will break in about 2,500 years */
173 signal(SIGUSR1, parentready);
174 if ((child = fork()) != 0) {
181 copyroot = FSCopy(©hlinks, mfscopy);
182 signal(SIGUSR1, started);
183 kill(child, SIGUSR1);
184 while (waitpid(child, &status, 0) != child)
186 exit(WEXITSTATUS(status));
193 omask = sigblock(sigmask(SIGUSR1));
194 while (parentready_signalled == 0)
197 if (filename != NULL) {
198 unsigned char buf[BUFSIZ];
202 fd = open(filename, O_RDWR|O_TRUNC|O_CREAT, 0644);
204 err(12, "%s", filename);
205 l1 = fssize * sectorsize;
208 for (l = 0; l < fssize * (u_long)sectorsize; l += l1) {
209 w = write(fd, buf, l1);
210 if (w < 0 || (u_long)w != l1)
211 err(12, "%s", filename);
213 membase = mmap(NULL, fssize * sectorsize,
214 PROT_READ|PROT_WRITE,
216 if (membase == MAP_FAILED)
220 membase = mmap(NULL, fssize * sectorsize,
221 PROT_READ|PROT_WRITE,
222 MAP_SHARED|MAP_ANON, -1, 0);
223 if (membase == MAP_FAILED)
224 errx(13, "mmap (anonymous memory) failed");
230 sblock.fs_inodefmt = FS_42INODEFMT;
231 sblock.fs_maxsymlinklen = 0;
233 sblock.fs_inodefmt = FS_44INODEFMT;
234 sblock.fs_maxsymlinklen = UFS1_MAXSYMLINKLEN;
237 sblock.fs_flags |= FS_DOSOFTDEP;
239 strlcpy(sblock.fs_volname, volumelabel, MAXVOLLEN);
242 * Validate the given file system size.
243 * Verify that its last block can actually be accessed.
246 printf("preposterous size %lu\n", fssize), exit(13);
247 wtfs(fssize - (realsectorsize / DEV_BSIZE), realsectorsize,
250 * collect and verify the sector and track info
252 sblock.fs_nsect = nsectors;
253 sblock.fs_ntrak = ntracks;
254 if (sblock.fs_ntrak <= 0)
255 printf("preposterous ntrak %d\n", sblock.fs_ntrak), exit(14);
256 if (sblock.fs_nsect <= 0)
257 printf("preposterous nsect %d\n", sblock.fs_nsect), exit(15);
259 * collect and verify the filesystem density info
261 sblock.fs_avgfilesize = avgfilesize;
262 sblock.fs_avgfpdir = avgfilesperdir;
263 if (sblock.fs_avgfilesize <= 0)
264 printf("illegal expected average file size %d\n",
265 sblock.fs_avgfilesize), exit(14);
266 if (sblock.fs_avgfpdir <= 0)
267 printf("illegal expected number of files per directory %d\n",
268 sblock.fs_avgfpdir), exit(15);
270 * collect and verify the block and fragment sizes
272 sblock.fs_bsize = bsize;
273 sblock.fs_fsize = fsize;
274 if (!POWEROF2(sblock.fs_bsize)) {
275 printf("block size must be a power of 2, not %d\n",
279 if (!POWEROF2(sblock.fs_fsize)) {
280 printf("fragment size must be a power of 2, not %d\n",
284 if (sblock.fs_fsize < sectorsize) {
285 printf("fragment size %d is too small, minimum is %d\n",
286 sblock.fs_fsize, sectorsize);
289 if (sblock.fs_bsize < MINBSIZE) {
290 printf("block size %d is too small, minimum is %d\n",
291 sblock.fs_bsize, MINBSIZE);
294 if (sblock.fs_bsize < sblock.fs_fsize) {
295 printf("block size (%d) cannot be smaller than fragment size (%d)\n",
296 sblock.fs_bsize, sblock.fs_fsize);
299 sblock.fs_bmask = ~(sblock.fs_bsize - 1);
300 sblock.fs_fmask = ~(sblock.fs_fsize - 1);
301 sblock.fs_qbmask = ~sblock.fs_bmask;
302 sblock.fs_qfmask = ~sblock.fs_fmask;
303 for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1)
305 for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1)
307 sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize);
308 for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1)
309 sblock.fs_fragshift++;
310 if (sblock.fs_frag > MAXFRAG) {
311 printf("fragment size %d is too small, minimum with block size %d is %d\n",
312 sblock.fs_fsize, sblock.fs_bsize,
313 sblock.fs_bsize / MAXFRAG);
316 sblock.fs_nrpos = nrpos;
317 sblock.fs_nindir = sblock.fs_bsize / sizeof(daddr_t);
318 sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs1_dinode);
319 sblock.fs_nspf = sblock.fs_fsize / sectorsize;
320 for (sblock.fs_fsbtodb = 0, i = NSPF(&sblock); i > 1; i >>= 1)
323 roundup(howmany(bbsize + sbsize, sblock.fs_fsize), sblock.fs_frag);
324 sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno +
325 roundup(howmany(sbsize, sblock.fs_fsize), sblock.fs_frag));
326 sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag;
327 sblock.fs_cgoffset = roundup(
328 howmany(sblock.fs_nsect, NSPF(&sblock)), sblock.fs_frag);
329 for (sblock.fs_cgmask = 0xffffffff, i = sblock.fs_ntrak; i > 1; i >>= 1)
330 sblock.fs_cgmask <<= 1;
331 if (!POWEROF2(sblock.fs_ntrak))
332 sblock.fs_cgmask <<= 1;
333 sblock.fs_maxfilesize = sblock.fs_bsize * UFS_NDADDR - 1;
334 for (sizepb = sblock.fs_bsize, i = 0; i < UFS_NIADDR; i++) {
335 sizepb *= NINDIR(&sblock);
336 sblock.fs_maxfilesize += sizepb;
339 * Validate specified/determined secpercyl
340 * and calculate minimum cylinders per group.
342 sblock.fs_spc = secpercyl;
343 for (sblock.fs_cpc = NSPB(&sblock), i = sblock.fs_spc;
344 sblock.fs_cpc > 1 && (i & 1) == 0;
345 sblock.fs_cpc >>= 1, i >>= 1)
347 mincpc = sblock.fs_cpc;
348 bpcg = sblock.fs_spc * sectorsize;
349 inospercg = roundup(bpcg / sizeof(struct ufs1_dinode), INOPB(&sblock));
350 if (inospercg > MAXIPG(&sblock))
351 inospercg = MAXIPG(&sblock);
352 used = (sblock.fs_iblkno + inospercg / INOPF(&sblock)) * NSPF(&sblock);
353 mincpgcnt = howmany(sblock.fs_cgoffset * (~sblock.fs_cgmask) + used,
355 mincpg = roundup(mincpgcnt, mincpc);
357 * Ensure that cylinder group with mincpg has enough space
360 sblock.fs_cpg = mincpg;
361 sblock.fs_ipg = inospercg;
363 sblock.fs_contigsumsize = MIN(maxcontig, FS_MAXCONTIG);
365 while (CGSIZE(&sblock) > (uint32_t)sblock.fs_bsize) {
367 if (sblock.fs_bsize < MAXBSIZE) {
368 sblock.fs_bsize <<= 1;
374 mincpg = roundup(mincpgcnt, mincpc);
375 sblock.fs_cpg = mincpg;
377 sblock.fs_frag <<= 1;
378 sblock.fs_fragshift += 1;
379 if (sblock.fs_frag <= MAXFRAG)
382 if (sblock.fs_fsize == sblock.fs_bsize) {
383 printf("There is no block size that");
384 printf(" can support this disk\n");
387 sblock.fs_frag >>= 1;
388 sblock.fs_fragshift -= 1;
389 sblock.fs_fsize <<= 1;
390 sblock.fs_nspf <<= 1;
393 * Ensure that cylinder group with mincpg has enough space for inodes.
396 inospercg = calcipg(mincpg, bpcg, &usedb);
397 sblock.fs_ipg = inospercg;
398 while (inospercg > MAXIPG(&sblock)) {
400 if (mincpc == 1 || sblock.fs_frag == 1 ||
401 sblock.fs_bsize == MINBSIZE)
403 printf("With a block size of %d %s %d\n", sblock.fs_bsize,
404 "minimum bytes per inode is",
405 (int)((mincpg * (off_t)bpcg - usedb)
406 / MAXIPG(&sblock) + 1));
407 sblock.fs_bsize >>= 1;
408 sblock.fs_frag >>= 1;
409 sblock.fs_fragshift -= 1;
411 sblock.fs_cpg = roundup(mincpgcnt, mincpc);
412 if (CGSIZE(&sblock) > (uint32_t)sblock.fs_bsize) {
413 sblock.fs_bsize <<= 1;
416 mincpg = sblock.fs_cpg;
417 inospercg = calcipg(mincpg, bpcg, &usedb);
418 sblock.fs_ipg = inospercg;
421 if (inospercg > MAXIPG(&sblock)) {
422 printf("Minimum bytes per inode is %d\n",
423 (int)((mincpg * (off_t)bpcg - usedb)
424 / MAXIPG(&sblock) + 1));
425 } else if (!mapcramped) {
426 printf("With %d bytes per inode, ", density);
427 printf("minimum cylinders per group is %ld\n", mincpg);
431 printf("With %d sectors per cylinder, ", sblock.fs_spc);
432 printf("minimum cylinders per group is %ld\n", mincpg);
434 if (inodecramped || mapcramped) {
435 if (sblock.fs_bsize != bsize)
436 printf("%s to be changed from %d to %d\n",
437 "This requires the block size",
438 bsize, sblock.fs_bsize);
439 if (sblock.fs_fsize != fsize)
440 printf("\t%s to be changed from %d to %d\n",
441 "and the fragment size",
442 fsize, sblock.fs_fsize);
446 * Calculate the number of cylinders per group
449 if (sblock.fs_cpg % mincpc != 0) {
450 printf("%s groups must have a multiple of %ld cylinders\n",
451 cpgflg ? "Cylinder" : "Warning: cylinder", mincpc);
452 sblock.fs_cpg = roundup(sblock.fs_cpg, mincpc);
457 * Must ensure there is enough space for inodes.
459 sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
460 while (sblock.fs_ipg > MAXIPG(&sblock)) {
462 sblock.fs_cpg -= mincpc;
463 sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
466 * Must ensure there is enough space to hold block map.
468 while (CGSIZE(&sblock) > (uint32_t)sblock.fs_bsize) {
470 sblock.fs_cpg -= mincpc;
471 sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
473 sblock.fs_fpg = (sblock.fs_cpg * sblock.fs_spc) / NSPF(&sblock);
474 if ((sblock.fs_cpg * sblock.fs_spc) % NSPB(&sblock) != 0) {
475 printf("panic (fs_cpg * fs_spc) %% NSPF != 0");
478 if (sblock.fs_cpg < mincpg) {
479 printf("cylinder groups must have at least %ld cylinders\n",
482 } else if (sblock.fs_cpg != cpg) {
485 else if (!mapcramped && !inodecramped)
488 if (mapcramped && inodecramped)
489 printf("Block size and bytes per inode restrict");
491 printf("Block size restricts");
493 printf("Bytes per inode restrict");
494 printf(" cylinders per group to %d.\n", sblock.fs_cpg);
499 sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock));
501 * Now have size for file system and nsect and ntrak.
502 * Determine number of cylinders and blocks in the file system.
504 sblock.fs_size = fssize = dbtofsb(&sblock, fssize);
505 sblock.fs_ncyl = fssize * NSPF(&sblock) / sblock.fs_spc;
506 if ((long)fssize * NSPF(&sblock) > sblock.fs_ncyl * sblock.fs_spc) {
510 if (sblock.fs_ncyl < 1) {
511 printf("file systems must have at least one cylinder\n");
515 * Determine feasability/values of rotational layout tables.
517 * The size of the rotational layout tables is limited by the
518 * size of the superblock, SBSIZE. The amount of space available
519 * for tables is calculated as (SBSIZE - sizeof (struct fs)).
520 * The size of these tables is inversely proportional to the block
521 * size of the file system. The size increases if sectors per track
522 * are not powers of two, because more cylinders must be described
523 * by the tables before the rotational pattern repeats (fs_cpc).
525 sblock.fs_interleave = interleave;
526 sblock.fs_trackskew = trackskew;
527 sblock.fs_npsect = nphyssectors;
528 sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
529 sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs));
530 if (sblock.fs_sbsize > SBSIZE)
531 sblock.fs_sbsize = SBSIZE;
532 if (sblock.fs_ntrak == 1) {
536 postblsize = sblock.fs_nrpos * sblock.fs_cpc * sizeof(int16_t);
537 rotblsize = sblock.fs_cpc * sblock.fs_spc / NSPB(&sblock);
538 totalsbsize = sizeof(struct fs) + rotblsize;
539 if (sblock.fs_nrpos == 8 && sblock.fs_cpc <= 16) {
540 /* use old static table space */
541 sblock.fs_postbloff = (char *)(&sblock.fs_opostbl[0][0]) -
542 (char *)(&sblock.fs_firstfield);
543 sblock.fs_rotbloff = &sblock.fs_space[0] -
544 (u_char *)(&sblock.fs_firstfield);
546 /* use dynamic table space */
547 sblock.fs_postbloff = &sblock.fs_space[0] -
548 (u_char *)(&sblock.fs_firstfield);
549 sblock.fs_rotbloff = sblock.fs_postbloff + postblsize;
550 totalsbsize += postblsize;
552 if (totalsbsize > SBSIZE ||
553 sblock.fs_nsect > (1 << NBBY) * NSPB(&sblock)) {
554 printf("%s %s %d %s %d.%s",
555 "Warning: insufficient space in super block for\n",
556 "rotational layout tables with nsect", sblock.fs_nsect,
557 "and ntrak", sblock.fs_ntrak,
558 "\nFile system performance may be impaired.\n");
562 sblock.fs_sbsize = fragroundup(&sblock, totalsbsize);
563 if (sblock.fs_sbsize > SBSIZE)
564 sblock.fs_sbsize = SBSIZE;
566 * calculate the available blocks for each rotational position
568 for (cylno = 0; cylno < sblock.fs_cpc; cylno++)
569 for (rpos = 0; rpos < sblock.fs_nrpos; rpos++)
570 fs_postbl(&sblock, cylno)[rpos] = -1;
571 for (i = (rotblsize - 1) * sblock.fs_frag;
572 i >= 0; i -= sblock.fs_frag) {
573 cylno = cbtocylno(&sblock, i);
574 rpos = cbtorpos(&sblock, i);
575 blk = fragstoblks(&sblock, i);
576 if (fs_postbl(&sblock, cylno)[rpos] == -1)
577 fs_rotbl(&sblock)[blk] = 0;
579 fs_rotbl(&sblock)[blk] =
580 fs_postbl(&sblock, cylno)[rpos] - blk;
581 fs_postbl(&sblock, cylno)[rpos] = blk;
585 * Compute/validate number of cylinder groups.
587 sblock.fs_ncg = sblock.fs_ncyl / sblock.fs_cpg;
588 if (sblock.fs_ncyl % sblock.fs_cpg)
590 sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock);
591 i = MIN(~sblock.fs_cgmask, sblock.fs_ncg - 1);
592 if (cgdmin(&sblock, i) - cgbase(&sblock, i) >= sblock.fs_fpg) {
593 printf("inode blocks/cyl group (%ld) >= data blocks (%ld)\n",
594 cgdmin(&sblock, i) - cgbase(&sblock, i) / sblock.fs_frag,
595 (long)(sblock.fs_fpg / sblock.fs_frag));
596 printf("number of cylinders per cylinder group (%d) %s.\n",
597 sblock.fs_cpg, "must be increased");
600 j = sblock.fs_ncg - 1;
601 if ((i = fssize - j * sblock.fs_fpg) < sblock.fs_fpg &&
602 cgdmin(&sblock, j) - cgbase(&sblock, j) > i) {
604 printf("Filesystem must have at least %d sectors\n",
606 (cgdmin(&sblock, 0) + 3 * sblock.fs_frag));
610 "Warning: inode blocks/cyl group (%ld) >= data blocks (%ld) in last\n",
611 (cgdmin(&sblock, j) - cgbase(&sblock, j)) / sblock.fs_frag,
614 " cylinder group. This implies %ld sector(s) cannot be allocated.\n",
617 sblock.fs_ncyl -= sblock.fs_ncyl % sblock.fs_cpg;
618 sblock.fs_size = fssize = sblock.fs_ncyl * sblock.fs_spc /
622 if (emitwarn && !mfs) {
623 printf("Warning: %lu sector(s) in last cylinder unallocated\n",
625 (fssize * NSPF(&sblock) - (sblock.fs_ncyl - 1)
629 * fill in remaining fields of the super block
631 sblock.fs_csaddr = cgdmin(&sblock, 0);
633 fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
635 * The superblock fields 'fs_csmask' and 'fs_csshift' are no
636 * longer used. However, we still initialise them so that the
637 * filesystem remains compatible with old kernels.
639 i = sblock.fs_bsize / sizeof(struct csum);
640 sblock.fs_csmask = ~(i - 1);
641 for (sblock.fs_csshift = 0; i > 1; i >>= 1)
643 fscs = (struct csum *)calloc(1, sblock.fs_cssize);
645 errx(31, "calloc failed");
646 sblock.fs_magic = FS_MAGIC;
647 sblock.fs_rotdelay = rotdelay;
648 sblock.fs_minfree = minfree;
649 sblock.fs_maxcontig = maxcontig;
650 sblock.fs_maxbpg = maxbpg;
651 sblock.fs_rps = rpm / 60;
652 sblock.fs_optim = opt;
653 sblock.fs_cgrotor = 0;
654 sblock.fs_cstotal.cs_ndir = 0;
655 sblock.fs_cstotal.cs_nbfree = 0;
656 sblock.fs_cstotal.cs_nifree = 0;
657 sblock.fs_cstotal.cs_nffree = 0;
662 sblock.fs_id[0] = (long)utime;
663 sblock.fs_id[1] = random();
667 * Dump out summary information about file system.
670 printf("%s:\t%d sectors in %d %s of %d tracks, %d sectors\n",
671 fsys, sblock.fs_size * NSPF(&sblock), sblock.fs_ncyl,
672 "cylinders", sblock.fs_ntrak, sblock.fs_nsect);
673 #define B2MBFACTOR (1 / (1024.0 * 1024.0))
674 printf("\t%.1fMB in %d cyl groups (%d c/g, %.2fMB/g, %d i/g)%s\n",
675 (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
676 sblock.fs_ncg, sblock.fs_cpg,
677 (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
679 sblock.fs_flags & FS_DOSOFTDEP ? " SOFTUPDATES" : "");
683 if (Eflag && !Nflag) {
684 printf("Erasing sectors [%"PRIu64" --- %"PRIu64"]\n",
685 (SBOFF + slice_offset) / sectorsize,
686 fsbtodb(&sblock,sblock.fs_size) -
687 ((SBOFF + slice_offset) / sectorsize) - 1);
688 erfs(SBOFF + slice_offset, (fsbtodb(&sblock,sblock.fs_size) -
689 ((SBOFF + slice_offset)/ sectorsize) - 1) *
690 (unsigned long long)sectorsize);
693 * Now build the cylinders group blocks and
694 * then print out indices of cylinder groups.
697 printf("super-block backups (for fsck -b #) at:\n");
699 width = charsperline();
700 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
701 initcg(cylno, utime);
704 j = snprintf(tmpbuf, sizeof(tmpbuf), " %ld%s",
705 fsbtodb(&sblock, cgsblock(&sblock, cylno)),
706 cylno < (sblock.fs_ncg-1) ? "," : "" );
707 if (i + j >= width) {
712 printf("%s", tmpbuf);
720 * Now construct the initial file system,
721 * then write out the super-block.
724 sblock.fs_time = utime;
725 wtfs((int)SBOFF / sectorsize, sbsize, (char *)&sblock);
726 for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize)
727 wtfs(fsbtodb(&sblock, sblock.fs_csaddr + numfrags(&sblock, i)),
728 sblock.fs_cssize - i < sblock.fs_bsize ?
729 sblock.fs_cssize - i : sblock.fs_bsize,
732 * Write out the duplicate super blocks
734 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
735 wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)),
736 sbsize, (char *)&sblock);
740 * NOTE: we no longer update information in the disklabel
744 * Notify parent process of success.
745 * Dissociate from session and tty.
747 * NOTE: We are the child and may receive a SIGINT due
748 * to losing the tty session? XXX
752 kill(mfs_ppid, SIGUSR1);
758 /* returns to mount_mfs (newfs) and issues the mount */
763 * Initialize a cylinder group.
766 initcg(int cylno, time_t utime)
768 daddr_t cbase, d, dlower, dupper, dmax, blkno;
777 * Determine block bounds for cylinder group.
778 * Allow space for super block summary information in first
781 cbase = cgbase(&sblock, cylno);
782 dmax = cbase + sblock.fs_fpg;
783 if (dmax > sblock.fs_size)
784 dmax = sblock.fs_size;
785 dlower = cgsblock(&sblock, cylno) - cbase;
786 dupper = cgdmin(&sblock, cylno) - cbase;
788 dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
790 memset(&acg, 0, sblock.fs_cgsize);
792 acg.cg_magic = CG_MAGIC;
794 if (cylno == sblock.fs_ncg - 1)
795 acg.cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg;
797 acg.cg_ncyl = sblock.fs_cpg;
798 acg.cg_niblk = sblock.fs_ipg;
799 acg.cg_ndblk = dmax - cbase;
800 if (sblock.fs_contigsumsize > 0)
801 acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag;
802 acg.cg_btotoff = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield);
803 acg.cg_boff = acg.cg_btotoff + sblock.fs_cpg * sizeof(int32_t);
804 acg.cg_iusedoff = acg.cg_boff +
805 sblock.fs_cpg * sblock.fs_nrpos * sizeof(u_int16_t);
806 acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, NBBY);
807 if (sblock.fs_contigsumsize <= 0) {
808 acg.cg_nextfreeoff = acg.cg_freeoff +
809 howmany(sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY);
811 acg.cg_clustersumoff = acg.cg_freeoff + howmany
812 (sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY) -
814 acg.cg_clustersumoff =
815 roundup(acg.cg_clustersumoff, sizeof(u_int32_t));
816 acg.cg_clusteroff = acg.cg_clustersumoff +
817 (sblock.fs_contigsumsize + 1) * sizeof(u_int32_t);
818 acg.cg_nextfreeoff = acg.cg_clusteroff + howmany
819 (sblock.fs_cpg * sblock.fs_spc / NSPB(&sblock), NBBY);
821 if (acg.cg_nextfreeoff - (long)(&acg.cg_firstfield) > sblock.fs_cgsize) {
822 printf("Panic: cylinder group too big\n");
825 acg.cg_cs.cs_nifree += sblock.fs_ipg;
827 for (k = 0; k < UFS_ROOTINO; k++) {
828 setbit(cg_inosused(&acg), k);
829 acg.cg_cs.cs_nifree--;
832 for (i = 0; i < sblock.fs_ipg / INOPF(&sblock); i += sblock.fs_frag) {
835 j < sblock.fs_bsize / sizeof(struct ufs1_dinode);
837 zino[j].di_gen = random();
840 wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
841 sblock.fs_bsize, (char *)zino);
845 * In cylno 0, beginning space is reserved
846 * for boot and super blocks.
848 for (d = 0; d < dlower; d += sblock.fs_frag) {
849 blkno = d / sblock.fs_frag;
850 setblock(&sblock, cg_blksfree(&acg), blkno);
851 if (sblock.fs_contigsumsize > 0)
852 setbit(cg_clustersfree(&acg), blkno);
853 acg.cg_cs.cs_nbfree++;
854 cg_blktot(&acg)[cbtocylno(&sblock, d)]++;
855 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))
856 [cbtorpos(&sblock, d)]++;
858 sblock.fs_dsize += dlower;
860 sblock.fs_dsize += acg.cg_ndblk - dupper;
861 if ((i = dupper % sblock.fs_frag)) {
862 acg.cg_frsum[sblock.fs_frag - i]++;
863 for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
864 setbit(cg_blksfree(&acg), dupper);
865 acg.cg_cs.cs_nffree++;
868 for (d = dupper; d + sblock.fs_frag <= dmax - cbase; ) {
869 blkno = d / sblock.fs_frag;
870 setblock(&sblock, cg_blksfree(&acg), blkno);
871 if (sblock.fs_contigsumsize > 0)
872 setbit(cg_clustersfree(&acg), blkno);
873 acg.cg_cs.cs_nbfree++;
874 cg_blktot(&acg)[cbtocylno(&sblock, d)]++;
875 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))
876 [cbtorpos(&sblock, d)]++;
879 if (d < dmax - cbase) {
880 acg.cg_frsum[dmax - cbase - d]++;
881 for (; d < dmax - cbase; d++) {
882 setbit(cg_blksfree(&acg), d);
883 acg.cg_cs.cs_nffree++;
886 if (sblock.fs_contigsumsize > 0) {
887 int32_t *sump = cg_clustersum(&acg);
888 u_char *mapp = cg_clustersfree(&acg);
893 for (i = 0; i < acg.cg_nclusterblks; i++) {
894 if ((map & bit) != 0) {
896 } else if (run != 0) {
897 if (run > sblock.fs_contigsumsize)
898 run = sblock.fs_contigsumsize;
902 if ((i & (NBBY - 1)) != (NBBY - 1)) {
910 if (run > sblock.fs_contigsumsize)
911 run = sblock.fs_contigsumsize;
915 sblock.fs_cstotal.cs_ndir += acg.cg_cs.cs_ndir;
916 sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree;
917 sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree;
918 sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree;
920 wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
921 sblock.fs_bsize, (char *)&acg);
925 * initialize the file system
927 struct ufs1_dinode node;
935 struct direct root_dir[] = {
936 { UFS_ROOTINO, sizeof(struct direct), DT_DIR, 1, "." },
937 { UFS_ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
939 { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 10, "lost+found" },
946 u_char d_name[MAXNAMLEN + 1];
948 { UFS_ROOTINO, sizeof(struct direct), 1, "." },
949 { UFS_ROOTINO, sizeof(struct direct), 2, ".." },
951 { LOSTFOUNDINO, sizeof(struct direct), 10, "lost+found" },
955 struct direct lost_found_dir[] = {
956 { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 1, "." },
957 { UFS_ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
958 { 0, DIRBLKSIZ, 0, 0, 0 },
960 struct odirect olost_found_dir[] = {
961 { LOSTFOUNDINO, sizeof(struct direct), 1, "." },
962 { UFS_ROOTINO, sizeof(struct direct), 2, ".." },
963 { 0, DIRBLKSIZ, 0, 0 },
976 * initialize the node
978 node.di_atime = utime;
979 node.di_mtime = utime;
980 node.di_ctime = utime;
983 * create the lost+found directory
986 makedir((struct direct *)olost_found_dir, 2);
987 for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ)
988 memmove(&buf[i], &olost_found_dir[2],
989 DIRSIZ(0, &olost_found_dir[2]));
991 makedir(lost_found_dir, 2);
992 for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ)
993 memmove(&buf[i], &lost_found_dir[2],
994 DIRSIZ(0, &lost_found_dir[2]));
996 node.di_mode = IFDIR | UMASK;
998 node.di_size = sblock.fs_bsize;
999 node.di_db[0] = alloc(node.di_size, node.di_mode);
1000 node.di_blocks = btodb(fragroundup(&sblock, node.di_size));
1001 wtfs(fsbtodb(&sblock, node.di_db[0]), node.di_size, buf);
1002 iput(&node, LOSTFOUNDINO);
1005 * create the root directory
1008 node.di_mode = IFDIR | 01777;
1010 node.di_mode = IFDIR | UMASK;
1011 node.di_nlink = PREDEFDIR;
1013 node.di_size = makedir((struct direct *)oroot_dir, PREDEFDIR);
1015 node.di_size = makedir(root_dir, PREDEFDIR);
1016 node.di_db[0] = alloc(sblock.fs_fsize, node.di_mode);
1017 node.di_blocks = btodb(fragroundup(&sblock, node.di_size));
1018 wtfs(fsbtodb(&sblock, node.di_db[0]), sblock.fs_fsize, buf);
1019 iput(&node, UFS_ROOTINO);
1023 * construct a set of directory entries in "buf".
1024 * return size of directory.
1027 makedir(struct direct *protodir, int entries)
1032 spcleft = DIRBLKSIZ;
1033 for (cp = buf, i = 0; i < entries - 1; i++) {
1034 protodir[i].d_reclen = DIRSIZ(0, &protodir[i]);
1035 memmove(cp, &protodir[i], protodir[i].d_reclen);
1036 cp += protodir[i].d_reclen;
1037 spcleft -= protodir[i].d_reclen;
1039 protodir[i].d_reclen = spcleft;
1040 memmove(cp, &protodir[i], DIRSIZ(0, &protodir[i]));
1045 * allocate a block or frag
1048 alloc(int size, int mode)
1053 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
1055 if (acg.cg_magic != CG_MAGIC) {
1056 printf("cg 0: bad magic number\n");
1059 if (acg.cg_cs.cs_nbfree == 0) {
1060 printf("first cylinder group ran out of space\n");
1063 for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag)
1064 if (isblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag))
1066 printf("internal error: can't find block in cyl 0\n");
1069 blkno = fragstoblks(&sblock, d);
1070 clrblock(&sblock, cg_blksfree(&acg), blkno);
1071 if (sblock.fs_contigsumsize > 0)
1072 clrbit(cg_clustersfree(&acg), blkno);
1073 acg.cg_cs.cs_nbfree--;
1074 sblock.fs_cstotal.cs_nbfree--;
1075 fscs[0].cs_nbfree--;
1077 acg.cg_cs.cs_ndir++;
1078 sblock.fs_cstotal.cs_ndir++;
1081 cg_blktot(&acg)[cbtocylno(&sblock, d)]--;
1082 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))[cbtorpos(&sblock, d)]--;
1083 if (size != sblock.fs_bsize) {
1084 frag = howmany(size, sblock.fs_fsize);
1085 fscs[0].cs_nffree += sblock.fs_frag - frag;
1086 sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag;
1087 acg.cg_cs.cs_nffree += sblock.fs_frag - frag;
1088 acg.cg_frsum[sblock.fs_frag - frag]++;
1089 for (i = frag; i < sblock.fs_frag; i++)
1090 setbit(cg_blksfree(&acg), d + i);
1092 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
1098 * Calculate number of inodes per group.
1101 calcipg(long cylspg, long bpcg, off_t *usedbp)
1104 long ipg, new_ipg, ncg, ncyl;
1108 * Prepare to scale by fssize / (number of sectors in cylinder groups).
1109 * Note that fssize is still in sectors, not filesystem blocks.
1111 ncyl = howmany(fssize, (u_int)secpercyl);
1112 ncg = howmany(ncyl, cylspg);
1114 * Iterate a few times to allow for ipg depending on itself.
1117 for (i = 0; i < 10; i++) {
1118 usedb = (sblock.fs_iblkno + ipg / INOPF(&sblock))
1119 * NSPF(&sblock) * (off_t)sectorsize;
1120 new_ipg = (cylspg * (quad_t)bpcg - usedb) / density * fssize
1121 / ncg / secpercyl / cylspg;
1122 new_ipg = roundup(new_ipg, INOPB(&sblock));
1132 * Allocate an inode on the disk
1135 iput(struct ufs1_dinode *ip, ino_t ino)
1137 struct ufs1_dinode inobuf[MAXINOPB];
1141 ip->di_gen = random();
1143 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
1145 if (acg.cg_magic != CG_MAGIC) {
1146 printf("cg 0: bad magic number\n");
1149 acg.cg_cs.cs_nifree--;
1150 setbit(cg_inosused(&acg), ino);
1151 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
1153 sblock.fs_cstotal.cs_nifree--;
1154 fscs[0].cs_nifree--;
1155 if (ino >= (uint32_t)sblock.fs_ipg * (uint32_t)sblock.fs_ncg) {
1156 printf("fsinit: inode value out of range (%ju).\n",
1160 d = fsbtodb(&sblock, ino_to_fsba(&sblock, ino));
1161 rdfs(d, sblock.fs_bsize, (char *)inobuf);
1162 inobuf[ino_to_fsbo(&sblock, ino)] = *ip;
1163 wtfs(d, sblock.fs_bsize, (char *)inobuf);
1167 * Parent notifies child that it can proceed with the newfs and mount
1168 * operation (occurs after parent has copied the underlying filesystem
1169 * if the -C option was specified (for MFS), or immediately after the
1170 * parent forked the child otherwise).
1173 parentready(__unused int signo)
1175 parentready_signalled = 1;
1179 * Notify parent process that the filesystem has created itself successfully.
1181 * We have to wait until the mount has actually completed!
1184 started(__unused int signo)
1186 int retry = 100; /* 10 seconds, 100ms */
1188 while (mfs_ppid && retry) {
1192 stat(mfs_mtpt, &st) < 0 ||
1193 st.st_dev != mfs_mtstat.st_dev
1201 fatal("mfs mount failed waiting for mount to go active");
1202 } else if (copyroot) {
1203 FSPaste(mfs_mtpt, copyroot, copyhlinks);
1209 * read a block from the file system
1212 rdfs(daddr_t bno, int size, char *bf)
1218 memmove(bf, membase + bno * sectorsize, size);
1221 if (lseek(fsi, (off_t)bno * sectorsize, 0) < 0) {
1222 printf("seek error: %ld\n", (long)bno);
1225 n = read(fsi, bf, size);
1227 printf("read error: %ld\n", (long)bno);
1232 #define WCSIZE (128 * 1024)
1233 daddr_t wc_sect; /* units of sectorsize */
1234 int wc_end; /* bytes */
1235 static char wc[WCSIZE]; /* bytes */
1238 * Flush dirty write behind buffer.
1245 if (lseek(fso, (off_t)wc_sect * sectorsize, SEEK_SET) < 0) {
1246 printf("seek error: %ld\n", (long)wc_sect);
1247 err(35, "wtfs - writecombine");
1249 n = write(fso, wc, wc_end);
1251 printf("write error: %ld\n", (long)wc_sect);
1252 err(36, "wtfs - writecombine");
1259 * Issue ioctl to erase range of sectors using TRIM
1262 erfs(off_t byte_start, off_t size)
1265 ioarg[0] = byte_start;
1267 if (ioctl(fsi, DAIOCTRIM, ioarg) < 0) {
1268 err(37, "Device trim failed\n");
1273 * write a block to the file system
1276 wtfs(daddr_t bno, int size, char *bf)
1282 memmove(membase + bno * sectorsize, bf, size);
1288 if (wc_end == 0 && size <= WCSIZE) {
1290 bcopy(bf, wc, size);
1292 if (wc_end < WCSIZE)
1296 if ((off_t)wc_sect * sectorsize + wc_end == (off_t)bno * sectorsize &&
1297 wc_end + size <= WCSIZE) {
1298 bcopy(bf, wc + wc_end, size);
1300 if (wc_end < WCSIZE)
1307 if (lseek(fso, (off_t)bno * sectorsize, SEEK_SET) < 0) {
1308 printf("seek error: %ld\n", (long)bno);
1311 n = write(fso, bf, size);
1313 printf("write error: fso %d blk %ld %d/%d\n",
1314 fso, (long)bno, n, size);
1320 * check if a block is available
1323 isblock(struct fs *fs, unsigned char *cp, int h)
1327 switch (fs->fs_frag) {
1329 return (cp[h] == 0xff);
1331 mask = 0x0f << ((h & 0x1) << 2);
1332 return ((cp[h >> 1] & mask) == mask);
1334 mask = 0x03 << ((h & 0x3) << 1);
1335 return ((cp[h >> 2] & mask) == mask);
1337 mask = 0x01 << (h & 0x7);
1338 return ((cp[h >> 3] & mask) == mask);
1340 fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag);
1346 * take a block out of the map
1349 clrblock(struct fs *fs, unsigned char *cp, int h)
1351 switch ((fs)->fs_frag) {
1356 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
1359 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
1362 cp[h >> 3] &= ~(0x01 << (h & 0x7));
1365 fprintf(stderr, "clrblock bad fs_frag %d\n", fs->fs_frag);
1371 * put a block into the map
1374 setblock(struct fs *fs, unsigned char *cp, int h)
1376 switch (fs->fs_frag) {
1381 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
1384 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
1387 cp[h >> 3] |= (0x01 << (h & 0x7));
1390 fprintf(stderr, "setblock bad fs_frag %d\n", fs->fs_frag);
1396 * Determine the number of characters in a
1408 if (ioctl(0, TIOCGWINSZ, &ws) != -1)
1409 columns = ws.ws_col;
1410 if (columns == 0 && (cp = getenv("COLUMNS")))
1413 columns = 80; /* last resort */