When a traditional bsd disklabel is present, try to reserve SBSIZE bytes
[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 $
3af2835d 36 * $DragonFly: src/sbin/newfs/newfs.c,v 1.15 2007/05/19 06:39:39 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>
44#include <sys/disklabel.h>
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
MD
163int Uflag; /* enable soft updates for file system */
164int fssize; /* file system size */
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
203char device[MAXPATHLEN];
e4c9c0c8 204char mfsdevname[256];
984263bc
MD
205char *progname;
206
8af0d623
JS
207struct disklabel *getdisklabel(char *, int);
208static void rewritelabel(char *, int, struct disklabel *);
9a76cd75 209static void usage(void);
e4c9c0c8 210static void mfsintr(int signo);
984263bc
MD
211
212int
b5744197 213main(int argc, char **argv)
984263bc 214{
4462150b
CP
215 int ch;
216 struct partition *pp;
217 struct disklabel *lp;
984263bc 218 struct disklabel mfsfakelabel;
984263bc
MD
219 struct partition oldpartition;
220 struct stat st;
221 struct statfs *mp;
3af2835d
MD
222 int fsi = -1, fso, fsx = -1, len, n, vflag;
223 char *cp = NULL, *s1, *s2, *special, *special_slice;
4462150b 224 const char *opstring;
984263bc
MD
225#ifdef MFS
226 struct vfsconf vfc;
227 int error;
984263bc
MD
228#endif
229
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 ?
76f3e8b3 242 "NCF:T:Ua:b:c:d:e:f:g:h:i:m:o:s:v" :
984263bc
MD
243 "NOS: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 'N':
247 Nflag = 1;
248 break;
249 case 'O':
250 Oflag = 1;
251 break;
76f3e8b3
MD
252 case 'C':
253 Cflag = 1; /* MFS only */
254 break;
984263bc
MD
255 case 'S':
256 if ((sectorsize = atoi(optarg)) <= 0)
257 fatal("%s: bad sector size", optarg);
258 break;
259#ifdef COMPAT
260 case 'T':
261 disktype = optarg;
262 break;
263#endif
264 case 'F':
265 filename = optarg;
266 break;
267 case 'U':
268 Uflag = 1;
269 break;
270 case 'a':
271 if ((maxcontig = atoi(optarg)) <= 0)
272 fatal("%s: bad maximum contiguous blocks",
273 optarg);
274 break;
275 case 'b':
276 if ((bsize = atoi(optarg)) < MINBSIZE)
277 fatal("%s: bad block size", optarg);
278 break;
279 case 'c':
280 if ((cpg = atoi(optarg)) <= 0)
281 fatal("%s: bad cylinders/group", optarg);
282 cpgflg++;
283 break;
284 case 'd':
285 if ((rotdelay = atoi(optarg)) < 0)
286 fatal("%s: bad rotational delay", optarg);
287 break;
288 case 'e':
289 if ((maxbpg = atoi(optarg)) <= 0)
290 fatal("%s: bad blocks per file in a cylinder group",
291 optarg);
292 break;
293 case 'f':
294 if ((fsize = atoi(optarg)) <= 0)
295 fatal("%s: bad fragment size", optarg);
296 break;
297 case 'g':
298 if ((avgfilesize = atoi(optarg)) <= 0)
299 fatal("%s: bad average file size", optarg);
300 break;
301 case 'h':
302 if ((avgfilesperdir = atoi(optarg)) <= 0)
303 fatal("%s: bad average files per dir", optarg);
304 break;
305 case 'i':
306 if ((density = atoi(optarg)) <= 0)
307 fatal("%s: bad bytes per inode", optarg);
308 break;
309 case 'k':
310 if ((trackskew = atoi(optarg)) < 0)
311 fatal("%s: bad track skew", optarg);
312 break;
313 case 'l':
314 if ((interleave = atoi(optarg)) <= 0)
315 fatal("%s: bad interleave", optarg);
316 break;
317 case 'm':
318 if ((minfree = atoi(optarg)) < 0 || minfree > 99)
319 fatal("%s: bad free space %%", optarg);
320 break;
321 case 'n':
322 if ((nrpos = atoi(optarg)) < 0)
323 fatal("%s: bad rotational layout count",
324 optarg);
325 if (nrpos == 0)
326 nrpos = 1;
327 break;
328 case 'o':
329 if (mfs)
330 getmntopts(optarg, mopts, &mntflags, 0);
331 else {
332 if (strcmp(optarg, "space") == 0)
333 opt = FS_OPTSPACE;
334 else if (strcmp(optarg, "time") == 0)
335 opt = FS_OPTTIME;
336 else
337 fatal("%s: unknown optimization preference: use `space' or `time'", optarg);
338 }
339 break;
340 case 'p':
341 if ((trackspares = atoi(optarg)) < 0)
342 fatal("%s: bad spare sectors per track",
343 optarg);
344 break;
345 case 'r':
346 if ((rpm = atoi(optarg)) <= 0)
347 fatal("%s: bad revolutions/minute", optarg);
348 break;
349 case 's':
350 if ((fssize = atoi(optarg)) <= 0)
351 fatal("%s: bad file system size", optarg);
352 break;
353 case 't':
354 t_or_u_flag++;
355 if ((ntracks = atoi(optarg)) < 0)
356 fatal("%s: bad total tracks", optarg);
357 break;
358 case 'u':
359 t_or_u_flag++;
360 if ((nsectors = atoi(optarg)) < 0)
361 fatal("%s: bad sectors/track", optarg);
362 break;
363 case 'v':
364 vflag = 1;
365 break;
366 case 'x':
367 if ((cylspares = atoi(optarg)) < 0)
368 fatal("%s: bad spare sectors per cylinder",
369 optarg);
370 break;
371 case '?':
372 default:
373 usage();
374 }
375 argc -= optind;
376 argv += optind;
377
378 if (argc != 2 && (mfs || argc != 1))
379 usage();
380
381 special = argv[0];
3af2835d 382 special_slice = NULL;
984263bc
MD
383 /* Copy the NetBSD way of faking up a disk label */
384 if (mfs && !strcmp(special, "swap")) {
385 /*
386 * it's an MFS, mounted on "swap." fake up a label.
387 * XXX XXX XXX
388 */
389 fso = -1; /* XXX; normally done below. */
390
391 memset(&mfsfakelabel, 0, sizeof(mfsfakelabel));
392 mfsfakelabel.d_secsize = 512;
393 mfsfakelabel.d_nsectors = 64;
394 mfsfakelabel.d_ntracks = 16;
395 mfsfakelabel.d_ncylinders = 16;
396 mfsfakelabel.d_secpercyl = 1024;
397 mfsfakelabel.d_secperunit = 16384;
398 mfsfakelabel.d_rpm = 3600;
399 mfsfakelabel.d_interleave = 1;
400 mfsfakelabel.d_npartitions = 1;
401 mfsfakelabel.d_partitions[0].p_size = 16384;
402 mfsfakelabel.d_partitions[0].p_fsize = 1024;
403 mfsfakelabel.d_partitions[0].p_frag = 8;
404 mfsfakelabel.d_partitions[0].p_cpg = 16;
405
406 lp = &mfsfakelabel;
407 pp = &mfsfakelabel.d_partitions[0];
408
409 goto havelabel;
410 }
411
412 cp = strrchr(special, '/');
413 if (cp == 0) {
414 /*
415 * No path prefix; try /dev/%s.
416 */
7cee7052 417 snprintf(device, sizeof(device), "%s%s", _PATH_DEV, special);
984263bc
MD
418 special = device;
419 }
420 if (Nflag) {
421 fso = -1;
422 } else {
423 fso = open(special, O_WRONLY);
424 if (fso < 0)
425 fatal("%s: %s", special, strerror(errno));
426
427 /* Bail if target special is mounted */
428 n = getmntinfo(&mp, MNT_NOWAIT);
429 if (n == 0)
430 fatal("%s: getmntinfo: %s", special, strerror(errno));
431
432 len = sizeof(_PATH_DEV) - 1;
433 s1 = special;
434 if (strncmp(_PATH_DEV, s1, len) == 0)
435 s1 += len;
436
437 while (--n >= 0) {
438 s2 = mp->f_mntfromname;
439 if (strncmp(_PATH_DEV, s2, len) == 0) {
440 s2 += len - 1;
441 *s2 = 'r';
442 }
443 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
444 fatal("%s is mounted on %s",
445 special, mp->f_mntonname);
446 ++mp;
447 }
448 }
449 if (mfs && disktype != NULL) {
450 lp = (struct disklabel *)getdiskbyname(disktype);
451 if (lp == NULL)
452 fatal("%s: unknown disk type", disktype);
453 pp = &lp->d_partitions[1];
454 } else {
455 fsi = open(special, O_RDONLY);
456 if (fsi < 0)
457 fatal("%s: %s", special, strerror(errno));
458 if (fstat(fsi, &st) < 0)
459 fatal("%s: %s", special, strerror(errno));
460 if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs)
461 printf("%s: %s: not a character-special device\n",
462 progname, special);
3af2835d 463 if (special[0] == 0)
984263bc 464 fatal("null special file name");
3af2835d
MD
465 cp = special + strlen(special) - 1;
466 if (!vflag && (*cp < 'a' || *cp >= 'a' + MAXPARTITIONS)) {
984263bc
MD
467 fatal("%s: can't figure out file system partition",
468 argv[0]);
b44495f7 469 }
984263bc
MD
470#ifdef COMPAT
471 if (!mfs && disktype == NULL)
472 disktype = argv[1];
473#endif
3af2835d
MD
474 if (vflag) {
475 asprintf(&special_slice, "%s", special);
476 } else {
477 asprintf(&special_slice, "%*.*s",
478 cp - special, cp - special, special);
479 }
480 fsx = open(special_slice, O_RDONLY);
481 if (fsx < 0) {
482 fatal("cannot retrieve disklabel from %s",
483 special_slice);
484 }
485 lp = getdisklabel(special, fsx);
486 close(fsx);
487 if (vflag)
488 pp = &lp->d_partitions[2];
984263bc
MD
489 else
490 pp = &lp->d_partitions[*cp - 'a'];
491 if (pp->p_size == 0)
492 fatal("%s: `%c' partition is unavailable",
493 argv[0], *cp);
494 if (pp->p_fstype == FS_BOOT)
495 fatal("%s: `%c' partition overlaps boot program",
496 argv[0], *cp);
497 }
498havelabel:
499 if (fssize == 0)
500 fssize = pp->p_size;
4462150b 501 if ((uint32_t)fssize > pp->p_size && !mfs)
984263bc
MD
502 fatal("%s: maximum file system size on the `%c' partition is %d",
503 argv[0], *cp, pp->p_size);
504 if (rpm == 0) {
505 rpm = lp->d_rpm;
506 if (rpm <= 0)
507 rpm = 3600;
508 }
509 if (ntracks == 0) {
510 ntracks = lp->d_ntracks;
511 if (ntracks <= 0)
512 fatal("%s: no default #tracks", argv[0]);
513 }
514 if (nsectors == 0) {
515 nsectors = lp->d_nsectors;
516 if (nsectors <= 0)
517 fatal("%s: no default #sectors/track", argv[0]);
518 }
519 if (sectorsize == 0) {
520 sectorsize = lp->d_secsize;
521 if (sectorsize <= 0)
522 fatal("%s: no default sector size", argv[0]);
523 }
524 if (trackskew == -1) {
525 trackskew = lp->d_trackskew;
526 if (trackskew < 0)
527 trackskew = 0;
528 }
529 if (interleave == 0) {
530 interleave = lp->d_interleave;
531 if (interleave <= 0)
532 interleave = 1;
533 }
534 if (fsize == 0) {
535 fsize = pp->p_fsize;
536 if (fsize <= 0)
537 fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
538 }
539 if (bsize == 0) {
540 bsize = pp->p_frag * pp->p_fsize;
541 if (bsize <= 0)
542 bsize = MIN(DFL_BLKSIZE, 8 * fsize);
543 }
544 /*
545 * Maxcontig sets the default for the maximum number of blocks
546 * that may be allocated sequentially. With filesystem clustering
547 * it is possible to allocate contiguous blocks up to the maximum
548 * transfer size permitted by the controller or buffering.
549 */
550 if (maxcontig == 0)
551 maxcontig = MAX(1, MAXPHYS / bsize - 1);
552 if (density == 0)
553 density = NFPI * fsize;
554 if (minfree < MINFREE && opt != FS_OPTSPACE) {
555 fprintf(stderr, "Warning: changing optimization to space ");
556 fprintf(stderr, "because minfree is less than %d%%\n", MINFREE);
557 opt = FS_OPTSPACE;
558 }
559 if (trackspares == -1) {
560 trackspares = lp->d_sparespertrack;
561 if (trackspares < 0)
562 trackspares = 0;
563 }
564 nphyssectors = nsectors + trackspares;
565 if (cylspares == -1) {
566 cylspares = lp->d_sparespercyl;
567 if (cylspares < 0)
568 cylspares = 0;
569 }
570 secpercyl = nsectors * ntracks - cylspares;
571 /*
572 * Only complain if -t or -u have been specified; the default
573 * case (4096 sectors per cylinder) is intended to disagree
574 * with the disklabel.
575 */
4462150b 576 if (t_or_u_flag && (uint32_t)secpercyl != lp->d_secpercyl)
984263bc
MD
577 fprintf(stderr, "%s (%d) %s (%lu)\n",
578 "Warning: calculated sectors per cylinder", secpercyl,
579 "disagrees with disk label", (u_long)lp->d_secpercyl);
580 if (maxbpg == 0)
581 maxbpg = MAXBLKPG(bsize);
582 headswitch = lp->d_headswitch;
583 trackseek = lp->d_trkseek;
584#ifdef notdef /* label may be 0 if faked up by kernel */
585 bbsize = lp->d_bbsize;
586 sbsize = lp->d_sbsize;
587#endif
588 oldpartition = *pp;
984263bc
MD
589 realsectorsize = sectorsize;
590 if (sectorsize != DEV_BSIZE) { /* XXX */
591 int secperblk = sectorsize / DEV_BSIZE;
592
593 sectorsize = DEV_BSIZE;
594 nsectors *= secperblk;
595 nphyssectors *= secperblk;
596 secpercyl *= secperblk;
597 fssize *= secperblk;
598 pp->p_size *= secperblk;
599 }
984263bc
MD
600 if (mfs) {
601 mfs_mtpt = argv[1];
602 if (
603 stat(mfs_mtpt, &mfs_mtstat) < 0 ||
604 !S_ISDIR(mfs_mtstat.st_mode)
605 ) {
606 fatal("mount point not dir: %s", mfs_mtpt);
607 }
608 }
76f3e8b3 609 mkfs(pp, special, fsi, fso, (Cflag && mfs) ? argv[1] : NULL);
984263bc
MD
610 if (realsectorsize != DEV_BSIZE)
611 pp->p_size /= realsectorsize /DEV_BSIZE;
3af2835d
MD
612 if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition))) {
613 if (special_slice && (fsx = open(special_slice, O_RDWR)) >= 0) {
614 rewritelabel(special_slice, fsx, lp);
615 close(fsx);
616 }
617 }
984263bc
MD
618 if (!Nflag)
619 close(fso);
620 close(fsi);
621#ifdef MFS
622 if (mfs) {
623 struct mfs_args args;
e4c9c0c8 624 int udev;
984263bc 625
e4c9c0c8
MD
626 snprintf(mfsdevname, sizeof(mfsdevname), "/dev/mfs%d",
627 getpid());
628 args.fspec = mfsdevname;
984263bc
MD
629 args.export.ex_root = -2;
630 if (mntflags & MNT_RDONLY)
631 args.export.ex_flags = MNT_EXRDONLY;
632 else
633 args.export.ex_flags = 0;
634 args.base = membase;
635 args.size = fssize * sectorsize;
636
637 error = getvfsbyname("mfs", &vfc);
638 if (error && vfsisloadable("mfs")) {
639 if (vfsload("mfs"))
640 fatal("vfsload(mfs)");
641 endvfsent(); /* flush cache */
642 error = getvfsbyname("mfs", &vfc);
643 }
644 if (error)
645 fatal("mfs filesystem not available");
646
e4c9c0c8
MD
647 udev = (253 << 8) | (getpid() & 255) |
648 ((getpid() & ~0xFF) << 8);
649 if (mknod(mfsdevname, S_IFCHR | 0700, udev) < 0)
650 printf("Warning: unable to create %s\n", mfsdevname);
651 signal(SIGINT, mfsintr);
984263bc
MD
652 if (mount(vfc.vfc_name, argv[1], mntflags, &args) < 0)
653 fatal("%s: %s", argv[1], strerror(errno));
e4c9c0c8
MD
654 signal(SIGINT, SIG_DFL);
655 mfsintr(SIGINT);
984263bc
MD
656 }
657#endif
658 exit(0);
659}
660
e4c9c0c8
MD
661#ifdef MFS
662
663static void
4462150b 664mfsintr(__unused int signo)
e4c9c0c8
MD
665{
666 if (filename)
667 munmap(membase, fssize * sectorsize);
668 remove(mfsdevname);
669}
670
671#endif
672
984263bc
MD
673#ifdef COMPAT
674char lmsg[] = "%s: can't read disk label; disk type must be specified";
675#else
676char lmsg[] = "%s: can't read disk label";
677#endif
678
679struct disklabel *
b5744197 680getdisklabel(char *s, int fd)
984263bc
MD
681{
682 static struct disklabel lab;
683
684 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
685#ifdef COMPAT
686 if (disktype) {
4462150b 687 struct disklabel *lp;
984263bc
MD
688
689 unlabeled++;
690 lp = getdiskbyname(disktype);
691 if (lp == NULL)
692 fatal("%s: unknown disk type", disktype);
693 return (lp);
694 }
695#endif
696 warn("ioctl (GDINFO)");
697 fatal(lmsg, s);
698 }
699 return (&lab);
700}
701
8af0d623
JS
702static void
703rewritelabel(char *s, int fd, struct disklabel *lp)
984263bc
MD
704{
705#ifdef COMPAT
706 if (unlabeled)
707 return;
708#endif
709 lp->d_checksum = 0;
710 lp->d_checksum = dkcksum(lp);
711 if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
712 warn("ioctl (WDINFO)");
713 fatal("%s: can't rewrite disk label", s);
714 }
715}
716
717/*VARARGS*/
718void
984263bc 719fatal(const char *fmt, ...)
984263bc
MD
720{
721 va_list ap;
722
984263bc 723 va_start(ap, fmt);
984263bc
MD
724 if (fcntl(STDERR_FILENO, F_GETFL) < 0) {
725 openlog(progname, LOG_CONS, LOG_DAEMON);
726 vsyslog(LOG_ERR, fmt, ap);
727 closelog();
728 } else {
729 vwarnx(fmt, ap);
730 }
731 va_end(ap);
732 exit(1);
733 /*NOTREACHED*/
734}
735
736static void
b5744197 737usage(void)
984263bc
MD
738{
739 if (mfs) {
740 fprintf(stderr,
741 "usage: %s [ -fsoptions ] special-device mount-point\n",
742 progname);
743 } else
744 fprintf(stderr,
745 "usage: %s [ -fsoptions ] special-device%s\n",
746 progname,
747#ifdef COMPAT
748 " [device-type]");
749#else
750 "");
751#endif
752 fprintf(stderr, "where fsoptions are:\n");
70d0e24f 753 fprintf(stderr, "\t-C (mfs) Copy the underlying filesystem to the MFS mount\n");
984263bc
MD
754 fprintf(stderr,
755 "\t-N do not create file system, just print out parameters\n");
756 fprintf(stderr, "\t-O create a 4.3BSD format filesystem\n");
757 fprintf(stderr, "\t-S sector size\n");
758#ifdef COMPAT
759 fprintf(stderr, "\t-T disktype\n");
760#endif
761 fprintf(stderr, "\t-U enable soft updates\n");
762 fprintf(stderr, "\t-a maximum contiguous blocks\n");
763 fprintf(stderr, "\t-b block size\n");
764 fprintf(stderr, "\t-c cylinders/group\n");
765 fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
766 fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
767 fprintf(stderr, "\t-f frag size\n");
768 fprintf(stderr, "\t-g average file size\n");
769 fprintf(stderr, "\t-h average files per directory\n");
770 fprintf(stderr, "\t-i number of bytes per inode\n");
771 fprintf(stderr, "\t-k sector 0 skew, per track\n");
772 fprintf(stderr, "\t-l hardware sector interleave\n");
773 fprintf(stderr, "\t-m minimum free space %%\n");
774 fprintf(stderr, "\t-n number of distinguished rotational positions\n");
775 fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
776 fprintf(stderr, "\t-p spare sectors per track\n");
777 fprintf(stderr, "\t-s file system size (sectors)\n");
778 fprintf(stderr, "\t-r revolutions/minute\n");
779 fprintf(stderr, "\t-t tracks/cylinder\n");
780 fprintf(stderr, "\t-u sectors/track\n");
781 fprintf(stderr,
782 "\t-v do not attempt to determine partition name from device name\n");
783 fprintf(stderr, "\t-x spare sectors per cylinder\n");
784 exit(1);
785}