date and time created 89/04/02 12:30:51 by bostic
[unix-history] / usr / src / sbin / dump / main.c
index 69c2bd1..541d302 100644 (file)
@@ -1,27 +1,48 @@
-static char *sccsid = "@(#)main.c      1.4 (Berkeley) %G%";
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
+#ifndef lint
+static char sccsid[] = "@(#)main.c     5.6 (Berkeley) %G%";
+#endif not lint
+
 #include "dump.h"
 
 int    notify = 0;     /* notify operator flag */
 int    blockswritten = 0;      /* number of blocks written on current tape */
 int    tapeno = 0;     /* current tape number */
 #include "dump.h"
 
 int    notify = 0;     /* notify operator flag */
 int    blockswritten = 0;      /* number of blocks written on current tape */
 int    tapeno = 0;     /* current tape number */
-int    density = 160;  /* density in 0.1" units */
+int    density = 0;    /* density in bytes/0.1" */
+int    ntrec = NTREC;  /* # tape blocks in each tape record */
+int    cartridge = 0;  /* Assume non-cartridge tape */
+long   dev_bsize = 1;  /* recalculated below */
+#ifdef RDUMP
+char   *host;
+#endif
+int    anydskipped;    /* set true in mark() if any directories are skipped */
+                       /* this lets us avoid map pass 2 in some cases */
 
 main(argc, argv)
        int     argc;
        char    *argv[];
 {
        char            *arg;
 
 main(argc, argv)
        int     argc;
        char    *argv[];
 {
        char            *arg;
-       register        i;
+       int             bflag = 0, i;
        float           fetapes;
        register        struct  fstab   *dt;
 
        time(&(spcl.c_date));
 
        float           fetapes;
        register        struct  fstab   *dt;
 
        time(&(spcl.c_date));
 
-       tsize = 2300L*12L*10L;
+       tsize = 0;      /* Default later, based on 'c' option for cart tapes */
        tape = TAPE;
        disk = DISK;
        increm = NINCREM;
        tape = TAPE;
        disk = DISK;
        increm = NINCREM;
-
+       temp = TEMP;
+       if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0) {
+               msg("TP_BSIZE must be a multiple of DEV_BSIZE\n");
+               dumpabort();
+       }
        incno = '9';
        uflag = 0;
        arg = "u";
        incno = '9';
        uflag = 0;
        arg = "u";
@@ -43,11 +64,6 @@ main(argc, argv)
                exit(0);                /* do nothing else */
                break;
 
                exit(0);                /* do nothing else */
                break;
 
-       case 'J':                       /* update old to new */
-               o_nconvert();
-               exit(0);                /* do nothing else */
-               break;
-
        case 'f':                       /* output file */
                if(argc > 1) {
                        argv++;
        case 'f':                       /* output file */
                if(argc > 1) {
                        argv++;
@@ -61,6 +77,8 @@ main(argc, argv)
                        argv++;
                        argc--;
                        density = atoi(*argv) / 10;
                        argv++;
                        argc--;
                        density = atoi(*argv) / 10;
+                       if (density >= 625 && !bflag)
+                               ntrec = HIGHDENSITYTREC;
                }
                break;
 
                }
                break;
 
@@ -73,6 +91,19 @@ main(argc, argv)
                }
                break;
 
                }
                break;
 
+       case 'b':                       /* blocks per tape write */
+               if(argc > 1) {
+                       argv++;
+                       argc--;
+                       bflag++;
+                       ntrec = atol(*argv);
+               }
+               break;
+
+       case 'c':                       /* Tape is cart. not 9-track */
+               cartridge++;
+               break;
+
        case '0':                       /* dump level */
        case '1':
        case '2':
        case '0':                       /* dump level */
        case '1':
        case '2':
@@ -95,7 +126,7 @@ main(argc, argv)
                break;
 
        default:
                break;
 
        default:
-               printf("bad key '%c%'\n", arg[-1]);
+               fprintf(stderr, "bad key '%c%'\n", arg[-1]);
                Exit(X_ABORT);
        }
        if(argc > 1) {
                Exit(X_ABORT);
        }
        if(argc > 1) {
@@ -103,7 +134,38 @@ main(argc, argv)
                argc--;
                disk = *argv;
        }
                argc--;
                disk = *argv;
        }
