Imported Upstream version 2.5.0
[debian/amanda] / server-src / planner.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1999 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: planner.c,v 1.180 2006/03/10 13:51:06 martinea Exp $
28  *
29  * backup schedule planner for the Amanda backup system.
30  */
31 #include "amanda.h"
32 #include "arglist.h"
33 #include "conffile.h"
34 #include "diskfile.h"
35 #include "tapefile.h"
36 #include "infofile.h"
37 #include "logfile.h"
38 #include "clock.h"
39 #include "packet.h"
40 #include "security.h"
41 #include "protocol.h"
42 #include "version.h"
43 #include "amfeatures.h"
44 #include "server_util.h"
45 #include "holding.h"
46
47 #define MAX_LEVELS                  3   /* max# of estimates per filesys */
48
49 #define RUNS_REDZONE                5   /* should be in conf file? */
50
51 #define PROMOTE_THRESHOLD        0.05   /* if <5% unbalanced, don't promote */
52 #define DEFAULT_DUMPRATE         1024.0 /* K/s */
53
54 /* configuration file stuff */
55
56 char *conf_tapetype;
57 am64_t conf_maxdumpsize;
58 int conf_runtapes;
59 int conf_dumpcycle;
60 int conf_runspercycle;
61 int conf_tapecycle;
62 int conf_etimeout;
63 int conf_reserve;
64 int conf_autoflush;
65
66 #define HOST_READY                              ((void *)0)     /* must be 0 */
67 #define HOST_ACTIVE                             ((void *)1)
68 #define HOST_DONE                               ((void *)2)
69
70 #define DISK_READY                              0               /* must be 0 */
71 #define DISK_ACTIVE                             1
72 #define DISK_PARTIALY_DONE                      2
73 #define DISK_DONE                               3
74
75 typedef struct est_s {
76     int state;
77     int got_estimate;
78     int dump_priority;
79     int dump_level;
80     long dump_size;
81     int degr_level;     /* if dump_level == 0, what would be the inc level */
82     long degr_size;
83     int last_level;
84     long last_lev0size;
85     int next_level0;
86     int level_days;
87     int promote;
88     double fullrate, incrrate;
89     double fullcomp, incrcomp;
90     char *errstr;
91     int level[MAX_LEVELS];
92     char *dumpdate[MAX_LEVELS];
93     long est_size[MAX_LEVELS];
94 } est_t;
95
96 #define est(dp) ((est_t *)(dp)->up)
97
98 /* pestq = partial estimate */
99 disklist_t startq, waitq, pestq, estq, failq, schedq;
100 am64_t total_size;
101 double total_lev0, balanced_size, balance_threshold;
102 am64_t tape_length, tape_mark;
103
104 tapetype_t *tape;
105 long tt_blocksize;
106 long tt_blocksize_kb;
107 int runs_per_cycle = 0;
108 time_t today;
109 char *datestamp = NULL;
110
111 static am_feature_t *our_features = NULL;
112 static char *our_feature_string = NULL;
113
114 /* We keep a LIFO queue of before images for all modifications made
115  * to schedq in our attempt to make the schedule fit on the tape.
116  * Enough information is stored to reinstate a dump if it turns out
117  * that it shouldn't have been touched after all.
118  */
119 typedef struct bi_s {
120     struct bi_s *next;
121     struct bi_s *prev;
122     int deleted;                /* 0=modified, 1=deleted */
123     disk_t *dp;                 /* The disk that was changed */
124     int level;                  /* The original level */
125     long size;                  /* The original size */
126     char *errstr;               /* A message describing why this disk is here */
127 } bi_t;
128
129 typedef struct bilist_s {
130     bi_t *head, *tail;
131 } bilist_t;
132
133 bilist_t biq;                   /* The BI queue itself */
134
135 /*
136  * ========================================================================
137  * MAIN PROGRAM
138  *
139  */
140
141 static void setup_estimate P((disk_t *dp));
142 static void get_estimates P((void));
143 static void analyze_estimate P((disk_t *dp));
144 static void handle_failed P((disk_t *dp));
145 static void delay_dumps P((void));
146 static int promote_highest_priority_incremental P((void));
147 static int promote_hills P((void));
148 static void output_scheduleline P((disk_t *dp));
149 int main P((int, char **));
150
151 int main(argc, argv)
152 int argc;
153 char **argv;
154 {
155     disklist_t origq;
156     disk_t *dp;
157     int moved_one;
158     unsigned long malloc_hist_1, malloc_size_1;
159     unsigned long malloc_hist_2, malloc_size_2;
160     long initial_size;
161     int i;
162     char *conffile;
163     char *conf_diskfile;
164     char *conf_tapelist;
165     char *conf_infofile;
166     times_t section_start;
167
168     safe_fd(-1, 0);
169
170     setvbuf(stderr, (char *)NULL, _IOLBF, 0);
171
172     if (argc > 1) {
173         config_name = stralloc(argv[1]);
174         config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
175     } else {
176         char my_cwd[STR_SIZE];
177
178         if (getcwd(my_cwd, sizeof(my_cwd)) == NULL) {
179             error("cannot determine current working directory");
180         }
181         config_dir = stralloc2(my_cwd, "/");
182         if ((config_name = strrchr(my_cwd, '/')) != NULL) {
183             config_name = stralloc(config_name + 1);
184         }
185     }
186
187     safe_cd();
188
189     set_pname("planner");
190
191     /* Don't die when child closes pipe */
192     signal(SIGPIPE, SIG_IGN);
193
194     malloc_size_1 = malloc_inuse(&malloc_hist_1);
195
196     erroutput_type = (ERR_AMANDALOG|ERR_INTERACTIVE);
197     set_logerror(logerror);
198     startclock();
199     section_start = curclock();
200
201     our_features = am_init_feature_set();
202     our_feature_string = am_feature_to_string(our_features);
203
204     fprintf(stderr, "%s: pid %ld executable %s version %s\n",
205             get_pname(), (long) getpid(), argv[0], version());
206     for (i = 0; version_info[i] != NULL; i++)
207         fprintf(stderr, "%s: %s", get_pname(), version_info[i]);
208
209     /*
210      * 1. Networking Setup
211      *
212      * Planner runs setuid to get a priviledged socket for BSD security.
213      * We get the socket right away as root, then setuid back to a normal
214      * user.  If we are not using BSD security, planner is not installed
215      * setuid root.
216      */
217
218     protocol_init();
219
220     if(geteuid() == 0) {
221         uid_t ruid = getuid();
222         setuid(0);
223         seteuid(ruid);
224         setgid(getgid());
225     }
226
227     /*
228      * From this point on we are running under our real uid, so we don't
229      * have to worry about opening security holes below.  Make sure we
230      * are a valid user.
231      */
232
233     if(getpwuid(getuid()) == NULL)
234         error("can't get login name for my uid %ld", (long)getuid());
235
236     /*
237      * 2. Read in Configuration Information
238      *
239      * All the Amanda configuration files are loaded before we begin.
240      */
241
242     fprintf(stderr,"READING CONF FILES...\n");
243
244     conffile = stralloc2(config_dir, CONFFILE_NAME);
245     if(read_conffile(conffile)) {
246         error("errors processing config file \"%s\"", conffile);
247     }
248     amfree(conffile);
249
250     conf_diskfile = getconf_str(CNF_DISKFILE);
251     if (*conf_diskfile == '/') {
252         conf_diskfile = stralloc(conf_diskfile);
253     } else {
254         conf_diskfile = stralloc2(config_dir, conf_diskfile);
255     }
256     if (read_diskfile(conf_diskfile, &origq) < 0) {
257         error("could not load disklist \"%s\"", conf_diskfile);
258     }
259     match_disklist(&origq, argc-2, argv+2);
260     for(dp = origq.head; dp != NULL; dp = dp->next) {
261         if(dp->todo)
262             log_add(L_DISK, "%s %s", dp->host->hostname, dp->name);
263     }
264     amfree(conf_diskfile);
265
266     conf_tapelist = getconf_str(CNF_TAPELIST);
267     if (*conf_tapelist == '/') {
268         conf_tapelist = stralloc(conf_tapelist);
269     } else {
270         conf_tapelist = stralloc2(config_dir, conf_tapelist);
271     }
272     if(read_tapelist(conf_tapelist)) {
273         error("could not load tapelist \"%s\"", conf_tapelist);
274     }
275     amfree(conf_tapelist);
276
277     conf_infofile = getconf_str(CNF_INFOFILE);
278     if (*conf_infofile == '/') {
279         conf_infofile = stralloc(conf_infofile);
280     } else {
281         conf_infofile = stralloc2(config_dir, conf_infofile);
282     }
283     if(open_infofile(conf_infofile)) {
284         error("could not open info db \"%s\"", conf_infofile);
285     }
286     amfree(conf_infofile);
287
288     conf_tapetype = getconf_str(CNF_TAPETYPE);
289     conf_maxdumpsize = getconf_am64(CNF_MAXDUMPSIZE);
290     conf_runtapes = getconf_int(CNF_RUNTAPES);
291     conf_dumpcycle = getconf_int(CNF_DUMPCYCLE);
292     conf_runspercycle = getconf_int(CNF_RUNSPERCYCLE);
293     conf_tapecycle = getconf_int(CNF_TAPECYCLE);
294     conf_etimeout = getconf_int(CNF_ETIMEOUT);
295     conf_reserve  = getconf_int(CNF_RESERVE);
296     conf_autoflush = getconf_int(CNF_AUTOFLUSH);
297
298     amfree(datestamp);
299     today = time(0);
300     datestamp = construct_datestamp(NULL);
301     log_add(L_START, "date %s", datestamp);
302
303     /* some initializations */
304
305     if(conf_runspercycle == 0) {
306         runs_per_cycle = conf_dumpcycle;
307     } else if(conf_runspercycle == -1 ) {
308         runs_per_cycle = guess_runs_from_tapelist();
309     } else
310         runs_per_cycle = conf_runspercycle;
311
312     if (runs_per_cycle <= 0) {
313         runs_per_cycle = 1;
314     }
315
316     /*
317      * do some basic sanity checking
318      */
319      if(conf_tapecycle <= runs_per_cycle) {
320         log_add(L_WARNING, "tapecycle (%d) <= runspercycle (%d)",
321                 conf_tapecycle, runs_per_cycle);
322      }
323     
324     tape = lookup_tapetype(conf_tapetype);
325     if(conf_maxdumpsize > 0) {
326         tape_length = conf_maxdumpsize;
327     }
328     else {
329         tape_length = tape->length * conf_runtapes;
330     }
331     tape_mark   = tape->filemark;
332     tt_blocksize_kb = tape->blocksize;
333     tt_blocksize = tt_blocksize_kb * 1024;
334
335     fprintf(stderr, "%s: time %s: startup took %s secs\n",
336                     get_pname(),
337                     walltime_str(curclock()),
338                     walltime_str(timessub(curclock(), section_start)));
339
340     /*
341      * 3. Send autoflush dumps left on the holding disks
342      *
343      * This should give us something to do while we generate the new
344      * dump schedule.
345      */
346
347     fprintf(stderr,"\nSENDING FLUSHES...\n");
348
349     if(conf_autoflush) {
350         dumpfile_t file;
351         sl_t *holding_list;
352         sle_t *holding_file;
353         holding_list = get_flush(NULL, NULL, 0, 0);
354         for(holding_file=holding_list->first; holding_file != NULL;
355                                        holding_file = holding_file->next) {
356             get_dumpfile(holding_file->name, &file);
357             
358             log_add(L_DISK, "%s %s", file.name, file.disk);
359             fprintf(stderr,
360                     "FLUSH %s %s %s %d %s\n",
361                     file.name,
362                     file.disk,
363                     file.datestamp,
364                     file.dumplevel,
365                     holding_file->name);
366             fprintf(stdout,
367                     "FLUSH %s %s %s %d %s\n",
368                     file.name,
369                     file.disk,
370                     file.datestamp,
371                     file.dumplevel,
372                     holding_file->name);
373         }
374         free_sl(holding_list);
375         holding_list = NULL;
376     }
377     fprintf(stderr, "ENDFLUSH\n");
378     fprintf(stdout, "ENDFLUSH\n");
379     fflush(stdout);
380
381     /*
382      * 4. Calculate Preliminary Dump Levels
383      *
384      * Before we can get estimates from the remote slave hosts, we make a
385      * first attempt at guessing what dump levels we will be dumping at
386      * based on the curinfo database.
387      */
388
389     fprintf(stderr,"\nSETTING UP FOR ESTIMATES...\n");
390     section_start = curclock();
391
392     startq.head = startq.tail = NULL;
393     while(!empty(origq)) {
394         disk_t *dp = dequeue_disk(&origq);
395         if(dp->todo == 1) {
396             setup_estimate(dp);
397         }
398     }
399
400     fprintf(stderr, "%s: time %s: setting up estimates took %s secs\n",
401                     get_pname(),
402                     walltime_str(curclock()),
403                     walltime_str(timessub(curclock(), section_start)));
404
405
406     /*
407      * 5. Get Dump Size Estimates from Remote Client Hosts
408      *
409      * Each host is queried (in parallel) for dump size information on all
410      * of its disks, and the results gathered as they come in.
411      */
412
413     /* go out and get the dump estimates */
414
415     fprintf(stderr,"\nGETTING ESTIMATES...\n");
416     section_start = curclock();
417
418     estq.head = estq.tail = NULL;
419     pestq.head = pestq.tail = NULL;
420     waitq.head = waitq.tail = NULL;
421     failq.head = failq.tail = NULL;
422
423     get_estimates();
424
425     fprintf(stderr, "%s: time %s: getting estimates took %s secs\n",
426                     get_pname(),
427                     walltime_str(curclock()),
428                     walltime_str(timessub(curclock(), section_start)));
429
430     /*
431      * At this point, all disks with estimates are in estq, and
432      * all the disks on hosts that didn't respond to our inquiry
433      * are in failq.
434      */
435
436     dump_queue("FAILED", failq, 15, stderr);
437     dump_queue("DONE", estq, 15, stderr);
438
439
440     /*
441      * 6. Analyze Dump Estimates
442      *
443      * Each disk's estimates are looked at to determine what level it
444      * should dump at, and to calculate the expected size and time taking
445      * historical dump rates and compression ratios into account.  The
446      * total expected size is accumulated as well.
447      */
448
449     fprintf(stderr,"\nANALYZING ESTIMATES...\n");
450     section_start = curclock();
451
452                         /* an empty tape still has a label and an endmark */
453     total_size = (tt_blocksize_kb + tape_mark) * 2;
454     total_lev0 = 0.0;
455     balanced_size = 0.0;
456
457     schedq.head = schedq.tail = NULL;
458     while(!empty(estq)) analyze_estimate(dequeue_disk(&estq));
459     while(!empty(failq)) handle_failed(dequeue_disk(&failq));
460
461     /*
462      * At this point, all the disks are on schedq sorted by priority.
463      * The total estimated size of the backups is in total_size.
464      */
465
466     {
467         disk_t *dp;
468
469         fprintf(stderr, "INITIAL SCHEDULE (size " AM64_FMT "):\n", total_size);
470         for(dp = schedq.head; dp != NULL; dp = dp->next) {
471             fprintf(stderr, "  %s %s pri %d lev %d size %ld\n",
472                     dp->host->hostname, dp->name, est(dp)->dump_priority,
473                     est(dp)->dump_level, est(dp)->dump_size);
474         }
475     }
476
477
478     /*
479      * 7. Delay Dumps if Schedule Too Big
480      *
481      * If the generated schedule is too big to fit on the tape, we need to
482      * delay some full dumps to make room.  Incrementals will be done
483      * instead (except for new or forced disks).
484      *
485      * In extreme cases, delaying all the full dumps is not even enough.
486      * If so, some low-priority incrementals will be skipped completely
487      * until the dumps fit on the tape.
488      */
489
490     fprintf(stderr,
491       "\nDELAYING DUMPS IF NEEDED, total_size " AM64_FMT ", tape length " AM64_FMT " mark " AM64_FMT "\n",
492             total_size, tape_length, tape_mark);
493
494     initial_size = total_size;
495
496     delay_dumps();
497
498     /* XXX - why bother checking this? */
499     if(empty(schedq) && total_size < initial_size)
500         error("cannot fit anything on tape, bailing out");
501
502
503     /*
504      * 8. Promote Dumps if Schedule Too Small
505      *
506      * Amanda attempts to balance the full dumps over the length of the
507      * dump cycle.  If this night's full dumps are too small relative to
508      * the other nights, promote some high-priority full dumps that will be
509      * due for the next run, to full dumps for tonight, taking care not to
510      * overflow the tape size.
511      *
512      * This doesn't work too well for small sites.  For these we scan ahead
513      * looking for nights that have an excessive number of dumps and promote
514      * one of them.
515      *
516      * Amanda never delays full dumps just for the sake of balancing the
517      * schedule, so it can take a full cycle to balance the schedule after
518      * a big bump.
519      */
520
521     fprintf(stderr,
522      "\nPROMOTING DUMPS IF NEEDED, total_lev0 %1.0f, balanced_size %1.0f...\n",
523             total_lev0, balanced_size);
524
525     balance_threshold = balanced_size * PROMOTE_THRESHOLD;
526     moved_one = 1;
527     while((balanced_size - total_lev0) > balance_threshold && moved_one)
528         moved_one = promote_highest_priority_incremental();
529
530     moved_one = promote_hills();
531
532     fprintf(stderr, "%s: time %s: analysis took %s secs\n",
533                     get_pname(),
534                     walltime_str(curclock()),
535                     walltime_str(timessub(curclock(), section_start)));
536
537
538     /*
539      * 9. Output Schedule
540      *
541      * The schedule goes to stdout, presumably to driver.  A copy is written
542      * on stderr for the debug file.
543      */
544
545     fprintf(stderr,"\nGENERATING SCHEDULE:\n--------\n");
546
547     while(!empty(schedq)) output_scheduleline(dequeue_disk(&schedq));
548     fprintf(stderr, "--------\n");
549
550     close_infofile();
551     log_add(L_FINISH, "date %s time %s", datestamp, walltime_str(curclock()));
552
553     amfree(datestamp);
554     amfree(config_dir);
555     amfree(config_name);
556     amfree(our_feature_string);
557     am_release_feature_set(our_features);
558     our_features = NULL;
559
560     malloc_size_2 = malloc_inuse(&malloc_hist_2);
561
562     if(malloc_size_1 != malloc_size_2) {
563         malloc_list(fileno(stderr), malloc_hist_1, malloc_hist_2);
564     }
565
566     return 0;
567 }
568
569
570 \f
571 /*
572  * ========================================================================
573  * SETUP FOR ESTIMATES
574  *
575  */
576
577 static void askfor P((est_t *, int, int, info_t *));
578 static int last_level P((info_t *info));                  /* subroutines */
579 static long est_size P((disk_t *dp, int level));
580 static long est_tape_size P((disk_t *dp, int level));
581 static int next_level0 P((disk_t *dp, info_t *info));
582 static int runs_at P((info_t *info, int lev));
583 static long bump_thresh P((int level, long size_level_0, int bumppercent, int bumpsize, double bumpmult));
584 static int when_overwrite P((char *label));
585
586 static void askfor(ep, seq, lev, info)
587 est_t *ep;      /* esimate data block */
588 int seq;        /* sequence number of request */
589 int lev;        /* dump level being requested */
590 info_t *info;   /* info block for disk */
591 {
592     if(seq < 0 || seq >= MAX_LEVELS) {
593         error("error [planner askfor: seq out of range 0..%d: %d]",
594               MAX_LEVELS, seq);
595     }
596     if(lev < -1 || lev >= DUMP_LEVELS) {
597         error("error [planner askfor: lev out of range -1..%d: %d]",
598               DUMP_LEVELS, lev);
599     }
600
601     if (lev == -1) {
602         ep->level[seq] = -1;
603         ep->dumpdate[seq] = (char *)0;
604         ep->est_size[seq] = -2;
605         return;
606     }
607
608     ep->level[seq] = lev;
609
610     ep->dumpdate[seq] = stralloc(get_dumpdate(info,lev));
611     malloc_mark(ep->dumpdate[seq]);
612
613     ep->est_size[seq] = -2;
614
615     return;
616 }
617
618 static void
619 setup_estimate(dp)
620      disk_t *dp;
621 {
622     est_t *ep;
623     info_t info;
624     int i;
625
626     assert(dp && dp->host);
627     fprintf(stderr, "%s: time %s: setting up estimates for %s:%s\n",
628                     get_pname(), walltime_str(curclock()),
629                     dp->host->hostname, dp->name);
630
631     /* get current information about disk */
632
633     if(get_info(dp->host->hostname, dp->name, &info)) {
634         /* no record for this disk, make a note of it */
635         log_add(L_INFO, "Adding new disk %s:%s.", dp->host->hostname, dp->name);
636     }
637
638     /* setup working data struct for disk */
639
640     ep = alloc(sizeof(est_t));
641     malloc_mark(ep);
642     dp->up = (void *) ep;
643     ep->state = DISK_READY;
644     ep->dump_size = -1;
645     ep->dump_priority = dp->priority;
646     ep->errstr = 0;
647     ep->promote = 0;
648
649     /* calculated fields */
650
651     if (ISSET(info.command, FORCE_FULL)) {
652         /* force a level 0, kind of like a new disk */
653         if(dp->strategy == DS_NOFULL) {
654             /*
655              * XXX - Not sure what it means to force a no-full disk.  The
656              * purpose of no-full is to just dump changes relative to a
657              * stable base, for example root partitions that vary only
658              * slightly from a site-wide prototype.  Only the variations
659              * are dumped.
660              *
661              * If we allow a level 0 onto the Amanda cycle, then we are
662              * hosed when that tape gets re-used next.  Disallow this for
663              * now.
664              */
665             log_add(L_ERROR,
666                     "Cannot force full dump of %s:%s with no-full option.",
667                     dp->host->hostname, dp->name);
668
669             /* clear force command */
670             CLR(info.command, FORCE_FULL);
671             if(put_info(dp->host->hostname, dp->name, &info))
672                 error("could not put info record for %s:%s: %s",
673                       dp->host->hostname, dp->name, strerror(errno));
674             ep->last_level = last_level(&info);
675             ep->next_level0 = next_level0(dp, &info);
676         }
677         else {
678             ep->last_level = -1;
679             ep->next_level0 = -conf_dumpcycle;
680             log_add(L_INFO, "Forcing full dump of %s:%s as directed.",
681                     dp->host->hostname, dp->name);
682         }
683     }
684     else if(dp->strategy == DS_NOFULL) {
685         /* force estimate of level 1 */
686         ep->last_level = 1;
687         ep->next_level0 = next_level0(dp, &info);
688     }
689     else {
690         ep->last_level = last_level(&info);
691         ep->next_level0 = next_level0(dp, &info);
692     }
693
694     /* adjust priority levels */
695
696     if(ep->next_level0 < 0) {
697         fprintf(stderr,"%s:%s overdue %d day%s for level 0\n",
698                 dp->host->hostname, dp->name,
699                 - ep->next_level0, ((- ep->next_level0) == 1) ? "" : "s");
700         ep->dump_priority -= ep->next_level0;
701         /* warn if dump will be overwritten */
702         if(ep->last_level > -1) {
703             int overwrite_runs = when_overwrite(info.inf[0].label);
704             if(overwrite_runs == 0) {
705                 log_add(L_WARNING,
706                   "Last full dump of %s:%s on tape %s overwritten on this run.",
707                         dp->host->hostname, dp->name, info.inf[0].label);
708             }
709             else if(overwrite_runs < RUNS_REDZONE) {
710                 log_add(L_WARNING,
711                   "Last full dump of %s:%s on tape %s overwritten in %d run%s.",
712                         dp->host->hostname, dp->name, info.inf[0].label,
713                     overwrite_runs, overwrite_runs == 1? "" : "s");
714             }
715         }
716     }
717     else if (ISSET(info.command, FORCE_FULL))
718         ep->dump_priority += 1;
719     /* else XXX bump up the priority of incrementals that failed last night */
720
721     /* handle external level 0 dumps */
722
723     if(dp->skip_full && dp->strategy != DS_NOINC) {
724         if(ep->next_level0 <= 0) {
725             /* update the date field */
726             info.inf[0].date = today;
727             CLR(info.command, FORCE_FULL);
728             ep->next_level0 += conf_dumpcycle;
729             ep->last_level = 0;
730             if(put_info(dp->host->hostname, dp->name, &info))
731                 error("could not put info record for %s:%s: %s",
732                       dp->host->hostname, dp->name, strerror(errno));
733             log_add(L_INFO, "Skipping full dump of %s:%s today.",
734                     dp->host->hostname, dp->name);
735             fprintf(stderr,"%s:%s lev 0 skipped due to skip-full flag\n",
736                     dp->host->hostname, dp->name);
737             /* don't enqueue the disk */
738             askfor(ep, 0, -1, &info);
739             askfor(ep, 1, -1, &info);
740             askfor(ep, 2, -1, &info);
741             fprintf(stderr, "%s: SKIPPED %s %s 0 [skip-full]\n",
742                     get_pname(), dp->host->hostname, dp->name);
743             log_add(L_SUCCESS, "%s %s %s 0 [skipped: skip-full]",
744                     dp->host->hostname, dp->name, datestamp);
745             return;
746         }
747
748         if(ep->last_level == -1) {
749             /* probably a new disk, but skip-full means no full! */
750             ep->last_level = 0;
751         }
752
753         if(ep->next_level0 == 1) {
754             log_add(L_WARNING, "Skipping full dump of %s:%s tomorrow.",
755                     dp->host->hostname, dp->name);
756         }
757     }
758
759     if(dp->strategy == DS_INCRONLY && ep->last_level == -1 && !ISSET(info.command, FORCE_FULL)) {
760         /* don't enqueue the disk */
761         askfor(ep, 0, -1, &info);
762         askfor(ep, 1, -1, &info);
763         askfor(ep, 2, -1, &info);
764         log_add(L_FAIL, "%s %s 19000101 1 [Skipping incronly because no full dump were done]",
765                 dp->host->hostname, dp->name);
766         fprintf(stderr,"%s:%s lev 1 skipped due to strategy incronly and no full dump were done\n",
767                 dp->host->hostname, dp->name);
768         return;
769     }
770
771     /* handle "skip-incr" type archives */
772
773     if(dp->skip_incr && ep->next_level0 > 0) {
774         fprintf(stderr,"%s:%s lev 1 skipped due to skip-incr flag\n",
775                 dp->host->hostname, dp->name);
776         /* don't enqueue the disk */
777         askfor(ep, 0, -1, &info);
778         askfor(ep, 1, -1, &info);
779         askfor(ep, 2, -1, &info);
780
781         fprintf(stderr, "%s: SKIPPED %s %s 1 [skip-incr]\n",
782                 get_pname(), dp->host->hostname, dp->name);
783
784         log_add(L_SUCCESS, "%s %s %s 1 [skipped: skip-incr]",
785                 dp->host->hostname, dp->name, datestamp);
786         return;
787     }
788
789     if( ep->last_level == -1 && ep->next_level0 > 0 && 
790         dp->strategy != DS_NOFULL && dp->strategy != DS_INCRONLY &&
791         conf_reserve == 100) {
792         log_add(L_WARNING,
793              "%s:%s mismatch: no tapelist record, but curinfo next_level0: %d.",
794                 dp->host->hostname, dp->name, ep->next_level0);
795         ep->next_level0 = 0;
796     }
797
798     if(ep->last_level == 0) ep->level_days = 0;
799     else ep->level_days = runs_at(&info, ep->last_level);
800     ep->last_lev0size = info.inf[0].csize;
801
802     ep->fullrate = perf_average(info.full.rate, 0.0);
803     ep->incrrate = perf_average(info.incr.rate, 0.0);
804
805     ep->fullcomp = perf_average(info.full.comp, dp->comprate[0]);
806     ep->incrcomp = perf_average(info.incr.comp, dp->comprate[1]);
807
808     /* determine which estimates to get */
809
810     i = 0;
811
812     if (dp->strategy == DS_NOINC ||
813         (!dp->skip_full &&
814          (!ISSET(info.command, FORCE_BUMP) ||
815           dp->skip_incr ||
816           ep->last_level == -1))) {
817         if(info.command & FORCE_BUMP && ep->last_level == -1) {
818             log_add(L_INFO,
819                   "Remove force-bump command of %s:%s because it's a new disk.",
820                     dp->host->hostname, dp->name);
821         }
822         switch (dp->strategy) {
823         case DS_STANDARD: 
824         case DS_NOINC:
825             askfor(ep, i++, 0, &info);
826             if(dp->skip_full) {
827                 log_add(L_INFO,
828                   "Ignoring skip_full for %s:%s because the strategy is NOINC.",
829                         dp->host->hostname, dp->name);
830             }
831             if(info.command & FORCE_BUMP) {
832                 log_add(L_INFO,
833                  "Ignoring FORCE_BUMP for %s:%s because the strategy is NOINC.",
834                         dp->host->hostname, dp->name);
835             }
836             
837             break;
838
839         case DS_NOFULL:
840             break;
841
842         case DS_INCRONLY:
843             if (ISSET(info.command, FORCE_FULL))
844                 askfor(ep, i++, 0, &info);
845             break;
846         }
847     }
848
849     if(!dp->skip_incr && !(dp->strategy == DS_NOINC)) {
850         if(ep->last_level == -1) {              /* a new disk */
851             if(dp->strategy == DS_NOFULL || dp->strategy == DS_INCRONLY) {
852                 askfor(ep, i++, 1, &info);
853             } else {
854                 assert(!dp->skip_full);         /* should be handled above */
855             }
856         } else {                                /* not new, pick normally */
857             int curr_level;
858
859             curr_level = ep->last_level;
860
861             if (ISSET(info.command, FORCE_NO_BUMP)) {
862                 if(curr_level > 0) { /* level 0 already asked for */
863                     askfor(ep, i++, curr_level, &info);
864                 }
865                 log_add(L_INFO,"Preventing bump of %s:%s as directed.",
866                         dp->host->hostname, dp->name);
867             } else if (ISSET(info.command, FORCE_BUMP)
868                        && curr_level + 1 < DUMP_LEVELS) {
869                 askfor(ep, i++, curr_level+1, &info);
870                 log_add(L_INFO,"Bumping of %s:%s at level %d as directed.",
871                         dp->host->hostname, dp->name, curr_level+1);
872             } else if (curr_level == 0) {
873                 askfor(ep, i++, 1, &info);
874             } else {
875                 askfor(ep, i++, curr_level, &info);
876                 /*
877                  * If last time we dumped less than the threshold, then this
878                  * time we will too, OR the extra size will be charged to both
879                  * cur_level and cur_level + 1, so we will never bump.  Also,
880                  * if we haven't been at this level 2 days, or the dump failed
881                  * last night, we can't bump.
882                  */
883                 if((info.inf[curr_level].size == 0 || /* no data, try it anyway */
884                     (((info.inf[curr_level].size > bump_thresh(curr_level, info.inf[0].size,dp->bumppercent, dp->bumpsize, dp->bumpmult)))
885                      && ep->level_days >= dp->bumpdays))
886                    && curr_level + 1 < DUMP_LEVELS) {
887                     askfor(ep, i++, curr_level+1, &info);
888                 }
889             } 
890         }
891     }
892
893     while(i < MAX_LEVELS)       /* mark end of estimates */
894         askfor(ep, i++, -1, &info);
895
896     /* debug output */
897
898     fprintf(stderr, "setup_estimate: %s:%s: command %d, options: %s    last_level %d next_level0 %d level_days %d    getting estimates %d (%ld) %d (%ld) %d (%ld)\n",
899             dp->host->hostname, dp->name, info.command,
900             dp->strategy == DS_NOFULL ? "no-full" :
901                  dp->strategy == DS_INCRONLY ? "incr-only" :
902                  dp->skip_full ? "skip-full" :
903                  dp->skip_incr ? "skip-incr" : "none",
904             ep->last_level, ep->next_level0, ep->level_days,
905             ep->level[0], ep->est_size[0],
906             ep->level[1], ep->est_size[1],
907             ep->level[2], ep->est_size[2]);
908
909     assert(ep->level[0] != -1);
910     enqueue_disk(&startq, dp);
911 }
912
913 static int when_overwrite(label)
914 char *label;
915 {
916     tape_t *tp;
917
918     if((tp = lookup_tapelabel(label)) == NULL)
919         return 1;       /* "shouldn't happen", but trigger warning message */
920     else if(!reusable_tape(tp))
921         return 1024;
922     else if(lookup_nb_tape() > conf_tapecycle)
923         return (lookup_nb_tape() - tp->position) / conf_runtapes;
924     else
925         return (conf_tapecycle - tp->position) / conf_runtapes;
926 }
927
928 /* Return the estimated size for a particular dump */
929 static long est_size(dp, level)
930 disk_t *dp;
931 int level;
932 {
933     int i;
934
935     for(i = 0; i < MAX_LEVELS; i++) {
936         if(level == est(dp)->level[i])
937             return est(dp)->est_size[i];
938     }
939     return -1;
940 }
941
942 /* Return the estimated on-tape size of a particular dump */
943 static long est_tape_size(dp, level)
944 disk_t *dp;
945 int level;
946 {
947     long size;
948     double ratio;
949
950     size = est_size(dp, level);
951
952     if(size == -1) return size;
953
954     if(dp->compress == COMP_NONE)
955         return size;
956
957     if(level == 0) ratio = est(dp)->fullcomp;
958     else ratio = est(dp)->incrcomp;
959
960     /*
961      * make sure over-inflated compression ratios don't throw off the
962      * estimates, this is mostly for when you have a small dump getting
963      * compressed which takes up alot more disk/tape space relatively due
964      * to the overhead of the compression.  This is specifically for
965      * Digital Unix vdump.  This patch is courtesy of Rudolf Gabler
966      * (RUG@USM.Uni-Muenchen.DE)
967      */
968
969     if(ratio > 1.1) ratio = 1.1;
970
971     size *= ratio;
972
973     /*
974      * Ratio can be very small in some error situations, so make sure
975      * size goes back greater than zero.  It may not be right, but
976      * indicates we did get an estimate.
977      */
978     if(size <= 0) {
979         size = 1;
980     }
981
982     return size;
983 }
984
985
986 /* what was the level of the last successful dump to tape? */
987 static int last_level(info)
988 info_t *info;
989 {
990     int min_pos, min_level, i;
991     time_t lev0_date, last_date;
992     tape_t *tp;
993
994     if(info->last_level != -1)
995         return info->last_level;
996
997     /* to keep compatibility with old infofile */
998     min_pos = 1000000000;
999     min_level = -1;
1000     lev0_date = EPOCH;
1001     last_date = EPOCH;
1002     for(i = 0; i < 9; i++) {
1003         if(conf_reserve < 100) {
1004             if(i == 0) lev0_date = info->inf[0].date;
1005             else if(info->inf[i].date < lev0_date) continue;
1006             if(info->inf[i].date > last_date) {
1007                 last_date = info->inf[i].date;
1008                 min_level = i;
1009             }
1010         }
1011         else {
1012             if((tp = lookup_tapelabel(info->inf[i].label)) == NULL) continue;
1013             /* cull any entries from previous cycles */
1014             if(i == 0) lev0_date = info->inf[0].date;
1015             else if(info->inf[i].date < lev0_date) continue;
1016
1017             if(tp->position < min_pos) {
1018                 min_pos = tp->position;
1019                 min_level = i;
1020             }
1021         }
1022     }
1023     info->last_level = i;
1024     return min_level;
1025 }
1026
1027 /* when is next level 0 due? 0 = today, 1 = tomorrow, etc*/
1028 static int
1029 next_level0(dp, info)
1030      disk_t *dp;
1031      info_t *info;
1032 {
1033     if(dp->strategy == DS_NOFULL || dp->strategy == DS_INCRONLY)
1034         return 1;               /* fake it */
1035     else if (dp->strategy == DS_NOINC)
1036         return 0;
1037     else if(info->inf[0].date < (time_t)0)
1038         return -days_diff(EPOCH, today);        /* new disk */
1039     else
1040         return dp->dumpcycle - days_diff(info->inf[0].date, today);
1041 }
1042
1043 /* how many runs at current level? */
1044 static int runs_at(info, lev)
1045 info_t *info;
1046 int lev;
1047 {
1048     tape_t *cur_tape, *old_tape;
1049     int last, nb_runs;
1050
1051     last = last_level(info);
1052     if(lev != last) return 0;
1053     if(lev == 0) return 1;
1054
1055     if(info->consecutive_runs != -1)
1056         return info->consecutive_runs;
1057
1058     /* to keep compatibility with old infofile */
1059     cur_tape = lookup_tapelabel(info->inf[lev].label);
1060     old_tape = lookup_tapelabel(info->inf[lev-1].label);
1061     if(cur_tape == NULL || old_tape == NULL) return 0;
1062
1063     nb_runs = (old_tape->position - cur_tape->position) / conf_runtapes;
1064     info->consecutive_runs = nb_runs;
1065
1066     return nb_runs;
1067 }
1068
1069
1070 static long bump_thresh(level, size_level_0, bumppercent, bumpsize, bumpmult)
1071 int level;
1072 long size_level_0;
1073 int bumppercent;
1074 int bumpsize;
1075 double bumpmult;
1076 {
1077     double bump;
1078
1079     if(bumppercent != 0 && size_level_0 > 1024) {
1080         bump = (size_level_0 * bumppercent)/100.0;
1081     }
1082     else {
1083         bump = bumpsize;
1084     }
1085     while(--level) bump = bump * bumpmult;
1086
1087     return (long)bump;
1088 }
1089
1090
1091 \f
1092 /*
1093  * ========================================================================
1094  * GET REMOTE DUMP SIZE ESTIMATES
1095  *
1096  */
1097
1098 static void getsize P((am_host_t *hostp));
1099 static disk_t *lookup_hostdisk P((am_host_t *hp, char *str));
1100 static void handle_result P((void *datap, pkt_t *pkt, security_handle_t *sech));
1101
1102
1103 static void get_estimates P((void))
1104 {
1105     am_host_t *hostp;
1106     disk_t *dp;
1107     int something_started;
1108
1109     something_started = 1;
1110     while(something_started) {
1111         something_started = 0;
1112         for(dp = startq.head; dp != NULL; dp = dp->next) {
1113             hostp = dp->host;
1114             if(hostp->up == HOST_READY) {
1115                 something_started = 1;
1116                 getsize(hostp);
1117                 protocol_check();
1118                 /*
1119                  * dp is no longer on startq, so dp->next is not valid
1120                  * and we have to start all over.
1121                  */
1122                 break;
1123             }
1124         }
1125     }
1126     protocol_run();
1127
1128     while(!empty(waitq)) {
1129         disk_t *dp = dequeue_disk(&waitq);
1130         est(dp)->errstr = "hmm, disk was stranded on waitq";
1131         enqueue_disk(&failq, dp);
1132     }
1133
1134     while(!empty(pestq)) {
1135         disk_t *dp = dequeue_disk(&pestq);
1136
1137         if(est(dp)->level[0] != -1 && est(dp)->est_size[0] < 0) {
1138             if(est(dp)->est_size[0] == -1) {
1139                 log_add(L_WARNING,
1140                         "disk %s:%s, estimate of level %d failed.",
1141                         dp->host->hostname, dp->name,
1142                         est(dp)->level[0]);
1143             }
1144             else {
1145                 log_add(L_WARNING,
1146                         "disk %s:%s, estimate of level %d timed out.",
1147                         dp->host->hostname, dp->name,
1148                         est(dp)->level[0]);
1149             }
1150             est(dp)->level[0] = -1;
1151         }
1152
1153         if(est(dp)->level[1] != -1 && est(dp)->est_size[1] < 0) {
1154             if(est(dp)->est_size[1] == -1) {
1155                 log_add(L_WARNING,
1156                         "disk %s:%s, estimate of level %d failed.",
1157                         dp->host->hostname, dp->name,
1158                         est(dp)->level[1]);
1159             }
1160             else {
1161                 log_add(L_WARNING,
1162                         "disk %s:%s, estimate of level %d timed out.",
1163                         dp->host->hostname, dp->name,
1164                         est(dp)->level[1]);
1165             }
1166             est(dp)->level[1] = -1;
1167         }
1168
1169         if(est(dp)->level[2] != -1 && est(dp)->est_size[2] < 0) {
1170             if(est(dp)->est_size[2] == -1) {
1171                 log_add(L_WARNING,
1172                         "disk %s:%s, estimate of level %d failed.",
1173                         dp->host->hostname, dp->name,
1174                         est(dp)->level[2]);
1175             }
1176             else {
1177                 log_add(L_WARNING,
1178                         "disk %s:%s, estimate of level %d timed out.",
1179                         dp->host->hostname, dp->name,
1180                         est(dp)->level[2]);
1181             }
1182             est(dp)->level[2] = -1;
1183         }
1184
1185         if((est(dp)->level[0] != -1 && est(dp)->est_size[0] > 0) ||
1186            (est(dp)->level[1] != -1 && est(dp)->est_size[1] > 0) ||
1187            (est(dp)->level[2] != -1 && est(dp)->est_size[2] > 0)) {
1188             enqueue_disk(&estq, dp);
1189         }
1190         else {
1191            est(dp)->errstr = vstralloc("disk ", dp->name,
1192                                        ", all estimate timed out", NULL);
1193            enqueue_disk(&failq, dp);
1194         }
1195     }
1196 }
1197
1198 static void getsize(hostp)
1199 am_host_t *hostp;
1200 {
1201     char number[NUM_STR_SIZE], *req;
1202     disk_t *dp;
1203     int i, estimates, timeout, req_len;
1204     const security_driver_t *secdrv;
1205     char *dumper;
1206     char *calcsize;
1207
1208     assert(hostp->disks != NULL);
1209
1210     if(hostp->up != HOST_READY) {
1211         return;
1212     }
1213
1214     /*
1215      * The first time through here we send a "noop" request.  This will
1216      * return the feature list from the client if it supports that.
1217      * If it does not, handle_result() will set the feature list to an
1218      * empty structure.  In either case, we do the disks on the second
1219      * (and subsequent) pass(es).
1220      */
1221     if(hostp->features != NULL) { /* sendsize service */
1222         int nb_client = 0;
1223         int nb_server = 0;
1224
1225         int has_features = am_has_feature(hostp->features,
1226                                           fe_req_options_features);
1227         int has_hostname = am_has_feature(hostp->features,
1228                                           fe_req_options_hostname);
1229         int has_maxdumps = am_has_feature(hostp->features,
1230                                           fe_req_options_maxdumps);
1231
1232         snprintf(number, sizeof(number), "%d", hostp->maxdumps);
1233         req = vstralloc("SERVICE ", "sendsize", "\n",
1234                         "OPTIONS ",
1235                         has_features ? "features=" : "",
1236                         has_features ? our_feature_string : "",
1237                         has_features ? ";" : "",
1238                         has_maxdumps ? "maxdumps=" : "",
1239                         has_maxdumps ? number : "",
1240                         has_maxdumps ? ";" : "",
1241                         has_hostname ? "hostname=" : "",
1242                         has_hostname ? hostp->hostname : "",
1243                         has_hostname ? ";" : "",
1244                         "\n",
1245                         NULL);
1246         req_len = strlen(req);
1247         req_len += 128;                             /* room for SECURITY ... */
1248         estimates = 0;
1249         for(dp = hostp->disks; dp != NULL; dp = dp->hostnext) {
1250             char *s = NULL;
1251             int s_len = 0;
1252
1253             if(dp->todo == 0) continue;
1254
1255             if(est(dp)->state != DISK_READY) continue;
1256
1257             est(dp)->got_estimate = 0;
1258             if(est(dp)->level[0] == -1) {
1259                 est(dp)->state = DISK_DONE;
1260                 continue;
1261             }
1262
1263             if(dp->estimate == ES_CLIENT ||
1264                dp->estimate == ES_CALCSIZE) {
1265                 nb_client++;
1266
1267                 for(i = 0; i < MAX_LEVELS; i++) {
1268                     char *l;
1269                     char *exclude1 = "";
1270                     char *exclude2 = "";
1271                     char *excludefree = NULL;
1272                     char spindle[NUM_STR_SIZE];
1273                     char level[NUM_STR_SIZE];
1274                     int lev = est(dp)->level[i];
1275
1276                     if(lev == -1) break;
1277
1278                     snprintf(level, sizeof(level), "%d", lev);
1279                     snprintf(spindle, sizeof(spindle), "%d", dp->spindle);
1280                     if(am_has_feature(hostp->features,fe_sendsize_req_options)){
1281                         exclude1 = " OPTIONS |";
1282                         exclude2 = optionstr(dp, hostp->features, NULL);
1283                         excludefree = exclude2;
1284                     }
1285                     else {
1286                         if(dp->exclude_file &&
1287                            dp->exclude_file->nb_element == 1) {
1288                             exclude1 = " exclude-file=";
1289                             exclude2 = dp->exclude_file->first->name;
1290                         }
1291                         else if(dp->exclude_list &&
1292                                 dp->exclude_list->nb_element == 1) {
1293                             exclude1 = " exclude-list=";
1294                             exclude2 = dp->exclude_list->first->name;
1295                         }
1296                     }
1297
1298                     if(dp->estimate == ES_CALCSIZE &&
1299                        !am_has_feature(hostp->features, fe_calcsize_estimate)) {
1300                         log_add(L_WARNING,"%s:%s does not support CALCSIZE for estimate, using CLIENT.\n",
1301                                 hostp->hostname, dp->name);
1302                         dp->estimate = ES_CLIENT;
1303                     }
1304                     if(dp->estimate == ES_CLIENT)
1305                         calcsize = "";
1306                     else
1307                         calcsize = "CALCSIZE ";
1308
1309                     if(strncmp(dp->program,"DUMP",4) == 0 || 
1310                        strncmp(dp->program,"GNUTAR",6) == 0) {
1311                         dumper = "";
1312                     } else {
1313                         dumper = "DUMPER ";
1314                     }
1315                     l = vstralloc(calcsize,
1316                                   dumper,
1317                                   dp->program,
1318                                   " ", dp->name,
1319                                   " ", dp->device ? dp->device : "",
1320                                   " ", level,
1321                                   " ", est(dp)->dumpdate[i],
1322                                   " ", spindle,
1323                                   " ", exclude1, exclude2,
1324                                   "\n",
1325                                   NULL);
1326                     strappend(s, l);
1327                     s_len += strlen(l);
1328                     amfree(l);
1329                     amfree(excludefree);
1330                 }
1331                 /*
1332                  * Allow 2X for err response.
1333                  */
1334                 if(req_len + s_len > MAX_PACKET / 2) {
1335                     amfree(s);
1336                     break;
1337                 }
1338                 if (s != NULL) {
1339                     estimates += i;
1340                     strappend(req, s);
1341                     req_len += s_len;
1342                     amfree(s);
1343                 }
1344                 est(dp)->state = DISK_ACTIVE;
1345                 remove_disk(&startq, dp);
1346             }
1347             else if (dp->estimate == ES_SERVER) {
1348                 info_t info;
1349                 nb_server++;
1350                 get_info(dp->host->hostname, dp->name, &info);
1351                 for(i = 0; i < MAX_LEVELS; i++) {
1352                     int j;
1353                     int lev = est(dp)->level[i];
1354
1355                     if(lev == -1) break;
1356                     if(lev == 0) { /* use latest level 0, should do extrapolation */
1357                         long est_size = 0;
1358                         int nb_est = 0;
1359
1360                         for(j=NB_HISTORY-2;j>=0;j--) {
1361                             if(info.history[j].level == 0) {
1362                                 if(info.history[j].size < 0) continue;
1363                                 est_size = info.history[j].size;
1364                                 nb_est++;
1365                             }
1366                         }
1367                         if(nb_est > 0) {
1368                             est(dp)->est_size[i] = est_size;
1369                         }
1370                         else if(info.inf[lev].size > 1000) { /* stats */
1371                             est(dp)->est_size[i] = info.inf[lev].size;
1372                         }
1373                         else {
1374                             est(dp)->est_size[i] = 1000000;
1375                         }
1376                     }
1377                     else if(lev == est(dp)->last_level) {
1378                         /* means of all X day at the same level */
1379                         #define NB_DAY 30
1380                         int nb_day = 0;
1381                         long est_size_day[NB_DAY];
1382                         int nb_est_day[NB_DAY];
1383                         for(j=0;j<NB_DAY;j++) {
1384                             est_size_day[j]=0;
1385                             nb_est_day[j]=0;
1386                         }
1387
1388                         for(j=NB_HISTORY-2;j>=0;j--) {
1389                             if(info.history[j].level <= 0) continue;
1390                             if(info.history[j].size < 0) continue;
1391                             if(info.history[j].level==info.history[j+1].level) {
1392                                 if(nb_day <NB_DAY-1) nb_day++;
1393                                 est_size_day[nb_day] += info.history[j].size;
1394                                 nb_est_day[nb_day]++;
1395                             }
1396                             else {
1397                                 nb_day=0;
1398                             }
1399                         }
1400                         nb_day = info.consecutive_runs + 1;
1401                         if(nb_day > NB_DAY-1) nb_day = NB_DAY-1;
1402
1403                         while(nb_day > 0 && nb_est_day[nb_day] == 0) nb_day--;
1404
1405                         if(nb_est_day[nb_day] > 0) {
1406                             est(dp)->est_size[i] =
1407                                     est_size_day[nb_day] / nb_est_day[nb_day];
1408                         }
1409                         else if(info.inf[lev].size > 1000) { /* stats */
1410                             est(dp)->est_size[i] = info.inf[lev].size;
1411                         }
1412                         else {
1413                             est(dp)->est_size[i] = 10000;
1414                         }
1415                     }
1416                     else if(lev == est(dp)->last_level + 1) {
1417                         /* means of all first day at a new level */
1418                         long est_size = 0;
1419                         int nb_est = 0;
1420
1421                         for(j=NB_HISTORY-2;j>=0;j--) {
1422                             if(info.history[j].level <= 0) continue;
1423                             if(info.history[j].size < 0) continue;
1424                             if(info.history[j].level == info.history[j+1].level + 1 ) {
1425                                 est_size += info.history[j].size;
1426                                 nb_est++;
1427                             }
1428                         }
1429                         if(nb_est > 0) {
1430                             est(dp)->est_size[i] = est_size / nb_est;
1431                         }
1432                         else if(info.inf[lev].size > 1000) { /* stats */
1433                             est(dp)->est_size[i] = info.inf[lev].size;
1434                         }
1435                         else {
1436                             est(dp)->est_size[i] = 100000;
1437                         }
1438                     }
1439                 }
1440                 fprintf(stderr,"%s time %s: got result for host %s disk %s:",
1441                         get_pname(), walltime_str(curclock()),
1442                         dp->host->hostname, dp->name);
1443                 fprintf(stderr," %d -> %ldK, %d -> %ldK, %d -> %ldK\n",
1444                         est(dp)->level[0], est(dp)->est_size[0],
1445                         est(dp)->level[1], est(dp)->est_size[1],
1446                         est(dp)->level[2], est(dp)->est_size[2]);
1447                 est(dp)->state = DISK_DONE;
1448                 remove_disk(&startq, dp);
1449                 enqueue_disk(&estq, dp);
1450             }
1451         }
1452
1453         if(estimates == 0) {
1454             amfree(req);
1455             hostp->up = HOST_DONE;
1456             return;
1457         }
1458
1459         if (conf_etimeout < 0) {
1460             timeout = - conf_etimeout;
1461         } else {
1462             timeout = estimates * conf_etimeout;
1463         }
1464     } else { /* noop service */
1465         req = vstralloc("SERVICE ", "noop", "\n",
1466                         "OPTIONS ",
1467                         "features=", our_feature_string, ";",
1468                         "\n",
1469                         NULL);
1470         /*
1471          * We use ctimeout for the "noop" request because it should be
1472          * very fast and etimeout has other side effects.
1473          */
1474         timeout = getconf_int(CNF_CTIMEOUT);
1475     }
1476
1477     secdrv = security_getdriver(hostp->disks->security_driver);
1478     if (secdrv == NULL) {
1479         error("could not find security driver '%s' for host '%s'",
1480             hostp->disks->security_driver, hostp->hostname);
1481     }
1482     hostp->up = HOST_ACTIVE;
1483
1484     for(dp = hostp->disks; dp != NULL; dp = dp->hostnext) {
1485         if(dp->todo == 0) {
1486             continue;
1487         }
1488         if(est(dp)->state == DISK_ACTIVE) {
1489             est(dp)->errstr = NULL;
1490             enqueue_disk(&waitq, dp);
1491         }
1492     }
1493
1494     protocol_sendreq(hostp->hostname, secdrv, generic_get_security_conf, 
1495         req, timeout, handle_result, hostp);
1496     amfree(req);
1497 }
1498
1499 static disk_t *lookup_hostdisk(hp, str)
1500 am_host_t *hp;
1501 char *str;
1502 {
1503     disk_t *dp;
1504
1505     for(dp = hp->disks; dp != NULL; dp = dp->hostnext)
1506         if(strcmp(str, dp->name) == 0) return dp;
1507
1508     return NULL;
1509 }
1510
1511
1512 static void handle_result(datap, pkt, sech)
1513 void *datap;
1514 pkt_t *pkt;
1515 security_handle_t *sech;
1516 {
1517     int level, i;
1518     long size;
1519     disk_t *dp;
1520     am_host_t *hostp;
1521     char *msgdisk=NULL, *msgdisk_undo=NULL, msgdisk_undo_ch = '\0';
1522     char *remoterr, *errbuf = NULL;
1523     char *s;
1524     char *t;
1525     char *fp;
1526     char *line;
1527     int ch;
1528     int tch;
1529
1530     hostp = (am_host_t *)datap;
1531     hostp->up = HOST_READY;
1532
1533     if (pkt == NULL) {
1534         errbuf = vstralloc("Request to ", hostp->hostname, " failed: ", 
1535             security_geterror(sech), NULL);
1536         goto error_return;
1537     }
1538     if (pkt->type == P_NAK) {
1539 #define sc "ERROR "
1540         if(strncmp(pkt->body, sc, sizeof(sc)-1) == 0) {
1541             s = pkt->body + sizeof(sc)-1;
1542             ch = *s++;
1543 #undef sc
1544         } else {
1545             goto NAK_parse_failed;
1546         }
1547         skip_whitespace(s, ch);
1548         if(ch == '\0') goto NAK_parse_failed;
1549         remoterr = s - 1;
1550         if((s = strchr(remoterr, '\n')) != NULL) {
1551             if(s == remoterr) goto NAK_parse_failed;
1552                 *s = '\0';
1553         }
1554         if (strcmp(remoterr, "unknown service: noop") != 0
1555                    && strcmp(remoterr, "noop: invalid service") != 0) {
1556             errbuf = vstralloc(hostp->hostname, " NAK: ", remoterr, NULL);
1557             if(s) *s = '\n';
1558             goto error_return;
1559         }
1560     }
1561
1562     msgdisk_undo = NULL;
1563     s = pkt->body;
1564     ch = *s++;
1565     while(ch) {
1566         line = s - 1;
1567         skip_line(s, ch);
1568         if (s[-2] == '\n') {
1569             s[-2] = '\0';
1570         }
1571
1572 #define sc "OPTIONS "
1573         if(strncmp(line, sc, sizeof(sc)-1) == 0) {
1574 #undef sc
1575
1576 #define sc "features="
1577             t = strstr(line, sc);
1578             if(t != NULL && (isspace((int)t[-1]) || t[-1] == ';')) {
1579                 t += sizeof(sc)-1;
1580 #undef sc
1581                 am_release_feature_set(hostp->features);
1582                 if((hostp->features = am_string_to_feature(t)) == NULL) {
1583                     errbuf = vstralloc(hostp->hostname,
1584                                        ": bad features value: ",
1585                                        line,
1586                                        "\n",
1587                                        NULL);
1588                     goto error_return;
1589                 }
1590             }
1591
1592             continue;
1593         }
1594
1595 #define sc "ERROR "
1596         if(strncmp(line, sc, sizeof(sc)-1) == 0) {
1597             t = line + sizeof(sc)-1;
1598             tch = t[-1];
1599 #undef sc
1600
1601             fp = t - 1;
1602             skip_whitespace(t, tch);
1603             if (tch == '\n') {
1604                 t[-1] = '\0';
1605             }
1606             /*
1607              * If the "error" is that the "noop" service is unknown, it
1608              * just means the client is "old" (does not support the servie).
1609              * We can ignore this.
1610              */
1611             if(hostp->features == NULL
1612                && pkt->type == P_NAK
1613                && (strcmp(t - 1, "unknown service: noop") == 0
1614                    || strcmp(t - 1, "noop: invalid service") == 0)) {
1615                 continue;
1616             } else {
1617                 errbuf = vstralloc(hostp->hostname,
1618                                    (pkt->type == P_NAK) ? "NAK " : "",
1619                                    ": ",
1620                                    fp,
1621                                    NULL);
1622                 goto error_return;
1623             }
1624         }
1625
1626         msgdisk = t = line;
1627         tch = *t++;
1628         skip_non_whitespace(t, tch);
1629         msgdisk_undo = t - 1;
1630         msgdisk_undo_ch = *msgdisk_undo;
1631         *msgdisk_undo = '\0';
1632
1633         skip_whitespace(t, tch);
1634         if (sscanf(t - 1, "%d SIZE %ld", &level, &size) != 2) {
1635             goto bad_msg;
1636         }
1637
1638         dp = lookup_hostdisk(hostp, msgdisk);
1639
1640         *msgdisk_undo = msgdisk_undo_ch;        /* for error message */
1641         msgdisk_undo = NULL;
1642
1643         if(dp == NULL) {
1644             log_add(L_ERROR, "%s: invalid reply from sendsize: `%s'\n",
1645                     hostp->hostname, line);
1646         } else {
1647             for(i = 0; i < MAX_LEVELS; i++) {
1648                 if(est(dp)->level[i] == level) {
1649                     est(dp)->est_size[i] = size;
1650                     break;
1651                 }
1652             }
1653             if(i == MAX_LEVELS) {
1654                 goto bad_msg;                   /* this est wasn't requested */
1655             }
1656             est(dp)->got_estimate++;
1657         }
1658     }
1659
1660     if(hostp->up == HOST_READY && hostp->features == NULL) {
1661         /*
1662          * The client does not support the features list, so give it an
1663          * empty one.
1664          */
1665         dbprintf(("%s: no feature set from host %s\n",
1666                   debug_prefix_time(NULL), hostp->hostname));
1667         hostp->features = am_set_default_feature_set();
1668     }
1669
1670
1671     /* XXX what about disks that only got some estimates...  do we care? */
1672     /* XXX amanda 2.1 treated that case as a bad msg */
1673
1674     for(dp = hostp->disks; dp != NULL; dp = dp->hostnext) {
1675         if(dp->todo == 0) continue;
1676         if(est(dp)->state != DISK_ACTIVE &&
1677            est(dp)->state != DISK_PARTIALY_DONE) continue;
1678
1679         if(est(dp)->state == DISK_ACTIVE) {
1680             remove_disk(&waitq, dp);
1681         }
1682         else if(est(dp)->state == DISK_PARTIALY_DONE) {
1683             remove_disk(&pestq, dp);
1684         }
1685
1686         if(pkt->type == P_REP) {
1687             est(dp)->state = DISK_DONE;
1688         }
1689         else if(pkt->type == P_PREP) {
1690             est(dp)->state = DISK_PARTIALY_DONE;
1691         }
1692
1693         if(est(dp)->level[0] == -1) continue;   /* ignore this disk */
1694
1695
1696         if(pkt->type == P_PREP) {
1697                 fprintf(stderr,"%s: time %s: got partial result for host %s disk %s:",
1698                         get_pname(), walltime_str(curclock()),
1699                         dp->host->hostname, dp->name);
1700                 fprintf(stderr," %d -> %ldK, %d -> %ldK, %d -> %ldK\n",
1701                         est(dp)->level[0], est(dp)->est_size[0],
1702                         est(dp)->level[1], est(dp)->est_size[1],
1703                         est(dp)->level[2], est(dp)->est_size[2]);
1704             enqueue_disk(&pestq, dp);
1705         }
1706         else if(pkt->type == P_REP) {
1707                 fprintf(stderr,"%s: time %s: got result for host %s disk %s:",
1708                         get_pname(), walltime_str(curclock()),
1709                         dp->host->hostname, dp->name);
1710                 fprintf(stderr," %d -> %ldK, %d -> %ldK, %d -> %ldK\n",
1711                         est(dp)->level[0], est(dp)->est_size[0],
1712                         est(dp)->level[1], est(dp)->est_size[1],
1713                         est(dp)->level[2], est(dp)->est_size[2]);
1714                 if((est(dp)->level[0] != -1 && est(dp)->est_size[0] > 0) ||
1715                    (est(dp)->level[1] != -1 && est(dp)->est_size[1] > 0) ||
1716                    (est(dp)->level[2] != -1 && est(dp)->est_size[2] > 0)) {
1717
1718                     if(est(dp)->level[2] != -1 && est(dp)->est_size[2] < 0) {
1719                         log_add(L_WARNING,
1720                                 "disk %s:%s, estimate of level %d failed.",
1721                                 dp->host->hostname, dp->name,
1722                                 est(dp)->level[2]);
1723                         est(dp)->level[2] = -1;
1724                     }
1725                     if(est(dp)->level[1] != -1 && est(dp)->est_size[1] < 0) {
1726                         log_add(L_WARNING,
1727                                 "disk %s:%s, estimate of level %d failed.",
1728                                 dp->host->hostname, dp->name,
1729                                 est(dp)->level[1]);
1730                         est(dp)->level[1] = -1;
1731                     }
1732                     if(est(dp)->level[0] != -1 && est(dp)->est_size[0] < 0) {
1733                         log_add(L_WARNING,
1734                                 "disk %s:%s, estimate of level %d failed.",
1735                                 dp->host->hostname, dp->name,
1736                                 est(dp)->level[0]);
1737                         est(dp)->level[0] = -1;
1738                     }
1739                     enqueue_disk(&estq, dp);
1740             }
1741             else {
1742                 enqueue_disk(&failq, dp);
1743                 if(est(dp)->got_estimate) {
1744                     est(dp)->errstr = vstralloc("disk ", dp->name,
1745                                                 ", all estimate failed", NULL);
1746                 }
1747                 else {
1748                     fprintf(stderr, "error result for host %s disk %s: missing estimate\n",
1749                             dp->host->hostname, dp->name);
1750                     est(dp)->errstr = vstralloc("missing result for ", dp->name,
1751                                                 " in ", dp->host->hostname,
1752                                                 " response",
1753                                                 NULL);
1754                 }
1755             }
1756         }
1757     }
1758     getsize(hostp);
1759     return;
1760
1761  NAK_parse_failed:
1762
1763     /* msgdisk_undo is always NULL */
1764     /* if(msgdisk_undo) { */
1765     /*  *msgdisk_undo = msgdisk_undo_ch; */
1766     /*  msgdisk_undo = NULL; */
1767     /* } */
1768     errbuf = stralloc2(hostp->hostname, " NAK: [NAK parse failed]");
1769     fprintf(stderr, "got strange nak from %s:\n----\n%s----\n\n",
1770             hostp->hostname, pkt->body);
1771     goto error_return;
1772
1773  bad_msg:
1774
1775     if(msgdisk_undo) {
1776         *msgdisk_undo = msgdisk_undo_ch;
1777         msgdisk_undo = NULL;
1778     }
1779     fprintf(stderr,"got a bad message, stopped at:\n");
1780     fprintf(stderr,"----\n%s----\n\n", line);
1781     errbuf = stralloc2("badly formatted response from ", hostp->hostname);
1782     /* fall through to ... */
1783
1784  error_return:
1785
1786     i = 0;
1787     for(dp = hostp->disks; dp != NULL; dp = dp->hostnext) {
1788         if(est(dp)->state != DISK_ACTIVE) continue;
1789         est(dp)->state = DISK_DONE;
1790         if(est(dp)->state == DISK_ACTIVE) {
1791             est(dp)->state = DISK_DONE;
1792             remove_disk(&waitq, dp);
1793             enqueue_disk(&failq, dp);
1794             i++;
1795
1796             est(dp)->errstr = stralloc(errbuf);
1797             fprintf(stderr, "error result for host %s disk %s: %s\n",
1798                 dp->host->hostname, dp->name, errbuf);
1799         }
1800     }
1801     if(i == 0) {
1802         /*
1803          * If there were no disks involved, make sure the error gets
1804          * reported.
1805          */
1806         log_add(L_ERROR, "%s", errbuf);
1807     }
1808     hostp->up = HOST_DONE;
1809     amfree(errbuf);
1810 }
1811
1812
1813
1814 \f
1815 /*
1816  * ========================================================================
1817  * ANALYSE ESTIMATES
1818  *
1819  */
1820
1821 static int schedule_order P((disk_t *a, disk_t *b));      /* subroutines */
1822 static int pick_inclevel P((disk_t *dp));
1823
1824 static void analyze_estimate(dp)
1825 disk_t *dp;
1826 {
1827     est_t *ep;
1828     info_t info;
1829     int have_info = 0;
1830
1831     ep = est(dp);
1832
1833     fprintf(stderr, "pondering %s:%s... ",
1834             dp->host->hostname, dp->name);
1835     fprintf(stderr, "next_level0 %d last_level %d ",
1836             ep->next_level0, ep->last_level);
1837
1838     if(get_info(dp->host->hostname, dp->name, &info) == 0) {
1839         have_info = 1;
1840     }
1841
1842     ep->degr_level = -1;
1843     ep->degr_size = -1;
1844
1845     if(ep->next_level0 <= 0
1846        || (have_info && ep->last_level == 0 && (info.command & FORCE_NO_BUMP))) {
1847         if(ep->next_level0 <= 0) {
1848             fprintf(stderr,"(due for level 0) ");
1849         }
1850         ep->dump_level = 0;
1851         ep->dump_size = est_tape_size(dp, 0);
1852         if(ep->dump_size <= 0) {
1853             fprintf(stderr,
1854                     "(no estimate for level 0, picking an incr level)\n");
1855             ep->dump_level = pick_inclevel(dp);
1856             ep->dump_size = est_tape_size(dp, ep->dump_level);
1857
1858             if(ep->dump_size == -1) {
1859                 ep->dump_level = ep->dump_level + 1;
1860                 ep->dump_size = est_tape_size(dp, ep->dump_level);
1861             }
1862         }
1863         else {
1864             total_lev0 += (double) ep->dump_size;
1865             if(ep->last_level == -1 || dp->skip_incr) {
1866                 fprintf(stderr,"(%s disk, can't switch to degraded mode)\n",
1867                         dp->skip_incr? "skip-incr":"new");
1868                 ep->degr_level = -1;
1869                 ep->degr_size = -1;
1870             }
1871             else {
1872                 /* fill in degraded mode info */
1873                 fprintf(stderr,"(picking inclevel for degraded mode)");
1874                 ep->degr_level = pick_inclevel(dp);
1875                 ep->degr_size = est_tape_size(dp, ep->degr_level);
1876                 if(ep->degr_size == -1) {
1877                     ep->degr_level = ep->degr_level + 1;
1878                     ep->degr_size = est_tape_size(dp, ep->degr_level);
1879                 }
1880                 if(ep->degr_size == -1) {
1881                     fprintf(stderr,"(no inc estimate)");
1882                     ep->degr_level = -1;
1883                 }
1884                 fprintf(stderr,"\n");
1885             }
1886         }
1887     }
1888     else {
1889         fprintf(stderr,"(not due for a full dump, picking an incr level)\n");
1890         /* XXX - if this returns -1 may be we should force a total? */
1891         ep->dump_level = pick_inclevel(dp);
1892         ep->dump_size = est_tape_size(dp, ep->dump_level);
1893
1894         if(ep->dump_size == -1) {
1895             ep->dump_level = ep->last_level;
1896             ep->dump_size = est_tape_size(dp, ep->dump_level);
1897         }
1898         if(ep->dump_size == -1) {
1899             ep->dump_level = ep->last_level + 1;
1900             ep->dump_size = est_tape_size(dp, ep->dump_level);
1901         }
1902         if(ep->dump_size == -1) {
1903             ep->dump_level = 0;
1904             ep->dump_size = est_tape_size(dp, ep->dump_level);
1905         }
1906     }
1907
1908     fprintf(stderr,"  curr level %d size %ld ", ep->dump_level, ep->dump_size);
1909
1910     insert_disk(&schedq, dp, schedule_order);
1911
1912     total_size += tt_blocksize_kb + ep->dump_size + tape_mark;
1913
1914     /* update the balanced size */
1915     if(!(dp->skip_full || dp->strategy == DS_NOFULL || 
1916          dp->strategy == DS_INCRONLY)) {
1917         long lev0size;
1918
1919         lev0size = est_tape_size(dp, 0);
1920         if(lev0size == -1) lev0size = ep->last_lev0size;
1921
1922         balanced_size += lev0size / runs_per_cycle;
1923     }
1924
1925     fprintf(stderr,"total size " AM64_FMT " total_lev0 %1.0f balanced-lev0size %1.0f\n",
1926             total_size, total_lev0, balanced_size);
1927 }
1928
1929 static void handle_failed(dp)
1930 disk_t *dp;
1931 {
1932     char *errstr;
1933
1934 /*
1935  * From George Scott <George.Scott@cc.monash.edu.au>:
1936  * --------
1937  * If a machine is down when the planner is run it guesses from historical
1938  * data what the size of tonights dump is likely to be and schedules a
1939  * dump anyway.  The dumper then usually discovers that that machine is
1940  * still down and ends up with a half full tape.  Unfortunately the
1941  * planner had to delay another dump because it thought that the tape was
1942  * full.  The fix here is for the planner to ignore unavailable machines
1943  * rather than ignore the fact that they are unavailable.
1944  * --------
1945  */
1946
1947 #ifdef old_behavior
1948     if(est(dp)->last_level != -1) {
1949         log_add(L_WARNING,
1950                 "Could not get estimate for %s:%s, using historical data.",
1951                 dp->host->hostname, dp->name);
1952         analyze_estimate(dp);
1953         return;
1954     }
1955 #endif
1956
1957     errstr = est(dp)->errstr? est(dp)->errstr : "hmm, no error indicator!";
1958
1959     fprintf(stderr, "%s: FAILED %s %s %s 0 [%s]\n",
1960         get_pname(), dp->host->hostname, dp->name, datestamp, errstr);
1961
1962     log_add(L_FAIL, "%s %s %s 0 [%s]", dp->host->hostname, dp->name, 
1963             datestamp, errstr);
1964
1965     /* XXX - memory leak with *dp */
1966 }
1967
1968
1969 static int schedule_order(a, b)
1970 disk_t *a, *b;
1971 /*
1972  * insert-sort by decreasing priority, then
1973  * by decreasing size within priority levels.
1974  */
1975 {
1976     int diff;
1977     long ldiff;
1978
1979     diff = est(b)->dump_priority - est(a)->dump_priority;
1980     if(diff != 0) return diff;
1981
1982     ldiff = est(b)->dump_size - est(a)->dump_size;
1983     if(ldiff < 0) return -1; /* XXX - there has to be a better way to dothis */
1984     if(ldiff > 0) return 1;
1985     return 0;
1986 }
1987
1988
1989 static int pick_inclevel(dp)
1990 disk_t *dp;
1991 {
1992     int base_level, bump_level;
1993     long base_size, bump_size;
1994     long thresh;
1995
1996     base_level = est(dp)->last_level;
1997
1998     /* if last night was level 0, do level 1 tonight, no ifs or buts */
1999     if(base_level == 0) {
2000         fprintf(stderr,"   picklev: last night 0, so tonight level 1\n");
2001         return 1;
2002     }
2003
2004     /* if no-full option set, always do level 1 */
2005     if(dp->strategy == DS_NOFULL) {
2006         fprintf(stderr,"   picklev: no-full set, so always level 1\n");
2007         return 1;
2008     }
2009
2010     base_size = est_size(dp, base_level);
2011
2012     /* if we didn't get an estimate, we can't do an inc */
2013     if(base_size == -1) {
2014         base_size = est_size(dp, base_level+1);
2015         if(base_size > 0) /* FORCE_BUMP */
2016             return base_level+1;
2017         fprintf(stderr,"   picklev: no estimate for level %d, so no incs\n", base_level);
2018         return base_level;
2019     }
2020
2021     thresh = bump_thresh(base_level, est_size(dp, 0), dp->bumppercent, dp->bumpsize, dp->bumpmult);
2022
2023     fprintf(stderr,
2024             "   pick: size %ld level %d days %d (thresh %ldK, %d days)\n",
2025             base_size, base_level, est(dp)->level_days,
2026             thresh, dp->bumpdays);
2027
2028     if(base_level == 9
2029        || est(dp)->level_days < dp->bumpdays
2030        || base_size <= thresh)
2031             return base_level;
2032
2033     bump_level = base_level + 1;
2034     bump_size = est_size(dp, bump_level);
2035
2036     if(bump_size == -1) return base_level;
2037
2038     fprintf(stderr, "   pick: next size %ld... ", bump_size);
2039
2040     if(base_size - bump_size < thresh) {
2041         fprintf(stderr, "not bumped\n");
2042         return base_level;
2043     }
2044
2045     fprintf(stderr, "BUMPED\n");
2046     log_add(L_INFO, "Incremental of %s:%s bumped to level %d.",
2047             dp->host->hostname, dp->name, bump_level);
2048
2049     return bump_level;
2050 }
2051
2052
2053
2054 \f
2055 /*
2056 ** ========================================================================
2057 ** ADJUST SCHEDULE
2058 **
2059 ** We have two strategies here:
2060 **
2061 ** 1. Delay dumps
2062 **
2063 ** If we are trying to fit too much on the tape something has to go.  We
2064 ** try to delay totals until tomorrow by converting them into incrementals
2065 ** and, if that is not effective enough, dropping incrementals altogether.
2066 ** While we are searching for the guilty dump (the one that is really
2067 ** causing the schedule to be oversize) we have probably trampled on a lot of
2068 ** innocent dumps, so we maintain a "before image" list and use this to
2069 ** put back what we can.
2070 **
2071 ** 2. Promote dumps.
2072 **
2073 ** We try to keep the amount of tape used by total dumps the same each night.
2074 ** If there is some spare tape in this run we have a look to see if any of
2075 ** tonights incrementals could be promoted to totals and leave us with a
2076 ** more balanced cycle.
2077 */
2078
2079 static void delay_one_dump P((disk_t *dp, int delete, ...));
2080
2081 static void delay_dumps P((void))
2082 /* delay any dumps that will not fit */
2083 {
2084     disk_t *dp, *ndp, *preserve;
2085     bi_t *bi, *nbi;
2086     am64_t new_total;   /* New total_size */
2087     char est_kb[20];     /* Text formatted dump size */
2088     int nb_forced_level_0;
2089     info_t info;
2090     int delete;
2091     char *message;
2092
2093     biq.head = biq.tail = NULL;
2094
2095     /*
2096     ** 1. Delay dumps that are way oversize.
2097     **
2098     ** Dumps larger that the size of the tapes we are using are just plain
2099     ** not going to fit no matter how many other dumps we drop.  Delay
2100     ** oversize totals until tomorrow (by which time my owner will have
2101     ** resolved the problem!) and drop incrementals altogether.  Naturally
2102     ** a large total might be delayed into a large incremental so these
2103     ** need to be checked for separately.
2104     */
2105
2106     for(dp = schedq.head; dp != NULL; dp = ndp) {
2107         int avail_tapes = 1;
2108         if (dp->tape_splitsize > 0)
2109             avail_tapes = conf_runtapes;
2110
2111         ndp = dp->next; /* remove_disk zaps this */
2112
2113         if (est(dp)->dump_size == -1 ||
2114             est(dp)->dump_size <= tape->length * avail_tapes) {
2115             continue;
2116         }
2117
2118         /* Format dumpsize for messages */
2119         snprintf(est_kb, 20, "%ld KB,", est(dp)->dump_size);
2120
2121         if(est(dp)->dump_level == 0) {
2122             if(dp->skip_incr) {
2123                 delete = 1;
2124                 message = "but cannot incremental dump skip-incr disk";
2125             }
2126             else if(est(dp)->last_level < 0) {
2127                 delete = 1;
2128                 message = "but cannot incremental dump new disk";
2129             }
2130             else if(est(dp)->degr_level < 0) {
2131                 delete = 1;
2132                 message = "but no incremental estimate";
2133             }
2134             else if (est(dp)->degr_size > tape->length) {
2135                 delete = 1;
2136                 message = "incremental dump also larger than tape";
2137             }
2138             else {
2139                 delete = 0;
2140                 message = "full dump delayed";
2141             }
2142         }
2143         else {
2144             delete = 1;
2145             message = "skipping incremental";
2146         }
2147         delay_one_dump(dp, delete, "dump larger than available tape space,", est_kb,
2148                        message, NULL);
2149     }
2150
2151     /*
2152     ** 2. Delay total dumps.
2153     **
2154     ** Delay total dumps until tomorrow (or the day after!).  We start with
2155     ** the lowest priority (most dispensable) and work forwards.  We take
2156     ** care not to delay *all* the dumps since this could lead to a stale
2157     ** mate [for any one disk there are only three ways tomorrows dump will
2158     ** be smaller than todays: 1. we do a level 0 today so tomorows dump
2159     ** will be a level 1; 2. the disk gets more data so that it is bumped
2160     ** tomorrow (this can be a slow process); and, 3. the disk looses some
2161     ** data (when does that ever happen?)].
2162     */
2163
2164     nb_forced_level_0 = 0;
2165     preserve = NULL;
2166     for(dp = schedq.head; dp != NULL && preserve == NULL; dp = dp->next)
2167         if(est(dp)->dump_level == 0)
2168             preserve = dp;
2169
2170     /* 2.a. Do not delay forced full */
2171     for(dp = schedq.tail;
2172                 dp != NULL && total_size > tape_length;
2173                 dp = ndp) {
2174         ndp = dp->prev;
2175
2176         if(est(dp)->dump_level != 0) continue;
2177
2178         get_info(dp->host->hostname, dp->name, &info);
2179         if(info.command & FORCE_FULL) {
2180             nb_forced_level_0 += 1;
2181             preserve = dp;
2182             continue;
2183         }
2184
2185         if(dp != preserve) {
2186
2187             /* Format dumpsize for messages */
2188             snprintf(est_kb, 20, "%ld KB,", est(dp)->dump_size);
2189
2190             if(dp->skip_incr) {
2191                 delete = 1;
2192                 message = "but cannot incremental dump skip-incr disk";
2193             }
2194             else if(est(dp)->last_level < 0) {
2195                 delete = 1;
2196                 message = "but cannot incremental dump new disk";
2197             }
2198             else if(est(dp)->degr_level < 0) {
2199                 delete = 1;
2200                 message = "but no incremental estimate";
2201             }
2202             else {
2203                 delete = 0;
2204                 message = "full dump delayed";
2205             }
2206             delay_one_dump(dp, delete, "dumps too big,", est_kb,
2207                            message, NULL);
2208         }
2209     }
2210
2211     /* 2.b. Delay forced full if needed */
2212     if(nb_forced_level_0 > 0 && total_size > tape_length) {
2213         for(dp = schedq.tail;
2214                 dp != NULL && total_size > tape_length;
2215                 dp = ndp) {
2216             ndp = dp->prev;
2217
2218             if(est(dp)->dump_level == 0 && dp != preserve) {
2219
2220                 /* Format dumpsize for messages */
2221                 snprintf(est_kb, 20, "%ld KB,", est(dp)->dump_size);
2222
2223                 if(dp->skip_incr) {
2224                     delete = 1;
2225                     message = "but cannot incremental dump skip-incr disk";
2226                 }
2227                 else if(est(dp)->last_level < 0) {
2228                     delete = 1;
2229                     message = "but cannot incremental dump new disk";
2230                 }
2231                 else if(est(dp)->degr_level < 0) {
2232                     delete = 1;
2233                     message = "but no incremental estimate";
2234                 }
2235                 else {
2236                     delete = 0;
2237                     message = "full dump delayed";
2238                 }
2239                 delay_one_dump(dp, delete, "dumps too big,", est_kb,
2240                                message, NULL);
2241             }
2242         }
2243     }
2244
2245     /*
2246     ** 3. Delay incremental dumps.
2247     **
2248     ** Delay incremental dumps until tomorrow.  This is a last ditch attempt
2249     ** at making things fit.  Again, we start with the lowest priority (most
2250     ** dispensable) and work forwards.
2251     */
2252
2253     for(dp = schedq.tail;
2254             dp != NULL && total_size > tape_length;
2255             dp = ndp) {
2256         ndp = dp->prev;
2257
2258         if(est(dp)->dump_level != 0) {
2259
2260             /* Format dumpsize for messages */
2261             snprintf(est_kb, 20, "%ld KB,", est(dp)->dump_size);
2262
2263             delay_one_dump(dp, 1,
2264                            "dumps way too big,",
2265                            est_kb,
2266                            "must skip incremental dumps",
2267                            NULL);
2268         }
2269     }
2270
2271     /*
2272     ** 4. Reinstate delayed dumps.
2273     **
2274     ** We might not have needed to stomp on all of the dumps we have just
2275     ** delayed above.  Try to reinstate them all starting with the last one
2276     ** and working forwards.  It is unlikely that the last one will fit back
2277     ** in but why complicate the code?
2278     */
2279
2280     for(bi = biq.tail; bi != NULL; bi = nbi) {
2281         int avail_tapes = 1;
2282         nbi = bi->prev;
2283         dp = bi->dp;
2284         if(dp->tape_splitsize > 0) avail_tapes = conf_runtapes;
2285
2286         if(bi->deleted)
2287             new_total = total_size + tt_blocksize_kb + bi->size + tape_mark;
2288         else
2289             new_total = total_size - est(dp)->dump_size + bi->size;
2290
2291         if(new_total <= tape_length && bi->size < tape->length * avail_tapes) {
2292             /* reinstate it */
2293             total_size = new_total;
2294             if(bi->deleted) {
2295                 if(bi->level == 0) {
2296                     total_lev0 += (double) bi->size;
2297                 }
2298                 insert_disk(&schedq, dp, schedule_order);
2299             }
2300             else {
2301                 est(dp)->dump_level = bi->level;
2302                 est(dp)->dump_size = bi->size;
2303             }
2304
2305             /* Keep it clean */
2306             if(bi->next == NULL)
2307                 biq.tail = bi->prev;
2308             else
2309                 (bi->next)->prev = bi->prev;
2310             if(bi->prev == NULL)
2311                 biq.head = bi->next;
2312             else
2313                 (bi->prev)->next = bi->next;
2314             amfree(bi->errstr);
2315             amfree(bi);
2316         }
2317     }
2318
2319     /*
2320     ** 5. Output messages about what we have done.
2321     **
2322     ** We can't output messages while we are delaying dumps because we might
2323     ** reinstate them later.  We remember all the messages and output them
2324     ** now.
2325     */
2326
2327     for(bi = biq.head; bi != NULL; bi = nbi) {
2328         nbi = bi->next;
2329
2330         if(bi->deleted) {
2331             fprintf(stderr, "%s: FAILED %s\n", get_pname(), bi->errstr);
2332             log_add(L_FAIL, "%s", bi->errstr);
2333         }
2334         else {
2335             dp = bi->dp;
2336             fprintf(stderr, "  delay: %s now at level %d\n",
2337                 bi->errstr, est(dp)->dump_level);
2338             log_add(L_INFO, "%s", bi->errstr);
2339         }
2340
2341         /* Clean up - dont be too fancy! */
2342         amfree(bi->errstr);
2343         amfree(bi);
2344     }
2345
2346     fprintf(stderr, "  delay: Total size now " AM64_FMT ".\n", total_size);
2347
2348     return;
2349 }
2350
2351
2352 /*
2353  * Remove a dump or modify it from full to incremental.
2354  * Keep track of it on the bi q in case we can add it back later.
2355  */
2356 arglist_function1(static void delay_one_dump,
2357                   disk_t *, dp,
2358                   int, delete)
2359 {
2360     bi_t *bi;
2361     va_list argp;
2362     char level_str[NUM_STR_SIZE];
2363     char *sep;
2364     char *next;
2365
2366     arglist_start(argp, delete);
2367
2368     total_size -= tt_blocksize_kb + est(dp)->dump_size + tape_mark;
2369     if(est(dp)->dump_level == 0) {
2370         total_lev0 -= (double) est(dp)->dump_size;
2371     }
2372
2373     bi = alloc(sizeof(bi_t));
2374     bi->next = NULL;
2375     bi->prev = biq.tail;
2376     if(biq.tail == NULL)
2377         biq.head = bi;
2378     else
2379         biq.tail->next = bi;
2380     biq.tail = bi;
2381
2382     bi->deleted = delete;
2383     bi->dp = dp;
2384     bi->level = est(dp)->dump_level;
2385     bi->size = est(dp)->dump_size;
2386
2387     snprintf(level_str, sizeof(level_str), "%d", est(dp)->dump_level);
2388     bi->errstr = vstralloc(dp->host->hostname,
2389                            " ", dp->name,
2390                            " ", datestamp ? datestamp : "?",
2391                            " ", level_str,
2392                            NULL);
2393     sep = " [";
2394     while ((next = arglist_val(argp, char *)) != NULL) {
2395         bi->errstr = newvstralloc(bi->errstr, bi->errstr, sep, next, NULL);
2396         sep = " ";
2397     }
2398     strappend(bi->errstr, "]");
2399     arglist_end(argp);
2400
2401     if (delete) {
2402         remove_disk(&schedq, dp);
2403     } else {
2404         est(dp)->dump_level = est(dp)->degr_level;
2405         est(dp)->dump_size = est(dp)->degr_size;
2406         total_size += tt_blocksize_kb + est(dp)->dump_size + tape_mark;
2407     }
2408
2409     return;
2410 }
2411
2412
2413 static int promote_highest_priority_incremental P((void))
2414 {
2415     disk_t *dp, *dp1, *dp_promote;
2416     long new_size, new_total, new_lev0;
2417     int check_days;
2418     int nb_today, nb_same_day, nb_today2;
2419     int nb_disk_today, nb_disk_same_day;
2420
2421     /*
2422      * return 1 if did so; must update total_size correctly; must not
2423      * cause total_size to exceed tape_length
2424      */
2425
2426     dp_promote = NULL;
2427     for(dp = schedq.head; dp != NULL; dp = dp->next) {
2428
2429         est(dp)->promote = -1000;
2430
2431         if(est_size(dp,0) <= 0)
2432             continue;
2433
2434         if(est(dp)->next_level0 <= 0)
2435             continue;
2436
2437         if(est(dp)->next_level0 > dp->maxpromoteday)
2438             continue;
2439
2440         new_size = est_tape_size(dp, 0);
2441         new_total = total_size - est(dp)->dump_size + new_size;
2442         new_lev0 = total_lev0 + new_size;
2443
2444         nb_today = 0;
2445         nb_same_day = 0;
2446         nb_disk_today = 0;
2447         nb_disk_same_day = 0;
2448         for(dp1 = schedq.head; dp1 != NULL; dp1 = dp1->next) {
2449             if(est(dp1)->dump_level == 0)
2450                 nb_disk_today++;
2451             else if(est(dp1)->next_level0 == est(dp)->next_level0)
2452                 nb_disk_same_day++;
2453             if(strcmp(dp->host->hostname, dp1->host->hostname) == 0) {
2454                 if(est(dp1)->dump_level == 0)
2455                     nb_today++;
2456                 else if(est(dp1)->next_level0 == est(dp)->next_level0)
2457                     nb_same_day++;
2458             }
2459         }
2460
2461         /* do not promote if overflow tape */
2462         if(new_total > tape_length) continue;
2463
2464         /* do not promote if overflow balanced size and something today */
2465         /* promote if nothing today */
2466         if(new_lev0 > balanced_size+balance_threshold && nb_disk_today > 0)
2467             continue;
2468
2469         /* do not promote if only one disk due that day and nothing today */
2470         if(nb_disk_same_day == 1 && nb_disk_today == 0) continue;
2471
2472         nb_today2 = nb_today*nb_today;
2473         if(nb_today == 0 && nb_same_day > 1) nb_same_day++;
2474
2475         if(nb_same_day >= nb_today2) {
2476             est(dp)->promote = ((nb_same_day - nb_today2)*(nb_same_day - nb_today2)) + 
2477                                conf_dumpcycle - est(dp)->next_level0;
2478         }
2479         else {
2480             est(dp)->promote = -nb_today2 +
2481                                conf_dumpcycle - est(dp)->next_level0;
2482         }
2483
2484         if(!dp_promote || est(dp_promote)->promote < est(dp)->promote) {
2485             dp_promote = dp;
2486             fprintf(stderr,"   try %s:%s %d %d %d = %d\n",
2487                     dp->host->hostname, dp->name, nb_same_day, nb_today, est(dp)->next_level0, est(dp)->promote);
2488         }
2489         else {
2490             fprintf(stderr,"no try %s:%s %d %d %d = %d\n",
2491                     dp->host->hostname, dp->name, nb_same_day, nb_today, est(dp)->next_level0, est(dp)->promote);
2492         }
2493     }
2494
2495     if(dp_promote) {
2496         dp = dp_promote;
2497
2498         new_size = est_tape_size(dp, 0);
2499         new_total = total_size - est(dp)->dump_size + new_size;
2500         new_lev0 = total_lev0 + new_size;
2501
2502         total_size = new_total;
2503         total_lev0 = new_lev0;
2504         check_days = est(dp)->next_level0;
2505         est(dp)->degr_level = est(dp)->dump_level;
2506         est(dp)->degr_size = est(dp)->dump_size;
2507         est(dp)->dump_level = 0;
2508         est(dp)->dump_size = new_size;
2509         est(dp)->next_level0 = 0;
2510
2511         fprintf(stderr,
2512               "   promote: moving %s:%s up, total_lev0 %1.0f, total_size " AM64_FMT "\n",
2513                 dp->host->hostname, dp->name,
2514                 total_lev0, total_size);
2515
2516         log_add(L_INFO,
2517                 "Full dump of %s:%s promoted from %d day%s ahead.",
2518                 dp->host->hostname, dp->name,
2519                 check_days, (check_days == 1) ? "" : "s");
2520         return 1;
2521     }
2522     return 0;
2523 }
2524
2525
2526 static int promote_hills P((void))
2527 {
2528     disk_t *dp;
2529     struct balance_stats {
2530         int disks;
2531         long size;
2532     } *sp = NULL;
2533     int days;
2534     int hill_days = 0;
2535     long hill_size;
2536     long new_size;
2537     long new_total;
2538     int my_dumpcycle;
2539
2540     /* If we are already doing a level 0 don't bother */
2541     if(total_lev0 > 0)
2542         return 0;
2543
2544     /* Do the guts of an "amadmin balance" */
2545     my_dumpcycle = conf_dumpcycle;
2546     if(my_dumpcycle > 10000) my_dumpcycle = 10000;
2547
2548     sp = (struct balance_stats *)
2549         alloc(sizeof(struct balance_stats) * my_dumpcycle);
2550
2551     for(days = 0; days < my_dumpcycle; days++)
2552         sp[days].disks = sp[days].size = 0;
2553
2554     for(dp = schedq.head; dp != NULL; dp = dp->next) {
2555         days = est(dp)->next_level0;   /* This is > 0 by definition */
2556         if(days<my_dumpcycle && !dp->skip_full && dp->strategy != DS_NOFULL &&
2557            dp->strategy != DS_INCRONLY) {
2558             sp[days].disks++;
2559             sp[days].size += est(dp)->last_lev0size;
2560         }
2561     }
2562
2563     /* Search for a suitable big hill and cut it down */
2564     while(1) {
2565         /* Find the tallest hill */
2566         hill_size = 0;
2567         for(days = 0; days < my_dumpcycle; days++) {
2568             if(sp[days].disks > 1 && sp[days].size > hill_size) {
2569                 hill_size = sp[days].size;
2570                 hill_days = days;
2571             }
2572         }
2573
2574         if(hill_size <= 0) break;       /* no suitable hills */
2575
2576         /* Find all the dumps in that hill and try and remove one */
2577         for(dp = schedq.head; dp != NULL; dp = dp->next) {
2578             if(est(dp)->next_level0 != hill_days ||
2579                est(dp)->next_level0 > dp->maxpromoteday ||
2580                dp->skip_full ||
2581                dp->strategy == DS_NOFULL ||
2582                dp->strategy == DS_INCRONLY)
2583                 continue;
2584             new_size = est_tape_size(dp, 0);
2585             new_total = total_size - est(dp)->dump_size + new_size;
2586             if(new_total > tape_length)
2587                 continue;
2588             /* We found a disk we can promote */
2589             total_size = new_total;
2590             total_lev0 += new_size;
2591             est(dp)->degr_level = est(dp)->dump_level;
2592             est(dp)->degr_size = est(dp)->dump_size;
2593             est(dp)->dump_level = 0;
2594             est(dp)->next_level0 = 0;
2595             est(dp)->dump_size = new_size;
2596
2597             fprintf(stderr,
2598                     "   promote: moving %s:%s up, total_lev0 %1.0f, total_size " AM64_FMT "\n",
2599                     dp->host->hostname, dp->name,
2600                     total_lev0, total_size);
2601
2602             log_add(L_INFO,
2603                     "Full dump of %s:%s specially promoted from %d day%s ahead.",
2604                     dp->host->hostname, dp->name,
2605                     hill_days, (hill_days == 1) ? "" : "s");
2606
2607             amfree(sp);
2608             return 1;
2609         }
2610         /* All the disks in that hill were unsuitable. */
2611         sp[hill_days].disks = 0;        /* Don't get tricked again */
2612     }
2613
2614     amfree(sp);
2615     return 0;
2616 }
2617
2618 /*
2619  * ========================================================================
2620  * OUTPUT SCHEDULE
2621  *
2622  * XXX - memory leak - we shouldn't just throw away *dp
2623  */
2624 static void output_scheduleline(dp)
2625     disk_t *dp;
2626 {
2627     est_t *ep;
2628     long dump_time = 0, degr_time = 0;
2629     char *schedline = NULL, *degr_str = NULL;
2630     char dump_priority_str[NUM_STR_SIZE];
2631     char dump_level_str[NUM_STR_SIZE];
2632     char dump_size_str[NUM_STR_SIZE];
2633     char dump_time_str[NUM_STR_SIZE];
2634     char degr_level_str[NUM_STR_SIZE];
2635     char degr_size_str[NUM_STR_SIZE];
2636     char degr_time_str[NUM_STR_SIZE];
2637     char *dump_date, *degr_date;
2638     char *features;
2639     int i;
2640
2641     ep = est(dp);
2642
2643     if(ep->dump_size == -1) {
2644         /* no estimate, fail the disk */
2645         fprintf(stderr,
2646                 "%s: FAILED %s %s %s %d [no estimate]\n",
2647                 get_pname(),
2648                 dp->host->hostname, dp->name, datestamp, ep->dump_level);
2649         log_add(L_FAIL, "%s %s %s %d [no estimate]",
2650                 dp->host->hostname, dp->name, datestamp, ep->dump_level);
2651         return;
2652     }
2653
2654     dump_date = degr_date = (char *)0;
2655     for(i = 0; i < MAX_LEVELS; i++) {
2656         if(ep->dump_level == ep->level[i])
2657             dump_date = ep->dumpdate[i];
2658         if(ep->degr_level == ep->level[i])
2659             degr_date = ep->dumpdate[i];
2660     }
2661
2662 #define fix_rate(rate) (rate < 1.0 ? DEFAULT_DUMPRATE : rate)
2663
2664     if(ep->dump_level == 0) {
2665         dump_time = ep->dump_size / fix_rate(ep->fullrate);
2666
2667         if(ep->degr_size != -1) {
2668             degr_time = ep->degr_size / fix_rate(ep->incrrate);
2669         }
2670     }
2671     else {
2672         dump_time = ep->dump_size / fix_rate(ep->incrrate);
2673     }
2674
2675     if(ep->dump_level == 0 && ep->degr_size != -1) {
2676         snprintf(degr_level_str, sizeof(degr_level_str),
2677                     "%d", ep->degr_level);
2678         snprintf(degr_size_str, sizeof(degr_size_str),
2679                     "%ld", ep->degr_size);
2680         snprintf(degr_time_str, sizeof(degr_time_str),
2681                     "%ld", degr_time);
2682         degr_str = vstralloc(" ", degr_level_str,
2683                              " ", degr_date,
2684                              " ", degr_size_str,
2685                              " ", degr_time_str,
2686                              NULL);
2687     }
2688     snprintf(dump_priority_str, sizeof(dump_priority_str),
2689                 "%d", ep->dump_priority);
2690     snprintf(dump_level_str, sizeof(dump_level_str),
2691                 "%d", ep->dump_level);
2692     snprintf(dump_size_str, sizeof(dump_size_str),
2693                 "%ld", ep->dump_size);
2694     snprintf(dump_time_str, sizeof(dump_time_str),
2695                 "%ld", dump_time);
2696     features = am_feature_to_string(dp->host->features);
2697     schedline = vstralloc("DUMP ",dp->host->hostname,
2698                           " ", features,
2699                           " ", dp->name,
2700                           " ", datestamp,
2701                           " ", dump_priority_str,
2702                           " ", dump_level_str,
2703                           " ", dump_date,
2704                           " ", dump_size_str,
2705                           " ", dump_time_str,
2706                           degr_str ? degr_str : "",
2707                           "\n", NULL);
2708
2709     fputs(schedline, stdout);
2710     fputs(schedline, stderr);
2711     amfree(features);
2712     amfree(schedline);
2713     amfree(degr_str);
2714 }