newfs.c revision 1.46 1 /* $NetBSD: newfs.c,v 1.46 2001/08/30 14:37:26 lukem Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1983, 1989, 1993, 1994\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)newfs.c 8.13 (Berkeley) 5/1/95";
45 #else
46 __RCSID("$NetBSD: newfs.c,v 1.46 2001/08/30 14:37:26 lukem Exp $");
47 #endif
48 #endif /* not lint */
49
50 /*
51 * newfs: friendly front end to mkfs
52 */
53 #include <sys/param.h>
54 #include <sys/stat.h>
55 #include <sys/ioctl.h>
56 #include <sys/disklabel.h>
57 #include <sys/file.h>
58 #include <sys/mount.h>
59 #include <sys/sysctl.h>
60 #include <sys/wait.h>
61
62 #include <ufs/ufs/dir.h>
63 #include <ufs/ufs/dinode.h>
64 #include <ufs/ufs/ufsmount.h>
65 #include <ufs/ffs/fs.h>
66
67 #include <ctype.h>
68 #include <disktab.h>
69 #include <errno.h>
70 #include <paths.h>
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <string.h>
74 #include <syslog.h>
75 #include <unistd.h>
76 #include <signal.h>
77 #include <err.h>
78 #include <util.h>
79
80 #include "mntopts.h"
81 #include "dkcksum.h"
82 #include "extern.h"
83
84 struct mntopt mopts[] = {
85 MOPT_STDOPTS,
86 MOPT_ASYNC,
87 MOPT_UPDATE,
88 MOPT_NOATIME,
89 { NULL },
90 };
91
92 static struct disklabel *getdisklabel(char *, int);
93 static void rewritelabel(char *, int, struct disklabel *);
94 static int strsuftoi(const char *, const char *, int, int);
95 static void usage(void);
96 int main(int, char *[]);
97
98 #define COMPAT /* allow non-labeled disks */
99
100 /*
101 * The following two constants set the default block and fragment sizes.
102 * Both constants must be a power of 2 and meet the following constraints:
103 * MINBSIZE <= DESBLKSIZE <= MAXBSIZE
104 * sectorsize <= DESFRAGSIZE <= DESBLKSIZE
105 * DESBLKSIZE / DESFRAGSIZE <= 8
106 */
107 #define DFL_FRAGSIZE 1024
108 #define DFL_BLKSIZE 8192
109
110 /*
111 * Default sector size.
112 */
113 #define DFL_SECSIZE 512
114
115 /*
116 * Cylinder groups may have up to many cylinders. The actual
117 * number used depends upon how much information can be stored
118 * on a single cylinder. The default is to use 16 cylinders
119 * per group.
120 */
121 #define DESCPG 16 /* desired fs_cpg */
122
123 /*
124 * ROTDELAY gives the minimum number of milliseconds to initiate
125 * another disk transfer on the same cylinder. It is used in
126 * determining the rotationally optimal layout for disk blocks
127 * within a file; the default of fs_rotdelay is 0ms.
128 */
129 #define ROTDELAY 0
130
131 /*
132 * MAXBLKPG determines the maximum number of data blocks which are
133 * placed in a single cylinder group. The default is one indirect
134 * block worth of data blocks.
135 */
136 #define MAXBLKPG(bsize) ((bsize) / sizeof(daddr_t))
137
138 /*
139 * Each file system has a number of inodes statically allocated.
140 * We allocate one inode slot per NFPI fragments, expecting this
141 * to be far more than we will ever need.
142 */
143 #define NFPI 4
144
145 /*
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. Caching
151 * and zoning pretty much defeats rotational optimization, so we now use a
152 * default of 1.
153 */
154 #define NRPOS 1 /* number distinct rotational positions */
155
156
157 int mfs; /* run as the memory based filesystem */
158 int Nflag; /* run without writing file system */
159 int Oflag; /* format as an 4.3BSD file system */
160 int fssize; /* file system size */
161 int ntracks; /* # tracks/cylinder */
162 int nsectors; /* # sectors/track */
163 int nphyssectors; /* # sectors/track including spares */
164 int secpercyl; /* sectors per cylinder */
165 int trackspares = -1; /* spare sectors per track */
166 int cylspares = -1; /* spare sectors per cylinder */
167 int sectorsize; /* bytes/sector */
168 int rpm; /* revolutions/minute of drive */
169 int interleave; /* hardware sector interleave */
170 int trackskew = -1; /* sector 0 skew, per track */
171 int fsize = 0; /* fragment size */
172 int bsize = 0; /* block size */
173 int cpg = DESCPG; /* cylinders/cylinder group */
174 int cpgflg; /* cylinders/cylinder group flag was given */
175 int minfree = MINFREE; /* free space threshold */
176 int opt = DEFAULTOPT; /* optimization preference (space or time) */
177 int density; /* number of bytes per inode */
178 int maxcontig = 0; /* max contiguous blocks to allocate */
179 int rotdelay = ROTDELAY; /* rotational delay between blocks */
180 int maxbpg; /* maximum blocks per file in a cyl group */
181 int nrpos = NRPOS; /* # of distinguished rotational positions */
182 int bbsize = BBSIZE; /* boot block size */
183 int sbsize = SBSIZE; /* superblock size */
184 int mntflags = MNT_ASYNC; /* flags to be passed to mount */
185 u_long memleft; /* virtual memory available */
186 caddr_t membase; /* start address of memory based filesystem */
187 int needswap; /* Filesystem not in native byte order */
188 #ifdef COMPAT
189 char *disktype;
190 int unlabeled;
191 #endif
192
193 char device[MAXPATHLEN];
194
195 int
196 main(int argc, char *argv[])
197 {
198 struct partition *pp;
199 struct disklabel *lp;
200 struct disklabel mfsfakelabel;
201 struct partition oldpartition;
202 struct stat st;
203 struct statfs *mp;
204 int ch, fsi, fso, len, maxpartitions, n, Fflag, Zflag;
205 char *cp, *endp, *s1, *s2, *special;
206 const char *opstring;
207 long long llsize;
208 #ifdef MFS
209 char mountfromname[100];
210 pid_t pid, res;
211 struct statfs sf;
212 int status;
213 #endif
214
215 cp = NULL;
216 fsi = fso = -1;
217 Fflag = Zflag = 0;
218 if (strstr(getprogname(), "mfs")) {
219 mfs = 1;
220 Nflag++;
221 }
222
223 maxpartitions = getmaxpartitions();
224 if (maxpartitions > 26)
225 errx(1, "insane maxpartitions value %d", maxpartitions);
226
227 opstring = mfs ?
228 "NT:a:b:c:d:e:f:i:m:o:s:" :
229 "B:FNOS:T:Za:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:";
230 while ((ch = getopt(argc, argv, opstring)) != -1)
231 switch (ch) {
232 case 'B':
233 if (strcmp(optarg, "be") == 0) {
234 #if BYTE_ORDER == LITTLE_ENDIAN
235 needswap = 1;
236 #endif
237 } else if (strcmp(optarg, "le") == 0) {
238 #if BYTE_ORDER == BIG_ENDIAN
239 needswap = 1;
240 #endif
241 } else
242 usage();
243 break;
244 case 'F':
245 Fflag = 1;
246 break;
247 case 'N':
248 Nflag = 1;
249 break;
250 case 'O':
251 Oflag = 1;
252 break;
253 case 'S':
254 sectorsize = strsuftoi("sector size",
255 optarg, 1, INT_MAX);
256 break;
257 #ifdef COMPAT
258 case 'T':
259 disktype = optarg;
260 break;
261 #endif
262 case 'Z':
263 Zflag = 1;
264 break;
265 case 'a':
266 maxcontig = strsuftoi("maximum contiguous blocks",
267 optarg, 1, INT_MAX);
268 break;
269 case 'b':
270 bsize = strsuftoi("block size",
271 optarg, MINBSIZE, INT_MAX);
272 break;
273 case 'c':
274 cpg = strsuftoi("cylinders per group",
275 optarg, 1, INT_MAX);
276 cpgflg++;
277 break;
278 case 'd':
279 rotdelay = strsuftoi("rotational delay",
280 optarg, 0, INT_MAX);
281 break;
282 case 'e':
283 maxbpg = strsuftoi(
284 "blocks per file in a cylinder group",
285 optarg, 1, INT_MAX);
286 break;
287 case 'f':
288 fsize = strsuftoi("fragment size",
289 optarg, 1, INT_MAX);
290 break;
291 case 'i':
292 density = strsuftoi("bytes per inode",
293 optarg, 1, INT_MAX);
294 break;
295 case 'k':
296 trackskew = strsuftoi("track skew",
297 optarg, 0, INT_MAX);
298 break;
299 case 'l':
300 interleave = strsuftoi("interleave",
301 optarg, 1, INT_MAX);
302 break;
303 case 'm':
304 minfree = strsuftoi("free space %",
305 optarg, 0, 99);
306 break;
307 case 'n':
308 nrpos = strsuftoi("rotational layout count",
309 optarg, 1, INT_MAX);
310 break;
311 case 'o':
312 if (mfs)
313 getmntopts(optarg, mopts, &mntflags, 0);
314 else {
315 if (strcmp(optarg, "space") == 0)
316 opt = FS_OPTSPACE;
317 else if (strcmp(optarg, "time") == 0)
318 opt = FS_OPTTIME;
319 else
320 errx(1, "%s %s",
321 "unknown optimization preference: ",
322 "use `space' or `time'.");
323 }
324 break;
325 case 'p':
326 trackspares = strsuftoi("spare sectors per track",
327 optarg, 0, INT_MAX);
328 break;
329 case 'r':
330 rpm = strsuftoi("revolutions per minute",
331 optarg, 1, INT_MAX);
332 break;
333 case 's':
334 llsize = strtoll(optarg, &endp, 10);
335 if (endp[0] != '\0' && endp[1] != '\0')
336 llsize = -1;
337 else {
338 int ssiz;
339
340 ssiz = (sectorsize ? sectorsize : DFL_SECSIZE);
341 switch (tolower((unsigned char)endp[0])) {
342 case 'b':
343 llsize /= ssiz;
344 break;
345 case 'k':
346 llsize *= 1024 / ssiz;
347 break;
348 case 'm':
349 llsize *= 1024 * 1024 / ssiz;
350 break;
351 case 'g':
352 llsize *= 1024 * 1024 * 1024 / ssiz;
353 break;
354 case '\0':
355 case 's':
356 break;
357 default:
358 llsize = -1;
359 }
360 }
361 if (llsize > INT_MAX)
362 errx(1, "file system size `%s' is too large.",
363 optarg);
364 if (llsize <= 0)
365 errx(1,
366 "`%s' is not a valid number for file system size.",
367 optarg);
368 fssize = (int)llsize;
369 break;
370 case 't':
371 ntracks = strsuftoi("total tracks",
372 optarg, 1, INT_MAX);
373 break;
374 case 'u':
375 nsectors = strsuftoi("sectors per track",
376 optarg, 1, INT_MAX);
377 break;
378 case 'x':
379 cylspares = strsuftoi("spare sectors per cylinder",
380 optarg, 0, INT_MAX);
381 break;
382 case '?':
383 default:
384 usage();
385 }
386 argc -= optind;
387 argv += optind;
388
389 if (argc != 2 && (mfs || argc != 1))
390 usage();
391
392 special = argv[0];
393 if ((mfs && !strcmp(special, "swap")) || Fflag) {
394 /*
395 * it's an MFS mounted on "swap" or a file system image;
396 * fake up a label. XXX
397 */
398 if (!sectorsize)
399 sectorsize = DFL_SECSIZE;
400
401 if (Fflag && (stat(special, &st) != -1 && !S_ISREG(st.st_mode)))
402 errx(1, "%s is not a regular file", special);
403 if (Fflag && !Nflag) { /* creating image in a regular file */
404 if (fssize == 0)
405 errx(1, "need to specify size when using -F");
406 fso = open(special, O_RDWR | O_CREAT | O_TRUNC, 0777);
407 if (fso == -1)
408 err(1, "can't open file %s", special);
409 if ((fsi = dup(fso)) == -1)
410 err(1, "can't dup(2) image fd");
411 if (ftruncate(fso, (off_t)fssize * sectorsize) == -1)
412 err(1, "can't resize %s to %d",
413 special, fssize);
414
415 if (Zflag) { /* pre-zero the file */
416 char *buf;
417 int bufsize, i;
418 off_t bufrem;
419 struct statfs sfs;
420
421 if (fstatfs(fso, &sfs) == -1) {
422 warn("can't fstatfs `%s'", special);
423 bufsize = 8192;
424 } else
425 bufsize = sfs.f_iosize;
426
427 if ((buf = calloc(1, bufsize)) == NULL)
428 err(1, "can't malloc buffer of %d",
429 bufsize);
430 bufrem = fssize * sectorsize;
431 printf(
432 "Creating file system image in `%s', size %lld bytes, in %d byte chunks.\n",
433 special, (long long)bufrem, bufsize);
434 while (bufrem > 0) {
435 i = write(fso, buf,
436 MIN(bufsize, bufrem));
437 if (i == -1)
438 err(1, "writing image");
439 bufrem -= i;
440 }
441 }
442
443 }
444
445 memset(&mfsfakelabel, 0, sizeof(mfsfakelabel));
446 mfsfakelabel.d_secsize = sectorsize;
447 mfsfakelabel.d_nsectors = 64; /* these 3 add up to 16MB */
448 mfsfakelabel.d_ntracks = 16;
449 mfsfakelabel.d_ncylinders = 16;
450 mfsfakelabel.d_secpercyl =
451 mfsfakelabel.d_nsectors * mfsfakelabel.d_ntracks;
452 mfsfakelabel.d_secperunit =
453 mfsfakelabel.d_ncylinders * mfsfakelabel.d_secpercyl;
454 mfsfakelabel.d_rpm = 3600;
455 mfsfakelabel.d_interleave = 1;
456 mfsfakelabel.d_npartitions = 1;
457 mfsfakelabel.d_partitions[0].p_size = mfsfakelabel.d_secperunit;
458 mfsfakelabel.d_partitions[0].p_fsize = 1024;
459 mfsfakelabel.d_partitions[0].p_frag = 8;
460 mfsfakelabel.d_partitions[0].p_cpg = 16;
461
462 lp = &mfsfakelabel;
463 pp = &mfsfakelabel.d_partitions[0];
464
465 goto havelabel;
466 }
467 cp = strrchr(special, '/');
468 if (cp == NULL) {
469 /*
470 * No path prefix; try /dev/r%s then /dev/%s.
471 */
472 (void)snprintf(device, sizeof(device), "%sr%s",
473 _PATH_DEV, special);
474 if (stat(device, &st) == -1)
475 (void)snprintf(device, sizeof(device), "%s%s",
476 _PATH_DEV, special);
477 special = device;
478 }
479 if (Nflag) {
480 fso = -1;
481 } else {
482 fso = open(special, O_WRONLY);
483 if (fso < 0)
484 err(1, "%s: open", special);
485
486 /* Bail if target special is mounted */
487 n = getmntinfo(&mp, MNT_NOWAIT);
488 if (n == 0)
489 err(1, "%s: getmntinfo", special);
490
491 len = sizeof(_PATH_DEV) - 1;
492 s1 = special;
493 if (strncmp(_PATH_DEV, s1, len) == 0)
494 s1 += len;
495
496 while (--n >= 0) {
497 s2 = mp->f_mntfromname;
498 if (strncmp(_PATH_DEV, s2, len) == 0) {
499 s2 += len - 1;
500 *s2 = 'r';
501 }
502 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
503 errx(1, "%s is mounted on %s",
504 special, mp->f_mntonname);
505 ++mp;
506 }
507 }
508 if (mfs && disktype != NULL) {
509 lp = (struct disklabel *)getdiskbyname(disktype);
510 if (lp == NULL)
511 errx(1, "%s: unknown disk type", disktype);
512 pp = &lp->d_partitions[1];
513 } else {
514 fsi = open(special, O_RDONLY);
515 if (fsi < 0)
516 err(1, "%s: open", special);
517 if (fstat(fsi, &st) < 0)
518 err(1, "%s: fstat", special);
519 if (!S_ISCHR(st.st_mode) && !mfs)
520 warnx("%s: not a character-special device", special);
521 cp = strchr(argv[0], '\0') - 1;
522 if (cp == 0 || ((*cp < 'a' || *cp > ('a' + maxpartitions - 1))
523 && !isdigit(*cp)))
524 errx(1, "can't figure out file system partition");
525 #ifdef COMPAT
526 if (!mfs && disktype == NULL)
527 disktype = argv[1];
528 #endif
529 lp = getdisklabel(special, fsi);
530 if (isdigit(*cp))
531 pp = &lp->d_partitions[0];
532 else
533 pp = &lp->d_partitions[*cp - 'a'];
534 if (pp->p_size == 0)
535 errx(1, "`%c' partition is unavailable", *cp);
536 if (pp->p_fstype == FS_BOOT)
537 errx(1, "`%c' partition overlaps boot program", *cp);
538 }
539
540 havelabel:
541 if (fssize == 0)
542 fssize = pp->p_size;
543 if (fssize > pp->p_size && !mfs && !Fflag)
544 errx(1, "maximum file system size on the `%c' partition is %d",
545 *cp, pp->p_size);
546 if (rpm == 0) {
547 rpm = lp->d_rpm;
548 if (rpm <= 0)
549 rpm = 3600;
550 }
551 if (ntracks == 0) {
552 ntracks = lp->d_ntracks;
553 if (ntracks <= 0)
554 errx(1, "no default #tracks");
555 }
556 if (nsectors == 0) {
557 nsectors = lp->d_nsectors;
558 if (nsectors <= 0)
559 errx(1, "no default #sectors/track");
560 }
561 if (sectorsize == 0) {
562 sectorsize = lp->d_secsize;
563 if (sectorsize <= 0)
564 errx(1, "no default sector size");
565 }
566 if (trackskew == -1) {
567 trackskew = lp->d_trackskew;
568 if (trackskew < 0)
569 trackskew = 0;
570 }
571 if (interleave == 0) {
572 interleave = lp->d_interleave;
573 if (interleave <= 0)
574 interleave = 1;
575 }
576 if (fsize == 0) {
577 fsize = pp->p_fsize;
578 if (fsize <= 0)
579 fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
580 }
581 if (bsize == 0) {
582 bsize = pp->p_frag * pp->p_fsize;
583 if (bsize <= 0)
584 bsize = MIN(DFL_BLKSIZE, 8 * fsize);
585 }
586 if (cpgflg == 0) {
587 if (pp->p_cpg != 0)
588 cpg = pp->p_cpg;
589 }
590 /*
591 * Maxcontig sets the default for the maximum number of blocks
592 * that may be allocated sequentially. With filesystem clustering
593 * it is possible to allocate contiguous blocks up to the maximum
594 * transfer size permitted by the controller or buffering.
595 */
596 if (maxcontig == 0)
597 maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize);
598 if (density == 0)
599 density = NFPI * fsize;
600 if (minfree < MINFREE && opt != FS_OPTSPACE) {
601 warnx("%s %s %d%%", "Warning: changing optimization to space",
602 "because minfree is less than", MINFREE);
603 opt = FS_OPTSPACE;
604 }
605 if (trackspares == -1) {
606 trackspares = lp->d_sparespertrack;
607 if (trackspares < 0)
608 trackspares = 0;
609 }
610 nphyssectors = nsectors + trackspares;
611 if (cylspares == -1) {
612 cylspares = lp->d_sparespercyl;
613 if (cylspares < 0)
614 cylspares = 0;
615 }
616 secpercyl = nsectors * ntracks - cylspares;
617 if (secpercyl != lp->d_secpercyl)
618 warnx("%s (%d) %s (%u)\n",
619 "Warning: calculated sectors per cylinder", secpercyl,
620 "disagrees with disk label", lp->d_secpercyl);
621 if (maxbpg == 0)
622 maxbpg = MAXBLKPG(bsize);
623 #ifdef notdef /* label may be 0 if faked up by kernel */
624 bbsize = lp->d_bbsize;
625 sbsize = lp->d_sbsize;
626 #endif
627 oldpartition = *pp;
628 mkfs(pp, special, fsi, fso);
629 if (!Nflag && memcmp(pp, &oldpartition, sizeof(oldpartition)) && !Fflag)
630 rewritelabel(special, fso, lp);
631 if (!Nflag)
632 close(fso);
633 close(fsi);
634 #ifdef MFS
635 if (mfs) {
636 struct mfs_args args;
637
638 switch (pid = fork()) {
639 case -1:
640 perror("mfs");
641 exit(10);
642 case 0:
643 (void)snprintf(mountfromname, sizeof(mountfromname),
644 "mfs:%d", getpid());
645 break;
646 default:
647 (void)snprintf(mountfromname, sizeof(mountfromname),
648 "mfs:%d", pid);
649 for (;;) {
650 /*
651 * spin until the mount succeeds
652 * or the child exits
653 */
654 usleep(1);
655
656 /*
657 * XXX Here is a race condition: another process
658 * can mount a filesystem which hides our
659 * ramdisk before we see the success.
660 */
661 if (statfs(argv[1], &sf) < 0)
662 err(88, "statfs %s", argv[1]);
663 if (!strcmp(sf.f_mntfromname, mountfromname) &&
664 !strncmp(sf.f_mntonname, argv[1],
665 MNAMELEN) &&
666 !strcmp(sf.f_fstypename, "mfs"))
667 exit(0);
668
669 res = waitpid(pid, &status, WNOHANG);
670 if (res == -1)
671 err(11, "waitpid");
672 if (res != pid)
673 continue;
674 if (WIFEXITED(status)) {
675 if (WEXITSTATUS(status) == 0)
676 exit(0);
677 errx(1, "%s: mount: %s", argv[1],
678 strerror(WEXITSTATUS(status)));
679 } else
680 errx(11, "abnormal termination");
681 }
682 /* NOTREACHED */
683 }
684
685 (void) setsid();
686 (void) close(0);
687 (void) close(1);
688 (void) close(2);
689 (void) chdir("/");
690
691 args.fspec = mountfromname;
692 args.export.ex_root = -2;
693 if (mntflags & MNT_RDONLY)
694 args.export.ex_flags = MNT_EXRDONLY;
695 else
696 args.export.ex_flags = 0;
697 args.base = membase;
698 args.size = fssize * sectorsize;
699 if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0)
700 exit(errno); /* parent prints message */
701 }
702 #endif
703 exit(0);
704 }
705
706 #ifdef COMPAT
707 const char lmsg[] = "%s: can't read disk label; disk type must be specified";
708 #else
709 const char lmsg[] = "%s: can't read disk label";
710 #endif
711
712 static struct disklabel *
713 getdisklabel(char *s, volatile int fd)
714 /* XXX why is fs volatile?! */
715 {
716 static struct disklabel lab;
717
718 if (ioctl(fd, DIOCGDINFO, &lab) < 0) {
719 #ifdef COMPAT
720 if (disktype) {
721 struct disklabel *lp;
722
723 unlabeled++;
724 lp = getdiskbyname(disktype);
725 if (lp == NULL)
726 errx(1, "%s: unknown disk type", disktype);
727 return (lp);
728 }
729 #endif
730 warn("ioctl (GDINFO)");
731 errx(1, lmsg, s);
732 }
733 return (&lab);
734 }
735
736 static void
737 rewritelabel(char *s, volatile int fd, struct disklabel *lp)
738 /* XXX why is fd volatile?! */
739 {
740 #ifdef COMPAT
741 if (unlabeled)
742 return;
743 #endif
744 lp->d_checksum = 0;
745 lp->d_checksum = dkcksum(lp);
746 if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
747 warn("ioctl (WDINFO)");
748 errx(1, "%s: can't rewrite disk label", s);
749 }
750 #if __vax__
751 if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
752 int i;
753 int cfd;
754 daddr_t alt;
755 char specname[64];
756 char blk[1024];
757 char *cp;
758
759 /*
760 * Make name for 'c' partition.
761 */
762 strcpy(specname, s);
763 cp = specname + strlen(specname) - 1;
764 if (!isdigit(*cp))
765 *cp = 'c';
766 cfd = open(specname, O_WRONLY);
767 if (cfd < 0)
768 err(1, "%s: open", specname);
769 memset(blk, 0, sizeof(blk));
770 *(struct disklabel *)(blk + LABELOFFSET) = *lp;
771 alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
772 for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
773 off_t offset;
774
775 offset = alt + i;
776 offset *= lp->d_secsize;
777 if (lseek(cfd, offset, SEEK_SET) == -1)
778 err(1, "lseek to badsector area: ");
779 if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
780 warn("alternate label %d write", i/2);
781 }
782 close(cfd);
783 }
784 #endif
785 }
786
787 static int
788 strsuftoi(const char *desc, const char *arg, int min, int max)
789 {
790 long long result;
791 char *ep;
792
793 errno = 0;
794 result = strtoll(arg, &ep, 10);
795 if (ep[0] != '\0' && ep[1] != '\0')
796 errx(1, "%s `%s' is not a valid number.", desc, arg);
797 switch (tolower((unsigned char)ep[0])) {
798 case '\0':
799 case 'b':
800 break;
801 case 'k':
802 result <<= 10;
803 break;
804 case 'm':
805 result <<= 20;
806 break;
807 case 'g':
808 result <<= 30;
809 break;
810 default:
811 errx(1, "`%s' is not a valid suffix for %s.", ep, desc);
812 }
813 if (result < min)
814 errx(1, "%s `%s' (%lld) is less than minimum (%d).",
815 desc, arg, result, min);
816 if (result > max)
817 errx(1, "%s `%s' (%lld) is greater than maximum (%d).",
818 desc, arg, result, max);
819 return ((int)result);
820 }
821
822 static void
823 usage(void)
824 {
825
826 if (mfs) {
827 fprintf(stderr,
828 "usage: %s [ fsoptions ] special-device mount-point\n",
829 getprogname());
830 } else
831 fprintf(stderr,
832 "usage: %s [ fsoptions ] special-device%s\n",
833 getprogname(),
834 #ifdef COMPAT
835 " [device-type]");
836 #else
837 "");
838 #endif
839 fprintf(stderr, "where fsoptions are:\n");
840 if (!mfs) {
841 fprintf(stderr,
842 "\t-B byteorder\tbyte order (`be' or `le')\n");
843 fprintf(stderr,
844 "\t-F\t\tcreate file system image in regular file\n");
845 }
846 fprintf(stderr, "\t-N\t\tdo not create file system, "
847 "just print out parameters\n");
848 if (!mfs) {
849 fprintf(stderr,
850 "\t-O\t\tcreate a 4.3BSD format filesystem\n");
851 fprintf(stderr,
852 "\t-S secsize\tsector size\n");
853 }
854 #ifdef COMPAT
855 fprintf(stderr, "\t-T disktype\tdisk type\n");
856 #endif
857 if (!mfs)
858 fprintf(stderr,
859 "\t-Z\t\tpre-zero the image file (with -F)\n");
860 fprintf(stderr, "\t-a maxcontig\tmaximum contiguous blocks\n");
861 fprintf(stderr, "\t-b bsize\tblock size\n");
862 fprintf(stderr, "\t-c cpg\t\tcylinders/group\n");
863 fprintf(stderr, "\t-d rotdelay\trotational delay between "
864 "contiguous blocks\n");
865 fprintf(stderr, "\t-e maxbpg\tmaximum blocks per file "
866 "in a cylinder group\n");
867 fprintf(stderr, "\t-f fsize\tfragment size\n");
868 fprintf(stderr, "\t-i density\tnumber of bytes per inode\n");
869 if (!mfs) {
870 fprintf(stderr,
871 "\t-k trackskew\tsector 0 skew, per track\n");
872 fprintf(stderr,
873 "\t-l interleave\thardware sector interleave\n");
874 }
875 fprintf(stderr, "\t-m minfree\tminimum free space %%\n");
876 if (!mfs)
877 fprintf(stderr,
878 "\t-n nrpos\tnumber of distinguished "
879 "rotational positions\n");
880 fprintf(stderr, "\t-o optim\toptimization preference "
881 "(`space' or `time')\n");
882 if (!mfs)
883 fprintf(stderr,
884 "\t-p trackspares\tspare sectors per track\n");
885 fprintf(stderr, "\t-s fssize\tfile system size (sectors)\n");
886 if (!mfs) {
887 fprintf(stderr,
888 "\t-r rpm\t\trevolutions/minute\n");
889 fprintf(stderr,
890 "\t-t ntracks\ttracks/cylinder\n");
891 fprintf(stderr,
892 "\t-u nsectors\tsectors/track\n");
893 fprintf(stderr,
894 "\t-x cylspares\tspare sectors per cylinder\n");
895 }
896 exit(1);
897 }
898