Imported Upstream version 2.6.0
[debian/amanda] / server-src / driver.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998 University of Maryland at College Park
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of U.M. not be used in advertising or
11  * publicity pertaining to distribution of the software without specific,
12  * written prior permission.  U.M. makes no representations about the
13  * suitability of this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  *
16  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  * Authors: the Amanda Development Team.  Its members are listed in a
24  * file named AUTHORS, in the root directory of this distribution.
25  */
26 /*
27  * $Id: driver.c 6512 2007-05-24 17:00:24Z ian $
28  *
29  * controlling process for the Amanda backup system
30  */
31
32 /*
33  * XXX possibly modify tape queue to be cognizant of how much room is left on
34  *     tape.  Probably not effective though, should do this in planner.
35  */
36
37 #include "amanda.h"
38 #include "clock.h"
39 #include "conffile.h"
40 #include "diskfile.h"
41 #include "event.h"
42 #include "holding.h"
43 #include "infofile.h"
44 #include "logfile.h"
45 #include "fsusage.h"
46 #include "version.h"
47 #include "driverio.h"
48 #include "server_util.h"
49 #include "timestamp.h"
50
51 #define driver_debug(i, ...) do {       \
52         if ((i) <= debug_driver) {      \
53             dbprintf(__VA_ARGS__);      \
54         }                               \
55 } while (0)
56
57 #define hold_debug(i, ...) do {         \
58         if ((i) <= debug_holding) {     \
59             dbprintf(__VA_ARGS__);      \
60         }                               \
61 } while (0)
62
63 static disklist_t waitq;        // dle waiting estimate result
64 static disklist_t runq;         // dle waiting to be dumped to holding disk
65 static disklist_t directq;      // dle waiting to be dumped directly to tape
66 static disklist_t tapeq;        // dle on holding disk waiting to be written
67                                 //   to tape
68 static disklist_t roomq;        // dle waiting for more space on holding disk
69 static int pending_aborts;
70 static disk_t *taper_disk;
71 static int degraded_mode;
72 static off_t reserved_space;
73 static off_t total_disksize;
74 static char *dumper_program;
75 static char *chunker_program;
76 static int  inparallel;
77 static int nodump = 0;
78 static off_t tape_length = (off_t)0;
79 static off_t tape_left = (off_t)0;
80 static int current_tape = 0;
81 static int conf_taperalgo;
82 static int conf_runtapes;
83 static time_t sleep_time;
84 static int idle_reason;
85 static char *driver_timestamp;
86 static char *hd_driver_timestamp;
87 static am_host_t *flushhost = NULL;
88 static int need_degraded=0;
89 static holdalloc_t *holdalloc;
90 static int num_holdalloc;
91 static event_handle_t *dumpers_ev_time = NULL;
92 static event_handle_t *schedule_ev_read = NULL;
93 static int   conf_flush_threshold_dumped;
94 static int   conf_flush_threshold_scheduled;
95 static int   conf_taperflush;
96 static off_t flush_threshold_dumped;
97 static off_t flush_threshold_scheduled;
98 static off_t taperflush;
99 static int   schedule_done;                     // 1 if we don't wait for a
100                                                 //   schedule from the planner
101 static int   force_flush;                       // All dump are terminated, we
102                                                 // must now respect taper_flush
103
104 static int wait_children(int count);
105 static void wait_for_children(void);
106 static void allocate_bandwidth(netif_t *ip, unsigned long kps);
107 static int assign_holdingdisk(assignedhd_t **holdp, disk_t *diskp);
108 static void adjust_diskspace(disk_t *diskp, cmd_t cmd);
109 static void delete_diskspace(disk_t *diskp);
110 static assignedhd_t **build_diskspace(char *destname);
111 static int client_constrained(disk_t *dp);
112 static void deallocate_bandwidth(netif_t *ip, unsigned long kps);
113 static void dump_schedule(disklist_t *qp, char *str);
114 static void dump_to_tape(disk_t *dp);
115 static assignedhd_t **find_diskspace(off_t size, int *cur_idle,
116                                         assignedhd_t *preferred);
117 static unsigned long free_kps(netif_t *ip);
118 static off_t free_space(void);
119 static void dumper_chunker_result(disk_t *dp);
120 static void dumper_taper_result(disk_t *dp);
121 static void file_taper_result(disk_t *dp);
122 static void handle_dumper_result(void *);
123 static void handle_chunker_result(void *);
124 static void handle_dumpers_time(void *);
125 static void handle_taper_result(void *);
126
127 static void holdingdisk_state(char *time_str);
128 static dumper_t *idle_dumper(void);
129 static void interface_state(char *time_str);
130 static int queue_length(disklist_t q);
131 static disklist_t read_flush(void);
132 static void read_schedule(void *cookie);
133 static void short_dump_state(void);
134 static void startaflush(void);
135 static void start_degraded_mode(disklist_t *queuep);
136 static void start_some_dumps(disklist_t *rq);
137 static void continue_port_dumps(void);
138 static void update_failed_dump_to_tape(disk_t *);
139
140 typedef enum {
141     TAPE_ACTION_NO_ACTION     = 0,
142     TAPE_ACTION_NEW_TAPE      = (1 << 0),
143     TAPE_ACTION_NO_NEW_TAPE   = (1 << 1),
144     TAPE_ACTION_START_A_FLUSH = (1 << 2)
145 } TapeAction;
146
147 static TapeAction tape_action(void);
148
149 static const char *idle_strings[] = {
150 #define NOT_IDLE                0
151     T_("not-idle"),
152 #define IDLE_NO_DUMPERS         1
153     T_("no-dumpers"),
154 #define IDLE_START_WAIT         2
155     T_("start-wait"),
156 #define IDLE_NO_HOLD            3
157     T_("no-hold"),
158 #define IDLE_CLIENT_CONSTRAINED 4
159     T_("client-constrained"),
160 #define IDLE_NO_BANDWIDTH       5
161     T_("no-bandwidth"),
162 #define IDLE_NO_DISKSPACE       6
163     T_("no-diskspace")
164 };
165
166 int
167 main(
168     int         argc,
169     char **     argv)
170 {
171     disklist_t origq;
172     disk_t *diskp;
173     int dsk;
174     dumper_t *dumper;
175     char *newdir = NULL;
176     struct fs_usage fsusage;
177     holdingdisk_t *hdp;
178     unsigned long reserve = 100;
179     char *conf_diskfile;
180     cmd_t cmd;
181     int result_argc;
182     char *result_argv[MAX_ARGS+1];
183     char *taper_program;
184     char *conf_tapetype;
185     tapetype_t *tape;
186     char *line;
187     char hostname[1025];
188     intmax_t kb_avail;
189     config_overwrites_t *cfg_ovr = NULL;
190     char *cfg_opt = NULL;
191     holdalloc_t *ha, *ha_last;
192
193     /*
194      * Configure program for internationalization:
195      *   1) Only set the message locale for now.
196      *   2) Set textdomain for all amanda related programs to "amanda"
197      *      We don't want to be forced to support dozens of message catalogs.
198      */  
199     setlocale(LC_MESSAGES, "C");
200     textdomain("amanda");
201
202     safe_fd(-1, 0);
203
204     setvbuf(stdout, (char *)NULL, (int)_IOLBF, 0);
205     setvbuf(stderr, (char *)NULL, (int)_IOLBF, 0);
206
207     set_pname("driver");
208
209     dbopen(DBG_SUBDIR_SERVER);
210
211     atexit(wait_for_children);
212
213     /* Don't die when child closes pipe */
214     signal(SIGPIPE, SIG_IGN);
215
216     erroutput_type = (ERR_AMANDALOG|ERR_INTERACTIVE);
217     set_logerror(logerror);
218
219     startclock();
220
221     cfg_ovr = extract_commandline_config_overwrites(&argc, &argv);
222
223     if (argc > 1)
224         cfg_opt = argv[1];
225     config_init(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_USE_CWD | CONFIG_INIT_FATAL,
226                 cfg_opt);
227     apply_config_overwrites(cfg_ovr);
228
229     g_printf(_("%s: pid %ld executable %s version %s\n"),
230            get_pname(), (long) getpid(), argv[0], version());
231
232     if(argc > 2) {
233         if(strncmp(argv[2], "nodump", 6) == 0) {
234             nodump = 1;
235         }
236     }
237
238     safe_cd(); /* do this *after* config_init */
239
240     check_running_as(RUNNING_AS_DUMPUSER);
241
242     dbrename(config_name, DBG_SUBDIR_SERVER);
243
244     amfree(driver_timestamp);
245     /* read timestamp from stdin */
246     while ((line = agets(stdin)) != NULL) {
247         if (line[0] != '\0')
248             break;
249         amfree(line);
250     }
251     if ( line == NULL ) {
252       error(_("Did not get DATE line from planner"));
253       /*NOTREACHED*/
254     }
255     driver_timestamp = alloc(15);
256     strncpy(driver_timestamp, &line[5], 14);
257     driver_timestamp[14] = '\0';
258     amfree(line);
259     log_add(L_START,_("date %s"), driver_timestamp);
260
261     gethostname(hostname, SIZEOF(hostname));
262     log_add(L_STATS,_("hostname %s"), hostname);
263
264     /* check that we don't do many dump in a day and usetimestamps is off */
265     if(strlen(driver_timestamp) == 8) {
266         if (!nodump) {
267             char *conf_logdir = getconf_str(CNF_LOGDIR);
268             char *logfile    = vstralloc(conf_logdir, "/log.",
269                                          driver_timestamp, ".0", NULL);
270             char *oldlogfile = vstralloc(conf_logdir, "/oldlog/log.",
271                                          driver_timestamp, ".0", NULL);
272             if(access(logfile, F_OK) == 0 || access(oldlogfile, F_OK) == 0) {
273                 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."));
274             }
275             amfree(oldlogfile);
276             amfree(logfile);
277         }
278         hd_driver_timestamp = get_timestamp_from_time(0);
279     }
280     else {
281         hd_driver_timestamp = stralloc(driver_timestamp);
282     }
283
284     taper_program = vstralloc(amlibexecdir, "/", "taper", versionsuffix(), NULL);
285     dumper_program = vstralloc(amlibexecdir, "/", "dumper", versionsuffix(),
286                                NULL);
287     chunker_program = vstralloc(amlibexecdir, "/", "chunker", versionsuffix(),
288                                NULL);
289
290     conf_taperalgo = getconf_taperalgo(CNF_TAPERALGO);
291     conf_tapetype = getconf_str(CNF_TAPETYPE);
292     conf_runtapes = getconf_int(CNF_RUNTAPES);
293     tape = lookup_tapetype(conf_tapetype);
294     tape_length = tapetype_get_length(tape);
295     conf_flush_threshold_dumped = getconf_int(CNF_FLUSH_THRESHOLD_DUMPED);
296     conf_flush_threshold_scheduled = getconf_int(CNF_FLUSH_THRESHOLD_SCHEDULED);
297     conf_taperflush = getconf_int(CNF_TAPERFLUSH);
298
299     flush_threshold_dumped = (conf_flush_threshold_dumped * tape_length) / 100;
300     flush_threshold_scheduled = (conf_flush_threshold_scheduled * tape_length) / 100;
301     taperflush = (conf_taperflush *tape_length) / 100;
302
303     driver_debug(1, _("flush_threshold_dumped: %lld\n"), (long long)flush_threshold_dumped);
304     driver_debug(1, _("flush_threshold_scheduled: %lld\n"), (long long)flush_threshold_scheduled);
305     driver_debug(1, _("taperflush: %lld\n"), (long long)taperflush);
306
307     /* start initializing: read in databases */
308
309     conf_diskfile = config_dir_relative(getconf_str(CNF_DISKFILE));
310     if (read_diskfile(conf_diskfile, &origq) < 0) {
311         error(_("could not load disklist \"%s\""), conf_diskfile);
312         /*NOTREACHED*/
313     }
314     amfree(conf_diskfile);
315
316     /* set up any configuration-dependent variables */
317
318     inparallel  = getconf_int(CNF_INPARALLEL);
319
320     reserve = (unsigned long)getconf_int(CNF_RESERVE);
321
322     total_disksize = (off_t)0;
323     ha_last = NULL;
324     num_holdalloc = 0;
325     for(hdp = getconf_holdingdisks(), dsk = 0; hdp != NULL; hdp = holdingdisk_next(hdp), dsk++) {
326         ha = alloc(SIZEOF(holdalloc_t));
327         num_holdalloc++;
328
329         /* link the list in the same order as getconf_holdingdisks's results */
330         ha->next = NULL;
331         if (ha_last == NULL)
332             holdalloc = ha;
333         else
334             ha_last->next = ha;
335         ha_last = ha;
336
337         ha->hdisk = hdp;
338         ha->allocated_dumpers = 0;
339         ha->allocated_space = (off_t)0;
340         ha->disksize = holdingdisk_get_disksize(hdp);
341
342         /* get disk size */
343         if(get_fs_usage(holdingdisk_get_diskdir(hdp), NULL, &fsusage) == -1
344            || access(holdingdisk_get_diskdir(hdp), W_OK) == -1) {
345             log_add(L_WARNING, _("WARNING: ignoring holding disk %s: %s\n"),
346                     holdingdisk_get_diskdir(hdp), strerror(errno));
347             ha->disksize = 0L;
348             continue;
349         }
350
351         /* do the division first to avoid potential integer overflow */
352         if (fsusage.fsu_bavail_top_bit_set)
353             kb_avail = 0;
354         else
355             kb_avail = fsusage.fsu_bavail / 1024 * fsusage.fsu_blocksize;
356
357         if(ha->disksize > (off_t)0) {
358             if(ha->disksize > kb_avail) {
359                 log_add(L_WARNING,
360                         _("WARNING: %s: %lld KB requested, "
361                         "but only %lld KB available."),
362                         holdingdisk_get_diskdir(hdp),
363                         (long long)ha->disksize,
364                         (long long)kb_avail);
365                         ha->disksize = kb_avail;
366             }
367         }
368         /* ha->disksize is negative; use all but that amount */
369         else if(kb_avail < -ha->disksize) {
370             log_add(L_WARNING,
371                     _("WARNING: %s: not %lld KB free."),
372                     holdingdisk_get_diskdir(hdp),
373                     (long long)-ha->disksize);
374             ha->disksize = (off_t)0;
375             continue;
376         }
377         else
378             ha->disksize += kb_avail;
379
380         g_printf(_("driver: adding holding disk %d dir %s size %lld chunksize %lld\n"),
381                dsk, holdingdisk_get_diskdir(hdp),
382                (long long)ha->disksize,
383                (long long)(holdingdisk_get_chunksize(hdp)));
384
385         newdir = newvstralloc(newdir,
386                               holdingdisk_get_diskdir(hdp), "/", hd_driver_timestamp,
387                               NULL);
388         if(!mkholdingdir(newdir)) {
389             ha->disksize = (off_t)0;
390         }
391         total_disksize += ha->disksize;
392     }
393
394     reserved_space = total_disksize * (off_t)(reserve / 100);
395
396     g_printf(_("reserving %lld out of %lld for degraded-mode dumps\n"),
397            (long long)reserved_space, (long long)free_space());
398
399     amfree(newdir);
400
401     if(inparallel > MAX_DUMPERS) inparallel = MAX_DUMPERS;
402
403     /* taper takes a while to get going, so start it up right away */
404
405     init_driverio();
406     if(conf_runtapes > 0) {
407         startup_tape_process(taper_program);
408         taper_cmd(START_TAPER, driver_timestamp, NULL, 0, NULL);
409     }
410
411     /* fire up the dumpers now while we are waiting */
412     if(!nodump) startup_dump_processes(dumper_program, inparallel, driver_timestamp);
413
414     /*
415      * Read schedule from stdin.  Usually, this is a pipe from planner,
416      * so the effect is that we wait here for the planner to
417      * finish, but meanwhile the taper is rewinding the tape, reading
418      * the label, checking it, writing a new label and all that jazz
419      * in parallel with the planner.
420      */
421
422     runq.head = NULL;
423     runq.tail = NULL;
424     directq.head = NULL;
425     directq.tail = NULL;
426     waitq = origq;
427     taper_state = TAPER_STATE_DEFAULT;
428     tapeq = read_flush();
429
430     roomq.head = roomq.tail = NULL;
431
432     log_add(L_STATS, _("startup time %s"), walltime_str(curclock()));
433
434     g_printf(_("driver: start time %s inparallel %d bandwidth %lu diskspace %lld "), walltime_str(curclock()), inparallel,
435            free_kps(NULL), (long long)free_space());
436     g_printf(_(" dir %s datestamp %s driver: drain-ends tapeq %s big-dumpers %s\n"),
437            "OBSOLETE", driver_timestamp, taperalgo2str(conf_taperalgo),
438            getconf_str(CNF_DUMPORDER));
439     fflush(stdout);
440
441     /* ok, planner is done, now lets see if the tape is ready */
442
443     if (conf_runtapes > 0) {
444         cmd = getresult(taper, 1, &result_argc, result_argv, MAX_ARGS+1);
445         if (cmd != TAPER_OK) {
446             /* no tape, go into degraded mode: dump to holding disk */
447             need_degraded = 1;
448         }
449     } else {
450         need_degraded = 1;
451     }
452
453     tape_left = tape_length;
454     taper_busy = 0;
455     taper_input_error = NULL;
456     taper_tape_error = NULL;
457     taper_disk = NULL;
458     taper_ev_read = NULL;
459
460     schedule_done = nodump;
461     force_flush = 0;
462
463     if(!need_degraded) startaflush();
464
465     if(!nodump)
466         schedule_ev_read = event_register((event_id_t)0, EV_READFD, read_schedule, NULL);
467
468     short_dump_state();
469     event_loop(0);
470
471     force_flush = 1;
472
473     /* mv runq to directq */
474     while (!empty(runq)) {
475         diskp = dequeue_disk(&runq);
476         headqueue_disk(&directq, diskp);
477     }
478
479     /* handle any remaining dumps by dumping directly to tape, if possible */
480     while(!empty(directq) && taper > 0) {
481         diskp = dequeue_disk(&directq);
482         if (diskp->to_holdingdisk == HOLD_REQUIRED) {
483             log_add(L_FAIL, _("%s %s %s %d [%s]"),
484                 diskp->host->hostname, diskp->name, sched(diskp)->datestamp,
485                 sched(diskp)->level,
486                 _("can't dump required holdingdisk"));
487         }
488         else if (!degraded_mode) {
489             taper_state |= TAPER_STATE_DUMP_TO_TAPE;
490             dump_to_tape(diskp);
491             event_loop(0);
492             taper_state &= !TAPER_STATE_DUMP_TO_TAPE;
493         }
494         else
495             log_add(L_FAIL, _("%s %s %s %d [%s]"),
496                 diskp->host->hostname, diskp->name, sched(diskp)->datestamp,
497                 sched(diskp)->level,
498                 diskp->to_holdingdisk == HOLD_AUTO ?
499                     _("no more holding disk space") :
500                     _("can't dump no-hold disk in degraded mode"));
501     }
502
503     /* fill up the tape or start new one for taperflush */
504     startaflush();
505     event_loop(0);
506
507     short_dump_state();                         /* for amstatus */
508
509     g_printf(_("driver: QUITTING time %s telling children to quit\n"),
510            walltime_str(curclock()));
511     fflush(stdout);
512
513     if(!nodump) {
514         for(dumper = dmptable; dumper < dmptable + inparallel; dumper++) {
515             if(dumper->fd >= 0)
516                 dumper_cmd(dumper, QUIT, NULL);
517         }
518     }
519
520     if(taper >= 0) {
521         taper_cmd(QUIT, NULL, NULL, 0, NULL);
522     }
523
524     /* wait for all to die */
525     wait_children(600);
526
527     /* cleanup */
528     holding_cleanup(NULL, NULL);
529
530     amfree(newdir);
531
532     check_unfree_serial();
533     g_printf(_("driver: FINISHED time %s\n"), walltime_str(curclock()));
534     fflush(stdout);
535     log_add(L_FINISH,_("date %s time %s"), driver_timestamp, walltime_str(curclock()));
536     amfree(driver_timestamp);
537
538     amfree(dumper_program);
539     amfree(taper_program);
540
541     dbclose();
542
543     return 0;
544 }
545
546 /* sleep up to count seconds, and wait for terminating child process */
547 /* if sleep is negative, this function will not timeout              */
548 /* exit once all child process are finished or the timout expired    */
549 /* return 0 if no more children to wait                              */
550 /* return 1 if some children are still alive                         */
551 static int
552 wait_children(int count)
553 {
554     pid_t     pid;
555     amwait_t  retstat;
556     char     *who;
557     char     *what;
558     int       code=0;
559     dumper_t *dumper;
560     int       wait_errno;
561
562     do {
563         do {
564             pid = waitpid((pid_t)-1, &retstat, WNOHANG);
565             wait_errno = errno;
566             if (pid > 0) {
567                 what = NULL;
568                 if (! WIFEXITED(retstat)) {
569                     what = _("signal");
570                     code = WTERMSIG(retstat);
571                 } else if (WEXITSTATUS(retstat) != 0) {
572                     what = _("code");
573                     code = WEXITSTATUS(retstat);
574                 }
575                 who = NULL;
576                 for (dumper = dmptable; dumper < dmptable + inparallel;
577                      dumper++) {
578                     if (pid == dumper->pid) {
579                         who = stralloc(dumper->name);
580                         dumper->pid = -1;
581                         break;
582                     }
583                     if (dumper->chunker && pid == dumper->chunker->pid) {
584                         who = stralloc(dumper->chunker->name);
585                         dumper->chunker->pid = -1;
586                         break;
587                     }
588                 }
589                 if (who == NULL && pid == taper_pid) {
590                     who = stralloc("taper");
591                     taper_pid = -1;
592                 }
593                 if(what != NULL && who == NULL) {
594                     who = stralloc("unknown");
595                 }
596                 if(who && what) {
597                     log_add(L_WARNING, _("%s pid %u exited with %s %d\n"), who, 
598                             (unsigned)pid, what, code);
599                     g_printf(_("driver: %s pid %u exited with %s %d\n"), who,
600                            (unsigned)pid, what, code);
601                 }
602                 amfree(who);
603             }
604         } while (pid > 0 || wait_errno == EINTR);
605         if (errno != ECHILD)
606             sleep(1);
607         if (count > 0)
608             count--;
609     } while ((errno != ECHILD) && (count != 0));
610     return (errno != ECHILD);
611 }
612
613 static void
614 kill_children(int signal)
615 {
616     dumper_t *dumper;
617
618     if(!nodump) {
619         for(dumper = dmptable; dumper < dmptable + inparallel; dumper++) {
620             if (!dumper->down && dumper->pid > 1) {
621                 g_printf(_("driver: sending signal %d to %s pid %u\n"), signal,
622                        dumper->name, (unsigned)dumper->pid);
623                 if (kill(dumper->pid, signal) == -1 && errno == ESRCH) {
624                     if (dumper->chunker)
625                         dumper->chunker->pid = 0;
626                 }
627                 if (dumper->chunker && dumper->chunker->pid > 1) {
628                     g_printf(_("driver: sending signal %d to %s pid %u\n"), signal,
629                            dumper->chunker->name,
630                            (unsigned)dumper->chunker->pid);
631                     if (kill(dumper->chunker->pid, signal) == -1 &&
632                         errno == ESRCH)
633                         dumper->chunker->pid = 0;
634                 }
635             }
636         }
637     }
638
639     if(taper_pid > 1)
640         g_printf(_("driver: sending signal %d to %s pid %u\n"), signal,
641                "taper", (unsigned)taper_pid);
642         if (kill(taper_pid, signal) == -1 && errno == ESRCH)
643             taper_pid = 0;
644 }
645
646 static void
647 wait_for_children(void)
648 {
649     dumper_t *dumper;
650
651     if(!nodump) {
652         for(dumper = dmptable; dumper < dmptable + inparallel; dumper++) {
653             if (dumper->pid > 1 && dumper->fd >= 0) {
654                 dumper_cmd(dumper, QUIT, NULL);
655                 if (dumper->chunker && dumper->chunker->pid > 1 &&
656                     dumper->chunker->fd >= 0)
657                     chunker_cmd(dumper->chunker, QUIT, NULL);
658             }
659         }
660     }
661
662     if(taper_pid > 1 && taper > 0) {
663         taper_cmd(QUIT, NULL, NULL, 0, NULL);
664     }
665
666     if(wait_children(60) == 0)
667         return;
668
669     kill_children(SIGHUP);
670     if(wait_children(60) == 0)
671         return;
672
673     kill_children(SIGKILL);
674     if(wait_children(-1) == 0)
675         return;
676
677 }
678
679 static void
680 startaflush(void)
681 {
682     disk_t *dp = NULL;
683     disk_t *fit = NULL;
684     char *datestamp;
685     int extra_tapes = 0;
686     char *qname;
687     TapeAction result_tape_action;
688
689     result_tape_action = tape_action();
690
691     if (result_tape_action & TAPE_ACTION_NEW_TAPE) {
692         taper_state &= !TAPER_STATE_WAIT_FOR_TAPE;
693         taper_cmd(NEW_TAPE, NULL, NULL, 0, NULL);
694     } else if (result_tape_action & TAPE_ACTION_NO_NEW_TAPE) {
695         taper_state &= !TAPER_STATE_WAIT_FOR_TAPE;
696         taper_cmd(NO_NEW_TAPE, NULL, NULL, 0, NULL);
697     }
698
699     if (!degraded_mode && !taper_busy && !empty(tapeq) &&
700         (result_tape_action & TAPE_ACTION_START_A_FLUSH)) {
701         
702         datestamp = sched(tapeq.head)->datestamp;
703         switch(conf_taperalgo) {
704         case ALGO_FIRST:
705                 dp = dequeue_disk(&tapeq);
706                 break;
707         case ALGO_FIRSTFIT:
708                 fit = tapeq.head;
709                 while (fit != NULL) {
710                     extra_tapes = (fit->tape_splitsize > (off_t)0) ? 
711                                         conf_runtapes - current_tape : 0;
712                     if(sched(fit)->act_size <= (tape_left +
713                              tape_length * (off_t)extra_tapes) &&
714                              strcmp(sched(fit)->datestamp, datestamp) <= 0) {
715                         dp = fit;
716                         fit = NULL;
717                     }
718                     else {
719                         fit = fit->next;
720                     }
721                 }
722                 if(dp) remove_disk(&tapeq, dp);
723                 break;
724         case ALGO_LARGEST:
725                 fit = dp = tapeq.head;
726                 while (fit != NULL) {
727                     if(sched(fit)->act_size > sched(dp)->act_size &&
728                        strcmp(sched(fit)->datestamp, datestamp) <= 0) {
729                         dp = fit;
730                     }
731                     fit = fit->next;
732                 }
733                 if(dp) remove_disk(&tapeq, dp);
734                 break;
735         case ALGO_LARGESTFIT:
736                 fit = tapeq.head;
737                 while (fit != NULL) {
738                     extra_tapes = (fit->tape_splitsize > (off_t)0) ? 
739                                         conf_runtapes - current_tape : 0;
740                     if(sched(fit)->act_size <=
741                        (tape_left + tape_length * (off_t)extra_tapes) &&
742                        (!dp || sched(fit)->act_size > sched(dp)->act_size) &&
743                        strcmp(sched(fit)->datestamp, datestamp) <= 0) {
744                         dp = fit;
745                     }
746                     fit = fit->next;
747                 }
748                 if(dp) remove_disk(&tapeq, dp);
749                 break;
750         case ALGO_SMALLEST:
751                 break;
752         case ALGO_LAST:
753                 dp = tapeq.tail;
754                 remove_disk(&tapeq, dp);
755                 break;
756         }
757         if(!dp) { /* ALGO_SMALLEST, or default if nothing fit. */
758             if(conf_taperalgo != ALGO_SMALLEST)  {
759                 g_fprintf(stderr,
760                    _("driver: startaflush: Using SMALLEST because nothing fit\n"));
761             }
762             fit = dp = tapeq.head;
763             while (fit != NULL) {
764                 if(sched(fit)->act_size < sched(dp)->act_size &&
765                    strcmp(sched(fit)->datestamp, datestamp) <= 0) {
766                     dp = fit;
767                 }
768                 fit = fit->next;
769             }
770             if(dp) remove_disk(&tapeq, dp);
771         }
772         if(taper_ev_read == NULL) {
773             taper_ev_read = event_register((event_id_t)taper, EV_READFD,
774                                            handle_taper_result, NULL);
775         }
776         if (dp) {
777             taper_disk = dp;
778             taper_busy = 1;
779             taper_input_error = NULL;
780             taper_tape_error = NULL;
781             taper_result = LAST_TOK;
782             taper_sendresult = 0;
783             taper_first_label = NULL;
784             taper_written = 0;
785             taper_state &= !TAPER_STATE_DUMP_TO_TAPE;
786             taper_dumper = NULL;
787             qname = quote_string(dp->name);
788             taper_cmd(FILE_WRITE, dp, sched(dp)->destname, sched(dp)->level,
789                       sched(dp)->datestamp);
790             g_fprintf(stderr,_("driver: startaflush: %s %s %s %lld %lld\n"),
791                     taperalgo2str(conf_taperalgo), dp->host->hostname, qname,
792                     (long long)sched(taper_disk)->act_size,
793                     (long long)tape_left);
794             if(sched(dp)->act_size <= tape_left)
795                 tape_left -= sched(dp)->act_size;
796             else
797                 tape_left = (off_t)0;
798             amfree(qname);
799         } else {
800             error(_("FATAL: Taper marked busy and no work found."));
801             /*NOTREACHED*/
802         }
803     } else if(!taper_busy && taper_ev_read != NULL) {
804         event_release(taper_ev_read);
805         taper_ev_read = NULL;
806     }
807 }
808
809 static int
810 client_constrained(
811     disk_t *    dp)
812 {
813     disk_t *dp2;
814
815     /* first, check if host is too busy */
816
817     if(dp->host->inprogress >= dp->host->maxdumps) {
818         return 1;
819     }
820
821     /* next, check conflict with other dumps on same spindle */
822
823     if(dp->spindle == -1) {     /* but spindle -1 never conflicts by def. */
824         return 0;
825     }
826
827     for(dp2 = dp->host->disks; dp2 != NULL; dp2 = dp2->hostnext)
828         if(dp2->inprogress && dp2->spindle == dp->spindle) {
829             return 1;
830         }
831
832     return 0;
833 }
834
835 static void
836 start_some_dumps(
837     disklist_t *        rq)
838 {
839     int cur_idle;
840     disk_t *diskp, *delayed_diskp, *diskp_accept;
841     assignedhd_t **holdp=NULL, **holdp_accept;
842     const time_t now = time(NULL);
843     cmd_t cmd;
844     int result_argc;
845     char *result_argv[MAX_ARGS+1];
846     chunker_t *chunker;
847     dumper_t *dumper;
848     char dumptype;
849     char *dumporder;
850     int  busy_dumpers = 0;
851
852     idle_reason = IDLE_NO_DUMPERS;
853     sleep_time = 0;
854
855     if(dumpers_ev_time != NULL) {
856         event_release(dumpers_ev_time);
857         dumpers_ev_time = NULL;
858     }
859
860     for(dumper = dmptable; dumper < (dmptable+inparallel); dumper++) {
861         if( dumper->busy ) {
862             busy_dumpers++;
863         }
864     }
865
866     for (dumper = dmptable; dumper < dmptable+inparallel; dumper++) {
867
868         if( dumper->busy || dumper->down) {
869             continue;
870         }
871
872         if (dumper->ev_read != NULL) {
873             event_release(dumper->ev_read);
874             dumper->ev_read = NULL;
875         }
876
877         /*
878          * A potential problem with starting from the bottom of the dump time
879          * distribution is that a slave host will have both one of the shortest
880          * and one of the longest disks, so starting its shortest disk first will
881          * tie up the host and eliminate its longest disk from consideration the
882          * first pass through.  This could cause a big delay in starting that long
883          * disk, which could drag out the whole night's dumps.
884          *
885          * While starting from the top of the dump time distribution solves the
886          * above problem, this turns out to be a bad idea, because the big dumps
887          * will almost certainly pack the holding disk completely, leaving no
888          * room for even one small dump to start.  This ends up shutting out the
889          * small-end dumpers completely (they stay idle).
890          *
891          * The introduction of multiple simultaneous dumps to one host alleviates
892          * the biggest&smallest dumps problem: both can be started at the
893          * beginning.
894          */
895
896         diskp_accept = NULL;
897         holdp_accept = NULL;
898         delayed_diskp = NULL;
899
900         cur_idle = NOT_IDLE;
901
902         dumporder = getconf_str(CNF_DUMPORDER);
903         if(strlen(dumporder) > (size_t)(dumper-dmptable)) {
904             dumptype = dumporder[dumper-dmptable];
905         }
906         else {
907             if(dumper-dmptable < 3)
908                 dumptype = 't';
909             else
910                 dumptype = 'T';
911         }
912
913         for(diskp = rq->head; diskp != NULL; diskp = diskp->next) {
914             assert(diskp->host != NULL && sched(diskp) != NULL);
915
916             if (diskp->host->start_t > now) {
917                 cur_idle = max(cur_idle, IDLE_START_WAIT);
918                 if (delayed_diskp == NULL || sleep_time > diskp->host->start_t) {
919                     delayed_diskp = diskp;
920                     sleep_time = diskp->host->start_t;
921                 }
922             } else if(diskp->start_t > now) {
923                 cur_idle = max(cur_idle, IDLE_START_WAIT);
924                 if (delayed_diskp == NULL || sleep_time > diskp->start_t) {
925                     delayed_diskp = diskp;
926                     sleep_time = diskp->start_t;
927                 }
928             } else if (diskp->host->netif->curusage > 0 &&
929                        sched(diskp)->est_kps > free_kps(diskp->host->netif)) {
930                 cur_idle = max(cur_idle, IDLE_NO_BANDWIDTH);
931             } else if(sched(diskp)->no_space) {
932                 cur_idle = max(cur_idle, IDLE_NO_DISKSPACE);
933             } else if (diskp->to_holdingdisk == HOLD_NEVER) {
934                 cur_idle = max(cur_idle, IDLE_NO_HOLD);
935             } else if ((holdp =
936                 find_diskspace(sched(diskp)->est_size, &cur_idle, NULL)) == NULL) {
937                 cur_idle = max(cur_idle, IDLE_NO_DISKSPACE);
938                 if (empty(tapeq) && busy_dumpers == 0) {
939                     remove_disk(rq, diskp);
940                     enqueue_disk(&directq, diskp);
941                 }
942             } else if (client_constrained(diskp)) {
943                 free_assignedhd(holdp);
944                 cur_idle = max(cur_idle, IDLE_CLIENT_CONSTRAINED);
945             } else {
946
947                 /* disk fits, dump it */
948                 int accept = !diskp_accept;
949                 if(!accept) {
950                     switch(dumptype) {
951                       case 's': accept = (sched(diskp)->est_size < sched(diskp_accept)->est_size);
952                                 break;
953                       case 'S': accept = (sched(diskp)->est_size > sched(diskp_accept)->est_size);
954                                 break;
955                       case 't': accept = (sched(diskp)->est_time < sched(diskp_accept)->est_time);
956                                 break;
957                       case 'T': accept = (sched(diskp)->est_time > sched(diskp_accept)->est_time);
958                                 break;
959                       case 'b': accept = (sched(diskp)->est_kps < sched(diskp_accept)->est_kps);
960                                 break;
961                       case 'B': accept = (sched(diskp)->est_kps > sched(diskp_accept)->est_kps);
962                                 break;
963                       default:  log_add(L_WARNING, _("Unknown dumporder character \'%c\', using 's'.\n"),
964                                         dumptype);
965                                 accept = (sched(diskp)->est_size < sched(diskp_accept)->est_size);
966                                 break;
967                     }
968                 }
969                 if(accept) {
970                     if( !diskp_accept || !degraded_mode || diskp->priority >= diskp_accept->priority) {
971                         if(holdp_accept) free_assignedhd(holdp_accept);
972                         diskp_accept = diskp;
973                         holdp_accept = holdp;
974                     }
975                     else {
976                         free_assignedhd(holdp);
977                     }
978                 }
979                 else {
980                     free_assignedhd(holdp);
981                 }
982             }
983         }
984
985         diskp = diskp_accept;
986         holdp = holdp_accept;
987
988         idle_reason = max(idle_reason, cur_idle);
989
990         /*
991          * If we have no disk at this point, and there are disks that
992          * are delayed, then schedule a time event to call this dumper
993          * with the disk with the shortest delay.
994          */
995         if (diskp == NULL && delayed_diskp != NULL) {
996             assert(sleep_time > now);
997             sleep_time -= now;
998             dumpers_ev_time = event_register((event_id_t)sleep_time, EV_TIME,
999                 handle_dumpers_time, &runq);
1000             return;
1001         } else if (diskp != NULL) {
1002             sched(diskp)->act_size = (off_t)0;
1003             allocate_bandwidth(diskp->host->netif, sched(diskp)->est_kps);
1004             sched(diskp)->activehd = assign_holdingdisk(holdp, diskp);
1005             amfree(holdp);
1006             sched(diskp)->destname = newstralloc(sched(diskp)->destname,
1007                                                  sched(diskp)->holdp[0]->destname);
1008             diskp->host->inprogress++;  /* host is now busy */
1009             diskp->inprogress = 1;
1010             sched(diskp)->dumper = dumper;
1011             sched(diskp)->timestamp = now;
1012
1013             dumper->busy = 1;           /* dumper is now busy */
1014             dumper->dp = diskp;         /* link disk to dumper */
1015             remove_disk(rq, diskp);             /* take it off the run queue */
1016
1017             sched(diskp)->origsize = (off_t)-1;
1018             sched(diskp)->dumpsize = (off_t)-1;
1019             sched(diskp)->dumptime = (time_t)0;
1020             sched(diskp)->tapetime = (time_t)0;
1021             chunker = dumper->chunker;
1022             chunker->result = LAST_TOK;
1023             dumper->result = LAST_TOK;
1024             startup_chunk_process(chunker,chunker_program);
1025             chunker_cmd(chunker, START, (void *)driver_timestamp);
1026             chunker->dumper = dumper;
1027             chunker_cmd(chunker, PORT_WRITE, diskp);
1028             cmd = getresult(chunker->fd, 1, &result_argc, result_argv, MAX_ARGS+1);
1029             if(cmd != PORT) {
1030                 assignedhd_t **h=NULL;
1031                 int activehd;
1032
1033                 g_printf(_("driver: did not get PORT from %s for %s:%s\n"),
1034                        chunker->name, diskp->host->hostname, diskp->name);
1035                 fflush(stdout);
1036
1037                 deallocate_bandwidth(diskp->host->netif, sched(diskp)->est_kps);
1038                 h = sched(diskp)->holdp;
1039                 activehd = sched(diskp)->activehd;
1040                 h[activehd]->used = 0;
1041                 h[activehd]->disk->allocated_dumpers--;
1042                 adjust_diskspace(diskp, DONE);
1043                 delete_diskspace(diskp);
1044                 diskp->host->inprogress--;
1045                 diskp->inprogress = 0;
1046                 sched(diskp)->dumper = NULL;
1047                 dumper->busy = 0;
1048                 dumper->dp = NULL;
1049                 sched(diskp)->dump_attempted++;
1050                 free_serial_dp(diskp);
1051                 if(sched(diskp)->dump_attempted < 2)
1052                     enqueue_disk(rq, diskp);
1053             }
1054             else {
1055                 dumper->ev_read = event_register((event_id_t)dumper->fd, EV_READFD,
1056                                                  handle_dumper_result, dumper);
1057                 chunker->ev_read = event_register((event_id_t)chunker->fd, EV_READFD,
1058                                                    handle_chunker_result, chunker);
1059                 dumper->output_port = atoi(result_argv[2]);
1060
1061                 dumper_cmd(dumper, PORT_DUMP, diskp);
1062             }
1063             diskp->host->start_t = now + 15;
1064         }
1065     }
1066 }
1067
1068 /*
1069  * This gets called when a dumper is delayed for some reason.  It may
1070  * be because a disk has a delayed start, or amanda is constrained
1071  * by network or disk limits.
1072  */
1073
1074 static void
1075 handle_dumpers_time(
1076     void *      cookie)
1077 {
1078     disklist_t *runq = cookie;
1079     event_release(dumpers_ev_time);
1080     dumpers_ev_time = NULL; 
1081     start_some_dumps(runq);
1082 }
1083
1084 static void
1085 dump_schedule(
1086     disklist_t *qp,
1087     char *      str)
1088 {
1089     disk_t *dp;
1090     char *qname;
1091
1092     g_printf(_("dump of driver schedule %s:\n--------\n"), str);
1093
1094     for(dp = qp->head; dp != NULL; dp = dp->next) {
1095         qname = quote_string(dp->name);
1096         g_printf("  %-20s %-25s lv %d t %5lu s %lld p %d\n",
1097                dp->host->hostname, qname, sched(dp)->level,
1098                sched(dp)->est_time,
1099                (long long)sched(dp)->est_size, sched(dp)->priority);
1100         amfree(qname);
1101     }
1102     g_printf("--------\n");
1103 }
1104
1105 static void
1106 start_degraded_mode(
1107     /*@keep@*/ disklist_t *queuep)
1108 {
1109     disk_t *dp;
1110     disklist_t newq;
1111     off_t est_full_size;
1112     char *qname;
1113
1114     newq.head = newq.tail = 0;
1115
1116     dump_schedule(queuep, _("before start degraded mode"));
1117
1118     est_full_size = (off_t)0;
1119     while(!empty(*queuep)) {
1120         dp = dequeue_disk(queuep);
1121
1122         qname = quote_string(dp->name);
1123         if(sched(dp)->level != 0)
1124             /* go ahead and do the disk as-is */
1125             enqueue_disk(&newq, dp);
1126         else {
1127             if (reserved_space + est_full_size + sched(dp)->est_size
1128                 <= total_disksize) {
1129                 enqueue_disk(&newq, dp);
1130                 est_full_size += sched(dp)->est_size;
1131             }
1132             else if(sched(dp)->degr_level != -1) {
1133                 sched(dp)->level = sched(dp)->degr_level;
1134                 sched(dp)->dumpdate = sched(dp)->degr_dumpdate;
1135                 sched(dp)->est_nsize = sched(dp)->degr_nsize;
1136                 sched(dp)->est_csize = sched(dp)->degr_csize;
1137                 sched(dp)->est_time = sched(dp)->degr_time;
1138                 sched(dp)->est_kps  = sched(dp)->degr_kps;
1139                 enqueue_disk(&newq, dp);
1140             }
1141             else {
1142                 log_add(L_FAIL,_("%s %s %s %d [can't switch to incremental dump]"),
1143                         dp->host->hostname, qname, sched(dp)->datestamp,
1144                         sched(dp)->level);
1145             }
1146         }
1147         amfree(qname);
1148     }
1149
1150     /*@i@*/ *queuep = newq;
1151     degraded_mode = 1;
1152
1153     dump_schedule(queuep, _("after start degraded mode"));
1154 }
1155
1156
1157 static void
1158 continue_port_dumps(void)
1159 {
1160     disk_t *dp, *ndp;
1161     assignedhd_t **h;
1162     int active_dumpers=0, busy_dumpers=0, i;
1163     dumper_t *dumper;
1164
1165     /* First we try to grant diskspace to some dumps waiting for it. */
1166     for( dp = roomq.head; dp; dp = ndp ) {
1167         ndp = dp->next;
1168         /* find last holdingdisk used by this dump */
1169         for( i = 0, h = sched(dp)->holdp; h[i+1]; i++ ) {
1170             (void)h; /* Quiet lint */
1171         }
1172         /* find more space */
1173         h = find_diskspace( sched(dp)->est_size - sched(dp)->act_size,
1174                             &active_dumpers, h[i] );
1175         if( h ) {
1176             for(dumper = dmptable; dumper < dmptable + inparallel &&
1177                                    dumper->dp != dp; dumper++) {
1178                 (void)dp; /* Quiet lint */
1179             }
1180             assert( dumper < dmptable + inparallel );
1181             sched(dp)->activehd = assign_holdingdisk( h, dp );
1182             chunker_cmd( dumper->chunker, CONTINUE, dp );
1183             amfree(h);
1184             remove_disk( &roomq, dp );
1185         }
1186     }
1187
1188     /* So for some disks there is less holding diskspace available than
1189      * was asked for. Possible reasons are
1190      * a) diskspace has been allocated for other dumps which are
1191      *    still running or already being written to tape
1192      * b) all other dumps have been suspended due to lack of diskspace
1193      * c) this dump doesn't fit on all the holding disks
1194      * Case a) is not a problem. We just wait for the diskspace to
1195      * be freed by moving the current disk to a queue.
1196      * If case b) occurs, we have a deadlock situation. We select
1197      * a dump from the queue to be aborted and abort it. It will
1198      * be retried later dumping to disk.
1199      * If case c) is detected, the dump is aborted. Next time
1200      * it will be dumped directly to tape. Actually, case c is a special
1201      * manifestation of case b) where only one dumper is busy.
1202      */
1203     for(dp=NULL, dumper = dmptable; dumper < (dmptable+inparallel); dumper++) {
1204         if( dumper->busy ) {
1205             busy_dumpers++;
1206             if( !find_disk(&roomq, dumper->dp) ) {
1207                 active_dumpers++;
1208             } else if( !dp || 
1209                        sched(dp)->est_size > sched(dumper->dp)->est_size ) {
1210                 dp = dumper->dp;
1211             }
1212         }
1213     }
1214     if((dp != NULL) && (active_dumpers == 0) && (busy_dumpers > 0) && 
1215         ((!taper_busy && empty(tapeq)) || degraded_mode) &&
1216         pending_aborts == 0 ) { /* not case a */
1217         if( busy_dumpers == 1 ) { /* case c */
1218             sched(dp)->no_space = 1;
1219         }
1220         /* case b */
1221         /* At this time, dp points to the dump with the smallest est_size.
1222          * We abort that dump, hopefully not wasting too much time retrying it.
1223          */
1224         remove_disk( &roomq, dp );
1225         chunker_cmd( sched(dp)->dumper->chunker, ABORT, NULL);
1226         dumper_cmd( sched(dp)->dumper, ABORT, NULL );
1227         pending_aborts++;
1228     }
1229 }
1230
1231
1232 static void
1233 handle_taper_result(
1234     void *cookie)
1235 {
1236     disk_t *dp;
1237     cmd_t cmd;
1238     int result_argc;
1239     char *result_argv[MAX_ARGS+1];
1240     char *qname;
1241
1242     (void)cookie;       /* Quiet unused parameter warning */
1243
1244     assert(cookie == NULL);
1245     
1246     do {
1247         
1248         short_dump_state();
1249         
1250         cmd = getresult(taper, 1, &result_argc, result_argv, MAX_ARGS+1);
1251         
1252         switch(cmd) {
1253             
1254         case FAILED:    /* FAILED <handle> INPUT-* TAPE-* <input err mesg> <tape err mesg> */
1255             if(result_argc != 6) {
1256                 error(_("error: [taper FAILED result_argc != 6: %d"), result_argc);
1257                 /*NOTREACHED*/
1258             }
1259             
1260             dp = serial2disk(result_argv[2]);
1261             assert(dp == taper_disk);
1262             if (!taper_dumper)
1263                 free_serial(result_argv[2]);
1264             
1265             qname = quote_string(dp->name);
1266             g_printf(_("driver: finished-cmd time %s taper wrote %s:%s\n"),
1267                    walltime_str(curclock()), dp->host->hostname, qname);
1268             fflush(stdout);
1269             amfree(qname);
1270
1271             if (strcmp(result_argv[3], "INPUT-ERROR") == 0) {
1272                 taper_input_error = stralloc(result_argv[5]);
1273             }
1274             if (strcmp(result_argv[4], "TAPE-ERROR") == 0) {
1275                 taper_tape_error = stralloc(result_argv[6]);
1276             }
1277
1278             taper_result = cmd;
1279
1280             break;
1281             
1282         case PARTIAL:   /* PARTIAL <handle> INPUT-* TAPE-* <stat mess> <input err mesg> <tape err mesg>*/
1283         case DONE:      /* DONE <handle> INPUT-GOOD TAPE-GOOD <stat mess> <input err mesg> <tape err mesg> */
1284             if(result_argc != 7) {
1285                 error(_("error: [taper PARTIAL result_argc != 7: %d"), result_argc);
1286                 /*NOTREACHED*/
1287             }
1288             
1289             dp = serial2disk(result_argv[2]);
1290             assert(dp == taper_disk);
1291             if (!taper_dumper)
1292                 free_serial(result_argv[2]);
1293
1294             g_printf(_("driver: finished-cmd time %s taper wrote %s:%s\n"),
1295                    walltime_str(curclock()), dp->host->hostname, dp->name);
1296             fflush(stdout);
1297
1298             if (strcmp(result_argv[3], "INPUT-ERROR") == 0) {
1299                 taper_input_error = stralloc(result_argv[5]);
1300             }
1301             if (strcmp(result_argv[4], "TAPE-ERROR") == 0) {
1302                 taper_tape_error = stralloc(result_argv[6]);
1303             }
1304
1305             taper_result = cmd;
1306
1307             break;
1308             
1309         case PARTDONE:  /* PARTDONE <handle> <label> <fileno> <stat> */
1310             dp = serial2disk(result_argv[2]);
1311             assert(dp == taper_disk);
1312             if (result_argc != 6) {
1313                 error(_("error [taper PARTDONE result_argc != 5: %d]"),
1314                       result_argc);
1315                 /*NOTREACHED*/
1316             }
1317             if (!taper_first_label) {
1318                 taper_first_label = stralloc(result_argv[3]);
1319                 taper_first_fileno = OFF_T_ATOI(result_argv[4]);
1320             }
1321             taper_written = OFF_T_ATOI(result_argv[5]);
1322             if (taper_written > sched(taper_disk)->act_size)
1323                 sched(taper_disk)->act_size = taper_written;
1324             
1325             break;
1326
1327         case REQUEST_NEW_TAPE:  /* REQUEST-NEW-TAPE */
1328             if (result_argc != 2) {
1329                 error(_("error [taper REQUEST_NEW_TAPE result_argc != 2: %d]"),
1330                       result_argc);
1331                 /*NOTREACHED*/
1332             }
1333             taper_state &= !TAPER_STATE_TAPE_STARTED;
1334
1335             if (current_tape >= conf_runtapes) {
1336                 taper_cmd(NO_NEW_TAPE, NULL, NULL, 0, NULL);
1337                 log_add(L_WARNING,
1338                         _("Out of tapes; going into degraded mode."));
1339                 start_degraded_mode(&runq);
1340             } else {
1341                 TapeAction result_tape_action;
1342
1343                 taper_state |= TAPER_STATE_WAIT_FOR_TAPE;
1344                 result_tape_action = tape_action();
1345                 if (result_tape_action & TAPE_ACTION_NEW_TAPE) {
1346                     taper_cmd(NEW_TAPE, NULL, NULL, 0, NULL);
1347                     taper_state &= !TAPER_STATE_WAIT_FOR_TAPE;
1348                 } else if (result_tape_action & TAPE_ACTION_NO_NEW_TAPE) {
1349                     taper_cmd(NO_NEW_TAPE, NULL, NULL, 0, NULL);
1350                     taper_state &= !TAPER_STATE_WAIT_FOR_TAPE;
1351                 }
1352             }
1353             break;
1354
1355         case NEW_TAPE: /* NEW-TAPE <handle> <label> */
1356             if (result_argc != 3) {
1357                 error(_("error [taper NEW_TAPE result_argc != 3: %d]"),
1358                       result_argc);
1359                 /*NOTREACHED*/
1360             }
1361
1362             /* Update our tape counter and reset tape_left */
1363             current_tape++;
1364             tape_left = tape_length;
1365             taper_state |= TAPER_STATE_TAPE_STARTED;
1366             break;
1367
1368         case NO_NEW_TAPE:  /* NO-NEW-TAPE <handle> */
1369             if (result_argc != 2) {
1370                 error(_("error [taper NO_NEW_TAPE result_argc != 2: %d]"),
1371                       result_argc);
1372                 /*NOTREACHED*/
1373             }
1374             break;
1375
1376         case DUMPER_STATUS:  /* DUMPER-STATUS <handle> */
1377             if (result_argc != 2) {
1378                 error(_("error [taper NO_NEW_TAPE result_argc != 2: %d]"),
1379                       result_argc);
1380                 /*NOTREACHED*/
1381             }
1382             if (taper_dumper->result == LAST_TOK) {
1383                 taper_sendresult = 1;
1384             } else {
1385                 taper_cmd(taper_dumper->result, NULL, NULL, 0, NULL);
1386             }
1387             break;
1388
1389         case TAPE_ERROR: /* TAPE-ERROR <handle> <err mess> */
1390             dp = serial2disk(result_argv[2]);
1391             if (!taper_dumper)
1392                 free_serial(result_argv[2]);
1393             g_printf(_("driver: finished-cmd time %s taper wrote %s:%s\n"),
1394                    walltime_str(curclock()), dp->host->hostname, dp->name);
1395             fflush(stdout);
1396             log_add(L_WARNING, _("Taper  error: %s"), result_argv[3]);
1397             taper_tape_error = stralloc(result_argv[3]);
1398             /*FALLTHROUGH*/
1399
1400         case BOGUS:
1401             if (cmd == BOGUS) {
1402                 log_add(L_WARNING, _("Taper protocol error"));
1403                 taper_tape_error = stralloc("BOGUS");
1404             }
1405             /*
1406              * Since we received a taper error, we can't send anything more
1407              * to the taper.  Go into degraded mode to try to get everthing
1408              * onto disk.  Later, these dumps can be flushed to a new tape.
1409              * The tape queue is zapped so that it appears empty in future
1410              * checks. If there are dumps waiting for diskspace to be freed,
1411              * cancel one.
1412              */
1413             if(!nodump) {
1414                 log_add(L_WARNING,
1415                         _("going into degraded mode because of taper component error."));
1416                 start_degraded_mode(&runq);
1417             }
1418             tapeq.head = tapeq.tail = NULL;
1419             taper_busy = 0;
1420             if(taper_ev_read != NULL) {
1421                 event_release(taper_ev_read);
1422                 taper_ev_read = NULL;
1423             }
1424             if(cmd != TAPE_ERROR) aclose(taper);
1425             taper_result = cmd;
1426
1427             break;
1428
1429         default:
1430             error(_("driver received unexpected token (%s) from taper"),
1431                   cmdstr[cmd]);
1432             /*NOTREACHED*/
1433         }
1434
1435         if (taper_result != LAST_TOK) {
1436             if(taper_dumper) {
1437                 if (taper_dumper->result != LAST_TOK) {
1438                     // Dumper already returned it's result
1439                     dumper_taper_result(taper_disk);
1440                 }
1441             } else {
1442                 file_taper_result(taper_disk);
1443             }
1444         }
1445         
1446     } while(areads_dataready(taper));
1447 }
1448
1449
1450 static void
1451 file_taper_result(
1452     disk_t *dp)
1453 {
1454     if (taper_result == DONE) {
1455         update_info_taper(dp, taper_first_label, taper_first_fileno,
1456                           sched(dp)->level);
1457     }
1458
1459     sched(dp)->taper_attempted += 1;
1460
1461     if (taper_input_error) {
1462         g_printf("driver: taper failed %s %s: %s\n",
1463                    dp->host->hostname, dp->name, taper_input_error);
1464         if (strcmp(sched(dp)->datestamp, driver_timestamp) == 0) {
1465             if(sched(dp)->taper_attempted >= 2) {
1466                 log_add(L_FAIL, _("%s %s %s %d [too many taper retries after holding disk error: %s]"),
1467                     dp->host->hostname, dp->name, sched(dp)->datestamp,
1468                     sched(dp)->level, taper_input_error);
1469                 g_printf("driver: taper failed %s %s, too many taper retry after holding disk error\n",
1470                    dp->host->hostname, dp->name);
1471                 amfree(sched(dp)->destname);
1472                 amfree(sched(dp)->dumpdate);
1473                 amfree(sched(dp)->degr_dumpdate);
1474                 amfree(sched(dp)->datestamp);
1475                 amfree(dp->up);
1476             } else {
1477                 log_add(L_INFO, _("%s %s %s %d [Will retry dump because of holding disk error: %s]"),
1478                         dp->host->hostname, dp->name, sched(dp)->datestamp,
1479                         sched(dp)->level, taper_input_error);
1480                 g_printf("driver: taper will retry %s %s because of holding disk error\n",
1481                         dp->host->hostname, dp->name);
1482                 if (dp->to_holdingdisk != HOLD_REQUIRED) {
1483                     dp->to_holdingdisk = HOLD_NEVER;
1484                     sched(dp)->dump_attempted -= 1;
1485                     headqueue_disk(&directq, dp);
1486                 } else {
1487                     amfree(sched(dp)->destname);
1488                     amfree(sched(dp)->dumpdate);
1489                     amfree(sched(dp)->degr_dumpdate);
1490                     amfree(sched(dp)->datestamp);
1491                     amfree(dp->up);
1492                 }
1493             }
1494         } else {
1495             amfree(sched(dp)->destname);
1496             amfree(sched(dp)->dumpdate);
1497             amfree(sched(dp)->degr_dumpdate);
1498             amfree(sched(dp)->datestamp);
1499             amfree(dp->up);
1500         }
1501     } else if (taper_tape_error) {
1502         if(sched(dp)->taper_attempted >= 2) {
1503             log_add(L_FAIL, _("%s %s %s %d [too many taper retries]"),
1504                     dp->host->hostname, dp->name, sched(dp)->datestamp,
1505                     sched(dp)->level);
1506             g_printf("driver: taper failed %s %s, too many taper retry\n",
1507                    dp->host->hostname, dp->name);
1508             amfree(sched(dp)->destname);
1509             amfree(sched(dp)->dumpdate);
1510             amfree(sched(dp)->degr_dumpdate);
1511             amfree(sched(dp)->datestamp);
1512             amfree(dp->up);
1513         } else {
1514             g_printf("driver: taper will retry %s %s\n",
1515                    dp->host->hostname, dp->name);
1516             /* Re-insert into taper queue. */
1517             headqueue_disk(&tapeq, dp);
1518         }
1519     } else {
1520         delete_diskspace(dp);
1521         amfree(sched(dp)->destname);
1522         amfree(sched(dp)->dumpdate);
1523         amfree(sched(dp)->degr_dumpdate);
1524         amfree(sched(dp)->datestamp);
1525         amfree(dp->up);
1526     }
1527
1528     taper_busy = 0;
1529     taper_input_error = NULL;
1530     taper_tape_error = NULL;
1531     taper_disk = NULL;
1532             
1533     /* continue with those dumps waiting for diskspace */
1534     continue_port_dumps();
1535     start_some_dumps(&runq);
1536     startaflush();
1537 }
1538
1539 static void
1540 dumper_taper_result(
1541     disk_t *dp)
1542 {
1543     dumper_t *dumper;
1544     int is_partial;
1545     char *qname;
1546
1547     dumper = sched(dp)->dumper;
1548
1549     free_serial_dp(dp);
1550     if(dumper->result == DONE && taper_result == DONE) {
1551         update_info_dumper(dp, sched(dp)->origsize,
1552                            sched(dp)->dumpsize, sched(dp)->dumptime);
1553         update_info_taper(dp, taper_first_label, taper_first_fileno,
1554                           sched(dp)->level);
1555         qname = quote_string(dp->name); /*quote to take care of spaces*/
1556
1557         log_add(L_STATS, _("estimate %s %s %s %d [sec %ld nkb %lld ckb %lld kps %lu]"),
1558                 dp->host->hostname, qname, sched(dp)->datestamp,
1559                 sched(dp)->level,
1560                 sched(dp)->est_time, (long long)sched(dp)->est_nsize,
1561                 (long long)sched(dp)->est_csize,
1562                 sched(dp)->est_kps);
1563         amfree(qname);
1564     } else {
1565         update_failed_dump_to_tape(dp);
1566     }
1567
1568     is_partial = dumper->result != DONE || taper_result != DONE;
1569
1570     sched(dp)->dump_attempted += 1;
1571     sched(dp)->taper_attempted += 1;
1572
1573     if((dumper->result != DONE || taper_result != DONE) &&
1574         sched(dp)->dump_attempted <= 1 &&
1575         sched(dp)->taper_attempted <= 1) {
1576         enqueue_disk(&directq, dp);
1577     }
1578
1579     if(dumper->ev_read != NULL) {
1580         event_release(dumper->ev_read);
1581         dumper->ev_read = NULL;
1582     }
1583     if(taper_ev_read != NULL) {
1584         event_release(taper_ev_read);
1585         taper_ev_read = NULL;
1586     }
1587     taper_busy = 0;
1588     taper_input_error = NULL;
1589     taper_tape_error = NULL;
1590     dumper->busy = 0;
1591     dp->host->inprogress -= 1;
1592     dp->inprogress = 0;
1593     deallocate_bandwidth(dp->host->netif, sched(dp)->est_kps);
1594 }
1595
1596
1597 static dumper_t *
1598 idle_dumper(void)
1599 {
1600     dumper_t *dumper;
1601
1602     for(dumper = dmptable; dumper < dmptable+inparallel; dumper++)
1603         if(!dumper->busy && !dumper->down) return dumper;
1604
1605     return NULL;
1606 }
1607
1608 static void
1609 dumper_chunker_result(
1610     disk_t *    dp)
1611 {
1612     dumper_t *dumper;
1613     chunker_t *chunker;
1614     assignedhd_t **h=NULL;
1615     int activehd, i;
1616     off_t dummy;
1617     off_t size;
1618     int is_partial;
1619     char *qname;
1620
1621     dumper = sched(dp)->dumper;
1622     chunker = dumper->chunker;
1623
1624     free_serial_dp(dp);
1625
1626     h = sched(dp)->holdp;
1627     activehd = sched(dp)->activehd;
1628
1629     if(dumper->result == DONE && chunker->result == DONE) {
1630         update_info_dumper(dp, sched(dp)->origsize,
1631                            sched(dp)->dumpsize, sched(dp)->dumptime);
1632         qname = quote_string(dp->name);/*quote to take care of spaces*/
1633
1634         log_add(L_STATS, _("estimate %s %s %s %d [sec %ld nkb %lld ckb %lld kps %lu]"),
1635                 dp->host->hostname, qname, sched(dp)->datestamp,
1636                 sched(dp)->level,
1637                 sched(dp)->est_time, (long long)sched(dp)->est_nsize, 
1638                 (long long)sched(dp)->est_csize,
1639                 sched(dp)->est_kps);
1640         amfree(qname);
1641     }
1642
1643     deallocate_bandwidth(dp->host->netif, sched(dp)->est_kps);
1644
1645     is_partial = dumper->result != DONE || chunker->result != DONE;
1646     rename_tmp_holding(sched(dp)->destname, !is_partial);
1647
1648     dummy = (off_t)0;
1649     for( i = 0, h = sched(dp)->holdp; i < activehd; i++ ) {
1650         dummy += h[i]->used;
1651     }
1652
1653     size = holding_file_size(sched(dp)->destname, 0);
1654     h[activehd]->used = size - dummy;
1655     h[activehd]->disk->allocated_dumpers--;
1656     adjust_diskspace(dp, DONE);
1657
1658     sched(dp)->dump_attempted += 1;
1659
1660     if((dumper->result != DONE || chunker->result != DONE) &&
1661        sched(dp)->dump_attempted <= 1) {
1662         delete_diskspace(dp);
1663         if (sched(dp)->no_space) {
1664             enqueue_disk(&directq, dp);
1665         } else {
1666             enqueue_disk(&runq, dp);
1667         }
1668     }
1669     else if(size > (off_t)DISK_BLOCK_KB) {
1670         enqueue_disk(&tapeq, dp);
1671     }
1672     else {
1673         delete_diskspace(dp);
1674     }
1675
1676     dumper->busy = 0;
1677     dp->host->inprogress -= 1;
1678     dp->inprogress = 0;
1679
1680     waitpid(chunker->pid, NULL, 0 );
1681     aclose(chunker->fd);
1682     chunker->fd = -1;
1683     chunker->down = 1;
1684     
1685     dp = NULL;
1686     if (chunker->result == ABORT_FINISHED)
1687         pending_aborts--;
1688     continue_port_dumps();
1689     /*
1690      * Wakeup any dumpers that are sleeping because of network
1691      * or disk constraints.
1692      */
1693     start_some_dumps(&runq);
1694     startaflush();
1695 }
1696
1697
1698 static void
1699 handle_dumper_result(
1700     void *      cookie)
1701 {
1702     /*static int pending_aborts = 0;*/
1703     dumper_t *dumper = cookie;
1704     disk_t *dp, *sdp;
1705     cmd_t cmd;
1706     int result_argc;
1707     char *qname;
1708     char *result_argv[MAX_ARGS+1];
1709
1710     assert(dumper != NULL);
1711     dp = dumper->dp;
1712     assert(dp != NULL);
1713     assert(sched(dp) != NULL);
1714     do {
1715
1716         short_dump_state();
1717
1718         cmd = getresult(dumper->fd, 1, &result_argc, result_argv, MAX_ARGS+1);
1719
1720         if(cmd != BOGUS) {
1721             /* result_argv[2] always contains the serial number */
1722             sdp = serial2disk(result_argv[2]);
1723             if (sdp != dp) {
1724                 error(_("Invalid serial number %s"), result_argv[2]);
1725                 g_assert_not_reached();
1726             }
1727         }
1728
1729         qname = quote_string(dp->name);
1730         switch(cmd) {
1731
1732         case DONE: /* DONE <handle> <origsize> <dumpsize> <dumptime> <errstr> */
1733             if(result_argc != 6) {
1734                 error(_("error [dumper DONE result_argc != 6: %d]"), result_argc);
1735                 /*NOTREACHED*/
1736             }
1737
1738             sched(dp)->origsize = OFF_T_ATOI(result_argv[3]);
1739             sched(dp)->dumptime = TIME_T_ATOI(result_argv[5]);
1740
1741             g_printf(_("driver: finished-cmd time %s %s dumped %s:%s\n"),
1742                    walltime_str(curclock()), dumper->name,
1743                    dp->host->hostname, qname);
1744             fflush(stdout);
1745
1746             dumper->result = cmd;
1747
1748             break;
1749
1750         case TRYAGAIN: /* TRY-AGAIN <handle> <errstr> */
1751             /*
1752              * Requeue this disk, and fall through to the FAILED
1753              * case for cleanup.
1754              */
1755             if(sched(dp)->dump_attempted) {
1756                 log_add(L_FAIL, _("%s %s %s %d [too many dumper retry: %s]"),
1757                     dp->host->hostname, dp->name, sched(dp)->datestamp,
1758                     sched(dp)->level, result_argv[3]);
1759                 g_printf(_("driver: dump failed %s %s %s, too many dumper retry: %s\n"),
1760                         result_argv[2], dp->host->hostname, dp->name,
1761                         result_argv[3]);
1762             }
1763             /* FALLTHROUGH */
1764         case FAILED: /* FAILED <handle> <errstr> */
1765             /*free_serial(result_argv[2]);*/
1766             dumper->result = cmd;
1767             break;
1768
1769         case ABORT_FINISHED: /* ABORT-FINISHED <handle> */
1770             /*
1771              * We sent an ABORT from the NO-ROOM case because this dump
1772              * wasn't going to fit onto the holding disk.  We now need to
1773              * clean up the remains of this image, and try to finish
1774              * other dumps that are waiting on disk space.
1775              */
1776             assert(pending_aborts);
1777             /*free_serial(result_argv[2]);*/
1778             dumper->result = cmd;
1779             break;
1780
1781         case BOGUS:
1782             /* either EOF or garbage from dumper.  Turn it off */
1783             log_add(L_WARNING, _("%s pid %ld is messed up, ignoring it.\n"),
1784                     dumper->name, (long)dumper->pid);
1785             if (dumper->ev_read) {
1786                 event_release(dumper->ev_read);
1787                 dumper->ev_read = NULL;
1788             }
1789             aclose(dumper->fd);
1790             dumper->busy = 0;
1791             dumper->down = 1;   /* mark it down so it isn't used again */
1792             if(dp) {
1793                 /* if it was dumping something, zap it and try again */
1794                 if(sched(dp)->dump_attempted) {
1795                 log_add(L_FAIL, _("%s %s %s %d [%s died]"),
1796                         dp->host->hostname, qname, sched(dp)->datestamp,
1797                         sched(dp)->level, dumper->name);
1798                 }
1799                 else {
1800                 log_add(L_WARNING, _("%s died while dumping %s:%s lev %d."),
1801                         dumper->name, dp->host->hostname, qname,
1802                         sched(dp)->level);
1803                 }
1804             }
1805             dumper->result = cmd;
1806             break;
1807
1808         default:
1809             assert(0);
1810         }
1811         amfree(qname);
1812
1813         /* send the dumper result to the chunker */
1814         if (dumper->chunker) {
1815             if (dumper->chunker->down == 0 && dumper->chunker->fd != -1 &&
1816                 dumper->chunker->result == LAST_TOK) {
1817                 if (cmd == DONE) {
1818                     chunker_cmd(dumper->chunker, DONE, dp);
1819                 }
1820                 else {
1821                     chunker_cmd(dumper->chunker, FAILED, dp);
1822                 }
1823             }
1824             if( dumper->result != LAST_TOK &&
1825                 dumper->chunker->result != LAST_TOK)
1826                 dumper_chunker_result(dp);
1827         } else { /* send the dumper result to the taper */
1828             if (taper_sendresult) {
1829                 if (cmd == DONE) {
1830                     taper_cmd(DONE, driver_timestamp, NULL, 0, NULL);
1831                 } else {
1832                     taper_cmd(FAILED, driver_timestamp, NULL, 0, NULL);
1833                 }
1834                 taper_sendresult = 0;
1835             }
1836         }
1837         if (taper_dumper && taper_result != LAST_TOK) {
1838             dumper_taper_result(dp);
1839         }
1840     } while(areads_dataready(dumper->fd));
1841 }
1842
1843
1844 static void
1845 handle_chunker_result(
1846     void *      cookie)
1847 {
1848     /*static int pending_aborts = 0;*/
1849     chunker_t *chunker = cookie;
1850     assignedhd_t **h=NULL;
1851     dumper_t *dumper;
1852     disk_t *dp, *sdp;
1853     cmd_t cmd;
1854     int result_argc;
1855     char *result_argv[MAX_ARGS+1];
1856     int dummy;
1857     int activehd = -1;
1858     char *qname;
1859
1860     assert(chunker != NULL);
1861     dumper = chunker->dumper;
1862     assert(dumper != NULL);
1863     dp = dumper->dp;
1864     assert(dp != NULL);
1865     assert(sched(dp) != NULL);
1866     assert(sched(dp)->destname != NULL);
1867     assert(dp != NULL && sched(dp) != NULL && sched(dp)->destname);
1868
1869     if(dp && sched(dp) && sched(dp)->holdp) {
1870         h = sched(dp)->holdp;
1871         activehd = sched(dp)->activehd;
1872     }
1873
1874     do {
1875
1876         short_dump_state();
1877
1878         cmd = getresult(chunker->fd, 1, &result_argc, result_argv, MAX_ARGS+1);
1879
1880         if(cmd != BOGUS) {
1881             /* result_argv[2] always contains the serial number */
1882             sdp = serial2disk(result_argv[2]);
1883             if (sdp != dp) {
1884                 error(_("Invalid serial number %s"), result_argv[2]);
1885                 g_assert_not_reached();
1886             }
1887         }
1888
1889         switch(cmd) {
1890
1891         case PARTIAL: /* PARTIAL <handle> <dumpsize> <errstr> */
1892         case DONE: /* DONE <handle> <dumpsize> <errstr> */
1893             if(result_argc != 4) {
1894                 error(_("error [chunker %s result_argc != 4: %d]"), cmdstr[cmd],
1895                       result_argc);
1896                 /*NOTREACHED*/
1897             }
1898             /*free_serial(result_argv[2]);*/
1899
1900             sched(dp)->dumpsize = (off_t)atof(result_argv[3]);
1901
1902             qname = quote_string(dp->name);
1903             g_printf(_("driver: finished-cmd time %s %s chunked %s:%s\n"),
1904                    walltime_str(curclock()), chunker->name,
1905                    dp->host->hostname, qname);
1906             fflush(stdout);
1907             amfree(qname);
1908
1909             event_release(chunker->ev_read);
1910
1911             chunker->result = cmd;
1912
1913             break;
1914
1915         case TRYAGAIN: /* TRY-AGAIN <handle> <errstr> */
1916             event_release(chunker->ev_read);
1917
1918             chunker->result = cmd;
1919
1920             break;
1921         case FAILED: /* FAILED <handle> <errstr> */
1922             /*free_serial(result_argv[2]);*/
1923
1924             event_release(chunker->ev_read);
1925
1926             chunker->result = cmd;
1927
1928             break;
1929
1930         case NO_ROOM: /* NO-ROOM <handle> <missing_size> */
1931             if (!h || activehd < 0) { /* should never happen */
1932                 error(_("!h || activehd < 0"));
1933                 /*NOTREACHED*/
1934             }
1935             h[activehd]->used -= OFF_T_ATOI(result_argv[3]);
1936             h[activehd]->reserved -= OFF_T_ATOI(result_argv[3]);
1937             h[activehd]->disk->allocated_space -= OFF_T_ATOI(result_argv[3]);
1938             h[activehd]->disk->disksize -= OFF_T_ATOI(result_argv[3]);
1939             break;
1940
1941         case RQ_MORE_DISK: /* RQ-MORE-DISK <handle> */
1942             if (!h || activehd < 0) { /* should never happen */
1943                 error(_("!h || activehd < 0"));
1944                 /*NOTREACHED*/
1945             }
1946             h[activehd]->disk->allocated_dumpers--;
1947             h[activehd]->used = h[activehd]->reserved;
1948             if( h[++activehd] ) { /* There's still some allocated space left.
1949                                    * Tell the dumper about it. */
1950                 sched(dp)->activehd++;
1951                 chunker_cmd( chunker, CONTINUE, dp );
1952             } else { /* !h[++activehd] - must allocate more space */
1953                 sched(dp)->act_size = sched(dp)->est_size; /* not quite true */
1954                 sched(dp)->est_size = (sched(dp)->act_size/(off_t)20) * (off_t)21; /* +5% */
1955                 sched(dp)->est_size = am_round(sched(dp)->est_size, (off_t)DISK_BLOCK_KB);
1956                 if (sched(dp)->est_size < sched(dp)->act_size + 2*DISK_BLOCK_KB)
1957                     sched(dp)->est_size += 2 * DISK_BLOCK_KB;
1958                 h = find_diskspace( sched(dp)->est_size - sched(dp)->act_size,
1959                                     &dummy,
1960                                     h[activehd-1] );
1961                 if( !h ) {
1962                     /* No diskspace available. The reason for this will be
1963                      * determined in continue_port_dumps(). */
1964                     enqueue_disk( &roomq, dp );
1965                     continue_port_dumps();
1966                 } else {
1967                     /* OK, allocate space for disk and have chunker continue */
1968                     sched(dp)->activehd = assign_holdingdisk( h, dp );
1969                     chunker_cmd( chunker, CONTINUE, dp );
1970                     amfree(h);
1971                 }
1972             }
1973             break;
1974
1975         case ABORT_FINISHED: /* ABORT-FINISHED <handle> */
1976             /*
1977              * We sent an ABORT from the NO-ROOM case because this dump
1978              * wasn't going to fit onto the holding disk.  We now need to
1979              * clean up the remains of this image, and try to finish
1980              * other dumps that are waiting on disk space.
1981              */
1982             /*assert(pending_aborts);*/
1983
1984             /*free_serial(result_argv[2]);*/
1985
1986             event_release(chunker->ev_read);
1987
1988             chunker->result = cmd;
1989
1990             break;
1991
1992         case BOGUS:
1993             /* either EOF or garbage from chunker.  Turn it off */
1994             log_add(L_WARNING, _("%s pid %ld is messed up, ignoring it.\n"),
1995                     chunker->name, (long)chunker->pid);
1996
1997             if(dp) {
1998                 /* if it was dumping something, zap it and try again */
1999                 if (!h || activehd < 0) { /* should never happen */
2000                     error(_("!h || activehd < 0"));
2001                     /*NOTREACHED*/
2002                 }
2003                 qname = quote_string(dp->name);
2004                 if(sched(dp)->dump_attempted) {
2005                     log_add(L_FAIL, _("%s %s %s %d [%s died]"),
2006                             dp->host->hostname, qname, sched(dp)->datestamp,
2007                             sched(dp)->level, chunker->name);
2008                 }
2009                 else {
2010                     log_add(L_WARNING, _("%s died while dumping %s:%s lev %d."),
2011                             chunker->name, dp->host->hostname, qname,
2012                             sched(dp)->level);
2013                 }
2014                 amfree(qname);
2015                 dp = NULL;
2016             }
2017
2018             event_release(chunker->ev_read);
2019
2020             chunker->result = cmd;
2021
2022             break;
2023
2024         default:
2025             assert(0);
2026         }
2027
2028         if(chunker->result != LAST_TOK && chunker->dumper->result != LAST_TOK)
2029             dumper_chunker_result(dp);
2030
2031     } while(areads_dataready(chunker->fd));
2032 }
2033
2034
2035 static disklist_t
2036 read_flush(void)
2037 {
2038     sched_t *sp;
2039     disk_t *dp;
2040     int line;
2041     dumpfile_t file;
2042     char *hostname, *diskname, *datestamp;
2043     int level;
2044     char *destname;
2045     disk_t *dp1;
2046     char *inpline = NULL;
2047     char *command;
2048     char *s;
2049     int ch;
2050     disklist_t tq;
2051     char *qname = NULL;
2052     char *qdestname = NULL;
2053
2054     tq.head = tq.tail = NULL;
2055
2056     for(line = 0; (inpline = agets(stdin)) != NULL; free(inpline)) {
2057         line++;
2058         if (inpline[0] == '\0')
2059             continue;
2060
2061         s = inpline;
2062         ch = *s++;
2063
2064         skip_whitespace(s, ch);                 /* find the command */
2065         if(ch == '\0') {
2066             error(_("flush line %d: syntax error (no command)"), line);
2067             /*NOTREACHED*/
2068         }
2069         command = s - 1;
2070         skip_non_whitespace(s, ch);
2071         s[-1] = '\0';
2072
2073         if(strcmp(command,"ENDFLUSH") == 0) {
2074             break;
2075         }
2076
2077         if(strcmp(command,"FLUSH") != 0) {
2078             error(_("flush line %d: syntax error (%s != FLUSH)"), line, command);
2079             /*NOTREACHED*/
2080         }
2081
2082         skip_whitespace(s, ch);                 /* find the hostname */
2083         if(ch == '\0') {
2084             error(_("flush line %d: syntax error (no hostname)"), line);
2085             /*NOTREACHED*/
2086         }
2087         hostname = s - 1;
2088         skip_non_whitespace(s, ch);
2089         s[-1] = '\0';
2090
2091         skip_whitespace(s, ch);                 /* find the diskname */
2092         if(ch == '\0') {
2093             error(_("flush line %d: syntax error (no diskname)"), line);
2094             /*NOTREACHED*/
2095         }
2096         qname = s - 1;
2097         skip_quoted_string(s, ch);
2098         s[-1] = '\0';                           /* terminate the disk name */
2099         diskname = unquote_string(qname);
2100
2101         skip_whitespace(s, ch);                 /* find the datestamp */
2102         if(ch == '\0') {
2103             error(_("flush line %d: syntax error (no datestamp)"), line);
2104             /*NOTREACHED*/
2105         }
2106         datestamp = s - 1;
2107         skip_non_whitespace(s, ch);
2108         s[-1] = '\0';
2109
2110         skip_whitespace(s, ch);                 /* find the level number */
2111         if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
2112             error(_("flush line %d: syntax error (bad level)"), line);
2113             /*NOTREACHED*/
2114         }
2115         skip_integer(s, ch);
2116
2117         skip_whitespace(s, ch);                 /* find the filename */
2118         if(ch == '\0') {
2119             error(_("flush line %d: syntax error (no filename)"), line);
2120             /*NOTREACHED*/
2121         }
2122         qdestname = s - 1;
2123         skip_quoted_string(s, ch);
2124         s[-1] = '\0';
2125         destname = unquote_string(qdestname);
2126
2127         holding_file_get_dumpfile(destname, &file);
2128         if( file.type != F_DUMPFILE) {
2129             if( file.type != F_CONT_DUMPFILE )
2130                 log_add(L_INFO, _("%s: ignoring cruft file."), destname);
2131             amfree(diskname);
2132             amfree(destname);
2133             continue;
2134         }
2135
2136         if(strcmp(hostname, file.name) != 0 ||
2137            strcmp(diskname, file.disk) != 0 ||
2138            strcmp(datestamp, file.datestamp) != 0) {
2139             log_add(L_INFO, _("disk %s:%s not consistent with file %s"),
2140                     hostname, diskname, destname);
2141             amfree(diskname);
2142             amfree(destname);
2143             continue;
2144         }
2145         amfree(diskname);
2146
2147         dp = lookup_disk(file.name, file.disk);
2148
2149         if (dp == NULL) {
2150             log_add(L_INFO, _("%s: disk %s:%s not in database, skipping it."),
2151                     destname, file.name, file.disk);
2152             amfree(destname);
2153             continue;
2154         }
2155
2156         if(file.dumplevel < 0 || file.dumplevel > 9) {
2157             log_add(L_INFO, _("%s: ignoring file with bogus dump level %d."),
2158                     destname, file.dumplevel);
2159             amfree(destname);
2160             continue;
2161         }
2162
2163         if (holding_file_size(destname,1) <= 0) {
2164             log_add(L_INFO, "%s: removing file with no data.", destname);
2165             holding_file_unlink(destname);
2166             amfree(destname);
2167             continue;
2168         }
2169
2170         dp1 = (disk_t *)alloc(SIZEOF(disk_t));
2171         *dp1 = *dp;
2172         dp1->next = dp1->prev = NULL;
2173
2174         /* add it to the flushhost list */
2175         if(!flushhost) {
2176             flushhost = alloc(SIZEOF(am_host_t));
2177             flushhost->next = NULL;
2178             flushhost->hostname = stralloc("FLUSHHOST");
2179             flushhost->up = NULL;
2180             flushhost->features = NULL;
2181         }
2182         dp1->hostnext = flushhost->disks;
2183         flushhost->disks = dp1;
2184
2185         sp = (sched_t *) alloc(SIZEOF(sched_t));
2186         sp->destname = destname;
2187         sp->level = file.dumplevel;
2188         sp->dumpdate = NULL;
2189         sp->degr_dumpdate = NULL;
2190         sp->datestamp = stralloc(file.datestamp);
2191         sp->est_nsize = (off_t)0;
2192         sp->est_csize = (off_t)0;
2193         sp->est_time = 0;
2194         sp->est_kps = 10;
2195         sp->priority = 0;
2196         sp->degr_level = -1;
2197         sp->dump_attempted = 0;
2198         sp->taper_attempted = 0;
2199         sp->act_size = holding_file_size(destname, 0);
2200         sp->holdp = build_diskspace(destname);
2201         if(sp->holdp == NULL) continue;
2202         sp->dumper = NULL;
2203         sp->timestamp = (time_t)0;
2204
2205         dp1->up = (char *)sp;
2206
2207         enqueue_disk(&tq, dp1);
2208     }
2209     amfree(inpline);
2210
2211     /*@i@*/ return tq;
2212 }
2213
2214 static void
2215 read_schedule(
2216     void *      cookie)
2217 {
2218     sched_t *sp;
2219     disk_t *dp;
2220     int level, line, priority;
2221     char *dumpdate, *degr_dumpdate;
2222     int degr_level;
2223     time_t time, degr_time;
2224     time_t *time_p = &time;
2225     time_t *degr_time_p = &degr_time;
2226     off_t nsize, csize, degr_nsize, degr_csize;
2227     unsigned long kps, degr_kps;
2228     char *hostname, *features, *diskname, *datestamp, *inpline = NULL;
2229     char *command;
2230     char *s;
2231     int ch;
2232     off_t flush_size = (off_t)0;
2233     char *qname = NULL;
2234     long long time_;
2235     long long nsize_;
2236     long long csize_;
2237     long long degr_nsize_;
2238     long long degr_csize_;
2239
2240     (void)cookie;       /* Quiet unused parameter warning */
2241
2242     event_release(schedule_ev_read);
2243
2244     /* read schedule from stdin */
2245
2246     for(line = 0; (inpline = agets(stdin)) != NULL; free(inpline)) {
2247         if (inpline[0] == '\0')
2248             continue;
2249         line++;
2250
2251         s = inpline;
2252         ch = *s++;
2253
2254         skip_whitespace(s, ch);                 /* find the command */
2255         if(ch == '\0') {
2256             error(_("schedule line %d: syntax error (no command)"), line);
2257             /*NOTREACHED*/
2258         }
2259         command = s - 1;
2260         skip_non_whitespace(s, ch);
2261         s[-1] = '\0';
2262
2263         if(strcmp(command,"DUMP") != 0) {
2264             error(_("schedule line %d: syntax error (%s != DUMP)"), line, command);
2265             /*NOTREACHED*/
2266         }
2267
2268         skip_whitespace(s, ch);                 /* find the host name */
2269         if(ch == '\0') {
2270             error(_("schedule line %d: syntax error (no host name)"), line);
2271             /*NOTREACHED*/
2272         }
2273         hostname = s - 1;
2274         skip_non_whitespace(s, ch);
2275         s[-1] = '\0';
2276
2277         skip_whitespace(s, ch);                 /* find the feature list */
2278         if(ch == '\0') {
2279             error(_("schedule line %d: syntax error (no feature list)"), line);
2280             /*NOTREACHED*/
2281         }
2282         features = s - 1;
2283         skip_non_whitespace(s, ch);
2284         s[-1] = '\0';
2285
2286         skip_whitespace(s, ch);                 /* find the disk name */
2287         if(ch == '\0') {
2288             error(_("schedule line %d: syntax error (no disk name)"), line);
2289             /*NOTREACHED*/
2290         }
2291         qname = s - 1;
2292         skip_quoted_string(s, ch);
2293         s[-1] = '\0';                           /* terminate the disk name */
2294         diskname = unquote_string(qname);
2295
2296         skip_whitespace(s, ch);                 /* find the datestamp */
2297         if(ch == '\0') {
2298             error(_("schedule line %d: syntax error (no datestamp)"), line);
2299             /*NOTREACHED*/
2300         }
2301         datestamp = s - 1;
2302         skip_non_whitespace(s, ch);
2303         s[-1] = '\0';
2304
2305         skip_whitespace(s, ch);                 /* find the priority number */
2306         if(ch == '\0' || sscanf(s - 1, "%d", &priority) != 1) {
2307             error(_("schedule line %d: syntax error (bad priority)"), line);
2308             /*NOTREACHED*/
2309         }
2310         skip_integer(s, ch);
2311
2312         skip_whitespace(s, ch);                 /* find the level number */
2313         if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
2314             error(_("schedule line %d: syntax error (bad level)"), line);
2315             /*NOTREACHED*/
2316         }
2317         skip_integer(s, ch);
2318
2319         skip_whitespace(s, ch);                 /* find the dump date */
2320         if(ch == '\0') {
2321             error(_("schedule line %d: syntax error (bad dump date)"), line);
2322             /*NOTREACHED*/
2323         }
2324         dumpdate = s - 1;
2325         skip_non_whitespace(s, ch);
2326         s[-1] = '\0';
2327
2328         skip_whitespace(s, ch);                 /* find the native size */
2329         nsize_ = (off_t)0;
2330         if(ch == '\0' || sscanf(s - 1, "%lld", &nsize_) != 1) {
2331             error(_("schedule line %d: syntax error (bad nsize)"), line);
2332             /*NOTREACHED*/
2333         }
2334         nsize = (off_t)nsize_;
2335         skip_integer(s, ch);
2336
2337         skip_whitespace(s, ch);                 /* find the compressed size */
2338         csize_ = (off_t)0;
2339         if(ch == '\0' || sscanf(s - 1, "%lld", &csize_) != 1) {
2340             error(_("schedule line %d: syntax error (bad csize)"), line);
2341             /*NOTREACHED*/
2342         }
2343         csize = (off_t)csize_;
2344         skip_integer(s, ch);
2345
2346         skip_whitespace(s, ch);                 /* find the time number */
2347         if(ch == '\0' || sscanf(s - 1, "%lld", &time_) != 1) {
2348             error(_("schedule line %d: syntax error (bad estimated time)"), line);
2349             /*NOTREACHED*/
2350         }
2351         *time_p = (time_t)time_;
2352         skip_integer(s, ch);
2353
2354         skip_whitespace(s, ch);                 /* find the kps number */
2355         if(ch == '\0' || sscanf(s - 1, "%lu", &kps) != 1) {
2356             error(_("schedule line %d: syntax error (bad kps)"), line);
2357             continue;
2358         }
2359         skip_integer(s, ch);
2360
2361         degr_dumpdate = NULL;                   /* flag if degr fields found */
2362         skip_whitespace(s, ch);                 /* find the degr level number */
2363         if(ch != '\0') {
2364             if(sscanf(s - 1, "%d", &degr_level) != 1) {
2365                 error(_("schedule line %d: syntax error (bad degr level)"), line);
2366                 /*NOTREACHED*/
2367             }
2368             skip_integer(s, ch);
2369
2370             skip_whitespace(s, ch);             /* find the degr dump date */
2371             if(ch == '\0') {
2372                 error(_("schedule line %d: syntax error (bad degr dump date)"), line);
2373                 /*NOTREACHED*/
2374             }
2375             degr_dumpdate = s - 1;
2376             skip_non_whitespace(s, ch);
2377             s[-1] = '\0';
2378
2379             skip_whitespace(s, ch);             /* find the degr native size */
2380             degr_nsize_ = (off_t)0;
2381             if(ch == '\0'  || sscanf(s - 1, "%lld", &degr_nsize_) != 1) {
2382                 error(_("schedule line %d: syntax error (bad degr nsize)"), line);
2383                 /*NOTREACHED*/
2384             }
2385             degr_nsize = (off_t)degr_nsize_;
2386             skip_integer(s, ch);
2387
2388             skip_whitespace(s, ch);             /* find the degr compressed size */
2389             degr_csize_ = (off_t)0;
2390             if(ch == '\0'  || sscanf(s - 1, "%lld", &degr_csize_) != 1) {
2391                 error(_("schedule line %d: syntax error (bad degr csize)"), line);
2392                 /*NOTREACHED*/
2393             }
2394             degr_csize = (off_t)degr_csize_;
2395             skip_integer(s, ch);
2396
2397             skip_whitespace(s, ch);             /* find the degr time number */
2398             if(ch == '\0' || sscanf(s - 1, "%lld", &time_) != 1) {
2399                 error(_("schedule line %d: syntax error (bad degr estimated time)"), line);
2400                 /*NOTREACHED*/
2401             }
2402             *degr_time_p = (time_t)time_;
2403             skip_integer(s, ch);
2404
2405             skip_whitespace(s, ch);             /* find the degr kps number */
2406             if(ch == '\0' || sscanf(s - 1, "%lu", &degr_kps) != 1) {
2407                 error(_("schedule line %d: syntax error (bad degr kps)"), line);
2408                 /*NOTREACHED*/
2409             }
2410             skip_integer(s, ch);
2411         } else {
2412             degr_level = -1;
2413             degr_nsize = (off_t)0;
2414             degr_csize = (off_t)0;
2415             degr_time = (time_t)0;
2416             degr_kps = 0;
2417         }
2418
2419         dp = lookup_disk(hostname, diskname);
2420         if(dp == NULL) {
2421             log_add(L_WARNING,
2422                     _("schedule line %d: %s:'%s' not in disklist, ignored"),
2423                     line, hostname, qname);
2424             amfree(diskname);
2425             continue;
2426         }
2427
2428         sp = (sched_t *) alloc(SIZEOF(sched_t));
2429         /*@ignore@*/
2430         sp->level = level;
2431         sp->dumpdate = stralloc(dumpdate);
2432         sp->est_nsize = DISK_BLOCK_KB + nsize; /* include header */
2433         sp->est_csize = DISK_BLOCK_KB + csize; /* include header */
2434         /* round estimate to next multiple of DISK_BLOCK_KB */
2435         sp->est_csize = am_round(sp->est_csize, DISK_BLOCK_KB);
2436         sp->est_size = sp->est_csize;
2437         sp->est_time = time;
2438         sp->est_kps = kps;
2439         sp->priority = priority;
2440         sp->datestamp = stralloc(datestamp);
2441
2442         if(degr_dumpdate) {
2443             sp->degr_level = degr_level;
2444             sp->degr_dumpdate = stralloc(degr_dumpdate);
2445             sp->degr_nsize = DISK_BLOCK_KB + degr_nsize;
2446             sp->degr_csize = DISK_BLOCK_KB + degr_csize;
2447             /* round estimate to next multiple of DISK_BLOCK_KB */
2448             sp->degr_csize = am_round(sp->degr_csize, DISK_BLOCK_KB);
2449             sp->degr_time = degr_time;
2450             sp->degr_kps = degr_kps;
2451         } else {
2452             sp->degr_level = -1;
2453             sp->degr_dumpdate = NULL;
2454         }
2455         /*@end@*/
2456
2457         sp->dump_attempted = 0;
2458         sp->taper_attempted = 0;
2459         sp->act_size = 0;
2460         sp->holdp = NULL;
2461         sp->activehd = -1;
2462         sp->dumper = NULL;
2463         sp->timestamp = (time_t)0;
2464         sp->destname = NULL;
2465         sp->no_space = 0;
2466
2467         dp->up = (char *) sp;
2468         if(dp->host->features == NULL) {
2469             dp->host->features = am_string_to_feature(features);
2470         }
2471         remove_disk(&waitq, dp);
2472         if (dp->to_holdingdisk == HOLD_NEVER) {
2473             enqueue_disk(&directq, dp);
2474         } else {
2475             enqueue_disk(&runq, dp);
2476         }
2477         flush_size += sp->act_size;
2478         amfree(diskname);
2479     }
2480     g_printf(_("driver: flush size %lld\n"), (long long)flush_size);
2481     amfree(inpline);
2482     if(line == 0)
2483         log_add(L_WARNING, _("WARNING: got empty schedule from planner"));
2484     if(need_degraded==1) start_degraded_mode(&runq);
2485     schedule_done = 1;
2486     start_some_dumps(&runq);
2487 }
2488
2489 static unsigned long
2490 free_kps(
2491     netif_t *ip)
2492 {
2493     unsigned long res;
2494
2495     if (ip == NULL) {
2496         netif_t *p;
2497         unsigned long maxusage=0;
2498         unsigned long curusage=0;
2499         for(p = disklist_netifs(); p != NULL; p = p->next) {
2500             maxusage += interface_get_maxusage(p->config);
2501             curusage += p->curusage;
2502         }
2503         if (maxusage >= curusage)
2504             res = maxusage - curusage;
2505         else
2506             res = 0;
2507 #ifndef __lint
2508     } else {
2509         if ((unsigned long)interface_get_maxusage(ip->config) >= ip->curusage)
2510             res = interface_get_maxusage(ip->config) - ip->curusage;
2511         else
2512             res = 0;
2513 #endif
2514     }
2515
2516     return res;
2517 }
2518
2519 static void
2520 interface_state(
2521     char *time_str)
2522 {
2523     netif_t *ip;
2524
2525     g_printf(_("driver: interface-state time %s"), time_str);
2526
2527     for(ip = disklist_netifs(); ip != NULL; ip = ip->next) {
2528         g_printf(_(" if %s: free %lu"), interface_name(ip->config), free_kps(ip));
2529     }
2530     g_printf("\n");
2531 }
2532
2533 static void
2534 allocate_bandwidth(
2535     netif_t *           ip,
2536     unsigned long       kps)
2537 {
2538     ip->curusage += kps;
2539 }
2540
2541 static void
2542 deallocate_bandwidth(
2543     netif_t *           ip,
2544     unsigned long       kps)
2545 {
2546     assert(kps <= ip->curusage);
2547     ip->curusage -= kps;
2548 }
2549
2550 /* ------------ */
2551 static off_t
2552 free_space(void)
2553 {
2554     holdalloc_t *ha;
2555     off_t total_free;
2556     off_t diff;
2557
2558     total_free = (off_t)0;
2559     for(ha = holdalloc; ha != NULL; ha = ha->next) {
2560         diff = ha->disksize - ha->allocated_space;
2561         if(diff > (off_t)0)
2562             total_free += diff;
2563     }
2564     return total_free;
2565 }
2566
2567 /*
2568  * We return an array of pointers to assignedhd_t. The array contains at
2569  * most one entry per holding disk. The list of pointers is terminated by
2570  * a NULL pointer. Each entry contains a pointer to a holdingdisk and
2571  * how much diskspace to use on that disk. Later on, assign_holdingdisk
2572  * will allocate the given amount of space.
2573  * If there is not enough room on the holdingdisks, NULL is returned.
2574  */
2575
2576 static assignedhd_t **
2577 find_diskspace(
2578     off_t               size,
2579     int *               cur_idle,
2580     assignedhd_t *      pref)
2581 {
2582     assignedhd_t **result = NULL;
2583     holdalloc_t *ha, *minp;
2584     int i=0;
2585     int j, minj;
2586     char *used;
2587     off_t halloc, dalloc, hfree, dfree;
2588
2589     (void)cur_idle;     /* Quiet unused parameter warning */
2590
2591     if (size < 2*DISK_BLOCK_KB)
2592         size = 2*DISK_BLOCK_KB;
2593     size = am_round(size, (off_t)DISK_BLOCK_KB);
2594
2595     hold_debug(1, _("find_diskspace: want %lld K\n"),
2596                    (long long)size);
2597
2598     used = alloc(SIZEOF(*used) * num_holdalloc);/*disks used during this run*/
2599     memset( used, 0, (size_t)num_holdalloc );
2600     result = alloc(SIZEOF(assignedhd_t *) * (num_holdalloc + 1));
2601     result[0] = NULL;
2602
2603     while( i < num_holdalloc && size > (off_t)0 ) {
2604         /* find the holdingdisk with the fewest active dumpers and among
2605          * those the one with the biggest free space
2606          */
2607         minp = NULL; minj = -1;
2608         for(j = 0, ha = holdalloc; ha != NULL; ha = ha->next, j++ ) {
2609             if( pref && pref->disk == ha && !used[j] &&
2610                 ha->allocated_space <= ha->disksize - (off_t)DISK_BLOCK_KB) {
2611                 minp = ha;
2612                 minj = j;
2613                 break;
2614             }
2615             else if( ha->allocated_space <= ha->disksize - (off_t)(2*DISK_BLOCK_KB) &&
2616                 !used[j] &&
2617                 (!minp ||
2618                  ha->allocated_dumpers < minp->allocated_dumpers ||
2619                  (ha->allocated_dumpers == minp->allocated_dumpers &&
2620                   ha->disksize-ha->allocated_space > minp->disksize-minp->allocated_space)) ) {
2621                 minp = ha;
2622                 minj = j;
2623             }
2624         }
2625
2626         pref = NULL;
2627         if( !minp ) { break; } /* all holding disks are full */
2628         used[minj] = 1;
2629
2630         /* hfree = free space on the disk */
2631         hfree = minp->disksize - minp->allocated_space;
2632
2633         /* dfree = free space for data, remove 1 header for each chunksize */
2634         dfree = hfree - (((hfree-(off_t)1)/holdingdisk_get_chunksize(minp->hdisk))+(off_t)1) * (off_t)DISK_BLOCK_KB;
2635
2636         /* dalloc = space I can allocate for data */
2637         dalloc = ( dfree < size ) ? dfree : size;
2638
2639         /* halloc = space to allocate, including 1 header for each chunksize */
2640         halloc = dalloc + (((dalloc-(off_t)1)/holdingdisk_get_chunksize(minp->hdisk))+(off_t)1) * (off_t)DISK_BLOCK_KB;
2641
2642         hold_debug(1, _("find_diskspace: find diskspace: size %lld hf %lld df %lld da %lld ha %lld\n"),
2643                        (long long)size,
2644                        (long long)hfree,
2645                        (long long)dfree,
2646                        (long long)dalloc,
2647                        (long long)halloc);
2648         size -= dalloc;
2649         result[i] = alloc(SIZEOF(assignedhd_t));
2650         result[i]->disk = minp;
2651         result[i]->reserved = halloc;
2652         result[i]->used = (off_t)0;
2653         result[i]->destname = NULL;
2654         result[i+1] = NULL;
2655         i++;
2656     }
2657     amfree(used);
2658
2659     if(size != (off_t)0) { /* not enough space available */
2660         g_printf(_("find diskspace: not enough diskspace. Left with %lld K\n"), (long long)size);
2661         fflush(stdout);
2662         free_assignedhd(result);
2663         result = NULL;
2664     }
2665
2666     if (debug_holding > 1) {
2667         for( i = 0; result && result[i]; i++ ) {
2668             hold_debug(1, _("find_diskspace: find diskspace: selected %s free %lld reserved %lld dumpers %d\n"),
2669                            holdingdisk_get_diskdir(result[i]->disk->hdisk),
2670                            (long long)(result[i]->disk->disksize -
2671                              result[i]->disk->allocated_space),
2672                            (long long)result[i]->reserved,
2673                            result[i]->disk->allocated_dumpers);
2674         }
2675     }
2676
2677     return result;
2678 }
2679
2680 static int
2681 assign_holdingdisk(
2682     assignedhd_t **     holdp,
2683     disk_t *            diskp)
2684 {
2685     int i, j, c, l=0;
2686     off_t size;
2687     char *sfn = sanitise_filename(diskp->name);
2688     char lvl[64];
2689     assignedhd_t **new_holdp;
2690     char *qname;
2691
2692     g_snprintf( lvl, SIZEOF(lvl), "%d", sched(diskp)->level );
2693
2694     size = am_round(sched(diskp)->est_size - sched(diskp)->act_size,
2695                     (off_t)DISK_BLOCK_KB);
2696
2697     for( c = 0; holdp[c]; c++ )
2698         (void)c; /* count number of disks */
2699
2700     /* allocate memory for sched(diskp)->holdp */
2701     for(j = 0; sched(diskp)->holdp && sched(diskp)->holdp[j]; j++)
2702         (void)j;        /* Quiet lint */
2703     new_holdp = (assignedhd_t **)alloc(SIZEOF(assignedhd_t*)*(j+c+1));
2704     if (sched(diskp)->holdp) {
2705         memcpy(new_holdp, sched(diskp)->holdp, j * SIZEOF(*new_holdp));
2706         amfree(sched(diskp)->holdp);
2707     }
2708     sched(diskp)->holdp = new_holdp;
2709     new_holdp = NULL;
2710
2711     i = 0;
2712     if( j > 0 ) { /* This is a request for additional diskspace. See if we can
2713                    * merge assignedhd_t's */
2714         l=j;
2715         if( sched(diskp)->holdp[j-1]->disk == holdp[0]->disk ) { /* Yes! */
2716             sched(diskp)->holdp[j-1]->reserved += holdp[0]->reserved;
2717             holdp[0]->disk->allocated_space += holdp[0]->reserved;
2718             size = (holdp[0]->reserved>size) ? (off_t)0 : size-holdp[0]->reserved;
2719             qname = quote_string(diskp->name);
2720             hold_debug(1, _("assign_holdingdisk: merging holding disk %s to disk %s:%s, add %lld for reserved %lld, left %lld\n"),
2721                            holdingdisk_get_diskdir(
2722                                                sched(diskp)->holdp[j-1]->disk->hdisk),
2723                            diskp->host->hostname, qname,
2724                            (long long)holdp[0]->reserved,
2725                            (long long)sched(diskp)->holdp[j-1]->reserved,
2726                            (long long)size);
2727             i++;
2728             amfree(qname);
2729             amfree(holdp[0]);
2730             l=j-1;
2731         }
2732     }
2733
2734     /* copy assignedhd_s to sched(diskp), adjust allocated_space */
2735     for( ; holdp[i]; i++ ) {
2736         holdp[i]->destname = newvstralloc( holdp[i]->destname,
2737                                            holdingdisk_get_diskdir(holdp[i]->disk->hdisk), "/",
2738                                            hd_driver_timestamp, "/",
2739                                            diskp->host->hostname, ".",
2740                                            sfn, ".",
2741                                            lvl, NULL );
2742         sched(diskp)->holdp[j++] = holdp[i];
2743         holdp[i]->disk->allocated_space += holdp[i]->reserved;
2744         size = (holdp[i]->reserved > size) ? (off_t)0 :
2745                   (size - holdp[i]->reserved);
2746         qname = quote_string(diskp->name);
2747         hold_debug(1,
2748                    _("assign_holdingdisk: %d assigning holding disk %s to disk %s:%s, reserved %lld, left %lld\n"),
2749                     i, holdingdisk_get_diskdir(holdp[i]->disk->hdisk),
2750                     diskp->host->hostname, qname,
2751                     (long long)holdp[i]->reserved,
2752                     (long long)size);
2753         amfree(qname);
2754         holdp[i] = NULL; /* so it doesn't get free()d... */
2755     }
2756     sched(diskp)->holdp[j] = NULL;
2757     amfree(sfn);
2758
2759     return l;
2760 }
2761
2762 static void
2763 adjust_diskspace(
2764     disk_t *    diskp,
2765     cmd_t       cmd)
2766 {
2767     assignedhd_t **holdp;
2768     off_t total = (off_t)0;
2769     off_t diff;
2770     int i;
2771     char *qname, *hqname, *qdest;
2772
2773     (void)cmd;  /* Quiet unused parameter warning */
2774
2775     qname = quote_string(diskp->name);
2776     qdest = quote_string(sched(diskp)->destname);
2777     hold_debug(1, _("adjust_diskspace: %s:%s %s\n"),
2778                    diskp->host->hostname, qname, qdest);
2779
2780     holdp = sched(diskp)->holdp;
2781
2782     assert(holdp != NULL);
2783
2784     for( i = 0; holdp[i]; i++ ) { /* for each allocated disk */
2785         diff = holdp[i]->used - holdp[i]->reserved;
2786         total += holdp[i]->used;
2787         holdp[i]->disk->allocated_space += diff;
2788         hqname = quote_string(holdingdisk_name(holdp[i]->disk->hdisk));
2789         hold_debug(1, _("adjust_diskspace: hdisk %s done, reserved %lld used %lld diff %lld alloc %lld dumpers %d\n"),
2790                        holdingdisk_name(holdp[i]->disk->hdisk),
2791                        (long long)holdp[i]->reserved,
2792                        (long long)holdp[i]->used,
2793                        (long long)diff,
2794                        (long long)holdp[i]->disk->allocated_space,
2795                        holdp[i]->disk->allocated_dumpers );
2796         holdp[i]->reserved += diff;
2797         amfree(hqname);
2798     }
2799
2800     sched(diskp)->act_size = total;
2801
2802     hold_debug(1, _("adjust_diskspace: after: disk %s:%s used %lld\n"),
2803                    diskp->host->hostname, qname,
2804                    (long long)sched(diskp)->act_size);
2805     amfree(qdest);
2806     amfree(qname);
2807 }
2808
2809 static void
2810 delete_diskspace(
2811     disk_t *diskp)
2812 {
2813     assignedhd_t **holdp;
2814     int i;
2815
2816     holdp = sched(diskp)->holdp;
2817
2818     assert(holdp != NULL);
2819
2820     for( i = 0; holdp[i]; i++ ) { /* for each disk */
2821         /* find all files of this dump on that disk, and subtract their
2822          * reserved sizes from the disk's allocated space
2823          */
2824         holdp[i]->disk->allocated_space -= holdp[i]->used;
2825     }
2826
2827     holding_file_unlink(holdp[0]->destname);    /* no need for the entire list,
2828                                                  * because holding_file_unlink
2829                                                  * will walk through all files
2830                                                  * using cont_filename */
2831     free_assignedhd(sched(diskp)->holdp);
2832     sched(diskp)->holdp = NULL;
2833     sched(diskp)->act_size = (off_t)0;
2834 }
2835
2836 static assignedhd_t **
2837 build_diskspace(
2838     char *      destname)
2839 {
2840     int i, j;
2841     int fd;
2842     ssize_t buflen;
2843     char buffer[DISK_BLOCK_BYTES];
2844     dumpfile_t file;
2845     assignedhd_t **result;
2846     holdalloc_t *ha;
2847     off_t *used;
2848     char dirname[1000], *ch;
2849     struct stat finfo;
2850     char *filename = destname;
2851
2852     memset(buffer, 0, sizeof(buffer));
2853     used = alloc(SIZEOF(off_t) * num_holdalloc);
2854     for(i=0;i<num_holdalloc;i++)
2855         used[i] = (off_t)0;
2856     result = alloc(SIZEOF(assignedhd_t *) * (num_holdalloc + 1));
2857     result[0] = NULL;
2858     while(filename != NULL && filename[0] != '\0') {
2859         strncpy(dirname, filename, 999);
2860         dirname[999]='\0';
2861         ch = strrchr(dirname,'/');
2862         *ch = '\0';
2863         ch = strrchr(dirname,'/');
2864         *ch = '\0';
2865
2866         for(j = 0, ha = holdalloc; ha != NULL; ha = ha->next, j++ ) {
2867             if(strcmp(dirname, holdingdisk_get_diskdir(ha->hdisk))==0) {
2868                 break;
2869             }
2870         }
2871
2872         if(stat(filename, &finfo) == -1) {
2873             g_fprintf(stderr, _("stat %s: %s\n"), filename, strerror(errno));
2874             finfo.st_size = (off_t)0;
2875         }
2876         used[j] += ((off_t)finfo.st_size+(off_t)1023)/(off_t)1024;
2877         if((fd = open(filename,O_RDONLY)) == -1) {
2878             g_fprintf(stderr,_("build_diskspace: open of %s failed: %s\n"),
2879                     filename, strerror(errno));
2880             return NULL;
2881         }
2882         if ((buflen = fullread(fd, buffer, SIZEOF(buffer))) > 0) {;
2883                 parse_file_header(buffer, &file, (size_t)buflen);
2884         }
2885         close(fd);
2886         filename = file.cont_filename;
2887     }
2888
2889     for(j = 0, i=0, ha = holdalloc; ha != NULL; ha = ha->next, j++ ) {
2890         if(used[j] != (off_t)0) {
2891             result[i] = alloc(SIZEOF(assignedhd_t));
2892             result[i]->disk = ha;
2893             result[i]->reserved = used[j];
2894             result[i]->used = used[j];
2895             result[i]->destname = stralloc(destname);
2896             result[i+1] = NULL;
2897             i++;
2898         }
2899     }
2900
2901     amfree(used);
2902     return result;
2903 }
2904
2905 static void
2906 holdingdisk_state(
2907     char *      time_str)
2908 {
2909     holdalloc_t *ha;
2910     int dsk;
2911     off_t diff;
2912
2913     g_printf(_("driver: hdisk-state time %s"), time_str);
2914
2915     for(ha = holdalloc, dsk = 0; ha != NULL; ha = ha->next, dsk++) {
2916         diff = ha->disksize - ha->allocated_space;
2917         g_printf(_(" hdisk %d: free %lld dumpers %d"), dsk,
2918                (long long)diff, ha->allocated_dumpers);
2919     }
2920     g_printf("\n");
2921 }
2922
2923 static void
2924 update_failed_dump_to_tape(
2925     disk_t *    dp)
2926 {
2927 /* JLM
2928  * should simply set no_bump
2929  */
2930
2931     time_t save_timestamp = sched(dp)->timestamp;
2932     /* setting timestamp to 0 removes the current level from the
2933      * database, so that we ensure that it will not be bumped to the
2934      * next level on the next run.  If we didn't do this, dumpdates or
2935      * gnutar-lists might have been updated already, and a bumped
2936      * incremental might be created.  */
2937     sched(dp)->timestamp = 0;
2938     update_info_dumper(dp, (off_t)-1, (off_t)-1, (time_t)-1);
2939     sched(dp)->timestamp = save_timestamp;
2940 }
2941
2942 /* ------------------- */
2943 static void
2944 dump_to_tape(
2945     disk_t *    dp)
2946 {
2947     dumper_t *dumper;
2948     cmd_t cmd;
2949     int result_argc;
2950     char *result_argv[MAX_ARGS+1];
2951     char *qname;
2952
2953     qname = quote_string(dp->name);
2954     g_printf(_("driver: dumping %s:%s directly to tape\n"),
2955            dp->host->hostname, qname);
2956     fflush(stdout);
2957
2958     /* pick a dumper and fail if there are no idle dumpers */
2959
2960     dumper = idle_dumper();
2961     if (!dumper) {
2962         g_printf(_("driver: no idle dumpers for %s:%s.\n"), 
2963                 dp->host->hostname, qname);
2964         fflush(stdout);
2965         log_add(L_WARNING, _("no idle dumpers for %s:%s.\n"),
2966                 dp->host->hostname, qname);
2967         amfree(qname);
2968         return; /* fatal problem */
2969     }
2970
2971     /* tell the taper to read from a port number of its choice */
2972
2973     taper_cmd(PORT_WRITE, dp, NULL, sched(dp)->level, sched(dp)->datestamp);
2974     cmd = getresult(taper, 1, &result_argc, result_argv, MAX_ARGS+1);
2975     if(cmd != PORT) {
2976         g_printf(_("driver: did not get PORT from taper for %s:%s\n"),
2977                 dp->host->hostname, qname);
2978         fflush(stdout);
2979         log_add(L_WARNING, _("driver: did not get PORT from taper for %s:%s.\n"),
2980                 dp->host->hostname, qname);
2981         amfree(qname);
2982         return; /* fatal problem */
2983     }
2984     amfree(qname);
2985
2986     /* copy port number */
2987     dumper->output_port = atoi(result_argv[2]);
2988
2989     dumper->dp = dp;
2990     dumper->chunker = NULL;
2991     dumper->result = LAST_TOK;
2992     taper_result = LAST_TOK;
2993     sched(dp)->dumper = dumper;
2994
2995     /* tell the dumper to dump to a port */
2996     dumper_cmd(dumper, PORT_DUMP, dp);
2997     dp->host->start_t = time(NULL) + 15;
2998
2999     /* update statistics & print state */
3000
3001     taper_busy = dumper->busy = 1;
3002     taper_input_error = NULL;
3003     taper_tape_error = NULL;
3004     taper_dumper = dumper;
3005     taper_disk = dp;
3006     taper_input_error = NULL;
3007     taper_tape_error = NULL;
3008     taper_first_label = NULL;
3009     taper_written = 0;
3010     taper_state |= TAPER_STATE_DUMP_TO_TAPE;
3011     sched(dp)->act_size = sched(dp)->est_size;
3012     dp->host->inprogress += 1;
3013     dp->inprogress = 1;
3014     sched(dp)->timestamp = time((time_t *)0);
3015     allocate_bandwidth(dp->host->netif, sched(dp)->est_kps);
3016     idle_reason = NOT_IDLE;
3017
3018     short_dump_state();
3019
3020     dumper->ev_read = event_register(dumper->fd, EV_READFD,
3021                                      handle_dumper_result, dumper);
3022     taper_ev_read = event_register(taper, EV_READFD,
3023                                    handle_taper_result, NULL);
3024 }
3025
3026 static int
3027 queue_length(
3028     disklist_t  q)
3029 {
3030     disk_t *p;
3031     int len;
3032
3033     for(len = 0, p = q.head; p != NULL; len++, p = p->next)
3034         (void)len;      /* Quiet lint */
3035     return len;
3036 }
3037
3038 static void
3039 short_dump_state(void)
3040 {
3041     int i, nidle;
3042     char *wall_time;
3043
3044     wall_time = walltime_str(curclock());
3045
3046     g_printf(_("driver: state time %s "), wall_time);
3047     g_printf(_("free kps: %lu space: %lld taper: "),
3048            free_kps(NULL),
3049            (long long)free_space());
3050     if(degraded_mode) g_printf(_("DOWN"));
3051     else if(!taper_busy) g_printf(_("idle"));
3052     else g_printf(_("writing"));
3053     nidle = 0;
3054     for(i = 0; i < inparallel; i++) if(!dmptable[i].busy) nidle++;
3055     g_printf(_(" idle-dumpers: %d"), nidle);
3056     g_printf(_(" qlen tapeq: %d"), queue_length(tapeq));
3057     g_printf(_(" runq: %d"), queue_length(runq));
3058     g_printf(_(" roomq: %d"), queue_length(roomq));
3059     g_printf(_(" wakeup: %d"), (int)sleep_time);
3060     g_printf(_(" driver-idle: %s\n"), _(idle_strings[idle_reason]));
3061     interface_state(wall_time);
3062     holdingdisk_state(wall_time);
3063     fflush(stdout);
3064 }
3065
3066 static TapeAction tape_action(void)
3067 {
3068     TapeAction result = TAPE_ACTION_NO_ACTION;
3069     dumper_t *dumper;
3070     disk_t   *dp;
3071     off_t dumpers_size;
3072     off_t runq_size;
3073     off_t directq_size;
3074     off_t tapeq_size;
3075     off_t sched_size;
3076     off_t dump_to_disk_size;
3077     int   dump_to_disk_terminated;
3078
3079     dumpers_size = 0;
3080     for(dumper = dmptable; dumper < (dmptable+inparallel); dumper++) {
3081         if (dumper->busy)
3082             dumpers_size += sched(dumper->dp)->est_size;
3083     }
3084     driver_debug(1, _("dumpers_size: %lld\n"), (long long)dumpers_size);
3085
3086     runq_size = 0;
3087     for(dp = runq.head; dp != NULL; dp = dp->next) {
3088         runq_size += sched(dp)->est_size;
3089     }
3090     driver_debug(1, _("runq_size: %lld\n"), (long long)runq_size);
3091
3092     directq_size = 0;
3093     for(dp = directq.head; dp != NULL; dp = dp->next) {
3094         directq_size += sched(dp)->est_size;
3095     }
3096     driver_debug(1, _("directq_size: %lld\n"), (long long)directq_size);
3097
3098     tapeq_size = 0;
3099     for(dp = tapeq.head; dp != NULL; dp = dp->next) {
3100         tapeq_size += sched(dp)->act_size;
3101     }
3102     if (taper_disk) {
3103         tapeq_size += sched(taper_disk)->act_size - taper_written;
3104     }
3105     driver_debug(1, _("tapeq_size: %lld\n"), (long long)tapeq_size);
3106
3107     sched_size = runq_size + tapeq_size + dumpers_size;
3108     driver_debug(1, _("sched_size: %lld\n"), (long long)sched_size);
3109
3110     dump_to_disk_size = dumpers_size + runq_size;
3111     driver_debug(1, _("dump_to_disk_size: %lld\n"), (long long)dump_to_disk_size);
3112
3113     dump_to_disk_terminated = schedule_done && dump_to_disk_size == 0;
3114
3115     // Changing conditionals can produce a driver hang, take care.
3116     // 
3117     // when to start writting to a new tape
3118     if ((taper_state & TAPER_STATE_WAIT_FOR_TAPE) &&
3119         ((taper_state & TAPER_STATE_DUMP_TO_TAPE) ||    // for dump to tape
3120          !empty(directq) ||                             // if a dle is waiting for a dump to tape
3121          !empty(roomq) ||                               // holding disk constraint
3122          idle_reason == IDLE_NO_DISKSPACE ||            // holding disk constraint
3123          (flush_threshold_dumped < tapeq_size &&        // flush-threshold-dumped &&
3124           flush_threshold_scheduled < sched_size) ||    //  flush-threshold-scheduled
3125          (taperflush < tapeq_size &&                    // taperflush
3126           (force_flush == 1 ||                          //  if force_flush
3127            dump_to_disk_terminated))                    //  or all dump to disk terminated
3128         )) {
3129         result |= TAPE_ACTION_NEW_TAPE;
3130     // when to stop using new tape
3131     } else if ((taper_state & TAPER_STATE_WAIT_FOR_TAPE) &&
3132                (taperflush >= tapeq_size &&             // taperflush criteria not meet
3133                 (force_flush == 1 ||                    //  if force_flush
3134                  dump_to_disk_terminated))              //  or all dump to disk terminated
3135               ) {
3136         result |= TAPE_ACTION_NO_NEW_TAPE;
3137     }
3138
3139     // when to start a flush
3140     // We don't start a flush if taper_tape_started == 1 && dump_to_disk_terminated && force_flush == 0,
3141     // it is a criteria need to exit the first event_loop without flushing everything to tape,
3142     // they will be flush in another event_loop.
3143     if (!degraded_mode && !taper_busy && !empty(tapeq) &&
3144         (!((taper_state & TAPER_STATE_TAPE_STARTED) &&
3145             dump_to_disk_terminated && force_flush == 0) ||     // if tape already started and dump to disk not terminated
3146          ((taper_state & TAPER_STATE_TAPE_STARTED) &&
3147           force_flush == 1) ||                                  // if tape already started and force_flush
3148          !empty(roomq) ||                                       // holding disk constraint
3149          idle_reason == IDLE_NO_DISKSPACE ||                    // holding disk constraint
3150          (flush_threshold_dumped < tapeq_size &&                // flush-threshold-dumped &&
3151          flush_threshold_scheduled < sched_size) ||             //  flush-threshold-scheduled
3152          (force_flush == 1 && taperflush < tapeq_size))) {      // taperflush if force_flush
3153         result |= TAPE_ACTION_START_A_FLUSH;
3154     }
3155     return result;
3156 }
3157
3158 #if 0
3159 static void
3160 dump_state(
3161     const char *str)
3162 {
3163     int i;
3164     disk_t *dp;
3165     char *qname;
3166
3167     g_printf("================\n");
3168     g_printf(_("driver state at time %s: %s\n"), walltime_str(curclock()), str);
3169     g_printf(_("free kps: %lu, space: %lld\n"),
3170            free_kps(NULL),
3171            (long long)free_space());
3172     if(degraded_mode) g_printf(_("taper: DOWN\n"));
3173     else if(!taper_busy) g_printf(_("taper: idle\n"));
3174     else g_printf(_("taper: writing %s:%s.%d est size %lld\n"),
3175                 taper_disk->host->hostname, taper_disk->name,
3176                 sched(taper_disk)->level,
3177                 (long long)sched(taper_disk)->est_size);
3178     for(i = 0; i < inparallel; i++) {
3179         dp = dmptable[i].dp;
3180         if(!dmptable[i].busy)
3181           g_printf(_("%s: idle\n"), dmptable[i].name);
3182         else
3183           qname = quote_string(dp->name);
3184           g_printf(_("%s: dumping %s:%s.%d est kps %d size %lld time %lu\n"),
3185                 dmptable[i].name, dp->host->hostname, qname, sched(dp)->level,
3186                 sched(dp)->est_kps, (long long)sched(dp)->est_size, sched(dp)->est_time);
3187           amfree(qname);
3188     }
3189     dump_queue("TAPE", tapeq, 5, stdout);
3190     dump_queue("ROOM", roomq, 5, stdout);
3191     dump_queue("RUN ", runq, 5, stdout);
3192     g_printf("================\n");
3193     fflush(stdout);
3194 }
3195 #endif