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