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