+       if (strcmp(tape, "-") == 0) {
+               pipeout++;
+               tape = "standard output";
+       }
+
+       /*
+        * Determine how to default tape size and density
+        *
+        *              density                         tape size
+        * 9-track      1600 bpi (160 bytes/.1")        2300 ft.
+        * 9-track      6250 bpi (625 bytes/.1")        2300 ft.
+        * cartridge    8000 bpi (100 bytes/.1")        1700 ft. (450*4 - slop)
+        */
+       if (density == 0)
+               density = cartridge ? 100 : 160;
+       if (tsize == 0)
+               tsize = cartridge ? 1700L*120L : 2300L*120L;
 
 
+#ifdef RDUMP
+       { char *index();
+         host = tape;
+         tape = index(host, ':');
+         if (tape == 0) {
+               msg("need keyletter ``f'' and device ``host:tape''\n");
+               exit(1);
+         }
+         *tape++ = 0;
+         if (rmthost(host) == 0)
+               exit(X_ABORT);
+       }
+       setuid(getuid());       /* rmthost() is the only reason to be setuid */
+#endif
        if (signal(SIGHUP, sighup) == SIG_IGN)
                signal(SIGHUP, SIG_IGN);
        if (signal(SIGTRAP, sigtrap) == SIG_IGN)
        if (signal(SIGHUP, sighup) == SIG_IGN)
                signal(SIGHUP, SIG_IGN);
        if (signal(SIGTRAP, sigtrap) == SIG_IGN)
@@ -130,76 +192,133 @@ main(argc, argv)
         *      the file system name with or without the leading '/'.
         */
        dt = fstabsearch(disk);
         *      the file system name with or without the leading '/'.
         */
        dt = fstabsearch(disk);
-       if (dt != 0)
+       if (dt != 0) {
                disk = rawname(dt->fs_spec);
                disk = rawname(dt->fs_spec);
+               strncpy(spcl.c_dev, dt->fs_spec, NAMELEN);
+               strncpy(spcl.c_filesys, dt->fs_file, NAMELEN);
+       } else {
+               strncpy(spcl.c_dev, disk, NAMELEN);
+               strncpy(spcl.c_filesys, "an unlisted file system", NAMELEN);
+       }
+       strcpy(spcl.c_label, "none");
+       gethostname(spcl.c_host, NAMELEN);
+       spcl.c_level = incno - '0';
+       spcl.c_type = TS_TAPE;
        getitime();             /* /etc/dumpdates snarfed */
 
        msg("Date of this level %c dump: %s\n", incno, prdate(spcl.c_date));
        getitime();             /* /etc/dumpdates snarfed */
 
        msg("Date of this level %c dump: %s\n", incno, prdate(spcl.c_date));
-       msg("Date of last level %c dump: %s\n", incno, prdate(spcl.c_ddate));
+       msg("Date of last level %c dump: %s\n",
+               lastincno, prdate(spcl.c_ddate));
        msg("Dumping %s ", disk);
        if (dt != 0)
                msgtail("(%s) ", dt->fs_file);
        msg("Dumping %s ", disk);
        if (dt != 0)
                msgtail("(%s) ", dt->fs_file);
+#ifdef RDUMP
+       msgtail("to %s on host %s\n", tape, host);
+#else
        msgtail("to %s\n", tape);
        msgtail("to %s\n", tape);
+#endif
 
        fi = open(disk, 0);
        if (fi < 0) {
                msg("Cannot open %s\n", disk);
                Exit(X_ABORT);
        }
 
        fi = open(disk, 0);
        if (fi < 0) {
                msg("Cannot open %s\n", disk);
                Exit(X_ABORT);
        }
-       CLR(clrmap);
-       CLR(dirmap);
-       CLR(nodmap);
        esize = 0;
        esize = 0;
+       sblock = (struct fs *)buf;
+       sync();
+       bread(SBOFF, sblock, SBSIZE);
+       if (sblock->fs_magic != FS_MAGIC) {
+               msg("bad sblock magic number\n");
+               dumpabort();
+       }
+       dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
+       msiz = roundup(howmany(sblock->fs_ipg * sblock->fs_ncg, NBBY),
+               TP_BSIZE);
+       clrmap = (char *)calloc(msiz, sizeof(char));
+       dirmap = (char *)calloc(msiz, sizeof(char));
+       nodmap = (char *)calloc(msiz, sizeof(char));
 
 
+       anydskipped = 0;
        msg("mapping (Pass I) [regular files]\n");
        msg("mapping (Pass I) [regular files]\n");
-       pass(mark, (short *)NULL);              /* mark updates esize */
+       pass(mark, (char *)NULL);               /* mark updates esize */
 
 
-       do {
+       if (anydskipped) {
+               do {
+                       msg("mapping (Pass II) [directories]\n");
+                       nadded = 0;
+                       pass(add, dirmap);
+               } while(nadded);
+       } else                          /* keep the operators happy */
                msg("mapping (Pass II) [directories]\n");
                msg("mapping (Pass II) [directories]\n");
-               nadded = 0;
-               pass(add, dirmap);
-       } while(nadded);
 
        bmapest(clrmap);
        bmapest(nodmap);
 
 
        bmapest(clrmap);
        bmapest(nodmap);
 
