2 * Copyright (c) 1980, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid[] = "@(#)mkfs.c 8.11 (Berkeley) 5/3/95";
38 static const char rcsid[] =
48 #include <sys/param.h>
50 #include <sys/types.h>
52 #include <sys/resource.h>
54 #include <ufs/ufs/dinode.h>
55 #include <ufs/ufs/dir.h>
56 #include <ufs/ffs/fs.h>
57 #include <sys/disklabel.h>
60 #include <sys/ioctl.h>
64 * make filesystem for cylinder-group style filesystems
68 * We limit the size of the inode map to be no more than a
69 * third of the cylinder group space, since we must leave at
70 * least an equal amount of space for the block map.
72 * N.B.: MAXIPG must be a multiple of INOPB(fs).
74 #define MAXIPG(fs) roundup((fs)->fs_bsize * NBBY / 3, INOPB(fs))
77 #define MAXINOPB (MAXBSIZE / sizeof(struct dinode))
78 #define POWEROF2(num) (((num) & ((num) - 1)) == 0)
84 #define sblock fsun.fs
85 static struct csum *fscs;
93 static struct dinode zino[MAXBSIZE / sizeof(struct dinode)];
96 static daddr_t alloc(int size, int mode);
97 static long calcipg(long lcpg, long bpcg, off_t *usedbp);
98 static int charsperline(void);
99 static void clrblock(struct fs *, unsigned char *, int);
100 static void fsinit(time_t);
101 static int ilog2(int);
102 static void initcg(int, time_t);
103 static int isblock(struct fs *, unsigned char *, int);
104 static void iput(struct dinode *, ino_t);
105 static int makedir(struct direct *, int);
106 static void rdfs(daddr_t, int, char *);
107 static void setblock(struct fs *, unsigned char *, int);
108 static void wtfs(daddr_t, int, char *);
109 static void wtfsflush(void);
112 mkfs(struct partition *pp, char *fsys)
114 long i, mincpc, mincpg, inospercg;
115 long cylno, j, lwarn = 0;
116 long used, mincpgcnt, bpcg;
118 long mapcramped, inodecramped;
122 char tmpbuf[100]; /* XXX this will break in about 2,500 years */
128 if (!Rflag && !randinit) {
132 sblock.fs_inodefmt = FS_44INODEFMT;
133 sblock.fs_maxsymlinklen = MAXSYMLINKLEN;
135 sblock.fs_flags |= FS_DOSOFTDEP;
137 * Validate the given filesystem size.
138 * Verify that its last block can actually be accessed.
141 printf("preposterous size %d\n", fssize), exit(13);
142 wtfs(fssize - (realsectorsize / DEV_BSIZE), realsectorsize,
145 * collect and verify the sector and track info
147 sblock.fs_nsect = secpercyl;
149 if (sblock.fs_nsect <= 0)
150 printf("preposterous nsect %d\n", sblock.fs_nsect), exit(15);
152 * collect and verify the filesystem density info
154 sblock.fs_avgfilesize = avgfilesize;
155 sblock.fs_avgfpdir = avgfilesperdir;
156 if (sblock.fs_avgfilesize <= 0)
157 printf("illegal expected average file size %d\n",
158 sblock.fs_avgfilesize), exit(14);
159 if (sblock.fs_avgfpdir <= 0)
160 printf("illegal expected number of files per directory %d\n",
161 sblock.fs_avgfpdir), exit(15);
163 * collect and verify the block and fragment sizes
165 sblock.fs_bsize = bsize;
166 sblock.fs_fsize = fsize;
167 if (!POWEROF2(sblock.fs_bsize)) {
168 printf("block size must be a power of 2, not %d\n",
172 if (!POWEROF2(sblock.fs_fsize)) {
173 printf("fragment size must be a power of 2, not %d\n",
177 if (sblock.fs_fsize < sectorsize) {
178 printf("increasing fragment size from %d to sector size (%d)\n",
179 sblock.fs_fsize, sectorsize);
180 sblock.fs_fsize = sectorsize;
182 if (sblock.fs_bsize < MINBSIZE) {
183 printf("increasing block size from %d to minimum (%d)\n",
184 sblock.fs_bsize, MINBSIZE);
185 sblock.fs_bsize = MINBSIZE;
187 if (sblock.fs_bsize < sblock.fs_fsize) {
188 printf("increasing block size from %d to fragment size (%d)\n",
189 sblock.fs_bsize, sblock.fs_fsize);
190 sblock.fs_bsize = sblock.fs_fsize;
192 if (sblock.fs_fsize * MAXFRAG < sblock.fs_bsize) {
194 "increasing fragment size from %d to block size / %d (%d)\n",
195 sblock.fs_fsize, MAXFRAG, sblock.fs_bsize / MAXFRAG);
196 sblock.fs_fsize = sblock.fs_bsize / MAXFRAG;
198 sblock.fs_bmask = ~(sblock.fs_bsize - 1);
199 sblock.fs_fmask = ~(sblock.fs_fsize - 1);
200 sblock.fs_qbmask = ~sblock.fs_bmask;
201 sblock.fs_qfmask = ~sblock.fs_fmask;
202 sblock.fs_bshift = ilog2(sblock.fs_bsize);
203 sblock.fs_fshift = ilog2(sblock.fs_fsize);
204 sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize);
205 sblock.fs_fragshift = ilog2(sblock.fs_frag);
206 if (sblock.fs_frag > MAXFRAG) {
207 printf("fragment size %d is still too small (can't happen)\n",
208 sblock.fs_bsize / MAXFRAG);
212 sblock.fs_nindir = sblock.fs_bsize / sizeof(daddr_t);
213 sblock.fs_inopb = sblock.fs_bsize / sizeof(struct dinode);
214 sblock.fs_nspf = sblock.fs_fsize / sectorsize;
215 sblock.fs_fsbtodb = ilog2(NSPF(&sblock));
217 roundup(howmany(BBSIZE + SBSIZE, sblock.fs_fsize), sblock.fs_frag);
218 sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno +
219 roundup(howmany(SBSIZE, sblock.fs_fsize), sblock.fs_frag));
220 sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag;
222 roundup(howmany(sblock.fs_nsect, NSPF(&sblock)), sblock.fs_frag);
223 sblock.fs_cgmask = 0xffffffff;
224 sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
225 for (sizepb = sblock.fs_bsize, i = 0; i < NIADDR; i++) {
226 sizepb *= NINDIR(&sblock);
227 sblock.fs_maxfilesize += sizepb;
230 * Validate specified/determined secpercyl
231 * and calculate minimum cylinders per group.
233 sblock.fs_spc = secpercyl;
234 for (sblock.fs_cpc = NSPB(&sblock), i = sblock.fs_spc;
235 sblock.fs_cpc > 1 && (i & 1) == 0;
236 sblock.fs_cpc >>= 1, i >>= 1)
238 mincpc = sblock.fs_cpc;
239 bpcg = sblock.fs_spc * sectorsize;
240 inospercg = roundup(bpcg / sizeof(struct dinode), INOPB(&sblock));
241 if (inospercg > MAXIPG(&sblock))
242 inospercg = MAXIPG(&sblock);
243 used = (sblock.fs_iblkno + inospercg / INOPF(&sblock)) * NSPF(&sblock);
244 mincpgcnt = howmany(sblock.fs_cgoffset * (~sblock.fs_cgmask) + used,
246 mincpg = roundup(mincpgcnt, mincpc);
248 * Ensure that cylinder group with mincpg has enough space
251 sblock.fs_cpg = mincpg;
252 sblock.fs_ipg = inospercg;
254 sblock.fs_contigsumsize = MIN(maxcontig, FS_MAXCONTIG);
256 while (CGSIZE(&sblock) > sblock.fs_bsize) {
258 if (sblock.fs_bsize < MAXBSIZE) {
259 sblock.fs_bsize <<= 1;
265 mincpg = roundup(mincpgcnt, mincpc);
266 sblock.fs_cpg = mincpg;
268 sblock.fs_frag <<= 1;
269 sblock.fs_fragshift += 1;
270 if (sblock.fs_frag <= MAXFRAG)
273 if (sblock.fs_fsize == sblock.fs_bsize) {
274 printf("There is no block size that");
275 printf(" can support this disk\n");
278 sblock.fs_frag >>= 1;
279 sblock.fs_fragshift -= 1;
280 sblock.fs_fsize <<= 1;
281 sblock.fs_nspf <<= 1;
284 * Ensure that cylinder group with mincpg has enough space for inodes.
287 inospercg = calcipg(mincpg, bpcg, &usedb);
288 sblock.fs_ipg = inospercg;
289 while (inospercg > MAXIPG(&sblock)) {
291 if (mincpc == 1 || sblock.fs_frag == 1 ||
292 sblock.fs_bsize == MINBSIZE)
294 printf("With a block size of %d %s %d\n", sblock.fs_bsize,
295 "minimum bytes per inode is",
296 (int)((mincpg * (off_t)bpcg - usedb) /
297 MAXIPG(&sblock) + 1));
298 sblock.fs_bsize >>= 1;
299 sblock.fs_frag >>= 1;
300 sblock.fs_fragshift -= 1;
302 sblock.fs_cpg = roundup(mincpgcnt, mincpc);
303 if (CGSIZE(&sblock) > sblock.fs_bsize) {
304 sblock.fs_bsize <<= 1;
307 mincpg = sblock.fs_cpg;
308 inospercg = calcipg(mincpg, bpcg, &usedb);
309 sblock.fs_ipg = inospercg;
312 if (inospercg > MAXIPG(&sblock)) {
313 printf("Minimum bytes per inode is %d\n",
314 (int)((mincpg * (off_t)bpcg - usedb) /
315 MAXIPG(&sblock) + 1));
316 } else if (!mapcramped) {
317 printf("With %d bytes per inode, ", density);
318 printf("minimum cylinders per group is %ld\n", mincpg);
322 printf("With %d sectors per cylinder, ", sblock.fs_spc);
323 printf("minimum cylinders per group is %ld\n", mincpg);
325 if (inodecramped || mapcramped) {
326 if (sblock.fs_bsize != bsize)
327 printf("%s to be changed from %d to %d\n",
328 "This requires the block size",
329 bsize, sblock.fs_bsize);
330 if (sblock.fs_fsize != fsize)
331 printf("\t%s to be changed from %d to %d\n",
332 "and the fragment size", fsize, sblock.fs_fsize);
336 * Calculate the number of cylinders per group
339 if (sblock.fs_cpg % mincpc != 0) {
340 printf("%s groups must have a multiple of %ld cylinders\n",
341 cpgflg ? "Cylinder" : "Warning: cylinder", mincpc);
342 sblock.fs_cpg = roundup(sblock.fs_cpg, mincpc);
347 * Must ensure there is enough space for inodes.
349 sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
350 while (sblock.fs_ipg > MAXIPG(&sblock)) {
352 sblock.fs_cpg -= mincpc;
353 sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
356 * Must ensure there is enough space to hold block map.
358 while (CGSIZE(&sblock) > sblock.fs_bsize) {
360 sblock.fs_cpg -= mincpc;
361 sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
363 sblock.fs_fpg = (sblock.fs_cpg * sblock.fs_spc) / NSPF(&sblock);
364 if ((sblock.fs_cpg * sblock.fs_spc) % NSPB(&sblock) != 0) {
365 printf("panic (fs_cpg * fs_spc) %% NSPF != 0");
368 if (sblock.fs_cpg < mincpg) {
369 printf("cylinder groups must have at least %ld cylinders\n",
372 } else if (cpgflg && sblock.fs_cpg != cpg) {
373 if (!mapcramped && !inodecramped)
375 if (mapcramped && inodecramped)
376 printf("Block size and bytes per inode restrict");
378 printf("Block size restricts");
380 printf("Bytes per inode restrict");
381 printf(" cylinders per group to %d.\n", sblock.fs_cpg);
385 sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock));
387 * Now have size for filesystem and nsect and ntrak.
388 * Determine number of cylinders and blocks in the filesystem.
390 sblock.fs_size = fssize = dbtofsb(&sblock, fssize);
391 sblock.fs_ncyl = fssize * NSPF(&sblock) / sblock.fs_spc;
392 if (fssize * NSPF(&sblock) > sblock.fs_ncyl * sblock.fs_spc) {
396 if (sblock.fs_ncyl < 1) {
397 printf("filesystems must have at least one cylinder\n");
401 * Determine feasability/values of rotational layout tables.
403 * The size of the rotational layout tables is limited by the
404 * size of the superblock, SBSIZE. The amount of space available
405 * for tables is calculated as (SBSIZE - sizeof (struct fs)).
406 * The size of these tables is inversely proportional to the block
407 * size of the filesystem. The size increases if sectors per track
408 * are not powers of two, because more cylinders must be described
409 * by the tables before the rotational pattern repeats (fs_cpc).
411 sblock.fs_interleave = 1;
412 sblock.fs_trackskew = 0;
413 sblock.fs_npsect = secpercyl;
414 sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
415 sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs));
416 if (sblock.fs_sbsize > SBSIZE)
417 sblock.fs_sbsize = SBSIZE;
420 * Compute/validate number of cylinder groups.
422 sblock.fs_ncg = sblock.fs_ncyl / sblock.fs_cpg;
423 if (sblock.fs_ncyl % sblock.fs_cpg)
425 sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock);
426 i = MIN(~sblock.fs_cgmask, sblock.fs_ncg - 1);
427 if (cgdmin(&sblock, i) - cgbase(&sblock, i) >= sblock.fs_fpg) {
428 printf("inode blocks/cyl group (%ld) >= data blocks (%ld)\n",
429 cgdmin(&sblock, i) - cgbase(&sblock, i) / sblock.fs_frag,
430 (long)(sblock.fs_fpg / sblock.fs_frag));
431 printf("number of cylinders per cylinder group (%d) %s.\n",
432 sblock.fs_cpg, "must be increased");
435 j = sblock.fs_ncg - 1;
436 if ((i = fssize - j * sblock.fs_fpg) < sblock.fs_fpg &&
437 cgdmin(&sblock, j) - cgbase(&sblock, j) > i) {
439 printf("Filesystem must have at least %d sectors\n",
441 (cgdmin(&sblock, 0) + 3 * sblock.fs_frag));
445 "Warning: inode blocks/cyl group (%ld) >= data blocks (%ld) in last\n",
446 (cgdmin(&sblock, j) - cgbase(&sblock, j)) / sblock.fs_frag,
449 " cylinder group. This implies %ld sector(s) cannot be allocated.\n",
452 sblock.fs_ncyl -= sblock.fs_ncyl % sblock.fs_cpg;
453 sblock.fs_size = fssize = sblock.fs_ncyl * sblock.fs_spc /
458 printf("Warning: %d sector(s) in last cylinder unallocated\n",
460 (fssize * NSPF(&sblock) - (sblock.fs_ncyl - 1) *
464 * fill in remaining fields of the super block
466 sblock.fs_csaddr = cgdmin(&sblock, 0);
468 fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
470 * The superblock fields 'fs_csmask' and 'fs_csshift' are no
471 * longer used. However, we still initialise them so that the
472 * filesystem remains compatible with old kernels.
474 i = sblock.fs_bsize / sizeof(struct csum);
475 sblock.fs_csmask = ~(i - 1);
476 sblock.fs_csshift = ilog2(i);
477 fscs = (struct csum *)calloc(1, sblock.fs_cssize);
479 errx(31, "calloc failed");
480 sblock.fs_magic = FS_MAGIC;
481 sblock.fs_rotdelay = 0;
482 sblock.fs_minfree = minfree;
483 sblock.fs_maxcontig = maxcontig;
484 sblock.fs_maxbpg = maxbpg;
486 sblock.fs_optim = opt;
487 sblock.fs_cgrotor = 0;
488 sblock.fs_cstotal.cs_ndir = 0;
489 sblock.fs_cstotal.cs_nbfree = 0;
490 sblock.fs_cstotal.cs_nifree = 0;
491 sblock.fs_cstotal.cs_nffree = 0;
495 sblock.fs_id[0] = (long)utime;
496 sblock.fs_id[1] = random();
499 * Dump out summary information about filesystem.
501 printf("%s:\t%d sectors in %d %s of %d tracks, %d sectors\n",
502 fsys, sblock.fs_size * NSPF(&sblock), sblock.fs_ncyl,
503 "cylinders", sblock.fs_ntrak, sblock.fs_nsect);
504 #define B2MBFACTOR (1 / (1024.0 * 1024.0))
505 printf("\t%.1fMB in %d cyl groups (%d c/g, %.2fMB/g, %d i/g)%s\n",
506 (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
507 sblock.fs_ncg, sblock.fs_cpg,
508 (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
510 sblock.fs_flags & FS_DOSOFTDEP ? " SOFTUPDATES" : "");
513 * Now build the cylinders group blocks and
514 * then print out indices of cylinder groups.
516 printf("super-block backups (for fsck -b #) at:\n");
518 width = charsperline();
519 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
520 initcg(cylno, utime);
521 j = snprintf(tmpbuf, sizeof(tmpbuf), " %ld%s",
522 fsbtodb(&sblock, cgsblock(&sblock, cylno)),
523 cylno < (sblock.fs_ncg-1) ? "," : "");
525 tmpbuf[j = 0] = '\0';
526 if (i + j >= width) {
531 printf("%s", tmpbuf);
538 * Now construct the initial filesystem,
539 * then write out the super-block.
542 sblock.fs_time = utime;
543 wtfs((int)SBOFF / sectorsize, SBSIZE, (char *)&sblock);
544 for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize)
545 wtfs(fsbtodb(&sblock, sblock.fs_csaddr + numfrags(&sblock, i)),
546 sblock.fs_cssize - i < sblock.fs_bsize ?
547 sblock.fs_cssize - i : sblock.fs_bsize,
550 * Write out the duplicate super blocks
552 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
553 wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)),
554 SBSIZE, (char *)&sblock);
557 * Update information about this partion in pack
558 * label, to that it may be updated on disk.
561 pp->p_fstype = FS_BSDFFS;
562 pp->p_fsize = sblock.fs_fsize;
563 pp->p_frag = sblock.fs_frag;
564 pp->p_cpg = sblock.fs_cpg;
569 * Initialize a cylinder group.
572 initcg(int cylno, time_t utime)
574 daddr_t cbase, d, dlower, dupper, dmax, blkno;
579 * Determine block bounds for cylinder group.
580 * Allow space for super block summary information in first
583 cbase = cgbase(&sblock, cylno);
584 dmax = cbase + sblock.fs_fpg;
585 if (dmax > sblock.fs_size)
586 dmax = sblock.fs_size;
587 dlower = cgsblock(&sblock, cylno) - cbase;
588 dupper = cgdmin(&sblock, cylno) - cbase;
590 dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
592 memset(&acg, 0, sblock.fs_cgsize);
594 acg.cg_magic = CG_MAGIC;
596 if (cylno == sblock.fs_ncg - 1)
597 acg.cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg;
599 acg.cg_ncyl = sblock.fs_cpg;
600 acg.cg_niblk = sblock.fs_ipg;
601 acg.cg_ndblk = dmax - cbase;
602 if (sblock.fs_contigsumsize > 0)
603 acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag;
604 acg.cg_btotoff = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield);
605 acg.cg_boff = acg.cg_btotoff + sblock.fs_cpg * sizeof(int32_t);
606 acg.cg_iusedoff = acg.cg_boff +
607 sblock.fs_cpg * sizeof(u_int16_t);
608 acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, NBBY);
609 if (sblock.fs_contigsumsize <= 0) {
610 acg.cg_nextfreeoff = acg.cg_freeoff +
611 howmany(sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock),
614 acg.cg_clustersumoff = acg.cg_freeoff + howmany
615 (sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY) -
617 acg.cg_clustersumoff =
618 roundup(acg.cg_clustersumoff, sizeof(u_int32_t));
619 acg.cg_clusteroff = acg.cg_clustersumoff +
620 (sblock.fs_contigsumsize + 1) * sizeof(u_int32_t);
621 acg.cg_nextfreeoff = acg.cg_clusteroff + howmany
622 (sblock.fs_cpg * sblock.fs_spc / NSPB(&sblock), NBBY);
624 if (acg.cg_nextfreeoff - (long)(&acg.cg_firstfield) >
626 printf("Panic: cylinder group too big\n");
629 acg.cg_cs.cs_nifree += sblock.fs_ipg;
631 for (i = 0; i < ROOTINO; i++) {
632 setbit(cg_inosused(&acg), i);
633 acg.cg_cs.cs_nifree--;
635 for (i = 0; i < sblock.fs_ipg / INOPF(&sblock); i += sblock.fs_frag) {
636 for (j = 0; j < sblock.fs_bsize / sizeof(struct dinode); j++)
637 zino[j].di_gen = random();
638 wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
639 sblock.fs_bsize, (char *)zino);
643 * In cylno 0, beginning space is reserved
644 * for boot and super blocks.
646 for (d = 0; d < dlower; d += sblock.fs_frag) {
647 blkno = d / sblock.fs_frag;
648 setblock(&sblock, cg_blksfree(&acg), blkno);
649 if (sblock.fs_contigsumsize > 0)
650 setbit(cg_clustersfree(&acg), blkno);
651 acg.cg_cs.cs_nbfree++;
652 cg_blktot(&acg)[cbtocylno(&sblock, d)]++;
653 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))
654 [cbtorpos(&sblock, d)]++;
656 sblock.fs_dsize += dlower;
658 sblock.fs_dsize += acg.cg_ndblk - dupper;
659 if ((i = dupper % sblock.fs_frag)) {
660 acg.cg_frsum[sblock.fs_frag - i]++;
661 for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
662 setbit(cg_blksfree(&acg), dupper);
663 acg.cg_cs.cs_nffree++;
666 for (d = dupper; d + sblock.fs_frag <= dmax - cbase;) {
667 blkno = d / sblock.fs_frag;
668 setblock(&sblock, cg_blksfree(&acg), blkno);
669 if (sblock.fs_contigsumsize > 0)
670 setbit(cg_clustersfree(&acg), blkno);
671 acg.cg_cs.cs_nbfree++;
672 cg_blktot(&acg)[cbtocylno(&sblock, d)]++;
673 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))
674 [cbtorpos(&sblock, d)]++;
677 if (d < dmax - cbase) {
678 acg.cg_frsum[dmax - cbase - d]++;
679 for (; d < dmax - cbase; d++) {
680 setbit(cg_blksfree(&acg), d);
681 acg.cg_cs.cs_nffree++;
684 if (sblock.fs_contigsumsize > 0) {
685 int32_t *sump = cg_clustersum(&acg);
686 u_char *mapp = cg_clustersfree(&acg);
691 for (i = 0; i < acg.cg_nclusterblks; i++) {
692 if ((map & bit) != 0)
695 if (run > sblock.fs_contigsumsize)
696 run = sblock.fs_contigsumsize;
700 if ((i & (NBBY - 1)) != NBBY - 1)
708 if (run > sblock.fs_contigsumsize)
709 run = sblock.fs_contigsumsize;
713 sblock.fs_cstotal.cs_ndir += acg.cg_cs.cs_ndir;
714 sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree;
715 sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree;
716 sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree;
718 wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
719 sblock.fs_bsize, (char *)&acg);
723 * initialize the filesystem
729 struct direct root_dir[] = {
730 { ROOTINO, sizeof(struct direct), DT_DIR, 1, "." },
731 { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
737 u_char d_name[MAXNAMLEN + 1];
739 { ROOTINO, sizeof(struct direct), 1, "." },
740 { ROOTINO, sizeof(struct direct), 2, ".." },
749 * initialize the node
751 node.di_atime = utime;
752 node.di_mtime = utime;
753 node.di_ctime = utime;
755 * create the root directory
757 node.di_mode = IFDIR | UMASK;
758 node.di_nlink = PREDEFDIR;
759 node.di_size = makedir(root_dir, PREDEFDIR);
760 node.di_db[0] = alloc(sblock.fs_fsize, node.di_mode);
761 node.di_blocks = btodb(fragroundup(&sblock, node.di_size));
762 wtfs(fsbtodb(&sblock, node.di_db[0]), sblock.fs_fsize, buf);
763 iput(&node, ROOTINO);
767 * construct a set of directory entries in "buf".
768 * return size of directory.
771 makedir(struct direct *protodir, int entries)
777 for (cp = buf, i = 0; i < entries - 1; i++) {
778 protodir[i].d_reclen = DIRSIZ(0, &protodir[i]);
779 memmove(cp, &protodir[i], protodir[i].d_reclen);
780 cp += protodir[i].d_reclen;
781 spcleft -= protodir[i].d_reclen;
783 protodir[i].d_reclen = spcleft;
784 memmove(cp, &protodir[i], DIRSIZ(0, &protodir[i]));
789 * allocate a block or frag
792 alloc(int size, int mode)
797 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
799 if (acg.cg_magic != CG_MAGIC) {
800 printf("cg 0: bad magic number\n");
803 if (acg.cg_cs.cs_nbfree == 0) {
804 printf("first cylinder group ran out of space\n");
807 for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag)
808 if (isblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag))
810 printf("internal error: can't find block in cyl 0\n");
813 blkno = fragstoblks(&sblock, d);
814 clrblock(&sblock, cg_blksfree(&acg), blkno);
815 if (sblock.fs_contigsumsize > 0)
816 clrbit(cg_clustersfree(&acg), blkno);
817 acg.cg_cs.cs_nbfree--;
818 sblock.fs_cstotal.cs_nbfree--;
822 sblock.fs_cstotal.cs_ndir++;
825 cg_blktot(&acg)[cbtocylno(&sblock, d)]--;
826 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))[cbtorpos(&sblock, d)]--;
827 if (size != sblock.fs_bsize) {
828 frag = howmany(size, sblock.fs_fsize);
829 fscs[0].cs_nffree += sblock.fs_frag - frag;
830 sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag;
831 acg.cg_cs.cs_nffree += sblock.fs_frag - frag;
832 acg.cg_frsum[sblock.fs_frag - frag]++;
833 for (i = frag; i < sblock.fs_frag; i++)
834 setbit(cg_blksfree(&acg), d + i);
836 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
842 * Calculate number of inodes per group.
845 calcipg(long lcpg, long bpcg, off_t *usedbp)
848 long ipg, new_ipg, ncg, ncyl;
852 * Prepare to scale by fssize / (number of sectors in cylinder groups).
853 * Note that fssize is still in sectors, not filesystem blocks.
855 ncyl = howmany(fssize, (u_int)secpercyl);
856 ncg = howmany(ncyl, lcpg);
858 * Iterate a few times to allow for ipg depending on itself.
861 for (i = 0; i < 10; i++) {
862 usedb = (sblock.fs_iblkno + ipg / INOPF(&sblock)) *
863 NSPF(&sblock) * (off_t)sectorsize;
864 new_ipg = (lcpg * (quad_t)bpcg - usedb) / density *
865 fssize / ncg / secpercyl / lcpg;
866 new_ipg = roundup(new_ipg, INOPB(&sblock));
876 * Allocate an inode on the disk
879 iput(struct dinode *ip, ino_t ino)
881 struct dinode lbuf[MAXINOPB];
885 ip->di_gen = random();
886 c = ino_to_cg(&sblock, ino);
887 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
889 if (acg.cg_magic != CG_MAGIC) {
890 printf("cg 0: bad magic number\n");
893 acg.cg_cs.cs_nifree--;
894 setbit(cg_inosused(&acg), ino);
895 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
897 sblock.fs_cstotal.cs_nifree--;
899 if (ino >= sblock.fs_ipg * sblock.fs_ncg) {
900 printf("fsinit: inode value out of range (%d).\n", ino);
903 d = fsbtodb(&sblock, ino_to_fsba(&sblock, ino));
904 rdfs(d, sblock.fs_bsize, (char *)lbuf);
905 lbuf[ino_to_fsbo(&sblock, ino)] = *ip;
906 wtfs(d, sblock.fs_bsize, (char *)lbuf);
910 * read a block from the filesystem
913 rdfs(daddr_t bno, int size, char *bf)
918 if (lseek(fso, (off_t)bno * sectorsize, 0) < 0) {
919 printf("seek error: %ld\n", (long)bno);
922 n = read(fso, bf, size);
924 printf("read error: %ld\n", (long)bno);
929 #define WCSIZE (128 * 1024)
930 daddr_t wc_sect; /* units of sectorsize */
931 int wc_end; /* bytes */
932 static char wc[WCSIZE]; /* bytes */
935 * Flush dirty write behind buffer.
942 if (lseek(fso, (off_t)wc_sect * sectorsize, SEEK_SET) < 0) {
943 printf("seek error: %ld\n", (long)wc_sect);
944 err(35, "wtfs - writecombine");
946 n = write(fso, wc, wc_end);
948 printf("write error: %ld\n", (long)wc_sect);
949 err(36, "wtfs - writecombine");
956 * write a block to the filesystem
959 wtfs(daddr_t bno, int size, char *bf)
966 if (wc_end == 0 && size <= WCSIZE) {
974 if ((off_t)wc_sect * sectorsize + wc_end == (off_t)bno * sectorsize &&
975 wc_end + size <= WCSIZE) {
976 bcopy(bf, wc + wc_end, size);
985 if (lseek(fso, (off_t)bno * sectorsize, SEEK_SET) < 0) {
986 printf("seek error: %ld\n", (long)bno);
989 n = write(fso, bf, size);
991 printf("write error: %ld\n", (long)bno);
997 * check if a block is available
1000 isblock(struct fs *fs, unsigned char *cp, int h)
1004 switch (fs->fs_frag) {
1006 return (cp[h] == 0xff);
1008 mask = 0x0f << ((h & 0x1) << 2);
1009 return ((cp[h >> 1] & mask) == mask);
1011 mask = 0x03 << ((h & 0x3) << 1);
1012 return ((cp[h >> 2] & mask) == mask);
1014 mask = 0x01 << (h & 0x7);
1015 return ((cp[h >> 3] & mask) == mask);
1017 fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag);
1023 * take a block out of the map
1026 clrblock(struct fs *fs, unsigned char *cp, int h)
1028 switch ((fs)->fs_frag) {
1033 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
1036 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
1039 cp[h >> 3] &= ~(0x01 << (h & 0x7));
1042 fprintf(stderr, "clrblock bad fs_frag %d\n", fs->fs_frag);
1048 * put a block into the map
1051 setblock(struct fs *fs, unsigned char *cp, int h)
1053 switch (fs->fs_frag) {
1058 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
1061 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
1064 cp[h >> 3] |= (0x01 << (h & 0x7));
1067 fprintf(stderr, "setblock bad fs_frag %d\n", fs->fs_frag);
1073 * Determine the number of characters in a
1085 if (ioctl(0, TIOCGWINSZ, &ws) != -1)
1086 columns = ws.ws_col;
1087 if (columns == 0 && (cp = getenv("COLUMNS")))
1090 columns = 80; /* last resort */
1099 for (n = 0; n < sizeof(n) * NBBY; n++)
1102 errx(1, "ilog2: %d is not a power of 2\n", val);