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