-       fetapes =
-               (        esize          /* blocks */
-                       *FSIZE          /* bytes / block */
-                       *(1.0/density)  /* 0.1" / byte */
+       if (cartridge) {
+               /* Estimate number of tapes, assuming streaming stops at
+                  the end of each block written, and not in mid-block.
+                  Assume no erroneous blocks; this can be compensated for
+                  with an artificially low tape size. */
+               fetapes = 
+               (         esize         /* blocks */
+                       * TP_BSIZE      /* bytes/block */
+                       * (1.0/density) /* 0.1" / byte */
                  +
                  +
-                        esize          /* blocks */
-                       *(1.0/NTREC)    /* IRG's / block */
-                       *7              /* 0.1" / IRG */
-               ) * (1.0 / tsize )      /* tape / 0.1" */
-       ;
+                         esize         /* blocks */
+                       * (1.0/ntrec)   /* streaming-stops per block */
+                       * 15.48         /* 0.1" / streaming-stop */
+               ) * (1.0 / tsize );     /* tape / 0.1" */
+       } else {
+               /* Estimate number of tapes, for old fashioned 9-track tape */
+               int tenthsperirg = (density == 625) ? 3 : 7;
+               fetapes =
+               (         esize         /* blocks */
+                       * TP_BSIZE      /* bytes / block */
+                       * (1.0/density) /* 0.1" / byte */
+                 +
+                         esize         /* blocks */
+                       * (1.0/ntrec)   /* IRG's / block */
+                       * tenthsperirg  /* 0.1" / IRG */
+               ) * (1.0 / tsize );     /* tape / 0.1" */
+       }
        etapes = fetapes;               /* truncating assignment */
        etapes++;
        etapes = fetapes;               /* truncating assignment */
        etapes++;
-       /*
-        *      esize is typically about 5% too low; we frob it here
-        */
-       esize += ((5*esize)/100);
+       /* count the nodemap on each additional tape */
+       for (i = 1; i < etapes; i++)
+               bmapest(nodmap);
+       esize += i + 10;        /* headers + 10 trailer blocks */
        msg("estimated %ld tape blocks on %3.2f tape(s).\n", esize, fetapes);
 
        msg("estimated %ld tape blocks on %3.2f tape(s).\n", esize, fetapes);
 
+       alloctape();                    /* Allocate tape buffer */
+
        otape();                        /* bitmap is the first to tape write */
        time(&(tstart_writing));
        bitmap(clrmap, TS_CLRI);
 
        msg("dumping (Pass III) [directories]\n");
        otape();                        /* bitmap is the first to tape write */
        time(&(tstart_writing));
        bitmap(clrmap, TS_CLRI);
 
        msg("dumping (Pass III) [directories]\n");
-       pass(dump, dirmap);
+       pass(dirdump, dirmap);
 
        msg("dumping (Pass IV) [regular files]\n");
        pass(dump, nodmap);
 
        spcl.c_type = TS_END;
 
        msg("dumping (Pass IV) [regular files]\n");
        pass(dump, nodmap);
 
        spcl.c_type = TS_END;
-       for(i=0; i<NTREC; i++)
+#ifndef RDUMP
+       for(i=0; i<ntrec; i++)
                spclrec();
                spclrec();
+#endif
        msg("DUMP: %ld tape blocks on %d tape(s)\n",spcl.c_tapea,spcl.c_volume);
        msg("DUMP IS DONE\n");
 
        putitime();
        msg("DUMP: %ld tape blocks on %d tape(s)\n",spcl.c_tapea,spcl.c_volume);
        msg("DUMP IS DONE\n");
 
        putitime();
-       close(to);
+#ifndef RDUMP
+       if (!pipeout) {
+               close(to);
+               rewind();
+       }
+#else
+       tflush(1);
        rewind();
        rewind();
+#endif
        broadcast("DUMP IS DONE!\7\7\n");
        Exit(X_FINOK);
 }
        broadcast("DUMP IS DONE!\7\7\n");
        Exit(X_FINOK);
 }
@@ -214,6 +333,10 @@ int        sigterm(){      msg("SIGTERM()  try rewriting\n"); sigAbort();}
 
 sigAbort()
 {
 
 sigAbort()
 {
+       if (pipeout) {
+               msg("Unknown signal, cannot recover\n");
+               dumpabort();
+       }
        msg("Rewriting attempted as response to unknown signal.\n");
        fflush(stderr);
        fflush(stdout);
        msg("Rewriting attempted as response to unknown signal.\n");
        fflush(stderr);
        fflush(stdout);