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