Imported Debian patch 2.5.2p1-1
[debian/amanda] / server-src / driver.c
index 1d3bc767d2550f45032dcf9dbc30e4b50dd52770..d5f265c0d664f084d16d3eccac23c29927acd6b6 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
- * Copyright (c) 1991-2000 University of Maryland at College Park
+ * Copyright (c) 1991-1998 University of Maryland at College Park
  * All Rights Reserved.
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
@@ -24,7 +24,7 @@
  * file named AUTHORS, in the root directory of this distribution.
  */
 /*
- * $Id: driver.c,v 1.58.2.31.2.8.2.21 2004/04/26 15:02:47 martinea Exp $
+ * $Id: driver.c,v 1.198 2006/08/24 01:57:16 paddy_s Exp $
  *
  * controlling process for the Amanda backup system
  */
@@ -38,6 +38,7 @@
 #include "clock.h"
 #include "conffile.h"
 #include "diskfile.h"
+#include "event.h"
 #include "holding.h"
 #include "infofile.h"
 #include "logfile.h"
 #include "driverio.h"
 #include "server_util.h"
 
-disklist_t waitq, runq, tapeq, roomq;
-int pending_aborts, inside_dump_to_tape;
-disk_t *taper_disk;
-int degraded_mode;
-unsigned long reserved_space;
-unsigned long total_disksize;
-char *dumper_program;
-int  inparallel;
-int nodump = 0;
-long tape_length, tape_left = 0;
-int conf_taperalgo;
-host_t *flushhost = NULL;
-
-int client_constrained P((disk_t *dp));
-int sort_by_priority_reversed P((disk_t *a, disk_t *b));
-int sort_by_time P((disk_t *a, disk_t *b));
-int start_some_dumps P((disklist_t *rq));
-void dump_schedule P((disklist_t *qp, char *str));
-void start_degraded_mode P((disklist_t *queuep));
-void handle_taper_result P((void));
-dumper_t *idle_dumper P((void));
-int some_dumps_in_progress P((void));
-int num_busy_dumpers P((void));
-dumper_t *lookup_dumper P((int fd));
-void handle_dumper_result P((int fd));
-void read_flush P((disklist_t *tapeqp));
-void read_schedule P((disklist_t *waitqp, disklist_t *runqp));
-int free_kps P((interface_t *ip));
-void interface_state P((char *time_str));
-void allocate_bandwidth P((interface_t *ip, int kps));
-void deallocate_bandwidth P((interface_t *ip, int kps));
-unsigned long free_space P((void));
-assignedhd_t **find_diskspace P((unsigned long size, int *cur_idle, assignedhd_t *preferred));
-char *diskname2filename P((char *dname));
-int assign_holdingdisk P((assignedhd_t **holdp, disk_t *diskp));
-static void adjust_diskspace P((disk_t *diskp, cmd_t cmd));
-static void delete_diskspace P((disk_t *diskp));
-assignedhd_t **build_diskspace P((char *destname));
-void holdingdisk_state P((char *time_str));
-int dump_to_tape P((disk_t *dp));
-int queue_length P((disklist_t q));
-void short_dump_state P((void));
-void dump_state P((char *str));
-void startaflush P((void));
-int main P((int main_argc, char **main_argv));
-
+#define driver_debug(i,x) do {         \
+       if ((i) <= debug_driver) {      \
+           dbprintf(x);                \
+       }                               \
+} while (0)
+
+#define hold_debug(i,x) do {           \
+       if ((i) <= debug_holding) {     \
+           dbprintf(x);                \
+       }                               \
+} while (0)
+
+static disklist_t waitq, runq, tapeq, roomq;
+static int pending_aborts;
+static disk_t *taper_disk;
+static int degraded_mode;
+static off_t reserved_space;
+static off_t total_disksize;
+static char *dumper_program;
+static char *chunker_program;
+static int  inparallel;
+static int nodump = 0;
+static off_t tape_length = (off_t)0;
+static off_t tape_left = (off_t)0;
+static int current_tape = 1;
+static int conf_taperalgo;
+static int conf_runtapes;
+static time_t sleep_time;
 static int idle_reason;
-char *datestamp;
+static char *driver_timestamp;
+static char *hd_driver_timestamp;
+static am_host_t *flushhost = NULL;
+static int need_degraded=0;
+
+static event_handle_t *dumpers_ev_time = NULL;
+static event_handle_t *schedule_ev_read = NULL;
+
+static int wait_children(int count);
+static void wait_for_children(void);
+static void allocate_bandwidth(interface_t *ip, unsigned long kps);
+static int assign_holdingdisk(assignedhd_t **holdp, disk_t *diskp);
+static void adjust_diskspace(disk_t *diskp, cmd_t cmd);
+static void delete_diskspace(disk_t *diskp);
+static assignedhd_t **build_diskspace(char *destname);
+static int client_constrained(disk_t *dp);
+static void deallocate_bandwidth(interface_t *ip, unsigned long kps);
+static void dump_schedule(disklist_t *qp, char *str);
+static int dump_to_tape(disk_t *dp);
+static assignedhd_t **find_diskspace(off_t size, int *cur_idle,
+                                       assignedhd_t *preferred);
+static unsigned long free_kps(interface_t *ip);
+static off_t free_space(void);
+static void dumper_result(disk_t *dp);
+static void handle_dumper_result(void *);
+static void handle_chunker_result(void *);
+static void handle_dumpers_time(void *);
+static void handle_taper_result(void *);
+static void holdingdisk_state(char *time_str);
+static dumper_t *idle_dumper(void);
+static void interface_state(char *time_str);
+static int queue_length(disklist_t q);
+static disklist_t read_flush(void);
+static void read_schedule(void *cookie);
+static void short_dump_state(void);
+static void startaflush(void);
+static void start_degraded_mode(disklist_t *queuep);
+static void start_some_dumps(disklist_t *rq);
+static void continue_port_dumps(void);
+static void update_failed_dump_to_tape(disk_t *);
+#if 0
+static void dump_state(const char *str);
+#endif
+int main(int main_argc, char **main_argv);
 
