newfs.c revision 1.96 1 /* $NetBSD: newfs.c,v 1.96 2006/11/25 18:18:22 christos 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.96 2006/11/25 18:18:22 christos 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/disk.h>
92 #include <sys/file.h>
93 #include <sys/mount.h>
94 #include <sys/sysctl.h>
95 #include <sys/wait.h>
96
97 #include <ufs/ufs/dir.h>
98 #include <ufs/ufs/dinode.h>
99 #include <ufs/ufs/ufsmount.h>
100 #include <ufs/ffs/fs.h>
101
102 #include <ctype.h>
103 #include <disktab.h>
104 #include <err.h>
105 #include <errno.h>
106 #include <grp.h>
107 #include <limits.h>
108 #include <paths.h>
109 #include <pwd.h>
110 #include <signal.h>
111 #include <stdint.h>
112 #include <stdio.h>
113 #include <stdlib.h>
114 #include <string.h>
115 #include <syslog.h>
116 #include <unistd.h>
117 #include <util.h>
118 #include <mntopts.h>
119
120 #include "dkcksum.h"
121 #include "extern.h"
122 #include "partutil.h"
123
124 struct mntopt mopts[] = {
125 MOPT_STDOPTS,
126 MOPT_ASYNC,
127 MOPT_UPDATE,
128 MOPT_GETARGS,
129 MOPT_NOATIME,
130 { .m_option = NULL },
131 };
132
133 static gid_t mfs_group(const char *);
134 static uid_t mfs_user(const char *);
135 static int64_t strsuftoi64(const char *, const char *, int64_t, int64_t, int *);
136 static void usage(void) __attribute__((__noreturn__));
137
138 #define COMPAT /* allow non-labeled disks */
139
140 #ifdef COMPAT
141 const char lmsg[] = "%s: can't read disk label; disk type must be specified";
142 #else
143 const char lmsg[] = "%s: can't read disk label";
144 #endif
145
146 /*
147 * The following two constants set the default block and fragment sizes.
148 * Both constants must be a power of 2 and meet the following constraints:
149 * MINBSIZE <= DESBLKSIZE <= MAXBSIZE
150 * sectorsize <= DESFRAGSIZE <= DESBLKSIZE
151 * DESBLKSIZE / DESFRAGSIZE <= 8
152 */
153 /*
154 * For file systems smaller than SMALL_FSSIZE we use the S_DFL_* defaults,
155 * otherwise if less than MEDIUM_FSSIZE use M_DFL_*, otherwise use
156 * L_DFL_*.
157 */
158 #define SMALL_FSSIZE (20*1024*2)
159 #define S_DFL_FRAGSIZE 512
160 #define MEDIUM_FSSIZE (1000*1024*2)
161 #define M_DFL_FRAGSIZE 1024
162 #define L_DFL_FRAGSIZE 2048
163 #define DFL_FRAG_BLK 8
164
165 /* Apple requires the fragment size to be at least APPLEUFS_DIRBLKSIZ
166 * but the block size cannot be larger than Darwin's PAGE_SIZE. See
167 * the mount check in Darwin's ffs_mountfs for an explanation.
168 */
169 #define APPLEUFS_DFL_FRAGSIZE APPLEUFS_DIRBLKSIZ /* 1024 */
170 #define APPLEUFS_DFL_BLKSIZE 4096 /* default Darwin PAGE_SIZE */
171
172 /*
173 * Default sector size.
174 */
175 #define DFL_SECSIZE 512
176
177 /*
178 * Default file system size for "mount_mfs swap /dir" case.
179 */
180 #define DFL_FSSIZE (8 * 1024 * 1024)
181
182 /*
183 * MAXBLKPG determines the maximum number of data blocks which are
184 * placed in a single cylinder group. The default is one indirect
185 * block worth of data blocks.
186 */
187 #define MAXBLKPG_UFS1(bsize) ((bsize) / sizeof(int32_t))
188 #define MAXBLKPG_UFS2(bsize) ((bsize) / sizeof(int64_t))
189
190 /*
191 * Each file system has a number of inodes statically allocated.
192 * We allocate one inode slot per NFPI fragments, expecting this
193 * to be far more than we will ever need.
194 */
195 #define NFPI 4
196
197
198 int mfs; /* run as the memory based filesystem */
199 int Nflag; /* run without writing file system */
200 int Oflag = 1; /* format as an 4.3BSD file system */
201 int verbosity; /* amount of printf() output */
202 #define DEFAULT_VERBOSITY 3 /* 4 is traditional behavior */
203 int64_t fssize; /* file system size */
204 int sectorsize; /* bytes/sector */
205 int fsize = 0; /* fragment size */
206 int bsize = 0; /* block size */
207 int maxbsize = 0; /* maximum clustering */
208 int minfree = MINFREE; /* free space threshold */
209 int opt = DEFAULTOPT; /* optimization preference (space or time) */
210 int density; /* number of bytes per inode */
211 int num_inodes; /* number of inodes (overrides density) */
212 int maxcontig = 0; /* max contiguous blocks to allocate */
213 int maxbpg; /* maximum blocks per file in a cyl group */
214 int avgfilesize = AVFILESIZ;/* expected average file size */
215 int avgfpdir = AFPDIR; /* expected number of files per directory */
216 int mntflags = 0; /* flags to be passed to mount */
217 u_long memleft; /* virtual memory available */
218 caddr_t membase; /* start address of memory based filesystem */
219 int needswap; /* Filesystem not in native byte order */
220 char *disktype = NULL;
221 int unlabeled;
222 char *appleufs_volname = 0; /* Apple UFS volume name */
223 int isappleufs = 0;
224
225 char device[MAXPATHLEN];
226
227 int
228 main(int argc, char *argv[])
229 {
230 struct disk_geom geo;
231 struct dkwedge_info dkw;
232 struct statvfs *mp;
233 struct stat sb;
234 int ch, fsi, fso, len, n, Fflag, Iflag, Zflag;
235 char *cp, *s1, *s2, *special;
236 const char *opstring;
237 int byte_sized = 0;
238 #ifdef MFS
239 struct mfs_args args;
240 char mountfromname[100];
241 pid_t pid, res;
242 struct statvfs sf;
243 int status;
244 #endif
245 mode_t mfsmode = 01777; /* default mode for a /tmp-type directory */
246 uid_t mfsuid = 0; /* user root */
247 gid_t mfsgid = 0; /* group wheel */
248 mntoptparse_t mo;
249
250 cp = NULL;
251 fsi = fso = -1;
252 Fflag = Iflag = Zflag = 0;
253 verbosity = -1;
254 if (strstr(getprogname(), "mfs")) {
255 mfs = 1;
256 } else {
257 /* Undocumented, for ease of testing */
258 if (argv[1] != NULL && !strcmp(argv[1], "-mfs")) {
259 argv++;
260 argc--;
261 mfs = 1;
262 }
263 }
264
265 opstring = mfs ?
266 "NT:V:a:b:d:e:f:g:h:i:m:n:o:p:s:u:" :
267 "B:FINO:S:T:V:Za:b:d:e:f:g:h:i:l:m:n:o:r:s:v:";
268 while ((ch = getopt(argc, argv, opstring)) != -1)
269 switch (ch) {
270 case 'B':
271 if (strcmp(optarg, "be") == 0) {
272 #if BYTE_ORDER == LITTLE_ENDIAN
273 needswap = 1;
274 #endif
275 } else if (strcmp(optarg, "le") == 0) {
276 #if BYTE_ORDER == BIG_ENDIAN
277 needswap = 1;
278 #endif
279 } else
280 usage();
281 break;
282 case 'F':
283 Fflag = 1;
284 break;
285 case 'I':
286 Iflag = 1;
287 break;
288 case 'N':
289 Nflag = 1;
290 if (verbosity == -1)
291 verbosity = DEFAULT_VERBOSITY;
292 break;
293 case 'O':
294 Oflag = strsuftoi64("format", optarg, 0, 2, NULL);
295 break;
296 case 'S':
297 /* XXX: non-512 byte sectors almost certainly don't work. */
298 sectorsize = strsuftoi64("sector size",
299 optarg, 512, 65536, NULL);
300 if (sectorsize & (sectorsize - 1))
301 errx(1, "sector size `%s' is not a power of 2.",
302 optarg);
303 break;
304 #ifdef COMPAT
305 case 'T':
306 disktype = optarg;
307 break;
308 #endif
309 case 'V':
310 verbosity = strsuftoi64("verbose", optarg, 0, 4, NULL);
311 break;
312 case 'Z':
313 Zflag = 1;
314 break;
315 case 'a':
316 maxcontig = strsuftoi64("maximum contiguous blocks",
317 optarg, 1, INT_MAX, NULL);
318 break;
319 case 'b':
320 bsize = strsuftoi64("block size",
321 optarg, MINBSIZE, MAXBSIZE, NULL);
322 break;
323 case 'd':
324 maxbsize = strsuftoi64("maximum extent size",
325 optarg, 0, INT_MAX, NULL);
326 break;
327 case 'e':
328 maxbpg = strsuftoi64(
329 "blocks per file in a cylinder group",
330 optarg, 1, INT_MAX, NULL);
331 break;
332 case 'f':
333 fsize = strsuftoi64("fragment size",
334 optarg, 1, MAXBSIZE, NULL);
335 break;
336 case 'g':
337 if (mfs)
338 mfsgid = mfs_group(optarg);
339 else {
340 avgfilesize = strsuftoi64("average file size",
341 optarg, 1, INT_MAX, NULL);
342 }
343 break;
344 case 'h':
345 avgfpdir = strsuftoi64("expected files per directory",
346 optarg, 1, INT_MAX, NULL);
347 break;
348 case 'i':
349 density = strsuftoi64("bytes per inode",
350 optarg, 1, INT_MAX, NULL);
351 break;
352 case 'm':
353 minfree = strsuftoi64("free space %",
354 optarg, 0, 99, NULL);
355 break;
356 case 'n':
357 num_inodes = strsuftoi64("number of inodes",
358 optarg, 1, INT_MAX, NULL);
359 break;
360 case 'o':
361 if (mfs) {
362 mo = getmntopts(optarg, mopts, &mntflags, 0);
363 if (mo == NULL)
364 err(1, "getmntopts");
365 freemntopts(mo);
366 } else {
367 if (strcmp(optarg, "space") == 0)
368 opt = FS_OPTSPACE;
369 else if (strcmp(optarg, "time") == 0)
370 opt = FS_OPTTIME;
371 else
372 errx(1, "%s %s",
373 "unknown optimization preference: ",
374 "use `space' or `time'.");
375 }
376 break;
377 case 'p':
378 /* mfs only */
379 if ((mfsmode = strtol(optarg, NULL, 8)) <= 0)
380 errx(1, "bad mode `%s'", optarg);
381 break;
382 case 's':
383 fssize = strsuftoi64("file system size",
384 optarg, INT64_MIN, INT64_MAX, &byte_sized);
385 break;
386 case 'u':
387 /* mfs only */
388 mfsuid = mfs_user(optarg);
389 break;
390 case 'v':
391 appleufs_volname = optarg;
392 if (strchr(appleufs_volname, ':') || strchr(appleufs_volname, '/'))
393 errx(1,"Apple UFS volume name cannot contain ':' or '/'");
394 if (appleufs_volname[0] == '\0')
395 errx(1,"Apple UFS volume name cannot be zero length");
396 isappleufs = 1;
397 break;
398 case '?':
399 default:
400 usage();
401 }
402 argc -= optind;
403 argv += optind;
404
405 if (verbosity == -1)
406 /* Default to not showing CG info if mfs */
407 verbosity = mfs ? 0 : DEFAULT_VERBOSITY;
408
409 #ifdef MFS
410 /* This is enough to get through the correct kernel code paths */
411 memset(&args, 0, sizeof args);
412 args.fspec = mountfromname;
413 if (mntflags & (MNT_GETARGS | MNT_UPDATE)) {
414 if ((mntflags & MNT_GETARGS) == 0)
415 mntflags |= MNT_ASYNC;
416 if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0)
417 err(1, "mount `%s' failed", argv[1]);
418 if (mntflags & MNT_GETARGS)
419 printf("base=%p, size=%ld\n", args.base, args.size);
420 exit(0);
421 }
422 #endif
423
424 if (argc != 2 && (mfs || argc != 1))
425 usage();
426
427 memset(&sb, 0, sizeof sb);
428 special = argv[0];
429 if (Fflag || mfs) {
430 /*
431 * It's a file system image or an MFS,
432 * no label, use fixed default for sectorsize.
433 */
434 if (sectorsize == 0)
435 sectorsize = DFL_SECSIZE;
436
437 if (mfs) {
438 /*
439 * Default filesystem size to that of supplied device,
440 * and fall back to 8M
441 */
442 if (fssize == 0)
443 if (stat(special, &sb) == -1)
444 fssize = DFL_FSSIZE / sectorsize;
445 } else {
446 /* creating image in a regular file */
447 int fl;
448 if (Nflag)
449 fl = O_RDONLY;
450 else {
451 if (fssize > 0)
452 fl = O_RDWR | O_CREAT;
453 else
454 fl = O_RDWR;
455 }
456 fsi = open(special, fl, 0777);
457 if (fsi == -1)
458 err(1, "can't open file %s", special);
459 if (fstat(fsi, &sb) == -1)
460 err(1, "can't fstat opened %s", special);
461 if (!Nflag)
462 fso = fsi;
463 }
464 } else { /* !Fflag && !mfs */
465 fsi = opendisk(special, O_RDONLY, device, sizeof(device), 0);
466 special = device;
467 if (fsi < 0 || fstat(fsi, &sb) == -1)
468 err(1, "%s: open for read", special);
469
470 if (!Nflag) {
471 fso = open(special, O_WRONLY, 0);
472 if (fso < 0)
473 err(1, "%s: open for write", special);
474
475 /* Bail if target special is mounted */
476 n = getmntinfo(&mp, MNT_NOWAIT);
477 if (n == 0)
478 err(1, "%s: getmntinfo", special);
479
480 len = sizeof(_PATH_DEV) - 1;
481 s1 = special;
482 if (strncmp(_PATH_DEV, s1, len) == 0)
483 s1 += len;
484
485 while (--n >= 0) {
486 s2 = mp->f_mntfromname;
487 if (strncmp(_PATH_DEV, s2, len) == 0) {
488 s2 += len - 1;
489 *s2 = 'r';
490 }
491 if (strcmp(s1, s2) == 0 ||
492 strcmp(s1, &s2[1]) == 0)
493 errx(1, "%s is mounted on %s",
494 special, mp->f_mntonname);
495 ++mp;
496 }
497 }
498
499 #ifdef COMPAT
500 if (disktype == NULL)
501 disktype = argv[1];
502 #endif
503 if (getdiskinfo(special, fsi, disktype, &geo, &dkw) == -1)
504 errx(1, lmsg, special);
505 unlabeled = disktype != NULL;
506
507 if (sectorsize == 0) {
508 sectorsize = geo.dg_secsize;
509 if (sectorsize <= 0)
510 errx(1, "no default sector size");
511 }
512
513 if (dkw.dkw_parent[0]) {
514 if (dkw.dkw_size == 0)
515 errx(1, "%s partition is unavailable", special);
516
517 if (strcmp(dkw.dkw_ptype, DKW_PTYPE_APPLEUFS) == 0)
518 isappleufs = 1;
519
520 if (!Iflag) {
521 static const char m[] =
522 "%s partition type is not `%s'";
523 if (isappleufs) {
524 if (strcmp(dkw.dkw_ptype,
525 DKW_PTYPE_APPLEUFS))
526 errx(1, m,
527 special, "Apple UFS");
528 } else {
529 if (strcmp(dkw.dkw_ptype,
530 DKW_PTYPE_FFS))
531 errx(1, m, special, "4.2BSD");
532 }
533 }
534 } /* !Fflag && !mfs */
535 }
536
537 if (byte_sized)
538 fssize /= sectorsize;
539 if (fssize <= 0) {
540 if (sb.st_size != 0)
541 fssize += sb.st_size / sectorsize;
542 else
543 fssize += dkw.dkw_size;
544 if (fssize <= 0)
545 errx(1, "Unable to determine file system size");
546 }
547
548 if (dkw.dkw_parent[0] && fssize > dkw.dkw_size)
549 errx(1, "size %" PRIu64 " exceeds maximum file system size on "
550 "`%s' of %" PRIu64 " sectors",
551 fssize, special, dkw.dkw_size);
552
553 /* XXXLUKEM: only ftruncate() regular files ? (dsl: or at all?) */
554 if (Fflag && fso != -1
555 && ftruncate(fso, (off_t)fssize * sectorsize) == -1)
556 err(1, "can't ftruncate %s to %" PRId64, special, fssize);
557
558 if (Zflag && fso != -1) { /* pre-zero (and de-sparce) the file */
559 char *buf;
560 int bufsize, i;
561 off_t bufrem;
562 struct statvfs sfs;
563
564 if (fstatvfs(fso, &sfs) == -1) {
565 warn("can't fstatvfs `%s'", special);
566 bufsize = 8192;
567 } else
568 bufsize = sfs.f_iosize;
569
570 if ((buf = calloc(1, bufsize)) == NULL)
571 err(1, "can't malloc buffer of %d",
572 bufsize);
573 bufrem = fssize * sectorsize;
574 if (verbosity > 0)
575 printf( "Creating file system image in `%s', "
576 "size %lld bytes, in %d byte chunks.\n",
577 special, (long long)bufrem, bufsize);
578 while (bufrem > 0) {
579 i = write(fso, buf, MIN(bufsize, bufrem));
580 if (i == -1)
581 err(1, "writing image");
582 bufrem -= i;
583 }
584 free(buf);
585 }
586
587 /* Sort out fragment and block sizes */
588 if (fsize == 0) {
589 fsize = bsize / DFL_FRAG_BLK;
590 if (fsize <= 0) {
591 if (isappleufs) {
592 fsize = APPLEUFS_DFL_FRAGSIZE;
593 } else {
594 if (fssize < SMALL_FSSIZE)
595 fsize = S_DFL_FRAGSIZE;
596 else if (fssize < MEDIUM_FSSIZE)
597 fsize = M_DFL_FRAGSIZE;
598 else
599 fsize = L_DFL_FRAGSIZE;
600 if (fsize < sectorsize)
601 fsize = sectorsize;
602 }
603 }
604 }
605 if (bsize <= 0) {
606 if (isappleufs)
607 bsize = APPLEUFS_DFL_BLKSIZE;
608 else
609 bsize = DFL_FRAG_BLK * fsize;
610 }
611
612 if (isappleufs && (fsize < APPLEUFS_DFL_FRAGSIZE)) {
613 warnx("Warning: chosen fsize of %d is less than Apple UFS minimum of %d",
614 fsize, APPLEUFS_DFL_FRAGSIZE);
615 }
616 if (isappleufs && (bsize > APPLEUFS_DFL_BLKSIZE)) {
617 warnx("Warning: chosen bsize of %d is greater than Apple UFS maximum of %d",
618 bsize, APPLEUFS_DFL_BLKSIZE);
619 }
620
621 /*
622 * Maxcontig sets the default for the maximum number of blocks
623 * that may be allocated sequentially. With filesystem clustering
624 * it is possible to allocate contiguous blocks up to the maximum
625 * transfer size permitted by the controller or buffering.
626 */
627 if (maxcontig == 0)
628 maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize);
629 if (density == 0)
630 density = NFPI * fsize;
631 if (minfree < MINFREE && opt != FS_OPTSPACE) {
632 warnx("%s %s %d%%", "Warning: changing optimization to space",
633 "because minfree is less than", MINFREE);
634 opt = FS_OPTSPACE;
635 }
636 if (maxbpg == 0) {
637 if (Oflag <= 1)
638 maxbpg = MAXBLKPG_UFS1(bsize);
639 else
640 maxbpg = MAXBLKPG_UFS2(bsize);
641 }
642 mkfs(special, fsi, fso, mfsmode, mfsuid, mfsgid);
643 if (fsi != -1 && fsi != fso)
644 close(fsi);
645 if (fso != -1)
646 close(fso);
647 #ifdef MFS
648 if (mfs) {
649
650 switch (pid = fork()) {
651 case -1:
652 perror("mfs");
653 exit(10);
654 case 0:
655 (void)snprintf(mountfromname, sizeof(mountfromname),
656 "mfs:%d", getpid());
657 break;
658 default:
659 (void)snprintf(mountfromname, sizeof(mountfromname),
660 "mfs:%d", pid);
661 for (;;) {
662 /*
663 * spin until the mount succeeds
664 * or the child exits
665 */
666 usleep(1);
667
668 /*
669 * XXX Here is a race condition: another process
670 * can mount a filesystem which hides our
671 * ramdisk before we see the success.
672 */
673 if (statvfs(argv[1], &sf) < 0)
674 err(88, "statvfs %s", argv[1]);
675 if (!strcmp(sf.f_mntfromname, mountfromname) &&
676 !strncmp(sf.f_mntonname, argv[1],
677 MNAMELEN) &&
678 !strcmp(sf.f_fstypename, "mfs"))
679 exit(0);
680
681 res = waitpid(pid, &status, WNOHANG);
682 if (res == -1)
683 err(11, "waitpid");
684 if (res != pid)
685 continue;
686 if (WIFEXITED(status)) {
687 if (WEXITSTATUS(status) == 0)
688 exit(0);
689 errx(1, "%s: mount: %s", argv[1],
690 strerror(WEXITSTATUS(status)));
691 } else
692 errx(11, "abnormal termination");
693 }
694 /* NOTREACHED */
695 }
696
697 (void) setsid();
698 (void) close(0);
699 (void) close(1);
700 (void) close(2);
701 (void) chdir("/");
702
703 args.base = membase;
704 args.size = fssize * sectorsize;
705 if (mount(MOUNT_MFS, argv[1], mntflags | MNT_ASYNC, &args) < 0)
706 exit(errno); /* parent prints message */
707 }
708 #endif
709 exit(0);
710 }
711
712 static gid_t
713 mfs_group(const char *gname)
714 {
715 struct group *gp;
716
717 if (!(gp = getgrnam(gname)) && !isdigit((unsigned char)*gname))
718 errx(1, "unknown gname %s", gname);
719 return gp ? gp->gr_gid : atoi(gname);
720 }
721
722 static uid_t
723 mfs_user(const char *uname)
724 {
725 struct passwd *pp;
726
727 if (!(pp = getpwnam(uname)) && !isdigit((unsigned char)*uname))
728 errx(1, "unknown user %s", uname);
729 return pp ? pp->pw_uid : atoi(uname);
730 }
731
732 static int64_t
733 strsuftoi64(const char *desc, const char *arg, int64_t min, int64_t max, int *num_suffix)
734 {
735 int64_t result, r1;
736 int shift = 0;
737 char *ep;
738
739 errno = 0;
740 r1 = strtoll(arg, &ep, 10);
741 if (ep[0] != '\0' && ep[1] != '\0')
742 errx(1, "%s `%s' is not a valid number.", desc, arg);
743 switch (ep[0]) {
744 case '\0':
745 case 's': case 'S':
746 if (num_suffix != NULL)
747 *num_suffix = 0;
748 break;
749 case 'g': case 'G':
750 shift += 10;
751 /* FALLTHROUGH */
752 case 'm': case 'M':
753 shift += 10;
754 /* FALLTHROUGH */
755 case 'k': case 'K':
756 shift += 10;
757 /* FALLTHROUGH */
758 case 'b': case 'B':
759 if (num_suffix != NULL)
760 *num_suffix = 1;
761 break;
762 default:
763 errx(1, "`%s' is not a valid suffix for %s.", ep, desc);
764 }
765 result = r1 << shift;
766 if (errno == ERANGE || result >> shift != r1)
767 errx(1, "%s `%s' is too large to convert.", desc, arg);
768 if (result < min)
769 errx(1, "%s `%s' (%" PRId64 ") is less than the minimum (%" PRId64 ").",
770 desc, arg, result, min);
771 if (result > max)
772 errx(1, "%s `%s' (%" PRId64 ") is greater than the maximum (%" PRId64 ").",
773 desc, arg, result, max);
774 return result;
775 }
776
777 #define NEWFS 1
778 #define MFS_MOUNT 2
779 #define BOTH NEWFS | MFS_MOUNT
780
781 struct help_strings {
782 int flags;
783 const char *str;
784 } const help_strings[] = {
785 { NEWFS, "-B byteorder\tbyte order (`be' or `le')" },
786 { NEWFS, "-F \t\tcreate file system image in regular file" },
787 { NEWFS, "-I \t\tdo not check that the file system type is '4.2BSD'" },
788 { BOTH, "-N \t\tdo not create file system, just print out "
789 "parameters" },
790 { NEWFS, "-O N\t\tfilesystem format: 0 ==> 4.3BSD, 1 ==> FFS, 2 ==> UFS2" },
791 { NEWFS, "-S secsize\tsector size" },
792 #ifdef COMPAT
793 { NEWFS, "-T disktype\tdisk type" },
794 #endif
795 { BOTH, "-V verbose\toutput verbosity: 0 ==> none, 4 ==> max" },
796 { NEWFS, "-Z \t\tpre-zero the image file" },
797 { BOTH, "-a maxcontig\tmaximum contiguous blocks" },
798 { BOTH, "-b bsize\tblock size" },
799 { BOTH, "-d maxbsize\tmaximum extent size" },
800 { BOTH, "-e maxbpg\tmaximum blocks per file in a cylinder group"
801 },
802 { BOTH, "-f fsize\tfrag size" },
803 { NEWFS, "-g avgfilesize\taverage file size" },
804 { MFS_MOUNT, "-g groupname\tgroup name of mount point" },
805 { BOTH, "-h avgfpdir\taverage files per directory" },
806 { BOTH, "-i density\tnumber of bytes per inode" },
807 { BOTH, "-m minfree\tminimum free space %%" },
808 { BOTH, "-n inodes\tnumber of inodes (overrides -i density)" },
809 { BOTH, "-o optim\toptimization preference (`space' or `time')"
810 },
811 { MFS_MOUNT, "-p perm\t\tpermissions (in octal)" },
812 { BOTH, "-s fssize\tfile system size (sectors)" },
813 { MFS_MOUNT, "-u username\tuser name of mount point" },
814 { NEWFS, "-v volname\tApple UFS volume name" },
815 { 0, NULL }
816 };
817
818 static void
819 usage(void)
820 {
821 int match;
822 const struct help_strings *hs;
823
824 if (mfs) {
825 fprintf(stderr,
826 "usage: %s [ fsoptions ] special-device mount-point\n",
827 getprogname());
828 } else
829 fprintf(stderr,
830 "usage: %s [ fsoptions ] special-device%s\n",
831 getprogname(),
832 #ifdef COMPAT
833 " [disk-type]");
834 #else
835 "");
836 #endif
837 fprintf(stderr, "where fsoptions are:\n");
838
839 match = mfs ? MFS_MOUNT : NEWFS;
840 for (hs = help_strings; hs->flags != 0; hs++)
841 if (hs->flags & match)
842 fprintf(stderr, "\t%s\n", hs->str);
843 exit(1);
844 }
845