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