-char *idle_strings[] = {
+static const char *idle_strings[] = {
 #define NOT_IDLE               0
     "not-idle",
-#define IDLE_START_WAIT                1
-    "start-wait",
-#define IDLE_NO_DUMPERS                2
+#define IDLE_NO_DUMPERS                1
     "no-dumpers",
+#define IDLE_START_WAIT                2
+    "start-wait",
 #define IDLE_NO_HOLD           3
     "no-hold",
 #define IDLE_CLIENT_CONSTRAINED        4
@@ -116,19 +142,14 @@ char *idle_strings[] = {
     "taper-wait",
 };
 
-#define SLEEP_MAX              (24*3600)
-struct timeval sleep_time = { SLEEP_MAX, 0 };
-/* enabled if any disks are in start-wait: */
-int any_delayed_disk = 0;
-
-int main(main_argc, main_argv)
-     int main_argc;
-     char **main_argv;
+int
+main(
+    int                main_argc,
+    char **    main_argv)
 {
-    disklist_t *origqp;
+    disklist_t origq;
     disk_t *diskp;
-    fd_set selectset;
-    int fd, dsk;
+    int dsk;
     dumper_t *dumper;
     char *newdir = NULL;
     generic_fs_stats_t fs;
@@ -142,22 +163,25 @@ int main(main_argc, main_argv)
     int result_argc;
     char *result_argv[MAX_ARGS+1];
     char *taper_program;
-    amwait_t retstat;
     char *conf_tapetype;
     tapetype_t *tape;
+    char *line;
+    int    new_argc,   my_argc;
+    char **new_argv, **my_argv;
+    char hostname[1025];
 
-    for(fd = 3; fd < FD_SETSIZE; fd++) {
-       /*
-        * Make sure nobody spoofs us with a lot of extra open files
-        * that would cause an open we do to get a very high file
-        * descriptor, which in turn might be used as an index into
-        * an array (e.g. an fd_set).
-        */
-       close(fd);
-    }
+    safe_fd(-1, 0);
+
+    setvbuf(stdout, (char *)NULL, (int)_IOLBF, 0);
+    setvbuf(stderr, (char *)NULL, (int)_IOLBF, 0);
 
     set_pname("driver");
 
+    dbopen(DBG_SUBDIR_SERVER);
+
+    atexit(wait_for_children);
+
+    /* Don't die when child closes pipe */
     signal(SIGPIPE, SIG_IGN);
 
     malloc_size_1 = malloc_inuse(&malloc_hist_1);
@@ -166,25 +190,30 @@ int main(main_argc, main_argv)
     set_logerror(logerror);
 
     startclock();
-    FD_ZERO(&readset);
+
+    parse_conf(main_argc, main_argv, &new_argc, &new_argv);
+    my_argc = new_argc;
+    my_argv = new_argv;
 
     printf("%s: pid %ld executable %s version %s\n",
-          get_pname(), (long) getpid(), main_argv[0], version());
+          get_pname(), (long) getpid(), my_argv[0], version());
 
-    if (main_argc > 1) {
-       config_name = stralloc(main_argv[1]);
+    if (my_argc > 1) {
+       config_name = stralloc(my_argv[1]);
        config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
-       if(main_argc > 2) {
-           if(strncmp(main_argv[2], "nodump", 6) == 0) {
+       if(my_argc > 2) {
+           if(strncmp(my_argv[2], "nodump", 6) == 0) {
                nodump = 1;
            }
        }
 
     } else {
+
        char my_cwd[STR_SIZE];
 
-       if (getcwd(my_cwd, sizeof(my_cwd)) == NULL) {
+       if (getcwd(my_cwd, SIZEOF(my_cwd)) == NULL) {
            error("cannot determine current working directory");
+           /*NOTREACHED*/
        }
        config_dir = stralloc2(my_cwd, "/");
        if ((config_name = strrchr(my_cwd, '/')) != NULL) {
@@ -197,28 +226,66 @@ int main(main_argc, main_argv)
     conffile = stralloc2(config_dir, CONFFILE_NAME);
     if(read_conffile(conffile)) {
        error("errors processing config file \"%s\"", conffile);
+       /*NOTREACHED*/
     }
     amfree(conffile);
 
-    amfree(datestamp);
-    datestamp = construct_datestamp(NULL);
-    log_add(L_START,"date %s", datestamp);
+    dbrename(config_name, DBG_SUBDIR_SERVER);
+
+    report_bad_conf_arg();
+
+    amfree(driver_timestamp);
+    /* read timestamp from stdin */
+    while ((line = agets(stdin)) != NULL) {
+       if (line[0] != '\0')
+           break;
+       amfree(line);
+    }
+    if ( line == NULL ) {
+      error("Did not get DATE line from planner");
+      /*NOTREACHED*/
+    }
+    driver_timestamp = alloc(15);
+    strncpy(driver_timestamp, &line[5], 14);
+    driver_timestamp[14] = '\0';
+    amfree(line);
+    log_add(L_START,"date %s", driver_timestamp);
+
+    gethostname(hostname, SIZEOF(hostname));
+    log_add(L_STATS,"hostname %s", hostname);
+
+    /* check that we don't do many dump in a day and usetimestamps is off */
+    if(strlen(driver_timestamp) == 8) {
+       if (!nodump) {
+           char *conf_logdir = getconf_str(CNF_LOGDIR);
+           char *logfile    = vstralloc(conf_logdir, "/log.",
+                                        driver_timestamp, ".0", NULL);
+           char *oldlogfile = vstralloc(conf_logdir, "/oldlog/log.",
+                                        driver_timestamp, ".0", NULL);
+           if(access(logfile, F_OK) == 0 || access(oldlogfile, F_OK) == 0) {
+               log_add(L_WARNING, "WARNING: This is not the first amdump run today. Enable the usetimestamps option in the configuration file if you want to run amdump more than once per calendar day.");
+           }
+           amfree(oldlogfile);
+           amfree(logfile);
+       }
+       hd_driver_timestamp = construct_timestamp(NULL);
+    }
+    else {
+       hd_driver_timestamp = stralloc(driver_timestamp);
+    }
 
     taper_program = vstralloc(libexecdir, "/", "taper", versionsuffix(), NULL);
     dumper_program = vstralloc(libexecdir, "/", "dumper", versionsuffix(),
                               NULL);
+    chunker_program = vstralloc(libexecdir, "/", "chunker", versionsuffix(),
+                              NULL);
 
-    conf_taperalgo = getconf_int(CNF_TAPERALGO);
+    conf_taperalgo = getconf_taperalgo(CNF_TAPERALGO);
     conf_tapetype = getconf_str(CNF_TAPETYPE);
+    conf_runtapes = getconf_int(CNF_RUNTAPES);
     tape = lookup_tapetype(conf_tapetype);
-    tape_length = tape->length;
-    printf("driver: tape size %ld\n", tape_length);
-
-    /* taper takes a while to get going, so start it up right away */
-
-    init_driverio();
-    startup_tape_process(taper_program);
-    taper_cmd(START_TAPER, datestamp, NULL, 0, NULL);
+    tape_length = tapetype_get_length(tape);
+    printf("driver: tape size " OFF_T_FMT "\n", (OFF_T_FMT_TYPE)tape_length);
 
     /* start initializing: read in databases */
 
@@ -228,8 +295,9 @@ int main(main_argc, main_argv)
     } else {
        conf_diskfile = stralloc2(config_dir, conf_diskfile);
     }
-    if((origqp = read_diskfile(conf_diskfile)) == NULL) {
+    if (read_diskfile(conf_diskfile, &origq) < 0) {
        error("could not load disklist \"%s\"", conf_diskfile);
+       /*NOTREACHED*/
     }
     amfree(conf_diskfile);
 
@@ -237,66 +305,80 @@ int main(main_argc, main_argv)
 
     inparallel = getconf_int(CNF_INPARALLEL);
 
-    reserve = getconf_int(CNF_RESERVE);
+    reserve = (unsigned long)getconf_int(CNF_RESERVE);
 
-    total_disksize = 0;
+    total_disksize = (off_t)0;
     for(hdp = getconf_holdingdisks(), dsk = 0; hdp != NULL; hdp = hdp->next, dsk++) {
-       hdp->up = (void *)alloc(sizeof(holdalloc_t));
+       hdp->up = (void *)alloc(SIZEOF(holdalloc_t));
        holdalloc(hdp)->allocated_dumpers = 0;
-       holdalloc(hdp)->allocated_space = 0L;
+       holdalloc(hdp)->allocated_space = (off_t)0;
 
-       if(get_fs_stats(hdp->diskdir, &fs) == -1
-          || access(hdp->diskdir, W_OK) == -1) {
+       if(get_fs_stats(holdingdisk_get_diskdir(hdp), &fs) == -1
+          || access(holdingdisk_get_diskdir(hdp), W_OK) == -1) {
            log_add(L_WARNING, "WARNING: ignoring holding disk %s: %s\n",
-                   hdp->diskdir, strerror(errno));
+                   holdingdisk_get_diskdir(hdp), strerror(errno));
            hdp->disksize = 0L;
            continue;
        }
 
-       if(fs.avail != -1) {
-           if(hdp->disksize > 0) {
+       if(fs.avail != (off_t)-1) {
+           if(hdp->disksize > (off_t)0) {
                if(hdp->disksize > fs.avail) {
                    log_add(L_WARNING,
-                           "WARNING: %s: %ld KB requested, but only %ld KB available.",
-                           hdp->diskdir, hdp->disksize, fs.avail);
+                           "WARNING: %s: " OFF_T_FMT " KB requested, "
+                           "but only " OFF_T_FMT " KB available.",
+                           holdingdisk_get_diskdir(hdp),
+                           (OFF_T_FMT_TYPE)hdp->disksize,
+                           (OFF_T_FMT_TYPE)fs.avail);
                            hdp->disksize = fs.avail;
                }
            }
-           else if(fs.avail + hdp->disksize < 0) {
+           else if((fs.avail + hdp->disksize) < (off_t)0) {
                log_add(L_WARNING,
-                       "WARNING: %s: not %ld KB free.",
-                       hdp->diskdir, -hdp->disksize);
-               hdp->disksize = 0L;
+                       "WARNING: %s: not " OFF_T_FMT " KB free.",
+                       holdingdisk_get_diskdir(hdp), -hdp->disksize);
+               hdp->disksize = (off_t)0;
                continue;
            }
            else
                hdp->disksize += fs.avail;
        }
 
-       printf("driver: adding holding disk %d dir %s size %ld\n",
-              dsk, hdp->diskdir, hdp->disksize);
+       printf("driver: adding holding disk %d dir %s size "
+               OFF_T_FMT " chunksize " OFF_T_FMT "\n",
+              dsk, holdingdisk_get_diskdir(hdp),
+              (OFF_T_FMT_TYPE)hdp->disksize,
+              (OFF_T_FMT_TYPE)(holdingdisk_get_chunksize(hdp)));
 
        newdir = newvstralloc(newdir,
-                             hdp->diskdir, "/", datestamp,
+                             holdingdisk_get_diskdir(hdp), "/", hd_driver_timestamp,
                              NULL);
-        if(!mkholdingdir(newdir)) {
-           hdp->disksize = 0L;
+       if(!mkholdingdir(newdir)) {
+           hdp->disksize = (off_t)0;
        }
        total_disksize += hdp->disksize;
     }
 
-    reserved_space = total_disksize * (reserve / 100.0);
+    reserved_space = total_disksize * (off_t)(reserve / 100);
 
-    printf("reserving %ld out of %ld for degraded-mode dumps\n",
-               reserved_space, free_space());
+    printf("reserving " OFF_T_FMT " out of " OFF_T_FMT
+          " for degraded-mode dumps\n",
+          (OFF_T_FMT_TYPE)reserved_space, (OFF_T_FMT_TYPE)free_space());
 
     amfree(newdir);
 
     if(inparallel > MAX_DUMPERS) inparallel = MAX_DUMPERS;
 
-    /* fire up the dumpers now while we are waiting */
+    /* taper takes a while to get going, so start it up right away */
 
-    if(!nodump) startup_dump_processes(dumper_program, inparallel);
+    init_driverio();
+    if(conf_runtapes > 0) {
+        startup_tape_process(taper_program);
+        taper_cmd(START_TAPER, driver_timestamp, NULL, 0, NULL);
+    }
+
+    /* fire up the dumpers now while we are waiting */
+    if(!nodump) startup_dump_processes(dumper_program, inparallel, driver_timestamp);
 
     /*
      * Read schedule from stdin.  Usually, this is a pipe from planner,
@@ -306,77 +388,60 @@ int main(main_argc, main_argv)
      * in parallel with the planner.
      */
 
-    waitq = *origqp;
-    tapeq.head = tapeq.tail = NULL;
-    roomq.head = roomq.tail = NULL;
-    runq.head = runq.tail = NULL;
+    runq.head = NULL;
+    runq.tail = NULL;
+    waitq = origq;
+    tapeq = read_flush();
 
-    read_flush(&tapeq);
-    if(!nodump) read_schedule(&waitq, &runq);
+    roomq.head = roomq.tail = NULL;
 
     log_add(L_STATS, "startup time %s", walltime_str(curclock()));
 
-    printf("driver: start time %s inparallel %d bandwidth %d diskspace %lu",
-          walltime_str(curclock()), inparallel, free_kps((interface_t *)0),
-          free_space());
+    printf("driver: start time %s inparallel %d bandwidth %lu diskspace "
+          OFF_T_FMT " ", walltime_str(curclock()), inparallel,
+          free_kps((interface_t *)0), (OFF_T_FMT_TYPE)free_space());
     printf(" dir %s datestamp %s driver: drain-ends tapeq %s big-dumpers %s\n",
-          "OBSOLETE", datestamp, taperalgo2str(conf_taperalgo),
+          "OBSOLETE", driver_timestamp, taperalgo2str(conf_taperalgo),
           getconf_str(CNF_DUMPORDER));
     fflush(stdout);
 
     /* ok, planner is done, now lets see if the tape is ready */
 
-    cmd = getresult(taper, 1, &result_argc, result_argv, MAX_ARGS+1);
+    if(conf_runtapes > 0) {
+       cmd = getresult(taper, 1, &result_argc, result_argv, MAX_ARGS+1);
 
-    if(cmd != TAPER_OK) {
-       /* no tape, go into degraded mode: dump to holding disk */
-       start_degraded_mode(&runq);
-       FD_CLR(taper,&readset);
+       if(cmd != TAPER_OK) {
+           /* no tape, go into degraded mode: dump to holding disk */
+           need_degraded=1;
+       }
+    }
+    else {
+       need_degraded=1;
     }
 
     tape_left = tape_length;
     taper_busy = 0;
     taper_disk = NULL;
-    startaflush();
-
-    while(start_some_dumps(&runq) || some_dumps_in_progress() ||
-         any_delayed_disk) {
-
-       short_dump_state();
-
-       /* wait for results */
+    taper_ev_read = NULL;
+    if(!need_degraded) startaflush();
 
-       memcpy(&selectset, &readset, sizeof(fd_set));
-       if(select(maxfd+1, (SELECT_ARG_TYPE *)(&selectset),
-                 NULL, NULL, &sleep_time) == -1)
-           error("select: %s", strerror(errno));
+    if(!nodump)
+       schedule_ev_read = event_register((event_id_t)0, EV_READFD, read_schedule, NULL);
 
-       /* handle any results that have come in */
-
-       for(fd = 0; fd <= maxfd; fd++) {
-           /*
-            * The first pass through the following loop, we have
-            * data ready for areads (called by getresult, called by
-            * handle_.*_result).  But that may read more than one record,
-            * so we need to keep processing as long as areads has data.
-            * We will get control back after each record and the buffer
-            * will go empty (indicated by areads_dataready(fd) == 0)
-            * after the last one available has been processed.
-            */
-           while(FD_ISSET(fd, &selectset) || areads_dataready(fd) > 0) {
-               if(fd == taper) handle_taper_result();
-               else handle_dumper_result(fd);
-               FD_CLR(fd, &selectset);
-           }
-       }
-
-    }
+    short_dump_state();
+    event_loop(0);
 
     /* handle any remaining dumps by dumping directly to tape, if possible */
 
-    while(!empty(runq)) {
+    while(!empty(runq) && taper > 0) {
        diskp = dequeue_disk(&runq);
-       if(!degraded_mode) {
+       if (diskp->to_holdingdisk == HOLD_REQUIRED) {
+           log_add(L_FAIL, "%s %s %s %d [%s]",
+               diskp->host->hostname, diskp->name, sched(diskp)->datestamp,
+               sched(diskp)->level,
+               "can't dump required holdingdisk");
+       }
+       else if (!degraded_mode) {
            int rc = dump_to_tape(diskp);
            if(rc == 1)
                log_add(L_INFO,
@@ -393,11 +458,11 @@ int main(main_argc, main_argv)
        }
        else
            log_add(L_FAIL, "%s %s %s %d [%s]",
-                   diskp->host->hostname, diskp->name,
-                   sched(diskp)->datestamp, sched(diskp)->level,
-               diskp->no_hold ?
-                   "can't dump no-hold disk in degraded mode" :
-                   "no more holding disk space");
+               diskp->host->hostname, diskp->name, sched(diskp)->datestamp,
+               sched(diskp)->level,
+               diskp->to_holdingdisk == HOLD_AUTO ?
+                   "no more holding disk space" :
+                   "can't dump no-hold disk in degraded mode");
     }
 
     short_dump_state();                                /* for amstatus */
@@ -408,7 +473,8 @@ int main(main_argc, main_argv)
 
     if(!nodump) {
        for(dumper = dmptable; dumper < dmptable + inparallel; dumper++) {
-           dumper_cmd(dumper, QUIT, NULL);
+           if(dumper->fd >= 0)
+               dumper_cmd(dumper, QUIT, NULL);
        }
     }
 
@@ -417,62 +483,21 @@ int main(main_argc, main_argv)
     }
 
     /* wait for all to die */
-
-    while(1) {
-       char number[NUM_STR_SIZE];
-       pid_t pid;
-       char *who;
-       char *what;
-       int code=0;
-
-       if((pid = wait(&retstat)) == -1) {
-           if(errno == EINTR) continue;
-           else break;
-       }
-       what = NULL;
-       if(! WIFEXITED(retstat)) {
-           what = "signal";
-           code = WTERMSIG(retstat);
-       } else if(WEXITSTATUS(retstat) != 0) {
-           what = "code";
-           code = WEXITSTATUS(retstat);
-       }
-       who = NULL;
-       for(dumper = dmptable; dumper < dmptable + inparallel; dumper++) {
-           if(pid == dumper->pid) {
-               who = stralloc(dumper->name);
-               break;
-           }
-       }
-       if(who == NULL && pid == taper_pid) {
-           who = stralloc("taper");
-       }
-       if(what != NULL && who == NULL) {
-           ap_snprintf(number, sizeof(number), "%ld", (long)pid);
-           who = stralloc2("unknown pid ", number);
-       }
-       if(who && what) {
-           log_add(L_WARNING, "%s exited with %s %d\n", who, what, code);
-           printf("driver: %s exited with %s %d\n", who, what, code);
-       }
-       amfree(who);
-    }
-
-    for(dumper = dmptable; dumper < dmptable + inparallel; dumper++) {
-       amfree(dumper->name);
-    }
+    wait_children(600);
 
     for(hdp = getconf_holdingdisks(); hdp != NULL; hdp = hdp->next) {
-       cleanup_holdingdisk(hdp->diskdir, 0);
+       cleanup_holdingdisk(holdingdisk_get_diskdir(hdp), 0);
        amfree(hdp->up);
     }
     amfree(newdir);
 
+    check_unfree_serial();
     printf("driver: FINISHED time %s\n", walltime_str(curclock()));
     fflush(stdout);
-    log_add(L_FINISH,"date %s time %s", datestamp, walltime_str(curclock()));
-    amfree(datestamp);
+    log_add(L_FINISH,"date %s time %s", driver_timestamp, walltime_str(curclock()));
+    amfree(driver_timestamp);
 
+    free_new_argv(new_argc, new_argv);
     amfree(dumper_program);
     amfree(taper_program);
     amfree(config_dir);
@@ -484,25 +509,168 @@ int main(main_argc, main_argv)
        malloc_list(fileno(stderr), malloc_hist_1, malloc_hist_2);
     }
 
+    dbclose();
+
     return 0;
 }
 
-void startaflush() {
+/* sleep up to count seconds, and wait for terminating child process */
+/* if sleep is negative, this function will not timeout              */
+/* exit once all child process are finished or the timout expired    */
+/* return 0 if no more children to wait                              */
+/* return 1 if some children are still alive                         */
+static int
+wait_children(int count)
+{
+    pid_t     pid;
+    amwait_t  retstat;
+    char     *who;
+    char     *what;
+    int       code=0;
+    dumper_t *dumper;
+    int       wait_errno;
+
+    do {
+       do {
+           pid = waitpid((pid_t)-1, &retstat, WNOHANG);
+           wait_errno = errno;
+           if (pid > 0) {
+               what = NULL;
+               if (! WIFEXITED(retstat)) {
+                   what = "signal";
+                   code = WTERMSIG(retstat);
+               } else if (WEXITSTATUS(retstat) != 0) {
+                   what = "code";
+                   code = WEXITSTATUS(retstat);
+               }
+               who = NULL;
+               for (dumper = dmptable; dumper < dmptable + inparallel;
+                    dumper++) {
+                   if (pid == dumper->pid) {
+                       who = stralloc(dumper->name);
+                       dumper->pid = -1;
+                       break;
+                   }
+                   if (dumper->chunker && pid == dumper->chunker->pid) {
+                       who = stralloc(dumper->chunker->name);
+                       dumper->chunker->pid = -1;
+                       break;
+                   }
+               }
+               if (who == NULL && pid == taper_pid) {
+                   who = stralloc("taper");
+                   taper_pid = -1;
+               }
+               if(what != NULL && who == NULL) {
+                   who = stralloc("unknown");
+               }
+               if(who && what) {
+                   log_add(L_WARNING, "%s pid %u exited with %s %d\n", who, 
+                           (unsigned)pid, what, code);
+                   printf("driver: %s pid %u exited with %s %d\n", who,
+                          (unsigned)pid, what, code);
+               }
+               amfree(who);
+           }
+       } while (pid > 0 || wait_errno == EINTR);
+       if (errno != ECHILD)
+           sleep(1);
+       if (count > 0)
+           count--;
+    } while ((errno != ECHILD) && (count != 0));
+    return (errno != ECHILD);
+}
+
+static void
+kill_children(int signal)
+{
+    dumper_t *dumper;
+
+    if(!nodump) {
+        for(dumper = dmptable; dumper < dmptable + inparallel; dumper++) {
+           if (!dumper->down && dumper->pid > 1) {
+               printf("driver: sending signal %d to %s pid %u\n", signal,
+                      dumper->name, (unsigned)dumper->pid);
+               if (kill(dumper->pid, signal) == -1 && errno == ESRCH) {
+                   if (dumper->chunker)
+                       dumper->chunker->pid = 0;
+               }
+               if (dumper->chunker && dumper->chunker->pid > 1) {
+                   printf("driver: sending signal %d to %s pid %u\n", signal,
+                          dumper->chunker->name,
+                          (unsigned)dumper->chunker->pid);
+                   if (kill(dumper->chunker->pid, signal) == -1 &&
+                       errno == ESRCH)
+                       dumper->chunker->pid = 0;
+               }
+           }
+        }
+    }
+
+    if(taper_pid > 1)
+       printf("driver: sending signal %d to %s pid %u\n", signal,
+              "taper", (unsigned)taper_pid);
+       if (kill(taper_pid, signal) == -1 && errno == ESRCH)
+           taper_pid = 0;
+}
+
+static void
+wait_for_children(void)
+{
+    dumper_t *dumper;
+
+    if(!nodump) {
+       for(dumper = dmptable; dumper < dmptable + inparallel; dumper++) {
+           if (dumper->pid > 1 && dumper->fd >= 0) {
+               dumper_cmd(dumper, QUIT, NULL);
+               if (dumper->chunker && dumper->chunker->pid > 1 &&
+                   dumper->chunker->fd >= 0)
+                   chunker_cmd(dumper->chunker, QUIT, NULL);
+           }
+       }
+    }
+
+    if(taper_pid > 1 && taper > 0) {
+       taper_cmd(QUIT, NULL, NULL, 0, NULL);
+    }
+
+    if(wait_children(60) == 0)
+       return;
+
+    kill_children(SIGHUP);
+    if(wait_children(60) == 0)
+       return;
+
+    kill_children(SIGKILL);
+    if(wait_children(-1) == 0)
+       return;
+
+}
+
+static void
+startaflush(void)
+{
     disk_t *dp = NULL;
     disk_t *fit = NULL;
     char *datestamp;
+    int extra_tapes = 0;
+    char *qname;
 
     if(!degraded_mode && !taper_busy && !empty(tapeq)) {
+       
        datestamp = sched(tapeq.head)->datestamp;
        switch(conf_taperalgo) {
        case ALGO_FIRST:
                dp = dequeue_disk(&tapeq);
                break;
-       case ALGO_FIRSTFIT: 
+       case ALGO_FIRSTFIT:
                fit = tapeq.head;
                while (fit != NULL) {
-                   if(sched(fit)->act_size <= tape_left &&
-                      strcmp(sched(fit)->datestamp, datestamp) <= 0) {
+                   extra_tapes = (fit->tape_splitsize > (off_t)0) ? 
+                                       conf_runtapes - current_tape : 0;
+                   if(sched(fit)->act_size <= (tape_left +
+                            tape_length * (off_t)extra_tapes) &&
+                            strcmp(sched(fit)->datestamp, datestamp) <= 0) {
                        dp = fit;
                        fit = NULL;
                    }
@@ -512,7 +680,7 @@ void startaflush() {
                }
                if(dp) remove_disk(&tapeq, dp);
                break;
-       case ALGO_LARGEST: 
+       case ALGO_LARGEST:
                fit = dp = tapeq.head;
                while (fit != NULL) {
                    if(sched(fit)->act_size > sched(dp)->act_size &&
@@ -523,10 +691,13 @@ void startaflush() {
                }
                if(dp) remove_disk(&tapeq, dp);
                break;
-       case ALGO_LARGESTFIT: 
+       case ALGO_LARGESTFIT:
                fit = tapeq.head;
                while (fit != NULL) {
-                   if(sched(fit)->act_size <= tape_left &&
+                   extra_tapes = (fit->tape_splitsize > (off_t)0) ? 
+                                       conf_runtapes - current_tape : 0;
+                   if(sched(fit)->act_size <=
+                      (tape_left + tape_length * (off_t)extra_tapes) &&
                       (!dp || sched(fit)->act_size > sched(dp)->act_size) &&
                       strcmp(sched(fit)->datestamp, datestamp) <= 0) {
                        dp = fit;
@@ -535,41 +706,62 @@ void startaflush() {
                }
                if(dp) remove_disk(&tapeq, dp);
                break;
-       case ALGO_SMALLEST: 
-               fit = dp = tapeq.head;
-               while (fit != NULL) {
-                   if(sched(fit)->act_size < sched(dp)->act_size &&
-                      strcmp(sched(fit)->datestamp, datestamp) <= 0) {
-                       dp = fit;
-                   }
-                   fit = fit->next;
-               }
-               if(dp) remove_disk(&tapeq, dp);
+       case ALGO_SMALLEST:
                break;
        case ALGO_LAST:
                dp = tapeq.tail;
                remove_disk(&tapeq, dp);
                break;
        }
-       if(!dp) {
-           dp = dequeue_disk(&tapeq); /* first if nothing fit */
-           fprintf(stderr,
-                   "driver: startaflush: Using first because nothing fit\n");
-       }
-       taper_disk = dp;
-       taper_busy = 1;
-       taper_cmd(FILE_WRITE, dp, sched(dp)->destname, sched(dp)->level, 
-                 sched(dp)->datestamp);
-       fprintf(stderr,"driver: startaflush: %s %s %s %ld %ld\n",
-               taperalgo2str(conf_taperalgo), dp->host->hostname,
-               dp->name, sched(taper_disk)->act_size, tape_left);
-       tape_left -= sched(dp)->act_size;
+       if(!dp) { /* ALGO_SMALLEST, or default if nothing fit. */
+           if(conf_taperalgo != ALGO_SMALLEST)  {
+               fprintf(stderr,
+                  "driver: startaflush: Using SMALLEST because nothing fit\n");
+           }
+           fit = dp = tapeq.head;
+           while (fit != NULL) {
+               if(sched(fit)->act_size < sched(dp)->act_size &&
+                  strcmp(sched(fit)->datestamp, datestamp) <= 0) {
+                   dp = fit;
+               }
+               fit = fit->next;
+           }
+           if(dp) remove_disk(&tapeq, dp);
+       }
+       if(taper_ev_read == NULL) {
+           taper_ev_read = event_register((event_id_t)taper, EV_READFD,
+                                          handle_taper_result, NULL);
+       }
+       if (dp) {
+           taper_disk = dp;
+           taper_busy = 1;
+           qname = quote_string(dp->name);
+           taper_cmd(FILE_WRITE, dp, sched(dp)->destname, sched(dp)->level,
+                     sched(dp)->datestamp);
+           fprintf(stderr,"driver: startaflush: %s %s %s "
+                   OFF_T_FMT " " OFF_T_FMT "\n",
+                   taperalgo2str(conf_taperalgo), dp->host->hostname, qname,
+                   (OFF_T_FMT_TYPE)sched(taper_disk)->act_size,
+                   (OFF_T_FMT_TYPE)tape_left);
+           if(sched(dp)->act_size <= tape_left)
+               tape_left -= sched(dp)->act_size;
+           else
+               tape_left = (off_t)0;
+           amfree(qname);
+       } else {
+           error("FATAL: Taper marked busy and no work found.");
+           /*NOTREACHED*/
+       }
+    } else if(!taper_busy && taper_ev_read != NULL) {
+       event_release(taper_ev_read);
+       taper_ev_read = NULL;
     }
 }
 
 
-int client_constrained(dp)
-disk_t *dp;
+static int
+client_constrained(
+    disk_t *   dp)
 {
     disk_t *dp2;
 
@@ -593,86 +785,103 @@ disk_t *dp;
     return 0;
 }
 
-int start_some_dumps(rq)
-disklist_t *rq;
+static void
+start_some_dumps(
+    disklist_t *       rq)
 {
-    int total, cur_idle;
-    disk_t *diskp, *diskp_accept;
-    dumper_t *dumper;
+    int cur_idle;
+    disk_t *diskp, *delayed_diskp, *diskp_accept;
     assignedhd_t **holdp=NULL, **holdp_accept;
-    time_t now = time(NULL);
+    const time_t now = time(NULL);
+    cmd_t cmd;
+    int result_argc;
+    char *result_argv[MAX_ARGS+1];
+    chunker_t *chunker;
+    dumper_t *dumper;
+    char dumptype;
+    char *dumporder;
 
-    total = 0;
     idle_reason = IDLE_NO_DUMPERS;
-    sleep_time.tv_sec = SLEEP_MAX;
-    sleep_time.tv_usec = 0;
-    any_delayed_disk = 0;
+    sleep_time = 0;
 
-    if(rq->head == NULL) {
-       idle_reason = 0;
-       return 0;
+    if(dumpers_ev_time != NULL) {
+       event_release(dumpers_ev_time);
+       dumpers_ev_time = NULL;
     }
 
-    /*
-     * A potential problem with starting from the bottom of the dump time
-     * distribution is that a slave host will have both one of the shortest
-     * and one of the longest disks, so starting its shortest disk first will
-     * tie up the host and eliminate its longest disk from consideration the
-     * first pass through.  This could cause a big delay in starting that long
-     * disk, which could drag out the whole night's dumps.
-     *
-     * While starting from the top of the dump time distribution solves the
-     * above problem, this turns out to be a bad idea, because the big dumps
-     * will almost certainly pack the holding disk completely, leaving no
-     * room for even one small dump to start.  This ends up shutting out the
-     * small-end dumpers completely (they stay idle).
-     *
-     * The introduction of multiple simultaneous dumps to one host alleviates
-     * the biggest&smallest dumps problem: both can be started at the
-     * beginning.
-     */
-    for(dumper = dmptable; dumper < dmptable+inparallel; dumper++) {
-       if(dumper->busy || dumper->down) continue;
-       /* found an idle dumper, now find a disk for it */
-       diskp = rq->head;
+    for (dumper = dmptable; dumper < dmptable+inparallel; dumper++) {
+
+       if( dumper->busy || dumper->down) {
+           continue;
+       }
+
+       if (dumper->ev_read != NULL) {
+           event_release(dumper->ev_read);
+           dumper->ev_read = NULL;
+       }
+
+       /*
+        * A potential problem with starting from the bottom of the dump time
+        * distribution is that a slave host will have both one of the shortest
+        * and one of the longest disks, so starting its shortest disk first will
+        * tie up the host and eliminate its longest disk from consideration the
+        * first pass through.  This could cause a big delay in starting that long
+        * disk, which could drag out the whole night's dumps.
+        *
+        * While starting from the top of the dump time distribution solves the
+        * above problem, this turns out to be a bad idea, because the big dumps
+        * will almost certainly pack the holding disk completely, leaving no
+        * room for even one small dump to start.  This ends up shutting out the
+        * small-end dumpers completely (they stay idle).
+        *
+        * The introduction of multiple simultaneous dumps to one host alleviates
+        * the biggest&smallest dumps problem: both can be started at the
+        * beginning.
+        */
+
        diskp_accept = NULL;
        holdp_accept = NULL;
-
-       if(idle_reason == IDLE_NO_DUMPERS)
-           idle_reason = NOT_IDLE;
+       delayed_diskp = NULL;
 
        cur_idle = NOT_IDLE;
 
-       while(diskp) {
-           assert(diskp->host != NULL && sched(diskp) != NULL);
+       dumporder = getconf_str(CNF_DUMPORDER);
+       if(strlen(dumporder) > (size_t)(dumper-dmptable)) {
+           dumptype = dumporder[dumper-dmptable];
+       }
+       else {
+           if(dumper-dmptable < 3)
+               dumptype = 't';
+           else
+               dumptype = 'T';
+       }
 
-           /* round estimate to next multiple of DISK_BLOCK_KB */
-           sched(diskp)->est_size = am_round(sched(diskp)->est_size,
-                                             DISK_BLOCK_KB);
+       for(diskp = rq->head; diskp != NULL; diskp = diskp->next) {
+           assert(diskp->host != NULL && sched(diskp) != NULL);
 
-           if(diskp->host->start_t > now) {
+           if (diskp->host->start_t > now) {
                cur_idle = max(cur_idle, IDLE_START_WAIT);
-               sleep_time.tv_sec = min(diskp->host->start_t - now, 
-                                       sleep_time.tv_sec);
-               any_delayed_disk = 1;
-           }
-           else if(diskp->start_t > now) {
+               if (delayed_diskp == NULL || sleep_time > diskp->host->start_t) {
+                   delayed_diskp = diskp;
+                   sleep_time = diskp->host->start_t;
+               }
+           else if(diskp->start_t > now) {
                cur_idle = max(cur_idle, IDLE_START_WAIT);
-               sleep_time.tv_sec = min(diskp->start_t - now, 
-                                       sleep_time.tv_sec);
-               any_delayed_disk = 1;
-           }
-           else if(diskp->host->netif->curusage > 0 &&
-                   sched(diskp)->est_kps > free_kps(diskp->host->netif))
+               if (delayed_diskp == NULL || sleep_time > diskp->start_t) {
+                   delayed_diskp = diskp;
+                   sleep_time = diskp->start_t;
+               }
+           } else if (diskp->host->netif->curusage > 0 &&
+                      sched(diskp)->est_kps > free_kps(diskp->host->netif)) {
                cur_idle = max(cur_idle, IDLE_NO_BANDWIDTH);
-           else if(sched(diskp)->no_space)
+           } else if(sched(diskp)->no_space) {
                cur_idle = max(cur_idle, IDLE_NO_DISKSPACE);
-           else if((holdp = find_diskspace(sched(diskp)->est_size,&cur_idle,NULL)) == NULL)
-               cur_idle = max(cur_idle, IDLE_NO_DISKSPACE);
-           else if(diskp->no_hold) {
-               free_assignedhd(holdp);
+           } else if (diskp->to_holdingdisk == HOLD_NEVER) {
                cur_idle = max(cur_idle, IDLE_NO_HOLD);
-           } else if(client_constrained(diskp)) {
+           } else if ((holdp =
+               find_diskspace(sched(diskp)->est_size, &cur_idle, NULL)) == NULL) {
+               cur_idle = max(cur_idle, IDLE_NO_DISKSPACE);
+           } else if (client_constrained(diskp)) {
                free_assignedhd(holdp);
                cur_idle = max(cur_idle, IDLE_CLIENT_CONSTRAINED);
            } else {
@@ -680,17 +889,6 @@ disklist_t *rq;
                /* disk fits, dump it */
                int accept = !diskp_accept;
                if(!accept) {
-                   char dumptype;
-                   char *dumporder = getconf_str(CNF_DUMPORDER);
-                   if(strlen(dumporder) <= (dumper-dmptable)) {
-                       if(dumper-dmptable < 3)
-                           dumptype = 't';
-                       else
-                           dumptype = 'T';
-                   }
-                   else {
-                       dumptype = dumporder[dumper-dmptable];
-                   }
                    switch(dumptype) {
                      case 's': accept = (sched(diskp)->est_size < sched(diskp_accept)->est_size);
                                break;
@@ -704,7 +902,7 @@ disklist_t *rq;
                                break;
                      case 'B': accept = (sched(diskp)->est_kps > sched(diskp_accept)->est_kps);
                                break;
-                     default:  log_add(L_WARNING, "Unknown dumporder character \'%c\', using 's'.\n",
+                     default:  log_add(L_WARNING, "Unknown dumporder character \'%c\', using 's'.\n",
                                        dumptype);
                                accept = (sched(diskp)->est_size < sched(diskp_accept)->est_size);
                                break;
@@ -724,140 +922,211 @@ disklist_t *rq;
                    free_assignedhd(holdp);
                }
            }
-           diskp = diskp->next;
        }
 
        diskp = diskp_accept;
        holdp = holdp_accept;
-       if(diskp) {
-           cur_idle = NOT_IDLE;
-           sched(diskp)->act_size = 0;
+
+       idle_reason = max(idle_reason, cur_idle);
+
+       /*
+        * If we have no disk at this point, and there are disks that
+        * are delayed, then schedule a time event to call this dumper
+        * with the disk with the shortest delay.
+        */
+       if (diskp == NULL && delayed_diskp != NULL) {
+           assert(sleep_time > now);
+           sleep_time -= now;
+           dumpers_ev_time = event_register((event_id_t)sleep_time, EV_TIME,
+               handle_dumpers_time, &runq);
+           return;
+       } else if (diskp != NULL) {
+           sched(diskp)->act_size = (off_t)0;
            allocate_bandwidth(diskp->host->netif, sched(diskp)->est_kps);
            sched(diskp)->activehd = assign_holdingdisk(holdp, diskp);
            amfree(holdp);
-           diskp->host->inprogress += 1;       /* host is now busy */
+           sched(diskp)->destname = newstralloc(sched(diskp)->destname,
+                                                sched(diskp)->holdp[0]->destname);
+           diskp->host->inprogress++;  /* host is now busy */
            diskp->inprogress = 1;
            sched(diskp)->dumper = dumper;
-           sched(diskp)->timestamp = time((time_t *)0);
+           sched(diskp)->timestamp = now;
 
            dumper->busy = 1;           /* dumper is now busy */
            dumper->dp = diskp;         /* link disk to dumper */
-           total++;
            remove_disk(rq, diskp);             /* take it off the run queue */
-           dumper_cmd(dumper, FILE_DUMP, diskp);
-           diskp->host->start_t = time(NULL) + 15;
+
+           sched(diskp)->origsize = (off_t)-1;
+           sched(diskp)->dumpsize = (off_t)-1;
+           sched(diskp)->dumptime = (time_t)0;
+           sched(diskp)->tapetime = (time_t)0;
+           chunker = dumper->chunker;
+           chunker->result = LAST_TOK;
+           dumper->result = LAST_TOK;
+           startup_chunk_process(chunker,chunker_program);
+           chunker_cmd(chunker, START, (void *)driver_timestamp);
+           chunker->dumper = dumper;
+           chunker_cmd(chunker, PORT_WRITE, diskp);
+           cmd = getresult(chunker->fd, 1, &result_argc, result_argv, MAX_ARGS+1);
+           if(cmd != PORT) {
+               assignedhd_t **h=NULL;
+               int activehd;
+
+               printf("driver: did not get PORT from %s for %s:%s\n",
+                      chunker->name, diskp->host->hostname, diskp->name);
+               fflush(stdout);
+
+               deallocate_bandwidth(diskp->host->netif, sched(diskp)->est_kps);
+               h = sched(diskp)->holdp;
+               activehd = sched(diskp)->activehd;
+               h[activehd]->used = 0;
+               holdalloc(h[activehd]->disk)->allocated_dumpers--;
+               adjust_diskspace(diskp, DONE);
+               delete_diskspace(diskp);
+               diskp->host->inprogress--;
+               diskp->inprogress = 0;
+               sched(diskp)->dumper = NULL;
+               dumper->busy = 0;
+               dumper->dp = NULL;
+               sched(diskp)->attempted++;
+               free_serial_dp(diskp);
+               if(sched(diskp)->attempted < 2)
+                   enqueue_disk(rq, diskp);
+           }
+           else {
+               dumper->ev_read = event_register((event_id_t)dumper->fd, EV_READFD,
+                                                handle_dumper_result, dumper);
+               chunker->ev_read = event_register((event_id_t)chunker->fd, EV_READFD,
+                                                  handle_chunker_result, chunker);
+               dumper->output_port = atoi(result_argv[2]);
+
+               dumper_cmd(dumper, PORT_DUMP, diskp);
+           }
+           diskp->host->start_t = now + 15;
        }
-       idle_reason = max(idle_reason, cur_idle);
     }
-    return total;
 }
 
-int sort_by_priority_reversed(a, b)
-disk_t *a, *b;
-{
-    if(sched(b)->priority - sched(a)->priority != 0)
-       return sched(b)->priority - sched(a)->priority;
-    else
-       return sort_by_time(a, b);
-}
+/*
+ * This gets called when a dumper is delayed for some reason.  It may
+ * be because a disk has a delayed start, or amanda is constrained
+ * by network or disk limits.
+ */
 
-int sort_by_time(a, b)
-disk_t *a, *b;
+static void
+handle_dumpers_time(
+    void *     cookie)
 {
-    long diff;
-
-    if ((diff = sched(a)->est_time - sched(b)->est_time) < 0) {
-       return -1;
-    } else if (diff > 0) {
-       return 1;
-    } else {
-       return 0;
-    }
+    disklist_t *runq = cookie;
+    event_release(dumpers_ev_time);
+    dumpers_ev_time = NULL; 
+    start_some_dumps(runq);
 }
 
-void dump_schedule(qp, str)
-disklist_t *qp;
-char *str;
+static void
+dump_schedule(
+    disklist_t *qp,
+    char *     str)
 {
     disk_t *dp;
+    char *qname;
 
     printf("dump of driver schedule %s:\n--------\n", str);
 
     for(dp = qp->head; dp != NULL; dp = dp->next) {
-       printf("  %-10.10s %.16s lv %d t %5ld s %8lu p %d\n",
-              dp->host->hostname, dp->name, sched(dp)->level,
-              sched(dp)->est_time, sched(dp)->est_size, sched(dp)->priority);
+        qname = quote_string(dp->name);
+       printf("  %-20s %-25s lv %d t %5lu s " OFF_T_FMT " p %d\n",
+              dp->host->hostname, qname, sched(dp)->level,
+              sched(dp)->est_time,
+              (OFF_T_FMT_TYPE)sched(dp)->est_size, sched(dp)->priority);
+        amfree(qname);
     }
     printf("--------\n");
 }
 
-
-void start_degraded_mode(queuep)
-disklist_t *queuep;
+static void
+start_degraded_mode(
+    /*@keep@*/ disklist_t *queuep)
 {
     disk_t *dp;
     disklist_t newq;
-    unsigned long est_full_size;
+    off_t est_full_size;
+    char *qname;
+
+    if (taper_ev_read != NULL) {
+       event_release(taper_ev_read);
+       taper_ev_read = NULL;
+    }
 
     newq.head = newq.tail = 0;
 
     dump_schedule(queuep, "before start degraded mode");
 
-    est_full_size = 0;
+    est_full_size = (off_t)0;
     while(!empty(*queuep)) {
        dp = dequeue_disk(queuep);
 
+       qname = quote_string(dp->name);
        if(sched(dp)->level != 0)
            /* go ahead and do the disk as-is */
-           insert_disk(&newq, dp, sort_by_priority_reversed);
+           enqueue_disk(&newq, dp);
        else {
            if (reserved_space + est_full_size + sched(dp)->est_size
                <= total_disksize) {
-               insert_disk(&newq, dp, sort_by_priority_reversed);
+               enqueue_disk(&newq, dp);
                est_full_size += sched(dp)->est_size;
            }
            else if(sched(dp)->degr_level != -1) {
                sched(dp)->level = sched(dp)->degr_level;
                sched(dp)->dumpdate = sched(dp)->degr_dumpdate;
-               sched(dp)->est_size = sched(dp)->degr_size;
+               sched(dp)->est_nsize = sched(dp)->degr_nsize;
+               sched(dp)->est_csize = sched(dp)->degr_csize;
                sched(dp)->est_time = sched(dp)->degr_time;
                sched(dp)->est_kps  = sched(dp)->degr_kps;
-               insert_disk(&newq, dp, sort_by_priority_reversed);
+               enqueue_disk(&newq, dp);
            }
            else {
-               log_add(L_FAIL, "%s %s %s %d [can't switch to incremental dump]",
-                       dp->host->hostname, dp->name,
-                       sched(dp)->datestamp, sched(dp)->level);
+               log_add(L_FAIL,"%s %s %s %d [can't switch to incremental dump]",
+                       dp->host->hostname, qname, sched(dp)->datestamp,
+                       sched(dp)->level);
            }
        }
+        amfree(qname);
     }
 
-    *queuep = newq;
+    /*@i@*/ *queuep = newq;
     degraded_mode = 1;
 
     dump_schedule(queuep, "after start degraded mode");
 }
 
-void continue_dumps()
+
+static void
+continue_port_dumps(void)
 {
-disk_t *dp, *ndp;
-assignedhd_t **h;
-int active_dumpers=0, busy_dumpers=0, i;
-dumper_t *dumper;
+    disk_t *dp, *ndp;
+    assignedhd_t **h;
+    int active_dumpers=0, busy_dumpers=0, i;
+    dumper_t *dumper;
 
     /* First we try to grant diskspace to some dumps waiting for it. */
     for( dp = roomq.head; dp; dp = ndp ) {
        ndp = dp->next;
        /* find last holdingdisk used by this dump */
-       for( i = 0, h = sched(dp)->holdp; h[i+1]; i++ );
+       for( i = 0, h = sched(dp)->holdp; h[i+1]; i++ ) {
+           (void)h; /* Quiet lint */
+       }
        /* find more space */
-       h = find_diskspace( sched(dp)->est_size - sched(dp)->act_size, &active_dumpers, h[i] );
+       h = find_diskspace( sched(dp)->est_size - sched(dp)->act_size,
+                           &active_dumpers, h[i] );
        if( h ) {
            for(dumper = dmptable; dumper < dmptable + inparallel &&
-                                  dumper->dp != dp; dumper++);
+                                  dumper->dp != dp; dumper++) {
+               (void)dp; /* Quiet lint */
+           }
            assert( dumper < dmptable + inparallel );
            sched(dp)->activehd = assign_holdingdisk( h, dp );
-           dumper_cmd( dumper, CONTINUE, dp );
+           chunker_cmd( dumper->chunker, CONTINUE, dp );
            amfree(h);
            remove_disk( &roomq, dp );
        }
@@ -878,17 +1147,18 @@ dumper_t *dumper;
      * it will be dumped directly to tape. Actually, case c is a special
      * manifestation of case b) where only one dumper is busy.
      */
-    for( dp=NULL, dumper = dmptable; dumper < dmptable + inparallel; dumper++) {
+    for(dp=NULL, dumper = dmptable; dumper < (dmptable+inparallel); dumper++) {
        if( dumper->busy ) {
            busy_dumpers++;
            if( !find_disk(&roomq, dumper->dp) ) {
                active_dumpers++;
-           } else if( !dp || sched(dp)->est_size > sched(dumper->dp)->est_size ) {
+           } else if( !dp || 
+                      sched(dp)->est_size > sched(dumper->dp)->est_size ) {
                dp = dumper->dp;
            }
        }
     }
-    if( !active_dumpers && busy_dumpers > 0 && 
+    if((dp != NULL) && (active_dumpers == 0) && (busy_dumpers > 0) && 
         ((!taper_busy && empty(tapeq)) || degraded_mode) &&
        pending_aborts == 0 ) { /* not case a */
        if( busy_dumpers == 1 ) { /* case c */
@@ -899,409 +1169,619 @@ dumper_t *dumper;
         * We abort that dump, hopefully not wasting too much time retrying it.
         */
        remove_disk( &roomq, dp );
+       chunker_cmd( sched(dp)->dumper->chunker, ABORT, NULL);
        dumper_cmd( sched(dp)->dumper, ABORT, NULL );
        pending_aborts++;
     }
 }
 
-void handle_taper_result()
+
+static void
+handle_taper_result(
+    void *     cookie)
 {
     disk_t *dp;
-    int filenum;
+    off_t filenum;
     cmd_t cmd;
     int result_argc;
     char *result_argv[MAX_ARGS+1];
+    int avail_tapes = 0;
+    
+    (void)cookie;      /* Quiet unused parameter warning */
+
+    assert(cookie == NULL);
+    
+    do {
+        
+       short_dump_state();
+        
+       cmd = getresult(taper, 1, &result_argc, result_argv, MAX_ARGS+1);
+        
+       switch(cmd) {
+            
+       case PARTIAL:
+       case DONE:      /* DONE <handle> <label> <tape file> <err mess> */
+           if(result_argc != 5) {
+               error("error: [taper DONE result_argc != 5: %d", result_argc);
+               /*NOTREACHED*/
+           }
+            
+           dp = serial2disk(result_argv[2]);
+           free_serial(result_argv[2]);
+            
+           filenum = OFF_T_ATOI(result_argv[4]);
+           if(cmd == DONE) {
+               update_info_taper(dp, result_argv[3], filenum,
+                                  sched(dp)->level);
+           }
+            
+           delete_diskspace(dp);
+            
+           printf("driver: finished-cmd time %s taper wrote %s:%s\n",
+                  walltime_str(curclock()), dp->host->hostname, dp->name);
+           fflush(stdout);
+            
+           amfree(sched(dp)->destname);
+           amfree(sched(dp)->dumpdate);
+           amfree(sched(dp)->degr_dumpdate);
+           amfree(sched(dp)->datestamp);
+           amfree(dp->up);
+            
+           taper_busy = 0;
+           taper_disk = NULL;
+           startaflush();
+            
+           /* continue with those dumps waiting for diskspace */
+           continue_port_dumps();
+           break;
+            
+       case TRYAGAIN:  /* TRY-AGAIN <handle> <err mess> */
+           if (result_argc < 2) {
+               error("error [taper TRYAGAIN result_argc < 2: %d]",
+                     result_argc);
+               /*NOTREACHED*/
+           }
+           dp = serial2disk(result_argv[2]);
+           free_serial(result_argv[2]);
+           printf("driver: taper-tryagain time %s disk %s:%s\n",
+                  walltime_str(curclock()), dp->host->hostname, dp->name);
+           fflush(stdout);
+            
+           /* See how many tapes we have left, but we alwyays
+              retry once (why?) */
+           current_tape++;
+           if(dp->tape_splitsize > (off_t)0)
+               avail_tapes = conf_runtapes - current_tape;
+           else
+               avail_tapes = 0;
+            
+           if(sched(dp)->attempted > avail_tapes) {
+               log_add(L_FAIL, "%s %s %s %d [too many taper retries]",
+                        dp->host->hostname, dp->name, sched(dp)->datestamp,
+                        sched(dp)->level);
+               printf("driver: taper failed %s %s %s, too many taper retry\n",
+                       result_argv[2], dp->host->hostname, dp->name);
+           }
+           else {
+               /* Re-insert into taper queue. */
+               sched(dp)->attempted++;
+               headqueue_disk(&tapeq, dp);
+           }
+            
+           tape_left = tape_length;
+            
+           /* run next thing from queue */
+            
+           taper_busy = 0;
+           taper_disk = NULL;
+           startaflush();
+           continue_port_dumps();
+           break;
+            
+        case SPLIT_CONTINUE:  /* SPLIT_CONTINUE <handle> <new_label> */
+            if (result_argc != 3) {
+                error("error [taper SPLIT_CONTINUE result_argc != 3: %d]",
+                      result_argc);
+               /*NOTREACHED*/
+            }
+            
+            break;
+        case SPLIT_NEEDNEXT:  /* SPLIT-NEEDNEXT <handle> <kb written> */
+            if (result_argc != 3) {
+                error("error [taper SPLIT_NEEDNEXT result_argc != 3: %d]",
+                      result_argc);
+               /*NOTREACHED*/
+            }
+            
+            /* Update our tape counter and reset tape_left */
+            current_tape++;
+            tape_left = tape_length;
+            
+            /* Reduce the size of the dump by amount written and reduce
+               tape_left by the amount left over */
+            dp = serial2disk(result_argv[2]);
+            sched(dp)->act_size -= OFF_T_ATOI(result_argv[3]);
+            if (sched(dp)->act_size < tape_left)
+                tape_left -= sched(dp)->act_size;
+            else
+                tape_length = 0;
+            
+            break;
+            
+        case TAPE_ERROR: /* TAPE-ERROR <handle> <err mess> */
+            dp = serial2disk(result_argv[2]);
+            free_serial(result_argv[2]);
+            printf("driver: finished-cmd time %s taper wrote %s:%s\n",
+                   walltime_str(curclock()), dp->host->hostname, dp->name);
+            fflush(stdout);
+            log_add(L_WARNING, "Taper  error: %s", result_argv[3]);
+            /*FALLTHROUGH*/
+
+        case BOGUS:
+            if (cmd == BOGUS) {
+               log_add(L_WARNING, "Taper protocol error");
+            }
+            /*
+             * Since we received a taper error, we can't send anything more
+             * to the taper.  Go into degraded mode to try to get everthing
+             * onto disk.  Later, these dumps can be flushed to a new tape.
+             * The tape queue is zapped so that it appears empty in future
+             * checks. If there are dumps waiting for diskspace to be freed,
+             * cancel one.
+             */
+            if(!nodump) {
+                log_add(L_WARNING,
+                        "going into degraded mode because of taper component error.");
+                start_degraded_mode(&runq);
+            }
+            tapeq.head = tapeq.tail = NULL;
+            taper_busy = 0;
+            taper_disk = NULL;
+            if(taper_ev_read != NULL) {
+                event_release(taper_ev_read);
+                taper_ev_read = NULL;
+            }
+            if(cmd != TAPE_ERROR) aclose(taper);
+            continue_port_dumps();
+            break;
+
+       default:
+            error("driver received unexpected token (%s) from taper",
+                  cmdstr[cmd]);
+           /*NOTREACHED*/
+       }
+       /*
+        * Wakeup any dumpers that are sleeping because of network
+        * or disk constraints.
+        */
+       start_some_dumps(&runq);
+        
+    } while(areads_dataready(taper));
+}
 
-    cmd = getresult(taper, 1, &result_argc, result_argv, MAX_ARGS+1);
+static dumper_t *
+idle_dumper(void)
+{
+    dumper_t *dumper;
 
-    switch(cmd) {
+    for(dumper = dmptable; dumper < dmptable+inparallel; dumper++)
+       if(!dumper->busy && !dumper->down) return dumper;
 
-    case DONE: /* DONE <handle> <label> <tape file> <err mess> */
-       if(result_argc != 5) {
-           error("error: [taper DONE result_argc != 5: %d", result_argc);
-       }
+    return NULL;
+}
 
-       dp = serial2disk(result_argv[2]);
-       free_serial(result_argv[2]);
+static void
+dumper_result(
+    disk_t *   dp)
+{
+    dumper_t *dumper;
+    chunker_t *chunker;
+    assignedhd_t **h=NULL;
+    int activehd, i;
+    off_t dummy;
+    off_t size;
+    int is_partial;
+
+    dumper = sched(dp)->dumper;
+    chunker = dumper->chunker;
+
+    free_serial_dp(dp);
+
+    h = sched(dp)->holdp;
+    activehd = sched(dp)->activehd;
+
+    if(dumper->result == DONE && chunker->result == DONE) {
+       update_info_dumper(dp, sched(dp)->origsize,
+                          sched(dp)->dumpsize, sched(dp)->dumptime);
+       log_add(L_STATS, "estimate %s %s %s %d [sec %ld nkb " OFF_T_FMT
+               " ckb " OFF_T_FMT " kps %lu]",
+               dp->host->hostname, dp->name, sched(dp)->datestamp,
+               sched(dp)->level,
+               sched(dp)->est_time, (OFF_T_FMT_TYPE)sched(dp)->est_nsize, 
+                (OFF_T_FMT_TYPE)sched(dp)->est_csize,
+               sched(dp)->est_kps);
+    }
 
-       filenum = atoi(result_argv[4]);
-       update_info_taper(dp, result_argv[3], filenum, sched(dp)->level);
+    deallocate_bandwidth(dp->host->netif, sched(dp)->est_kps);
 
-       delete_diskspace(dp);
+    is_partial = dumper->result != DONE || chunker->result != DONE;
+    rename_tmp_holding(sched(dp)->destname, !is_partial);
 
-       printf("driver: finished-cmd time %s taper wrote %s:%s\n",
-              walltime_str(curclock()), dp->host->hostname, dp->name);
-       fflush(stdout);
+    dummy = (off_t)0;
+    for( i = 0, h = sched(dp)->holdp; i < activehd; i++ ) {
+       dummy += h[i]->used;
+    }
+
+    size = holding_file_size(sched(dp)->destname, 0);
+    h[activehd]->used = size - dummy;
+    holdalloc(h[activehd]->disk)->allocated_dumpers--;
+    adjust_diskspace(dp, DONE);
 
-       amfree(sched(dp)->dumpdate);
-       amfree(sched(dp)->degr_dumpdate);
-       amfree(sched(dp)->datestamp);
-       amfree(dp->up);
+    sched(dp)->attempted += 1;
 
-       taper_busy = 0;
-       taper_disk = NULL;
+    if((dumper->result != DONE || chunker->result != DONE) &&
+       sched(dp)->attempted <= 1) {
+       delete_diskspace(dp);
+       enqueue_disk(&runq, dp);
+    }
+    else if(size > (off_t)DISK_BLOCK_KB) {
+       sched(dp)->attempted = 0;
+       enqueue_disk(&tapeq, dp);
        startaflush();
-       continue_dumps(); /* continue with those dumps waiting for diskspace */
-       break;
+    }
+    else {
+       delete_diskspace(dp);
+    }
 
-    case TRYAGAIN:  /* TRY-AGAIN <handle> <err mess> */
-       if (result_argc < 2) {
-           error("error [taper TRYAGAIN result_argc < 2: %d]", result_argc);
-       }
-       dp = serial2disk(result_argv[2]);
-       free_serial(result_argv[2]);
-       printf("driver: taper-tryagain time %s disk %s:%s\n",
-              walltime_str(curclock()), dp->host->hostname, dp->name);
-       fflush(stdout);
+    dumper->busy = 0;
+    dp->host->inprogress -= 1;
+    dp->inprogress = 0;
 
-       /* re-insert into taper queue */
+    waitpid(chunker->pid, NULL, 0 );
+    aclose(chunker->fd);
+    chunker->fd = -1;
+    chunker->down = 1;
+    
+    dp = NULL;
+    if (chunker->result == ABORT_FINISHED)
+       pending_aborts--;
+    continue_port_dumps();
+    /*
+     * Wakeup any dumpers that are sleeping because of network
+     * or disk constraints.
+     */
+    start_some_dumps(&runq);
+}
 
-       if(sched(dp)->attempted) {
-           log_add(L_FAIL, "%s %s %d %s [too many taper retries]",
-                   dp->host->hostname, dp->name, sched(dp)->level,
-                   sched(dp)->datestamp);
-       }
-       else {
-           sched(dp)->attempted++;
-           headqueue_disk(&tapeq, dp);
-       }
 
-       tape_left = tape_length;
+static void
+handle_dumper_result(
+    void *     cookie)
+{
+    /*static int pending_aborts = 0;*/
+    dumper_t *dumper = cookie;
+    disk_t *dp, *sdp;
+    cmd_t cmd;
+    int result_argc;
+    char *qname;
+    char *result_argv[MAX_ARGS+1];
+
+    assert(dumper != NULL);
+    dp = dumper->dp;
+    assert(dp != NULL && sched(dp) != NULL);
 
-       /* run next thing from queue */
-       taper_busy = 0;
-       taper_disk = NULL;
-       startaflush();
-       continue_dumps(); /* continue with those dumps waiting for diskspace */
+    do {
 
-       break;
+       short_dump_state();
 
-    case TAPE_ERROR: /* TAPE-ERROR <handle> <err mess> */
-       dp = serial2disk(result_argv[2]);
-       free_serial(result_argv[2]);
-       printf("driver: finished-cmd time %s taper wrote %s:%s\n",
-              walltime_str(curclock()), dp->host->hostname, dp->name);
-       fflush(stdout);
-       /* Note: fall through code... */
+       cmd = getresult(dumper->fd, 1, &result_argc, result_argv, MAX_ARGS+1);
 
-    case BOGUS:
-       /*
-        * Since we've gotten a tape error, we can't send anything more
-        * to the taper.  Go into degraded mode to try to get everthing
-        * onto disk.  Later, these dumps can be flushed to a new tape.
-        * The tape queue is zapped so that it appears empty in future
-        * checks. If there are dumps waiting for diskspace to be freed,
-        * cancel one.
-        */
-       if(!nodump) {
-           log_add(L_WARNING,
-                   "going into degraded mode because of tape error.");
+       if(cmd != BOGUS) {
+           /* result_argv[2] always contains the serial number */
+           sdp = serial2disk(result_argv[2]);
+           if (sdp != dp) {
+               error("%s: Invalid serial number: %s", get_pname(), result_argv[2]);
+               /*NOTREACHED*/
+           }
        }
-       start_degraded_mode(&runq);
-       taper_busy = 0;
-       taper_disk = NULL;
-       tapeq.head = tapeq.tail = NULL;
-       FD_CLR(taper,&readset);
-       if(cmd != TAPE_ERROR) aclose(taper);
-       continue_dumps();
-       break;
-    default:
-       error("driver received unexpected token (%d) from taper", cmd);
-    }
-}
 
+       qname = quote_string(dp->name);
+       switch(cmd) {
 
-dumper_t *idle_dumper()
-{
-    dumper_t *dumper;
+       case DONE: /* DONE <handle> <origsize> <dumpsize> <dumptime> <errstr> */
+           if(result_argc != 6) {
+               error("error [dumper DONE result_argc != 6: %d]", result_argc);
+               /*NOTREACHED*/
+           }
 
-    for(dumper = dmptable; dumper < dmptable+inparallel; dumper++)
-       if(!dumper->busy && !dumper->down) return dumper;
+           /*free_serial(result_argv[2]);*/
 
-    return NULL;
-}
+           sched(dp)->origsize = OFF_T_ATOI(result_argv[3]);
+           sched(dp)->dumptime = TIME_T_ATOI(result_argv[5]);
 
-int some_dumps_in_progress()
-{
-    dumper_t *dumper;
+           printf("driver: finished-cmd time %s %s dumped %s:%s\n",
+                  walltime_str(curclock()), dumper->name,
+                  dp->host->hostname, qname);
+           fflush(stdout);
 
-    for(dumper = dmptable; dumper < dmptable+inparallel; dumper++)
-       if(dumper->busy) return 1;
+           dumper->result = cmd;
 
-    return taper_busy;
-}
+           break;
 
-int num_busy_dumpers()
-{
-    dumper_t *dumper;
-    int n;
+       case TRYAGAIN: /* TRY-AGAIN <handle> <errstr> */
+           /*
+            * Requeue this disk, and fall through to the FAILED
+            * case for cleanup.
+            */
+           if(sched(dp)->attempted) {
+               log_add(L_FAIL, "%s %s %s %d [too many dumper retry: %s]",
+                   dp->host->hostname, dp->name, sched(dp)->datestamp,
+                   sched(dp)->level, result_argv[3]);
+               printf("driver: dump failed %s %s %s, too many dumper retry: %s\n",
+                       result_argv[2], dp->host->hostname, dp->name,
+                       result_argv[3]);
+           }
+           /* FALLTHROUGH */
+       case FAILED: /* FAILED <handle> <errstr> */
+           /*free_serial(result_argv[2]);*/
+           dumper->result = cmd;
+           break;
 
-    n = 0;
-    for(dumper = dmptable; dumper < dmptable+inparallel; dumper++)
-       if(dumper->busy) n += 1;
+       case ABORT_FINISHED: /* ABORT-FINISHED <handle> */
+           /*
+            * We sent an ABORT from the NO-ROOM case because this dump
+            * wasn't going to fit onto the holding disk.  We now need to
+            * clean up the remains of this image, and try to finish
+            * other dumps that are waiting on disk space.
+            */
+           assert(pending_aborts);
+           /*free_serial(result_argv[2]);*/
+           dumper->result = cmd;
+           break;
 
-    return n;
-}
+       case BOGUS:
+           /* either EOF or garbage from dumper.  Turn it off */
+           log_add(L_WARNING, "%s pid %ld is messed up, ignoring it.\n",
+                   dumper->name, (long)dumper->pid);
+           if (dumper->ev_read) {
+               event_release(dumper->ev_read);
+               dumper->ev_read = NULL;
+           }
+           aclose(dumper->fd);
+           dumper->busy = 0;
+           dumper->down = 1;   /* mark it down so it isn't used again */
+           if(dp) {
+               /* if it was dumping something, zap it and try again */
+               if(sched(dp)->attempted) {
+               log_add(L_FAIL, "%s %s %s %d [%s died]",
+                       dp->host->hostname, qname, sched(dp)->datestamp,
+                       sched(dp)->level, dumper->name);
+               }
+               else {
+               log_add(L_WARNING, "%s died while dumping %s:%s lev %d.",
+                       dumper->name, dp->host->hostname, qname,
+                       sched(dp)->level);
+               }
+           }
+           dumper->result = cmd;
+           break;
 
-dumper_t *lookup_dumper(fd)
-int fd;
-{
-    dumper_t *dumper;
+       default:
+           assert(0);
+       }
+        amfree(qname);
 
-    for(dumper = dmptable; dumper < dmptable+inparallel; dumper++)
-       if(dumper->outfd == fd) return dumper;
+       /* send the dumper result to the chunker */
+       if(dumper->chunker->down == 0 && dumper->chunker->fd != -1 &&
+          dumper->chunker->result == LAST_TOK) {
+           if(cmd == DONE) {
+               chunker_cmd(dumper->chunker, DONE, dp);
+           }
+           else {
+               chunker_cmd(dumper->chunker, FAILED, dp);
+           }
+       }
 
-    return NULL;
+       if(dumper->result != LAST_TOK && dumper->chunker->result != LAST_TOK)
+           dumper_result(dp);
+
+    } while(areads_dataready(dumper->fd));
 }
 
 
-void handle_dumper_result(fd)
-     int fd;
+static void
+handle_chunker_result(
+    void *     cookie)
 {
+    /*static int pending_aborts = 0;*/
+    chunker_t *chunker = cookie;
     assignedhd_t **h=NULL;
     dumper_t *dumper;
     disk_t *dp, *sdp;
-    long origsize;
-    long dumpsize;
-    long dumptime;
     cmd_t cmd;
     int result_argc;
     char *result_argv[MAX_ARGS+1];
-    int i, dummy;
+    int dummy;
     int activehd = -1;
+    char *qname;
 
-    dumper = lookup_dumper(fd);
+    assert(chunker != NULL);
+    dumper = chunker->dumper;
+    assert(dumper != NULL);
     dp = dumper->dp;
-    assert(dp && sched(dp) && sched(dp)->destname);
+    assert(dp != NULL);
+    assert(sched(dp) != NULL);
+    assert(sched(dp)->destname != NULL);
+    assert(dp != NULL && sched(dp) != NULL && sched(dp)->destname);
 
     if(dp && sched(dp) && sched(dp)->holdp) {
        h = sched(dp)->holdp;
        activehd = sched(dp)->activehd;
     }
 
-    cmd = getresult(fd, 1, &result_argc, result_argv, MAX_ARGS+1);
+    do {
 
-    if(cmd != BOGUS) {
-       sdp = serial2disk(result_argv[2]); /* result_argv[2] always contains the serial number */
-       assert(sdp == dp);
-    }
+       short_dump_state();
 
-    switch(cmd) {
+       cmd = getresult(chunker->fd, 1, &result_argc, result_argv, MAX_ARGS+1);
 
-    case DONE: /* DONE <handle> <origsize> <dumpsize> <dumptime> <err str> */
-       if(result_argc != 6) {
-           error("error [dumper DONE result_argc != 6: %d]", result_argc);
+       if(cmd != BOGUS) {
+           /* result_argv[2] always contains the serial number */
+           sdp = serial2disk(result_argv[2]);
+           if (sdp != dp) {
+               error("%s: Invalid serial number: %s", get_pname(), result_argv[2]);
+               /*NOTREACHED*/
+           }
        }
 
-       free_serial(result_argv[2]);
+       switch(cmd) {
 
-       origsize = (long)atof(result_argv[3]);
-       dumpsize = (long)atof(result_argv[4]);
-       dumptime = (long)atof(result_argv[5]);
-       update_info_dumper(dp, origsize, dumpsize, dumptime);
+       case PARTIAL: /* PARTIAL <handle> <dumpsize> <errstr> */
+       case DONE: /* DONE <handle> <dumpsize> <errstr> */
+           if(result_argc != 4) {
+               error("error [chunker %s result_argc != 4: %d]", cmdstr[cmd],
+                     result_argc);
+               /*NOTREACHED*/
+           }
+           /*free_serial(result_argv[2]);*/
 
-       /* adjust holdp[active]->used using the real dumpsize and all other
-        * holdp[i]->used as an estimate.
-        */
+           sched(dp)->dumpsize = (off_t)atof(result_argv[3]);
 
-       dummy = 0;
-       for( i = 0, h = sched(dp)->holdp; i < activehd; i++ ) {
-           dummy += h[i]->used;
-       }
+           qname = quote_string(dp->name);
+           printf("driver: finished-cmd time %s %s chunked %s:%s\n",
+                  walltime_str(curclock()), chunker->name,
+                  dp->host->hostname, qname);
+           fflush(stdout);
+            amfree(qname);
 
-       rename_tmp_holding(sched(dp)->destname, 1);
-       assert( h && activehd >= 0 );
-       h[activehd]->used = size_holding_files(sched(dp)->destname) - dummy;
-       deallocate_bandwidth(dp->host->netif, sched(dp)->est_kps);
-       holdalloc(h[activehd]->disk)->allocated_dumpers--;
-       adjust_diskspace(dp, DONE);
-       dumper->busy = 0;
-       dp->host->inprogress -= 1;
-       dp->inprogress = 0;
-       sched(dp)->attempted = 0;
-       printf("driver: finished-cmd time %s %s dumped %s:%s\n",
-              walltime_str(curclock()), dumper->name,
-              dp->host->hostname, dp->name);
-       fflush(stdout);
+           event_release(chunker->ev_read);
 
-       enqueue_disk(&tapeq, dp);
-       dp = NULL;
+           chunker->result = cmd;
 
-       startaflush();
-       continue_dumps();
+           break;
 
-       break;
+       case TRYAGAIN: /* TRY-AGAIN <handle> <errstr> */
+           event_release(chunker->ev_read);
 
-    case TRYAGAIN: /* TRY-AGAIN <handle> <err str> */
-    case FATAL_TRYAGAIN:
-       free_serial(result_argv[2]);
+           chunker->result = cmd;
 
-       rename_tmp_holding(sched(dp)->destname, 0);
-       deallocate_bandwidth(dp->host->netif, sched(dp)->est_kps);
-       assert( h && activehd >= 0 );
-       holdalloc(h[activehd]->disk)->allocated_dumpers--;
-       /* Because we don't know how much was written to disk the
-        * following functions *must* be called together!
-        */
-       adjust_diskspace(dp, DONE);
-       delete_diskspace(dp);
-       dumper->busy = 0;
-       dp->host->inprogress -= 1;
-       dp->inprogress = 0;
-
-       if(sched(dp)->attempted) {
-           log_add(L_FAIL, "%s %s %d %s[could not connect to %s]",
-                   dp->host->hostname, dp->name,
-                   sched(dp)->level, sched(dp)->datestamp, dp->host->hostname);
-       } else {
-           sched(dp)->attempted++;
-           enqueue_disk(&runq, dp);
-       }
-       continue_dumps();
+           break;
+       case FAILED: /* FAILED <handle> <errstr> */
+           /*free_serial(result_argv[2]);*/
 
-       if(cmd == FATAL_TRYAGAIN) {
-           /* dumper is confused, start another */
-           log_add(L_WARNING, "%s (pid %ld) confused, restarting it.",
-                   dumper->name, (long)dumper->pid);
-           FD_CLR(fd,&readset);
-           aclose(fd);
-           startup_dump_process(dumper, dumper_program);
-       }
-       /* sleep in case the dumper failed because of a temporary network
-          problem, as NIS or NFS... */
-       sleep(15);
-       break;
+           event_release(chunker->ev_read);
 
-    case FAILED: /* FAILED <handle> <errstr> */
-       free_serial(result_argv[2]);
+           chunker->result = cmd;
 
-       rename_tmp_holding(sched(dp)->destname, 0);
-       deallocate_bandwidth(dp->host->netif, sched(dp)->est_kps);
-       assert( h && activehd >= 0 );
-       holdalloc(h[activehd]->disk)->allocated_dumpers--;
-       /* Because we don't know how much was written to disk the
-        * following functions *must* be called together!
-        */
-       adjust_diskspace(dp, DONE);
-       delete_diskspace(dp);
-       dumper->busy = 0;
-       dp->host->inprogress -= 1;
-       dp->inprogress = 0;
-       continue_dumps();
-
-       /* no need to log this, dumper will do it */
-       /* sleep in case the dumper failed because of a temporary network
-          problem, as NIS or NFS... */
-       sleep(15);
-       break;
+           break;
 
-    case NO_ROOM: /* NO-ROOM <handle> <missing_size> */
-       assert( h && activehd >= 0 );
-       h[activehd]->used -= atoi(result_argv[3]);
-       h[activehd]->reserved -= atoi(result_argv[3]);
-       holdalloc(h[activehd]->disk)->allocated_space -= atoi(result_argv[3]);
-       h[activehd]->disk->disksize -= atoi(result_argv[3]);
-       break;
+       case NO_ROOM: /* NO-ROOM <handle> <missing_size> */
+           if (!h || activehd < 0) { /* should never happen */
+               error("!h || activehd < 0");
+               /*NOTREACHED*/
+           }
+           h[activehd]->used -= OFF_T_ATOI(result_argv[3]);
+           h[activehd]->reserved -= OFF_T_ATOI(result_argv[3]);
+           holdalloc(h[activehd]->disk)->allocated_space -= OFF_T_ATOI(result_argv[3]);
+           h[activehd]->disk->disksize -= OFF_T_ATOI(result_argv[3]);
+           break;
 
-    case RQ_MORE_DISK: /* RQ-MORE-DISK <handle> */
-       assert( h && activehd >= 0 );
-       holdalloc(h[activehd]->disk)->allocated_dumpers--;
-       h[activehd]->used = h[activehd]->reserved;
-       if( h[++activehd] ) { /* There's still some allocated space left. Tell
-                              * the dumper about it. */
-           sched(dp)->activehd++;
-           dumper_cmd( dumper, CONTINUE, dp );
-       } else { /* !h[++activehd] - must allocate more space */
-           sched(dp)->act_size = sched(dp)->est_size; /* not quite true */
-           sched(dp)->est_size = sched(dp)->act_size * 21 / 20; /* +5% */
-           sched(dp)->est_size = am_round(sched(dp)->est_size, DISK_BLOCK_KB);
-           h = find_diskspace( sched(dp)->est_size - sched(dp)->act_size,
-                               &dummy,
-                               h[activehd-1] );
-           if( !h ) {
-    /*     cur_idle = max(cur_idle, IDLE_NO_DISKSPACE); */
-               /* No diskspace available. The reason for this will be
-                * determined in continue_dumps(). */
-               enqueue_disk( &roomq, dp );
-               continue_dumps();
-           } else {
-               /* OK, allocate space for disk and have dumper continue */
-               sched(dp)->activehd = assign_holdingdisk( h, dp );
-               dumper_cmd( dumper, CONTINUE, dp );
-               amfree(h);
+       case RQ_MORE_DISK: /* RQ-MORE-DISK <handle> */
+           if (!h || activehd < 0) { /* should never happen */
+               error("!h || activehd < 0");
+               /*NOTREACHED*/
            }
-       }
-       break;
+           holdalloc(h[activehd]->disk)->allocated_dumpers--;
+           h[activehd]->used = h[activehd]->reserved;
+           if( h[++activehd] ) { /* There's still some allocated space left.
+                                  * Tell the dumper about it. */
+               sched(dp)->activehd++;
+               chunker_cmd( chunker, CONTINUE, dp );
+           } else { /* !h[++activehd] - must allocate more space */
+               sched(dp)->act_size = sched(dp)->est_size; /* not quite true */
+               sched(dp)->est_size = (sched(dp)->act_size/(off_t)20) * (off_t)21; /* +5% */
+               sched(dp)->est_size = am_round(sched(dp)->est_size, (off_t)DISK_BLOCK_KB);
+               if (sched(dp)->est_size < sched(dp)->act_size + 2*DISK_BLOCK_KB)
+                   sched(dp)->est_size += 2 * DISK_BLOCK_KB;
+               h = find_diskspace( sched(dp)->est_size - sched(dp)->act_size,
+                                   &dummy,
+                                   h[activehd-1] );
+               if( !h ) {
+                   /* No diskspace available. The reason for this will be
+                    * determined in continue_port_dumps(). */
+                   enqueue_disk( &roomq, dp );
+                   continue_port_dumps();
+               } else {
+                   /* OK, allocate space for disk and have chunker continue */
+                   sched(dp)->activehd = assign_holdingdisk( h, dp );
+                   chunker_cmd( chunker, CONTINUE, dp );
+                   amfree(h);
+               }
+           }
+           break;
 
-    case ABORT_FINISHED: /* ABORT-FINISHED <handle> */
-       assert(pending_aborts);
-       free_serial(result_argv[2]);
+       case ABORT_FINISHED: /* ABORT-FINISHED <handle> */
+           /*
+            * We sent an ABORT from the NO-ROOM case because this dump
+            * wasn't going to fit onto the holding disk.  We now need to
+            * clean up the remains of this image, and try to finish
+            * other dumps that are waiting on disk space.
+            */
+           /*assert(pending_aborts);*/
 
-       rename_tmp_holding(sched(dp)->destname, 0);
-       deallocate_bandwidth(dp->host->netif, sched(dp)->est_kps);
-       /* Because we don't know how much was written to disk the
-        * following functions *must* be called together!
-        */
-       adjust_diskspace(dp, DONE);
-       delete_diskspace(dp);
-       sched(dp)->attempted++;
-       enqueue_disk(&runq, dp);        /* we'll try again later */
-       dumper->busy = 0;
-       dp->host->inprogress -= 1;
-       dp->inprogress = 0;
-       dp = NULL;
-       pending_aborts--;
-       continue_dumps();
-       break;
+           /*free_serial(result_argv[2]);*/
 
-    case BOGUS:
-       /* either EOF or garbage from dumper.  Turn it off */
-       log_add(L_WARNING, "%s pid %ld is messed up, ignoring it.\n",
-               dumper->name, (long)dumper->pid);
-       FD_CLR(fd,&readset);
-       aclose(fd);
-       dumper->busy = 0;
-       dumper->down = 1;       /* mark it down so it isn't used again */
-       if(dp) {
-           /* if it was dumping something, zap it and try again */
-           rename_tmp_holding(sched(dp)->destname, 0);
-           deallocate_bandwidth(dp->host->netif, sched(dp)->est_kps);
-           assert( h && activehd >= 0 );
-           holdalloc(h[activehd]->disk)->allocated_dumpers--;
-           /* Because we don't know how much was written to disk the
-            * following functions *must* be called together!
-            */
-           adjust_diskspace(dp, DONE);
-           delete_diskspace(dp);
-           dp->host->inprogress -= 1;
-           dp->inprogress = 0;
-           if(sched(dp)->attempted) {
-               log_add(L_FAIL, "%s %s %d %s [%s died]",
-                       dp->host->hostname, dp->name,
-                       sched(dp)->level, sched(dp)->datestamp, dumper->name);
-           }
-           else {
-               log_add(L_WARNING, "%s died while dumping %s:%s lev %d.",
-                       dumper->name, dp->host->hostname, dp->name,
-                       sched(dp)->level);
-               sched(dp)->attempted++;
-               enqueue_disk(&runq, dp);
+           event_release(chunker->ev_read);
+
+           chunker->result = cmd;
+
+           break;
+
+       case BOGUS:
+           /* either EOF or garbage from chunker.  Turn it off */
+           log_add(L_WARNING, "%s pid %ld is messed up, ignoring it.\n",
+                   chunker->name, (long)chunker->pid);
+
+           if(dp) {
+               /* if it was dumping something, zap it and try again */
+               if (!h || activehd < 0) { /* should never happen */
+                   error("!h || activehd < 0");
+                   /*NOTREACHED*/
+               }
+               qname = quote_string(dp->name);
+               if(sched(dp)->attempted) {
+                   log_add(L_FAIL, "%s %s %s %d [%s died]",
+                           dp->host->hostname, qname, sched(dp)->datestamp,
+                           sched(dp)->level, chunker->name);
+               }
+               else {
+                   log_add(L_WARNING, "%s died while dumping %s:%s lev %d.",
+                           chunker->name, dp->host->hostname, qname,
+                           sched(dp)->level);
+               }
+               amfree(qname);
+               dp = NULL;
            }
-           dp = NULL;
-           continue_dumps();
+
+           event_release(chunker->ev_read);
+
+           chunker->result = cmd;
+
+           break;
+
+       default:
+           assert(0);
        }
-       break;
 
-    default:
-       assert(0);
-    }
+       if(chunker->result != LAST_TOK && chunker->dumper->result != LAST_TOK)
+           dumper_result(dp);
 
-    return;
+    } while(areads_dataready(chunker->fd));
 }
 
 
-void read_flush(tapeqp)
-disklist_t *tapeqp;
+static disklist_t
+read_flush(void)
 {
     sched_t *sp;
     disk_t *dp;
@@ -1315,20 +1795,24 @@ disklist_t *tapeqp;
     char *command;
     char *s;
     int ch;
-    long flush_size = 0;
+    disklist_t tq;
+    char *qname = NULL;
+    char *qdestname = NULL;
 
-    /* read schedule from stdin */
+    tq.head = tq.tail = NULL;
 
     for(line = 0; (inpline = agets(stdin)) != NULL; free(inpline)) {
        line++;
+       if (inpline[0] == '\0')
+           continue;
 
        s = inpline;
        ch = *s++;
 
-       skip_whitespace(s, ch);                 /* find the command */
+       skip_whitespace(s, ch);                 /* find the command */
        if(ch == '\0') {
-           error("Aflush line %d: syntax error", line);
-           continue;
+           error("flush line %d: syntax error (no command)", line);
+           /*NOTREACHED*/
        }
        command = s - 1;
        skip_non_whitespace(s, ch);
@@ -1339,14 +1823,14 @@ disklist_t *tapeqp;
        }
 
        if(strcmp(command,"FLUSH") != 0) {
-           error("Bflush line %d: syntax error", line);
-           continue;
+           error("flush line %d: syntax error (%s != FLUSH)", line, command);
+           /*NOTREACHED*/
        }
 
        skip_whitespace(s, ch);                 /* find the hostname */
        if(ch == '\0') {
-           error("Cflush line %d: syntax error", line);
-           continue;
+           error("flush line %d: syntax error (no hostname)", line);
+           /*NOTREACHED*/
        }
        hostname = s - 1;
        skip_non_whitespace(s, ch);
@@ -1354,17 +1838,18 @@ disklist_t *tapeqp;
 
        skip_whitespace(s, ch);                 /* find the diskname */
        if(ch == '\0') {
-           error("Cflush line %d: syntax error", line);
-           continue;
+           error("flush line %d: syntax error (no diskname)", line);
+           /*NOTREACHED*/
        }
-       diskname = s - 1;
-       skip_non_whitespace(s, ch);
-       s[-1] = '\0';
+       qname = s - 1;
+       skip_quoted_string(s, ch);
+       s[-1] = '\0';                           /* terminate the disk name */
+       diskname = unquote_string(qname);
 
        skip_whitespace(s, ch);                 /* find the datestamp */
        if(ch == '\0') {
-           error("Cflush line %d: syntax error", line);
-           continue;
+           error("flush line %d: syntax error (no datestamp)", line);
+           /*NOTREACHED*/
        }
        datestamp = s - 1;
        skip_non_whitespace(s, ch);
@@ -1372,24 +1857,26 @@ disklist_t *tapeqp;
 
        skip_whitespace(s, ch);                 /* find the level number */
        if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
-           error("Cflush line %d: syntax error", line);
-           continue;
+           error("flush line %d: syntax error (bad level)", line);
+           /*NOTREACHED*/
        }
        skip_integer(s, ch);
 
        skip_whitespace(s, ch);                 /* find the filename */
        if(ch == '\0') {
-           error("Cflush line %d: syntax error", line);
-           continue;
+           error("flush line %d: syntax error (no filename)", line);
+           /*NOTREACHED*/
        }
-       destname = s - 1;
-       skip_non_whitespace(s, ch);
+       qdestname = s - 1;
+       skip_quoted_string(s, ch);
        s[-1] = '\0';
+       destname = unquote_string(qdestname);
 
-       get_dumpfile(destname, &file);
+       holding_file_get_dumpfile(destname, &file);
        if( file.type != F_DUMPFILE) {
            if( file.type != F_CONT_DUMPFILE )
                log_add(L_INFO, "%s: ignoring cruft file.", destname);
+           amfree(diskname);
            continue;
        }
 
@@ -1398,8 +1885,10 @@ disklist_t *tapeqp;
           strcmp(datestamp, file.datestamp) != 0) {
            log_add(L_INFO, "disk %s:%s not consistent with file %s",
                    hostname, diskname, destname);
+           amfree(diskname);
            continue;
        }
+       amfree(diskname);
 
        dp = lookup_disk(file.name, file.disk);
 
@@ -1415,13 +1904,19 @@ disklist_t *tapeqp;
            continue;
        }
 
-       dp1 = (disk_t *)alloc(sizeof(disk_t));
+       if (holding_file_size(destname,1) <= 0) {
+           log_add(L_INFO, "%s: removing file with no data.", destname);
+           holding_file_unlink(destname);
+           continue;
+       }
+
+       dp1 = (disk_t *)alloc(SIZEOF(disk_t));
        *dp1 = *dp;
        dp1->next = dp1->prev = NULL;
 
        /* add it to the flushhost list */
        if(!flushhost) {
-           flushhost = alloc(sizeof(host_t));
+           flushhost = alloc(SIZEOF(am_host_t));
            flushhost->next = NULL;
            flushhost->hostname = stralloc("FLUSHHOST");
            flushhost->up = NULL;
@@ -1430,53 +1925,68 @@ disklist_t *tapeqp;
        dp1->hostnext = flushhost->disks;
        flushhost->disks = dp1;
 
-       sp = (sched_t *) alloc(sizeof(sched_t));
+       sp = (sched_t *) alloc(SIZEOF(sched_t));
        sp->destname = stralloc(destname);
        sp->level = file.dumplevel;
        sp->dumpdate = NULL;
        sp->degr_dumpdate = NULL;
        sp->datestamp = stralloc(file.datestamp);
-       sp->est_size = 0;
+       sp->est_nsize = (off_t)0;
+       sp->est_csize = (off_t)0;
        sp->est_time = 0;
+       sp->est_kps = 10;
        sp->priority = 0;
        sp->degr_level = -1;
-       sp->est_kps = 10;
        sp->attempted = 0;
-       sp->act_size = size_holding_files(destname);
-       /*sp->holdp = NULL; JLM: must be build*/
+       sp->act_size = holding_file_size(destname, 0);
        sp->holdp = build_diskspace(destname);
-        if(sp->holdp == NULL) continue;
+       if(sp->holdp == NULL) continue;
        sp->dumper = NULL;
        sp->timestamp = (time_t)0;
 
        dp1->up = (char *)sp;
 
-       enqueue_disk(tapeqp, dp1);
-       flush_size += sp->act_size;
+       enqueue_disk(&tq, dp1);
     }
-    printf("driver: flush size %ld\n", flush_size);
     amfree(inpline);
-}
 
+    /*@i@*/ return tq;
+}
 
-void read_schedule(waitqp, runqp)
-disklist_t *waitqp, *runqp;
+static void
+read_schedule(
+    void *     cookie)
 {
     sched_t *sp;
     disk_t *dp;
     int level, line, priority;
     char *dumpdate, *degr_dumpdate;
     int degr_level;
-    long time, degr_time;
-    unsigned long size, degr_size;
+    time_t time, degr_time;
+    time_t *time_p = &time;
+    time_t *degr_time_p = &degr_time;
+    off_t nsize, csize, degr_nsize, degr_csize;
+    unsigned long kps, degr_kps;
     char *hostname, *features, *diskname, *datestamp, *inpline = NULL;
     char *command;
     char *s;
     int ch;
+    off_t flush_size = (off_t)0;
+    char *qname = NULL;
+    OFF_T_FMT_TYPE nsize_;
+    OFF_T_FMT_TYPE csize_;
+    OFF_T_FMT_TYPE degr_nsize_;
+    OFF_T_FMT_TYPE degr_csize_;
+
+    (void)cookie;      /* Quiet unused parameter warning */
+
+    event_release(schedule_ev_read);
 
     /* read schedule from stdin */
 
     for(line = 0; (inpline = agets(stdin)) != NULL; free(inpline)) {
+       if (inpline[0] == '\0')
+           continue;
        line++;
 
        s = inpline;
@@ -1485,7 +1995,7 @@ disklist_t *waitqp, *runqp;
        skip_whitespace(s, ch);                 /* find the command */
        if(ch == '\0') {
            error("schedule line %d: syntax error (no command)", line);
-           continue;
+           /*NOTREACHED*/
        }
        command = s - 1;
        skip_non_whitespace(s, ch);
@@ -1493,13 +2003,13 @@ disklist_t *waitqp, *runqp;
 
        if(strcmp(command,"DUMP") != 0) {
            error("schedule line %d: syntax error (%s != DUMP)", line, command);
-           continue;
+           /*NOTREACHED*/
        }
 
        skip_whitespace(s, ch);                 /* find the host name */
        if(ch == '\0') {
            error("schedule line %d: syntax error (no host name)", line);
-           continue;
+           /*NOTREACHED*/
        }
        hostname = s - 1;
        skip_non_whitespace(s, ch);
@@ -1508,7 +2018,7 @@ disklist_t *waitqp, *runqp;
        skip_whitespace(s, ch);                 /* find the feature list */
        if(ch == '\0') {
            error("schedule line %d: syntax error (no feature list)", line);
-           continue;
+           /*NOTREACHED*/
        }
        features = s - 1;
        skip_non_whitespace(s, ch);
@@ -1517,16 +2027,17 @@ disklist_t *waitqp, *runqp;
        skip_whitespace(s, ch);                 /* find the disk name */
        if(ch == '\0') {
            error("schedule line %d: syntax error (no disk name)", line);
-           continue;
+           /*NOTREACHED*/
        }
-       diskname = s - 1;
-       skip_non_whitespace(s, ch);
-       s[-1] = '\0';
+       qname = s - 1;
+       skip_quoted_string(s, ch);
+       s[-1] = '\0';                           /* terminate the disk name */
+       diskname = unquote_string(qname);
 
        skip_whitespace(s, ch);                 /* find the datestamp */
        if(ch == '\0') {
            error("schedule line %d: syntax error (no datestamp)", line);
-           continue;
+           /*NOTREACHED*/
        }
        datestamp = s - 1;
        skip_non_whitespace(s, ch);
@@ -1535,36 +2046,55 @@ disklist_t *waitqp, *runqp;
        skip_whitespace(s, ch);                 /* find the priority number */
        if(ch == '\0' || sscanf(s - 1, "%d", &priority) != 1) {
            error("schedule line %d: syntax error (bad priority)", line);
-           continue;
+           /*NOTREACHED*/
        }
        skip_integer(s, ch);
 
        skip_whitespace(s, ch);                 /* find the level number */
        if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
            error("schedule line %d: syntax error (bad level)", line);
-           continue;
+           /*NOTREACHED*/
        }
        skip_integer(s, ch);
 
        skip_whitespace(s, ch);                 /* find the dump date */
        if(ch == '\0') {
            error("schedule line %d: syntax error (bad dump date)", line);
-           continue;
+           /*NOTREACHED*/
        }
        dumpdate = s - 1;
        skip_non_whitespace(s, ch);
        s[-1] = '\0';
 
-       skip_whitespace(s, ch);                 /* find the size number */
-       if(ch == '\0' || sscanf(s - 1, "%lu", &size) != 1) {
-           error("schedule line %d: syntax error (bad size)", line);
-           continue;
+       skip_whitespace(s, ch);                 /* find the native size */
+       nsize_ = (OFF_T_FMT_TYPE)0;
+       if(ch == '\0' || sscanf(s - 1, OFF_T_FMT, &nsize_) != 1) {
+           error("schedule line %d: syntax error (bad nsize)", line);
+           /*NOTREACHED*/
+       }
+       nsize = (off_t)nsize_;
+       skip_integer(s, ch);
+
+       skip_whitespace(s, ch);                 /* find the compressed size */
+       csize_ = (OFF_T_FMT_TYPE)0;
+       if(ch == '\0' || sscanf(s - 1, OFF_T_FMT, &csize_) != 1) {
+           error("schedule line %d: syntax error (bad csize)", line);
+           /*NOTREACHED*/
        }
+       csize = (off_t)csize_;
        skip_integer(s, ch);
 
        skip_whitespace(s, ch);                 /* find the time number */
-       if(ch == '\0' || sscanf(s - 1, "%ld", &time) != 1) {
+       if(ch == '\0' || sscanf(s - 1, TIME_T_FMT,
+                               (TIME_T_FMT_TYPE *)time_p) != 1) {
            error("schedule line %d: syntax error (bad estimated time)", line);
+           /*NOTREACHED*/
+       }
+       skip_integer(s, ch);
+
+       skip_whitespace(s, ch);                 /* find the kps number */
+       if(ch == '\0' || sscanf(s - 1, "%lu", &kps) != 1) {
+           error("schedule line %d: syntax error (bad kps)", line);
            continue;
        }
        skip_integer(s, ch);
@@ -1574,74 +2104,99 @@ disklist_t *waitqp, *runqp;
        if(ch != '\0') {
            if(sscanf(s - 1, "%d", &degr_level) != 1) {
                error("schedule line %d: syntax error (bad degr level)", line);
-               continue;
+               /*NOTREACHED*/
            }
            skip_integer(s, ch);
 
            skip_whitespace(s, ch);             /* find the degr dump date */
            if(ch == '\0') {
                error("schedule line %d: syntax error (bad degr dump date)", line);
-               continue;
+               /*NOTREACHED*/
            }
            degr_dumpdate = s - 1;
            skip_non_whitespace(s, ch);
            s[-1] = '\0';
 
-           skip_whitespace(s, ch);             /* find the degr size number */
-           if(ch == '\0'  || sscanf(s - 1, "%lu", &degr_size) != 1) {
-               error("schedule line %d: syntax error (bad degr size)", line);
-               continue;
+           skip_whitespace(s, ch);             /* find the degr native size */
+           degr_nsize_ = (OFF_T_FMT_TYPE)0;
+           if(ch == '\0'  || sscanf(s - 1, OFF_T_FMT, &degr_nsize_) != 1) {
+               error("schedule line %d: syntax error (bad degr nsize)", line);
+               /*NOTREACHED*/
            }
+           degr_nsize = (off_t)degr_nsize_;
+           skip_integer(s, ch);
+
+           skip_whitespace(s, ch);             /* find the degr compressed size */
+           degr_csize_ = (OFF_T_FMT_TYPE)0;
+           if(ch == '\0'  || sscanf(s - 1, OFF_T_FMT, &degr_csize_) != 1) {
+               error("schedule line %d: syntax error (bad degr csize)", line);
+               /*NOTREACHED*/
+           }
+           degr_csize = (off_t)degr_csize_;
            skip_integer(s, ch);
 
            skip_whitespace(s, ch);             /* find the degr time number */
-           if(ch == '\0' || sscanf(s - 1, "%lu", &degr_time) != 1) {
+           if(ch == '\0' || sscanf(s - 1, TIME_T_FMT,
+                               (TIME_T_FMT_TYPE *)degr_time_p) != 1) {
                error("schedule line %d: syntax error (bad degr estimated time)", line);
-               continue;
+               /*NOTREACHED*/
            }
            skip_integer(s, ch);
+
+           skip_whitespace(s, ch);             /* find the degr kps number */
+           if(ch == '\0' || sscanf(s - 1, "%lu", &degr_kps) != 1) {
+               error("schedule line %d: syntax error (bad degr kps)", line);
+               /*NOTREACHED*/
+           }
+           skip_integer(s, ch);
+       } else {
+           degr_level = -1;
+           degr_nsize = (off_t)0;
+           degr_csize = (off_t)0;
+           degr_time = (time_t)0;
+           degr_kps = 0;
        }
 
        dp = lookup_disk(hostname, diskname);
        if(dp == NULL) {
            log_add(L_WARNING,
-                   "schedule line %d: %s:%s not in disklist, ignored",
-                   line, hostname, diskname);
+                   "schedule line %d: %s:'%s' not in disklist, ignored",
+                   line, hostname, qname);
+           amfree(diskname);
            continue;
        }
 
-       sp = (sched_t *) alloc(sizeof(sched_t));
-       sp->level    = level;
+       sp = (sched_t *) alloc(SIZEOF(sched_t));
+       /*@ignore@*/
+       sp->level = level;
        sp->dumpdate = stralloc(dumpdate);
-       sp->est_size = DISK_BLOCK_KB + size; /* include header */
+       sp->est_nsize = DISK_BLOCK_KB + nsize; /* include header */
+       sp->est_csize = DISK_BLOCK_KB + csize; /* include header */
+       /* round estimate to next multiple of DISK_BLOCK_KB */
+       sp->est_csize = am_round(sp->est_csize, DISK_BLOCK_KB);
+       sp->est_size = sp->est_csize;
        sp->est_time = time;
+       sp->est_kps = kps;
        sp->priority = priority;
        sp->datestamp = stralloc(datestamp);
 
        if(degr_dumpdate) {
            sp->degr_level = degr_level;
            sp->degr_dumpdate = stralloc(degr_dumpdate);
-           sp->degr_size = DISK_BLOCK_KB + degr_size;
+           sp->degr_nsize = DISK_BLOCK_KB + degr_nsize;
+           sp->degr_csize = DISK_BLOCK_KB + degr_csize;
+           /* round estimate to next multiple of DISK_BLOCK_KB */
+           sp->degr_csize = am_round(sp->degr_csize, DISK_BLOCK_KB);
            sp->degr_time = degr_time;
+           sp->degr_kps = degr_kps;
        } else {
            sp->degr_level = -1;
            sp->degr_dumpdate = NULL;
        }
-
-       if(time <= 0)
-           sp->est_kps = 10;
-       else
-           sp->est_kps = size/time;
-
-       if(sp->degr_level != -1) {
-           if(degr_time <= 0)
-               sp->degr_kps = 10;
-           else
-               sp->degr_kps = degr_size/degr_time;
-       }
+       /*@end@*/
 
        sp->attempted = 0;
-       sp->act_size = 0;
+       sp->act_size = (off_t)0;
        sp->holdp = NULL;
        sp->activehd = -1;
        sp->dumper = NULL;
@@ -1653,87 +2208,98 @@ disklist_t *waitqp, *runqp;
        if(dp->host->features == NULL) {
            dp->host->features = am_string_to_feature(features);
        }
-       remove_disk(waitqp, dp);
-       insert_disk(&runq, dp, sort_by_time);
+       remove_disk(&waitq, dp);
+       enqueue_disk(&runq, dp);
+       flush_size += sp->act_size;
+       amfree(diskname);
     }
+    printf("driver: flush size " OFF_T_FMT "\n", (OFF_T_FMT_TYPE)flush_size);
     amfree(inpline);
     if(line == 0)
        log_add(L_WARNING, "WARNING: got empty schedule from planner");
+    if(need_degraded==1) start_degraded_mode(&runq);
+    start_some_dumps(&runq);
 }
 
-int free_kps(ip)
-interface_t *ip;
+static unsigned long
+free_kps(
+    interface_t *ip)
 {
-    int res;
+    unsigned long res;
 
     if (ip == (interface_t *)0) {
        interface_t *p;
-       int maxusage=0;
-       int curusage=0;
+       unsigned long maxusage=0;
+       unsigned long curusage=0;
        for(p = lookup_interface(NULL); p != NULL; p = p->next) {
-           maxusage += p->maxusage;
+           maxusage += interface_get_maxusage(p);
            curusage += p->curusage;
        }
-       res = maxusage - curusage;
-    }
-    else {
-       res = ip->maxusage - ip->curusage;
+       if (maxusage >= curusage)
+           res = maxusage - curusage;
+       else
+           res = 0;
+#ifndef __lint
+    } else {
+       if ((unsigned long)interface_get_maxusage(ip) >= ip->curusage)
+           res = interface_get_maxusage(ip) - ip->curusage;
+       else
+           res = 0;
+#endif
     }
 
     return res;
 }
 
-void interface_state(time_str)
-char *time_str;
+static void
+interface_state(
+    char *time_str)
 {
     interface_t *ip;
 
     printf("driver: interface-state time %s", time_str);
 
     for(ip = lookup_interface(NULL); ip != NULL; ip = ip->next) {
-       printf(" if %s: free %d", ip->name, free_kps(ip));
+       printf(" if %s: free %lu", ip->name, free_kps(ip));
     }
     printf("\n");
 }
 
-void allocate_bandwidth(ip, kps)
-interface_t *ip;
-int kps;
+static void
+allocate_bandwidth(
+    interface_t *      ip,
+    unsigned long      kps)
 {
     ip->curusage += kps;
 }
 
-void deallocate_bandwidth(ip, kps)
-interface_t *ip;
-int kps;
+static void
+deallocate_bandwidth(
+    interface_t *      ip,
+    unsigned long      kps)
 {
     assert(kps <= ip->curusage);
     ip->curusage -= kps;
 }
 
 /* ------------ */
-unsigned long free_space()
+static off_t
+free_space(void)
 {
     holdingdisk_t *hdp;
-    unsigned long total_free;
-    long diff;
+    off_t total_free;
+    off_t diff;
 
-    total_free = 0L;
+    total_free = (off_t)0;
     for(hdp = getconf_holdingdisks(); hdp != NULL; hdp = hdp->next) {
        diff = hdp->disksize - holdalloc(hdp)->allocated_space;
-       if(diff > 0)
+       if(diff > (off_t)0)
            total_free += diff;
     }
     return total_free;
 }
 
-assignedhd_t **find_diskspace(size, cur_idle, pref)
-unsigned long size;
-int *cur_idle;
-assignedhd_t *pref;
-/* Rewrite by Peter Conrad <conrad@opus5.de>, June '99:
- *  - enable splitting a dump across several holding disks
- *  - allocate only as much as size tells us, dumpers may request more later
+/*
  * We return an array of pointers to assignedhd_t. The array contains at
  * most one entry per holding disk. The list of pointers is terminated by
  * a NULL pointer. Each entry contains a pointer to a holdingdisk and
@@ -1741,44 +2307,53 @@ assignedhd_t *pref;
  * will allocate the given amount of space.
  * If there is not enough room on the holdingdisks, NULL is returned.
  */
+
+static assignedhd_t **
+find_diskspace(
+    off_t              size,
+    int *              cur_idle,
+    assignedhd_t *     pref)
 {
-assignedhd_t **result = NULL;
+    assignedhd_t **result = NULL;
     holdingdisk_t *minp, *hdp;
     int i=0, num_holdingdisks=0; /* are we allowed to use the global thing? */
     int j, minj;
     char *used;
-    long halloc, dalloc, hfree, dfree;
+    off_t halloc, dalloc, hfree, dfree;
 
-    size = am_round(size, DISK_BLOCK_KB);
+    (void)cur_idle;    /* Quiet unused parameter warning */
 
-#ifdef HOLD_DEBUG
-    printf("find diskspace: want %lu K\n", size );
-    fflush(stdout);
-#endif
+    if (size < 2*DISK_BLOCK_KB)
+       size = 2*DISK_BLOCK_KB;
+    size = am_round(size, (off_t)DISK_BLOCK_KB);
+
+    hold_debug(1, ("%s: want " OFF_T_FMT " K\n",
+                  debug_prefix_time(": find_diskspace"),
+                  (OFF_T_FMT_TYPE)size));
 
     for(hdp = getconf_holdingdisks(); hdp != NULL; hdp = hdp->next) {
        num_holdingdisks++;
     }
 
-    used = alloc(sizeof(char) * num_holdingdisks);/*disks used during this run*/
-    memset( used, 0, num_holdingdisks );
-    result = alloc( sizeof(assignedhd_t *) * (num_holdingdisks+1) );
+    used = alloc(SIZEOF(*used) * num_holdingdisks);/*disks used during this run*/
+    memset( used, 0, (size_t)num_holdingdisks );
+    result = alloc(SIZEOF(assignedhd_t *) * (num_holdingdisks + 1));
     result[0] = NULL;
 
-    while( i < num_holdingdisks && size > 0 ) {
+    while( i < num_holdingdisks && size > (off_t)0 ) {
        /* find the holdingdisk with the fewest active dumpers and among
         * those the one with the biggest free space
         */
        minp = NULL; minj = -1;
        for(j = 0, hdp = getconf_holdingdisks(); hdp != NULL; hdp = hdp->next, j++ ) {
            if( pref && pref->disk == hdp && !used[j] &&
-               holdalloc(hdp)->allocated_space <= hdp->disksize - DISK_BLOCK_KB) {
+               holdalloc(hdp)->allocated_space <= hdp->disksize - (off_t)DISK_BLOCK_KB) {
                minp = hdp;
                minj = j;
                break;
            }
-           else if( holdalloc(hdp)->allocated_space <= hdp->disksize - 2*DISK_BLOCK_KB &&
-               !used[j] && 
+           else if( holdalloc(hdp)->allocated_space <= hdp->disksize - (off_t)(2*DISK_BLOCK_KB) &&
+               !used[j] &&
                (!minp ||
                 holdalloc(hdp)->allocated_dumpers < holdalloc(minp)->allocated_dumpers ||
                 (holdalloc(hdp)->allocated_dumpers == holdalloc(minp)->allocated_dumpers &&
@@ -1787,6 +2362,7 @@ assignedhd_t **result = NULL;
                minj = j;
            }
        }
+
        pref = NULL;
        if( !minp ) { break; } /* all holding disks are full */
        used[minj] = 1;
@@ -1795,78 +2371,83 @@ assignedhd_t **result = NULL;
        hfree = minp->disksize - holdalloc(minp)->allocated_space;
 
        /* dfree = free space for data, remove 1 header for each chunksize */
-       dfree = hfree - (((hfree-1)/minp->chunksize)+1) * DISK_BLOCK_KB;
+       dfree = hfree - (((hfree-(off_t)1)/holdingdisk_get_chunksize(minp))+(off_t)1) * (off_t)DISK_BLOCK_KB;
 
        /* dalloc = space I can allocate for data */
        dalloc = ( dfree < size ) ? dfree : size;
 
        /* halloc = space to allocate, including 1 header for each chunksize */
-       halloc = dalloc + (((dalloc-1)/minp->chunksize)+1) * DISK_BLOCK_KB;
-
-#ifdef HOLD_DEBUG
-       fprintf(stdout,"find diskspace: size %ld hf %ld df %ld da %ld ha %ld\n",                size, hfree, dfree, dalloc, halloc);
-       fflush(stdout);
-#endif
+       halloc = dalloc + (((dalloc-(off_t)1)/holdingdisk_get_chunksize(minp))+(off_t)1) * (off_t)DISK_BLOCK_KB;
+
+       hold_debug(1, ("%s: find diskspace: size " OFF_T_FMT " hf " OFF_T_FMT
+                      " df " OFF_T_FMT " da " OFF_T_FMT " ha " OFF_T_FMT"\n",
+                      debug_prefix_time(": find_diskspace"),
+                      (OFF_T_FMT_TYPE)size,
+                      (OFF_T_FMT_TYPE)hfree,
+                      (OFF_T_FMT_TYPE)dfree,
+                      (OFF_T_FMT_TYPE)dalloc,
+                      (OFF_T_FMT_TYPE)halloc));
        size -= dalloc;
-       result[i] = alloc(sizeof(assignedhd_t));
+       result[i] = alloc(SIZEOF(assignedhd_t));
        result[i]->disk = minp;
        result[i]->reserved = halloc;
-       result[i]->used = 0;
+       result[i]->used = (off_t)0;
        result[i]->destname = NULL;
        result[i+1] = NULL;
        i++;
     } /* while i < num_holdingdisks && size > 0 */
     amfree(used);
 
-    if( size ) { /* not enough space available */
-#ifdef HOLD_DEBUG
-       printf("find diskspace: not enough diskspace. Left with %lu K\n", size);
+    if(size != (off_t)0) { /* not enough space available */
+       printf("find diskspace: not enough diskspace. Left with "
+              OFF_T_FMT " K\n", (OFF_T_FMT_TYPE)size);
        fflush(stdout);
-#endif
        free_assignedhd(result);
        result = NULL;
     }
 
-#ifdef HOLD_DEBUG
-    for( i = 0; result && result[i]; i++ ) {
-    printf("find diskspace: selected %s free %ld reserved %ld dumpers %d\n",
-           result[i]->disk->diskdir,
-           result[i]->disk->disksize - holdalloc(result[i]->disk)->allocated_space,
-          result[i]->reserved,
-           holdalloc(result[i]->disk)->allocated_dumpers);
+    if (debug_holding > 1) {
+       for( i = 0; result && result[i]; i++ ) {
+           hold_debug(1, ("%s: find diskspace: selected %s free " OFF_T_FMT
+                          " reserved " OFF_T_FMT " dumpers %d\n",
+                          debug_prefix_time(": find_diskspace"),
+                          holdingdisk_get_diskdir(result[i]->disk),
+                          (OFF_T_FMT_TYPE)(result[i]->disk->disksize -
+                            holdalloc(result[i]->disk)->allocated_space),
+                          (OFF_T_FMT_TYPE)result[i]->reserved,
+                          holdalloc(result[i]->disk)->allocated_dumpers));
+       }
     }
-    fflush(stdout);
-#endif
 
     return result;
 }
 
-int assign_holdingdisk(holdp, diskp)
-assignedhd_t **holdp;
-disk_t *diskp;
+static int
+assign_holdingdisk(
+    assignedhd_t **    holdp,
+    disk_t *           diskp)
 {
-/* Modified by Peter Conrad <conrad@opus5.de>, June '99
- * Modifications for splitting dumps across holding disks:
- * sched(diskp)->holdp now contains an array of pointers to assignedhd_t.
- */
     int i, j, c, l=0;
-    unsigned long size;
+    off_t size;
     char *sfn = sanitise_filename(diskp->name);
     char lvl[64];
     assignedhd_t **new_holdp;
+    char *qname;
 
-    ap_snprintf( lvl, sizeof(lvl), "%d", sched(diskp)->level );
+    snprintf( lvl, SIZEOF(lvl), "%d", sched(diskp)->level );
 
     size = am_round(sched(diskp)->est_size - sched(diskp)->act_size,
-                   DISK_BLOCK_KB);
+                   (off_t)DISK_BLOCK_KB);
 
-    for( c = 0; holdp[c]; c++ ); /* count number of disks */
+    for( c = 0; holdp[c]; c++ )
+       (void)c; /* count number of disks */
 
     /* allocate memory for sched(diskp)->holdp */
-    for(j = 0; sched(diskp)->holdp && sched(diskp)->holdp[j]; j++) {}
-    new_holdp = (assignedhd_t **)alloc(sizeof(assignedhd_t*)*(j+c+1));
+    for(j = 0; sched(diskp)->holdp && sched(diskp)->holdp[j]; j++)
+       (void)j;        /* Quiet lint */
+    new_holdp = (assignedhd_t **)alloc(SIZEOF(assignedhd_t*)*(j+c+1));
     if (sched(diskp)->holdp) {
-       memcpy(new_holdp, sched(diskp)->holdp, j * sizeof(*new_holdp));
+       memcpy(new_holdp, sched(diskp)->holdp, j * SIZEOF(*new_holdp));
        amfree(sched(diskp)->holdp);
     }
     sched(diskp)->holdp = new_holdp;
@@ -1879,16 +2460,20 @@ disk_t *diskp;
        if( sched(diskp)->holdp[j-1]->disk == holdp[0]->disk ) { /* Yes! */
            sched(diskp)->holdp[j-1]->reserved += holdp[0]->reserved;
            holdalloc(holdp[0]->disk)->allocated_space += holdp[0]->reserved;
-           size = (holdp[0]->reserved>size) ? 0 : size-holdp[0]->reserved;
-#ifdef HOLD_DEBUG
-           printf("merging holding disk %s to disk %s:%s, add %lu for reserved %lu, left %lu\n",
-                  sched(diskp)->holdp[j-1]->disk->diskdir,
-                  diskp->host->hostname, diskp->name,
-                  holdp[0]->reserved, sched(diskp)->holdp[j-1]->reserved,
-                  size );
-           fflush(stdout);
-#endif
+           size = (holdp[0]->reserved>size) ? (off_t)0 : size-holdp[0]->reserved;
+           qname = quote_string(diskp->name);
+           hold_debug(1, ("%s: merging holding disk %s to disk %s:%s, add "
+                          OFF_T_FMT " for reserved " OFF_T_FMT ", left "
+                          OFF_T_FMT "\n",
+                          debug_prefix_time(": assign_holdingdisk"),
+                          holdingdisk_get_diskdir(
+                                              sched(diskp)->holdp[j-1]->disk),
+                          diskp->host->hostname, qname,
+                          (OFF_T_FMT_TYPE)holdp[0]->reserved,
+                          (OFF_T_FMT_TYPE)sched(diskp)->holdp[j-1]->reserved,
+                          (OFF_T_FMT_TYPE)size));
            i++;
+           amfree(qname);
            amfree(holdp[0]);
            l=j-1;
        }
@@ -1897,135 +2482,138 @@ disk_t *diskp;
     /* copy assignedhd_s to sched(diskp), adjust allocated_space */
     for( ; holdp[i]; i++ ) {
        holdp[i]->destname = newvstralloc( holdp[i]->destname,
-                                          holdp[i]->disk->diskdir, "/",
-                                          datestamp, "/",
+                                          holdingdisk_get_diskdir(holdp[i]->disk), "/",
+                                          hd_driver_timestamp, "/",
                                           diskp->host->hostname, ".",
                                           sfn, ".",
                                           lvl, NULL );
        sched(diskp)->holdp[j++] = holdp[i];
        holdalloc(holdp[i]->disk)->allocated_space += holdp[i]->reserved;
-       size = (holdp[i]->reserved>size) ? 0 : size-holdp[i]->reserved;
-#ifdef HOLD_DEBUG
-        printf("assigning holding disk %s to disk %s:%s, reserved %lu, left %lu\n",
-                holdp[i]->disk->diskdir, diskp->host->hostname, diskp->name,
-                holdp[i]->reserved, size );
-        fflush(stdout);
-#endif
+       size = (holdp[i]->reserved > size) ? (off_t)0 :
+                 (size - holdp[i]->reserved);
+       qname = quote_string(diskp->name);
+       hold_debug(1,
+                  ("%s: %d assigning holding disk %s to disk %s:%s, reserved "
+                    OFF_T_FMT ", left " OFF_T_FMT "\n",
+                   debug_prefix_time(": assign_holdingdisk"),
+                   i, holdingdisk_get_diskdir(holdp[i]->disk),
+                   diskp->host->hostname, qname,
+                   (OFF_T_FMT_TYPE)holdp[i]->reserved,
+                   (OFF_T_FMT_TYPE)size));
+       amfree(qname);
        holdp[i] = NULL; /* so it doesn't get free()d... */
     }
     sched(diskp)->holdp[j] = NULL;
-    sched(diskp)->destname = newstralloc(sched(diskp)->destname,sched(diskp)->holdp[0]->destname);
     amfree(sfn);
 
     return l;
 }
 
-static void adjust_diskspace(diskp, cmd)
-disk_t *diskp;
-cmd_t cmd;
+static void
+adjust_diskspace(
+    disk_t *   diskp,
+    cmd_t      cmd)
 {
-/* Re-write by Peter Conrad <conrad@opus5.de>, March '99
- * Modifications for splitting dumps across holding disks:
- * Dumpers no longer write more than they've allocated, therefore an
- * adjustment may only free some allocated space.
- * 08/99: Jean-Louis suggested that dumpers tell us how much they've written.
- * We just believe them and don't stat all the files but rely on the used
- * field.
- */
-
     assignedhd_t **holdp;
-    unsigned long total=0;
-    long diff;
+    off_t total = (off_t)0;
+    off_t diff;
     int i;
+    char *qname, *hqname, *qdest;
 
-#ifdef HOLD_DEBUG
-    printf("adjust: %s:%s %s\n", diskp->host->hostname, diskp->name,
-           sched(diskp)->destname );
-    fflush(stdout);
-#endif
+    (void)cmd; /* Quiet unused parameter warning */
+
+    qname = quote_string(diskp->name);
+    qdest = quote_string(sched(diskp)->destname);
+    hold_debug(1, ("%s: %s:%s %s\n",
+                  debug_prefix_time(": adjust_diskspace"),
+                  diskp->host->hostname, qname, qdest));
 
     holdp = sched(diskp)->holdp;
 
-    assert(holdp);
+    assert(holdp != NULL);
 
     for( i = 0; holdp[i]; i++ ) { /* for each allocated disk */
        diff = holdp[i]->used - holdp[i]->reserved;
        total += holdp[i]->used;
        holdalloc(holdp[i]->disk)->allocated_space += diff;
-#ifdef HOLD_DEBUG
-       printf("adjust: hdisk %s done, reserved %ld used %ld diff %ld alloc %ld dumpers %d\n",
-               holdp[i]->disk->name, holdp[i]->reserved, holdp[i]->used, diff,
-               holdalloc(holdp[i]->disk)->allocated_space,
-               holdalloc(holdp[i]->disk)->allocated_dumpers );
-               fflush(stdout);
-#endif
+       hqname = quote_string(holdp[i]->disk->name);
+       hold_debug(1, ("%s: hdisk %s done, reserved " OFF_T_FMT " used "
+                      OFF_T_FMT " diff " OFF_T_FMT " alloc " OFF_T_FMT
+                      " dumpers %d\n",
+                      debug_prefix_time(": adjust_diskspace"),
+                      holdp[i]->disk->name,
+                      (OFF_T_FMT_TYPE)holdp[i]->reserved,
+                      (OFF_T_FMT_TYPE)holdp[i]->used,
+                      (OFF_T_FMT_TYPE)diff,
+                      (OFF_T_FMT_TYPE)holdalloc(holdp[i]->disk)
+                                                            ->allocated_space,
+                      holdalloc(holdp[i]->disk)->allocated_dumpers ));
        holdp[i]->reserved += diff;
+       amfree(hqname);
     }
 
     sched(diskp)->act_size = total;
-#ifdef HOLD_DEBUG
-    printf("adjust: after: disk %s:%s used %ld\n", diskp->host->hostname,
-          diskp->name, sched(diskp)->act_size );
-    fflush(stdout);
-#endif
+
+    hold_debug(1, ("%s: after: disk %s:%s used " OFF_T_FMT "\n",
+                  debug_prefix_time(": adjust_diskspace"),
+                  diskp->host->hostname, qname,
+                  (OFF_T_FMT_TYPE)sched(diskp)->act_size));
+    amfree(qdest);
+    amfree(qname);
 }
 
-static void delete_diskspace(diskp)
-disk_t *diskp;
+static void
+delete_diskspace(
+    disk_t *diskp)
 {
-/* Re-write by Peter Conrad <conrad@opus5.de>, March '99
- * Modifications for splitting dumps across holding disks:
- * After implementing Jean-Louis' suggestion (see above) this looks much
- * simpler... again, we rely on assignedhd_s containing correct info
- */
     assignedhd_t **holdp;
     int i;
 
     holdp = sched(diskp)->holdp;
 
-    assert(holdp);
+    assert(holdp != NULL);
 
     for( i = 0; holdp[i]; i++ ) { /* for each disk */
-        /* find all files of this dump on that disk, and subtract their
-         * reserved sizes from the disk's allocated space
-         */
+       /* find all files of this dump on that disk, and subtract their
+        * reserved sizes from the disk's allocated space
+        */
        holdalloc(holdp[i]->disk)->allocated_space -= holdp[i]->used;
     }
 
-    unlink_holding_files(holdp[0]->destname); /* no need for the entire list, 
-                                                        because unlink_holding_files
-                                                will walk through all files
-                                                        using cont_filename */
-
+    holding_file_unlink(holdp[0]->destname);   /* no need for the entire list,
+                                                * because holding_file_unlink
+                                                * will walk through all files
+                                                * using cont_filename */
     free_assignedhd(sched(diskp)->holdp);
     sched(diskp)->holdp = NULL;
-    sched(diskp)->act_size = 0;
-    amfree(sched(diskp)->destname);
+    sched(diskp)->act_size = (off_t)0;
 }
 
-assignedhd_t **build_diskspace(destname)
-char *destname;
+static assignedhd_t **
+build_diskspace(
+    char *     destname)
 {
     int i, j;
     int fd;
-    int buflen;
+    ssize_t buflen;
     char buffer[DISK_BLOCK_BYTES];
     dumpfile_t file;
     assignedhd_t **result;
     holdingdisk_t *hdp;
-    int *used;
+    off_t *used;
     int num_holdingdisks=0;
     char dirname[1000], *ch;
     struct stat finfo;
     char *filename = destname;
 
+    memset(buffer, 0, sizeof(buffer));
     for(hdp = getconf_holdingdisks(); hdp != NULL; hdp = hdp->next) {
         num_holdingdisks++;
     }
-    used = alloc(sizeof(int) * num_holdingdisks);
+    used = alloc(SIZEOF(off_t) * num_holdingdisks);
     for(i=0;i<num_holdingdisks;i++)
-       used[i] = 0;
-    result = alloc( sizeof(assignedhd_t *) * (num_holdingdisks+1) );
+       used[i] = (off_t)0;
+    result = alloc(SIZEOF(assignedhd_t *) * (num_holdingdisks + 1));
     result[0] = NULL;
     while(filename != NULL && filename[0] != '\0') {
        strncpy(dirname, filename, 999);
@@ -2037,31 +2625,32 @@ char *destname;
 
        for(j = 0, hdp = getconf_holdingdisks(); hdp != NULL;
                                                 hdp = hdp->next, j++ ) {
-           if(strcmp(dirname,hdp->diskdir)==0) {
+           if(strcmp(dirname, holdingdisk_get_diskdir(hdp))==0) {
                break;
            }
        }
 
        if(stat(filename, &finfo) == -1) {
            fprintf(stderr, "stat %s: %s\n", filename, strerror(errno));
-           finfo.st_size = 0;
+           finfo.st_size = (off_t)0;
        }
-       used[j] += (finfo.st_size+1023)/1024;
+       used[j] += ((off_t)finfo.st_size+(off_t)1023)/(off_t)1024;
        if((fd = open(filename,O_RDONLY)) == -1) {
            fprintf(stderr,"build_diskspace: open of %s failed: %s\n",
                    filename, strerror(errno));
            return NULL;
        }
-       buflen = fullread(fd, buffer, sizeof(buffer));
-       parse_file_header(buffer, &file, buflen);
+       if ((buflen = fullread(fd, buffer, SIZEOF(buffer))) > 0) {;
+               parse_file_header(buffer, &file, (size_t)buflen);
+       }
        close(fd);
        filename = file.cont_filename;
     }
 
     for(j = 0, i=0, hdp = getconf_holdingdisks(); hdp != NULL;
                                                  hdp = hdp->next, j++ ) {
-       if(used[j]) {
-           result[i] = alloc(sizeof(assignedhd_t));
+       if(used[j] != (off_t)0) {
+           result[i] = alloc(SIZEOF(assignedhd_t));
            result[i]->disk = hdp;
            result[i]->reserved = used[j];
            result[i]->used = used[j];
@@ -2075,27 +2664,32 @@ char *destname;
     return result;
 }
 
-
-void holdingdisk_state(time_str)
-char *time_str;
+static void
+holdingdisk_state(
+    char *     time_str)
 {
     holdingdisk_t *hdp;
     int dsk;
-    long diff;
+    off_t diff;
 
     printf("driver: hdisk-state time %s", time_str);
 
     for(hdp = getconf_holdingdisks(), dsk = 0; hdp != NULL; hdp = hdp->next, dsk++) {
        diff = hdp->disksize - holdalloc(hdp)->allocated_space;
-       printf(" hdisk %d: free %ld dumpers %d", dsk, diff,
-              holdalloc(hdp)->allocated_dumpers);
+       printf(" hdisk %d: free " OFF_T_FMT " dumpers %d", dsk,
+              (OFF_T_FMT_TYPE)diff, holdalloc(hdp)->allocated_dumpers);
     }
     printf("\n");
 }
 
-static void update_failed_dump_to_tape(dp)
-disk_t *dp;
+static void
+update_failed_dump_to_tape(
+    disk_t *   dp)
 {
+/* JLM
+ * should simply set no_bump
+ */
+
     time_t save_timestamp = sched(dp)->timestamp;
     /* setting timestamp to 0 removes the current level from the
      * database, so that we ensure that it will not be bumped to the
@@ -2103,28 +2697,31 @@ disk_t *dp;
      * gnutar-lists might have been updated already, and a bumped
      * incremental might be created.  */
     sched(dp)->timestamp = 0;
-    update_info_dumper(dp, -1, -1, -1);
+    update_info_dumper(dp, (off_t)-1, (off_t)-1, (time_t)-1);
     sched(dp)->timestamp = save_timestamp;
 }
 
 /* ------------------- */
-int dump_to_tape(dp)
-     disk_t *dp;
+static int
+dump_to_tape(
+    disk_t *   dp)
 {
     dumper_t *dumper;
     int failed = 0;
-    int filenum;
-    long origsize = 0;
-    long dumpsize = 0;
-    long dumptime = 0;
+    off_t filenum;
+    off_t origsize = (off_t)0;
+    off_t dumpsize = (off_t)0;
+    time_t dumptime = (time_t)0;
+    double tapetime = 0.0;
     cmd_t cmd;
-    int result_argc;
+    int result_argc, rc;
     char *result_argv[MAX_ARGS+1];
+    int dumper_tryagain = 0;
+    char *qname;
 
-    inside_dump_to_tape = 1;   /* for simulator */
-
+    qname = quote_string(dp->name);
     printf("driver: dumping %s:%s directly to tape\n",
-          dp->host->hostname, dp->name);
+          dp->host->hostname, qname);
     fflush(stdout);
 
     /* pick a dumper and fail if there are no idle dumpers */
@@ -2132,11 +2729,11 @@ int dump_to_tape(dp)
     dumper = idle_dumper();
     if (!dumper) {
        printf("driver: no idle dumpers for %s:%s.\n", 
-               dp->host->hostname, dp->name);
+               dp->host->hostname, qname);
        fflush(stdout);
        log_add(L_WARNING, "no idle dumpers for %s:%s.\n",
-               dp->host->hostname, dp->name);
-       inside_dump_to_tape = 0;
+               dp->host->hostname, qname);
+        amfree(qname);
        return 2;       /* fatal problem */
     }
 
@@ -2146,13 +2743,13 @@ int dump_to_tape(dp)
     cmd = getresult(taper, 1, &result_argc, result_argv, MAX_ARGS+1);
     if(cmd != PORT) {
        printf("driver: did not get PORT from taper for %s:%s\n",
-               dp->host->hostname, dp->name);
+               dp->host->hostname, qname);
        fflush(stdout);
-       inside_dump_to_tape = 0;
+        amfree(qname);
        return 2;       /* fatal problem */
     }
     /* copy port number */
-    sched(dp)->destname = newvstralloc(sched(dp)->destname, result_argv[2], NULL );
+    dumper->output_port = atoi(result_argv[2]);
 
     /* tell the dumper to dump to a port */
 
@@ -2166,16 +2763,13 @@ int dump_to_tape(dp)
     dp->inprogress = 1;
     sched(dp)->timestamp = time((time_t *)0);
     allocate_bandwidth(dp->host->netif, sched(dp)->est_kps);
-    idle_reason = 0;
+    idle_reason = NOT_IDLE;
 
     short_dump_state();
 
     /* wait for result from dumper */
 
-    cmd = getresult(dumper->outfd, 1, &result_argc, result_argv, MAX_ARGS+1);
-
-    if(cmd != BOGUS)
-       free_serial(result_argv[2]);
+    cmd = getresult(dumper->fd, 1, &result_argc, result_argv, MAX_ARGS+1);
 
     switch(cmd) {
     case BOGUS:
@@ -2186,24 +2780,25 @@ int dump_to_tape(dp)
        failed = 1;     /* dump failed, must still finish up with taper */
        break;
 
-    case DONE: /* DONE <handle> <origsize> <dumpsize> <dumptime> <err str> */
+    case DONE: /* DONE <handle> <origsize> <dumpsize> <dumptime> <errstr> */
        /* everything went fine */
-       origsize = (long)atof(result_argv[3]);
-       dumpsize = (long)atof(result_argv[4]);
-       dumptime = (long)atof(result_argv[5]);
+       origsize = (off_t)atof(result_argv[3]);
+       /*dumpsize = (off_t)atof(result_argv[4]);*/
+       dumptime = (time_t)atof(result_argv[5]);
        break;
 
     case NO_ROOM: /* NO-ROOM <handle> */
        dumper_cmd(dumper, ABORT, dp);
-       cmd = getresult(dumper->outfd, 1, &result_argc, result_argv, MAX_ARGS+1);
-       if(cmd != BOGUS)
-           free_serial(result_argv[2]);
+       cmd = getresult(dumper->fd, 1, &result_argc, result_argv, MAX_ARGS+1);
        assert(cmd == ABORT_FINISHED);
 
-    case TRYAGAIN: /* TRY-AGAIN <handle> <err str> */
+    case TRYAGAIN: /* TRY-AGAIN <handle> <errstr> */
     default:
        /* dump failed, but we must still finish up with taper */
-       failed = 1;     /* problem with dump, possibly nonfatal */
+       /* problem with dump, possibly nonfatal, retry one time */
+       sched(dp)->attempted++;
+       failed = sched(dp)->attempted;
+       dumper_tryagain = 1;
        break;
        
     case FAILED: /* FAILED <handle> <errstr> */
@@ -2220,12 +2815,16 @@ int dump_to_tape(dp)
      * "no space on device", etc., since taper closed the port first.
      */
 
+    continue_port_dump:
+
     cmd = getresult(taper, 1, &result_argc, result_argv, MAX_ARGS+1);
 
     switch(cmd) {
+    case PARTIAL:
     case DONE: /* DONE <handle> <label> <tape file> <err mess> */
        if(result_argc != 5) {
            error("error [dump to tape DONE result_argc != 5: %d]", result_argc);
+           /*NOTREACHED*/
        }
 
        if(failed == 1) goto tryagain;  /* dump didn't work */
@@ -2233,25 +2832,67 @@ int dump_to_tape(dp)
 
        free_serial(result_argv[2]);
 
-       /* every thing went fine */
-       update_info_dumper(dp, origsize, dumpsize, dumptime);
-       filenum = atoi(result_argv[4]);
-       update_info_taper(dp, result_argv[3], filenum, sched(dp)->level);
-       /* note that update_info_dumper() must be run before
-          update_info_taper(), since update_info_dumper overwrites
-          tape information.  */
+       dumpsize = (off_t)0;
+       if (*result_argv[5] == '"') {
+           /* String was quoted */
+           OFF_T_FMT_TYPE dumpsize_ = (OFF_T_FMT_TYPE)0;
+           rc = sscanf(result_argv[5],"\"[sec %lf kb " OFF_T_FMT " ",
+                       &tapetime, &dumpsize_);
+           dumpsize = (off_t)dumpsize_;
+       } else {
+           /* String was not quoted */
+           OFF_T_FMT_TYPE dumpsize_ = (OFF_T_FMT_TYPE)0;
+           rc = sscanf(result_argv[5],"[sec %lf kb " OFF_T_FMT " ",
+                       &tapetime, &dumpsize_);
+           dumpsize = (off_t)dumpsize_;
+       }
+       if (rc < 2) {
+           error("error [malformed result: %d items matched in '%s']",
+                 rc, result_argv[5]);
+           /*NOTREACHED*/
+       }
+
+       if(cmd == DONE) {
+           /* every thing went fine */
+           update_info_dumper(dp, origsize, dumpsize, dumptime);
+           filenum = OFF_T_ATOI(result_argv[4]);
+           update_info_taper(dp, result_argv[3], filenum, sched(dp)->level);
+           /* note that update_info_dumper() must be run before
+              update_info_taper(), since update_info_dumper overwrites
+              tape information.  */
+       }
 
        break;
 
     case TRYAGAIN: /* TRY-AGAIN <handle> <err mess> */
+       tape_left = tape_length;
+       current_tape++;
+       if(dumper_tryagain == 0) {
+           sched(dp)->attempted++;
+           if(sched(dp)->attempted > failed)
+               failed = sched(dp)->attempted;
+       }
     tryagain:
-       headqueue_disk(&runq, dp);
+       if(failed <= 1)
+           headqueue_disk(&runq, dp);
     failed_dumper:
        update_failed_dump_to_tape(dp);
        free_serial(result_argv[2]);
-       tape_left = tape_length;
        break;
 
+    case SPLIT_CONTINUE:  /* SPLIT_CONTINUE <handle> <new_label> */
+        if (result_argc != 3) {
+            error("error [taper SPLIT_CONTINUE result_argc != 3: %d]", result_argc);
+           /*NOTREACHED*/
+        }
+        fprintf(stderr, "driver: Got SPLIT_CONTINUE %s %s\n",
+               result_argv[2], result_argv[3]);
+        goto continue_port_dump;
+
+    case SPLIT_NEEDNEXT:
+        fprintf(stderr, "driver: Got SPLIT_NEEDNEXT %s %s\n", result_argv[2], result_argv[3]);
+
+        goto continue_port_dump;
 
     case TAPE_ERROR: /* TAPE-ERROR <handle> <err mess> */
     case BOGUS:
@@ -2260,6 +2901,7 @@ int dump_to_tape(dp)
        free_serial(result_argv[2]);
        failed = 2;     /* fatal problem */
        start_degraded_mode(&runq);
+       break;
     }
 
     /* reset statistics & return */
@@ -2268,23 +2910,25 @@ int dump_to_tape(dp)
     dp->host->inprogress -= 1;
     dp->inprogress = 0;
     deallocate_bandwidth(dp->host->netif, sched(dp)->est_kps);
+    amfree(qname);
 
-    inside_dump_to_tape = 0;
     return failed;
 }
 
-int queue_length(q)
-disklist_t q;
+static int
+queue_length(
+    disklist_t q)
 {
     disk_t *p;
     int len;
 
-    for(len = 0, p = q.head; p != NULL; len++, p = p->next);
+    for(len = 0, p = q.head; p != NULL; len++, p = p->next)
+       (void)len;      /* Quiet lint */
     return len;
 }
 
-
-void short_dump_state()
+static void
+short_dump_state(void)
 {
     int i, nidle;
     char *wall_time;
@@ -2292,8 +2936,9 @@ void short_dump_state()
     wall_time = walltime_str(curclock());
 
     printf("driver: state time %s ", wall_time);
-    printf("free kps: %d space: %lu taper: ",
-          free_kps((interface_t *)0), free_space());
+    printf("free kps: %lu space: " OFF_T_FMT " taper: ",
+          free_kps((interface_t *)0),
+          (OFF_T_FMT_TYPE)free_space());
     if(degraded_mode) printf("DOWN");
     else if(!taper_busy) printf("idle");
     else printf("writing");
@@ -2303,25 +2948,30 @@ void short_dump_state()
     printf(" qlen tapeq: %d", queue_length(tapeq));
     printf(" runq: %d", queue_length(runq));
     printf(" roomq: %d", queue_length(roomq));
-    printf(" wakeup: %d", (int)sleep_time.tv_sec);
+    printf(" wakeup: %d", (int)sleep_time);
     printf(" driver-idle: %s\n", idle_strings[idle_reason]);
     interface_state(wall_time);
     holdingdisk_state(wall_time);
     fflush(stdout);
 }
 
-void dump_state(str)
-char *str;
+#if 0
+static void
+dump_state(
+    const char *str)
 {
     int i;
     disk_t *dp;
+    char *qname;
 
     printf("================\n");
     printf("driver state at time %s: %s\n", walltime_str(curclock()), str);
-    printf("free kps: %d, space: %lu\n", free_kps((interface_t *)0), free_space());
+    printf("free kps: %lu, space: " OFF_T_FMT "\n",
+          free_kps((interface_t *)0),
+          (OFF_T_FMT_TYPE)free_space());
     if(degraded_mode) printf("taper: DOWN\n");
     else if(!taper_busy) printf("taper: idle\n");
-    else printf("taper: writing %s:%s.%d est size %lu\n",
+    else printf("taper: writing %s:%s.%d est size " OFF_T_FMT "\n",
                taper_disk->host->hostname, taper_disk->name,
                sched(taper_disk)->level,
                sched(taper_disk)->est_size);
@@ -2330,9 +2980,11 @@ char *str;
        if(!dmptable[i].busy)
          printf("%s: idle\n", dmptable[i].name);
        else
-         printf("%s: dumping %s:%s.%d est kps %d size %lu time %ld\n",
-               dmptable[i].name, dp->host->hostname, dp->name, sched(dp)->level,
+         qname = quote_string(dp->name);
+         printf("%s: dumping %s:%s.%d est kps %d size " OFF_T_FMT " time %lu\n",
+               dmptable[i].name, dp->host->hostname, qname, sched(dp)->level,
                sched(dp)->est_kps, sched(dp)->est_size, sched(dp)->est_time);
+          amfree(qname);
     }
     dump_queue("TAPE", tapeq, 5, stdout);
     dump_queue("ROOM", roomq, 5, stdout);
@@ -2340,3 +2992,4 @@ char *str;
     printf("================\n");
     fflush(stdout);
 }
+#endif