newfs.c revision 1.44 1 /* $NetBSD: newfs.c,v 1.44 2001/07/30 07:45:08 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.44 2001/07/30 07:45:08 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 i;
420
421 if ((buf = calloc(1, sectorsize)) == NULL)
422 err(1, "can't malloc buffer of %d",
423 sectorsize);
424 printf(
425 "Creating file system image in `%s', size %lld bytes.\n",
426 special, (long long)(fssize * sectorsize));
427 for (i = 0; i < fssize; i++) {
428 if (write(fso, buf, sectorsize) !=
429 sectorsize)
430 err(1,
431 "writing image sector %d",
432 i);
433 }
434 }
435
436 }
437
438 memset(&mfsfakelabel, 0, sizeof(mfsfakelabel));
439 mfsfakelabel.d_secsize = sectorsize;
440 mfsfakelabel.d_nsectors = 64; /* these 3 add up to 16MB */
441 mfsfakelabel.d_ntracks = 16;
442 mfsfakelabel.d_ncylinders = 16;
443 mfsfakelabel.d_secpercyl =
444 mfsfakelabel.d_nsectors * mfsfakelabel.d_ntracks;
445 mfsfakelabel.d_secperunit =
446 mfsfakelabel.d_ncylinders * mfsfakelabel.d_secpercyl;
447 mfsfakelabel.d_rpm = 3600;
448 mfsfakelabel.d_interleave = 1;
449 mfsfakelabel.d_npartitions = 1;
450 mfsfakelabel.d_partitions[0].p_size = mfsfakelabel.d_secperunit;
451 mfsfakelabel.d_partitions[0].p_fsize = 1024;
452 mfsfakelabel.d_partitions[0].p_frag = 8;
453 mfsfakelabel.d_partitions[0].p_cpg = 16;
454
455 lp = &mfsfakelabel;
456 pp = &mfsfakelabel.d_partitions[0];
457
458 goto havelabel;
459 }
460 cp = strrchr(special, '/');
461 if (cp == NULL) {
462 /*
463 * No path prefix; try /dev/r%s then /dev/%s.
464 */
465 (void)snprintf(device, sizeof(device), "%sr%s",
466 _PATH_DEV, special);
467 if (stat(device, &st) == -1)
468 (void)snprintf(device, sizeof(device), "%s%s",
469 _PATH_DEV, special);
470 special = device;
471 }
472 if (Nflag) {
473 fso = -1;
474 } else {
475 fso = open(special, O_WRONLY);
476 if (fso < 0)
477 err(1, "%s: open", special);
478
479 /* Bail if target special is mounted */
480 n = getmntinfo(&mp, MNT_NOWAIT);
481 if (n == 0)
482 err(1, "%s: getmntinfo", special);
483
484 len = sizeof(_PATH_DEV) - 1;
485 s1 = special;
486 if (strncmp(_PATH_DEV, s1, len) == 0)
487 s1 += len;
488
489 while (--n >= 0) {
490 s2 = mp->f_mntfromname;
491 if (strncmp(_PATH_DEV, s2, len) == 0) {
492 s2 += len - 1;
493 *s2 = 'r';
494 }
495 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
496 errx(1, "%s is mounted on %s",
497 special, mp->f_mntonname);
498 ++mp;
499 }
500 }
501 if (mfs && disktype != NULL) {
502 lp = (struct disklabel *)getdiskbyname(disktype);
503 if (lp == NULL)
504 errx(1, "%s: unknown disk type", disktype);
505 pp = &lp->d_partitions[1];
506 } else {
507 fsi = open(special, O_RDONLY);
508 if (fsi < 0)
509 err(1, "%s: open", special);
510 if (fstat(fsi, &st) < 0)
511 err(1, "%s: fstat", special);
512 if (!S_ISCHR(st.st_mode) && !mfs)
513 warnx("%s: not a character-special device", special);
514 cp = strchr(argv[0], '\0') - 1;
515 if (cp == 0 || ((*cp < 'a' || *cp > ('a' + maxpartitions - 1))
516 && !isdigit(*cp)))
517 errx(1, "can't figure out file system partition");
518 #ifdef COMPAT
519 if (!mfs && disktype == NULL)
520 disktype = argv[1];
521 #endif
522 lp = getdisklabel(special, fsi);
523 if (isdigit(*cp))
524 pp = &lp->d_partitions[0];
525 else
526 pp = &lp->d_partitions[*cp - 'a'];
527 if (pp->p_size == 0)
528 errx(1, "`%c' partition is unavailable", *cp);
529 if (pp->p_fstype == FS_BOOT)
530 errx(1, "`%c' partition overlaps boot program", *cp);
531 }
532
533 havelabel:
534 if (fssize == 0)
535 fssize = pp->p_size;
536 if (fssize > pp->p_size && !mfs && !Fflag)
537 errx(1, "maximum file system size on the `%c' partition is %d",
538 *cp, pp->p_size);
539 if (rpm == 0) {
540 rpm = lp->d_rpm;
541 if (rpm <= 0)
542 rpm = 3600;
543 }
544 if (ntracks == 0) {
545 ntracks = lp->d_ntracks;
546 if (ntracks <= 0)
547 errx(1, "no default #tracks");
548 }
549 if (nsectors == 0) {
550 nsectors = lp->d_nsectors;
551 if (nsectors <= 0)
552 errx(1, "no default #sectors/track");
553 }
554 if (sectorsize == 0) {
555 sectorsize = lp->d_secsize;
556 if (sectorsize <= 0)
557 errx(1, "no default sector size");
558 }
559 if (trackskew == -1) {
560 trackskew = lp->d_trackskew;
561 if (trackskew < 0)
562 trackskew = 0;
563 }
564 if (interleave == 0) {
565 interleave = lp->d_interleave;
566 if (interleave <= 0)
567 interleave = 1;
568 }
569 if (fsize == 0) {
570 fsize = pp->p_fsize;
571 if (fsize <= 0)
572 fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
573 }
574 if (bsize == 0) {
575 bsize = pp->p_frag * pp->p_fsize;
576 if (bsize <= 0)
577 bsize = MIN(DFL_BLKSIZE, 8 * fsize);
578 }
579 if (cpgflg == 0) {
580 if (pp->p_cpg != 0)
581 cpg = pp->p_cpg;
582 }
583 /*
584 * Maxcontig sets the default for the maximum number of blocks
585 * that may be allocated sequentially. With filesystem clustering
586 * it is possible to allocate contiguous blocks up to the maximum
587 * transfer size permitted by the controller or buffering.
588 */
589 if (maxcontig == 0)
590 maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize);
591 if (density == 0)
592 density = NFPI * fsize;
593 if (minfree < MINFREE && opt != FS_OPTSPACE) {
594 warnx("%s %s %d%%", "Warning: changing optimization to space",
595 "because minfree is less than", MINFREE);
596 opt = FS_OPTSPACE;
597 }
598 if (trackspares == -1) {
599 trackspares = lp->d_sparespertrack;
600 if (trackspares < 0)
601 trackspares = 0;
602 }
603 nphyssectors = nsectors + trackspares;
604 if (cylspares == -1) {
605 cylspares = lp->d_sparespercyl;
606 if (cylspares < 0)
607 cylspares = 0;
608 }
609 secpercyl = nsectors * ntracks - cylspares;
610 if (secpercyl != lp->d_secpercyl)
611 warnx("%s (%d) %s (%u)\n",
612 "Warning: calculated sectors per cylinder", secpercyl,
613 "disagrees with disk label", lp->d_secpercyl);
614 if (maxbpg == 0)
615 maxbpg = MAXBLKPG(bsize);
616 headswitch = lp->d_headswitch;
617 trackseek = lp->d_trkseek;
618 #ifdef notdef /* label may be 0 if faked up by kernel */
619 bbsize = lp->d_bbsize;
620 sbsize = lp->d_sbsize;
621 #endif
622 oldpartition = *pp;
623 mkfs(pp, special, fsi, fso);
624 if (!Nflag && memcmp(pp, &oldpartition, sizeof(oldpartition)) && !Fflag)
625 rewritelabel(special, fso, lp);
626 if (!Nflag)
627 close(fso);
628 close(fsi);
629 #ifdef MFS
630 if (mfs) {
631 struct mfs_args args;
632
633 switch (pid = fork()) {
634 case -1:
635 perror("mfs");
636 exit(10);
637 case 0:
638 (void)snprintf(mountfromname, sizeof(mountfromname),
639 "mfs:%d", getpid());
640 break;
641 default:
642 (void)snprintf(mountfromname, sizeof(mountfromname),
643 "mfs:%d", pid);
644 for (;;) {
645 /*
646 * spin until the mount succeeds
647 * or the child exits
648 */
649 usleep(1);
650
651 /*
652 * XXX Here is a race condition: another process
653 * can mount a filesystem which hides our
654 * ramdisk before we see the success.
655 */
656 if (statfs(argv[1], &sf) < 0)
657 err(88, "statfs %s", argv[1]);
658 if (!strcmp(sf.f_mntfromname, mountfromname) &&
659 !strncmp(sf.f_mntonname, argv[1],
660 MNAMELEN) &&
661 !strcmp(sf.f_fstypename, "mfs"))
662 exit(0);
663
664 res = waitpid(pid, &status, WNOHANG);
665 if (res == -1)
666 err(11, "waitpid");
667 if (res != pid)
668 continue;
669 if (WIFEXITED(status)) {
670 if (WEXITSTATUS(status) == 0)
671 exit(0);
672 errx(1, "%s: mount: %s", argv[1],
673 strerror(WEXITSTATUS(status)));
674 } else
675 errx(11, "abnormal termination");
676 }
677 /* NOTREACHED */
678 }
679
680 (void) setsid();
681 (void) close(0);
682 (void) close(1);
683 (void) close(2);
684 (void) chdir("/");
685
686 args.fspec = mountfromname;
687 args.export.ex_root = -2;
688 if (mntflags & MNT_RDONLY)
689 args.export.ex_flags = MNT_EXRDONLY;
690 else
691 args.export.ex_flags = 0;
692 args.base = membase;
693 args.size = fssize * sectorsize;
694 if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0)
695 exit(errno); /* parent prints message */
696 }
697 #endif
698 exit(0);
699 }
700
701 #ifdef COMPAT
702 const char lmsg[] = "%s: can't read disk label; disk type must be specified";
703 #else
704 const char lmsg[] = "%s: can't read disk label";
705 #endif
706
707 static struct disklabel *
708 getdisklabel(char *s, volatile int fd)
709 /* XXX why is fs volatile?! */
710 {
711 static struct disklabel lab;
712
713 if (ioctl(fd, DIOCGDINFO, &lab) < 0) {
714 #ifdef COMPAT
715 if (disktype) {
716 struct disklabel *lp;
717
718 unlabeled++;
719 lp = getdiskbyname(disktype);
720 if (lp == NULL)
721 errx(1, "%s: unknown disk type", disktype);
722 return (lp);
723 }
724 #endif
725 warn("ioctl (GDINFO)");
726 errx(1, lmsg, s);
727 }
728 return (&lab);
729 }
730
731 static void
732 rewritelabel(char *s, volatile int fd, struct disklabel *lp)
733 /* XXX why is fd volatile?! */
734 {
735 #ifdef COMPAT
736 if (unlabeled)
737 return;
738 #endif
739 lp->d_checksum = 0;
740 lp->d_checksum = dkcksum(lp);
741 if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
742 warn("ioctl (WDINFO)");
743 errx(1, "%s: can't rewrite disk label", s);
744 }
745 #if __vax__
746 if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
747 int i;
748 int cfd;
749 daddr_t alt;
750 char specname[64];
751 char blk[1024];
752 char *cp;
753
754 /*
755 * Make name for 'c' partition.
756 */
757 strcpy(specname, s);
758 cp = specname + strlen(specname) - 1;
759 if (!isdigit(*cp))
760 *cp = 'c';
761 cfd = open(specname, O_WRONLY);
762 if (cfd < 0)
763 err(1, "%s: open", specname);
764 memset(blk, 0, sizeof(blk));
765 *(struct disklabel *)(blk + LABELOFFSET) = *lp;
766 alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
767 for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
768 off_t offset;
769
770 offset = alt + i;
771 offset *= lp->d_secsize;
772 if (lseek(cfd, offset, SEEK_SET) == -1)
773 err(1, "lseek to badsector area: ");
774 if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
775 warn("alternate label %d write", i/2);
776 }
777 close(cfd);
778 }
779 #endif
780 }
781
782 static int
783 strsuftoi(const char *desc, const char *arg, int min, int max)
784 {
785 long long result;
786 char *ep;
787
788 errno = 0;
789 result = strtoll(arg, &ep, 10);
790 if (ep[0] != '\0' && ep[1] != '\0')
791 errx(1, "%s `%s' is not a valid number.", desc, arg);
792 switch (tolower((unsigned char)ep[0])) {
793 case '\0':
794 case 'b':
795 break;
796 case 'k':
797 result <<= 10;
798 break;
799 case 'm':
800 result <<= 20;
801 break;
802 case 'g':
803 result <<= 30;
804 break;
805 default:
806 errx(1, "`%s' is not a valid suffix for %s.", ep, desc);
807 }
808 if (result < min)
809 errx(1, "%s `%s' (%lld) is less than minimum (%d).",
810 desc, arg, result, min);
811 if (result > max)
812 errx(1, "%s `%s' (%lld) is greater than maximum (%d).",
813 desc, arg, result, max);
814 return ((int)result);
815 }
816
817 static void
818 usage(void)
819 {
820
821 if (mfs) {
822 fprintf(stderr,
823 "usage: %s [ fsoptions ] special-device mount-point\n",
824 getprogname());
825 } else
826 fprintf(stderr,
827 "usage: %s [ fsoptions ] special-device%s\n",
828 getprogname(),
829 #ifdef COMPAT
830 " [device-type]");
831 #else
832 "");
833 #endif
834 fprintf(stderr, "where fsoptions are:\n");
835 if (!mfs) {
836 fprintf(stderr,
837 "\t-B byteorder\tbyte order (`be' or `le')\n");
838 fprintf(stderr,
839 "\t-F\t\tcreate file system image in regular file\n");
840 }
841 fprintf(stderr, "\t-N\t\tdo not create file system, "
842 "just print out parameters\n");
843 if (!mfs) {
844 fprintf(stderr,
845 "\t-O\t\tcreate a 4.3BSD format filesystem\n");
846 fprintf(stderr,
847 "\t-S secsize\tsector size\n");
848 }
849 #ifdef COMPAT
850 fprintf(stderr, "\t-T disktype\tdisk type\n");
851 #endif
852 if (!mfs)
853 fprintf(stderr,
854 "\t-Z\t\tpre-zero the image file (with -F)\n");
855 fprintf(stderr, "\t-a maxcontig\tmaximum contiguous blocks\n");
856 fprintf(stderr, "\t-b bsize\tblock size\n");
857 fprintf(stderr, "\t-c cpg\t\tcylinders/group\n");
858 fprintf(stderr, "\t-d rotdelay\trotational delay between "
859 "contiguous blocks\n");
860 fprintf(stderr, "\t-e maxbpg\tmaximum blocks per file "
861 "in a cylinder group\n");
862 fprintf(stderr, "\t-f fsize\tfragment size\n");
863 fprintf(stderr, "\t-i density\tnumber of bytes per inode\n");
864 if (!mfs) {
865 fprintf(stderr,
866 "\t-k trackskew\tsector 0 skew, per track\n");
867 fprintf(stderr,
868 "\t-l interleave\thardware sector interleave\n");
869 }
870 fprintf(stderr, "\t-m minfree\tminimum free space %%\n");
871 if (!mfs)
872 fprintf(stderr,
873 "\t-n nrpos\tnumber of distinguished "
874 "rotational positions\n");
875 fprintf(stderr, "\t-o optim\toptimization preference "
876 "(`space' or `time')\n");
877 if (!mfs)
878 fprintf(stderr,
879 "\t-p trackspares\tspare sectors per track\n");
880 fprintf(stderr, "\t-s fssize\tfile system size (sectors)\n");
881 if (!mfs) {
882 fprintf(stderr,
883 "\t-r rpm\t\trevolutions/minute\n");
884 fprintf(stderr,
885 "\t-t ntracks\ttracks/cylinder\n");
886 fprintf(stderr,
887 "\t-u nsectors\tsectors/track\n");
888 fprintf(stderr,
889 "\t-x cylspares\tspare sectors per cylinder\n");
890 }
891 exit(1);
892 }
893