Remove some __STDC__ checks.
[dragonfly.git] / sbin / newfs / newfs.c
1 /*
2  * Copyright (c) 1983, 1989, 1993, 1994
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.
32  *
33  * @(#) Copyright (c) 1983, 1989, 1993, 1994 The Regents of the University of California.  All rights reserved.
34  * @(#)newfs.c  8.13 (Berkeley) 5/1/95
35  * $FreeBSD: src/sbin/newfs/newfs.c,v 1.30.2.9 2003/05/13 12:03:55 joerg Exp $
36  */
37
38 /*
39  * newfs: friendly front end to mkfs
40  */
41 #include <sys/param.h>
42 #include <sys/stat.h>
43 #include <sys/diskslice.h>
44 #include <sys/file.h>
45 #include <sys/mount.h>
46 #include <sys/sysctl.h>
47
48 #include <vfs/ufs/dir.h>
49 #include <vfs/ufs/dinode.h>
50 #include <vfs/ufs/fs.h>
51 #include <vfs/ufs/ufsmount.h>
52
53 #include <ctype.h>
54 #include <err.h>
55 #include <errno.h>
56 #include <inttypes.h>
57 #include <paths.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <syslog.h>
62 #include <unistd.h>
63 #include <disktab.h>
64
65 #ifdef MFS
66 #include <sys/types.h>
67 #include <sys/mman.h>
68 #endif
69
70 #include <stdarg.h>
71
72 #include "mntopts.h"
73 #include "defs.h"
74
75 struct mntopt mopts[] = {
76         MOPT_STDOPTS,
77         MOPT_ASYNC,
78         MOPT_NULL,
79 };
80
81 void    fatal(const char *fmt, ...) __printflike(1, 2);
82
83 #define COMPAT                  /* allow non-labeled disks */
84
85 /*
86  * The following two constants set the default block and fragment sizes.
87  * Both constants must be a power of 2 and meet the following constraints:
88  *      MINBSIZE <= DESBLKSIZE <= MAXBSIZE
89  *      sectorsize <= DESFRAGSIZE <= DESBLKSIZE
90  *      DESBLKSIZE / DESFRAGSIZE <= 8
91  */
92 #define DFL_FRAGSIZE    2048
93 #define DFL_BLKSIZE     16384
94
95 /*
96  * Cylinder groups may have up to many cylinders. The actual
97  * number used depends upon how much information can be stored
98  * on a single cylinder. The default is to use as many as possible
99  * cylinders per group.
100  */
101 #define DESCPG          65536   /* desired fs_cpg ("infinity") */
102
103 /*
104  * Once upon a time...
105  *    ROTDELAY gives the minimum number of milliseconds to initiate
106  *    another disk transfer on the same cylinder. It is used in
107  *    determining the rotationally optimal layout for disk blocks
108  *    within a file; the default of fs_rotdelay is 4ms.
109  *
110  * ...but now we make this 0 to disable the rotdelay delay because
111  * modern drives with read/write-behind achieve higher performance
112  * without the delay.
113  */
114 #define ROTDELAY        0
115
116 /*
117  * MAXBLKPG determines the maximum number of data blocks which are
118  * placed in a single cylinder group. The default is one indirect
119  * block worth of data blocks.
120  */
121 #define MAXBLKPG(bsize) ((bsize) / sizeof(daddr_t))
122
123 /*
124  * Each file system has a number of inodes statically allocated.
125  * We allocate one inode slot per NFPI fragments, expecting this
126  * to be far more than we will ever need.
127  */
128 #define NFPI            4
129
130 /*
131  * Once upon a time...
132  *    For each cylinder we keep track of the availability of blocks at different
133  *    rotational positions, so that we can lay out the data to be picked
134  *    up with minimum rotational latency.  NRPOS is the default number of
135  *    rotational positions that we distinguish.  With NRPOS of 8 the resolution
136  *    of our summary information is 2ms for a typical 3600 rpm drive.
137  *
138  * ...but now we make this 1 (which essentially disables the rotational
139  * position table because modern drives with read-ahead and write-behind do
140  * better without the rotational position table.
141  */
142 #define NRPOS           1       /* number distinct rotational positions */
143
144 /*
145  * About the same time as the above, we knew what went where on the disks.
146  * no longer so, so kill the code which finds the different platters too...
147  * We do this by saying one head, with a lot of sectors on it.
148  * The number of sectors are used to determine the size of a cyl-group.
149  * Kirk suggested one or two meg per "cylinder" so we say two.
150  */
151 #define NTRACKS         1       /* number of heads */
152 #define NSECTORS        4096    /* number of sectors */
153
154 int     mfs;                    /* run as the memory based filesystem */
155 char    *mfs_mtpt;              /* mount point for mfs          */
156 struct stat mfs_mtstat;         /* stat prior to mount          */
157 int     Lflag;                  /* add a volume label */
158 int     Nflag;                  /* run without writing file system */
159 int     Oflag;                  /* format as an 4.3BSD file system */
160 int     Cflag;                  /* copy underlying filesystem (mfs only) */
161 int     Uflag;                  /* enable soft updates for file system */
162 int     Eflag;                  /* erase contents using TRIM */
163 uint64_t slice_offset;          /* Pysical device slice offset */
164 u_long  fssize;                 /* file system size */
165 int     ntracks = NTRACKS;      /* # tracks/cylinder */
166 int     nsectors = NSECTORS;    /* # sectors/track */
167 int     nphyssectors;           /* # sectors/track including spares */
168 int     secpercyl;              /* sectors per cylinder */
169 int     trackspares = -1;       /* spare sectors per track */
170 int     cylspares = -1;         /* spare sectors per cylinder */
171 int     sectorsize;             /* bytes/sector */
172 int     realsectorsize;         /* bytes/sector in hardware */
173 int     rpm;                    /* revolutions/minute of drive */
174 int     interleave;             /* hardware sector interleave */
175 int     trackskew = -1;         /* sector 0 skew, per track */
176 int     headswitch;             /* head switch time, usec */
177 int     trackseek;              /* track-to-track seek, usec */
178 int     fsize = 0;              /* fragment size */
179 int     bsize = 0;              /* block size */
180 int     cpg = DESCPG;           /* cylinders/cylinder group */
181 int     cpgflg;                 /* cylinders/cylinder group flag was given */
182 int     minfree = MINFREE;      /* free space threshold */
183 int     opt = DEFAULTOPT;       /* optimization preference (space or time) */
184 int     density;                /* number of bytes per inode */
185 int     maxcontig = 0;          /* max contiguous blocks to allocate */
186 int     rotdelay = ROTDELAY;    /* rotational delay between blocks */
187 int     maxbpg;                 /* maximum blocks per file in a cyl group */
188 int     nrpos = NRPOS;          /* # of distinguished rotational positions */
189 int     avgfilesize = AVFILESIZ;/* expected average file size */
190 int     avgfilesperdir = AFPDIR;/* expected number of files per directory */
191 int     bbsize = BBSIZE;        /* boot block size */
192 int     sbsize = SBSIZE;        /* superblock size */
193 int     mntflags = MNT_ASYNC;   /* flags to be passed to mount */
194 int     t_or_u_flag = 0;        /* user has specified -t or -u */
195 caddr_t membase;                /* start address of memory based filesystem */
196 char    *filename;
197 u_char  *volumelabel = NULL;    /* volume label for filesystem */
198 #ifdef COMPAT
199 char    *disktype;
200 int     unlabeled;
201 #endif
202
203 char    mfsdevname[256];
204 char    *progname;
205
206 static void usage(void);
207 static void mfsintr(int signo);
208
209 int
210 main(int argc, char **argv)
211 {
212         int ch, i;
213         struct disktab geom;            /* disk geometry data */
214         struct stat st;
215         struct statfs *mp;
216         int fsi = -1, fso = -1, len, n, vflag;
217         char *s1, *s2, *special;
218         const char *opstring;
219 #ifdef MFS
220         struct vfsconf vfc;
221         int error;
222 #endif
223
224         bzero(&geom, sizeof(geom));
225         vflag = 0;
226         if ((progname = strrchr(*argv, '/')))
227                 ++progname;
228         else
229                 progname = *argv;
230
231         if (strstr(progname, "mfs")) {
232                 mfs = 1;
233                 Nflag++;
234         }
235
236         opstring = mfs ?
237             "L:NCF:T:Ua:b:c:d:e:f:g:h:i:m:o:s:v" :
238             "L:NEOS:T:Ua:b:c:d:e:f:g:h:i:k:l:m:n:o:p:r:s:t:u:vx:";
239         while ((ch = getopt(argc, argv, opstring)) != -1) {
240                 switch (ch) {
241                 case 'E':
242                         Eflag = 1;
243                         break;
244                 case 'L':
245                         volumelabel = optarg;
246                         i = -1;
247                         while (isalnum(volumelabel[++i]))
248                                 ;
249                         if (volumelabel[i] != '\0')
250                                 errx(1, "bad volume label. Valid characters are alphanumerics.");
251                         if (strlen(volumelabel) >= MAXVOLLEN)
252                                 errx(1, "bad volume label. Length is longer than %d.",
253                                     MAXVOLLEN);
254                         Lflag = 1;
255                         break;
256                 case 'N':
257                         Nflag = 1;
258                         break;
259                 case 'O':
260                         Oflag = 1;
261                         break;
262                 case 'C':
263                         Cflag = 1;      /* MFS only */
264                         break;
265                 case 'S':
266                         if ((sectorsize = atoi(optarg)) <= 0)
267                                 fatal("%s: bad sector size", optarg);
268                         break;
269 #ifdef COMPAT
270                 case 'T':
271                         disktype = optarg;
272                         break;
273 #endif
274                 case 'F':
275                         filename = optarg;
276                         break;
277                 case 'U':
278                         Uflag = 1;
279                         break;
280                 case 'a':
281                         if ((maxcontig = atoi(optarg)) <= 0)
282                                 fatal("%s: bad maximum contiguous blocks",
283                                     optarg);
284                         break;
285                 case 'b':
286                         if ((bsize = atoi(optarg)) < MINBSIZE)
287                                 fatal("%s: bad block size", optarg);
288                         break;
289                 case 'c':
290                         if ((cpg = atoi(optarg)) <= 0)
291                                 fatal("%s: bad cylinders/group", optarg);
292                         cpgflg++;
293                         break;
294                 case 'd':
295                         if ((rotdelay = atoi(optarg)) < 0)
296                                 fatal("%s: bad rotational delay", optarg);
297                         break;
298                 case 'e':
299                         if ((maxbpg = atoi(optarg)) <= 0)
300                 fatal("%s: bad blocks per file in a cylinder group",
301                                     optarg);
302                         break;
303                 case 'f':
304                         if ((fsize = atoi(optarg)) <= 0)
305                                 fatal("%s: bad fragment size", optarg);
306                         break;
307                 case 'g':
308                         if ((avgfilesize = atoi(optarg)) <= 0)
309                                 fatal("%s: bad average file size", optarg);
310                         break;
311                 case 'h':
312                         if ((avgfilesperdir = atoi(optarg)) <= 0)
313                                 fatal("%s: bad average files per dir", optarg);
314                         break;
315                 case 'i':
316                         if ((density = atoi(optarg)) <= 0)
317                                 fatal("%s: bad bytes per inode", optarg);
318                         break;
319                 case 'k':
320                         if ((trackskew = atoi(optarg)) < 0)
321                                 fatal("%s: bad track skew", optarg);
322                         break;
323                 case 'l':
324                         if ((interleave = atoi(optarg)) <= 0)
325                                 fatal("%s: bad interleave", optarg);
326                         break;
327                 case 'm':
328                         if ((minfree = atoi(optarg)) < 0 || minfree > 99)
329                                 fatal("%s: bad free space %%", optarg);
330                         break;
331                 case 'n':
332                         if ((nrpos = atoi(optarg)) < 0)
333                                 fatal("%s: bad rotational layout count",
334                                     optarg);
335                         if (nrpos == 0)
336                                 nrpos = 1;
337                         break;
338                 case 'o':
339                         if (mfs)
340                                 getmntopts(optarg, mopts, &mntflags, 0);
341                         else {
342                                 if (strcmp(optarg, "space") == 0)
343                                         opt = FS_OPTSPACE;
344                                 else if (strcmp(optarg, "time") == 0)
345                                         opt = FS_OPTTIME;
346                                 else
347         fatal("%s: unknown optimization preference: use `space' or `time'", optarg);
348                         }
349                         break;
350                 case 'p':
351                         if ((trackspares = atoi(optarg)) < 0)
352                                 fatal("%s: bad spare sectors per track",
353                                     optarg);
354                         break;
355                 case 'r':
356                         if ((rpm = atoi(optarg)) <= 0)
357                                 fatal("%s: bad revolutions/minute", optarg);
358                         break;
359                 case 's':
360                         /*
361                          * Unsigned long but limit to long.  On 32 bit a
362                          * tad under 2G, on 64 bit the upper bound is more
363                          * swap space then operand size.
364                          *
365                          * NOTE: fssize is converted from 512 byte sectors
366                          * to filesystem block-sized sectors by mkfs XXX.
367                          */
368                         fssize = strtoul(optarg, NULL, 10);
369                         if (fssize == 0 || fssize > LONG_MAX)
370                                 fatal("%s: bad file system size", optarg);
371                         break;
372                 case 't':
373                         t_or_u_flag++;
374                         if ((ntracks = atoi(optarg)) < 0)
375                                 fatal("%s: bad total tracks", optarg);
376                         break;
377                 case 'u':
378                         t_or_u_flag++;
379                         if ((nsectors = atoi(optarg)) < 0)
380                                 fatal("%s: bad sectors/track", optarg);
381                         break;
382                 case 'v':
383                         vflag = 1;
384                         break;
385                 case 'x':
386                         if ((cylspares = atoi(optarg)) < 0)
387                                 fatal("%s: bad spare sectors per cylinder",
388                                     optarg);
389                         break;
390                 case '?':
391                 default:
392                         usage();
393                 }
394         }
395         argc -= optind;
396         argv += optind;
397
398         if (argc != 2 && (mfs || argc != 1))
399                 usage();
400
401         special = argv[0];
402         /* Copy the NetBSD way of faking up a disk label */
403         if (mfs && !strcmp(special, "swap")) {
404                 /* 
405                  * it's an MFS, mounted on "swap."  fake up a label.
406                  * XXX XXX XXX
407                  */
408                 fso = -1;       /* XXX; normally done below. */
409
410                 geom.d_media_blksize = 512;
411                 geom.d_nheads = 16;
412                 geom.d_secpertrack = 64;
413                 /* geom.d_ncylinders not used */
414                 geom.d_secpercyl = 1024;
415                 geom.d_media_blocks = 16384;
416                 geom.d_rpm = 3600;
417                 geom.d_interleave = 1;
418
419                 goto havelabel;
420         }
421
422         /*
423          * If we can't stat the device and the path is relative, try
424          * prepending /dev.
425          */
426         if (stat(special, &st) < 0 && special[0] && special[0] != '/')
427                 asprintf(&special, "/dev/%s", special);
428
429         if (Eflag) {
430                 char sysctl_name[64];
431                 int trim_enabled = 0;
432                 size_t olen = sizeof(trim_enabled);
433                 char *dev_name = strdup(special);
434
435                 dev_name = strtok(dev_name + strlen("/dev/da"),"s");
436                 sprintf(sysctl_name, "kern.cam.da.%s.trim_enabled",
437                     dev_name);
438
439                 sysctlbyname(sysctl_name, &trim_enabled, &olen, NULL, 0);
440
441                 if(errno == ENOENT) {
442                         printf("Device:%s does not support the TRIM command\n",
443                             special);
444                         usage();
445                 }
446                 if(!trim_enabled) {
447                         printf("Erase device option selected, but sysctl (%s) "
448                             "is not enabled\n",sysctl_name);
449                         usage();
450                           
451                 }
452         }
453         if (Nflag) {
454                 fso = -1;
455         } else {
456                 fso = open(special, O_WRONLY);
457                 if (fso < 0)
458                         fatal("%s: %s", special, strerror(errno));
459
460                 /* Bail if target special is mounted */
461                 n = getmntinfo(&mp, MNT_NOWAIT);
462                 if (n == 0)
463                         fatal("%s: getmntinfo: %s", special, strerror(errno));
464
465                 len = sizeof(_PATH_DEV) - 1;
466                 s1 = special;
467                 if (strncmp(_PATH_DEV, s1, len) == 0)
468                         s1 += len;
469
470                 while (--n >= 0) {
471                         s2 = mp->f_mntfromname;
472                         if (strncmp(_PATH_DEV, s2, len) == 0) {
473                                 s2 += len - 1;
474                                 *s2 = 'r';
475                         }
476                         if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
477                                 fatal("%s is mounted on %s",
478                                     special, mp->f_mntonname);
479                         ++mp;
480                 }
481         }
482         if (mfs && disktype != NULL) {
483                 struct disktab *dt;
484
485                 if ((dt = getdisktabbyname(disktype)) == NULL)
486                         fatal("%s: unknown disk type", disktype);
487                 geom = *dt;
488         } else {
489                 struct partinfo pinfo;
490
491                 if (special[0] == 0)
492                         fatal("null special file name");
493                 fsi = open(special, O_RDONLY);
494                 if (fsi < 0)
495                         fatal("%s: %s", special, strerror(errno));
496                 if (fstat(fsi, &st) < 0)
497                         fatal("%s: %s", special, strerror(errno));
498                 if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs && !vflag)
499                         printf("%s: %s: not a character-special device\n",
500                             progname, special);
501 #ifdef COMPAT
502                 if (!mfs && disktype == NULL)
503                         disktype = argv[1];
504 #endif
505                 if (ioctl(fsi, DIOCGPART, &pinfo) < 0) {
506                         if (!vflag) {
507                                 fatal("%s: unable to retrieve geometry "
508                                       "information", argv[0]);
509                         }
510                         /*
511                          * fake up geometry data
512                          */
513                         geom.d_media_blksize = 512;
514                         geom.d_nheads = 16;
515                         geom.d_secpertrack = 64;
516                         geom.d_secpercyl = 1024;
517                         geom.d_media_blocks = st.st_size / 
518                                               geom.d_media_blksize;
519                         geom.d_media_size = geom.d_media_blocks *
520                                             geom.d_media_blksize;
521                         /* geom.d_ncylinders not used */
522                 } else {
523                         /*
524                          * extract geometry from pinfo
525                          */
526                         geom.d_media_blksize = pinfo.media_blksize;
527                         geom.d_nheads = pinfo.d_nheads;
528                         geom.d_secpertrack = pinfo.d_secpertrack;
529                         geom.d_secpercyl = pinfo.d_secpercyl;
530                         /* geom.d_ncylinders not used */
531                         geom.d_media_blocks = pinfo.media_blocks;
532                         geom.d_media_size = pinfo.media_size;
533                         slice_offset = pinfo.media_offset;
534                 }
535                 if (geom.d_media_blocks == 0 || geom.d_media_size == 0) {
536                         fatal("%s: is unavailable", argv[0]);
537                 }
538                 printf("%s: media size %6.2fMB\n",
539                         argv[0], geom.d_media_size / 1024.0 / 1024.0);
540                 if (geom.d_media_size / 512 >= 0x80000000ULL)
541                         fatal("%s: media size is too large for newfs to handle",
542                               argv[0]);
543         }
544 havelabel:
545         if (fssize == 0)
546                 fssize = geom.d_media_blocks;
547         if ((u_long)fssize > geom.d_media_blocks && !mfs) {
548                fatal("%s: maximum file system size is %" PRIu64 " blocks",
549                      argv[0], geom.d_media_blocks);
550         }
551         if (rpm == 0) {
552                 rpm = geom.d_rpm;
553                 if (rpm <= 0)
554                         rpm = 3600;
555         }
556         if (ntracks == 0) {
557                 ntracks = geom.d_nheads;
558                 if (ntracks <= 0)
559                         fatal("%s: no default #tracks", argv[0]);
560         }
561         if (nsectors == 0) {
562                 nsectors = geom.d_secpertrack;
563                 if (nsectors <= 0)
564                         fatal("%s: no default #sectors/track", argv[0]);
565         }
566         if (sectorsize == 0) {
567                 sectorsize = geom.d_media_blksize;
568                 if (sectorsize <= 0)
569                         fatal("%s: no default sector size", argv[0]);
570         }
571         if (trackskew == -1) {
572                 trackskew = geom.d_trackskew;
573                 if (trackskew < 0)
574                         trackskew = 0;
575         }
576         if (interleave == 0) {
577                 interleave = geom.d_interleave;
578                 if (interleave <= 0)
579                         interleave = 1;
580         }
581         if (fsize == 0)
582                 fsize = MAX(DFL_FRAGSIZE, geom.d_media_blksize);
583         if (bsize == 0)
584                 bsize = MIN(DFL_BLKSIZE, 8 * fsize);
585         /*
586          * Maxcontig sets the default for the maximum number of blocks
587          * that may be allocated sequentially. With filesystem clustering
588          * it is possible to allocate contiguous blocks up to the maximum
589          * transfer size permitted by the controller or buffering.
590          */
591         if (maxcontig == 0)
592                 maxcontig = MAX(1, MAXPHYS / bsize - 1);
593         if (density == 0)
594                 density = NFPI * fsize;
595         if (minfree < MINFREE && opt != FS_OPTSPACE) {
596                 fprintf(stderr, "Warning: changing optimization to space ");
597                 fprintf(stderr, "because minfree is less than %d%%\n", MINFREE);
598                 opt = FS_OPTSPACE;
599         }
600         if (trackspares == -1)
601                 trackspares = 0;
602         nphyssectors = nsectors + trackspares;
603         if (cylspares == -1)
604                 cylspares = 0;
605         secpercyl = nsectors * ntracks - cylspares;
606         /*
607          * Only complain if -t or -u have been specified; the default
608          * case (4096 sectors per cylinder) is intended to disagree
609          * with the disklabel.
610          */
611         if (t_or_u_flag && (uint32_t)secpercyl != geom.d_secpercyl)
612                 fprintf(stderr, "%s (%d) %s (%u)\n",
613                         "Warning: calculated sectors per cylinder", secpercyl,
614                         "disagrees with disk label", geom.d_secpercyl);
615         if (maxbpg == 0)
616                 maxbpg = MAXBLKPG(bsize);
617         headswitch = geom.d_headswitch;
618         trackseek = geom.d_trkseek;
619         realsectorsize = sectorsize;
620         if (sectorsize != DEV_BSIZE) {          /* XXX */
621                 int secperblk = sectorsize / DEV_BSIZE;
622
623                 sectorsize = DEV_BSIZE;
624                 nsectors *= secperblk;
625                 nphyssectors *= secperblk;
626                 secpercyl *= secperblk;
627                 fssize *= secperblk;
628         }
629         if (mfs) {
630                 mfs_mtpt = argv[1];
631                 if (
632                     stat(mfs_mtpt, &mfs_mtstat) < 0 ||
633                     !S_ISDIR(mfs_mtstat.st_mode)
634                 ) {
635                         fatal("mount point not dir: %s", mfs_mtpt);
636                 }
637         }
638         mkfs(special, fsi, fso, (Cflag && mfs) ? argv[1] : NULL);
639
640         /*
641          * NOTE: Newfs no longer accesses or attempts to update the
642          * filesystem disklabel.
643          *
644          * NOTE: fssize is converted from 512 byte sectors
645          * to filesystem block-sized sectors by mkfs XXX.
646          */
647         if (!Nflag)
648                 close(fso);
649         close(fsi);
650 #ifdef MFS
651         if (mfs) {
652                 struct mfs_args args;
653
654                 bzero(&args, sizeof(args));
655
656                 snprintf(mfsdevname, sizeof(mfsdevname), "/dev/mfs%d",
657                         getpid());
658                 args.fspec = mfsdevname;
659                 args.export.ex_root = -2;
660                 if (mntflags & MNT_RDONLY)
661                         args.export.ex_flags = MNT_EXRDONLY;
662                 else
663                         args.export.ex_flags = 0;
664                 args.base = membase;
665                 args.size = fssize * fsize;
666
667                 error = getvfsbyname("mfs", &vfc);
668                 if (error && vfsisloadable("mfs")) {
669                         if (vfsload("mfs"))
670                                 fatal("vfsload(mfs)");
671                         endvfsent();    /* flush cache */
672                         error = getvfsbyname("mfs", &vfc);
673                 }
674                 if (error)
675                         fatal("mfs filesystem not available");
676
677 #if 0
678                 int udev;
679                 udev = (253 << 8) | (getpid() & 255) | 
680                         ((getpid() & ~0xFF) << 8);
681                 if (mknod(mfsdevname, S_IFCHR | 0700, udev) < 0)
682                         printf("Warning: unable to create %s\n", mfsdevname);
683 #endif
684                 signal(SIGINT, mfsintr);
685                 if (mount(vfc.vfc_name, argv[1], mntflags, &args) < 0)
686                         fatal("%s: %s", argv[1], strerror(errno));
687                 signal(SIGINT, SIG_DFL);
688                 mfsintr(SIGINT);
689         }
690 #endif
691         exit(0);
692 }
693
694 #ifdef MFS
695
696 static void
697 mfsintr(__unused int signo)
698 {
699         if (filename)
700                 munmap(membase, fssize * fsize);
701 #if 0
702         remove(mfsdevname);
703 #endif
704 }
705
706 #endif
707
708 /*VARARGS*/
709 void
710 fatal(const char *fmt, ...)
711 {
712         va_list ap;
713
714         va_start(ap, fmt);
715         if (fcntl(STDERR_FILENO, F_GETFL) < 0) {
716                 openlog(progname, LOG_CONS, LOG_DAEMON);
717                 vsyslog(LOG_ERR, fmt, ap);
718                 closelog();
719         } else {
720                 vwarnx(fmt, ap);
721         }
722         va_end(ap);
723         exit(1);
724         /*NOTREACHED*/
725 }
726
727 void
728 usage(void)
729 {
730         if (mfs) {
731                 fprintf(stderr,
732                     "usage: %s [ -fsoptions ] special-device mount-point\n",
733                         progname);
734         } else
735                 fprintf(stderr,
736                     "usage: %s [ -fsoptions ] special-device%s\n",
737                     progname,
738 #ifdef COMPAT
739                     " [device-type]");
740 #else
741                     "");
742 #endif
743         fprintf(stderr, "where fsoptions are:\n");
744         fprintf(stderr, "\t-C (mfs) Copy the underlying filesystem to the MFS mount\n");
745         fprintf(stderr, "\t-E erase file system contents using TRIM\n");
746         fprintf(stderr, "\t-L volume name\n");
747         fprintf(stderr,
748             "\t-N do not create file system, just print out parameters\n");
749         fprintf(stderr, "\t-O create a 4.3BSD format filesystem\n");
750         fprintf(stderr, "\t-S sector size\n");
751 #ifdef COMPAT
752         fprintf(stderr, "\t-T disktype\n");
753 #endif
754         fprintf(stderr, "\t-U enable soft updates\n");
755         fprintf(stderr, "\t-a maximum contiguous blocks\n");
756         fprintf(stderr, "\t-b block size\n");
757         fprintf(stderr, "\t-c cylinders/group\n");
758         fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
759         fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
760         fprintf(stderr, "\t-f frag size\n");
761         fprintf(stderr, "\t-g average file size\n");
762         fprintf(stderr, "\t-h average files per directory\n");
763         fprintf(stderr, "\t-i number of bytes per inode\n");
764         fprintf(stderr, "\t-k sector 0 skew, per track\n");
765         fprintf(stderr, "\t-l hardware sector interleave\n");
766         fprintf(stderr, "\t-m minimum free space %%\n");
767         fprintf(stderr, "\t-n number of distinguished rotational positions\n");
768         fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
769         fprintf(stderr, "\t-p spare sectors per track\n");
770         fprintf(stderr, "\t-s file system size (sectors)\n");
771         fprintf(stderr, "\t-r revolutions/minute\n");
772         fprintf(stderr, "\t-t tracks/cylinder\n");
773         fprintf(stderr, "\t-u sectors/track\n");
774         fprintf(stderr,
775         "\t-v do not attempt to determine partition name from device name\n");
776         fprintf(stderr, "\t-x spare sectors per cylinder\n");
777         exit(1);
778 }