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