newfs(8): Remove an unused variable.
[dragonfly.git] / sbin / newfs / mkfs.c
blob70e5057cfc8a7f3a9c2701317e0b0f29cbd67b71
1 /*
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
7 * are met:
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
31 * SUCH DAMAGE.
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 $
37 #include "defs.h"
39 #include <inttypes.h>
40 #include <stdlib.h>
41 #include <sys/ioctl_compat.h>
44 * make file system for cylinder-group style file systems
48 * We limit the size of the inode map to be no more than a
49 * third of the cylinder group space, since we must leave at
50 * least an equal amount of space for the block map.
52 * N.B.: MAXIPG must be a multiple of INOPB(fs).
54 #define MAXIPG(fs) roundup((fs)->fs_bsize * NBBY / 3, INOPB(fs))
56 #define UMASK 0755
57 #define MAXINOPB (MAXBSIZE / sizeof(struct ufs1_dinode))
58 #define POWEROF2(num) (((num) & ((num) - 1)) == 0)
60 #ifdef STANDALONE
61 #error "mkfs.c: STANDALONE compilation no longer supported"
62 #endif
65 * variables set up by front end.
67 extern int mfs; /* run as the memory based filesystem */
68 extern char *mfs_mtpt; /* mount point for mfs */
69 extern struct stat mfs_mtstat; /* stat prior to mount */
70 extern int Lflag; /* add a volume label */
71 extern int Nflag; /* run mkfs without writing file system */
72 extern int Oflag; /* format as an 4.3BSD file system */
73 extern int Uflag; /* enable soft updates for file system */
74 extern int Eflag; /* erase contents using TRIM */
75 extern uint64_t slice_offset; /* Pysical device slice offset */
76 extern u_long fssize; /* file system size */
77 extern int ntracks; /* # tracks/cylinder */
78 extern int nsectors; /* # sectors/track */
79 extern int nphyssectors; /* # sectors/track including spares */
80 extern int secpercyl; /* sectors per cylinder */
81 extern int sectorsize; /* bytes/sector */
82 extern int realsectorsize; /* bytes/sector in hardware*/
83 extern int rpm; /* revolutions/minute of drive */
84 extern int interleave; /* hardware sector interleave */
85 extern int trackskew; /* sector 0 skew, per track */
86 extern int fsize; /* fragment size */
87 extern int bsize; /* block size */
88 extern int cpg; /* cylinders/cylinder group */
89 extern int cpgflg; /* cylinders/cylinder group flag was given */
90 extern int minfree; /* free space threshold */
91 extern int opt; /* optimization preference (space or time) */
92 extern int density; /* number of bytes per inode */
93 extern int maxcontig; /* max contiguous blocks to allocate */
94 extern int rotdelay; /* rotational delay between blocks */
95 extern int maxbpg; /* maximum blocks per file in a cyl group */
96 extern int nrpos; /* # of distinguished rotational positions */
97 extern int bbsize; /* boot block size */
98 extern int sbsize; /* superblock size */
99 extern int avgfilesize; /* expected average file size */
100 extern int avgfilesperdir; /* expected number of files per directory */
101 extern caddr_t membase; /* start address of memory based filesystem */
102 extern char * filename;
103 extern u_char *volumelabel; /* volume label for filesystem */
104 extern struct disktab geom;
106 extern void fatal(const char *fmt, ...);
108 union {
109 struct fs fs;
110 char pad[SBSIZE];
111 } fsun;
112 #define sblock fsun.fs
113 struct csum *fscs;
115 union {
116 struct cg cg;
117 char pad[MAXBSIZE];
118 } cgun;
119 #define acg cgun.cg
121 struct ufs1_dinode zino[MAXBSIZE / sizeof(struct ufs1_dinode)];
123 int fsi, fso;
124 static fsnode_t copyroot;
125 static fsnode_t copyhlinks;
126 #ifdef FSIRAND
127 int randinit;
128 #endif
129 daddr_t alloc(int, int);
130 long calcipg(long, long, off_t *);
131 static int charsperline(void);
132 void clrblock(struct fs *, unsigned char *, int);
133 void fsinit(time_t);
134 void initcg(int, time_t);
135 int isblock(struct fs *, unsigned char *, int);
136 void iput(struct ufs1_dinode *, ino_t);
137 int makedir(struct direct *, int);
138 void parentready(int);
139 void rdfs(daddr_t, int, char *);
140 void setblock(struct fs *, unsigned char *, int);
141 void started(int);
142 void erfs(off_t, off_t);
143 void wtfs(daddr_t, int, char *);
144 void wtfsflush(void);
146 int mfs_ppid = 0;
147 int parentready_signalled;
149 void
150 mkfs(char *fsys, int fi, int fo, const char *mfscopy)
152 long i, mincpc, mincpg, inospercg;
153 long cylno, rpos, blk, j, emitwarn = 0;
154 long used, mincpgcnt, bpcg;
155 off_t usedb;
156 long mapcramped, inodecramped;
157 long postblsize, rotblsize, totalsbsize;
158 int status, fd;
159 time_t utime;
160 quad_t sizepb;
161 int width;
162 char tmpbuf[100]; /* XXX this will break in about 2,500 years */
164 time(&utime);
165 #ifdef FSIRAND
166 if (!randinit) {
167 randinit = 1;
168 srandomdev();
170 #endif
171 if (mfs) {
172 int omask;
173 pid_t child;
175 mfs_ppid = getpid();
176 signal(SIGUSR1, parentready);
177 if ((child = fork()) != 0) {
179 * Parent
181 if (child == -1)
182 err(10, "mfs");
183 if (mfscopy)
184 copyroot = FSCopy(&copyhlinks, mfscopy);
185 signal(SIGUSR1, started);
186 kill(child, SIGUSR1);
187 while (waitpid(child, &status, 0) != child)
189 exit(WEXITSTATUS(status));
190 /* NOTREACHED */
194 * Child
196 omask = sigblock(sigmask(SIGUSR1));
197 while (parentready_signalled == 0)
198 sigpause(omask);
199 sigsetmask(omask);
200 if (filename != NULL) {
201 unsigned char buf[BUFSIZ];
202 unsigned long l, l1;
203 ssize_t w;
205 fd = open(filename, O_RDWR|O_TRUNC|O_CREAT, 0644);
206 if(fd < 0)
207 err(12, "%s", filename);
208 l1 = fssize * sectorsize;
209 if (l1 > BUFSIZ)
210 l1 = BUFSIZ;
211 for (l = 0; l < fssize * (u_long)sectorsize; l += l1) {
212 w = write(fd, buf, l1);
213 if (w < 0 || (u_long)w != l1)
214 err(12, "%s", filename);
216 membase = mmap(NULL, fssize * sectorsize,
217 PROT_READ|PROT_WRITE,
218 MAP_SHARED, fd, 0);
219 if (membase == MAP_FAILED)
220 err(12, "mmap");
221 close(fd);
222 } else {
223 membase = mmap(NULL, fssize * sectorsize,
224 PROT_READ|PROT_WRITE,
225 MAP_SHARED|MAP_ANON, -1, 0);
226 if (membase == MAP_FAILED)
227 errx(13, "mmap (anonymous memory) failed");
230 fsi = fi;
231 fso = fo;
232 if (Oflag) {
233 sblock.fs_inodefmt = FS_42INODEFMT;
234 sblock.fs_maxsymlinklen = 0;
235 } else {
236 sblock.fs_inodefmt = FS_44INODEFMT;
237 sblock.fs_maxsymlinklen = MAXSYMLINKLEN;
239 if (Uflag)
240 sblock.fs_flags |= FS_DOSOFTDEP;
241 if (Lflag)
242 strlcpy(sblock.fs_volname, volumelabel, MAXVOLLEN);
245 * Validate the given file system size.
246 * Verify that its last block can actually be accessed.
248 if (fssize == 0)
249 printf("preposterous size %lu\n", fssize), exit(13);
250 wtfs(fssize - (realsectorsize / DEV_BSIZE), realsectorsize,
251 (char *)&sblock);
253 * collect and verify the sector and track info
255 sblock.fs_nsect = nsectors;
256 sblock.fs_ntrak = ntracks;
257 if (sblock.fs_ntrak <= 0)
258 printf("preposterous ntrak %d\n", sblock.fs_ntrak), exit(14);
259 if (sblock.fs_nsect <= 0)
260 printf("preposterous nsect %d\n", sblock.fs_nsect), exit(15);
262 * collect and verify the filesystem density info
264 sblock.fs_avgfilesize = avgfilesize;
265 sblock.fs_avgfpdir = avgfilesperdir;
266 if (sblock.fs_avgfilesize <= 0)
267 printf("illegal expected average file size %d\n",
268 sblock.fs_avgfilesize), exit(14);
269 if (sblock.fs_avgfpdir <= 0)
270 printf("illegal expected number of files per directory %d\n",
271 sblock.fs_avgfpdir), exit(15);
273 * collect and verify the block and fragment sizes
275 sblock.fs_bsize = bsize;
276 sblock.fs_fsize = fsize;
277 if (!POWEROF2(sblock.fs_bsize)) {
278 printf("block size must be a power of 2, not %d\n",
279 sblock.fs_bsize);
280 exit(16);
282 if (!POWEROF2(sblock.fs_fsize)) {
283 printf("fragment size must be a power of 2, not %d\n",
284 sblock.fs_fsize);
285 exit(17);
287 if (sblock.fs_fsize < sectorsize) {
288 printf("fragment size %d is too small, minimum is %d\n",
289 sblock.fs_fsize, sectorsize);
290 exit(18);
292 if (sblock.fs_bsize < MINBSIZE) {
293 printf("block size %d is too small, minimum is %d\n",
294 sblock.fs_bsize, MINBSIZE);
295 exit(19);
297 if (sblock.fs_bsize < sblock.fs_fsize) {
298 printf("block size (%d) cannot be smaller than fragment size (%d)\n",
299 sblock.fs_bsize, sblock.fs_fsize);
300 exit(20);
302 sblock.fs_bmask = ~(sblock.fs_bsize - 1);
303 sblock.fs_fmask = ~(sblock.fs_fsize - 1);
304 sblock.fs_qbmask = ~sblock.fs_bmask;
305 sblock.fs_qfmask = ~sblock.fs_fmask;
306 for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1)
307 sblock.fs_bshift++;
308 for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1)
309 sblock.fs_fshift++;
310 sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize);
311 for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1)
312 sblock.fs_fragshift++;
313 if (sblock.fs_frag > MAXFRAG) {
314 printf("fragment size %d is too small, minimum with block size %d is %d\n",
315 sblock.fs_fsize, sblock.fs_bsize,
316 sblock.fs_bsize / MAXFRAG);
317 exit(21);
319 sblock.fs_nrpos = nrpos;
320 sblock.fs_nindir = sblock.fs_bsize / sizeof(daddr_t);
321 sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs1_dinode);
322 sblock.fs_nspf = sblock.fs_fsize / sectorsize;
323 for (sblock.fs_fsbtodb = 0, i = NSPF(&sblock); i > 1; i >>= 1)
324 sblock.fs_fsbtodb++;
325 sblock.fs_sblkno =
326 roundup(howmany(bbsize + sbsize, sblock.fs_fsize), sblock.fs_frag);
327 sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno +
328 roundup(howmany(sbsize, sblock.fs_fsize), sblock.fs_frag));
329 sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag;
330 sblock.fs_cgoffset = roundup(
331 howmany(sblock.fs_nsect, NSPF(&sblock)), sblock.fs_frag);
332 for (sblock.fs_cgmask = 0xffffffff, i = sblock.fs_ntrak; i > 1; i >>= 1)
333 sblock.fs_cgmask <<= 1;
334 if (!POWEROF2(sblock.fs_ntrak))
335 sblock.fs_cgmask <<= 1;
336 sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
337 for (sizepb = sblock.fs_bsize, i = 0; i < NIADDR; i++) {
338 sizepb *= NINDIR(&sblock);
339 sblock.fs_maxfilesize += sizepb;
342 * Validate specified/determined secpercyl
343 * and calculate minimum cylinders per group.
345 sblock.fs_spc = secpercyl;
346 for (sblock.fs_cpc = NSPB(&sblock), i = sblock.fs_spc;
347 sblock.fs_cpc > 1 && (i & 1) == 0;
348 sblock.fs_cpc >>= 1, i >>= 1)
349 /* void */;
350 mincpc = sblock.fs_cpc;
351 bpcg = sblock.fs_spc * sectorsize;
352 inospercg = roundup(bpcg / sizeof(struct ufs1_dinode), INOPB(&sblock));
353 if (inospercg > MAXIPG(&sblock))
354 inospercg = MAXIPG(&sblock);
355 used = (sblock.fs_iblkno + inospercg / INOPF(&sblock)) * NSPF(&sblock);
356 mincpgcnt = howmany(sblock.fs_cgoffset * (~sblock.fs_cgmask) + used,
357 sblock.fs_spc);
358 mincpg = roundup(mincpgcnt, mincpc);
360 * Ensure that cylinder group with mincpg has enough space
361 * for block maps.
363 sblock.fs_cpg = mincpg;
364 sblock.fs_ipg = inospercg;
365 if (maxcontig > 1)
366 sblock.fs_contigsumsize = MIN(maxcontig, FS_MAXCONTIG);
367 mapcramped = 0;
368 while (CGSIZE(&sblock) > (uint32_t)sblock.fs_bsize) {
369 mapcramped = 1;
370 if (sblock.fs_bsize < MAXBSIZE) {
371 sblock.fs_bsize <<= 1;
372 if ((i & 1) == 0) {
373 i >>= 1;
374 } else {
375 sblock.fs_cpc <<= 1;
376 mincpc <<= 1;
377 mincpg = roundup(mincpgcnt, mincpc);
378 sblock.fs_cpg = mincpg;
380 sblock.fs_frag <<= 1;
381 sblock.fs_fragshift += 1;
382 if (sblock.fs_frag <= MAXFRAG)
383 continue;
385 if (sblock.fs_fsize == sblock.fs_bsize) {
386 printf("There is no block size that");
387 printf(" can support this disk\n");
388 exit(22);
390 sblock.fs_frag >>= 1;
391 sblock.fs_fragshift -= 1;
392 sblock.fs_fsize <<= 1;
393 sblock.fs_nspf <<= 1;
396 * Ensure that cylinder group with mincpg has enough space for inodes.
398 inodecramped = 0;
399 inospercg = calcipg(mincpg, bpcg, &usedb);
400 sblock.fs_ipg = inospercg;
401 while (inospercg > MAXIPG(&sblock)) {
402 inodecramped = 1;
403 if (mincpc == 1 || sblock.fs_frag == 1 ||
404 sblock.fs_bsize == MINBSIZE)
405 break;
406 printf("With a block size of %d %s %d\n", sblock.fs_bsize,
407 "minimum bytes per inode is",
408 (int)((mincpg * (off_t)bpcg - usedb)
409 / MAXIPG(&sblock) + 1));
410 sblock.fs_bsize >>= 1;
411 sblock.fs_frag >>= 1;
412 sblock.fs_fragshift -= 1;
413 mincpc >>= 1;
414 sblock.fs_cpg = roundup(mincpgcnt, mincpc);
415 if (CGSIZE(&sblock) > (uint32_t)sblock.fs_bsize) {
416 sblock.fs_bsize <<= 1;
417 break;
419 mincpg = sblock.fs_cpg;
420 inospercg = calcipg(mincpg, bpcg, &usedb);
421 sblock.fs_ipg = inospercg;
423 if (inodecramped) {
424 if (inospercg > MAXIPG(&sblock)) {
425 printf("Minimum bytes per inode is %d\n",
426 (int)((mincpg * (off_t)bpcg - usedb)
427 / MAXIPG(&sblock) + 1));
428 } else if (!mapcramped) {
429 printf("With %d bytes per inode, ", density);
430 printf("minimum cylinders per group is %ld\n", mincpg);
433 if (mapcramped) {
434 printf("With %d sectors per cylinder, ", sblock.fs_spc);
435 printf("minimum cylinders per group is %ld\n", mincpg);
437 if (inodecramped || mapcramped) {
438 if (sblock.fs_bsize != bsize)
439 printf("%s to be changed from %d to %d\n",
440 "This requires the block size",
441 bsize, sblock.fs_bsize);
442 if (sblock.fs_fsize != fsize)
443 printf("\t%s to be changed from %d to %d\n",
444 "and the fragment size",
445 fsize, sblock.fs_fsize);
446 exit(23);
449 * Calculate the number of cylinders per group
451 sblock.fs_cpg = cpg;
452 if (sblock.fs_cpg % mincpc != 0) {
453 printf("%s groups must have a multiple of %ld cylinders\n",
454 cpgflg ? "Cylinder" : "Warning: cylinder", mincpc);
455 sblock.fs_cpg = roundup(sblock.fs_cpg, mincpc);
456 if (!cpgflg)
457 cpg = sblock.fs_cpg;
460 * Must ensure there is enough space for inodes.
462 sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
463 while (sblock.fs_ipg > MAXIPG(&sblock)) {
464 inodecramped = 1;
465 sblock.fs_cpg -= mincpc;
466 sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
469 * Must ensure there is enough space to hold block map.
471 while (CGSIZE(&sblock) > (uint32_t)sblock.fs_bsize) {
472 mapcramped = 1;
473 sblock.fs_cpg -= mincpc;
474 sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
476 sblock.fs_fpg = (sblock.fs_cpg * sblock.fs_spc) / NSPF(&sblock);
477 if ((sblock.fs_cpg * sblock.fs_spc) % NSPB(&sblock) != 0) {
478 printf("panic (fs_cpg * fs_spc) %% NSPF != 0");
479 exit(24);
481 if (sblock.fs_cpg < mincpg) {
482 printf("cylinder groups must have at least %ld cylinders\n",
483 mincpg);
484 exit(25);
485 } else if (sblock.fs_cpg != cpg) {
486 if (!cpgflg && !mfs)
487 printf("Warning: ");
488 else if (!mapcramped && !inodecramped)
489 exit(26);
490 if (!mfs) {
491 if (mapcramped && inodecramped)
492 printf("Block size and bytes per inode restrict");
493 else if (mapcramped)
494 printf("Block size restricts");
495 else
496 printf("Bytes per inode restrict");
497 printf(" cylinders per group to %d.\n", sblock.fs_cpg);
499 if (cpgflg)
500 exit(27);
502 sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock));
504 * Now have size for file system and nsect and ntrak.
505 * Determine number of cylinders and blocks in the file system.
507 sblock.fs_size = fssize = dbtofsb(&sblock, fssize);
508 sblock.fs_ncyl = fssize * NSPF(&sblock) / sblock.fs_spc;
509 if ((long)fssize * NSPF(&sblock) > sblock.fs_ncyl * sblock.fs_spc) {
510 sblock.fs_ncyl++;
511 emitwarn = 1;
513 if (sblock.fs_ncyl < 1) {
514 printf("file systems must have at least one cylinder\n");
515 exit(28);
518 * Determine feasability/values of rotational layout tables.
520 * The size of the rotational layout tables is limited by the
521 * size of the superblock, SBSIZE. The amount of space available
522 * for tables is calculated as (SBSIZE - sizeof (struct fs)).
523 * The size of these tables is inversely proportional to the block
524 * size of the file system. The size increases if sectors per track
525 * are not powers of two, because more cylinders must be described
526 * by the tables before the rotational pattern repeats (fs_cpc).
528 sblock.fs_interleave = interleave;
529 sblock.fs_trackskew = trackskew;
530 sblock.fs_npsect = nphyssectors;
531 sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
532 sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs));
533 if (sblock.fs_sbsize > SBSIZE)
534 sblock.fs_sbsize = SBSIZE;
535 if (sblock.fs_ntrak == 1) {
536 sblock.fs_cpc = 0;
537 goto next;
539 postblsize = sblock.fs_nrpos * sblock.fs_cpc * sizeof(int16_t);
540 rotblsize = sblock.fs_cpc * sblock.fs_spc / NSPB(&sblock);
541 totalsbsize = sizeof(struct fs) + rotblsize;
542 if (sblock.fs_nrpos == 8 && sblock.fs_cpc <= 16) {
543 /* use old static table space */
544 sblock.fs_postbloff = (char *)(&sblock.fs_opostbl[0][0]) -
545 (char *)(&sblock.fs_firstfield);
546 sblock.fs_rotbloff = &sblock.fs_space[0] -
547 (u_char *)(&sblock.fs_firstfield);
548 } else {
549 /* use dynamic table space */
550 sblock.fs_postbloff = &sblock.fs_space[0] -
551 (u_char *)(&sblock.fs_firstfield);
552 sblock.fs_rotbloff = sblock.fs_postbloff + postblsize;
553 totalsbsize += postblsize;
555 if (totalsbsize > SBSIZE ||
556 sblock.fs_nsect > (1 << NBBY) * NSPB(&sblock)) {
557 printf("%s %s %d %s %d.%s",
558 "Warning: insufficient space in super block for\n",
559 "rotational layout tables with nsect", sblock.fs_nsect,
560 "and ntrak", sblock.fs_ntrak,
561 "\nFile system performance may be impaired.\n");
562 sblock.fs_cpc = 0;
563 goto next;
565 sblock.fs_sbsize = fragroundup(&sblock, totalsbsize);
566 if (sblock.fs_sbsize > SBSIZE)
567 sblock.fs_sbsize = SBSIZE;
569 * calculate the available blocks for each rotational position
571 for (cylno = 0; cylno < sblock.fs_cpc; cylno++)
572 for (rpos = 0; rpos < sblock.fs_nrpos; rpos++)
573 fs_postbl(&sblock, cylno)[rpos] = -1;
574 for (i = (rotblsize - 1) * sblock.fs_frag;
575 i >= 0; i -= sblock.fs_frag) {
576 cylno = cbtocylno(&sblock, i);
577 rpos = cbtorpos(&sblock, i);
578 blk = fragstoblks(&sblock, i);
579 if (fs_postbl(&sblock, cylno)[rpos] == -1)
580 fs_rotbl(&sblock)[blk] = 0;
581 else
582 fs_rotbl(&sblock)[blk] =
583 fs_postbl(&sblock, cylno)[rpos] - blk;
584 fs_postbl(&sblock, cylno)[rpos] = blk;
586 next:
588 * Compute/validate number of cylinder groups.
590 sblock.fs_ncg = sblock.fs_ncyl / sblock.fs_cpg;
591 if (sblock.fs_ncyl % sblock.fs_cpg)
592 sblock.fs_ncg++;
593 sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock);
594 i = MIN(~sblock.fs_cgmask, sblock.fs_ncg - 1);
595 if (cgdmin(&sblock, i) - cgbase(&sblock, i) >= sblock.fs_fpg) {
596 printf("inode blocks/cyl group (%ld) >= data blocks (%ld)\n",
597 cgdmin(&sblock, i) - cgbase(&sblock, i) / sblock.fs_frag,
598 (long)(sblock.fs_fpg / sblock.fs_frag));
599 printf("number of cylinders per cylinder group (%d) %s.\n",
600 sblock.fs_cpg, "must be increased");
601 exit(29);
603 j = sblock.fs_ncg - 1;
604 if ((i = fssize - j * sblock.fs_fpg) < sblock.fs_fpg &&
605 cgdmin(&sblock, j) - cgbase(&sblock, j) > i) {
606 if (j == 0) {
607 printf("Filesystem must have at least %d sectors\n",
608 NSPF(&sblock) *
609 (cgdmin(&sblock, 0) + 3 * sblock.fs_frag));
610 exit(30);
612 printf(
613 "Warning: inode blocks/cyl group (%ld) >= data blocks (%ld) in last\n",
614 (cgdmin(&sblock, j) - cgbase(&sblock, j)) / sblock.fs_frag,
615 i / sblock.fs_frag);
616 printf(
617 " cylinder group. This implies %ld sector(s) cannot be allocated.\n",
618 i * NSPF(&sblock));
619 sblock.fs_ncg--;
620 sblock.fs_ncyl -= sblock.fs_ncyl % sblock.fs_cpg;
621 sblock.fs_size = fssize = sblock.fs_ncyl * sblock.fs_spc /
622 NSPF(&sblock);
623 emitwarn = 0;
625 if (emitwarn && !mfs) {
626 printf("Warning: %lu sector(s) in last cylinder unallocated\n",
627 sblock.fs_spc -
628 (fssize * NSPF(&sblock) - (sblock.fs_ncyl - 1)
629 * sblock.fs_spc));
632 * fill in remaining fields of the super block
634 sblock.fs_csaddr = cgdmin(&sblock, 0);
635 sblock.fs_cssize =
636 fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
638 * The superblock fields 'fs_csmask' and 'fs_csshift' are no
639 * longer used. However, we still initialise them so that the
640 * filesystem remains compatible with old kernels.
642 i = sblock.fs_bsize / sizeof(struct csum);
643 sblock.fs_csmask = ~(i - 1);
644 for (sblock.fs_csshift = 0; i > 1; i >>= 1)
645 sblock.fs_csshift++;
646 fscs = (struct csum *)calloc(1, sblock.fs_cssize);
647 if (fscs == NULL)
648 errx(31, "calloc failed");
649 sblock.fs_magic = FS_MAGIC;
650 sblock.fs_rotdelay = rotdelay;
651 sblock.fs_minfree = minfree;
652 sblock.fs_maxcontig = maxcontig;
653 sblock.fs_maxbpg = maxbpg;
654 sblock.fs_rps = rpm / 60;
655 sblock.fs_optim = opt;
656 sblock.fs_cgrotor = 0;
657 sblock.fs_cstotal.cs_ndir = 0;
658 sblock.fs_cstotal.cs_nbfree = 0;
659 sblock.fs_cstotal.cs_nifree = 0;
660 sblock.fs_cstotal.cs_nffree = 0;
661 sblock.fs_fmod = 0;
662 sblock.fs_ronly = 0;
663 sblock.fs_clean = 1;
664 #ifdef FSIRAND
665 sblock.fs_id[0] = (long)utime;
666 sblock.fs_id[1] = random();
667 #endif
670 * Dump out summary information about file system.
672 if (!mfs) {
673 printf("%s:\t%d sectors in %d %s of %d tracks, %d sectors\n",
674 fsys, sblock.fs_size * NSPF(&sblock), sblock.fs_ncyl,
675 "cylinders", sblock.fs_ntrak, sblock.fs_nsect);
676 #define B2MBFACTOR (1 / (1024.0 * 1024.0))
677 printf("\t%.1fMB in %d cyl groups (%d c/g, %.2fMB/g, %d i/g)%s\n",
678 (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
679 sblock.fs_ncg, sblock.fs_cpg,
680 (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
681 sblock.fs_ipg,
682 sblock.fs_flags & FS_DOSOFTDEP ? " SOFTUPDATES" : "");
683 #undef B2MBFACTOR
686 if (Eflag && !Nflag) {
687 printf("Erasing sectors [%"PRIu64" --- %"PRIu64"]\n",
688 (SBOFF + slice_offset) / sectorsize,
689 fsbtodb(&sblock,sblock.fs_size) -
690 ((SBOFF + slice_offset) / sectorsize) - 1);
691 erfs(SBOFF + slice_offset, (fsbtodb(&sblock,sblock.fs_size) -
692 ((SBOFF + slice_offset)/ sectorsize) - 1) *
693 (unsigned long long)sectorsize);
696 * Now build the cylinders group blocks and
697 * then print out indices of cylinder groups.
699 if (!mfs)
700 printf("super-block backups (for fsck -b #) at:\n");
701 i = 0;
702 width = charsperline();
703 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
704 initcg(cylno, utime);
705 if (mfs)
706 continue;
707 j = snprintf(tmpbuf, sizeof(tmpbuf), " %ld%s",
708 fsbtodb(&sblock, cgsblock(&sblock, cylno)),
709 cylno < (sblock.fs_ncg-1) ? "," : "" );
710 if (i + j >= width) {
711 printf("\n");
712 i = 0;
714 i += j;
715 printf("%s", tmpbuf);
716 fflush(stdout);
718 if (!mfs)
719 printf("\n");
720 if (Nflag && !mfs)
721 exit(0);
723 * Now construct the initial file system,
724 * then write out the super-block.
726 fsinit(utime);
727 sblock.fs_time = utime;
728 wtfs((int)SBOFF / sectorsize, sbsize, (char *)&sblock);
729 for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize)
730 wtfs(fsbtodb(&sblock, sblock.fs_csaddr + numfrags(&sblock, i)),
731 sblock.fs_cssize - i < sblock.fs_bsize ?
732 sblock.fs_cssize - i : sblock.fs_bsize,
733 ((char *)fscs) + i);
735 * Write out the duplicate super blocks
737 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
738 wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)),
739 sbsize, (char *)&sblock);
740 wtfsflush();
743 * NOTE: we no longer update information in the disklabel
747 * Notify parent process of success.
748 * Dissociate from session and tty.
750 * NOTE: We are the child and may receive a SIGINT due
751 * to losing the tty session? XXX
753 if (mfs) {
754 /* YYY */
755 kill(mfs_ppid, SIGUSR1);
756 setsid();
757 close(0);
758 close(1);
759 close(2);
760 chdir("/");
761 /* returns to mount_mfs (newfs) and issues the mount */
766 * Initialize a cylinder group.
768 void
769 initcg(int cylno, time_t utime)
771 daddr_t cbase, d, dlower, dupper, dmax, blkno;
772 long i;
773 unsigned long k;
774 struct csum *cs;
775 #ifdef FSIRAND
776 uint32_t j;
777 #endif
780 * Determine block bounds for cylinder group.
781 * Allow space for super block summary information in first
782 * cylinder group.
784 cbase = cgbase(&sblock, cylno);
785 dmax = cbase + sblock.fs_fpg;
786 if (dmax > sblock.fs_size)
787 dmax = sblock.fs_size;
788 dlower = cgsblock(&sblock, cylno) - cbase;
789 dupper = cgdmin(&sblock, cylno) - cbase;
790 if (cylno == 0)
791 dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
792 cs = fscs + cylno;
793 memset(&acg, 0, sblock.fs_cgsize);
794 acg.cg_time = utime;
795 acg.cg_magic = CG_MAGIC;
796 acg.cg_cgx = cylno;
797 if (cylno == sblock.fs_ncg - 1)
798 acg.cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg;
799 else
800 acg.cg_ncyl = sblock.fs_cpg;
801 acg.cg_niblk = sblock.fs_ipg;
802 acg.cg_ndblk = dmax - cbase;
803 if (sblock.fs_contigsumsize > 0)
804 acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag;
805 acg.cg_btotoff = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield);
806 acg.cg_boff = acg.cg_btotoff + sblock.fs_cpg * sizeof(int32_t);
807 acg.cg_iusedoff = acg.cg_boff +
808 sblock.fs_cpg * sblock.fs_nrpos * sizeof(u_int16_t);
809 acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, NBBY);
810 if (sblock.fs_contigsumsize <= 0) {
811 acg.cg_nextfreeoff = acg.cg_freeoff +
812 howmany(sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY);
813 } else {
814 acg.cg_clustersumoff = acg.cg_freeoff + howmany
815 (sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY) -
816 sizeof(u_int32_t);
817 acg.cg_clustersumoff =
818 roundup(acg.cg_clustersumoff, sizeof(u_int32_t));
819 acg.cg_clusteroff = acg.cg_clustersumoff +
820 (sblock.fs_contigsumsize + 1) * sizeof(u_int32_t);
821 acg.cg_nextfreeoff = acg.cg_clusteroff + howmany
822 (sblock.fs_cpg * sblock.fs_spc / NSPB(&sblock), NBBY);
824 if (acg.cg_nextfreeoff - (long)(&acg.cg_firstfield) > sblock.fs_cgsize) {
825 printf("Panic: cylinder group too big\n");
826 exit(37);
828 acg.cg_cs.cs_nifree += sblock.fs_ipg;
829 if (cylno == 0) {
830 for (k = 0; k < ROOTINO; k++) {
831 setbit(cg_inosused(&acg), k);
832 acg.cg_cs.cs_nifree--;
835 for (i = 0; i < sblock.fs_ipg / INOPF(&sblock); i += sblock.fs_frag) {
836 #ifdef FSIRAND
837 for (j = 0;
838 j < sblock.fs_bsize / sizeof(struct ufs1_dinode);
839 j++) {
840 zino[j].di_gen = random();
842 #endif
843 wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
844 sblock.fs_bsize, (char *)zino);
846 if (cylno > 0) {
848 * In cylno 0, beginning space is reserved
849 * for boot and super blocks.
851 for (d = 0; d < dlower; d += sblock.fs_frag) {
852 blkno = d / sblock.fs_frag;
853 setblock(&sblock, cg_blksfree(&acg), blkno);
854 if (sblock.fs_contigsumsize > 0)
855 setbit(cg_clustersfree(&acg), blkno);
856 acg.cg_cs.cs_nbfree++;
857 cg_blktot(&acg)[cbtocylno(&sblock, d)]++;
858 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))
859 [cbtorpos(&sblock, d)]++;
861 sblock.fs_dsize += dlower;
863 sblock.fs_dsize += acg.cg_ndblk - dupper;
864 if ((i = dupper % sblock.fs_frag)) {
865 acg.cg_frsum[sblock.fs_frag - i]++;
866 for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
867 setbit(cg_blksfree(&acg), dupper);
868 acg.cg_cs.cs_nffree++;
871 for (d = dupper; d + sblock.fs_frag <= dmax - cbase; ) {
872 blkno = d / sblock.fs_frag;
873 setblock(&sblock, cg_blksfree(&acg), blkno);
874 if (sblock.fs_contigsumsize > 0)
875 setbit(cg_clustersfree(&acg), blkno);
876 acg.cg_cs.cs_nbfree++;
877 cg_blktot(&acg)[cbtocylno(&sblock, d)]++;
878 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))
879 [cbtorpos(&sblock, d)]++;
880 d += sblock.fs_frag;
882 if (d < dmax - cbase) {
883 acg.cg_frsum[dmax - cbase - d]++;
884 for (; d < dmax - cbase; d++) {
885 setbit(cg_blksfree(&acg), d);
886 acg.cg_cs.cs_nffree++;
889 if (sblock.fs_contigsumsize > 0) {
890 int32_t *sump = cg_clustersum(&acg);
891 u_char *mapp = cg_clustersfree(&acg);
892 int map = *mapp++;
893 int bit = 1;
894 int run = 0;
896 for (i = 0; i < acg.cg_nclusterblks; i++) {
897 if ((map & bit) != 0) {
898 run++;
899 } else if (run != 0) {
900 if (run > sblock.fs_contigsumsize)
901 run = sblock.fs_contigsumsize;
902 sump[run]++;
903 run = 0;
905 if ((i & (NBBY - 1)) != (NBBY - 1)) {
906 bit <<= 1;
907 } else {
908 map = *mapp++;
909 bit = 1;
912 if (run != 0) {
913 if (run > sblock.fs_contigsumsize)
914 run = sblock.fs_contigsumsize;
915 sump[run]++;
918 sblock.fs_cstotal.cs_ndir += acg.cg_cs.cs_ndir;
919 sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree;
920 sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree;
921 sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree;
922 *cs = acg.cg_cs;
923 wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
924 sblock.fs_bsize, (char *)&acg);
928 * initialize the file system
930 struct ufs1_dinode node;
932 #ifdef LOSTDIR
933 #define PREDEFDIR 3
934 #else
935 #define PREDEFDIR 2
936 #endif
938 struct direct root_dir[] = {
939 { ROOTINO, sizeof(struct direct), DT_DIR, 1, "." },
940 { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
941 #ifdef LOSTDIR
942 { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 10, "lost+found" },
943 #endif
945 struct odirect {
946 u_long d_ino;
947 u_short d_reclen;
948 u_short d_namlen;
949 u_char d_name[MAXNAMLEN + 1];
950 } oroot_dir[] = {
951 { ROOTINO, sizeof(struct direct), 1, "." },
952 { ROOTINO, sizeof(struct direct), 2, ".." },
953 #ifdef LOSTDIR
954 { LOSTFOUNDINO, sizeof(struct direct), 10, "lost+found" },
955 #endif
957 #ifdef LOSTDIR
958 struct direct lost_found_dir[] = {
959 { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 1, "." },
960 { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
961 { 0, DIRBLKSIZ, 0, 0, 0 },
963 struct odirect olost_found_dir[] = {
964 { LOSTFOUNDINO, sizeof(struct direct), 1, "." },
965 { ROOTINO, sizeof(struct direct), 2, ".." },
966 { 0, DIRBLKSIZ, 0, 0 },
968 #endif
969 char buf[MAXBSIZE];
971 void
972 fsinit(time_t utime)
974 #ifdef LOSTDIR
975 int i;
976 #endif
979 * initialize the node
981 node.di_atime = utime;
982 node.di_mtime = utime;
983 node.di_ctime = utime;
984 #ifdef LOSTDIR
986 * create the lost+found directory
988 if (Oflag) {
989 makedir((struct direct *)olost_found_dir, 2);
990 for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ)
991 memmove(&buf[i], &olost_found_dir[2],
992 DIRSIZ(0, &olost_found_dir[2]));
993 } else {
994 makedir(lost_found_dir, 2);
995 for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ)
996 memmove(&buf[i], &lost_found_dir[2],
997 DIRSIZ(0, &lost_found_dir[2]));
999 node.di_mode = IFDIR | UMASK;
1000 node.di_nlink = 2;
1001 node.di_size = sblock.fs_bsize;
1002 node.di_db[0] = alloc(node.di_size, node.di_mode);
1003 node.di_blocks = btodb(fragroundup(&sblock, node.di_size));
1004 wtfs(fsbtodb(&sblock, node.di_db[0]), node.di_size, buf);
1005 iput(&node, LOSTFOUNDINO);
1006 #endif
1008 * create the root directory
1010 if (mfs)
1011 node.di_mode = IFDIR | 01777;
1012 else
1013 node.di_mode = IFDIR | UMASK;
1014 node.di_nlink = PREDEFDIR;
1015 if (Oflag)
1016 node.di_size = makedir((struct direct *)oroot_dir, PREDEFDIR);
1017 else
1018 node.di_size = makedir(root_dir, PREDEFDIR);
1019 node.di_db[0] = alloc(sblock.fs_fsize, node.di_mode);
1020 node.di_blocks = btodb(fragroundup(&sblock, node.di_size));
1021 wtfs(fsbtodb(&sblock, node.di_db[0]), sblock.fs_fsize, buf);
1022 iput(&node, ROOTINO);
1026 * construct a set of directory entries in "buf".
1027 * return size of directory.
1030 makedir(struct direct *protodir, int entries)
1032 char *cp;
1033 int i, spcleft;
1035 spcleft = DIRBLKSIZ;
1036 for (cp = buf, i = 0; i < entries - 1; i++) {
1037 protodir[i].d_reclen = DIRSIZ(0, &protodir[i]);
1038 memmove(cp, &protodir[i], protodir[i].d_reclen);
1039 cp += protodir[i].d_reclen;
1040 spcleft -= protodir[i].d_reclen;
1042 protodir[i].d_reclen = spcleft;
1043 memmove(cp, &protodir[i], DIRSIZ(0, &protodir[i]));
1044 return (DIRBLKSIZ);
1048 * allocate a block or frag
1050 daddr_t
1051 alloc(int size, int mode)
1053 int i, frag;
1054 daddr_t d, blkno;
1056 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
1057 (char *)&acg);
1058 if (acg.cg_magic != CG_MAGIC) {
1059 printf("cg 0: bad magic number\n");
1060 return (0);
1062 if (acg.cg_cs.cs_nbfree == 0) {
1063 printf("first cylinder group ran out of space\n");
1064 return (0);
1066 for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag)
1067 if (isblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag))
1068 goto goth;
1069 printf("internal error: can't find block in cyl 0\n");
1070 return (0);
1071 goth:
1072 blkno = fragstoblks(&sblock, d);
1073 clrblock(&sblock, cg_blksfree(&acg), blkno);
1074 if (sblock.fs_contigsumsize > 0)
1075 clrbit(cg_clustersfree(&acg), blkno);
1076 acg.cg_cs.cs_nbfree--;
1077 sblock.fs_cstotal.cs_nbfree--;
1078 fscs[0].cs_nbfree--;
1079 if (mode & IFDIR) {
1080 acg.cg_cs.cs_ndir++;
1081 sblock.fs_cstotal.cs_ndir++;
1082 fscs[0].cs_ndir++;
1084 cg_blktot(&acg)[cbtocylno(&sblock, d)]--;
1085 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))[cbtorpos(&sblock, d)]--;
1086 if (size != sblock.fs_bsize) {
1087 frag = howmany(size, sblock.fs_fsize);
1088 fscs[0].cs_nffree += sblock.fs_frag - frag;
1089 sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag;
1090 acg.cg_cs.cs_nffree += sblock.fs_frag - frag;
1091 acg.cg_frsum[sblock.fs_frag - frag]++;
1092 for (i = frag; i < sblock.fs_frag; i++)
1093 setbit(cg_blksfree(&acg), d + i);
1095 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
1096 (char *)&acg);
1097 return (d);
1101 * Calculate number of inodes per group.
1103 long
1104 calcipg(long cylspg, long bpcg, off_t *usedbp)
1106 int i;
1107 long ipg, new_ipg, ncg, ncyl;
1108 off_t usedb;
1111 * Prepare to scale by fssize / (number of sectors in cylinder groups).
1112 * Note that fssize is still in sectors, not filesystem blocks.
1114 ncyl = howmany(fssize, (u_int)secpercyl);
1115 ncg = howmany(ncyl, cylspg);
1117 * Iterate a few times to allow for ipg depending on itself.
1119 ipg = 0;
1120 for (i = 0; i < 10; i++) {
1121 usedb = (sblock.fs_iblkno + ipg / INOPF(&sblock))
1122 * NSPF(&sblock) * (off_t)sectorsize;
1123 new_ipg = (cylspg * (quad_t)bpcg - usedb) / density * fssize
1124 / ncg / secpercyl / cylspg;
1125 new_ipg = roundup(new_ipg, INOPB(&sblock));
1126 if (new_ipg == ipg)
1127 break;
1128 ipg = new_ipg;
1130 *usedbp = usedb;
1131 return (ipg);
1135 * Allocate an inode on the disk
1137 void
1138 iput(struct ufs1_dinode *ip, ino_t ino)
1140 struct ufs1_dinode inobuf[MAXINOPB];
1141 daddr_t d;
1143 #ifdef FSIRAND
1144 ip->di_gen = random();
1145 #endif
1146 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
1147 (char *)&acg);
1148 if (acg.cg_magic != CG_MAGIC) {
1149 printf("cg 0: bad magic number\n");
1150 exit(31);
1152 acg.cg_cs.cs_nifree--;
1153 setbit(cg_inosused(&acg), ino);
1154 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
1155 (char *)&acg);
1156 sblock.fs_cstotal.cs_nifree--;
1157 fscs[0].cs_nifree--;
1158 if (ino >= (uint32_t)sblock.fs_ipg * (uint32_t)sblock.fs_ncg) {
1159 printf("fsinit: inode value out of range (%ju).\n",
1160 (uintmax_t)ino);
1161 exit(32);
1163 d = fsbtodb(&sblock, ino_to_fsba(&sblock, ino));
1164 rdfs(d, sblock.fs_bsize, (char *)inobuf);
1165 inobuf[ino_to_fsbo(&sblock, ino)] = *ip;
1166 wtfs(d, sblock.fs_bsize, (char *)inobuf);
1170 * Parent notifies child that it can proceed with the newfs and mount
1171 * operation (occurs after parent has copied the underlying filesystem
1172 * if the -C option was specified (for MFS), or immediately after the
1173 * parent forked the child otherwise).
1175 void
1176 parentready(__unused int signo)
1178 parentready_signalled = 1;
1182 * Notify parent process that the filesystem has created itself successfully.
1184 * We have to wait until the mount has actually completed!
1186 void
1187 started(__unused int signo)
1189 int retry = 100; /* 10 seconds, 100ms */
1191 while (mfs_ppid && retry) {
1192 struct stat st;
1194 if (
1195 stat(mfs_mtpt, &st) < 0 ||
1196 st.st_dev != mfs_mtstat.st_dev
1198 break;
1200 usleep(100*1000);
1201 --retry;
1203 if (retry == 0) {
1204 fatal("mfs mount failed waiting for mount to go active");
1205 } else if (copyroot) {
1206 FSPaste(mfs_mtpt, copyroot, copyhlinks);
1208 exit(0);
1212 * read a block from the file system
1214 void
1215 rdfs(daddr_t bno, int size, char *bf)
1217 int n;
1219 wtfsflush();
1220 if (mfs) {
1221 memmove(bf, membase + bno * sectorsize, size);
1222 return;
1224 if (lseek(fsi, (off_t)bno * sectorsize, 0) < 0) {
1225 printf("seek error: %ld\n", (long)bno);
1226 err(33, "rdfs");
1228 n = read(fsi, bf, size);
1229 if (n != size) {
1230 printf("read error: %ld\n", (long)bno);
1231 err(34, "rdfs");
1235 #define WCSIZE (128 * 1024)
1236 daddr_t wc_sect; /* units of sectorsize */
1237 int wc_end; /* bytes */
1238 static char wc[WCSIZE]; /* bytes */
1241 * Flush dirty write behind buffer.
1243 void
1244 wtfsflush(void)
1246 int n;
1247 if (wc_end) {
1248 if (lseek(fso, (off_t)wc_sect * sectorsize, SEEK_SET) < 0) {
1249 printf("seek error: %ld\n", (long)wc_sect);
1250 err(35, "wtfs - writecombine");
1252 n = write(fso, wc, wc_end);
1253 if (n != wc_end) {
1254 printf("write error: %ld\n", (long)wc_sect);
1255 err(36, "wtfs - writecombine");
1257 wc_end = 0;
1262 * Issue ioctl to erase range of sectors using TRIM
1264 void
1265 erfs(off_t byte_start, off_t size)
1267 off_t ioarg[2];
1268 ioarg[0] = byte_start;
1269 ioarg[1] = size;
1270 if (ioctl(fsi, IOCTLTRIM, ioarg) < 0) {
1271 err(37, "Device trim failed\n");
1276 * write a block to the file system
1278 void
1279 wtfs(daddr_t bno, int size, char *bf)
1281 int n;
1282 int done;
1284 if (mfs) {
1285 memmove(membase + bno * sectorsize, bf, size);
1286 return;
1288 if (Nflag)
1289 return;
1290 done = 0;
1291 if (wc_end == 0 && size <= WCSIZE) {
1292 wc_sect = bno;
1293 bcopy(bf, wc, size);
1294 wc_end = size;
1295 if (wc_end < WCSIZE)
1296 return;
1297 done = 1;
1299 if ((off_t)wc_sect * sectorsize + wc_end == (off_t)bno * sectorsize &&
1300 wc_end + size <= WCSIZE) {
1301 bcopy(bf, wc + wc_end, size);
1302 wc_end += size;
1303 if (wc_end < WCSIZE)
1304 return;
1305 done = 1;
1307 wtfsflush();
1308 if (done)
1309 return;
1310 if (lseek(fso, (off_t)bno * sectorsize, SEEK_SET) < 0) {
1311 printf("seek error: %ld\n", (long)bno);
1312 err(35, "wtfs");
1314 n = write(fso, bf, size);
1315 if (n != size) {
1316 printf("write error: fso %d blk %ld %d/%d\n",
1317 fso, (long)bno, n, size);
1318 err(36, "wtfs");
1323 * check if a block is available
1326 isblock(struct fs *fs, unsigned char *cp, int h)
1328 unsigned char mask;
1330 switch (fs->fs_frag) {
1331 case 8:
1332 return (cp[h] == 0xff);
1333 case 4:
1334 mask = 0x0f << ((h & 0x1) << 2);
1335 return ((cp[h >> 1] & mask) == mask);
1336 case 2:
1337 mask = 0x03 << ((h & 0x3) << 1);
1338 return ((cp[h >> 2] & mask) == mask);
1339 case 1:
1340 mask = 0x01 << (h & 0x7);
1341 return ((cp[h >> 3] & mask) == mask);
1342 default:
1343 fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag);
1344 return (0);
1349 * take a block out of the map
1351 void
1352 clrblock(struct fs *fs, unsigned char *cp, int h)
1354 switch ((fs)->fs_frag) {
1355 case 8:
1356 cp[h] = 0;
1357 return;
1358 case 4:
1359 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
1360 return;
1361 case 2:
1362 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
1363 return;
1364 case 1:
1365 cp[h >> 3] &= ~(0x01 << (h & 0x7));
1366 return;
1367 default:
1368 fprintf(stderr, "clrblock bad fs_frag %d\n", fs->fs_frag);
1369 return;
1374 * put a block into the map
1376 void
1377 setblock(struct fs *fs, unsigned char *cp, int h)
1379 switch (fs->fs_frag) {
1380 case 8:
1381 cp[h] = 0xff;
1382 return;
1383 case 4:
1384 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
1385 return;
1386 case 2:
1387 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
1388 return;
1389 case 1:
1390 cp[h >> 3] |= (0x01 << (h & 0x7));
1391 return;
1392 default:
1393 fprintf(stderr, "setblock bad fs_frag %d\n", fs->fs_frag);
1394 return;
1399 * Determine the number of characters in a
1400 * single line.
1403 static int
1404 charsperline(void)
1406 int columns;
1407 char *cp;
1408 struct winsize ws;
1410 columns = 0;
1411 if (ioctl(0, TIOCGWINSZ, &ws) != -1)
1412 columns = ws.ws_col;
1413 if (columns == 0 && (cp = getenv("COLUMNS")))
1414 columns = atoi(cp);
1415 if (columns == 0)
1416 columns = 80; /* last resort */
1417 return columns;