Imported Upstream version 2.6.1p1
[debian/amanda] / server-src / taper.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998, 2000 University of Maryland at College Park
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of U.M. not be used in advertising or
11  * publicity pertaining to distribution of the software without specific,
12  * written prior permission.  U.M. makes no representations about the
13  * suitability of this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  *
16  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  * Authors: the Amanda Development Team.  Its members are listed in a
24  * file named AUTHORS, in the root directory of this distribution.
25  */
26 /* $Id: taper.c 6512 2007-05-24 17:00:24Z ian $
27  *
28  * moves files from holding disk to tape, or from a socket to tape
29  */
30
31 /* FIXME: This file needs to use gettext. */
32
33 #include <glib.h>
34 #include "physmem.h"
35
36 #include "changer.h"
37 #include "clock.h"
38 #include "conffile.h"
39 #include "device.h"
40 #include "logfile.h"
41 #include "server_util.h"
42 #include "stream.h"
43 #include "tapefile.h"
44 #include "taperscan.h"
45 #include "taper-source.h"
46 #include "timestamp.h"
47 #include "util.h"
48 #include "version.h"
49 #include "queueing.h"
50 #include "device-queueing.h"
51
52 /* FIXME: This should not be here. */
53 #define CONNECT_TIMEOUT (2*60)
54
55 /* Use this instead of global variables, so that we are reentrant. */
56 typedef struct {
57     Device * device;
58     char * driver_start_time;
59     int    cur_tape;
60     char * next_tape_label;
61     char * next_tape_device;
62     taper_scan_tracker_t * taper_scan_tracker;
63     char * last_errmsg;
64     off_t  total_bytes;
65     int have_changer;
66 } taper_state_t;
67
68 typedef struct {
69     char * handle;
70     char * hostname;
71     char * diskname;
72     int level;
73     char * timestamp;
74     char * id_string;
75     TaperSource * source;
76     int current_part;
77     GTimeVal total_time;
78     guint64 total_bytes;
79 } dump_info_t;
80
81 static gboolean label_new_tape(taper_state_t * state, dump_info_t * dump_info);
82
83 static void init_taper_state(taper_state_t* state) {
84     state->device = NULL;
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;
89 }
90
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);
99         state->device = NULL;
100     }
101 }
102
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);
111         info->source = NULL;
112     }
113 }
114
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,
119                           char ** argnames) {
120     int len = g_strv_length(argnames);
121
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]);
125     }
126
127     if (len < cmdargs->argc) {
128         error("error [taper %s: Too many args: Got %d, expected %d.]",
129               cmdstr[cmdargs->cmd], cmdargs->argc, len);
130     }
131 }
132
133 /* Open a socket to the dumper. Returns TRUE if everything is happy, FALSE
134    otherwise. */
135 static gboolean open_read_socket(dump_info_t * info, char * split_diskbuffer,
136                              guint64 splitsize, guint64 fallback_splitsize) {
137     in_port_t port = 0;
138     int socket;
139     int fd;
140     int result;
141     struct addrinfo *res;
142
143     if ((result = resolve_hostname("localhost", 0, &res, NULL) != 0)) {
144         char *m;
145         char *q;
146         int save_errno = errno;
147         char *qdiskname = quote_string(info->diskname);
148
149         m = vstralloc("[localhost resolve failure: ",
150                       strerror(save_errno),
151                       "]",
152                       NULL);
153         q = quote_string(m);
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,
157                 info->level, q);
158         amfree(qdiskname);
159         amfree(m);
160         amfree(q);
161         return FALSE;
162     }
163
164     socket = stream_server(res->ai_family, &port, 0, STREAM_BUFSIZE, 0);
165     freeaddrinfo(res);
166
167     if (socket < 0) {
168         char *m;
169         char *q;
170         int save_errno = errno;
171         char *qdiskname = quote_string(info->diskname);
172
173         m = vstralloc("[port create failure: ",
174                       strerror(save_errno),
175                       "]",
176                       NULL);
177         q = quote_string(m);
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,
181                 info->level, q);
182         amfree(qdiskname);
183         amfree(m);
184         amfree(q);
185         return FALSE;
186     }
187
188     putresult(PORT, "%d\n", port);
189
190     fd = stream_accept(socket, CONNECT_TIMEOUT, 0, STREAM_BUFSIZE);
191
192     if (fd < 0) {
193         char *m, *q;
194         int save_errno = errno;
195         char *qdiskname = quote_string(info->diskname);
196         m = vstralloc("[port connect failure: ",
197                       strerror(save_errno),
198                       "]",
199                       NULL);
200         q = quote_string(m);
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,
204                 info->level, q);
205         amfree(qdiskname);
206         aclose(socket);
207         amfree(m);
208         amfree(q);
209         return FALSE;
210     } else {
211         aclose(socket);
212     }
213
214     info->source = taper_source_new(info->handle, PORT_WRITE, NULL, fd,
215                                     split_diskbuffer, splitsize,
216                                     fallback_splitsize);
217     /* FIXME: This should be handled properly. */
218     g_assert(info->source != NULL);
219     return TRUE;
220 }
221
222 typedef struct {
223     ConsumerFunctor next_consumer;
224     gpointer next_consumer_data;
225     guint64 bytes_written;
226 } CountingConsumerData;
227
228 /* A ConsumerFunctor. This consumer just passes its arguments on to a
229    second consumer, but counts the number of bytes successfully
230    written. */
231 static ssize_t counting_consumer(gpointer user_data, queue_buffer_t * buffer) {
232     ssize_t result;
233     CountingConsumerData * data = user_data;
234
235     result = data->next_consumer(data->next_consumer_data, buffer);
236     
237     if (result > 0) {
238         data->bytes_written += result;
239     }
240
241     return result;
242 }
243
244 static gboolean boolean_prolong(void * data) {
245     if (data == NULL) {
246         return TRUE; /* Do not interrupt. */
247     } else {
248         return *(gboolean*)data;
249     }
250 }
251
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;
258     int result;
259     result = taper_scan(NULL, label, &timestamp, 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"));
265         return FALSE;
266     } else if (result < 0) {
267         g_fprintf(stderr, _("Failed taper scan: %s\n"), (*error_message)?(*error_message):_("(no error message)"));
268         amfree(timestamp);
269         return FALSE;
270     } else {
271         g_fprintf(stderr, _("taper: using label `%s' date `%s'\n"), *label,
272                 state->driver_start_time);
273         if (result == 3) {
274             log_add(L_INFO,
275             _("Will write new label `%s' to new tape"),
276                     *label);
277         }
278
279     }
280     amfree(timestamp);
281     return TRUE;
282 }
283
284 typedef struct {
285     taper_state_t * state;
286     gboolean prolong; /* scan stops when this is FALSE. */
287     char *errmsg;
288 } tape_search_request_t;
289
290 /* A GThread that runs taper_scan. */
291 static gpointer tape_search_thread(gpointer data) {
292     tape_search_request_t * request = data;
293
294     if (request->state->next_tape_label != NULL &&
295         request->state->next_tape_device != NULL) {
296         return GINT_TO_POINTER(TRUE);
297     } else {
298         amfree(request->state->next_tape_label);
299         amfree(request->state->next_tape_device);
300     }
301
302     return GINT_TO_POINTER
303         (simple_taper_scan(request->state,
304                            &(request->prolong),
305                            &(request->errmsg)));
306 }
307
308 static void log_taper_scan_errmsg(char * errmsg) {
309     char *c, *c1;
310     if (errmsg == NULL)
311         return;
312
313     c = c1 = errmsg;
314     while (*c != '\0') {
315         if (*c == '\n') {
316             *c = '\0';
317             log_add(L_WARNING,"%s", c1);
318             c1 = c+1;
319         }
320         c++;
321     }
322     if (strlen(c1) > 1 )
323         log_add(L_WARNING,"%s", c1);
324     amfree(errmsg);
325 }
326
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;
335     cmd_t cmd;
336
337     if (state->device != NULL) {
338         return TRUE;
339     }
340
341     /* We save the value here in case it changes while we're running. */
342     use_threads = g_thread_supported();
343
344     search_request.state = state;
345     search_request.prolong = TRUE;
346     search_request.errmsg = NULL;
347     if (use_threads) {
348         tape_search = g_thread_create(tape_search_thread,
349                                       &search_request, TRUE, NULL);
350     }
351     
352     putresult(REQUEST_NEW_TAPE, "%s\n", dump->handle);
353     cmdargs = getcmd();
354     cmd = cmdargs->cmd;
355
356     switch (cmd) {
357     default:
358         g_fprintf(stderr, "taper: Got odd message from driver, expected NEW-TAPE or NO-NEW-TAPE.\n");
359         /* FALLTHROUGH. */
360     case NEW_TAPE: {
361         gboolean search_result;
362         if (use_threads) {
363             search_result = GPOINTER_TO_INT(g_thread_join(tape_search));
364         } else {
365             search_result =
366                 GPOINTER_TO_INT(tape_search_thread(&search_request));
367         }
368         if (search_result) {
369             /* We don't say NEW_TAPE until we actually write the label. */
370             amfree(search_request.errmsg);
371             free_cmdargs(cmdargs);
372             return TRUE;
373         } else {
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);
378             return FALSE;
379         }
380     }
381     case NO_NEW_TAPE:
382         search_request.prolong = FALSE;
383         if (use_threads) {
384             g_thread_join(tape_search);
385         }
386         log_add(L_ERROR, "no-tape [%s]", cmdargs->argv[1]);
387         state->last_errmsg = stralloc(cmdargs->argv[1]);
388         free_cmdargs(cmdargs);
389         return FALSE;
390     }
391     /* NOTREACHED */
392 }
393
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))
399         return TRUE;
400     if ((old_timestamp == NULL) != (device->volume_time == NULL))
401         return TRUE;
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)
404         return TRUE;
405     if (old_timestamp != NULL &&
406         strcmp(old_timestamp, device->volume_time) != 0)
407         return TRUE;
408
409     /* If we got here, everything is cool. */
410     return FALSE;
411 }
412
413 static void
414 update_tapelist(
415     taper_state_t *state)
416 {
417     char *tapelist_name = NULL;
418     char *tapelist_name_old = NULL;
419     tape_t *tp;
420     char *comment = NULL;
421
422     tapelist_name = config_dir_relative(getconf_str(CNF_TAPELIST));
423     if (state->cur_tape == 0) {
424         tapelist_name_old = stralloc2(tapelist_name, ".yesterday");
425     } else {
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);
430     }
431
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);
435         /*NOTREACHED*/
436     }
437
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));
442         /*NOTREACHED*/
443     }
444     amfree(tapelist_name_old);
445
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);
450
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,
455                   comment);
456     if (write_tapelist(tapelist_name)) {
457         error("could not write tapelist: %s", strerror(errno));
458         /*NOTREACHED*/
459     }
460     amfree(tapelist_name);
461     amfree(comment);
462 }
463
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) {
469         return TRUE;
470     }
471     state->total_bytes = 0;
472  
473     if (!find_new_tape(state, dump_info)) {
474         return FALSE;
475     }
476
477     return label_new_tape(state, dump_info);
478 }
479
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;
486
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. */
491
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);
496
497     if (state->device->status != DEVICE_STATUS_SUCCESS)
498         goto skip_volume;
499
500     if (!device_configure(state->device, TRUE))
501         goto skip_volume;
502
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))
508         goto skip_volume;
509
510     old_volume_name = g_strdup(state->device->volume_label);
511     old_volume_time = g_strdup(state->device->volume_time);
512
513     if (!device_start(state->device, ACCESS_WRITE, state->next_tape_label,
514                       state->driver_start_time)) {
515         gboolean tape_used;
516
517         /* Something broke, see if we can tell if the volume was erased or
518          * not. */
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));
522
523         if (!device_finish(state->device))
524             goto request_new_volume;
525
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;
532
533         tape_used = check_volume_changed(state->device, old_volume_name, 
534                                          old_volume_time);
535
536         if (tape_used)
537             goto request_new_volume;
538         else
539             goto skip_volume;
540     }
541
542     amfree(old_volume_name);
543     amfree(old_volume_time);
544     amfree(state->next_tape_label);
545
546     update_tapelist(state);
547     state->cur_tape++;
548
549     if (state->have_changer &&
550         changer_label("UNKNOWN", state->device->volume_label) != 0) {
551         error(_("couldn't update barcode database"));
552         /*NOTREACHED*/
553     }
554
555     log_add(L_START, "datestamp %s label %s tape %d",
556             state->driver_start_time, state->device->volume_label,
557             state->cur_tape);
558     putresult(NEW_TAPE, "%s %s\n", dump_info->handle,
559               state->device->volume_label);
560
561     return TRUE;
562
563 request_new_volume:
564     /* Tell the driver we overwrote this volume, even if it was empty, and request
565      * a new volume. */
566     if (state->device)
567         state->last_errmsg = newstralloc(state->last_errmsg, device_error_or_status(state->device));
568     else
569         state->last_errmsg = newstralloc(state->last_errmsg, "(unknown)");
570
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,
577                 state->last_errmsg);
578     } else {
579         log_add(L_WARNING, "Problem writing label '%s' to new volume "
580                 "(volume may be erased): %s\n", state->next_tape_label,
581                 state->last_errmsg);
582     }
583
584     if (state->device) {
585         g_object_unref(state->device);
586         state->device = NULL;
587     }
588
589     amfree(state->next_tape_label);
590     amfree(old_volume_name);
591     amfree(old_volume_time);
592
593     return find_and_label_new_tape(state, dump_info);
594
595 skip_volume:
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. */
598     if (state->device)
599         state->last_errmsg = newstralloc(state->last_errmsg, device_error_or_status(state->device));
600     else
601         state->last_errmsg = newstralloc(state->last_errmsg, "(unknown)");
602
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);
607     } else {
608         log_add(L_WARNING, "Problem writing label '%s' to new volume "
609                 "(old volume data intact): %s\n", state->next_tape_label,
610                 state->last_errmsg);
611     }
612
613     if (state->device) {
614         g_object_unref(state->device);
615         state->device = NULL;
616     }
617
618     amfree(state->next_tape_label);
619     amfree(old_volume_name);
620     amfree(old_volume_time);
621
622     request.state = state;
623     request.prolong = TRUE;
624     request.errmsg = NULL;
625     search_result = GPOINTER_TO_INT(tape_search_thread(&request));
626     if (search_result) {
627         amfree(request.errmsg);
628         return label_new_tape(state, dump_info);
629     } else {
630         /* Problem finding a new tape! */
631         log_taper_scan_errmsg(request.errmsg);
632         putresult(NO_NEW_TAPE, "%s\n", dump_info->handle);
633         return FALSE;
634     }
635 }
636
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.
646      */
647        
648     if (taper_source_is_partial(dump_info->source)) {
649         *result_cmd = DONE;
650         *result_log = L_PARTIAL;
651     } else {
652         *result_cmd = DONE;
653         *result_log = L_DONE;
654     }
655 }
656
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);
661
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,
666             errstr);
667     amfree(qdiskname);
668 }
669
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);
679         
680     char * qdiskname = quote_string(dump_info->diskname);
681
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;
685
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;
697         
698         if (taper_source_get_end_of_data(dump_info->source)) {
699             cmd_t result_cmd;
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);
704
705             find_completion_tags(dump_info, &result_cmd, &result_log);
706
707             g_object_unref(dump_info->source);
708             dump_info->source = NULL;
709         
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,
717                       dump_kbps);
718             
719             amfree(qdiskname);
720             return FALSE;
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 ++;
724             amfree(qdiskname);
725             return TRUE;
726         }
727         /* If we didn't read EOF or EOP, then an error
728            occured. But we read QUEUE_SUCCESS, so something is
729            b0rked. */
730         g_assert_not_reached();
731     } else {
732         char * volume_label = strdup(taper_state->device->volume_label);
733         int file_number = taper_state->device->file;
734         double dump_time, dump_kbps;
735         guint64 dump_kbytes;
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));
740
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,
747                 consumer_errstr);
748         log_add(L_INFO, "tape %s kb %lld fm %d [OK]\n",
749                 volume_label,
750                 (long long)((taper_state->total_bytes+(off_t)1023) / (off_t)1024),
751                 taper_state->device->file);
752
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);
759
760             /* Close the device. */
761             device_finish(taper_state->device);
762             g_object_unref(taper_state->device);
763             taper_state->device = NULL;
764         }
765         
766         amfree(volume_label);
767         
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. */
775                 amfree(qdiskname);
776                 return TRUE;
777             }
778         }
779
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);
785         
786         putresult(PARTIAL,
787                   "%s INPUT-%s TAPE-%s "
788                   "\"[sec %f kb %ju kps %f]\" %s %s\n",
789                   dump_info->handle,
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,
796                             consumer_errstr);
797         } else {
798             put_partial_log(dump_info, dump_time, dump_kbytes,
799                             producer_errstr);
800         }
801         amfree(producer_errstr);
802         amfree(consumer_errstr);
803     }
804
805     amfree(qdiskname);
806     return FALSE;
807 }
808
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) {
812     dumpfile_t * rval;
813     int expected_splits;
814     
815     rval = taper_source_get_first_header(dump_info->source);
816
817     if (rval == NULL) {
818         return NULL;
819     }
820
821     rval->cont_filename[0] = '\0';
822
823     expected_splits = taper_source_predict_parts(dump_info->source);
824
825     if (expected_splits != 1) {
826         rval->type = F_SPLIT_DUMPFILE;
827         rval->partnum = dump_info->current_part;
828         rval->totalparts = expected_splits;
829     }
830
831     return rval;
832 }
833
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,
840     char *errmsg)
841 {
842     char *errstr;
843     if (errmsg)
844         errstr = quote_string(errmsg);
845     else
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;
852         putresult(PARTIAL,
853                   "%s INPUT-GOOD TAPE-ERROR "
854                   "\"[sec %f kb %ju kps %f]\" \"\" %s\n",
855                   dump_info->handle, 
856                   dump_time, (uintmax_t)dump_kbytes, dump_kbps, errstr);
857         put_partial_log(dump_info, dump_time, dump_kbytes, errstr);
858     } else {
859         char * qdiskname = quote_string(dump_info->diskname);
860         putresult(FAILED,
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);
866         amfree(qdiskname);
867     }
868     amfree(errstr);
869 }
870
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) {
874     GValue val;
875     guint file_number;
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;
880
881     for (;;) {
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;
887         size_t max_memory;
888         
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);
893             if (!errstr)
894                 errstr = "Failed reading dump header.";
895             errstr = quote_string(errstr);
896             putresult(FAILED,
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);
902             amfree(qdiskname);
903             amfree(errstr);
904             return;
905         }            
906
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);
910             return;
911         }
912
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;
918
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);
922                 return;
923             }
924         }
925         dumpfile_free(this_header);
926
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;
932         } else {
933             streaming_mode = g_value_get_enum(&val);
934         }
935     
936         file_number = taper_state->device->file;
937
938         consumer_data.next_consumer = device_write_consumer;
939         consumer_data.next_consumer_data = taper_state->device;
940         consumer_data.bytes_written = 0;
941
942         g_get_current_time(&start_time);
943
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)) {
947                 g_fprintf(stderr,
948                         "Configuration directives 'device_output_buffer_size' "
949                         "and \n"
950                         "'tapebufs' are incompatible; using former.\n");
951             }
952         } else if (getconf_seen(CNF_TAPEBUFS)) {
953             max_memory = getconf_int(CNF_TAPEBUFS) *
954                 taper_state->device->block_size;
955         } else {
956             /* Use default. */
957             max_memory = getconf_size(CNF_DEVICE_OUTPUT_BUFFER_SIZE);
958         }
959
960         queue_result = do_consumer_producer_queue_full
961             (taper_source_producer,
962              dump_info->source,
963              counting_consumer,
964              &consumer_data,
965              taper_state->device->block_size, max_memory,
966              streaming_mode);
967
968         g_get_current_time(&end_time);
969         run_time = timesub(end_time, start_time);
970
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;
974         }
975
976         if (!finish_part_attempt(taper_state, dump_info, queue_result,
977                                  run_time, consumer_data.bytes_written)) {
978             break;
979         }
980     }
981 }
982
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;
987     guint64 splitsize;
988     guint64 fallback_splitsize;
989     char * split_diskbuffer;
990     char * argnames[] = {"command",               /* 0 */
991                          "handle",                /* 1 */
992                          "hostname",              /* 2 */
993                          "diskname",              /* 3 */
994                          "level",                 /* 4 */
995                          "datestamp",             /* 5 */
996                          "splitsize",             /* 6 */
997                          "split_diskbuffer",      /* 7 */
998                          "fallback_splitsize",    /* 8 */
999                           NULL };
1000
1001     validate_args(cmdargs, argnames);
1002
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]);
1006     
1007     errno = 0;
1008     dump_state.level = strtol(cmdargs->argv[4], NULL, 10);
1009     if (errno != 0) {
1010         error("error [taper PORT-WRITE: Invalid dump level %s]",
1011               cmdargs->argv[4]);
1012         g_assert_not_reached();
1013     }
1014     
1015     dump_state.timestamp = strdup(cmdargs->argv[5]);
1016
1017     errno = 0;
1018     splitsize = g_ascii_strtoull(cmdargs->argv[6], NULL, 10);
1019     if (errno != 0) {
1020         error("error [taper PORT-WRITE: Invalid splitsize %s]",
1021               cmdargs->argv[6]);
1022         g_assert_not_reached();
1023     }
1024     
1025     if (strcmp(cmdargs->argv[7], "NULL") == 0) {
1026         split_diskbuffer = NULL;
1027     } else {
1028         split_diskbuffer = g_strdup(cmdargs->argv[7]);
1029     }
1030     
1031     errno = 0;
1032     fallback_splitsize = g_ascii_strtoull(cmdargs->argv[8], NULL, 10);
1033     if (errno != 0) {
1034         error("error [taper PORT-WRITE: Invalid fallback_splitsize %s]",
1035               cmdargs->argv[8]);
1036         g_assert_not_reached();
1037     }
1038
1039     dump_state.id_string = g_strdup_printf("%s:%s.%d", dump_state.hostname,
1040                                            dump_state.diskname,
1041                                            dump_state.level);
1042     
1043     if (!open_read_socket(&dump_state, split_diskbuffer, splitsize,
1044                           fallback_splitsize)) {
1045         free(split_diskbuffer);
1046         return;
1047     }
1048     free(split_diskbuffer);
1049
1050     run_device_output(state, &dump_state);
1051
1052     free_dump_info(&dump_state);
1053 }
1054
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;
1060     guint64 splitsize;
1061     char * argnames[] = {"command",               /* 0 */
1062                          "handle",                /* 1 */
1063                          "filename",              /* 2 */
1064                          "hostname",              /* 3 */
1065                          "diskname",              /* 4 */
1066                          "level",                 /* 5 */
1067                          "datestamp",             /* 6 */
1068                          "splitsize",             /* 7 */
1069                           NULL };
1070
1071     validate_args(cmdargs, argnames);
1072
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]);
1077     
1078     errno = 0;
1079     dump_state.level = strtol(cmdargs->argv[5], NULL, 10);
1080     if (errno != 0) {
1081         error("error [taper FILE-WRITE: Invalid dump level %s]",
1082               cmdargs->argv[5]);
1083         g_assert_not_reached();
1084     }
1085     
1086     dump_state.timestamp = strdup(cmdargs->argv[6]);
1087
1088     errno = 0;
1089     splitsize = g_ascii_strtoull(cmdargs->argv[7], NULL, 10);
1090     if (errno != 0) {
1091         error("error [taper FILE-WRITE: Invalid splitsize %s]",
1092               cmdargs->argv[7]);
1093         g_assert_not_reached();
1094     }
1095
1096     dump_state.id_string = g_strdup_printf("%s:%s.%d", dump_state.hostname,
1097                                            dump_state.diskname,
1098                                            dump_state.level);
1099     
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);
1105
1106     run_device_output(state, &dump_state);
1107
1108     free_dump_info(&dump_state);
1109     amfree(holding_disk_file);
1110 }
1111
1112 /* Send QUITTING message to driver and associated logging. Always
1113    returns false. */
1114 static gboolean send_quitting(taper_state_t * state) {
1115     putresult(QUITTING, "\n");
1116     g_fprintf(stderr,"taper: DONE\n");
1117     cleanup(state);
1118     return FALSE;
1119 }
1120
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;
1128
1129     /* We save the value here in case it changes while we're running. */
1130     use_threads = g_thread_supported();
1131
1132     search_request.state = state;
1133     search_request.prolong = TRUE;
1134     search_request.errmsg = NULL;
1135     
1136     if (use_threads) {
1137         tape_search = g_thread_create(tape_search_thread,
1138                                       &search_request, TRUE, NULL);
1139     }
1140
1141     cmdargs = getcmd();
1142
1143     switch (cmdargs->cmd) {
1144     case START_TAPER: {
1145         gboolean search_result;
1146         state->driver_start_time = strdup(cmdargs->argv[1]);
1147         if (use_threads) {
1148             search_result = GPOINTER_TO_INT(g_thread_join(tape_search));
1149         } else {
1150             search_result =
1151                 GPOINTER_TO_INT(tape_search_thread(&search_request));
1152         }
1153         if (search_result) {
1154             putresult(TAPER_OK, "\n");
1155         } else {
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) {
1159                 char *c, *c1;
1160                 c = c1 = search_request.errmsg;
1161                 while (*c != '\0') {
1162                     if (*c == '\n') {
1163                         *c = '\0';
1164                         log_add(L_WARNING,"%s", c1);
1165                         c1 = c+1;
1166                     }
1167                     c++;
1168                 }
1169                 if (strlen(c1) > 1 )
1170                     log_add(L_WARNING,"%s", c1);
1171             }
1172         }
1173         amfree(search_request.errmsg);
1174         free_cmdargs(cmdargs);
1175         return TRUE;
1176     }
1177     case QUIT:
1178         search_request.prolong = FALSE;
1179         if (use_threads) {
1180             g_thread_join(tape_search);
1181         }
1182         free_cmdargs(cmdargs);
1183         return send_quitting(state);
1184     default:
1185         error("error [file_reader_side cmd %d argc %d]", cmdargs->cmd, cmdargs->argc);
1186     }
1187
1188     g_assert_not_reached();
1189 }
1190
1191 /* In running mode (not startup mode), get a command from driver and
1192    deal with it. */
1193 static gboolean process_driver_command(taper_state_t * state) {
1194     struct cmdargs *cmdargs;
1195     char * q;
1196
1197     /* This will return QUIT if driver has died. */
1198     cmdargs = getcmd();
1199     switch (cmdargs->cmd) {
1200     case PORT_WRITE:
1201         /*
1202          * PORT-WRITE
1203          *   handle
1204          *   hostname
1205          *   features
1206          *   diskname
1207          *   level
1208          *   datestamp
1209          *   splitsize
1210          *   split_diskbuffer
1211          */
1212         process_port_write(state, cmdargs);
1213         break;
1214         
1215     case FILE_WRITE:
1216         /*
1217          * FILE-WRITE
1218          *   handle
1219          *   filename
1220          *   hostname
1221          *   features
1222          *   diskname
1223          *   level
1224          *   datestamp
1225          *   splitsize
1226          */
1227         process_file_write(state, cmdargs);
1228         break;
1229         
1230     case QUIT:
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);
1237         }
1238         return send_quitting(state);
1239     default:
1240         if (cmdargs->argc >= 1) {
1241             q = quote_string(cmdargs->argv[0]);
1242         } else {
1243             q = stralloc("(no input?)");
1244         }
1245         putresult(BAD_COMMAND, "%s\n", q);
1246         amfree(q);
1247         break;
1248     }
1249     free_cmdargs(cmdargs);
1250
1251     return TRUE;
1252 }
1253
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;
1259
1260     /*
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.
1265      */
1266     setlocale(LC_MESSAGES, "C");
1267     textdomain("amanda");
1268     
1269     safe_fd(-1, 0);
1270     set_pname("taper");
1271
1272     dbopen("server");
1273
1274     device_api_init();
1275     init_taper_state(&state);
1276
1277     /* Don't die when child closes pipe */
1278     signal(SIGPIPE, SIG_IGN);
1279
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());
1284
1285     /* Process options */
1286
1287     cfg_ovr = extract_commandline_config_overwrites(&argc, &argv);
1288
1289     if(argc > 2) {
1290         error("Too many arguments!\n");
1291         g_assert_not_reached();
1292     }
1293     if (argc > 1)
1294         cfg_opt = argv[1];
1295     config_init(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_USE_CWD, cfg_opt);
1296     apply_config_overwrites(cfg_ovr);
1297
1298     if (config_errors(NULL) >= CFGERR_ERRORS) {
1299         g_critical(_("errors processing config file"));
1300     }
1301
1302     safe_cd();
1303
1304     set_logerror(logerror);
1305
1306     check_running_as(RUNNING_AS_DUMPUSER);
1307
1308     dbrename(get_config_name(), DBG_SUBDIR_SERVER);
1309
1310     log_add(L_INFO, "%s pid %ld", get_pname(), (long)getpid());
1311
1312     tapelist_name = config_dir_relative(getconf_str(CNF_TAPELIST));
1313
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();
1318     }
1319     amfree(tapelist_name);
1320
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();
1326     }
1327
1328     state.next_tape_label = NULL;
1329     state.next_tape_device = NULL;
1330     state.cur_tape = 0;
1331     
1332     if (!find_first_tape(&state)) {
1333         log_add(L_INFO, "pid-done %ld", (long)getpid());
1334         return EXIT_SUCCESS;
1335     }
1336
1337     while (process_driver_command(&state));
1338     log_add(L_INFO, "pid-done %ld", (long)getpid());
1339     return EXIT_SUCCESS;
1340 }