update for SETOPT/GETOPT (from karels)
[unix-history] / usr / src / sbin / newfs / newfs.c
index 7323521..01302a6 100644 (file)
@@ -1,17 +1,17 @@
 /*
 /*
- * Copyright (c) 1983, 1989, 1993
+ * Copyright (c) 1983, 1989, 1993, 1994
  *     The Regents of the University of California.  All rights reserved.
  *
  * %sccs.include.redist.c%
  */
 
 #ifndef lint
  *     The Regents of the University of California.  All rights reserved.
  *
  * %sccs.include.redist.c%
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)newfs.c    8.1 (Berkeley) %G%";
+static char sccsid[] = "@(#)newfs.c    8.11 (Berkeley) %G%";
 #endif /* not lint */
 
 #ifndef lint
 static char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 static char copyright[] =
-"@(#) Copyright (c) 1983, 1989, 1993\n\
+"@(#) Copyright (c) 1983, 1989, 1993, 1994\n\
        The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
        The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
@@ -24,21 +24,32 @@ static char copyright[] =
 #include <sys/disklabel.h>
 #include <sys/file.h>
 #include <sys/mount.h>
 #include <sys/disklabel.h>
 #include <sys/file.h>
 #include <sys/mount.h>
+
 #include <ufs/ufs/dir.h>
 #include <ufs/ffs/fs.h>
 
 #include <ufs/ufs/dir.h>
 #include <ufs/ffs/fs.h>
 
+#include <ctype.h>
 #include <errno.h>
 #include <errno.h>
+#include <paths.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <syslog.h>
+#include <unistd.h>
+
 #if __STDC__
 #include <stdarg.h>
 #else
 #include <varargs.h>
 #endif
 #if __STDC__
 #include <stdarg.h>
 #else
 #include <varargs.h>
 #endif
-#include <unistd.h>
-#include <stdio.h>
-#include <ctype.h>
-#include <string.h>
-#include <stdlib.h>
-#include <paths.h>
+
+#include "mntopts.h"
+
+struct mntopt mopts[] = {
+       MOPT_STDOPTS,
+       MOPT_ASYNC,
+       { NULL },
+};
 
 #if __STDC__
 void   fatal(const char *fmt, ...);
 
 #if __STDC__
 void   fatal(const char *fmt, ...);
@@ -66,19 +77,6 @@ void fatal();
  */
 #define        DESCPG          16      /* desired fs_cpg */
 
  */
 #define        DESCPG          16      /* desired fs_cpg */
 
-/*
- * MINFREE gives the minimum acceptable percentage of file system
- * blocks which may be free. If the freelist drops below this level
- * only the superuser may continue to allocate blocks. This may
- * be set to 0 if no reserve of free blocks is deemed necessary,
- * however throughput drops by fifty percent if the file system
- * is run at between 95% and 100% full; thus the default value of
- * fs_minfree is 5%. With 5% free space, fragmentation is not a
- * problem, so we choose to optimize for time.
- */
-#define MINFREE                5
-#define DEFAULTOPT     FS_OPTTIME
-
 /*
  * ROTDELAY gives the minimum number of milliseconds to initiate
  * another disk transfer on the same cylinder. It is used in
 /*
  * ROTDELAY gives the minimum number of milliseconds to initiate
  * another disk transfer on the same cylinder. It is used in
@@ -143,7 +141,7 @@ int maxbpg;                 /* maximum blocks per file in a cyl group */
 int    nrpos = NRPOS;          /* # of distinguished rotational positions */
 int    bbsize = BBSIZE;        /* boot block size */
 int    sbsize = SBSIZE;        /* superblock size */
 int    nrpos = NRPOS;          /* # of distinguished rotational positions */
 int    bbsize = BBSIZE;        /* boot block size */
 int    sbsize = SBSIZE;        /* superblock size */
-int    mntflags;               /* flags to be passed to mount */
+int    mntflags = MNT_ASYNC;   /* flags to be passed to mount */
 u_long memleft;                /* virtual memory available */
 caddr_t        membase;                /* start address of memory based filesystem */
 #ifdef COMPAT
 u_long memleft;                /* virtual memory available */
 caddr_t        membase;                /* start address of memory based filesystem */
 #ifdef COMPAT
@@ -154,6 +152,7 @@ int unlabeled;
 char   device[MAXPATHLEN];
 char   *progname;
 
 char   device[MAXPATHLEN];
 char   *progname;
 
+int
 main(argc, argv)
        int argc;
        char *argv[];
 main(argc, argv)
        int argc;
        char *argv[];
@@ -166,13 +165,11 @@ main(argc, argv)
        struct disklabel *getdisklabel();
        struct partition oldpartition;
        struct stat st;
        struct disklabel *getdisklabel();
        struct partition oldpartition;
        struct stat st;
-       int fsi, fso;
-       char *cp, *special, *opstring, buf[BUFSIZ];
        struct statfs *mp;
        struct statfs *mp;
-       char *s1, *s2;
-       int len, n;
+       int fsi, fso, len, n;
+       char *cp, *s1, *s2, *special, *opstring, buf[BUFSIZ];
 
 
-       if (progname = rindex(*argv, '/'))
+       if (progname = strrchr(*argv, '/'))
                ++progname;
        else
                progname = *argv;
                ++progname;
        else
                progname = *argv;
@@ -182,21 +179,16 @@ main(argc, argv)
                Nflag++;
        }
 
                Nflag++;
        }
 
