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