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