2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998 University of Maryland at College Park
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.
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.
23 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
27 * $Id: driver.c 6512 2007-05-24 17:00:24Z ian $
29 * controlling process for the Amanda backup system
33 * XXX possibly modify tape queue to be cognizant of how much room is left on
34 * tape. Probably not effective though, should do this in planner.
48 #include "server_util.h"
49 #include "timestamp.h"
51 #define driver_debug(i, ...) do { \
52 if ((i) <= debug_driver) { \
53 dbprintf(__VA_ARGS__); \
57 #define hold_debug(i, ...) do { \
58 if ((i) <= debug_holding) { \
59 dbprintf(__VA_ARGS__); \
63 static disklist_t waitq; // dle waiting estimate result
64 static disklist_t runq; // dle waiting to be dumped to holding disk
65 static disklist_t directq; // dle waiting to be dumped directly to tape
66 static disklist_t tapeq; // dle on holding disk waiting to be written
68 static disklist_t roomq; // dle waiting for more space on holding disk
69 static int pending_aborts;
70 static disk_t *taper_disk;
71 static int degraded_mode;
72 static off_t reserved_space;
73 static off_t total_disksize;
74 static char *dumper_program;
75 static char *chunker_program;
76 static int inparallel;
77 static int nodump = 0;
78 static off_t tape_length = (off_t)0;
79 static off_t tape_left = (off_t)0;
80 static int current_tape = 0;
81 static int conf_taperalgo;
82 static int conf_runtapes;
83 static time_t sleep_time;
84 static int idle_reason;
85 static char *driver_timestamp;
86 static char *hd_driver_timestamp;
87 static am_host_t *flushhost = NULL;
88 static int need_degraded=0;
89 static holdalloc_t *holdalloc;
90 static int num_holdalloc;
91 static event_handle_t *dumpers_ev_time = NULL;
92 static event_handle_t *schedule_ev_read = NULL;
93 static int conf_flush_threshold_dumped;
94 static int conf_flush_threshold_scheduled;
95 static int conf_taperflush;
96 static off_t flush_threshold_dumped;
97 static off_t flush_threshold_scheduled;
98 static off_t taperflush;
99 static int schedule_done; // 1 if we don't wait for a
100 // schedule from the planner
101 static int force_flush; // All dump are terminated, we
102 // must now respect taper_flush
104 static int wait_children(int count);
105 static void wait_for_children(void);
106 static void allocate_bandwidth(netif_t *ip, unsigned long kps);
107 static int assign_holdingdisk(assignedhd_t **holdp, disk_t *diskp);
108 static void adjust_diskspace(disk_t *diskp, cmd_t cmd);
109 static void delete_diskspace(disk_t *diskp);
110 static assignedhd_t **build_diskspace(char *destname);
111 static int client_constrained(disk_t *dp);
112 static void deallocate_bandwidth(netif_t *ip, unsigned long kps);
113 static void dump_schedule(disklist_t *qp, char *str);
114 static void dump_to_tape(disk_t *dp);
115 static assignedhd_t **find_diskspace(off_t size, int *cur_idle,
116 assignedhd_t *preferred);
117 static unsigned long free_kps(netif_t *ip);
118 static off_t free_space(void);
119 static void dumper_chunker_result(disk_t *dp);
120 static void dumper_taper_result(disk_t *dp);
121 static void file_taper_result(disk_t *dp);
122 static void handle_dumper_result(void *);
123 static void handle_chunker_result(void *);
124 static void handle_dumpers_time(void *);
125 static void handle_taper_result(void *);
127 static void holdingdisk_state(char *time_str);
128 static dumper_t *idle_dumper(void);
129 static void interface_state(char *time_str);
130 static int queue_length(disklist_t q);
131 static disklist_t read_flush(void);
132 static void read_schedule(void *cookie);
133 static void short_dump_state(void);
134 static void startaflush(void);
135 static void start_degraded_mode(disklist_t *queuep);
136 static void start_some_dumps(disklist_t *rq);
137 static void continue_port_dumps(void);
138 static void update_failed_dump_to_tape(disk_t *);
141 TAPE_ACTION_NO_ACTION = 0,
142 TAPE_ACTION_NEW_TAPE = (1 << 0),
143 TAPE_ACTION_NO_NEW_TAPE = (1 << 1),
144 TAPE_ACTION_START_A_FLUSH = (1 << 2)
147 static TapeAction tape_action(void);
149 static const char *idle_strings[] = {
152 #define IDLE_NO_DUMPERS 1
154 #define IDLE_START_WAIT 2
156 #define IDLE_NO_HOLD 3
158 #define IDLE_CLIENT_CONSTRAINED 4
159 T_("client-constrained"),
160 #define IDLE_NO_BANDWIDTH 5
162 #define IDLE_NO_DISKSPACE 6
176 struct fs_usage fsusage;
178 unsigned long reserve = 100;
182 char *result_argv[MAX_ARGS+1];
189 config_overwrites_t *cfg_ovr = NULL;
190 char *cfg_opt = NULL;
191 holdalloc_t *ha, *ha_last;
194 * Configure program for internationalization:
195 * 1) Only set the message locale for now.
196 * 2) Set textdomain for all amanda related programs to "amanda"
197 * We don't want to be forced to support dozens of message catalogs.
199 setlocale(LC_MESSAGES, "C");
200 textdomain("amanda");
204 setvbuf(stdout, (char *)NULL, (int)_IOLBF, 0);
205 setvbuf(stderr, (char *)NULL, (int)_IOLBF, 0);
209 dbopen(DBG_SUBDIR_SERVER);
211 atexit(wait_for_children);
213 /* Don't die when child closes pipe */
214 signal(SIGPIPE, SIG_IGN);
216 erroutput_type = (ERR_AMANDALOG|ERR_INTERACTIVE);
217 set_logerror(logerror);
221 cfg_ovr = extract_commandline_config_overwrites(&argc, &argv);
225 config_init(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_USE_CWD | CONFIG_INIT_FATAL,
227 apply_config_overwrites(cfg_ovr);
229 g_printf(_("%s: pid %ld executable %s version %s\n"),
230 get_pname(), (long) getpid(), argv[0], version());
233 if(strncmp(argv[2], "nodump", 6) == 0) {
238 safe_cd(); /* do this *after* config_init */
240 check_running_as(RUNNING_AS_DUMPUSER);
242 dbrename(config_name, DBG_SUBDIR_SERVER);
244 amfree(driver_timestamp);
245 /* read timestamp from stdin */
246 while ((line = agets(stdin)) != NULL) {
251 if ( line == NULL ) {
252 error(_("Did not get DATE line from planner"));
255 driver_timestamp = alloc(15);
256 strncpy(driver_timestamp, &line[5], 14);
257 driver_timestamp[14] = '\0';
259 log_add(L_START,_("date %s"), driver_timestamp);
261 gethostname(hostname, SIZEOF(hostname));
262 log_add(L_STATS,_("hostname %s"), hostname);
264 /* check that we don't do many dump in a day and usetimestamps is off */
265 if(strlen(driver_timestamp) == 8) {
267 char *conf_logdir = getconf_str(CNF_LOGDIR);
268 char *logfile = vstralloc(conf_logdir, "/log.",
269 driver_timestamp, ".0", NULL);
270 char *oldlogfile = vstralloc(conf_logdir, "/oldlog/log.",
271 driver_timestamp, ".0", NULL);
272 if(access(logfile, F_OK) == 0 || access(oldlogfile, F_OK) == 0) {
273 log_add(L_WARNING, _("WARNING: This is not the first amdump run today. Enable the usetimestamps option in the configuration file if you want to run amdump more than once per calendar day."));
278 hd_driver_timestamp = get_timestamp_from_time(0);
281 hd_driver_timestamp = stralloc(driver_timestamp);
284 taper_program = vstralloc(amlibexecdir, "/", "taper", versionsuffix(), NULL);
285 dumper_program = vstralloc(amlibexecdir, "/", "dumper", versionsuffix(),
287 chunker_program = vstralloc(amlibexecdir, "/", "chunker", versionsuffix(),
290 conf_taperalgo = getconf_taperalgo(CNF_TAPERALGO);
291 conf_tapetype = getconf_str(CNF_TAPETYPE);
292 conf_runtapes = getconf_int(CNF_RUNTAPES);
293 tape = lookup_tapetype(conf_tapetype);
294 tape_length = tapetype_get_length(tape);
295 conf_flush_threshold_dumped = getconf_int(CNF_FLUSH_THRESHOLD_DUMPED);
296 conf_flush_threshold_scheduled = getconf_int(CNF_FLUSH_THRESHOLD_SCHEDULED);
297 conf_taperflush = getconf_int(CNF_TAPERFLUSH);
299 flush_threshold_dumped = (conf_flush_threshold_dumped * tape_length) / 100;
300 flush_threshold_scheduled = (conf_flush_threshold_scheduled * tape_length) / 100;
301 taperflush = (conf_taperflush *tape_length) / 100;
303 driver_debug(1, _("flush_threshold_dumped: %lld\n"), (long long)flush_threshold_dumped);
304 driver_debug(1, _("flush_threshold_scheduled: %lld\n"), (long long)flush_threshold_scheduled);
305 driver_debug(1, _("taperflush: %lld\n"), (long long)taperflush);
307 /* start initializing: read in databases */
309 conf_diskfile = config_dir_relative(getconf_str(CNF_DISKFILE));
310 if (read_diskfile(conf_diskfile, &origq) < 0) {
311 error(_("could not load disklist \"%s\""), conf_diskfile);
314 amfree(conf_diskfile);
316 /* set up any configuration-dependent variables */
318 inparallel = getconf_int(CNF_INPARALLEL);
320 reserve = (unsigned long)getconf_int(CNF_RESERVE);
322 total_disksize = (off_t)0;
325 for(hdp = getconf_holdingdisks(), dsk = 0; hdp != NULL; hdp = holdingdisk_next(hdp), dsk++) {
326 ha = alloc(SIZEOF(holdalloc_t));
329 /* link the list in the same order as getconf_holdingdisks's results */
338 ha->allocated_dumpers = 0;
339 ha->allocated_space = (off_t)0;
340 ha->disksize = holdingdisk_get_disksize(hdp);
343 if(get_fs_usage(holdingdisk_get_diskdir(hdp), NULL, &fsusage) == -1
344 || access(holdingdisk_get_diskdir(hdp), W_OK) == -1) {
345 log_add(L_WARNING, _("WARNING: ignoring holding disk %s: %s\n"),
346 holdingdisk_get_diskdir(hdp), strerror(errno));
351 /* do the division first to avoid potential integer overflow */
352 if (fsusage.fsu_bavail_top_bit_set)
355 kb_avail = fsusage.fsu_bavail / 1024 * fsusage.fsu_blocksize;
357 if(ha->disksize > (off_t)0) {
358 if(ha->disksize > kb_avail) {
360 _("WARNING: %s: %lld KB requested, "
361 "but only %lld KB available."),
362 holdingdisk_get_diskdir(hdp),
363 (long long)ha->disksize,
364 (long long)kb_avail);
365 ha->disksize = kb_avail;
368 /* ha->disksize is negative; use all but that amount */
369 else if(kb_avail < -ha->disksize) {
371 _("WARNING: %s: not %lld KB free."),
372 holdingdisk_get_diskdir(hdp),
373 (long long)-ha->disksize);
374 ha->disksize = (off_t)0;
378 ha->disksize += kb_avail;
380 g_printf(_("driver: adding holding disk %d dir %s size %lld chunksize %lld\n"),
381 dsk, holdingdisk_get_diskdir(hdp),
382 (long long)ha->disksize,
383 (long long)(holdingdisk_get_chunksize(hdp)));
385 newdir = newvstralloc(newdir,
386 holdingdisk_get_diskdir(hdp), "/", hd_driver_timestamp,
388 if(!mkholdingdir(newdir)) {
389 ha->disksize = (off_t)0;
391 total_disksize += ha->disksize;
394 reserved_space = total_disksize * (off_t)(reserve / 100);
396 g_printf(_("reserving %lld out of %lld for degraded-mode dumps\n"),
397 (long long)reserved_space, (long long)free_space());
401 if(inparallel > MAX_DUMPERS) inparallel = MAX_DUMPERS;
403 /* taper takes a while to get going, so start it up right away */
406 if(conf_runtapes > 0) {
407 startup_tape_process(taper_program);
408 taper_cmd(START_TAPER, driver_timestamp, NULL, 0, NULL);
411 /* fire up the dumpers now while we are waiting */
412 if(!nodump) startup_dump_processes(dumper_program, inparallel, driver_timestamp);
415 * Read schedule from stdin. Usually, this is a pipe from planner,
416 * so the effect is that we wait here for the planner to
417 * finish, but meanwhile the taper is rewinding the tape, reading
418 * the label, checking it, writing a new label and all that jazz
419 * in parallel with the planner.
427 taper_state = TAPER_STATE_DEFAULT;
428 tapeq = read_flush();
430 roomq.head = roomq.tail = NULL;
432 log_add(L_STATS, _("startup time %s"), walltime_str(curclock()));
434 g_printf(_("driver: start time %s inparallel %d bandwidth %lu diskspace %lld "), walltime_str(curclock()), inparallel,
435 free_kps(NULL), (long long)free_space());
436 g_printf(_(" dir %s datestamp %s driver: drain-ends tapeq %s big-dumpers %s\n"),
437 "OBSOLETE", driver_timestamp, taperalgo2str(conf_taperalgo),
438 getconf_str(CNF_DUMPORDER));
441 /* ok, planner is done, now lets see if the tape is ready */
443 if (conf_runtapes > 0) {
444 cmd = getresult(taper, 1, &result_argc, result_argv, MAX_ARGS+1);
445 if (cmd != TAPER_OK) {
446 /* no tape, go into degraded mode: dump to holding disk */
453 tape_left = tape_length;
455 taper_input_error = NULL;
456 taper_tape_error = NULL;
458 taper_ev_read = NULL;
460 schedule_done = nodump;
463 if(!need_degraded) startaflush();
466 schedule_ev_read = event_register((event_id_t)0, EV_READFD, read_schedule, NULL);
473 /* mv runq to directq */
474 while (!empty(runq)) {
475 diskp = dequeue_disk(&runq);
476 headqueue_disk(&directq, diskp);
479 /* handle any remaining dumps by dumping directly to tape, if possible */
480 while(!empty(directq) && taper > 0) {
481 diskp = dequeue_disk(&directq);
482 if (diskp->to_holdingdisk == HOLD_REQUIRED) {
483 log_add(L_FAIL, _("%s %s %s %d [%s]"),
484 diskp->host->hostname, diskp->name, sched(diskp)->datestamp,
486 _("can't dump required holdingdisk"));
488 else if (!degraded_mode) {
489 taper_state |= TAPER_STATE_DUMP_TO_TAPE;
492 taper_state &= !TAPER_STATE_DUMP_TO_TAPE;
495 log_add(L_FAIL, _("%s %s %s %d [%s]"),
496 diskp->host->hostname, diskp->name, sched(diskp)->datestamp,
498 diskp->to_holdingdisk == HOLD_AUTO ?
499 _("no more holding disk space") :
500 _("can't dump no-hold disk in degraded mode"));
503 /* fill up the tape or start new one for taperflush */
507 short_dump_state(); /* for amstatus */
509 g_printf(_("driver: QUITTING time %s telling children to quit\n"),
510 walltime_str(curclock()));
514 for(dumper = dmptable; dumper < dmptable + inparallel; dumper++) {
516 dumper_cmd(dumper, QUIT, NULL);
521 taper_cmd(QUIT, NULL, NULL, 0, NULL);
524 /* wait for all to die */
528 holding_cleanup(NULL, NULL);
532 check_unfree_serial();
533 g_printf(_("driver: FINISHED time %s\n"), walltime_str(curclock()));
535 log_add(L_FINISH,_("date %s time %s"), driver_timestamp, walltime_str(curclock()));
536 amfree(driver_timestamp);
538 amfree(dumper_program);
539 amfree(taper_program);
546 /* sleep up to count seconds, and wait for terminating child process */
547 /* if sleep is negative, this function will not timeout */
548 /* exit once all child process are finished or the timout expired */
549 /* return 0 if no more children to wait */
550 /* return 1 if some children are still alive */
552 wait_children(int count)
564 pid = waitpid((pid_t)-1, &retstat, WNOHANG);
568 if (! WIFEXITED(retstat)) {
570 code = WTERMSIG(retstat);
571 } else if (WEXITSTATUS(retstat) != 0) {
573 code = WEXITSTATUS(retstat);
576 for (dumper = dmptable; dumper < dmptable + inparallel;
578 if (pid == dumper->pid) {
579 who = stralloc(dumper->name);
583 if (dumper->chunker && pid == dumper->chunker->pid) {
584 who = stralloc(dumper->chunker->name);
585 dumper->chunker->pid = -1;
589 if (who == NULL && pid == taper_pid) {
590 who = stralloc("taper");
593 if(what != NULL && who == NULL) {
594 who = stralloc("unknown");
597 log_add(L_WARNING, _("%s pid %u exited with %s %d\n"), who,
598 (unsigned)pid, what, code);
599 g_printf(_("driver: %s pid %u exited with %s %d\n"), who,
600 (unsigned)pid, what, code);
604 } while (pid > 0 || wait_errno == EINTR);
609 } while ((errno != ECHILD) && (count != 0));
610 return (errno != ECHILD);
614 kill_children(int signal)
619 for(dumper = dmptable; dumper < dmptable + inparallel; dumper++) {
620 if (!dumper->down && dumper->pid > 1) {
621 g_printf(_("driver: sending signal %d to %s pid %u\n"), signal,
622 dumper->name, (unsigned)dumper->pid);
623 if (kill(dumper->pid, signal) == -1 && errno == ESRCH) {
625 dumper->chunker->pid = 0;
627 if (dumper->chunker && dumper->chunker->pid > 1) {
628 g_printf(_("driver: sending signal %d to %s pid %u\n"), signal,
629 dumper->chunker->name,
630 (unsigned)dumper->chunker->pid);
631 if (kill(dumper->chunker->pid, signal) == -1 &&
633 dumper->chunker->pid = 0;
640 g_printf(_("driver: sending signal %d to %s pid %u\n"), signal,
641 "taper", (unsigned)taper_pid);
642 if (kill(taper_pid, signal) == -1 && errno == ESRCH)
647 wait_for_children(void)
652 for(dumper = dmptable; dumper < dmptable + inparallel; dumper++) {
653 if (dumper->pid > 1 && dumper->fd >= 0) {
654 dumper_cmd(dumper, QUIT, NULL);
655 if (dumper->chunker && dumper->chunker->pid > 1 &&
656 dumper->chunker->fd >= 0)
657 chunker_cmd(dumper->chunker, QUIT, NULL);
662 if(taper_pid > 1 && taper > 0) {
663 taper_cmd(QUIT, NULL, NULL, 0, NULL);
666 if(wait_children(60) == 0)
669 kill_children(SIGHUP);
670 if(wait_children(60) == 0)
673 kill_children(SIGKILL);
674 if(wait_children(-1) == 0)
687 TapeAction result_tape_action;
689 result_tape_action = tape_action();
691 if (result_tape_action & TAPE_ACTION_NEW_TAPE) {
692 taper_state &= !TAPER_STATE_WAIT_FOR_TAPE;
693 taper_cmd(NEW_TAPE, NULL, NULL, 0, NULL);
694 } else if (result_tape_action & TAPE_ACTION_NO_NEW_TAPE) {
695 taper_state &= !TAPER_STATE_WAIT_FOR_TAPE;
696 taper_cmd(NO_NEW_TAPE, NULL, NULL, 0, NULL);
699 if (!degraded_mode && !taper_busy && !empty(tapeq) &&
700 (result_tape_action & TAPE_ACTION_START_A_FLUSH)) {
702 datestamp = sched(tapeq.head)->datestamp;
703 switch(conf_taperalgo) {
705 dp = dequeue_disk(&tapeq);
709 while (fit != NULL) {
710 extra_tapes = (fit->tape_splitsize > (off_t)0) ?
711 conf_runtapes - current_tape : 0;
712 if(sched(fit)->act_size <= (tape_left +
713 tape_length * (off_t)extra_tapes) &&
714 strcmp(sched(fit)->datestamp, datestamp) <= 0) {
722 if(dp) remove_disk(&tapeq, dp);
725 fit = dp = tapeq.head;
726 while (fit != NULL) {
727 if(sched(fit)->act_size > sched(dp)->act_size &&
728 strcmp(sched(fit)->datestamp, datestamp) <= 0) {
733 if(dp) remove_disk(&tapeq, dp);
735 case ALGO_LARGESTFIT:
737 while (fit != NULL) {
738 extra_tapes = (fit->tape_splitsize > (off_t)0) ?
739 conf_runtapes - current_tape : 0;
740 if(sched(fit)->act_size <=
741 (tape_left + tape_length * (off_t)extra_tapes) &&
742 (!dp || sched(fit)->act_size > sched(dp)->act_size) &&
743 strcmp(sched(fit)->datestamp, datestamp) <= 0) {
748 if(dp) remove_disk(&tapeq, dp);
754 remove_disk(&tapeq, dp);
757 if(!dp) { /* ALGO_SMALLEST, or default if nothing fit. */
758 if(conf_taperalgo != ALGO_SMALLEST) {
760 _("driver: startaflush: Using SMALLEST because nothing fit\n"));
762 fit = dp = tapeq.head;
763 while (fit != NULL) {
764 if(sched(fit)->act_size < sched(dp)->act_size &&
765 strcmp(sched(fit)->datestamp, datestamp) <= 0) {
770 if(dp) remove_disk(&tapeq, dp);
772 if(taper_ev_read == NULL) {
773 taper_ev_read = event_register((event_id_t)taper, EV_READFD,
774 handle_taper_result, NULL);
779 taper_input_error = NULL;
780 taper_tape_error = NULL;
781 taper_result = LAST_TOK;
782 taper_sendresult = 0;
783 taper_first_label = NULL;
785 taper_state &= !TAPER_STATE_DUMP_TO_TAPE;
787 qname = quote_string(dp->name);
788 taper_cmd(FILE_WRITE, dp, sched(dp)->destname, sched(dp)->level,
789 sched(dp)->datestamp);
790 g_fprintf(stderr,_("driver: startaflush: %s %s %s %lld %lld\n"),
791 taperalgo2str(conf_taperalgo), dp->host->hostname, qname,
792 (long long)sched(taper_disk)->act_size,
793 (long long)tape_left);
794 if(sched(dp)->act_size <= tape_left)
795 tape_left -= sched(dp)->act_size;
797 tape_left = (off_t)0;
800 error(_("FATAL: Taper marked busy and no work found."));
803 } else if(!taper_busy && taper_ev_read != NULL) {
804 event_release(taper_ev_read);
805 taper_ev_read = NULL;
815 /* first, check if host is too busy */
817 if(dp->host->inprogress >= dp->host->maxdumps) {
821 /* next, check conflict with other dumps on same spindle */
823 if(dp->spindle == -1) { /* but spindle -1 never conflicts by def. */
827 for(dp2 = dp->host->disks; dp2 != NULL; dp2 = dp2->hostnext)
828 if(dp2->inprogress && dp2->spindle == dp->spindle) {
840 disk_t *diskp, *delayed_diskp, *diskp_accept;
841 assignedhd_t **holdp=NULL, **holdp_accept;
842 const time_t now = time(NULL);
845 char *result_argv[MAX_ARGS+1];
850 int busy_dumpers = 0;
852 idle_reason = IDLE_NO_DUMPERS;
855 if(dumpers_ev_time != NULL) {
856 event_release(dumpers_ev_time);
857 dumpers_ev_time = NULL;
860 for(dumper = dmptable; dumper < (dmptable+inparallel); dumper++) {
866 for (dumper = dmptable; dumper < dmptable+inparallel; dumper++) {
868 if( dumper->busy || dumper->down) {
872 if (dumper->ev_read != NULL) {
873 event_release(dumper->ev_read);
874 dumper->ev_read = NULL;
878 * A potential problem with starting from the bottom of the dump time
879 * distribution is that a slave host will have both one of the shortest
880 * and one of the longest disks, so starting its shortest disk first will
881 * tie up the host and eliminate its longest disk from consideration the
882 * first pass through. This could cause a big delay in starting that long
883 * disk, which could drag out the whole night's dumps.
885 * While starting from the top of the dump time distribution solves the
886 * above problem, this turns out to be a bad idea, because the big dumps
887 * will almost certainly pack the holding disk completely, leaving no
888 * room for even one small dump to start. This ends up shutting out the
889 * small-end dumpers completely (they stay idle).
891 * The introduction of multiple simultaneous dumps to one host alleviates
892 * the biggest&smallest dumps problem: both can be started at the
898 delayed_diskp = NULL;
902 dumporder = getconf_str(CNF_DUMPORDER);
903 if(strlen(dumporder) > (size_t)(dumper-dmptable)) {
904 dumptype = dumporder[dumper-dmptable];
907 if(dumper-dmptable < 3)
913 for(diskp = rq->head; diskp != NULL; diskp = diskp->next) {
914 assert(diskp->host != NULL && sched(diskp) != NULL);
916 if (diskp->host->start_t > now) {
917 cur_idle = max(cur_idle, IDLE_START_WAIT);
918 if (delayed_diskp == NULL || sleep_time > diskp->host->start_t) {
919 delayed_diskp = diskp;
920 sleep_time = diskp->host->start_t;
922 } else if(diskp->start_t > now) {
923 cur_idle = max(cur_idle, IDLE_START_WAIT);
924 if (delayed_diskp == NULL || sleep_time > diskp->start_t) {
925 delayed_diskp = diskp;
926 sleep_time = diskp->start_t;
928 } else if (diskp->host->netif->curusage > 0 &&
929 sched(diskp)->est_kps > free_kps(diskp->host->netif)) {
930 cur_idle = max(cur_idle, IDLE_NO_BANDWIDTH);
931 } else if(sched(diskp)->no_space) {
932 cur_idle = max(cur_idle, IDLE_NO_DISKSPACE);
933 } else if (diskp->to_holdingdisk == HOLD_NEVER) {
934 cur_idle = max(cur_idle, IDLE_NO_HOLD);
936 find_diskspace(sched(diskp)->est_size, &cur_idle, NULL)) == NULL) {
937 cur_idle = max(cur_idle, IDLE_NO_DISKSPACE);
938 if (empty(tapeq) && busy_dumpers == 0) {
939 remove_disk(rq, diskp);
940 enqueue_disk(&directq, diskp);
942 } else if (client_constrained(diskp)) {
943 free_assignedhd(holdp);
944 cur_idle = max(cur_idle, IDLE_CLIENT_CONSTRAINED);
947 /* disk fits, dump it */
948 int accept = !diskp_accept;
951 case 's': accept = (sched(diskp)->est_size < sched(diskp_accept)->est_size);
953 case 'S': accept = (sched(diskp)->est_size > sched(diskp_accept)->est_size);
955 case 't': accept = (sched(diskp)->est_time < sched(diskp_accept)->est_time);
957 case 'T': accept = (sched(diskp)->est_time > sched(diskp_accept)->est_time);
959 case 'b': accept = (sched(diskp)->est_kps < sched(diskp_accept)->est_kps);
961 case 'B': accept = (sched(diskp)->est_kps > sched(diskp_accept)->est_kps);
963 default: log_add(L_WARNING, _("Unknown dumporder character \'%c\', using 's'.\n"),
965 accept = (sched(diskp)->est_size < sched(diskp_accept)->est_size);
970 if( !diskp_accept || !degraded_mode || diskp->priority >= diskp_accept->priority) {
971 if(holdp_accept) free_assignedhd(holdp_accept);
972 diskp_accept = diskp;
973 holdp_accept = holdp;
976 free_assignedhd(holdp);
980 free_assignedhd(holdp);
985 diskp = diskp_accept;
986 holdp = holdp_accept;
988 idle_reason = max(idle_reason, cur_idle);
991 * If we have no disk at this point, and there are disks that
992 * are delayed, then schedule a time event to call this dumper
993 * with the disk with the shortest delay.
995 if (diskp == NULL && delayed_diskp != NULL) {
996 assert(sleep_time > now);
998 dumpers_ev_time = event_register((event_id_t)sleep_time, EV_TIME,
999 handle_dumpers_time, &runq);
1001 } else if (diskp != NULL) {
1002 sched(diskp)->act_size = (off_t)0;
1003 allocate_bandwidth(diskp->host->netif, sched(diskp)->est_kps);
1004 sched(diskp)->activehd = assign_holdingdisk(holdp, diskp);
1006 sched(diskp)->destname = newstralloc(sched(diskp)->destname,
1007 sched(diskp)->holdp[0]->destname);
1008 diskp->host->inprogress++; /* host is now busy */
1009 diskp->inprogress = 1;
1010 sched(diskp)->dumper = dumper;
1011 sched(diskp)->timestamp = now;
1013 dumper->busy = 1; /* dumper is now busy */
1014 dumper->dp = diskp; /* link disk to dumper */
1015 remove_disk(rq, diskp); /* take it off the run queue */
1017 sched(diskp)->origsize = (off_t)-1;
1018 sched(diskp)->dumpsize = (off_t)-1;
1019 sched(diskp)->dumptime = (time_t)0;
1020 sched(diskp)->tapetime = (time_t)0;
1021 chunker = dumper->chunker;
1022 chunker->result = LAST_TOK;
1023 dumper->result = LAST_TOK;
1024 startup_chunk_process(chunker,chunker_program);
1025 chunker_cmd(chunker, START, (void *)driver_timestamp);
1026 chunker->dumper = dumper;
1027 chunker_cmd(chunker, PORT_WRITE, diskp);
1028 cmd = getresult(chunker->fd, 1, &result_argc, result_argv, MAX_ARGS+1);
1030 assignedhd_t **h=NULL;
1033 g_printf(_("driver: did not get PORT from %s for %s:%s\n"),
1034 chunker->name, diskp->host->hostname, diskp->name);
1037 deallocate_bandwidth(diskp->host->netif, sched(diskp)->est_kps);
1038 h = sched(diskp)->holdp;
1039 activehd = sched(diskp)->activehd;
1040 h[activehd]->used = 0;
1041 h[activehd]->disk->allocated_dumpers--;
1042 adjust_diskspace(diskp, DONE);
1043 delete_diskspace(diskp);
1044 diskp->host->inprogress--;
1045 diskp->inprogress = 0;
1046 sched(diskp)->dumper = NULL;
1049 sched(diskp)->dump_attempted++;
1050 free_serial_dp(diskp);
1051 if(sched(diskp)->dump_attempted < 2)
1052 enqueue_disk(rq, diskp);
1055 dumper->ev_read = event_register((event_id_t)dumper->fd, EV_READFD,
1056 handle_dumper_result, dumper);
1057 chunker->ev_read = event_register((event_id_t)chunker->fd, EV_READFD,
1058 handle_chunker_result, chunker);
1059 dumper->output_port = atoi(result_argv[2]);
1061 dumper_cmd(dumper, PORT_DUMP, diskp);
1063 diskp->host->start_t = now + 15;
1069 * This gets called when a dumper is delayed for some reason. It may
1070 * be because a disk has a delayed start, or amanda is constrained
1071 * by network or disk limits.
1075 handle_dumpers_time(
1078 disklist_t *runq = cookie;
1079 event_release(dumpers_ev_time);
1080 dumpers_ev_time = NULL;
1081 start_some_dumps(runq);
1092 g_printf(_("dump of driver schedule %s:\n--------\n"), str);
1094 for(dp = qp->head; dp != NULL; dp = dp->next) {
1095 qname = quote_string(dp->name);
1096 g_printf(" %-20s %-25s lv %d t %5lu s %lld p %d\n",
1097 dp->host->hostname, qname, sched(dp)->level,
1098 sched(dp)->est_time,
1099 (long long)sched(dp)->est_size, sched(dp)->priority);
1102 g_printf("--------\n");
1106 start_degraded_mode(
1107 /*@keep@*/ disklist_t *queuep)
1111 off_t est_full_size;
1114 newq.head = newq.tail = 0;
1116 dump_schedule(queuep, _("before start degraded mode"));
1118 est_full_size = (off_t)0;
1119 while(!empty(*queuep)) {
1120 dp = dequeue_disk(queuep);
1122 qname = quote_string(dp->name);
1123 if(sched(dp)->level != 0)
1124 /* go ahead and do the disk as-is */
1125 enqueue_disk(&newq, dp);
1127 if (reserved_space + est_full_size + sched(dp)->est_size
1128 <= total_disksize) {
1129 enqueue_disk(&newq, dp);
1130 est_full_size += sched(dp)->est_size;
1132 else if(sched(dp)->degr_level != -1) {
1133 sched(dp)->level = sched(dp)->degr_level;
1134 sched(dp)->dumpdate = sched(dp)->degr_dumpdate;
1135 sched(dp)->est_nsize = sched(dp)->degr_nsize;
1136 sched(dp)->est_csize = sched(dp)->degr_csize;
1137 sched(dp)->est_time = sched(dp)->degr_time;
1138 sched(dp)->est_kps = sched(dp)->degr_kps;
1139 enqueue_disk(&newq, dp);
1142 log_add(L_FAIL,_("%s %s %s %d [can't switch to incremental dump]"),
1143 dp->host->hostname, qname, sched(dp)->datestamp,
1150 /*@i@*/ *queuep = newq;
1153 dump_schedule(queuep, _("after start degraded mode"));
1158 continue_port_dumps(void)
1162 int active_dumpers=0, busy_dumpers=0, i;
1165 /* First we try to grant diskspace to some dumps waiting for it. */
1166 for( dp = roomq.head; dp; dp = ndp ) {
1168 /* find last holdingdisk used by this dump */
1169 for( i = 0, h = sched(dp)->holdp; h[i+1]; i++ ) {
1170 (void)h; /* Quiet lint */
1172 /* find more space */
1173 h = find_diskspace( sched(dp)->est_size - sched(dp)->act_size,
1174 &active_dumpers, h[i] );
1176 for(dumper = dmptable; dumper < dmptable + inparallel &&
1177 dumper->dp != dp; dumper++) {
1178 (void)dp; /* Quiet lint */
1180 assert( dumper < dmptable + inparallel );
1181 sched(dp)->activehd = assign_holdingdisk( h, dp );
1182 chunker_cmd( dumper->chunker, CONTINUE, dp );
1184 remove_disk( &roomq, dp );
1188 /* So for some disks there is less holding diskspace available than
1189 * was asked for. Possible reasons are
1190 * a) diskspace has been allocated for other dumps which are
1191 * still running or already being written to tape
1192 * b) all other dumps have been suspended due to lack of diskspace
1193 * c) this dump doesn't fit on all the holding disks
1194 * Case a) is not a problem. We just wait for the diskspace to
1195 * be freed by moving the current disk to a queue.
1196 * If case b) occurs, we have a deadlock situation. We select
1197 * a dump from the queue to be aborted and abort it. It will
1198 * be retried later dumping to disk.
1199 * If case c) is detected, the dump is aborted. Next time
1200 * it will be dumped directly to tape. Actually, case c is a special
1201 * manifestation of case b) where only one dumper is busy.
1203 for(dp=NULL, dumper = dmptable; dumper < (dmptable+inparallel); dumper++) {
1204 if( dumper->busy ) {
1206 if( !find_disk(&roomq, dumper->dp) ) {
1209 sched(dp)->est_size > sched(dumper->dp)->est_size ) {
1214 if((dp != NULL) && (active_dumpers == 0) && (busy_dumpers > 0) &&
1215 ((!taper_busy && empty(tapeq)) || degraded_mode) &&
1216 pending_aborts == 0 ) { /* not case a */
1217 if( busy_dumpers == 1 ) { /* case c */
1218 sched(dp)->no_space = 1;
1221 /* At this time, dp points to the dump with the smallest est_size.
1222 * We abort that dump, hopefully not wasting too much time retrying it.
1224 remove_disk( &roomq, dp );
1225 chunker_cmd( sched(dp)->dumper->chunker, ABORT, NULL);
1226 dumper_cmd( sched(dp)->dumper, ABORT, NULL );
1233 handle_taper_result(
1239 char *result_argv[MAX_ARGS+1];
1242 (void)cookie; /* Quiet unused parameter warning */
1244 assert(cookie == NULL);
1250 cmd = getresult(taper, 1, &result_argc, result_argv, MAX_ARGS+1);
1254 case FAILED: /* FAILED <handle> INPUT-* TAPE-* <input err mesg> <tape err mesg> */
1255 if(result_argc != 6) {
1256 error(_("error: [taper FAILED result_argc != 6: %d"), result_argc);
1260 dp = serial2disk(result_argv[2]);
1261 assert(dp == taper_disk);
1263 free_serial(result_argv[2]);
1265 qname = quote_string(dp->name);
1266 g_printf(_("driver: finished-cmd time %s taper wrote %s:%s\n"),
1267 walltime_str(curclock()), dp->host->hostname, qname);
1271 if (strcmp(result_argv[3], "INPUT-ERROR") == 0) {
1272 taper_input_error = stralloc(result_argv[5]);
1274 if (strcmp(result_argv[4], "TAPE-ERROR") == 0) {
1275 taper_tape_error = stralloc(result_argv[6]);
1282 case PARTIAL: /* PARTIAL <handle> INPUT-* TAPE-* <stat mess> <input err mesg> <tape err mesg>*/
1283 case DONE: /* DONE <handle> INPUT-GOOD TAPE-GOOD <stat mess> <input err mesg> <tape err mesg> */
1284 if(result_argc != 7) {
1285 error(_("error: [taper PARTIAL result_argc != 7: %d"), result_argc);
1289 dp = serial2disk(result_argv[2]);
1290 assert(dp == taper_disk);
1292 free_serial(result_argv[2]);
1294 g_printf(_("driver: finished-cmd time %s taper wrote %s:%s\n"),
1295 walltime_str(curclock()), dp->host->hostname, dp->name);
1298 if (strcmp(result_argv[3], "INPUT-ERROR") == 0) {
1299 taper_input_error = stralloc(result_argv[5]);
1301 if (strcmp(result_argv[4], "TAPE-ERROR") == 0) {
1302 taper_tape_error = stralloc(result_argv[6]);
1309 case PARTDONE: /* PARTDONE <handle> <label> <fileno> <stat> */
1310 dp = serial2disk(result_argv[2]);
1311 assert(dp == taper_disk);
1312 if (result_argc != 6) {
1313 error(_("error [taper PARTDONE result_argc != 5: %d]"),
1317 if (!taper_first_label) {
1318 taper_first_label = stralloc(result_argv[3]);
1319 taper_first_fileno = OFF_T_ATOI(result_argv[4]);
1321 taper_written = OFF_T_ATOI(result_argv[5]);
1322 if (taper_written > sched(taper_disk)->act_size)
1323 sched(taper_disk)->act_size = taper_written;
1327 case REQUEST_NEW_TAPE: /* REQUEST-NEW-TAPE */
1328 if (result_argc != 2) {
1329 error(_("error [taper REQUEST_NEW_TAPE result_argc != 2: %d]"),
1333 taper_state &= !TAPER_STATE_TAPE_STARTED;
1335 if (current_tape >= conf_runtapes) {
1336 taper_cmd(NO_NEW_TAPE, NULL, NULL, 0, NULL);
1338 _("Out of tapes; going into degraded mode."));
1339 start_degraded_mode(&runq);
1341 TapeAction result_tape_action;
1343 taper_state |= TAPER_STATE_WAIT_FOR_TAPE;
1344 result_tape_action = tape_action();
1345 if (result_tape_action & TAPE_ACTION_NEW_TAPE) {
1346 taper_cmd(NEW_TAPE, NULL, NULL, 0, NULL);
1347 taper_state &= !TAPER_STATE_WAIT_FOR_TAPE;
1348 } else if (result_tape_action & TAPE_ACTION_NO_NEW_TAPE) {
1349 taper_cmd(NO_NEW_TAPE, NULL, NULL, 0, NULL);
1350 taper_state &= !TAPER_STATE_WAIT_FOR_TAPE;
1355 case NEW_TAPE: /* NEW-TAPE <handle> <label> */
1356 if (result_argc != 3) {
1357 error(_("error [taper NEW_TAPE result_argc != 3: %d]"),
1362 /* Update our tape counter and reset tape_left */
1364 tape_left = tape_length;
1365 taper_state |= TAPER_STATE_TAPE_STARTED;
1368 case NO_NEW_TAPE: /* NO-NEW-TAPE <handle> */
1369 if (result_argc != 2) {
1370 error(_("error [taper NO_NEW_TAPE result_argc != 2: %d]"),
1376 case DUMPER_STATUS: /* DUMPER-STATUS <handle> */
1377 if (result_argc != 2) {
1378 error(_("error [taper NO_NEW_TAPE result_argc != 2: %d]"),
1382 if (taper_dumper->result == LAST_TOK) {
1383 taper_sendresult = 1;
1385 taper_cmd(taper_dumper->result, NULL, NULL, 0, NULL);
1389 case TAPE_ERROR: /* TAPE-ERROR <handle> <err mess> */
1390 dp = serial2disk(result_argv[2]);
1392 free_serial(result_argv[2]);
1393 g_printf(_("driver: finished-cmd time %s taper wrote %s:%s\n"),
1394 walltime_str(curclock()), dp->host->hostname, dp->name);
1396 log_add(L_WARNING, _("Taper error: %s"), result_argv[3]);
1397 taper_tape_error = stralloc(result_argv[3]);
1402 log_add(L_WARNING, _("Taper protocol error"));
1403 taper_tape_error = stralloc("BOGUS");
1406 * Since we received a taper error, we can't send anything more
1407 * to the taper. Go into degraded mode to try to get everthing
1408 * onto disk. Later, these dumps can be flushed to a new tape.
1409 * The tape queue is zapped so that it appears empty in future
1410 * checks. If there are dumps waiting for diskspace to be freed,
1415 _("going into degraded mode because of taper component error."));
1416 start_degraded_mode(&runq);
1418 tapeq.head = tapeq.tail = NULL;
1420 if(taper_ev_read != NULL) {
1421 event_release(taper_ev_read);
1422 taper_ev_read = NULL;
1424 if(cmd != TAPE_ERROR) aclose(taper);
1430 error(_("driver received unexpected token (%s) from taper"),
1435 if (taper_result != LAST_TOK) {
1437 if (taper_dumper->result != LAST_TOK) {
1438 // Dumper already returned it's result
1439 dumper_taper_result(taper_disk);
1442 file_taper_result(taper_disk);
1446 } while(areads_dataready(taper));
1454 if (taper_result == DONE) {
1455 update_info_taper(dp, taper_first_label, taper_first_fileno,
1459 sched(dp)->taper_attempted += 1;
1461 if (taper_input_error) {
1462 g_printf("driver: taper failed %s %s: %s\n",
1463 dp->host->hostname, dp->name, taper_input_error);
1464 if (strcmp(sched(dp)->datestamp, driver_timestamp) == 0) {
1465 if(sched(dp)->taper_attempted >= 2) {
1466 log_add(L_FAIL, _("%s %s %s %d [too many taper retries after holding disk error: %s]"),
1467 dp->host->hostname, dp->name, sched(dp)->datestamp,
1468 sched(dp)->level, taper_input_error);
1469 g_printf("driver: taper failed %s %s, too many taper retry after holding disk error\n",
1470 dp->host->hostname, dp->name);
1471 amfree(sched(dp)->destname);
1472 amfree(sched(dp)->dumpdate);
1473 amfree(sched(dp)->degr_dumpdate);
1474 amfree(sched(dp)->datestamp);
1477 log_add(L_INFO, _("%s %s %s %d [Will retry dump because of holding disk error: %s]"),
1478 dp->host->hostname, dp->name, sched(dp)->datestamp,
1479 sched(dp)->level, taper_input_error);
1480 g_printf("driver: taper will retry %s %s because of holding disk error\n",
1481 dp->host->hostname, dp->name);
1482 if (dp->to_holdingdisk != HOLD_REQUIRED) {
1483 dp->to_holdingdisk = HOLD_NEVER;
1484 sched(dp)->dump_attempted -= 1;
1485 headqueue_disk(&directq, dp);
1487 amfree(sched(dp)->destname);
1488 amfree(sched(dp)->dumpdate);
1489 amfree(sched(dp)->degr_dumpdate);
1490 amfree(sched(dp)->datestamp);
1495 amfree(sched(dp)->destname);
1496 amfree(sched(dp)->dumpdate);
1497 amfree(sched(dp)->degr_dumpdate);
1498 amfree(sched(dp)->datestamp);
1501 } else if (taper_tape_error) {
1502 if(sched(dp)->taper_attempted >= 2) {
1503 log_add(L_FAIL, _("%s %s %s %d [too many taper retries]"),
1504 dp->host->hostname, dp->name, sched(dp)->datestamp,
1506 g_printf("driver: taper failed %s %s, too many taper retry\n",
1507 dp->host->hostname, dp->name);
1508 amfree(sched(dp)->destname);
1509 amfree(sched(dp)->dumpdate);
1510 amfree(sched(dp)->degr_dumpdate);
1511 amfree(sched(dp)->datestamp);
1514 g_printf("driver: taper will retry %s %s\n",
1515 dp->host->hostname, dp->name);
1516 /* Re-insert into taper queue. */
1517 headqueue_disk(&tapeq, dp);
1520 delete_diskspace(dp);
1521 amfree(sched(dp)->destname);
1522 amfree(sched(dp)->dumpdate);
1523 amfree(sched(dp)->degr_dumpdate);
1524 amfree(sched(dp)->datestamp);
1529 taper_input_error = NULL;
1530 taper_tape_error = NULL;
1533 /* continue with those dumps waiting for diskspace */
1534 continue_port_dumps();
1535 start_some_dumps(&runq);
1540 dumper_taper_result(
1547 dumper = sched(dp)->dumper;
1550 if(dumper->result == DONE && taper_result == DONE) {
1551 update_info_dumper(dp, sched(dp)->origsize,
1552 sched(dp)->dumpsize, sched(dp)->dumptime);
1553 update_info_taper(dp, taper_first_label, taper_first_fileno,
1555 qname = quote_string(dp->name); /*quote to take care of spaces*/
1557 log_add(L_STATS, _("estimate %s %s %s %d [sec %ld nkb %lld ckb %lld kps %lu]"),
1558 dp->host->hostname, qname, sched(dp)->datestamp,
1560 sched(dp)->est_time, (long long)sched(dp)->est_nsize,
1561 (long long)sched(dp)->est_csize,
1562 sched(dp)->est_kps);
1565 update_failed_dump_to_tape(dp);
1568 is_partial = dumper->result != DONE || taper_result != DONE;
1570 sched(dp)->dump_attempted += 1;
1571 sched(dp)->taper_attempted += 1;
1573 if((dumper->result != DONE || taper_result != DONE) &&
1574 sched(dp)->dump_attempted <= 1 &&
1575 sched(dp)->taper_attempted <= 1) {
1576 enqueue_disk(&directq, dp);
1579 if(dumper->ev_read != NULL) {
1580 event_release(dumper->ev_read);
1581 dumper->ev_read = NULL;
1583 if(taper_ev_read != NULL) {
1584 event_release(taper_ev_read);
1585 taper_ev_read = NULL;
1588 taper_input_error = NULL;
1589 taper_tape_error = NULL;
1591 dp->host->inprogress -= 1;
1593 deallocate_bandwidth(dp->host->netif, sched(dp)->est_kps);
1602 for(dumper = dmptable; dumper < dmptable+inparallel; dumper++)
1603 if(!dumper->busy && !dumper->down) return dumper;
1609 dumper_chunker_result(
1614 assignedhd_t **h=NULL;
1621 dumper = sched(dp)->dumper;
1622 chunker = dumper->chunker;
1626 h = sched(dp)->holdp;
1627 activehd = sched(dp)->activehd;
1629 if(dumper->result == DONE && chunker->result == DONE) {
1630 update_info_dumper(dp, sched(dp)->origsize,
1631 sched(dp)->dumpsize, sched(dp)->dumptime);
1632 qname = quote_string(dp->name);/*quote to take care of spaces*/
1634 log_add(L_STATS, _("estimate %s %s %s %d [sec %ld nkb %lld ckb %lld kps %lu]"),
1635 dp->host->hostname, qname, sched(dp)->datestamp,
1637 sched(dp)->est_time, (long long)sched(dp)->est_nsize,
1638 (long long)sched(dp)->est_csize,
1639 sched(dp)->est_kps);
1643 deallocate_bandwidth(dp->host->netif, sched(dp)->est_kps);
1645 is_partial = dumper->result != DONE || chunker->result != DONE;
1646 rename_tmp_holding(sched(dp)->destname, !is_partial);
1649 for( i = 0, h = sched(dp)->holdp; i < activehd; i++ ) {
1650 dummy += h[i]->used;
1653 size = holding_file_size(sched(dp)->destname, 0);
1654 h[activehd]->used = size - dummy;
1655 h[activehd]->disk->allocated_dumpers--;
1656 adjust_diskspace(dp, DONE);
1658 sched(dp)->dump_attempted += 1;
1660 if((dumper->result != DONE || chunker->result != DONE) &&
1661 sched(dp)->dump_attempted <= 1) {
1662 delete_diskspace(dp);
1663 if (sched(dp)->no_space) {
1664 enqueue_disk(&directq, dp);
1666 enqueue_disk(&runq, dp);
1669 else if(size > (off_t)DISK_BLOCK_KB) {
1670 enqueue_disk(&tapeq, dp);
1673 delete_diskspace(dp);
1677 dp->host->inprogress -= 1;
1680 waitpid(chunker->pid, NULL, 0 );
1681 aclose(chunker->fd);
1686 if (chunker->result == ABORT_FINISHED)
1688 continue_port_dumps();
1690 * Wakeup any dumpers that are sleeping because of network
1691 * or disk constraints.
1693 start_some_dumps(&runq);
1699 handle_dumper_result(
1702 /*static int pending_aborts = 0;*/
1703 dumper_t *dumper = cookie;
1708 char *result_argv[MAX_ARGS+1];
1710 assert(dumper != NULL);
1713 assert(sched(dp) != NULL);
1718 cmd = getresult(dumper->fd, 1, &result_argc, result_argv, MAX_ARGS+1);
1721 /* result_argv[2] always contains the serial number */
1722 sdp = serial2disk(result_argv[2]);
1724 error(_("Invalid serial number %s"), result_argv[2]);
1725 g_assert_not_reached();
1729 qname = quote_string(dp->name);
1732 case DONE: /* DONE <handle> <origsize> <dumpsize> <dumptime> <errstr> */
1733 if(result_argc != 6) {
1734 error(_("error [dumper DONE result_argc != 6: %d]"), result_argc);
1738 sched(dp)->origsize = OFF_T_ATOI(result_argv[3]);
1739 sched(dp)->dumptime = TIME_T_ATOI(result_argv[5]);
1741 g_printf(_("driver: finished-cmd time %s %s dumped %s:%s\n"),
1742 walltime_str(curclock()), dumper->name,
1743 dp->host->hostname, qname);
1746 dumper->result = cmd;
1750 case TRYAGAIN: /* TRY-AGAIN <handle> <errstr> */
1752 * Requeue this disk, and fall through to the FAILED
1755 if(sched(dp)->dump_attempted) {
1756 log_add(L_FAIL, _("%s %s %s %d [too many dumper retry: %s]"),
1757 dp->host->hostname, dp->name, sched(dp)->datestamp,
1758 sched(dp)->level, result_argv[3]);
1759 g_printf(_("driver: dump failed %s %s %s, too many dumper retry: %s\n"),
1760 result_argv[2], dp->host->hostname, dp->name,
1764 case FAILED: /* FAILED <handle> <errstr> */
1765 /*free_serial(result_argv[2]);*/
1766 dumper->result = cmd;
1769 case ABORT_FINISHED: /* ABORT-FINISHED <handle> */
1771 * We sent an ABORT from the NO-ROOM case because this dump
1772 * wasn't going to fit onto the holding disk. We now need to
1773 * clean up the remains of this image, and try to finish
1774 * other dumps that are waiting on disk space.
1776 assert(pending_aborts);
1777 /*free_serial(result_argv[2]);*/
1778 dumper->result = cmd;
1782 /* either EOF or garbage from dumper. Turn it off */
1783 log_add(L_WARNING, _("%s pid %ld is messed up, ignoring it.\n"),
1784 dumper->name, (long)dumper->pid);
1785 if (dumper->ev_read) {
1786 event_release(dumper->ev_read);
1787 dumper->ev_read = NULL;
1791 dumper->down = 1; /* mark it down so it isn't used again */
1793 /* if it was dumping something, zap it and try again */
1794 if(sched(dp)->dump_attempted) {
1795 log_add(L_FAIL, _("%s %s %s %d [%s died]"),
1796 dp->host->hostname, qname, sched(dp)->datestamp,
1797 sched(dp)->level, dumper->name);
1800 log_add(L_WARNING, _("%s died while dumping %s:%s lev %d."),
1801 dumper->name, dp->host->hostname, qname,
1805 dumper->result = cmd;
1813 /* send the dumper result to the chunker */
1814 if (dumper->chunker) {
1815 if (dumper->chunker->down == 0 && dumper->chunker->fd != -1 &&
1816 dumper->chunker->result == LAST_TOK) {
1818 chunker_cmd(dumper->chunker, DONE, dp);
1821 chunker_cmd(dumper->chunker, FAILED, dp);
1824 if( dumper->result != LAST_TOK &&
1825 dumper->chunker->result != LAST_TOK)
1826 dumper_chunker_result(dp);
1827 } else { /* send the dumper result to the taper */
1828 if (taper_sendresult) {
1830 taper_cmd(DONE, driver_timestamp, NULL, 0, NULL);
1832 taper_cmd(FAILED, driver_timestamp, NULL, 0, NULL);
1834 taper_sendresult = 0;
1837 if (taper_dumper && taper_result != LAST_TOK) {
1838 dumper_taper_result(dp);
1840 } while(areads_dataready(dumper->fd));
1845 handle_chunker_result(
1848 /*static int pending_aborts = 0;*/
1849 chunker_t *chunker = cookie;
1850 assignedhd_t **h=NULL;
1855 char *result_argv[MAX_ARGS+1];
1860 assert(chunker != NULL);
1861 dumper = chunker->dumper;
1862 assert(dumper != NULL);
1865 assert(sched(dp) != NULL);
1866 assert(sched(dp)->destname != NULL);
1867 assert(dp != NULL && sched(dp) != NULL && sched(dp)->destname);
1869 if(dp && sched(dp) && sched(dp)->holdp) {
1870 h = sched(dp)->holdp;
1871 activehd = sched(dp)->activehd;
1878 cmd = getresult(chunker->fd, 1, &result_argc, result_argv, MAX_ARGS+1);
1881 /* result_argv[2] always contains the serial number */
1882 sdp = serial2disk(result_argv[2]);
1884 error(_("Invalid serial number %s"), result_argv[2]);
1885 g_assert_not_reached();
1891 case PARTIAL: /* PARTIAL <handle> <dumpsize> <errstr> */
1892 case DONE: /* DONE <handle> <dumpsize> <errstr> */
1893 if(result_argc != 4) {
1894 error(_("error [chunker %s result_argc != 4: %d]"), cmdstr[cmd],
1898 /*free_serial(result_argv[2]);*/
1900 sched(dp)->dumpsize = (off_t)atof(result_argv[3]);
1902 qname = quote_string(dp->name);
1903 g_printf(_("driver: finished-cmd time %s %s chunked %s:%s\n"),
1904 walltime_str(curclock()), chunker->name,
1905 dp->host->hostname, qname);
1909 event_release(chunker->ev_read);
1911 chunker->result = cmd;
1915 case TRYAGAIN: /* TRY-AGAIN <handle> <errstr> */
1916 event_release(chunker->ev_read);
1918 chunker->result = cmd;
1921 case FAILED: /* FAILED <handle> <errstr> */
1922 /*free_serial(result_argv[2]);*/
1924 event_release(chunker->ev_read);
1926 chunker->result = cmd;
1930 case NO_ROOM: /* NO-ROOM <handle> <missing_size> */
1931 if (!h || activehd < 0) { /* should never happen */
1932 error(_("!h || activehd < 0"));
1935 h[activehd]->used -= OFF_T_ATOI(result_argv[3]);
1936 h[activehd]->reserved -= OFF_T_ATOI(result_argv[3]);
1937 h[activehd]->disk->allocated_space -= OFF_T_ATOI(result_argv[3]);
1938 h[activehd]->disk->disksize -= OFF_T_ATOI(result_argv[3]);
1941 case RQ_MORE_DISK: /* RQ-MORE-DISK <handle> */
1942 if (!h || activehd < 0) { /* should never happen */
1943 error(_("!h || activehd < 0"));
1946 h[activehd]->disk->allocated_dumpers--;
1947 h[activehd]->used = h[activehd]->reserved;
1948 if( h[++activehd] ) { /* There's still some allocated space left.
1949 * Tell the dumper about it. */
1950 sched(dp)->activehd++;
1951 chunker_cmd( chunker, CONTINUE, dp );
1952 } else { /* !h[++activehd] - must allocate more space */
1953 sched(dp)->act_size = sched(dp)->est_size; /* not quite true */
1954 sched(dp)->est_size = (sched(dp)->act_size/(off_t)20) * (off_t)21; /* +5% */
1955 sched(dp)->est_size = am_round(sched(dp)->est_size, (off_t)DISK_BLOCK_KB);
1956 if (sched(dp)->est_size < sched(dp)->act_size + 2*DISK_BLOCK_KB)
1957 sched(dp)->est_size += 2 * DISK_BLOCK_KB;
1958 h = find_diskspace( sched(dp)->est_size - sched(dp)->act_size,
1962 /* No diskspace available. The reason for this will be
1963 * determined in continue_port_dumps(). */
1964 enqueue_disk( &roomq, dp );
1965 continue_port_dumps();
1967 /* OK, allocate space for disk and have chunker continue */
1968 sched(dp)->activehd = assign_holdingdisk( h, dp );
1969 chunker_cmd( chunker, CONTINUE, dp );
1975 case ABORT_FINISHED: /* ABORT-FINISHED <handle> */
1977 * We sent an ABORT from the NO-ROOM case because this dump
1978 * wasn't going to fit onto the holding disk. We now need to
1979 * clean up the remains of this image, and try to finish
1980 * other dumps that are waiting on disk space.
1982 /*assert(pending_aborts);*/
1984 /*free_serial(result_argv[2]);*/
1986 event_release(chunker->ev_read);
1988 chunker->result = cmd;
1993 /* either EOF or garbage from chunker. Turn it off */
1994 log_add(L_WARNING, _("%s pid %ld is messed up, ignoring it.\n"),
1995 chunker->name, (long)chunker->pid);
1998 /* if it was dumping something, zap it and try again */
1999 if (!h || activehd < 0) { /* should never happen */
2000 error(_("!h || activehd < 0"));
2003 qname = quote_string(dp->name);
2004 if(sched(dp)->dump_attempted) {
2005 log_add(L_FAIL, _("%s %s %s %d [%s died]"),
2006 dp->host->hostname, qname, sched(dp)->datestamp,
2007 sched(dp)->level, chunker->name);
2010 log_add(L_WARNING, _("%s died while dumping %s:%s lev %d."),
2011 chunker->name, dp->host->hostname, qname,
2018 event_release(chunker->ev_read);
2020 chunker->result = cmd;
2028 if(chunker->result != LAST_TOK && chunker->dumper->result != LAST_TOK)
2029 dumper_chunker_result(dp);
2031 } while(areads_dataready(chunker->fd));
2042 char *hostname, *diskname, *datestamp;
2046 char *inpline = NULL;
2052 char *qdestname = NULL;
2054 tq.head = tq.tail = NULL;
2056 for(line = 0; (inpline = agets(stdin)) != NULL; free(inpline)) {
2058 if (inpline[0] == '\0')
2064 skip_whitespace(s, ch); /* find the command */
2066 error(_("flush line %d: syntax error (no command)"), line);
2070 skip_non_whitespace(s, ch);
2073 if(strcmp(command,"ENDFLUSH") == 0) {
2077 if(strcmp(command,"FLUSH") != 0) {
2078 error(_("flush line %d: syntax error (%s != FLUSH)"), line, command);
2082 skip_whitespace(s, ch); /* find the hostname */
2084 error(_("flush line %d: syntax error (no hostname)"), line);
2088 skip_non_whitespace(s, ch);
2091 skip_whitespace(s, ch); /* find the diskname */
2093 error(_("flush line %d: syntax error (no diskname)"), line);
2097 skip_quoted_string(s, ch);
2098 s[-1] = '\0'; /* terminate the disk name */
2099 diskname = unquote_string(qname);
2101 skip_whitespace(s, ch); /* find the datestamp */
2103 error(_("flush line %d: syntax error (no datestamp)"), line);
2107 skip_non_whitespace(s, ch);
2110 skip_whitespace(s, ch); /* find the level number */
2111 if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
2112 error(_("flush line %d: syntax error (bad level)"), line);
2115 skip_integer(s, ch);
2117 skip_whitespace(s, ch); /* find the filename */
2119 error(_("flush line %d: syntax error (no filename)"), line);
2123 skip_quoted_string(s, ch);
2125 destname = unquote_string(qdestname);
2127 holding_file_get_dumpfile(destname, &file);
2128 if( file.type != F_DUMPFILE) {
2129 if( file.type != F_CONT_DUMPFILE )
2130 log_add(L_INFO, _("%s: ignoring cruft file."), destname);
2136 if(strcmp(hostname, file.name) != 0 ||
2137 strcmp(diskname, file.disk) != 0 ||
2138 strcmp(datestamp, file.datestamp) != 0) {
2139 log_add(L_INFO, _("disk %s:%s not consistent with file %s"),
2140 hostname, diskname, destname);
2147 dp = lookup_disk(file.name, file.disk);
2150 log_add(L_INFO, _("%s: disk %s:%s not in database, skipping it."),
2151 destname, file.name, file.disk);
2156 if(file.dumplevel < 0 || file.dumplevel > 9) {
2157 log_add(L_INFO, _("%s: ignoring file with bogus dump level %d."),
2158 destname, file.dumplevel);
2163 if (holding_file_size(destname,1) <= 0) {
2164 log_add(L_INFO, "%s: removing file with no data.", destname);
2165 holding_file_unlink(destname);
2170 dp1 = (disk_t *)alloc(SIZEOF(disk_t));
2172 dp1->next = dp1->prev = NULL;
2174 /* add it to the flushhost list */
2176 flushhost = alloc(SIZEOF(am_host_t));
2177 flushhost->next = NULL;
2178 flushhost->hostname = stralloc("FLUSHHOST");
2179 flushhost->up = NULL;
2180 flushhost->features = NULL;
2182 dp1->hostnext = flushhost->disks;
2183 flushhost->disks = dp1;
2185 sp = (sched_t *) alloc(SIZEOF(sched_t));
2186 sp->destname = destname;
2187 sp->level = file.dumplevel;
2188 sp->dumpdate = NULL;
2189 sp->degr_dumpdate = NULL;
2190 sp->datestamp = stralloc(file.datestamp);
2191 sp->est_nsize = (off_t)0;
2192 sp->est_csize = (off_t)0;
2196 sp->degr_level = -1;
2197 sp->dump_attempted = 0;
2198 sp->taper_attempted = 0;
2199 sp->act_size = holding_file_size(destname, 0);
2200 sp->holdp = build_diskspace(destname);
2201 if(sp->holdp == NULL) continue;
2203 sp->timestamp = (time_t)0;
2205 dp1->up = (char *)sp;
2207 enqueue_disk(&tq, dp1);
2220 int level, line, priority;
2221 char *dumpdate, *degr_dumpdate;
2223 time_t time, degr_time;
2224 time_t *time_p = &time;
2225 time_t *degr_time_p = °r_time;
2226 off_t nsize, csize, degr_nsize, degr_csize;
2227 unsigned long kps, degr_kps;
2228 char *hostname, *features, *diskname, *datestamp, *inpline = NULL;
2232 off_t flush_size = (off_t)0;
2237 long long degr_nsize_;
2238 long long degr_csize_;
2240 (void)cookie; /* Quiet unused parameter warning */
2242 event_release(schedule_ev_read);
2244 /* read schedule from stdin */
2246 for(line = 0; (inpline = agets(stdin)) != NULL; free(inpline)) {
2247 if (inpline[0] == '\0')
2254 skip_whitespace(s, ch); /* find the command */
2256 error(_("schedule line %d: syntax error (no command)"), line);
2260 skip_non_whitespace(s, ch);
2263 if(strcmp(command,"DUMP") != 0) {
2264 error(_("schedule line %d: syntax error (%s != DUMP)"), line, command);
2268 skip_whitespace(s, ch); /* find the host name */
2270 error(_("schedule line %d: syntax error (no host name)"), line);
2274 skip_non_whitespace(s, ch);
2277 skip_whitespace(s, ch); /* find the feature list */
2279 error(_("schedule line %d: syntax error (no feature list)"), line);
2283 skip_non_whitespace(s, ch);
2286 skip_whitespace(s, ch); /* find the disk name */
2288 error(_("schedule line %d: syntax error (no disk name)"), line);
2292 skip_quoted_string(s, ch);
2293 s[-1] = '\0'; /* terminate the disk name */
2294 diskname = unquote_string(qname);
2296 skip_whitespace(s, ch); /* find the datestamp */
2298 error(_("schedule line %d: syntax error (no datestamp)"), line);
2302 skip_non_whitespace(s, ch);
2305 skip_whitespace(s, ch); /* find the priority number */
2306 if(ch == '\0' || sscanf(s - 1, "%d", &priority) != 1) {
2307 error(_("schedule line %d: syntax error (bad priority)"), line);
2310 skip_integer(s, ch);
2312 skip_whitespace(s, ch); /* find the level number */
2313 if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
2314 error(_("schedule line %d: syntax error (bad level)"), line);
2317 skip_integer(s, ch);
2319 skip_whitespace(s, ch); /* find the dump date */
2321 error(_("schedule line %d: syntax error (bad dump date)"), line);
2325 skip_non_whitespace(s, ch);
2328 skip_whitespace(s, ch); /* find the native size */
2330 if(ch == '\0' || sscanf(s - 1, "%lld", &nsize_) != 1) {
2331 error(_("schedule line %d: syntax error (bad nsize)"), line);
2334 nsize = (off_t)nsize_;
2335 skip_integer(s, ch);
2337 skip_whitespace(s, ch); /* find the compressed size */
2339 if(ch == '\0' || sscanf(s - 1, "%lld", &csize_) != 1) {
2340 error(_("schedule line %d: syntax error (bad csize)"), line);
2343 csize = (off_t)csize_;
2344 skip_integer(s, ch);
2346 skip_whitespace(s, ch); /* find the time number */
2347 if(ch == '\0' || sscanf(s - 1, "%lld", &time_) != 1) {
2348 error(_("schedule line %d: syntax error (bad estimated time)"), line);
2351 *time_p = (time_t)time_;
2352 skip_integer(s, ch);
2354 skip_whitespace(s, ch); /* find the kps number */
2355 if(ch == '\0' || sscanf(s - 1, "%lu", &kps) != 1) {
2356 error(_("schedule line %d: syntax error (bad kps)"), line);
2359 skip_integer(s, ch);
2361 degr_dumpdate = NULL; /* flag if degr fields found */
2362 skip_whitespace(s, ch); /* find the degr level number */
2364 if(sscanf(s - 1, "%d", °r_level) != 1) {
2365 error(_("schedule line %d: syntax error (bad degr level)"), line);
2368 skip_integer(s, ch);
2370 skip_whitespace(s, ch); /* find the degr dump date */
2372 error(_("schedule line %d: syntax error (bad degr dump date)"), line);
2375 degr_dumpdate = s - 1;
2376 skip_non_whitespace(s, ch);
2379 skip_whitespace(s, ch); /* find the degr native size */
2380 degr_nsize_ = (off_t)0;
2381 if(ch == '\0' || sscanf(s - 1, "%lld", °r_nsize_) != 1) {
2382 error(_("schedule line %d: syntax error (bad degr nsize)"), line);
2385 degr_nsize = (off_t)degr_nsize_;
2386 skip_integer(s, ch);
2388 skip_whitespace(s, ch); /* find the degr compressed size */
2389 degr_csize_ = (off_t)0;
2390 if(ch == '\0' || sscanf(s - 1, "%lld", °r_csize_) != 1) {
2391 error(_("schedule line %d: syntax error (bad degr csize)"), line);
2394 degr_csize = (off_t)degr_csize_;
2395 skip_integer(s, ch);
2397 skip_whitespace(s, ch); /* find the degr time number */
2398 if(ch == '\0' || sscanf(s - 1, "%lld", &time_) != 1) {
2399 error(_("schedule line %d: syntax error (bad degr estimated time)"), line);
2402 *degr_time_p = (time_t)time_;
2403 skip_integer(s, ch);
2405 skip_whitespace(s, ch); /* find the degr kps number */
2406 if(ch == '\0' || sscanf(s - 1, "%lu", °r_kps) != 1) {
2407 error(_("schedule line %d: syntax error (bad degr kps)"), line);
2410 skip_integer(s, ch);
2413 degr_nsize = (off_t)0;
2414 degr_csize = (off_t)0;
2415 degr_time = (time_t)0;
2419 dp = lookup_disk(hostname, diskname);
2422 _("schedule line %d: %s:'%s' not in disklist, ignored"),
2423 line, hostname, qname);
2428 sp = (sched_t *) alloc(SIZEOF(sched_t));
2431 sp->dumpdate = stralloc(dumpdate);
2432 sp->est_nsize = DISK_BLOCK_KB + nsize; /* include header */
2433 sp->est_csize = DISK_BLOCK_KB + csize; /* include header */
2434 /* round estimate to next multiple of DISK_BLOCK_KB */
2435 sp->est_csize = am_round(sp->est_csize, DISK_BLOCK_KB);
2436 sp->est_size = sp->est_csize;
2437 sp->est_time = time;
2439 sp->priority = priority;
2440 sp->datestamp = stralloc(datestamp);
2443 sp->degr_level = degr_level;
2444 sp->degr_dumpdate = stralloc(degr_dumpdate);
2445 sp->degr_nsize = DISK_BLOCK_KB + degr_nsize;
2446 sp->degr_csize = DISK_BLOCK_KB + degr_csize;
2447 /* round estimate to next multiple of DISK_BLOCK_KB */
2448 sp->degr_csize = am_round(sp->degr_csize, DISK_BLOCK_KB);
2449 sp->degr_time = degr_time;
2450 sp->degr_kps = degr_kps;
2452 sp->degr_level = -1;
2453 sp->degr_dumpdate = NULL;
2457 sp->dump_attempted = 0;
2458 sp->taper_attempted = 0;
2463 sp->timestamp = (time_t)0;
2464 sp->destname = NULL;
2467 dp->up = (char *) sp;
2468 if(dp->host->features == NULL) {
2469 dp->host->features = am_string_to_feature(features);
2471 remove_disk(&waitq, dp);
2472 if (dp->to_holdingdisk == HOLD_NEVER) {
2473 enqueue_disk(&directq, dp);
2475 enqueue_disk(&runq, dp);
2477 flush_size += sp->act_size;
2480 g_printf(_("driver: flush size %lld\n"), (long long)flush_size);
2483 log_add(L_WARNING, _("WARNING: got empty schedule from planner"));
2484 if(need_degraded==1) start_degraded_mode(&runq);
2486 start_some_dumps(&runq);
2489 static unsigned long
2497 unsigned long maxusage=0;
2498 unsigned long curusage=0;
2499 for(p = disklist_netifs(); p != NULL; p = p->next) {
2500 maxusage += interface_get_maxusage(p->config);
2501 curusage += p->curusage;
2503 if (maxusage >= curusage)
2504 res = maxusage - curusage;
2509 if ((unsigned long)interface_get_maxusage(ip->config) >= ip->curusage)
2510 res = interface_get_maxusage(ip->config) - ip->curusage;
2525 g_printf(_("driver: interface-state time %s"), time_str);
2527 for(ip = disklist_netifs(); ip != NULL; ip = ip->next) {
2528 g_printf(_(" if %s: free %lu"), interface_name(ip->config), free_kps(ip));
2538 ip->curusage += kps;
2542 deallocate_bandwidth(
2546 assert(kps <= ip->curusage);
2547 ip->curusage -= kps;
2558 total_free = (off_t)0;
2559 for(ha = holdalloc; ha != NULL; ha = ha->next) {
2560 diff = ha->disksize - ha->allocated_space;
2568 * We return an array of pointers to assignedhd_t. The array contains at
2569 * most one entry per holding disk. The list of pointers is terminated by
2570 * a NULL pointer. Each entry contains a pointer to a holdingdisk and
2571 * how much diskspace to use on that disk. Later on, assign_holdingdisk
2572 * will allocate the given amount of space.
2573 * If there is not enough room on the holdingdisks, NULL is returned.
2576 static assignedhd_t **
2580 assignedhd_t * pref)
2582 assignedhd_t **result = NULL;
2583 holdalloc_t *ha, *minp;
2587 off_t halloc, dalloc, hfree, dfree;
2589 (void)cur_idle; /* Quiet unused parameter warning */
2591 if (size < 2*DISK_BLOCK_KB)
2592 size = 2*DISK_BLOCK_KB;
2593 size = am_round(size, (off_t)DISK_BLOCK_KB);
2595 hold_debug(1, _("find_diskspace: want %lld K\n"),
2598 used = alloc(SIZEOF(*used) * num_holdalloc);/*disks used during this run*/
2599 memset( used, 0, (size_t)num_holdalloc );
2600 result = alloc(SIZEOF(assignedhd_t *) * (num_holdalloc + 1));
2603 while( i < num_holdalloc && size > (off_t)0 ) {
2604 /* find the holdingdisk with the fewest active dumpers and among
2605 * those the one with the biggest free space
2607 minp = NULL; minj = -1;
2608 for(j = 0, ha = holdalloc; ha != NULL; ha = ha->next, j++ ) {
2609 if( pref && pref->disk == ha && !used[j] &&
2610 ha->allocated_space <= ha->disksize - (off_t)DISK_BLOCK_KB) {
2615 else if( ha->allocated_space <= ha->disksize - (off_t)(2*DISK_BLOCK_KB) &&
2618 ha->allocated_dumpers < minp->allocated_dumpers ||
2619 (ha->allocated_dumpers == minp->allocated_dumpers &&
2620 ha->disksize-ha->allocated_space > minp->disksize-minp->allocated_space)) ) {
2627 if( !minp ) { break; } /* all holding disks are full */
2630 /* hfree = free space on the disk */
2631 hfree = minp->disksize - minp->allocated_space;
2633 /* dfree = free space for data, remove 1 header for each chunksize */
2634 dfree = hfree - (((hfree-(off_t)1)/holdingdisk_get_chunksize(minp->hdisk))+(off_t)1) * (off_t)DISK_BLOCK_KB;
2636 /* dalloc = space I can allocate for data */
2637 dalloc = ( dfree < size ) ? dfree : size;
2639 /* halloc = space to allocate, including 1 header for each chunksize */
2640 halloc = dalloc + (((dalloc-(off_t)1)/holdingdisk_get_chunksize(minp->hdisk))+(off_t)1) * (off_t)DISK_BLOCK_KB;
2642 hold_debug(1, _("find_diskspace: find diskspace: size %lld hf %lld df %lld da %lld ha %lld\n"),
2649 result[i] = alloc(SIZEOF(assignedhd_t));
2650 result[i]->disk = minp;
2651 result[i]->reserved = halloc;
2652 result[i]->used = (off_t)0;
2653 result[i]->destname = NULL;
2659 if(size != (off_t)0) { /* not enough space available */
2660 g_printf(_("find diskspace: not enough diskspace. Left with %lld K\n"), (long long)size);
2662 free_assignedhd(result);
2666 if (debug_holding > 1) {
2667 for( i = 0; result && result[i]; i++ ) {
2668 hold_debug(1, _("find_diskspace: find diskspace: selected %s free %lld reserved %lld dumpers %d\n"),
2669 holdingdisk_get_diskdir(result[i]->disk->hdisk),
2670 (long long)(result[i]->disk->disksize -
2671 result[i]->disk->allocated_space),
2672 (long long)result[i]->reserved,
2673 result[i]->disk->allocated_dumpers);
2682 assignedhd_t ** holdp,
2687 char *sfn = sanitise_filename(diskp->name);
2689 assignedhd_t **new_holdp;
2692 g_snprintf( lvl, SIZEOF(lvl), "%d", sched(diskp)->level );
2694 size = am_round(sched(diskp)->est_size - sched(diskp)->act_size,
2695 (off_t)DISK_BLOCK_KB);
2697 for( c = 0; holdp[c]; c++ )
2698 (void)c; /* count number of disks */
2700 /* allocate memory for sched(diskp)->holdp */
2701 for(j = 0; sched(diskp)->holdp && sched(diskp)->holdp[j]; j++)
2702 (void)j; /* Quiet lint */
2703 new_holdp = (assignedhd_t **)alloc(SIZEOF(assignedhd_t*)*(j+c+1));
2704 if (sched(diskp)->holdp) {
2705 memcpy(new_holdp, sched(diskp)->holdp, j * SIZEOF(*new_holdp));
2706 amfree(sched(diskp)->holdp);
2708 sched(diskp)->holdp = new_holdp;
2712 if( j > 0 ) { /* This is a request for additional diskspace. See if we can
2713 * merge assignedhd_t's */
2715 if( sched(diskp)->holdp[j-1]->disk == holdp[0]->disk ) { /* Yes! */
2716 sched(diskp)->holdp[j-1]->reserved += holdp[0]->reserved;
2717 holdp[0]->disk->allocated_space += holdp[0]->reserved;
2718 size = (holdp[0]->reserved>size) ? (off_t)0 : size-holdp[0]->reserved;
2719 qname = quote_string(diskp->name);
2720 hold_debug(1, _("assign_holdingdisk: merging holding disk %s to disk %s:%s, add %lld for reserved %lld, left %lld\n"),
2721 holdingdisk_get_diskdir(
2722 sched(diskp)->holdp[j-1]->disk->hdisk),
2723 diskp->host->hostname, qname,
2724 (long long)holdp[0]->reserved,
2725 (long long)sched(diskp)->holdp[j-1]->reserved,
2734 /* copy assignedhd_s to sched(diskp), adjust allocated_space */
2735 for( ; holdp[i]; i++ ) {
2736 holdp[i]->destname = newvstralloc( holdp[i]->destname,
2737 holdingdisk_get_diskdir(holdp[i]->disk->hdisk), "/",
2738 hd_driver_timestamp, "/",
2739 diskp->host->hostname, ".",
2742 sched(diskp)->holdp[j++] = holdp[i];
2743 holdp[i]->disk->allocated_space += holdp[i]->reserved;
2744 size = (holdp[i]->reserved > size) ? (off_t)0 :
2745 (size - holdp[i]->reserved);
2746 qname = quote_string(diskp->name);
2748 _("assign_holdingdisk: %d assigning holding disk %s to disk %s:%s, reserved %lld, left %lld\n"),
2749 i, holdingdisk_get_diskdir(holdp[i]->disk->hdisk),
2750 diskp->host->hostname, qname,
2751 (long long)holdp[i]->reserved,
2754 holdp[i] = NULL; /* so it doesn't get free()d... */
2756 sched(diskp)->holdp[j] = NULL;
2767 assignedhd_t **holdp;
2768 off_t total = (off_t)0;
2771 char *qname, *hqname, *qdest;
2773 (void)cmd; /* Quiet unused parameter warning */
2775 qname = quote_string(diskp->name);
2776 qdest = quote_string(sched(diskp)->destname);
2777 hold_debug(1, _("adjust_diskspace: %s:%s %s\n"),
2778 diskp->host->hostname, qname, qdest);
2780 holdp = sched(diskp)->holdp;
2782 assert(holdp != NULL);
2784 for( i = 0; holdp[i]; i++ ) { /* for each allocated disk */
2785 diff = holdp[i]->used - holdp[i]->reserved;
2786 total += holdp[i]->used;
2787 holdp[i]->disk->allocated_space += diff;
2788 hqname = quote_string(holdingdisk_name(holdp[i]->disk->hdisk));
2789 hold_debug(1, _("adjust_diskspace: hdisk %s done, reserved %lld used %lld diff %lld alloc %lld dumpers %d\n"),
2790 holdingdisk_name(holdp[i]->disk->hdisk),
2791 (long long)holdp[i]->reserved,
2792 (long long)holdp[i]->used,
2794 (long long)holdp[i]->disk->allocated_space,
2795 holdp[i]->disk->allocated_dumpers );
2796 holdp[i]->reserved += diff;
2800 sched(diskp)->act_size = total;
2802 hold_debug(1, _("adjust_diskspace: after: disk %s:%s used %lld\n"),
2803 diskp->host->hostname, qname,
2804 (long long)sched(diskp)->act_size);
2813 assignedhd_t **holdp;
2816 holdp = sched(diskp)->holdp;
2818 assert(holdp != NULL);
2820 for( i = 0; holdp[i]; i++ ) { /* for each disk */
2821 /* find all files of this dump on that disk, and subtract their
2822 * reserved sizes from the disk's allocated space
2824 holdp[i]->disk->allocated_space -= holdp[i]->used;
2827 holding_file_unlink(holdp[0]->destname); /* no need for the entire list,
2828 * because holding_file_unlink
2829 * will walk through all files
2830 * using cont_filename */
2831 free_assignedhd(sched(diskp)->holdp);
2832 sched(diskp)->holdp = NULL;
2833 sched(diskp)->act_size = (off_t)0;
2836 static assignedhd_t **
2843 char buffer[DISK_BLOCK_BYTES];
2845 assignedhd_t **result;
2848 char dirname[1000], *ch;
2850 char *filename = destname;
2852 memset(buffer, 0, sizeof(buffer));
2853 used = alloc(SIZEOF(off_t) * num_holdalloc);
2854 for(i=0;i<num_holdalloc;i++)
2856 result = alloc(SIZEOF(assignedhd_t *) * (num_holdalloc + 1));
2858 while(filename != NULL && filename[0] != '\0') {
2859 strncpy(dirname, filename, 999);
2861 ch = strrchr(dirname,'/');
2863 ch = strrchr(dirname,'/');
2866 for(j = 0, ha = holdalloc; ha != NULL; ha = ha->next, j++ ) {
2867 if(strcmp(dirname, holdingdisk_get_diskdir(ha->hdisk))==0) {
2872 if(stat(filename, &finfo) == -1) {
2873 g_fprintf(stderr, _("stat %s: %s\n"), filename, strerror(errno));
2874 finfo.st_size = (off_t)0;
2876 used[j] += ((off_t)finfo.st_size+(off_t)1023)/(off_t)1024;
2877 if((fd = open(filename,O_RDONLY)) == -1) {
2878 g_fprintf(stderr,_("build_diskspace: open of %s failed: %s\n"),
2879 filename, strerror(errno));
2882 if ((buflen = fullread(fd, buffer, SIZEOF(buffer))) > 0) {;
2883 parse_file_header(buffer, &file, (size_t)buflen);
2886 filename = file.cont_filename;
2889 for(j = 0, i=0, ha = holdalloc; ha != NULL; ha = ha->next, j++ ) {
2890 if(used[j] != (off_t)0) {
2891 result[i] = alloc(SIZEOF(assignedhd_t));
2892 result[i]->disk = ha;
2893 result[i]->reserved = used[j];
2894 result[i]->used = used[j];
2895 result[i]->destname = stralloc(destname);
2913 g_printf(_("driver: hdisk-state time %s"), time_str);
2915 for(ha = holdalloc, dsk = 0; ha != NULL; ha = ha->next, dsk++) {
2916 diff = ha->disksize - ha->allocated_space;
2917 g_printf(_(" hdisk %d: free %lld dumpers %d"), dsk,
2918 (long long)diff, ha->allocated_dumpers);
2924 update_failed_dump_to_tape(
2928 * should simply set no_bump
2931 time_t save_timestamp = sched(dp)->timestamp;
2932 /* setting timestamp to 0 removes the current level from the
2933 * database, so that we ensure that it will not be bumped to the
2934 * next level on the next run. If we didn't do this, dumpdates or
2935 * gnutar-lists might have been updated already, and a bumped
2936 * incremental might be created. */
2937 sched(dp)->timestamp = 0;
2938 update_info_dumper(dp, (off_t)-1, (off_t)-1, (time_t)-1);
2939 sched(dp)->timestamp = save_timestamp;
2942 /* ------------------- */
2950 char *result_argv[MAX_ARGS+1];
2953 qname = quote_string(dp->name);
2954 g_printf(_("driver: dumping %s:%s directly to tape\n"),
2955 dp->host->hostname, qname);
2958 /* pick a dumper and fail if there are no idle dumpers */
2960 dumper = idle_dumper();
2962 g_printf(_("driver: no idle dumpers for %s:%s.\n"),
2963 dp->host->hostname, qname);
2965 log_add(L_WARNING, _("no idle dumpers for %s:%s.\n"),
2966 dp->host->hostname, qname);
2968 return; /* fatal problem */
2971 /* tell the taper to read from a port number of its choice */
2973 taper_cmd(PORT_WRITE, dp, NULL, sched(dp)->level, sched(dp)->datestamp);
2974 cmd = getresult(taper, 1, &result_argc, result_argv, MAX_ARGS+1);
2976 g_printf(_("driver: did not get PORT from taper for %s:%s\n"),
2977 dp->host->hostname, qname);
2979 log_add(L_WARNING, _("driver: did not get PORT from taper for %s:%s.\n"),
2980 dp->host->hostname, qname);
2982 return; /* fatal problem */
2986 /* copy port number */
2987 dumper->output_port = atoi(result_argv[2]);
2990 dumper->chunker = NULL;
2991 dumper->result = LAST_TOK;
2992 taper_result = LAST_TOK;
2993 sched(dp)->dumper = dumper;
2995 /* tell the dumper to dump to a port */
2996 dumper_cmd(dumper, PORT_DUMP, dp);
2997 dp->host->start_t = time(NULL) + 15;
2999 /* update statistics & print state */
3001 taper_busy = dumper->busy = 1;
3002 taper_input_error = NULL;
3003 taper_tape_error = NULL;
3004 taper_dumper = dumper;
3006 taper_input_error = NULL;
3007 taper_tape_error = NULL;
3008 taper_first_label = NULL;
3010 taper_state |= TAPER_STATE_DUMP_TO_TAPE;
3011 sched(dp)->act_size = sched(dp)->est_size;
3012 dp->host->inprogress += 1;
3014 sched(dp)->timestamp = time((time_t *)0);
3015 allocate_bandwidth(dp->host->netif, sched(dp)->est_kps);
3016 idle_reason = NOT_IDLE;
3020 dumper->ev_read = event_register(dumper->fd, EV_READFD,
3021 handle_dumper_result, dumper);
3022 taper_ev_read = event_register(taper, EV_READFD,
3023 handle_taper_result, NULL);
3033 for(len = 0, p = q.head; p != NULL; len++, p = p->next)
3034 (void)len; /* Quiet lint */
3039 short_dump_state(void)
3044 wall_time = walltime_str(curclock());
3046 g_printf(_("driver: state time %s "), wall_time);
3047 g_printf(_("free kps: %lu space: %lld taper: "),
3049 (long long)free_space());
3050 if(degraded_mode) g_printf(_("DOWN"));
3051 else if(!taper_busy) g_printf(_("idle"));
3052 else g_printf(_("writing"));
3054 for(i = 0; i < inparallel; i++) if(!dmptable[i].busy) nidle++;
3055 g_printf(_(" idle-dumpers: %d"), nidle);
3056 g_printf(_(" qlen tapeq: %d"), queue_length(tapeq));
3057 g_printf(_(" runq: %d"), queue_length(runq));
3058 g_printf(_(" roomq: %d"), queue_length(roomq));
3059 g_printf(_(" wakeup: %d"), (int)sleep_time);
3060 g_printf(_(" driver-idle: %s\n"), _(idle_strings[idle_reason]));
3061 interface_state(wall_time);
3062 holdingdisk_state(wall_time);
3066 static TapeAction tape_action(void)
3068 TapeAction result = TAPE_ACTION_NO_ACTION;
3076 off_t dump_to_disk_size;
3077 int dump_to_disk_terminated;
3080 for(dumper = dmptable; dumper < (dmptable+inparallel); dumper++) {
3082 dumpers_size += sched(dumper->dp)->est_size;
3084 driver_debug(1, _("dumpers_size: %lld\n"), (long long)dumpers_size);
3087 for(dp = runq.head; dp != NULL; dp = dp->next) {
3088 runq_size += sched(dp)->est_size;
3090 driver_debug(1, _("runq_size: %lld\n"), (long long)runq_size);
3093 for(dp = directq.head; dp != NULL; dp = dp->next) {
3094 directq_size += sched(dp)->est_size;
3096 driver_debug(1, _("directq_size: %lld\n"), (long long)directq_size);
3099 for(dp = tapeq.head; dp != NULL; dp = dp->next) {
3100 tapeq_size += sched(dp)->act_size;
3103 tapeq_size += sched(taper_disk)->act_size - taper_written;
3105 driver_debug(1, _("tapeq_size: %lld\n"), (long long)tapeq_size);
3107 sched_size = runq_size + tapeq_size + dumpers_size;
3108 driver_debug(1, _("sched_size: %lld\n"), (long long)sched_size);
3110 dump_to_disk_size = dumpers_size + runq_size;
3111 driver_debug(1, _("dump_to_disk_size: %lld\n"), (long long)dump_to_disk_size);
3113 dump_to_disk_terminated = schedule_done && dump_to_disk_size == 0;
3115 // Changing conditionals can produce a driver hang, take care.
3117 // when to start writting to a new tape
3118 if ((taper_state & TAPER_STATE_WAIT_FOR_TAPE) &&
3119 ((taper_state & TAPER_STATE_DUMP_TO_TAPE) || // for dump to tape
3120 !empty(directq) || // if a dle is waiting for a dump to tape
3121 !empty(roomq) || // holding disk constraint
3122 idle_reason == IDLE_NO_DISKSPACE || // holding disk constraint
3123 (flush_threshold_dumped < tapeq_size && // flush-threshold-dumped &&
3124 flush_threshold_scheduled < sched_size) || // flush-threshold-scheduled
3125 (taperflush < tapeq_size && // taperflush
3126 (force_flush == 1 || // if force_flush
3127 dump_to_disk_terminated)) // or all dump to disk terminated
3129 result |= TAPE_ACTION_NEW_TAPE;
3130 // when to stop using new tape
3131 } else if ((taper_state & TAPER_STATE_WAIT_FOR_TAPE) &&
3132 (taperflush >= tapeq_size && // taperflush criteria not meet
3133 (force_flush == 1 || // if force_flush
3134 dump_to_disk_terminated)) // or all dump to disk terminated
3136 result |= TAPE_ACTION_NO_NEW_TAPE;
3139 // when to start a flush
3140 // We don't start a flush if taper_tape_started == 1 && dump_to_disk_terminated && force_flush == 0,
3141 // it is a criteria need to exit the first event_loop without flushing everything to tape,
3142 // they will be flush in another event_loop.
3143 if (!degraded_mode && !taper_busy && !empty(tapeq) &&
3144 (!((taper_state & TAPER_STATE_TAPE_STARTED) &&
3145 dump_to_disk_terminated && force_flush == 0) || // if tape already started and dump to disk not terminated
3146 ((taper_state & TAPER_STATE_TAPE_STARTED) &&
3147 force_flush == 1) || // if tape already started and force_flush
3148 !empty(roomq) || // holding disk constraint
3149 idle_reason == IDLE_NO_DISKSPACE || // holding disk constraint
3150 (flush_threshold_dumped < tapeq_size && // flush-threshold-dumped &&
3151 flush_threshold_scheduled < sched_size) || // flush-threshold-scheduled
3152 (force_flush == 1 && taperflush < tapeq_size))) { // taperflush if force_flush
3153 result |= TAPE_ACTION_START_A_FLUSH;
3167 g_printf("================\n");
3168 g_printf(_("driver state at time %s: %s\n"), walltime_str(curclock()), str);
3169 g_printf(_("free kps: %lu, space: %lld\n"),
3171 (long long)free_space());
3172 if(degraded_mode) g_printf(_("taper: DOWN\n"));
3173 else if(!taper_busy) g_printf(_("taper: idle\n"));
3174 else g_printf(_("taper: writing %s:%s.%d est size %lld\n"),
3175 taper_disk->host->hostname, taper_disk->name,
3176 sched(taper_disk)->level,
3177 (long long)sched(taper_disk)->est_size);
3178 for(i = 0; i < inparallel; i++) {
3179 dp = dmptable[i].dp;
3180 if(!dmptable[i].busy)
3181 g_printf(_("%s: idle\n"), dmptable[i].name);
3183 qname = quote_string(dp->name);
3184 g_printf(_("%s: dumping %s:%s.%d est kps %d size %lld time %lu\n"),
3185 dmptable[i].name, dp->host->hostname, qname, sched(dp)->level,
3186 sched(dp)->est_kps, (long long)sched(dp)->est_size, sched(dp)->est_time);
3189 dump_queue("TAPE", tapeq, 5, stdout);
3190 dump_queue("ROOM", roomq, 5, stdout);
3191 dump_queue("RUN ", runq, 5, stdout);
3192 g_printf("================\n");