BSD 4_4_Lite1 release
[unix-history] / usr / src / sbin / newfs / newfs.c
index f7293de..c7748e8 100644 (file)
@@ -1,5 +1,5 @@
 /*
 /*
- * Copyright (c) 1983, 1989, 1993
+ * Copyright (c) 1983, 1989, 1993, 1994
  *     The Regents of the University of California.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  *     The Regents of the University of California.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)newfs.c    8.1 (Berkeley) 6/5/93";
+static char sccsid[] = "@(#)newfs.c    8.8 (Berkeley) 4/18/94";
 #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 */
 
@@ -50,21 +50,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, ...);
@@ -92,19 +103,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
@@ -169,7 +167,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
@@ -180,6 +178,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[];
@@ -192,11 +191,9 @@ 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, '/'))
                ++progname;
 
        if (progname = rindex(*argv, '/'))
                ++progname;
@@ -208,21 +205,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)
@@ -235,7 +227,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':
@@ -253,12 +245,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)
@@ -282,13 +274,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)
@@ -297,7 +292,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)
@@ -452,7 +447,7 @@ main(argc, argv)
         * transfer size permitted by the controller or buffering.
         */
        if (maxcontig == 0)
         * transfer size permitted by the controller or buffering.
         */
        if (maxcontig == 0)
-               maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize);
+               maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize - 1);
        if (density == 0)
                density = NFPI * fsize;
        if (minfree < MINFREE && opt != FS_OPTSPACE) {
        if (density == 0)
                density = NFPI * fsize;
        if (minfree < MINFREE && opt != FS_OPTSPACE) {
@@ -513,7 +508,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)
@@ -548,8 +548,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);
@@ -567,8 +566,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
@@ -599,9 +597,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);
        }
@@ -625,11 +621,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()