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