-       opstring = "F:NOS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:";
-       if (!mfs)
-               opstring += 2;          /* -F is mfs only */
-
+       opstring = mfs ?
+           "NT:a:b:c:d:e:f:i:m:o:s:" :
+           "NOS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:";
        while ((ch = getopt(argc, argv, opstring)) != EOF)
        while ((ch = getopt(argc, argv, opstring)) != EOF)
-               switch(ch) {
-               case 'F':
-                       if ((mntflags = atoi(optarg)) == 0)
-                               fatal("%s: bad mount flags", optarg);
-                       break;
+               switch (ch) {
                case 'N':
                case 'N':
-                       Nflag++;
+                       Nflag = 1;
                        break;
                case 'O':
                        break;
                case 'O':
-                       Oflag++;
+                       Oflag = 1;
                        break;
                case 'S':
                        if ((sectorsize = atoi(optarg)) <= 0)
                        break;
                case 'S':
                        if ((sectorsize = atoi(optarg)) <= 0)
@@ -209,7 +201,7 @@ main(argc, argv)
 #endif
                case 'a':
                        if ((maxcontig = atoi(optarg)) <= 0)
 #endif
                case 'a':
                        if ((maxcontig = atoi(optarg)) <= 0)
-                               fatal("%s: bad max contiguous blocks\n",
+                               fatal("%s: bad maximum contiguous blocks\n",
                                    optarg);
                        break;
                case 'b':
                                    optarg);
                        break;
                case 'b':
@@ -227,12 +219,12 @@ main(argc, argv)
                        break;
                case 'e':
                        if ((maxbpg = atoi(optarg)) <= 0)
                        break;
                case 'e':
                        if ((maxbpg = atoi(optarg)) <= 0)
-                               fatal("%s: bad blocks per file in a cyl group\n",
+               fatal("%s: bad blocks per file in a cylinder group\n",
                                    optarg);
                        break;
                case 'f':
                        if ((fsize = atoi(optarg)) <= 0)
                                    optarg);
                        break;
                case 'f':
                        if ((fsize = atoi(optarg)) <= 0)
-                               fatal("%s: bad frag size", optarg);
+                               fatal("%s: bad fragment size", optarg);
                        break;
                case 'i':
                        if ((density = atoi(optarg)) <= 0)
                        break;
                case 'i':
                        if ((density = atoi(optarg)) <= 0)
@@ -256,13 +248,16 @@ main(argc, argv)
                                    optarg);
                        break;
                case 'o':
                                    optarg);
                        break;
                case 'o':
-                       if (strcmp(optarg, "space") == 0)
-                               opt = FS_OPTSPACE;
-                       else if (strcmp(optarg, "time") == 0)
-                               opt = FS_OPTTIME;
-                       else
-                               fatal("%s: bad optimization preference %s",
-                                   optarg, "(options are `space' or `time')");
+                       if (mfs)
+                               getmntopts(optarg, mopts, &mntflags);
+                       else {
+                               if (strcmp(optarg, "space") == 0)
+                                       opt = FS_OPTSPACE;
+                               else if (strcmp(optarg, "time") == 0)
+                                       opt = FS_OPTTIME;
+                               else
+       fatal("%s: unknown optimization preference: use `space' or `time'.");
+                       }
                        break;
                case 'p':
                        if ((trackspares = atoi(optarg)) < 0)
                        break;
                case 'p':
                        if ((trackspares = atoi(optarg)) < 0)
@@ -271,7 +266,7 @@ main(argc, argv)
                        break;
                case 'r':
                        if ((rpm = atoi(optarg)) <= 0)
                        break;
                case 'r':
                        if ((rpm = atoi(optarg)) <= 0)
-                               fatal("%s: bad revs/minute\n", optarg);
+                               fatal("%s: bad revolutions/minute\n", optarg);
                        break;
                case 's':
                        if ((fssize = atoi(optarg)) <= 0)
                        break;
                case 's':
                        if ((fssize = atoi(optarg)) <= 0)
@@ -301,7 +296,7 @@ main(argc, argv)
                usage();
 
        special = argv[0];
                usage();
 
        special = argv[0];
