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