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