newfs.c revision 1.70 1 /* $NetBSD: newfs.c,v 1.70 2003/09/11 12:19:45 dsl 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 2002 Networks Associates Technology, Inc.
34 * All rights reserved.
35 *
36 * This software was developed for the FreeBSD Project by Marshall
37 * Kirk McKusick and Network Associates Laboratories, the Security
38 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
39 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
40 * research program
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 */
70
71 #include <sys/cdefs.h>
72 #ifndef lint
73 __COPYRIGHT("@(#) Copyright (c) 1983, 1989, 1993, 1994\n\
74 The Regents of the University of California. All rights reserved.\n");
75 #endif /* not lint */
76
77 #ifndef lint
78 #if 0
79 static char sccsid[] = "@(#)newfs.c 8.13 (Berkeley) 5/1/95";
80 #else
81 __RCSID("$NetBSD: newfs.c,v 1.70 2003/09/11 12:19:45 dsl Exp $");
82 #endif
83 #endif /* not lint */
84
85 /*
86 * newfs: friendly front end to mkfs
87 */
88 #include <sys/param.h>
89 #include <sys/ioctl.h>
90 #include <sys/disklabel.h>
91 #include <sys/file.h>
92 #include <sys/mount.h>
93 #include <sys/sysctl.h>
94 #include <sys/wait.h>
95
96 #include <ufs/ufs/dir.h>
97 #include <ufs/ufs/dinode.h>
98 #include <ufs/ufs/ufsmount.h>
99 #include <ufs/ffs/fs.h>
100
101 #include <ctype.h>
102 #include <disktab.h>
103 #include <err.h>
104 #include <errno.h>
105 #include <grp.h>
106 #include <paths.h>
107 #include <pwd.h>
108 #include <signal.h>
109 #include <stdio.h>
110 #include <stdlib.h>
111 #include <string.h>
112 #include <syslog.h>
113 #include <unistd.h>
114 #include <util.h>
115
116 #include "mntopts.h"
117 #include "dkcksum.h"
118 #include "extern.h"
119
120 struct mntopt mopts[] = {
121 MOPT_STDOPTS,
122 MOPT_ASYNC,
123 MOPT_UPDATE,
124 MOPT_GETARGS,
125 MOPT_NOATIME,
126 { NULL },
127 };
128
129 static struct disklabel *getdisklabel(char *, int);
130 static void rewritelabel(char *, int, struct disklabel *);
131 static gid_t mfs_group(const char *);
132 static uid_t mfs_user(const char *);
133 static int strsuftoi(const char *, const char *, int, int);
134 static void usage(void);
135 int main(int, char *[]);
136
137 #define COMPAT /* allow non-labeled disks */
138
139 /*
140 * The following two constants set the default block and fragment sizes.
141 * Both constants must be a power of 2 and meet the following constraints:
142 * MINBSIZE <= DESBLKSIZE <= MAXBSIZE
143 * sectorsize <= DESFRAGSIZE <= DESBLKSIZE
144 * DESBLKSIZE / DESFRAGSIZE <= 8
145 */
146 /*
147 * For file systems smaller than SMALL_FSSIZE we use the S_DFL_* defaults,
148 * otherwise if less than MEDIUM_FSSIZE use M_DFL_*, otherwise use
149 * L_DFL_*.
150 */
151 #define SMALL_FSSIZE (20*1024*2)
152 #define S_DFL_FRAGSIZE 512
153 #define S_DFL_BLKSIZE 4096
154 #define MEDIUM_FSSIZE (1000*1024*2)
155 #define M_DFL_FRAGSIZE 1024
156 #define M_DFL_BLKSIZE 8192
157 #define L_DFL_FRAGSIZE 2048
158 #define L_DFL_BLKSIZE 16384
159
160 /* Apple requires the fragment size to be at least APPLEUFS_DIRBLKSIZ
161 * but the block size cannot be larger than Darwin's PAGE_SIZE. See
162 * the mount check in Darwin's ffs_mountfs for an explanation.
163 */
164 #define APPLEUFS_DFL_FRAGSIZE APPLEUFS_DIRBLKSIZ /* 1024 */
165 #define APPLEUFS_DFL_BLKSIZE 4096 /* default Darwin PAGE_SIZE */
166
167 /*
168 * Default sector size.
169 */
170 #define DFL_SECSIZE 512
171
172 /*
173 * MAXBLKPG determines the maximum number of data blocks which are
174 * placed in a single cylinder group. The default is one indirect
175 * block worth of data blocks.
176 */
177 #define MAXBLKPG_UFS1(bsize) ((bsize) / sizeof(int32_t))
178 #define MAXBLKPG_UFS2(bsize) ((bsize) / sizeof(int64_t))
179
180 /*
181 * Each file system has a number of inodes statically allocated.
182 * We allocate one inode slot per NFPI fragments, expecting this
183 * to be far more than we will ever need.
184 */
185 #define NFPI 4
186
187
188 int mfs; /* run as the memory based filesystem */
189 int Nflag; /* run without writing file system */
190 int Oflag = 1; /* format as an 4.3BSD file system */
191 int64_t fssize; /* file system size */
192 int sectorsize; /* bytes/sector */
193 int fsize = 0; /* fragment size */
194 int bsize = 0; /* block size */
195 int maxbsize = 0; /* maximum clustering */
196 int minfree = MINFREE; /* free space threshold */
197 int opt = DEFAULTOPT; /* optimization preference (space or time) */
198 int density; /* number of bytes per inode */
199 int num_inodes; /* number of inoder (overrides density) */
200 int maxcontig = 0; /* max contiguous blocks to allocate */
201 int maxbpg; /* maximum blocks per file in a cyl group */
202 int avgfilesize = AVFILESIZ;/* expected average file size */
203 int avgfpdir = AFPDIR; /* expected number of files per directory */
204 int mntflags = MNT_ASYNC; /* flags to be passed to mount */
205 u_long memleft; /* virtual memory available */
206 caddr_t membase; /* start address of memory based filesystem */
207 int needswap; /* Filesystem not in native byte order */
208 #ifdef COMPAT
209 char *disktype;
210 int unlabeled;
211 #endif
212 char *appleufs_volname = 0; /* Apple UFS volume name */
213 int isappleufs = 0;
214
215 char device[MAXPATHLEN];
216
217 int
218 main(int argc, char *argv[])
219 {
220 struct partition *pp;
221 struct disklabel *lp;
222 struct disklabel mfsfakelabel;
223 struct partition oldpartition;
224 struct statfs *mp;
225 int ch, fsi, fso, len, maxpartitions, n, Fflag, Iflag, Zflag;
226 char *cp, *endp, *s1, *s2, *special;
227 const char *opstring;
228 long long llsize;
229 int dfl_fragsize, dfl_blksize;
230 #ifdef MFS
231 char mountfromname[100];
232 pid_t pid, res;
233 struct statfs sf;
234 int status;
235 #endif
236 mode_t mfsmode;
237 uid_t mfsuid;
238 gid_t mfsgid;
239
240 cp = NULL;
241 fsi = fso = -1;
242 Fflag = Iflag = Zflag = 0;
243 if (strstr(getprogname(), "mfs")) {
244 mfs = 1;
245 mfsmode = 01777; /* default mode for a /tmp-type directory */
246 mfsuid = 0; /* user root */
247 mfsgid = 0; /* group wheel */
248 Nflag++;
249 }
250
251 maxpartitions = getmaxpartitions();
252 if (maxpartitions > 26)
253 errx(1, "insane maxpartitions value %d", maxpartitions);
254
255 opstring = mfs ?
256 "NT:a:b:c:d:e:f:g:h:i:m:n:o:p:s:u:" :
257 "B:FINO:S:T:Za:b:c:d:e:f:g:h:i:l:m:n:o:p:r:s:u:v:";
258 while ((ch = getopt(argc, argv, opstring)) != -1)
259 switch (ch) {
260 case 'B':
261 if (strcmp(optarg, "be") == 0) {
262 #if BYTE_ORDER == LITTLE_ENDIAN
263 needswap = 1;
264 #endif
265 } else if (strcmp(optarg, "le") == 0) {
266 #if BYTE_ORDER == BIG_ENDIAN
267 needswap = 1;
268 #endif
269 } else
270 usage();
271 break;
272 case 'F':
273 Fflag = 1;
274 break;
275 case 'I':
276 Iflag = 1;
277 break;
278 case 'N':
279 Nflag = 1;
280 break;
281 case 'O':
282 Oflag = strsuftoi("format", optarg, 0, 2);
283 break;
284 case 'S':
285 sectorsize = strsuftoi("sector size",
286 optarg, 1, INT_MAX);
287 break;
288 #ifdef COMPAT
289 case 'T':
290 disktype = optarg;
291 break;
292 #endif
293 case 'Z':
294 Zflag = 1;
295 break;
296 case 'a':
297 maxcontig = strsuftoi("maximum contiguous blocks",
298 optarg, 1, INT_MAX);
299 break;
300 case 'b':
301 bsize = strsuftoi("block size",
302 optarg, MINBSIZE, MAXBSIZE);
303 break;
304 case 'c': /* was cylinders per group... */
305 break;
306 case 'd':
307 maxbsize = strsuftoi("maximum extent size",
308 optarg, 0, INT_MAX);
309 break;
310 case 'e':
311 maxbpg = strsuftoi(
312 "blocks per file in a cylinder group",
313 optarg, 1, INT_MAX);
314 break;
315 case 'f':
316 fsize = strsuftoi("fragment size",
317 optarg, 1, MAXBSIZE);
318 break;
319 case 'g':
320 if (mfs)
321 mfsgid = mfs_group(optarg);
322 else {
323 avgfilesize = strsuftoi("average file size",
324 optarg, 1, INT_MAX);
325 }
326 break;
327 case 'h':
328 avgfpdir = strsuftoi("expected files per directory",
329 optarg, 1, INT_MAX);
330 break;
331 case 'i':
332 density = strsuftoi("bytes per inode",
333 optarg, 1, INT_MAX);
334 break;
335 case 'm':
336 minfree = strsuftoi("free space %",
337 optarg, 0, 99);
338 break;
339 case 'n':
340 num_inodes = strsuftoi("number of inodes",
341 optarg, 1, INT_MAX);
342 break;
343 case 'o':
344 if (mfs)
345 getmntopts(optarg, mopts, &mntflags, 0);
346 else {
347 if (strcmp(optarg, "space") == 0)
348 opt = FS_OPTSPACE;
349 else if (strcmp(optarg, "time") == 0)
350 opt = FS_OPTTIME;
351 else
352 errx(1, "%s %s",
353 "unknown optimization preference: ",
354 "use `space' or `time'.");
355 }
356 break;
357 case 'p':
358 if (mfs) {
359 if ((mfsmode = strtol(optarg, NULL, 8)) <= 0)
360 errx(1, "bad mode `%s'", optarg);
361 } else
362 errx(1, "unknown option 'p'");
363 break;
364 case 's':
365 llsize = strtoll(optarg, &endp, 10);
366 if (endp[0] != '\0' && endp[1] != '\0')
367 llsize = -1;
368 else {
369 int ssiz;
370
371 ssiz = (sectorsize ? sectorsize : DFL_SECSIZE);
372 switch (tolower((unsigned char)endp[0])) {
373 case 'b':
374 llsize /= ssiz;
375 break;
376 case 'k':
377 llsize *= 1024 / ssiz;
378 break;
379 case 'm':
380 llsize *= 1024 * 1024 / ssiz;
381 break;
382 case 'g':
383 llsize *= 1024 * 1024 * 1024 / ssiz;
384 break;
385 case '\0':
386 case 's':
387 break;
388 default:
389 llsize = -1;
390 }
391 }
392 if (llsize > LLONG_MAX)
393 errx(1, "file system size `%s' is too large.",
394 optarg);
395 if (llsize <= 0)
396 errx(1,
397 "`%s' is not a valid number for file system size.",
398 optarg);
399 fssize = llsize;
400 break;
401 case 'u':
402 if (mfs)
403 mfsuid = mfs_user(optarg);
404 else
405 errx(1, "deprecated option 'u'");
406 break;
407 case 'v':
408 appleufs_volname = optarg;
409 if (strchr(appleufs_volname, ':') || strchr(appleufs_volname, '/'))
410 errx(1,"Apple UFS volume name cannot contain ':' or '/'");
411 if (appleufs_volname[0] == '\0')
412 errx(1,"Apple UFS volume name cannot be zero length");
413 isappleufs = 1;
414 break;
415 case '?':
416 default:
417 usage();
418 }
419 argc -= optind;
420 argv += optind;
421 if (mntflags & MNT_GETARGS)
422 goto doit;
423
424 if (argc != 2 && (mfs || argc != 1))
425 usage();
426
427 special = argv[0];
428 if (Fflag || mfs) {
429 /*
430 * it's a file system image or an MFS, so fake up a label.
431 * XXX
432 */
433 if (!sectorsize)
434 sectorsize = DFL_SECSIZE;
435
436 if (Fflag && !Nflag) { /* creating image in a regular file */
437 if (fssize == 0)
438 errx(1, "need to specify size when using -F");
439 fso = open(special, O_RDWR | O_CREAT, 0777);
440 if (fso == -1)
441 err(1, "can't open file %s", special);
442 if ((fsi = dup(fso)) == -1)
443 err(1, "can't dup(2) image fd");
444 /* XXXLUKEM: only ftruncate() regular files ? */
445 if (ftruncate(fso, (off_t)fssize * sectorsize) == -1)
446 err(1, "can't resize %s to %lld",
447 special, (long long)fssize);
448
449 if (Zflag) { /* pre-zero (and de-sparce) the file */
450 char *buf;
451 int bufsize, i;
452 off_t bufrem;
453 struct statfs sfs;
454
455 if (fstatfs(fso, &sfs) == -1) {
456 warn("can't fstatfs `%s'", special);
457 bufsize = 8192;
458 } else
459 bufsize = sfs.f_iosize;
460
461 if ((buf = calloc(1, bufsize)) == NULL)
462 err(1, "can't malloc buffer of %d",
463 bufsize);
464 bufrem = fssize * sectorsize;
465 printf(
466 "Creating file system image in `%s', size %lld bytes, in %d byte chunks.\n",
467 special, (long long)bufrem, bufsize);
468 while (bufrem > 0) {
469 i = write(fso, buf,
470 MIN(bufsize, bufrem));
471 if (i == -1)
472 err(1, "writing image");
473 bufrem -= i;
474 }
475 free(buf);
476 }
477
478 }
479
480 memset(&mfsfakelabel, 0, sizeof(mfsfakelabel));
481 mfsfakelabel.d_secsize = sectorsize;
482 mfsfakelabel.d_nsectors = 64; /* these 3 add up to 16MB */
483 mfsfakelabel.d_ntracks = 16;
484 mfsfakelabel.d_ncylinders = 16;
485 mfsfakelabel.d_secpercyl =
486 mfsfakelabel.d_nsectors * mfsfakelabel.d_ntracks;
487 mfsfakelabel.d_secperunit =
488 mfsfakelabel.d_ncylinders * mfsfakelabel.d_secpercyl;
489 mfsfakelabel.d_rpm = 10000;
490 mfsfakelabel.d_interleave = 1;
491 mfsfakelabel.d_npartitions = 1;
492 mfsfakelabel.d_partitions[0].p_size = mfsfakelabel.d_secperunit;
493 mfsfakelabel.d_partitions[0].p_fsize = 1024;
494 mfsfakelabel.d_partitions[0].p_frag = 8;
495 mfsfakelabel.d_partitions[0].p_cpg = 16;
496
497 lp = &mfsfakelabel;
498 pp = &mfsfakelabel.d_partitions[0];
499 } else { /* !Fflag && !mfs */
500 fsi = opendisk(special, O_RDONLY, device, sizeof(device), 0);
501 special = device;
502 if (fsi < 0)
503 err(1, "%s: open for read", special);
504
505 if (Nflag) {
506 fso = -1;
507 } else {
508 fso = open(special, O_WRONLY);
509 if (fso < 0)
510 err(1, "%s: open for write", special);
511
512 /* Bail if target special is mounted */
513 n = getmntinfo(&mp, MNT_NOWAIT);
514 if (n == 0)
515 err(1, "%s: getmntinfo", special);
516
517 len = sizeof(_PATH_DEV) - 1;
518 s1 = special;
519 if (strncmp(_PATH_DEV, s1, len) == 0)
520 s1 += len;
521
522 while (--n >= 0) {
523 s2 = mp->f_mntfromname;
524 if (strncmp(_PATH_DEV, s2, len) == 0) {
525 s2 += len - 1;
526 *s2 = 'r';
527 }
528 if (strcmp(s1, s2) == 0 ||
529 strcmp(s1, &s2[1]) == 0)
530 errx(1, "%s is mounted on %s",
531 special, mp->f_mntonname);
532 ++mp;
533 }
534 }
535 cp = strchr(argv[0], '\0') - 1;
536 if (cp == 0 || ((*cp < 'a' || *cp > ('a' + maxpartitions - 1))
537 && !isdigit(*cp)))
538 errx(1, "can't figure out file system partition");
539 #ifdef COMPAT
540 if (disktype == NULL)
541 disktype = argv[1];
542 #endif
543 lp = getdisklabel(special, fsi);
544 if (isdigit(*cp))
545 pp = &lp->d_partitions[0];
546 else
547 pp = &lp->d_partitions[*cp - 'a'];
548 if (pp->p_size == 0)
549 errx(1, "`%c' partition is unavailable", *cp);
550 if (pp->p_fstype == FS_APPLEUFS)
551 isappleufs = 1;
552 if (isappleufs) {
553 if (!Iflag && (pp->p_fstype != FS_APPLEUFS))
554 errx(1, "`%c' partition type is not `Apple UFS'", *cp);
555 } else {
556 if (!Iflag && (pp->p_fstype != FS_BSDFFS))
557 errx(1, "`%c' partition type is not `4.2BSD'", *cp);
558 }
559 } /* !Fflag && !mfs */
560
561 if (fssize == 0)
562 fssize = pp->p_size;
563 if (fssize > pp->p_size && !mfs && !Fflag)
564 errx(1, "maximum file system size on the `%c' partition is %d",
565 *cp, pp->p_size);
566 if (sectorsize == 0) {
567 sectorsize = lp->d_secsize;
568 if (sectorsize <= 0)
569 errx(1, "no default sector size");
570 }
571
572 if (isappleufs) {
573 dfl_fragsize = APPLEUFS_DFL_FRAGSIZE;
574 dfl_blksize = APPLEUFS_DFL_BLKSIZE;
575 } else 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 if (isappleufs && (fsize < APPLEUFS_DFL_FRAGSIZE)) {
598 warnx("Warning: chosen fsize of %d is less than Apple UFS minimum of %d",
599 fsize,APPLEUFS_DFL_FRAGSIZE);
600 }
601 if (isappleufs && (bsize > APPLEUFS_DFL_BLKSIZE)) {
602 warnx("Warning: chosen bsize of %d is greater than Apple UFS maximum of %d",
603 bsize,APPLEUFS_DFL_BLKSIZE);
604 }
605
606 /*
607 * Maxcontig sets the default for the maximum number of blocks
608 * that may be allocated sequentially. With filesystem clustering
609 * it is possible to allocate contiguous blocks up to the maximum
610 * transfer size permitted by the controller or buffering.
611 */
612 if (maxcontig == 0)
613 maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize);
614 if (density == 0)
615 density = NFPI * fsize;
616 if (minfree < MINFREE && opt != FS_OPTSPACE) {
617 warnx("%s %s %d%%", "Warning: changing optimization to space",
618 "because minfree is less than", MINFREE);
619 opt = FS_OPTSPACE;
620 }
621 if (maxbpg == 0) {
622 if (Oflag <= 1)
623 maxbpg = MAXBLKPG_UFS1(bsize);
624 else
625 maxbpg = MAXBLKPG_UFS2(bsize);
626 }
627 oldpartition = *pp;
628 mkfs(pp, special, fsi, fso, mfsmode, mfsuid, mfsgid);
629 if (!Nflag && memcmp(pp, &oldpartition, sizeof(oldpartition)) && !Fflag)
630 rewritelabel(special, fso, lp);
631 if (!Nflag)
632 close(fso);
633 close(fsi);
634 #ifdef MFS
635 if (mfs) {
636 struct mfs_args args;
637
638 switch (pid = fork()) {
639 case -1:
640 perror("mfs");
641 exit(10);
642 case 0:
643 (void)snprintf(mountfromname, sizeof(mountfromname),
644 "mfs:%d", getpid());
645 break;
646 default:
647 (void)snprintf(mountfromname, sizeof(mountfromname),
648 "mfs:%d", pid);
649 for (;;) {
650 /*
651 * spin until the mount succeeds
652 * or the child exits
653 */
654 usleep(1);
655
656 /*
657 * XXX Here is a race condition: another process
658 * can mount a filesystem which hides our
659 * ramdisk before we see the success.
660 */
661 if (statfs(argv[1], &sf) < 0)
662 err(88, "statfs %s", argv[1]);
663 if (!strcmp(sf.f_mntfromname, mountfromname) &&
664 !strncmp(sf.f_mntonname, argv[1],
665 MNAMELEN) &&
666 !strcmp(sf.f_fstypename, "mfs"))
667 exit(0);
668
669 res = waitpid(pid, &status, WNOHANG);
670 if (res == -1)
671 err(11, "waitpid");
672 if (res != pid)
673 continue;
674 if (WIFEXITED(status)) {
675 if (WEXITSTATUS(status) == 0)
676 exit(0);
677 errx(1, "%s: mount: %s", argv[1],
678 strerror(WEXITSTATUS(status)));
679 } else
680 errx(11, "abnormal termination");
681 }
682 /* NOTREACHED */
683 }
684
685 (void) setsid();
686 (void) close(0);
687 (void) close(1);
688 (void) close(2);
689 (void) chdir("/");
690
691 args.fspec = mountfromname;
692 args.export.ex_root = -2;
693 if (mntflags & MNT_RDONLY)
694 args.export.ex_flags = MNT_EXRDONLY;
695 else
696 args.export.ex_flags = 0;
697 args.base = membase;
698 args.size = fssize * sectorsize;
699 doit:
700 if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0) {
701 if (mntflags & MNT_GETARGS)
702 err(1, "mount `%s' failed", argv[1]);
703 exit(errno); /* parent prints message */
704 }
705 if (mntflags & MNT_GETARGS)
706 printf("base=%p, size=%ld\n", args.base, args.size);
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 if (errno == ESRCH)
754 return;
755 warn("ioctl (WDINFO)");
756 errx(1, "%s: can't rewrite disk label", s);
757 }
758 #if __vax__
759 if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
760 int i;
761 int cfd;
762 daddr_t alt;
763 off_t loff;
764 char specname[64];
765 char blk[1024];
766 char *cp;
767
768 /*
769 * Make name for 'c' partition.
770 */
771 strlcpy(specname, s, sizeof(specname));
772 cp = specname + strlen(specname) - 1;
773 if (!isdigit(*cp))
774 *cp = 'c';
775 cfd = open(specname, O_WRONLY);
776 if (cfd < 0)
777 err(1, "%s: open", specname);
778 if ((loff = getlabeloffset()) < 0)
779 err(1, "getlabeloffset()");
780 memset(blk, 0, sizeof(blk));
781 *(struct disklabel *)(blk + loff) = *lp;
782 alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
783 for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
784 off_t offset;
785
786 offset = alt + i;
787 offset *= lp->d_secsize;
788 if (lseek(cfd, offset, SEEK_SET) == -1)
789 err(1, "lseek to badsector area: ");
790 if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
791 warn("alternate label %d write", i/2);
792 }
793 close(cfd);
794 }
795 #endif
796 }
797
798 static gid_t
799 mfs_group(const char *gname)
800 {
801 struct group *gp;
802
803 if (!(gp = getgrnam(gname)) && !isdigit((unsigned char)*gname))
804 errx(1, "unknown gname %s", gname);
805 return gp ? gp->gr_gid : atoi(gname);
806 }
807
808 static uid_t
809 mfs_user(const char *uname)
810 {
811 struct passwd *pp;
812
813 if (!(pp = getpwnam(uname)) && !isdigit((unsigned char)*uname))
814 errx(1, "unknown user %s", uname);
815 return pp ? pp->pw_uid : atoi(uname);
816 }
817
818 static int
819 strsuftoi(const char *desc, const char *arg, int min, int max)
820 {
821 long long result;
822 char *ep;
823
824 errno = 0;
825 result = strtoll(arg, &ep, 10);
826 if (ep[0] != '\0' && ep[1] != '\0')
827 errx(1, "%s `%s' is not a valid number.", desc, arg);
828 switch (tolower((unsigned char)ep[0])) {
829 case '\0':
830 case 'b':
831 break;
832 case 'k':
833 result <<= 10;
834 break;
835 case 'm':
836 result <<= 20;
837 break;
838 case 'g':
839 result <<= 30;
840 break;
841 default:
842 errx(1, "`%s' is not a valid suffix for %s.", ep, desc);
843 }
844 if (result < min)
845 errx(1, "%s `%s' (%lld) is less than minimum (%d).",
846 desc, arg, result, min);
847 if (result > max)
848 errx(1, "%s `%s' (%lld) is greater than maximum (%d).",
849 desc, arg, result, max);
850 return ((int)result);
851 }
852
853 #define NEWFS 1
854 #define MFS_MOUNT 2
855 #define BOTH NEWFS | MFS_MOUNT
856
857 struct help_strings {
858 int flags;
859 const char *str;
860 } const help_strings[] = {
861 { NEWFS, "-B byteorder\tbyte order (`be' or `le')" },
862 { NEWFS, "-F \t\tcreate file system image in regular file" },
863 { NEWFS, "-I \t\tdo not check that the file system type is '4.2BSD'" },
864 { BOTH, "-N \t\tdo not create file system, just print out "
865 "parameters" },
866 { NEWFS, "-O N\t\tfilesystem format: 0 ==> 4.3BSD, 1 ==> FFS, 2==> UFS2" },
867 { NEWFS, "-S secsize\tsector size" },
868 #ifdef COMPAT
869 { NEWFS, "-T disktype\tdisk type" },
870 #endif
871 { NEWFS, "-Z \t\tpre-zero the image file (with -F)" },
872 { BOTH, "-a maxcontig\tmaximum contiguous blocks" },
873 { BOTH, "-b bsize\tblock size" },
874 { BOTH, "-c blocks\tblocks per cylinder group" },
875 { BOTH, "-d maxbsize\t maximum extent size" },
876 { BOTH, "-e maxbpg\tmaximum blocks per file in a cylinder group"
877 },
878 { BOTH, "-f fsize\tfrag size" },
879 { NEWFS, "-g avgfilesize\taverage file size" },
880 { MFS_MOUNT, "-g groupname\tgroup name of mount point" },
881 { BOTH, "-h avgfpdir\taverage files per directory" },
882 { BOTH, "-i density\tnumber of bytes per inode" },
883 { BOTH, "-m minfree\tminimum free space %%" },
884 { BOTH, "-n inodes\tnumder of inodes" },
885 { BOTH, "-o optim\toptimization preference (`space' or `time')"
886 },
887 { MFS_MOUNT, "-p perm\t\tpermissions (in octal)" },
888 { BOTH, "-s fssize\tfile system size (sectors)" },
889 { MFS_MOUNT, "-u username\tuser name of mount point" },
890 { NEWFS, "-v volname\tApple UFS volume name" },
891 { 0, NULL }
892 };
893
894 static void
895 usage(void)
896 {
897 int match;
898 const struct help_strings *hs;
899
900 if (mfs) {
901 fprintf(stderr,
902 "usage: %s [ fsoptions ] special-device mount-point\n",
903 getprogname());
904 } else
905 fprintf(stderr,
906 "usage: %s [ fsoptions ] special-device%s\n",
907 getprogname(),
908 #ifdef COMPAT
909 " [device-type]");
910 #else
911 "");
912 #endif
913 fprintf(stderr, "where fsoptions are:\n");
914
915 match = mfs ? MFS_MOUNT : NEWFS;
916 for (hs = help_strings; hs->flags != 0; hs++)
917 if (hs->flags & match)
918 fprintf(stderr, "\t%s\n", hs->str);
919 exit(1);
920 }
921