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