newfs.c revision 1.79 1 /* $NetBSD: newfs.c,v 1.79 2004/03/07 00:17:04 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.79 2004/03/07 00:17:04 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 <limits.h>
107 #include <paths.h>
108 #include <pwd.h>
109 #include <signal.h>
110 #include <stdint.h>
111 #include <stdio.h>
112 #include <stdlib.h>
113 #include <string.h>
114 #include <syslog.h>
115 #include <unistd.h>
116 #include <util.h>
117
118 #include "mntopts.h"
119 #include "dkcksum.h"
120 #include "extern.h"
121
122 struct mntopt mopts[] = {
123 MOPT_STDOPTS,
124 MOPT_ASYNC,
125 MOPT_UPDATE,
126 MOPT_GETARGS,
127 MOPT_NOATIME,
128 { NULL },
129 };
130
131 static struct disklabel *getdisklabel(char *, int);
132 static void rewritelabel(char *, int, struct disklabel *);
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);
137 int main(int, char *[]);
138
139 #define COMPAT /* allow non-labeled disks */
140
141 /*
142 * The following two constants set the default block and fragment sizes.
143 * Both constants must be a power of 2 and meet the following constraints:
144 * MINBSIZE <= DESBLKSIZE <= MAXBSIZE
145 * sectorsize <= DESFRAGSIZE <= DESBLKSIZE
146 * DESBLKSIZE / DESFRAGSIZE <= 8
147 */
148 /*
149 * For file systems smaller than SMALL_FSSIZE we use the S_DFL_* defaults,
150 * otherwise if less than MEDIUM_FSSIZE use M_DFL_*, otherwise use
151 * L_DFL_*.
152 */
153 #define SMALL_FSSIZE (20*1024*2)
154 #define S_DFL_FRAGSIZE 512
155 #define MEDIUM_FSSIZE (1000*1024*2)
156 #define M_DFL_FRAGSIZE 1024
157 #define L_DFL_FRAGSIZE 2048
158 #define DFL_FRAG_BLK 8
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 = NULL;
221 struct disklabel *lp = NULL;
222 struct partition oldpartition;
223 struct statfs *mp;
224 struct stat sb;
225 int ch, fsi, fso, len, n, Fflag, Iflag, Zflag;
226 uint ptn = 0; /* gcc -Wuninitialised */
227 char *cp, *s1, *s2, *special;
228 const char *opstring;
229 int byte_sized = 0;
230 #ifdef MFS
231 struct mfs_args args;
232 char mountfromname[100];
233 pid_t pid, res;
234 struct statfs sf;
235 int status;
236 #endif
237 mode_t mfsmode = 01777; /* default mode for a /tmp-type directory */
238 uid_t mfsuid = 0; /* user root */
239 gid_t mfsgid = 0; /* group wheel */
240
241 cp = NULL;
242 fsi = fso = -1;
243 Fflag = Iflag = Zflag = 0;
244 if (strstr(getprogname(), "mfs")) {
245 mfs = 1;
246 Nflag++;
247 }
248
249 opstring = mfs ?
250 "NT:a:b:c:d:e:f:g:h:i:m:n:o:p:s:u:" :
251 "B:FINO:S:T:Za:b:c:d:e:f:g:h:i:l:m:n:o:p:r:s:u:v:";
252 while ((ch = getopt(argc, argv, opstring)) != -1)
253 switch (ch) {
254 case 'B':
255 if (strcmp(optarg, "be") == 0) {
256 #if BYTE_ORDER == LITTLE_ENDIAN
257 needswap = 1;
258 #endif
259 } else if (strcmp(optarg, "le") == 0) {
260 #if BYTE_ORDER == BIG_ENDIAN
261 needswap = 1;
262 #endif
263 } else
264 usage();
265 break;
266 case 'F':
267 Fflag = 1;
268 break;
269 case 'I':
270 Iflag = 1;
271 break;
272 case 'N':
273 Nflag = 1;
274 break;
275 case 'O':
276 Oflag = strsuftoi64("format", optarg, 0, 2, NULL);
277 break;
278 case 'S':
279 sectorsize = strsuftoi64("sector size",
280 optarg, 1, INT_MAX, NULL);
281 break;
282 #ifdef COMPAT
283 case 'T':
284 disktype = optarg;
285 break;
286 #endif
287 case 'Z':
288 Zflag = 1;
289 break;
290 case 'a':
291 maxcontig = strsuftoi64("maximum contiguous blocks",
292 optarg, 1, INT_MAX, NULL);
293 break;
294 case 'b':
295 bsize = strsuftoi64("block size",
296 optarg, MINBSIZE, MAXBSIZE, NULL);
297 break;
298 case 'c': /* was cylinders per group... */
299 break;
300 case 'd':
301 maxbsize = strsuftoi64("maximum extent size",
302 optarg, 0, INT_MAX, NULL);
303 break;
304 case 'e':
305 maxbpg = strsuftoi64(
306 "blocks per file in a cylinder group",
307 optarg, 1, INT_MAX, NULL);
308 break;
309 case 'f':
310 fsize = strsuftoi64("fragment size",
311 optarg, 1, MAXBSIZE, NULL);
312 break;
313 case 'g':
314 if (mfs)
315 mfsgid = mfs_group(optarg);
316 else {
317 avgfilesize = strsuftoi64("average file size",
318 optarg, 1, INT_MAX, NULL);
319 }
320 break;
321 case 'h':
322 avgfpdir = strsuftoi64("expected files per directory",
323 optarg, 1, INT_MAX, NULL);
324 break;
325 case 'i':
326 density = strsuftoi64("bytes per inode",
327 optarg, 1, INT_MAX, NULL);
328 break;
329 case 'm':
330 minfree = strsuftoi64("free space %",
331 optarg, 0, 99, NULL);
332 break;
333 case 'n':
334 num_inodes = strsuftoi64("number of inodes",
335 optarg, 1, INT_MAX, NULL);
336 break;
337 case 'o':
338 if (mfs)
339 getmntopts(optarg, mopts, &mntflags, 0);
340 else {
341 if (strcmp(optarg, "space") == 0)
342 opt = FS_OPTSPACE;
343 else if (strcmp(optarg, "time") == 0)
344 opt = FS_OPTTIME;
345 else
346 errx(1, "%s %s",
347 "unknown optimization preference: ",
348 "use `space' or `time'.");
349 }
350 break;
351 case 'p':
352 if (mfs) {
353 if ((mfsmode = strtol(optarg, NULL, 8)) <= 0)
354 errx(1, "bad mode `%s'", optarg);
355 } else
356 errx(1, "unknown option 'p'");
357 break;
358 case 's':
359 fssize = strsuftoi64("file system size",
360 optarg, INT64_MIN, INT64_MAX, &byte_sized);
361 break;
362 case 'u':
363 if (mfs)
364 mfsuid = mfs_user(optarg);
365 else
366 errx(1, "deprecated option 'u'");
367 break;
368 case 'v':
369 appleufs_volname = optarg;
370 if (strchr(appleufs_volname, ':') || strchr(appleufs_volname, '/'))
371 errx(1,"Apple UFS volume name cannot contain ':' or '/'");
372 if (appleufs_volname[0] == '\0')
373 errx(1,"Apple UFS volume name cannot be zero length");
374 isappleufs = 1;
375 break;
376 case '?':
377 default:
378 usage();
379 }
380 argc -= optind;
381 argv += optind;
382
383 #ifdef MFS
384 /* This is enough to get through the correct kernel code paths */
385 memset(&args, 0, sizeof args);
386 args.fspec = mountfromname;
387 if (mntflags & (MNT_GETARGS | MNT_UPDATE))
388 goto doit;
389 #endif
390
391 if (argc != 2 && (mfs || argc != 1))
392 usage();
393
394 memset(&oldpartition, 0, sizeof oldpartition);
395 memset(&sb, 0, sizeof sb);
396 special = argv[0];
397 if (Fflag || mfs) {
398 /*
399 * It's a file system image or an MFS,
400 * no label, use fixed default for sectorsize.
401 */
402 if (sectorsize == 0)
403 sectorsize = DFL_SECSIZE;
404
405 if (!mfs) { /* creating image in a regular file */
406 int fl;
407 if (Nflag)
408 fl = O_RDONLY;
409 else {
410 if (fssize > 0)
411 fl = O_RDWR | O_CREAT;
412 else
413 fl = O_RDWR;
414 }
415 fsi = open(special, fl, 0777);
416 if (fsi == -1)
417 err(1, "can't open file %s", special);
418 if (fstat(fsi, &sb) == -1)
419 err(1, "can't fstat opened %s", special);
420 if (!Nflag)
421 fso = fsi;
422 }
423 } else { /* !Fflag && !mfs */
424 fsi = opendisk(special, O_RDONLY, device, sizeof(device), 0);
425 special = device;
426 if (fsi < 0 || fstat(fsi, &sb) == -1)
427 err(1, "%s: open for read", special);
428
429 if (!Nflag) {
430 fso = open(special, O_WRONLY, 0);
431 if (fso < 0)
432 err(1, "%s: open for write", special);
433
434 /* Bail if target special is mounted */
435 n = getmntinfo(&mp, MNT_NOWAIT);
436 if (n == 0)
437 err(1, "%s: getmntinfo", special);
438
439 len = sizeof(_PATH_DEV) - 1;
440 s1 = special;
441 if (strncmp(_PATH_DEV, s1, len) == 0)
442 s1 += len;
443
444 while (--n >= 0) {
445 s2 = mp->f_mntfromname;
446 if (strncmp(_PATH_DEV, s2, len) == 0) {
447 s2 += len - 1;
448 *s2 = 'r';
449 }
450 if (strcmp(s1, s2) == 0 ||
451 strcmp(s1, &s2[1]) == 0)
452 errx(1, "%s is mounted on %s",
453 special, mp->f_mntonname);
454 ++mp;
455 }
456 }
457
458 #ifdef COMPAT
459 if (disktype == NULL)
460 disktype = argv[1];
461 #endif
462 lp = getdisklabel(special, fsi);
463 if (sectorsize == 0) {
464 sectorsize = lp->d_secsize;
465 if (sectorsize <= 0)
466 errx(1, "no default sector size");
467 }
468
469 ptn = strchr(special, '\0')[-1] - 'a';
470 if (ptn < lp->d_npartitions && ptn == DISKPART(sb.st_rdev)) {
471 /* Assume partition really does match the label */
472 pp = &lp->d_partitions[ptn];
473 oldpartition = *pp;
474 if (pp->p_size == 0)
475 errx(1, "`%c' partition is unavailable",
476 'a' + ptn);
477 if (pp->p_fstype == FS_APPLEUFS)
478 isappleufs = 1;
479 if (!Iflag) {
480 if (isappleufs) {
481 if (pp->p_fstype != FS_APPLEUFS)
482 errx(1, "`%c' partition type is not `Apple UFS'", 'a' + ptn);
483 } else {
484 if (pp->p_fstype != FS_BSDFFS)
485 errx(1, "`%c' partition type is not `4.2BSD'", 'a' + ptn);
486 }
487 }
488 }
489 } /* !Fflag && !mfs */
490
491 if (byte_sized)
492 fssize /= sectorsize;
493 if (fssize <= 0) {
494 if (sb.st_size != 0)
495 fssize += sb.st_size / sectorsize;
496 else
497 fssize += oldpartition.p_size;
498 if (fssize <= 0)
499 errx(1, "Unable to determine file system size");
500 }
501
502 if (pp != NULL && fssize > pp->p_size)
503 errx(1, "maximum file system size on `%s' is %d sectors",
504 special, pp->p_size);
505
506 /* XXXLUKEM: only ftruncate() regular files ? (dsl: or at all?) */
507 if (Fflag && fso != -1
508 && ftruncate(fso, (off_t)fssize * sectorsize) == -1)
509 err(1, "can't ftruncate %s to %" PRId64, special, fssize);
510
511 if (Zflag && fso != -1) { /* pre-zero (and de-sparce) the file */
512 char *buf;
513 int bufsize, i;
514 off_t bufrem;
515 struct statfs sfs;
516
517 if (fstatfs(fso, &sfs) == -1) {
518 warn("can't fstatfs `%s'", special);
519 bufsize = 8192;
520 } else
521 bufsize = sfs.f_iosize;
522
523 if ((buf = calloc(1, bufsize)) == NULL)
524 err(1, "can't malloc buffer of %d",
525 bufsize);
526 bufrem = fssize * sectorsize;
527 printf(
528 "Creating file system image in `%s', size %lld bytes, in %d byte chunks.\n",
529 special, (long long)bufrem, bufsize);
530 while (bufrem > 0) {
531 i = write(fso, buf, MIN(bufsize, bufrem));
532 if (i == -1)
533 err(1, "writing image");
534 bufrem -= i;
535 }
536 free(buf);
537 }
538
539 /* Sort out fragment and block sizes */
540 if (fsize == 0) {
541 fsize = bsize / DFL_FRAG_BLK;
542 if (fsize == 0)
543 fsize = oldpartition.p_fsize;
544 if (fsize <= 0) {
545 if (isappleufs) {
546 fsize = APPLEUFS_DFL_FRAGSIZE;
547 } else {
548 if (fssize < SMALL_FSSIZE)
549 fsize = S_DFL_FRAGSIZE;
550 else if (fssize < MEDIUM_FSSIZE)
551 fsize = M_DFL_FRAGSIZE;
552 else
553 fsize = L_DFL_FRAGSIZE;
554 if (fsize < sectorsize)
555 fsize = sectorsize;
556 }
557 }
558 }
559 if (bsize == 0) {
560 bsize = oldpartition.p_frag * oldpartition.p_fsize;
561 if (bsize <= 0) {
562 if (isappleufs)
563 bsize = APPLEUFS_DFL_BLKSIZE;
564 else
565 bsize = DFL_FRAG_BLK * fsize;
566 }
567 }
568
569 if (isappleufs && (fsize < APPLEUFS_DFL_FRAGSIZE)) {
570 warnx("Warning: chosen fsize of %d is less than Apple UFS minimum of %d",
571 fsize, APPLEUFS_DFL_FRAGSIZE);
572 }
573 if (isappleufs && (bsize > APPLEUFS_DFL_BLKSIZE)) {
574 warnx("Warning: chosen bsize of %d is greater than Apple UFS maximum of %d",
575 bsize, APPLEUFS_DFL_BLKSIZE);
576 }
577
578 /*
579 * Maxcontig sets the default for the maximum number of blocks
580 * that may be allocated sequentially. With filesystem clustering
581 * it is possible to allocate contiguous blocks up to the maximum
582 * transfer size permitted by the controller or buffering.
583 */
584 if (maxcontig == 0)
585 maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize);
586 if (density == 0)
587 density = NFPI * fsize;
588 if (minfree < MINFREE && opt != FS_OPTSPACE) {
589 warnx("%s %s %d%%", "Warning: changing optimization to space",
590 "because minfree is less than", MINFREE);
591 opt = FS_OPTSPACE;
592 }
593 if (maxbpg == 0) {
594 if (Oflag <= 1)
595 maxbpg = MAXBLKPG_UFS1(bsize);
596 else
597 maxbpg = MAXBLKPG_UFS2(bsize);
598 }
599 mkfs(pp, special, fsi, fso, mfsmode, mfsuid, mfsgid);
600 if (!Nflag && pp != NULL
601 && memcmp(pp, &oldpartition, sizeof(oldpartition)))
602 rewritelabel(special, fso, lp);
603 if (fsi != -1 && fsi != fso)
604 close(fsi);
605 if (fso != -1)
606 close(fso);
607 #ifdef MFS
608 if (mfs) {
609
610 switch (pid = fork()) {
611 case -1:
612 perror("mfs");
613 exit(10);
614 case 0:
615 (void)snprintf(mountfromname, sizeof(mountfromname),
616 "mfs:%d", getpid());
617 break;
618 default:
619 (void)snprintf(mountfromname, sizeof(mountfromname),
620 "mfs:%d", pid);
621 for (;;) {
622 /*
623 * spin until the mount succeeds
624 * or the child exits
625 */
626 usleep(1);
627
628 /*
629 * XXX Here is a race condition: another process
630 * can mount a filesystem which hides our
631 * ramdisk before we see the success.
632 */
633 if (statfs(argv[1], &sf) < 0)
634 err(88, "statfs %s", argv[1]);
635 if (!strcmp(sf.f_mntfromname, mountfromname) &&
636 !strncmp(sf.f_mntonname, argv[1],
637 MNAMELEN) &&
638 !strcmp(sf.f_fstypename, "mfs"))
639 exit(0);
640
641 res = waitpid(pid, &status, WNOHANG);
642 if (res == -1)
643 err(11, "waitpid");
644 if (res != pid)
645 continue;
646 if (WIFEXITED(status)) {
647 if (WEXITSTATUS(status) == 0)
648 exit(0);
649 errx(1, "%s: mount: %s", argv[1],
650 strerror(WEXITSTATUS(status)));
651 } else
652 errx(11, "abnormal termination");
653 }
654 /* NOTREACHED */
655 }
656
657 (void) setsid();
658 (void) close(0);
659 (void) close(1);
660 (void) close(2);
661 (void) chdir("/");
662
663 args.export.ex_root = -2;
664 if (mntflags & MNT_RDONLY)
665 args.export.ex_flags = MNT_EXRDONLY;
666 else
667 args.export.ex_flags = 0;
668 args.base = membase;
669 args.size = fssize * sectorsize;
670 doit:
671 if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0) {
672 if (mntflags & (MNT_GETARGS | MNT_UPDATE))
673 err(1, "mount `%s' failed", argv[1]);
674 exit(errno); /* parent prints message */
675 }
676 if (mntflags & MNT_GETARGS)
677 printf("base=%p, size=%ld\n", args.base, args.size);
678 }
679 #endif
680 exit(0);
681 }
682
683 #ifdef COMPAT
684 const char lmsg[] = "%s: can't read disk label; disk type must be specified";
685 #else
686 const char lmsg[] = "%s: can't read disk label";
687 #endif
688
689 static struct disklabel *
690 getdisklabel(char *s, int fd)
691 {
692 static struct disklabel lab;
693
694 #ifdef COMPAT
695 if (disktype) {
696 struct disklabel *lp;
697
698 unlabeled++;
699 lp = getdiskbyname(disktype);
700 if (lp == NULL)
701 errx(1, "%s: unknown disk type", disktype);
702 return (lp);
703 }
704 #endif
705 if (ioctl(fd, DIOCGDINFO, &lab) < 0) {
706 warn("ioctl (GDINFO)");
707 errx(1, lmsg, s);
708 }
709 return (&lab);
710 }
711
712 static void
713 rewritelabel(char *s, int fd, struct disklabel *lp)
714 {
715 #ifdef COMPAT
716 if (unlabeled)
717 return;
718 #endif
719 lp->d_checksum = 0;
720 lp->d_checksum = dkcksum(lp);
721 if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
722 if (errno == ESRCH)
723 return;
724 warn("ioctl (WDINFO)");
725 errx(1, "%s: can't rewrite disk label", s);
726 }
727 #if __vax__
728 if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
729 int i;
730 int cfd;
731 daddr_t alt;
732 off_t loff;
733 char specname[64];
734 char blk[1024];
735 char *cp;
736
737 /*
738 * Make name for 'c' partition.
739 */
740 strlcpy(specname, s, sizeof(specname));
741 cp = specname + strlen(specname) - 1;
742 if (!isdigit(*cp))
743 *cp = 'c';
744 cfd = open(specname, O_WRONLY);
745 if (cfd < 0)
746 err(1, "%s: open", specname);
747 if ((loff = getlabeloffset()) < 0)
748 err(1, "getlabeloffset()");
749 memset(blk, 0, sizeof(blk));
750 *(struct disklabel *)(blk + loff) = *lp;
751 alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
752 for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
753 off_t offset;
754
755 offset = alt + i;
756 offset *= lp->d_secsize;
757 if (lseek(cfd, offset, SEEK_SET) == -1)
758 err(1, "lseek to badsector area: ");
759 if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
760 warn("alternate label %d write", i/2);
761 }
762 close(cfd);
763 }
764 #endif
765 }
766
767 static gid_t
768 mfs_group(const char *gname)
769 {
770 struct group *gp;
771
772 if (!(gp = getgrnam(gname)) && !isdigit((unsigned char)*gname))
773 errx(1, "unknown gname %s", gname);
774 return gp ? gp->gr_gid : atoi(gname);
775 }
776
777 static uid_t
778 mfs_user(const char *uname)
779 {
780 struct passwd *pp;
781
782 if (!(pp = getpwnam(uname)) && !isdigit((unsigned char)*uname))
783 errx(1, "unknown user %s", uname);
784 return pp ? pp->pw_uid : atoi(uname);
785 }
786
787 static int64_t
788 strsuftoi64(const char *desc, const char *arg, int64_t min, int64_t max, int *num_suffix)
789 {
790 int64_t result, r1;
791 int shift = 0;
792 char *ep;
793
794 errno = 0;
795 r1 = strtoll(arg, &ep, 10);
796 if (ep[0] != '\0' && ep[1] != '\0')
797 errx(1, "%s `%s' is not a valid number.", desc, arg);
798 switch (ep[0]) {
799 case '\0':
800 case 's': case 'S':
801 if (num_suffix != NULL)
802 *num_suffix = 0;
803 break;
804 case 'g': case 'G':
805 shift += 10;
806 /* FALLTHROUGH */
807 case 'm': case 'M':
808 shift += 10;
809 /* FALLTHROUGH */
810 case 'k': case 'K':
811 shift += 10;
812 /* FALLTHROUGH */
813 case 'b': case 'B':
814 if (num_suffix != NULL)
815 *num_suffix = 1;
816 break;
817 default:
818 errx(1, "`%s' is not a valid suffix for %s.", ep, desc);
819 }
820 result = r1 << shift;
821 if (errno == ERANGE || result >> shift != r1)
822 errx(1, "%s `%s' is too large to convert.", desc, arg);
823 if (result < min)
824 errx(1, "%s `%s' (%" PRId64 ") is less than the minimum (%" PRId64 ").",
825 desc, arg, result, min);
826 if (result > max)
827 errx(1, "%s `%s' (%" PRId64 ") is greater than the maximum (%" PRId64 ").",
828 desc, arg, result, max);
829 return result;
830 }
831
832 #define NEWFS 1
833 #define MFS_MOUNT 2
834 #define BOTH NEWFS | MFS_MOUNT
835
836 struct help_strings {
837 int flags;
838 const char *str;
839 } const help_strings[] = {
840 { NEWFS, "-B byteorder\tbyte order (`be' or `le')" },
841 { NEWFS, "-F \t\tcreate file system image in regular file" },
842 { NEWFS, "-I \t\tdo not check that the file system type is '4.2BSD'" },
843 { BOTH, "-N \t\tdo not create file system, just print out "
844 "parameters" },
845 { NEWFS, "-O N\t\tfilesystem format: 0 ==> 4.3BSD, 1 ==> FFS, 2 ==> UFS2" },
846 { NEWFS, "-S secsize\tsector size" },
847 #ifdef COMPAT
848 { NEWFS, "-T disktype\tdisk type" },
849 #endif
850 { NEWFS, "-Z \t\tpre-zero the image file" },
851 { BOTH, "-a maxcontig\tmaximum contiguous blocks" },
852 { BOTH, "-b bsize\tblock size" },
853 { BOTH, "-c blocks\tblocks per cylinder group" },
854 { BOTH, "-d maxbsize\tmaximum extent size" },
855 { BOTH, "-e maxbpg\tmaximum blocks per file in a cylinder group"
856 },
857 { BOTH, "-f fsize\tfrag size" },
858 { NEWFS, "-g avgfilesize\taverage file size" },
859 { MFS_MOUNT, "-g groupname\tgroup name of mount point" },
860 { BOTH, "-h avgfpdir\taverage files per directory" },
861 { BOTH, "-i density\tnumber of bytes per inode" },
862 { BOTH, "-m minfree\tminimum free space %%" },
863 { BOTH, "-n inodes\tnumber of inodes (overrides -i density)" },
864 { BOTH, "-o optim\toptimization preference (`space' or `time')"
865 },
866 { MFS_MOUNT, "-p perm\t\tpermissions (in octal)" },
867 { BOTH, "-s fssize\tfile system size (sectors)" },
868 { MFS_MOUNT, "-u username\tuser name of mount point" },
869 { NEWFS, "-v volname\tApple UFS volume name" },
870 { 0, NULL }
871 };
872
873 static void
874 usage(void)
875 {
876 int match;
877 const struct help_strings *hs;
878
879 if (mfs) {
880 fprintf(stderr,
881 "usage: %s [ fsoptions ] special-device mount-point\n",
882 getprogname());
883 } else
884 fprintf(stderr,
885 "usage: %s [ fsoptions ] special-device%s\n",
886 getprogname(),
887 #ifdef COMPAT
888 " [disk-type]");
889 #else
890 "");
891 #endif
892 fprintf(stderr, "where fsoptions are:\n");
893
894 match = mfs ? MFS_MOUNT : NEWFS;
895 for (hs = help_strings; hs->flags != 0; hs++)
896 if (hs->flags & match)
897 fprintf(stderr, "\t%s\n", hs->str);
898 exit(1);
899 }
900