-       cp = rindex(special, '/');
+       cp = strrchr(special, '/');
        if (cp == 0) {
                /*
                 * No path prefix; try /dev/r%s then /dev/%s.
        if (cp == 0) {
                /*
                 * No path prefix; try /dev/r%s then /dev/%s.
@@ -354,7 +349,7 @@ main(argc, argv)
                if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs)
                        printf("%s: %s: not a character-special device\n",
                            progname, special);
                if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs)
                        printf("%s: %s: not a character-special device\n",
                            progname, special);
-               cp = index(argv[0], '\0') - 1;
+               cp = strchr(argv[0], '\0') - 1;
                if (cp == 0 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp))
                        fatal("%s: can't figure out file system partition",
                            argv[0]);
                if (cp == 0 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp))
                        fatal("%s: can't figure out file system partition",
                            argv[0]);
@@ -480,7 +475,7 @@ main(argc, argv)
        if (realsectorsize != DEV_BSIZE)
                pp->p_size *= DEV_BSIZE / realsectorsize;
 #endif
        if (realsectorsize != DEV_BSIZE)
                pp->p_size *= DEV_BSIZE / realsectorsize;
 #endif
-       if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition)))
+       if (!Nflag && memcmp(pp, &oldpartition, sizeof(oldpartition)))
                rewritelabel(special, fso, lp);
        if (!Nflag)
                close(fso);
                rewritelabel(special, fso, lp);
        if (!Nflag)
                close(fso);
@@ -490,7 +485,12 @@ main(argc, argv)
                struct mfs_args args;
 
                sprintf(buf, "mfs:%d", getpid());
                struct mfs_args args;
 
                sprintf(buf, "mfs:%d", getpid());
-               args.name = buf;
+               args.fspec = buf;
+               args.export.ex_root = -2;
+               if (mntflags & MNT_RDONLY)
+                       args.export.ex_flags = MNT_EXRDONLY;
+               else
+                       args.export.ex_flags = 0;
                args.base = membase;
                args.size = fssize * sectorsize;
                if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0)
                args.base = membase;
                args.size = fssize * sectorsize;
                if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0)
@@ -525,8 +525,7 @@ getdisklabel(s, fd)
                        return (lp);
                }
 #endif
                        return (lp);
                }
 #endif
-               (void)fprintf(stderr,
-                   "%s: ioctl (GDINFO): %s\n", progname, strerror(errno));
+               warn("ioctl (GDINFO)");
                fatal(lmsg, s);
        }
        return (&lab);
                fatal(lmsg, s);
        }
        return (&lab);
@@ -544,8 +543,7 @@ rewritelabel(s, fd, lp)
        lp->d_checksum = 0;
        lp->d_checksum = dkcksum(lp);
        if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
        lp->d_checksum = 0;
        lp->d_checksum = dkcksum(lp);
        if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
-               (void)fprintf(stderr,
-                   "%s: ioctl (WDINFO): %s\n", progname, strerror(errno));
+               warn("ioctl (WDINFO)");
                fatal("%s: can't rewrite disk label", s);
        }
 #if vax
                fatal("%s: can't rewrite disk label", s);
        }
 #if vax
@@ -567,7 +565,7 @@ rewritelabel(s, fd, lp)
                cfd = open(specname, O_WRONLY);
                if (cfd < 0)
                        fatal("%s: %s", specname, strerror(errno));
                cfd = open(specname, O_WRONLY);
                if (cfd < 0)
                        fatal("%s: %s", specname, strerror(errno));
-               bzero(blk, sizeof(blk));
+               memset(blk, 0, sizeof(blk));
                *(struct disklabel *)(blk + LABELOFFSET) = *lp;
                alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
                for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
                *(struct disklabel *)(blk + LABELOFFSET) = *lp;
                alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
                for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
@@ -576,9 +574,7 @@ rewritelabel(s, fd, lp)
                                fatal("lseek to badsector area: %s",
                                    strerror(errno));
                        if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
                                fatal("lseek to badsector area: %s",
                                    strerror(errno));
                        if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
-                               fprintf(stderr,
-                                   "%s: alternate label %d write: %s\n",
-                                   progname, i/2, strerror(errno));
+                               warn("alternate label %d write", i/2);
                }
                close(cfd);
        }
                }
                close(cfd);
        }
@@ -602,11 +598,16 @@ fatal(fmt, va_alist)
 #else
        va_start(ap);
 #endif
 #else
        va_start(ap);
 #endif
-       fprintf(stderr, "%s: ", progname);
-       (void)vfprintf(stderr, fmt, ap);
+       if (fcntl(STDERR_FILENO, F_GETFL) < 0) {
+               openlog(progname, LOG_CONS, LOG_DAEMON);
+               vsyslog(LOG_ERR, fmt, ap);
+               closelog();
+       } else {
+               vwarnx(fmt, ap);
+       }
        va_end(ap);
        va_end(ap);
-       putc('\n', stderr);
        exit(1);
        exit(1);
+       /*NOTREACHED*/
 }
 
 usage()
 }
 
 usage()