2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998, 2000 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.
26 /* $Id: taper.c 6512 2007-05-24 17:00:24Z ian $
28 * moves files from holding disk to tape, or from a socket to tape
31 /* FIXME: This file needs to use gettext. */
41 #include "server_util.h"
44 #include "taperscan.h"
45 #include "taper-source.h"
46 #include "timestamp.h"
50 #include "device-queueing.h"
52 /* FIXME: This should not be here. */
53 #define CONNECT_TIMEOUT (2*60)
55 /* Use this instead of global variables, so that we are reentrant. */
58 char * driver_start_time;
60 char * next_tape_label;
61 char * next_tape_device;
62 taper_scan_tracker_t * taper_scan_tracker;
81 static gboolean label_new_tape(taper_state_t * state, dump_info_t * dump_info);
83 static void init_taper_state(taper_state_t* state) {
85 state->driver_start_time = NULL;
86 state->taper_scan_tracker = taper_scan_tracker_new();
87 state->last_errmsg = NULL;
88 state->total_bytes = 0;
91 static void cleanup(taper_state_t * state) {
92 amfree(state->driver_start_time);
93 amfree(state->next_tape_label);
94 amfree(state->next_tape_device);
95 amfree(state->last_errmsg);
96 taper_scan_tracker_free(state->taper_scan_tracker);
97 if (state->device != NULL) {
98 g_object_unref(state->device);
103 static void free_dump_info(dump_info_t * info) {
104 amfree(info->handle);
105 amfree(info->hostname);
106 amfree(info->diskname);
107 amfree(info->timestamp);
108 amfree(info->id_string);
109 if (info->source != NULL) {
110 g_object_unref(info->source);
115 /* Validate that a command has the proper number of arguments, and
116 print a meaningful error message if not. It returns only if the
117 check is successful. */
118 static void validate_args(struct cmdargs * cmdargs,
120 int len = g_strv_length(argnames);
122 if (len > cmdargs->argc) {
123 error("error [taper %s: not enough args; first missing arg is %s]",
124 cmdstr[cmdargs->cmd], argnames[cmdargs->argc]);
127 if (len < cmdargs->argc) {
128 error("error [taper %s: Too many args: Got %d, expected %d.]",
129 cmdstr[cmdargs->cmd], cmdargs->argc, len);
133 /* Open a socket to the dumper. Returns TRUE if everything is happy, FALSE
135 static gboolean open_read_socket(dump_info_t * info, char * split_diskbuffer,
136 guint64 splitsize, guint64 fallback_splitsize) {
141 struct addrinfo *res;
143 if ((result = resolve_hostname("localhost", 0, &res, NULL) != 0)) {
146 int save_errno = errno;
147 char *qdiskname = quote_string(info->diskname);
149 m = vstralloc("[localhost resolve failure: ",
150 strerror(save_errno),
154 putresult(TAPE_ERROR, "%s %s\n", info->handle, q);
155 log_add(L_FAIL, "%s %s %s %d %s",
156 info->hostname, qdiskname, info->timestamp,
164 socket = stream_server(res->ai_family, &port, 0, STREAM_BUFSIZE, 0);
170 int save_errno = errno;
171 char *qdiskname = quote_string(info->diskname);
173 m = vstralloc("[port create failure: ",
174 strerror(save_errno),
178 putresult(TAPE_ERROR, "%s %s\n", info->handle, q);
179 log_add(L_FAIL, "%s %s %s %d %s",
180 info->hostname, qdiskname, info->timestamp,
188 putresult(PORT, "%d\n", port);
190 fd = stream_accept(socket, CONNECT_TIMEOUT, 0, STREAM_BUFSIZE);
194 int save_errno = errno;
195 char *qdiskname = quote_string(info->diskname);
196 m = vstralloc("[port connect failure: ",
197 strerror(save_errno),
201 putresult(TAPE_ERROR, "%s %s\n", info->handle, q);
202 log_add(L_FAIL, "%s %s %s %d %s",
203 info->hostname, qdiskname, info->timestamp,
214 info->source = taper_source_new(info->handle, PORT_WRITE, NULL, fd,
215 split_diskbuffer, splitsize,
217 /* FIXME: This should be handled properly. */
218 g_assert(info->source != NULL);
223 ConsumerFunctor next_consumer;
224 gpointer next_consumer_data;
225 guint64 bytes_written;
226 } CountingConsumerData;
228 /* A ConsumerFunctor. This consumer just passes its arguments on to a
229 second consumer, but counts the number of bytes successfully
231 static ssize_t counting_consumer(gpointer user_data, queue_buffer_t * buffer) {
233 CountingConsumerData * data = user_data;
235 result = data->next_consumer(data->next_consumer_data, buffer);
238 data->bytes_written += result;
244 static gboolean boolean_prolong(void * data) {
246 return TRUE; /* Do not interrupt. */
248 return *(gboolean*)data;
252 /* A (simpler) wrapper around taper_scan(). */
253 static gboolean simple_taper_scan(taper_state_t * state,
254 gboolean* prolong, char ** error_message) {
255 char ** label = &(state->next_tape_label);
256 char ** device = &(state->next_tape_device);
257 char *timestamp = NULL;
259 result = taper_scan(NULL, label, ×tamp, device,
260 state->taper_scan_tracker,
261 CHAR_taperscan_output_callback,
262 error_message, boolean_prolong, prolong);
263 if (prolong != NULL && !*prolong) {
264 g_fprintf(stderr, _("Cancelled taper scan.\n"));
266 } else if (result < 0) {
267 g_fprintf(stderr, _("Failed taper scan: %s\n"), (*error_message)?(*error_message):_("(no error message)"));
271 g_fprintf(stderr, _("taper: using label `%s' date `%s'\n"), *label,
272 state->driver_start_time);
275 _("Will write new label `%s' to new tape"),
285 taper_state_t * state;
286 gboolean prolong; /* scan stops when this is FALSE. */
288 } tape_search_request_t;
290 /* A GThread that runs taper_scan. */
291 static gpointer tape_search_thread(gpointer data) {
292 tape_search_request_t * request = data;
294 if (request->state->next_tape_label != NULL &&
295 request->state->next_tape_device != NULL) {
296 return GINT_TO_POINTER(TRUE);
298 amfree(request->state->next_tape_label);
299 amfree(request->state->next_tape_device);
302 return GINT_TO_POINTER
303 (simple_taper_scan(request->state,
305 &(request->errmsg)));
308 static void log_taper_scan_errmsg(char * errmsg) {
317 log_add(L_WARNING,"%s", c1);
323 log_add(L_WARNING,"%s", c1);
327 /* If handle is NULL, then this function assumes that we are in startup mode.
328 * In that case it will wait for a command from driver. If handle is not NULL,
329 * this this function will ask for permission with REQUEST-NEW-TAPE. */
330 static gboolean find_new_tape(taper_state_t * state, dump_info_t * dump) {
331 GThread * tape_search = NULL;
332 tape_search_request_t search_request;
333 gboolean use_threads;
334 struct cmdargs *cmdargs;
337 if (state->device != NULL) {
341 /* We save the value here in case it changes while we're running. */
342 use_threads = g_thread_supported();
344 search_request.state = state;
345 search_request.prolong = TRUE;
346 search_request.errmsg = NULL;
348 tape_search = g_thread_create(tape_search_thread,
349 &search_request, TRUE, NULL);
352 putresult(REQUEST_NEW_TAPE, "%s\n", dump->handle);
358 g_fprintf(stderr, "taper: Got odd message from driver, expected NEW-TAPE or NO-NEW-TAPE.\n");
361 gboolean search_result;
363 search_result = GPOINTER_TO_INT(g_thread_join(tape_search));
366 GPOINTER_TO_INT(tape_search_thread(&search_request));
369 /* We don't say NEW_TAPE until we actually write the label. */
370 amfree(search_request.errmsg);
371 free_cmdargs(cmdargs);
374 putresult(NO_NEW_TAPE, "%s\n", dump->handle);
375 log_taper_scan_errmsg(search_request.errmsg);
376 log_add(L_ERROR, "no-tape [%s]", "No more writable valid tape found");
377 free_cmdargs(cmdargs);
382 search_request.prolong = FALSE;
384 g_thread_join(tape_search);
386 log_add(L_ERROR, "no-tape [%s]", cmdargs->argv[1]);
387 state->last_errmsg = stralloc(cmdargs->argv[1]);
388 free_cmdargs(cmdargs);
394 /* Returns TRUE if the old volume details are not the same as the new ones. */
395 static gboolean check_volume_changed(Device * device,
396 char * old_label, char * old_timestamp) {
397 /* If one is NULL and the other is not, something changed. */
398 if ((old_label == NULL) != (device->volume_label == NULL))
400 if ((old_timestamp == NULL) != (device->volume_time == NULL))
402 /* If details were not NULL and is now different, we have a difference. */
403 if (old_label != NULL && strcmp(old_label, device->volume_label) != 0)
405 if (old_timestamp != NULL &&
406 strcmp(old_timestamp, device->volume_time) != 0)
409 /* If we got here, everything is cool. */
415 taper_state_t *state)
417 char *tapelist_name = NULL;
418 char *tapelist_name_old = NULL;
420 char *comment = NULL;
422 tapelist_name = config_dir_relative(getconf_str(CNF_TAPELIST));
423 if (state->cur_tape == 0) {
424 tapelist_name_old = stralloc2(tapelist_name, ".yesterday");
426 char cur_str[NUM_STR_SIZE];
427 g_snprintf(cur_str, SIZEOF(cur_str), "%d", state->cur_tape - 1);
428 tapelist_name_old = vstralloc(tapelist_name,
429 ".today.", cur_str, NULL);
432 if (read_tapelist(tapelist_name) != 0) {
433 log_add(L_INFO, "pid-done %ld", (long)getpid());
434 error("could not load tapelist \"%s\"", tapelist_name);
438 /* make a copy of the tapelist file */
439 if (write_tapelist(tapelist_name_old)) {
440 log_add(L_INFO, "pid-done %ld", (long)getpid());
441 error("could not write tapelist: %s", strerror(errno));
444 amfree(tapelist_name_old);
446 /* get a copy of the comment, before freeing the old record */
447 tp = lookup_tapelabel(state->device->volume_label);
448 if (tp && tp->comment)
449 comment = stralloc(tp->comment);
451 /* edit the tapelist and rewrite it */
452 remove_tapelabel(state->device->volume_label);
453 add_tapelabel(state->driver_start_time,
454 state->device->volume_label,
456 if (write_tapelist(tapelist_name)) {
457 error("could not write tapelist: %s", strerror(errno));
460 amfree(tapelist_name);
464 /* Find and label a new tape, if one is not already open. Returns TRUE
465 * if a tape could be written. */
466 static gboolean find_and_label_new_tape(taper_state_t * state,
467 dump_info_t * dump_info) {
468 if (state->device != NULL) {
471 state->total_bytes = 0;
473 if (!find_new_tape(state, dump_info)) {
477 return label_new_tape(state, dump_info);
480 static gboolean label_new_tape(taper_state_t * state, dump_info_t * dump_info) {
481 char *old_volume_name = NULL;
482 char *old_volume_time = NULL;
483 tape_search_request_t request;
484 gboolean search_result;
485 DeviceStatusFlags status;
487 /* If we got here, it means that we have found a tape to label and
488 * have gotten permission from the driver to write it. But we
489 * still can say NO-NEW-TAPE if a problem shows up, and must still
490 * say NEW-TAPE if one doesn't. */
492 amfree(state->last_errmsg);
493 state->device = device_open(state->next_tape_device);
494 g_assert(state->device != NULL);
495 amfree(state->next_tape_device);
497 if (state->device->status != DEVICE_STATUS_SUCCESS)
500 if (!device_configure(state->device, TRUE))
503 /* if we have an error, and are sure it isn't just an unlabeled volume,
504 * then skip this volume */
505 status = device_read_label(state->device);
506 if ((status & ~DEVICE_STATUS_VOLUME_UNLABELED) &&
507 !(status & DEVICE_STATUS_VOLUME_UNLABELED))
510 old_volume_name = g_strdup(state->device->volume_label);
511 old_volume_time = g_strdup(state->device->volume_time);
513 if (!device_start(state->device, ACCESS_WRITE, state->next_tape_label,
514 state->driver_start_time)) {
517 /* Something broke, see if we can tell if the volume was erased or
519 g_fprintf(stderr, "taper: Error writing label %s to device %s: %s.\n",
520 state->next_tape_label, state->device->device_name,
521 device_error_or_status(state->device));
523 if (!device_finish(state->device))
524 goto request_new_volume;
526 /* This time, if we can't read the label, assume we've overwritten
527 * the volume or otherwise corrupted it */
528 status = device_read_label(state->device);
529 if ((status & ~DEVICE_STATUS_VOLUME_UNLABELED) &&
530 !(status & DEVICE_STATUS_VOLUME_UNLABELED))
531 goto request_new_volume;
533 tape_used = check_volume_changed(state->device, old_volume_name,
537 goto request_new_volume;
542 amfree(old_volume_name);
543 amfree(old_volume_time);
544 amfree(state->next_tape_label);
546 update_tapelist(state);
549 if (state->have_changer &&
550 changer_label("UNKNOWN", state->device->volume_label) != 0) {
551 error(_("couldn't update barcode database"));
555 log_add(L_START, "datestamp %s label %s tape %d",
556 state->driver_start_time, state->device->volume_label,
558 putresult(NEW_TAPE, "%s %s\n", dump_info->handle,
559 state->device->volume_label);
564 /* Tell the driver we overwrote this volume, even if it was empty, and request
567 state->last_errmsg = newstralloc(state->last_errmsg, device_error_or_status(state->device));
569 state->last_errmsg = newstralloc(state->last_errmsg, "(unknown)");
571 putresult(NEW_TAPE, "%s %s\n", dump_info->handle,
572 state->next_tape_label);
573 if (old_volume_name) {
574 log_add(L_WARNING, "Problem writing label '%s' to volume %s "
575 "(volume may be erased): %s\n",
576 state->next_tape_label, old_volume_name,
579 log_add(L_WARNING, "Problem writing label '%s' to new volume "
580 "(volume may be erased): %s\n", state->next_tape_label,
585 g_object_unref(state->device);
586 state->device = NULL;
589 amfree(state->next_tape_label);
590 amfree(old_volume_name);
591 amfree(old_volume_time);
593 return find_and_label_new_tape(state, dump_info);
596 /* grab a new volume without talking to the driver again -- we do this if we're
597 * confident we didn't overwrite the last tape we got. */
599 state->last_errmsg = newstralloc(state->last_errmsg, device_error_or_status(state->device));
601 state->last_errmsg = newstralloc(state->last_errmsg, "(unknown)");
603 if (old_volume_name) {
604 log_add(L_WARNING, "Problem writing label '%s' to volume '%s' "
605 "(old volume data intact): %s\n",
606 state->next_tape_label, old_volume_name, state->last_errmsg);
608 log_add(L_WARNING, "Problem writing label '%s' to new volume "
609 "(old volume data intact): %s\n", state->next_tape_label,
614 g_object_unref(state->device);
615 state->device = NULL;
618 amfree(state->next_tape_label);
619 amfree(old_volume_name);
620 amfree(old_volume_time);
622 request.state = state;
623 request.prolong = TRUE;
624 request.errmsg = NULL;
625 search_result = GPOINTER_TO_INT(tape_search_thread(&request));
627 amfree(request.errmsg);
628 return label_new_tape(state, dump_info);
630 /* Problem finding a new tape! */
631 log_taper_scan_errmsg(request.errmsg);
632 putresult(NO_NEW_TAPE, "%s\n", dump_info->handle);
637 /* Find out if the dump is PARTIAL or not, and set the proper driver
638 and logfile tags for the dump. */
639 static void find_completion_tags(dump_info_t * dump_info, /* IN */
640 cmd_t * result_cmd, /* OUT */
641 logtype_t * result_log /* OUT */) {
642 /* result_cmd is always DONE because the taper wrote all the input to
643 * the output. driver need to know if the taper completed its job.
644 * result_log is set to L_PARTIAL if the image is partial, the log
645 * must tell if the image is partial or complete.
648 if (taper_source_is_partial(dump_info->source)) {
650 *result_log = L_PARTIAL;
653 *result_log = L_DONE;
657 /* Put an L_PARTIAL message to the logfile. */
658 static void put_partial_log(dump_info_t * dump_info, double dump_time,
659 guint64 dump_kbytes, char *errstr) {
660 char * qdiskname = quote_string(dump_info->diskname);
662 log_add(L_PARTIAL, "%s %s %s %d %d [sec %f kb %ju kps %f] %s",
663 dump_info->hostname, qdiskname, dump_info->timestamp,
664 dump_info->current_part, dump_info->level, dump_time,
665 (uintmax_t)dump_kbytes, dump_kbytes / dump_time,
670 /* Figure out what to do after a part attempt. Returns TRUE if another
671 attempt should proceed for this dump; FALSE if we are done. */
672 static gboolean finish_part_attempt(taper_state_t * taper_state,
673 dump_info_t * dump_info,
674 queue_result_flags queue_result,
675 GTimeVal run_time, guint64 run_bytes) {
676 double part_time = g_timeval_to_double(run_time);
677 guint64 part_kbytes = run_bytes / 1024;
678 double part_kbps = run_bytes / (1024 * part_time);
680 char * qdiskname = quote_string(dump_info->diskname);
682 if (queue_result == QUEUE_SUCCESS) {
683 dump_info->total_time = timesadd(run_time, dump_info->total_time);
684 dump_info->total_bytes += run_bytes;
686 log_add(L_PART, "%s %d %s %s %s %d/%d %d [sec %f kb %ju kps %f]",
687 taper_state->device->volume_label,
688 taper_state->device->file, dump_info->hostname, qdiskname,
689 dump_info->timestamp, dump_info->current_part,
690 taper_source_predict_parts(dump_info->source),
691 dump_info->level, part_time, (uintmax_t)part_kbytes, part_kbps);
692 putresult(PARTDONE, "%s %s %d %ju \"[sec %f kb %ju kps %f]\"\n",
693 dump_info->handle, taper_state->device->volume_label,
694 taper_state->device->file, (uintmax_t)part_kbytes, part_time,
695 (uintmax_t)part_kbytes, part_kbps);
696 taper_state->total_bytes += run_bytes;
698 if (taper_source_get_end_of_data(dump_info->source)) {
700 logtype_t result_log;
701 double dump_time = g_timeval_to_double(dump_info->total_time);
702 guint64 dump_kbytes = dump_info->total_bytes / 1024;
703 double dump_kbps = dump_info->total_bytes / (1024 * dump_time);
705 find_completion_tags(dump_info, &result_cmd, &result_log);
707 g_object_unref(dump_info->source);
708 dump_info->source = NULL;
710 log_add(result_log, "%s %s %s %d %d [sec %f kb %ju kps %f]",
711 dump_info->hostname, qdiskname, dump_info->timestamp,
712 dump_info->current_part, dump_info->level, dump_time,
713 (uintmax_t)dump_kbytes, dump_kbps);
714 putresult(result_cmd, "%s INPUT-GOOD TAPE-GOOD "
715 "\"[sec %f kb %ju kps %f]\" \"\" \"\"\n",
716 dump_info->handle, dump_time, (uintmax_t)dump_kbytes,
721 } else if (taper_source_get_end_of_part(dump_info->source)) {
722 taper_source_start_new_part(dump_info->source);
723 dump_info->current_part ++;
727 /* If we didn't read EOF or EOP, then an error
728 occured. But we read QUEUE_SUCCESS, so something is
730 g_assert_not_reached();
732 char * volume_label = strdup(taper_state->device->volume_label);
733 int file_number = taper_state->device->file;
734 double dump_time, dump_kbps;
736 char *producer_errstr = quote_string(
737 taper_source_get_errmsg(dump_info->source));
738 char *consumer_errstr = quote_string(
739 device_error(taper_state->device));
741 log_add(L_PARTPARTIAL,
742 "%s %d %s %s %s %d/%d %d [sec %f kb %ju kps %f] %s",
743 volume_label, file_number, dump_info->hostname, qdiskname,
744 dump_info->timestamp, dump_info->current_part,
745 taper_source_predict_parts(dump_info->source),
746 dump_info->level, part_time, (uintmax_t)part_kbytes, part_kbps,
748 log_add(L_INFO, "tape %s kb %lld fm %d [OK]\n",
750 (long long)((taper_state->total_bytes+(off_t)1023) / (off_t)1024),
751 taper_state->device->file);
753 /* A problem occured. */
754 if (queue_result & QUEUE_CONSUMER_ERROR) {
755 /* Make a note if this was EOM (we treat EOM the same as any other error,
756 * so it's just for debugging purposes */
757 if (taper_state->device->is_eof)
758 g_debug("device %s ran out of space", taper_state->device->device_name);
760 /* Close the device. */
761 device_finish(taper_state->device);
762 g_object_unref(taper_state->device);
763 taper_state->device = NULL;
766 amfree(volume_label);
768 if ((queue_result & QUEUE_CONSUMER_ERROR) &&
769 (!(queue_result & QUEUE_PRODUCER_ERROR)) &&
770 taper_source_seek_to_part_start(dump_info->source)) {
771 /* It is recoverable. */
772 log_add(L_INFO, "Will request retry of failed split part.");
773 if (find_and_label_new_tape(taper_state, dump_info)) {
774 /* dump_info->current_part is unchanged. */
780 dump_info->total_time = timesadd(run_time, dump_info->total_time);
781 dump_info->total_bytes += run_bytes;
782 dump_time = g_timeval_to_double(dump_info->total_time);
783 dump_kbytes = dump_info->total_bytes / 1024;
784 dump_kbps = dump_info->total_bytes / (1024 * dump_time);
787 "%s INPUT-%s TAPE-%s "
788 "\"[sec %f kb %ju kps %f]\" %s %s\n",
790 (queue_result & QUEUE_PRODUCER_ERROR) ? "ERROR" : "GOOD",
791 (queue_result & QUEUE_CONSUMER_ERROR) ? "ERROR" : "GOOD",
792 dump_time, (uintmax_t)dump_kbytes, dump_kbps,
793 producer_errstr, consumer_errstr);
794 if (queue_result & QUEUE_CONSUMER_ERROR) {
795 put_partial_log(dump_info, dump_time, dump_kbytes,
798 put_partial_log(dump_info, dump_time, dump_kbytes,
801 amfree(producer_errstr);
802 amfree(consumer_errstr);
809 /* Generate the actual header structure to write to tape. This means dropping
810 * bits related to the holding disk, and adding bits for split dumps. */
811 static dumpfile_t * munge_headers(dump_info_t * dump_info) {
815 rval = taper_source_get_first_header(dump_info->source);
821 rval->cont_filename[0] = '\0';
823 expected_splits = taper_source_predict_parts(dump_info->source);
825 if (expected_splits != 1) {
826 rval->type = F_SPLIT_DUMPFILE;
827 rval->partnum = dump_info->current_part;
828 rval->totalparts = expected_splits;
834 /* We call this when we can't find a tape to write data to. This could
835 happen with the first (or only) part of a file, but it could also
836 happen with an intermediate part of a split dump. dump_bytes
837 is 0 if this is the first part of a dump. */
838 static void bail_no_volume(
839 dump_info_t *dump_info,
844 errstr = quote_string(errmsg);
846 errstr = quote_string("no new tape");
847 if (dump_info->total_bytes > 0) {
848 /* Second or later part of a split dump, so PARTIAL message. */
849 double dump_time = g_timeval_to_double(dump_info->total_time);
850 guint64 dump_kbytes = dump_info->total_bytes / 1024;
851 double dump_kbps = dump_kbytes / dump_time;
853 "%s INPUT-GOOD TAPE-ERROR "
854 "\"[sec %f kb %ju kps %f]\" \"\" %s\n",
856 dump_time, (uintmax_t)dump_kbytes, dump_kbps, errstr);
857 put_partial_log(dump_info, dump_time, dump_kbytes, errstr);
859 char * qdiskname = quote_string(dump_info->diskname);
861 "%s INPUT-GOOD TAPE-ERROR \"\" %s\n",
862 dump_info->handle, errstr);
863 log_add(L_FAIL, "%s %s %s %d %s",
864 dump_info->hostname, qdiskname, dump_info->timestamp,
865 dump_info->level, errstr);
871 /* Link up the TaperSource with the Device, including retries etc. */
872 static void run_device_output(taper_state_t * taper_state,
873 dump_info_t * dump_info) {
876 dump_info->current_part = 1;
877 dump_info->total_time.tv_sec = 0;
878 dump_info->total_time.tv_usec = 0;
879 dump_info->total_bytes = 0;
882 GTimeVal start_time, end_time, run_time;
883 StreamingRequirement streaming_mode;
884 queue_result_flags queue_result;
885 CountingConsumerData consumer_data;
886 dumpfile_t *this_header;
889 this_header = munge_headers(dump_info);
890 if (this_header == NULL) {
891 char * qdiskname = quote_string(dump_info->diskname);
892 char * errstr = taper_source_get_errmsg(dump_info->source);
894 errstr = "Failed reading dump header.";
895 errstr = quote_string(errstr);
897 "%s INPUT-ERROR TAPE-GOOD %s \"\"\n",
898 dump_info->handle, errstr);
899 log_add(L_FAIL, "%s %s %s %d %s",
900 dump_info->hostname, qdiskname, dump_info->timestamp,
901 dump_info->level, errstr);
907 if (!find_and_label_new_tape(taper_state, dump_info)) {
908 bail_no_volume(dump_info, taper_state->last_errmsg);
909 dumpfile_free(this_header);
913 while (!device_start_file(taper_state->device, this_header)) {
914 /* Close the device. */
915 device_finish(taper_state->device);
916 g_object_unref(taper_state->device);
917 taper_state->device = NULL;
919 if (!find_and_label_new_tape(taper_state, dump_info)) {
920 bail_no_volume(dump_info, taper_state->last_errmsg);
921 dumpfile_free(this_header);
925 dumpfile_free(this_header);
927 bzero(&val, sizeof(val));
928 if (!device_property_get(taper_state->device, PROPERTY_STREAMING, &val)
929 || !G_VALUE_HOLDS(&val, STREAMING_REQUIREMENT_TYPE)) {
930 g_fprintf(stderr, "taper: Couldn't get streaming type!\n");
931 streaming_mode = STREAMING_REQUIREMENT_REQUIRED;
933 streaming_mode = g_value_get_enum(&val);
936 file_number = taper_state->device->file;
938 consumer_data.next_consumer = device_write_consumer;
939 consumer_data.next_consumer_data = taper_state->device;
940 consumer_data.bytes_written = 0;
942 g_get_current_time(&start_time);
944 if (getconf_seen(CNF_DEVICE_OUTPUT_BUFFER_SIZE)) {
945 max_memory = getconf_size(CNF_DEVICE_OUTPUT_BUFFER_SIZE);
946 if (getconf_seen(CNF_TAPEBUFS)) {
948 "Configuration directives 'device_output_buffer_size' "
950 "'tapebufs' are incompatible; using former.\n");
952 } else if (getconf_seen(CNF_TAPEBUFS)) {
953 max_memory = getconf_int(CNF_TAPEBUFS) *
954 taper_state->device->block_size;
957 max_memory = getconf_size(CNF_DEVICE_OUTPUT_BUFFER_SIZE);
960 queue_result = do_consumer_producer_queue_full
961 (taper_source_producer,
965 taper_state->device->block_size, max_memory,
968 g_get_current_time(&end_time);
969 run_time = timesub(end_time, start_time);
971 /* The device_write_consumer leaves the file open, so close it now. */
972 if (!device_finish_file(taper_state->device)) {
973 queue_result = queue_result | QUEUE_CONSUMER_ERROR;
976 if (!finish_part_attempt(taper_state, dump_info, queue_result,
977 run_time, consumer_data.bytes_written)) {
983 /* Handle a PORT_WRITE command. */
984 static void process_port_write(taper_state_t * state,
985 struct cmdargs * cmdargs) {
986 dump_info_t dump_state;
988 guint64 fallback_splitsize;
989 char * split_diskbuffer;
990 char * argnames[] = {"command", /* 0 */
997 "split_diskbuffer", /* 7 */
998 "fallback_splitsize", /* 8 */
1001 validate_args(cmdargs, argnames);
1003 dump_state.handle = g_strdup(cmdargs->argv[1]);
1004 dump_state.hostname = g_strdup(cmdargs->argv[2]);
1005 dump_state.diskname = g_strdup(cmdargs->argv[3]);
1008 dump_state.level = strtol(cmdargs->argv[4], NULL, 10);
1010 error("error [taper PORT-WRITE: Invalid dump level %s]",
1012 g_assert_not_reached();
1015 dump_state.timestamp = strdup(cmdargs->argv[5]);
1018 splitsize = g_ascii_strtoull(cmdargs->argv[6], NULL, 10);
1020 error("error [taper PORT-WRITE: Invalid splitsize %s]",
1022 g_assert_not_reached();
1025 if (strcmp(cmdargs->argv[7], "NULL") == 0) {
1026 split_diskbuffer = NULL;
1028 split_diskbuffer = g_strdup(cmdargs->argv[7]);
1032 fallback_splitsize = g_ascii_strtoull(cmdargs->argv[8], NULL, 10);
1034 error("error [taper PORT-WRITE: Invalid fallback_splitsize %s]",
1036 g_assert_not_reached();
1039 dump_state.id_string = g_strdup_printf("%s:%s.%d", dump_state.hostname,
1040 dump_state.diskname,
1043 if (!open_read_socket(&dump_state, split_diskbuffer, splitsize,
1044 fallback_splitsize)) {
1045 free(split_diskbuffer);
1048 free(split_diskbuffer);
1050 run_device_output(state, &dump_state);
1052 free_dump_info(&dump_state);
1055 /* Handle a FILE_WRITE command. */
1056 static void process_file_write(taper_state_t * state,
1057 struct cmdargs * cmdargs) {
1058 dump_info_t dump_state;
1059 char * holding_disk_file;
1061 char * argnames[] = {"command", /* 0 */
1067 "datestamp", /* 6 */
1068 "splitsize", /* 7 */
1071 validate_args(cmdargs, argnames);
1073 dump_state.handle = g_strdup(cmdargs->argv[1]);
1074 holding_disk_file = g_strdup(cmdargs->argv[2]);
1075 dump_state.hostname = g_strdup(cmdargs->argv[3]);
1076 dump_state.diskname = g_strdup(cmdargs->argv[4]);
1079 dump_state.level = strtol(cmdargs->argv[5], NULL, 10);
1081 error("error [taper FILE-WRITE: Invalid dump level %s]",
1083 g_assert_not_reached();
1086 dump_state.timestamp = strdup(cmdargs->argv[6]);
1089 splitsize = g_ascii_strtoull(cmdargs->argv[7], NULL, 10);
1091 error("error [taper FILE-WRITE: Invalid splitsize %s]",
1093 g_assert_not_reached();
1096 dump_state.id_string = g_strdup_printf("%s:%s.%d", dump_state.hostname,
1097 dump_state.diskname,
1100 dump_state.source = taper_source_new(dump_state.handle, FILE_WRITE,
1101 holding_disk_file, -1,
1102 NULL, splitsize, -1);
1103 /* FIXME: This should be handled properly. */
1104 g_assert(dump_state.source != NULL);
1106 run_device_output(state, &dump_state);
1108 free_dump_info(&dump_state);
1109 amfree(holding_disk_file);
1112 /* Send QUITTING message to driver and associated logging. Always
1114 static gboolean send_quitting(taper_state_t * state) {
1115 putresult(QUITTING, "\n");
1116 g_fprintf(stderr,"taper: DONE\n");
1121 /* This function recieves the START_TAPER command from driver, and
1122 returns the attached timestamp. */
1123 static gboolean find_first_tape(taper_state_t * state) {
1124 struct cmdargs *cmdargs;
1125 tape_search_request_t search_request;
1126 GThread * tape_search = NULL;
1127 gboolean use_threads;
1129 /* We save the value here in case it changes while we're running. */
1130 use_threads = g_thread_supported();
1132 search_request.state = state;
1133 search_request.prolong = TRUE;
1134 search_request.errmsg = NULL;
1137 tape_search = g_thread_create(tape_search_thread,
1138 &search_request, TRUE, NULL);
1143 switch (cmdargs->cmd) {
1145 gboolean search_result;
1146 state->driver_start_time = strdup(cmdargs->argv[1]);
1148 search_result = GPOINTER_TO_INT(g_thread_join(tape_search));
1151 GPOINTER_TO_INT(tape_search_thread(&search_request));
1153 if (search_result) {
1154 putresult(TAPER_OK, "\n");
1156 putresult(TAPE_ERROR, "Could not find a tape to use.\n");
1157 log_add(L_ERROR, "no-tape [%s]", "Could not find a tape to use");
1158 if (search_request.errmsg != NULL) {
1160 c = c1 = search_request.errmsg;
1161 while (*c != '\0') {
1164 log_add(L_WARNING,"%s", c1);
1169 if (strlen(c1) > 1 )
1170 log_add(L_WARNING,"%s", c1);
1173 amfree(search_request.errmsg);
1174 free_cmdargs(cmdargs);
1178 search_request.prolong = FALSE;
1180 g_thread_join(tape_search);
1182 free_cmdargs(cmdargs);
1183 return send_quitting(state);
1185 error("error [file_reader_side cmd %d argc %d]", cmdargs->cmd, cmdargs->argc);
1188 g_assert_not_reached();
1191 /* In running mode (not startup mode), get a command from driver and
1193 static gboolean process_driver_command(taper_state_t * state) {
1194 struct cmdargs *cmdargs;
1197 /* This will return QUIT if driver has died. */
1199 switch (cmdargs->cmd) {
1212 process_port_write(state, cmdargs);
1227 process_file_write(state, cmdargs);
1231 free_cmdargs(cmdargs);
1232 if (state->device && state->device->volume_label) {
1233 log_add(L_INFO, "tape %s kb %lld fm %d [OK]\n",
1234 state->device->volume_label,
1235 (long long)((state->total_bytes+(off_t)1023) / (off_t)1024),
1236 state->device->file);
1238 return send_quitting(state);
1240 if (cmdargs->argc >= 1) {
1241 q = quote_string(cmdargs->argv[0]);
1243 q = stralloc("(no input?)");
1245 putresult(BAD_COMMAND, "%s\n", q);
1249 free_cmdargs(cmdargs);
1254 int main(int argc, char ** argv) {
1255 char * tapelist_name;
1256 taper_state_t state;
1257 config_overwrites_t *cfg_ovr = NULL;
1258 char *cfg_opt = NULL;
1261 * Configure program for internationalization:
1262 * 1) Only set the message locale for now.
1263 * 2) Set textdomain for all amanda related programs to "amanda"
1264 * We don't want to be forced to support dozens of message catalogs.
1266 setlocale(LC_MESSAGES, "C");
1267 textdomain("amanda");
1275 init_taper_state(&state);
1277 /* Don't die when child closes pipe */
1278 signal(SIGPIPE, SIG_IGN);
1280 g_fprintf(stderr, _("%s: pid %ld executable %s version %s\n"),
1281 get_pname(), (long) getpid(), argv[0], version());
1282 dbprintf(_("%s: pid %ld executable %s version %s\n"),
1283 get_pname(), (long) getpid(), argv[0], version());
1285 /* Process options */
1287 cfg_ovr = extract_commandline_config_overwrites(&argc, &argv);
1290 error("Too many arguments!\n");
1291 g_assert_not_reached();
1295 config_init(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_USE_CWD, cfg_opt);
1296 apply_config_overwrites(cfg_ovr);
1298 if (config_errors(NULL) >= CFGERR_ERRORS) {
1299 g_critical(_("errors processing config file"));
1304 set_logerror(logerror);
1306 check_running_as(RUNNING_AS_DUMPUSER);
1308 dbrename(get_config_name(), DBG_SUBDIR_SERVER);
1310 log_add(L_INFO, "%s pid %ld", get_pname(), (long)getpid());
1312 tapelist_name = config_dir_relative(getconf_str(CNF_TAPELIST));
1314 if (read_tapelist(tapelist_name) != 0) {
1315 log_add(L_INFO, "pid-done %ld", (long)getpid());
1316 error("could not load tapelist \"%s\"", tapelist_name);
1317 g_assert_not_reached();
1319 amfree(tapelist_name);
1321 state.have_changer = changer_init();
1322 if (state.have_changer < 0) {
1323 log_add(L_INFO, "pid-done %ld", (long)getpid());
1324 error("changer initialization failed: %s", strerror(errno));
1325 g_assert_not_reached();
1328 state.next_tape_label = NULL;
1329 state.next_tape_device = NULL;
1332 if (!find_first_tape(&state)) {
1333 log_add(L_INFO, "pid-done %ld", (long)getpid());
1334 return EXIT_SUCCESS;
1337 while (process_driver_command(&state));
1338 log_add(L_INFO, "pid-done %ld", (long)getpid());
1339 return EXIT_SUCCESS;