2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 2009, 2010 Zmanda, Inc. All Rights Reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published
7 * by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
19 * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
26 #include "xfer-device.h"
33 * This declaration is entirely private; nothing but xfer_source_recovery() references
37 GType xfer_source_recovery_get_type(void);
38 #define XFER_SOURCE_RECOVERY_TYPE (xfer_source_recovery_get_type())
39 #define XFER_SOURCE_RECOVERY(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_source_recovery_get_type(), XferSourceRecovery)
40 #define XFER_SOURCE_RECOVERY_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_source_recovery_get_type(), XferSourceRecovery const)
41 #define XFER_SOURCE_RECOVERY_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), xfer_source_recovery_get_type(), XferSourceRecoveryClass)
42 #define IS_XFER_SOURCE_RECOVERY(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), xfer_source_recovery_get_type ())
43 #define XFER_SOURCE_RECOVERY_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), xfer_source_recovery_get_type(), XferSourceRecoveryClass)
45 static GObjectClass *parent_class = NULL;
48 * Main object structure
51 typedef struct XferSourceRecovery {
52 XferElement __parent__;
54 /* thread for monitoring directtcp transfers */
57 /* this mutex in this condition variable governs all variables below */
58 GCond *start_part_cond;
59 GMutex *start_part_mutex;
61 /* is this device currently paused and awaiting a new part? */
64 /* device to read from (refcounted) */
67 /* TRUE if use_device found the device unsuitable; this makes start_part
68 * a no-op, allowing the cancellation to be handled normally */
71 /* directtcp connection (only valid after XMSG_READY) */
72 DirectTCPConnection *conn;
75 /* and the block size for that device (reset to zero at the start of each
79 /* part size (potentially including any zero-padding from the
83 /* timer for the duration; NULL while paused or cancelled */
94 XferElementClass __parent__;
96 /* start reading the part at which DEVICE is positioned, sending an
97 * XMSG_PART_DONE when the part has been read */
98 void (*start_part)(XferSourceRecovery *self, Device *device);
100 /* use the given device, much like the same method for xfer-dest-taper */
101 void (*use_device)(XferSourceRecovery *self, Device *device);
102 } XferSourceRecoveryClass;
108 #define DBG(LEVEL, ...) if (debug_recovery >= LEVEL) { _xsr_dbg(__VA_ARGS__); }
110 _xsr_dbg(const char *fmt, ...)
115 arglist_start(argp, fmt);
116 g_vsnprintf(msg, sizeof(msg), fmt, argp);
118 g_debug("XSR thd-%p: %s", g_thread_self(), msg);
125 /* common code for both directtcp_listen_thread and directtcp_connect_thread;
126 * this is called after self->conn is filled in and carries out the data
127 * transfer over that connection. NOTE: start_part_mutex is HELD when this
130 directtcp_common_thread(
131 XferSourceRecovery *self)
133 XferElement *elt = XFER_ELEMENT(self);
136 /* send XMSG_READY to indicate it's OK to call start_part now */
137 DBG(2, "sending XMSG_READY");
138 xfer_queue_message(elt->xfer, xmsg_new(elt, XMSG_READY, 0));
140 /* now we sit around waiting for signals to write a part */
145 while (self->paused && !elt->cancelled) {
146 DBG(9, "waiting to be un-paused");
147 g_cond_wait(self->start_part_cond, self->start_part_mutex);
149 DBG(9, "done waiting");
151 if (elt->cancelled) {
152 g_mutex_unlock(self->start_part_mutex);
153 goto close_conn_and_send_done;
156 /* if the device is NULL, we're done */
161 self->part_timer = g_timer_new();
164 DBG(2, "reading part from %s", self->device->device_name);
165 if (!device_read_to_connection(self->device, G_MAXUINT64, &actual_size)) {
166 xfer_cancel_with_error(elt, _("error reading from device: %s"),
167 device_error_or_status(self->device));
168 g_mutex_unlock(self->start_part_mutex);
169 goto close_conn_and_send_done;
172 /* break on EOF; otherwise do another read_to_connection */
173 if (self->device->is_eof) {
177 DBG(2, "done reading part; sending XMSG_PART_DONE");
179 /* the device has signalled EOF (really end-of-part), so clean up instance
180 * variables and report the EOP to the caller in the form of an xmsg */
181 msg = xmsg_new(XFER_ELEMENT(self), XMSG_PART_DONE, 0);
182 msg->size = actual_size;
183 msg->duration = g_timer_elapsed(self->part_timer, NULL);
185 msg->fileno = self->device->file;
186 msg->successful = TRUE;
190 g_object_unref(self->device);
193 self->block_size = 0;
194 g_timer_destroy(self->part_timer);
195 self->part_timer = NULL;
197 xfer_queue_message(elt->xfer, msg);
199 g_mutex_unlock(self->start_part_mutex);
201 close_conn_and_send_done:
203 errmsg = directtcp_connection_close(self->conn);
204 g_object_unref(self->conn);
207 xfer_cancel_with_error(elt, _("error closing DirectTCP connection: %s"), errmsg);
208 wait_until_xfer_cancelled(elt->xfer);
212 xfer_queue_message(elt->xfer, xmsg_new(elt, XMSG_DONE, 0));
218 directtcp_connect_thread(
221 XferSourceRecovery *self = XFER_SOURCE_RECOVERY(data);
222 XferElement *elt = XFER_ELEMENT(self);
224 DBG(1, "(this is directtcp_connect_thread)")
226 /* first, we need to accept the incoming connection; we do this while
227 * holding the start_part_mutex, so that a part doesn't get started until
228 * we're finished with the device */
229 g_mutex_lock(self->start_part_mutex);
231 if (elt->cancelled) {
232 g_mutex_unlock(self->start_part_mutex);
236 g_assert(self->device != NULL); /* have a device */
237 g_assert(elt->output_listen_addrs != NULL); /* listening on it */
238 g_assert(self->listen_ok);
240 DBG(2, "accepting DirectTCP connection on device %s", self->device->device_name);
241 if (!device_accept(self->device, &self->conn, NULL, NULL)) {
242 xfer_cancel_with_error(elt,
243 _("error accepting DirectTCP connection: %s"),
244 device_error_or_status(self->device));
245 g_mutex_unlock(self->start_part_mutex);
246 wait_until_xfer_cancelled(elt->xfer);
249 DBG(2, "DirectTCP connection accepted");
251 return directtcp_common_thread(self);
254 xfer_queue_message(elt->xfer, xmsg_new(elt, XMSG_DONE, 0));
259 directtcp_listen_thread(
262 XferSourceRecovery *self = XFER_SOURCE_RECOVERY(data);
263 XferElement *elt = XFER_ELEMENT(self);
265 DBG(1, "(this is directtcp_listen_thread)");
267 /* we need to make an outgoing connection to downstream; we do this while
268 * holding the start_part_mutex, so that a part doesn't get started until
269 * we're finished with the device */
270 g_mutex_lock(self->start_part_mutex);
272 if (elt->cancelled) {
273 g_mutex_unlock(self->start_part_mutex);
277 g_assert(self->device != NULL); /* have a device */
278 g_assert(elt->downstream->input_listen_addrs != NULL); /* downstream listening */
280 DBG(2, "making DirectTCP connection on device %s", self->device->device_name);
281 if (!device_connect(self->device, FALSE, elt->downstream->input_listen_addrs,
282 &self->conn, NULL, NULL)) {
283 xfer_cancel_with_error(elt,
284 _("error making DirectTCP connection: %s"),
285 device_error_or_status(self->device));
286 g_mutex_unlock(self->start_part_mutex);
287 wait_until_xfer_cancelled(elt->xfer);
290 DBG(2, "DirectTCP connect succeeded");
292 return directtcp_common_thread(self);
295 xfer_queue_message(elt->xfer, xmsg_new(elt, XMSG_DONE, 0));
303 XferSourceRecovery *self = XFER_SOURCE_RECOVERY(elt);
305 if (elt->output_mech == XFER_MECH_DIRECTTCP_CONNECT) {
306 g_assert(self->device != NULL);
307 DBG(2, "listening for DirectTCP connection on device %s", self->device->device_name);
308 if (!device_listen(self->device, FALSE, &elt->output_listen_addrs)) {
309 xfer_cancel_with_error(elt,
310 _("error listening for DirectTCP connection: %s"),
311 device_error_or_status(self->device));
314 self->listen_ok = TRUE;
316 /* no output_listen_addrs for either XFER_MECH_DIRECTTCP_LISTEN or
317 * XFER_MECH_PULL_BUFFER */
318 elt->output_listen_addrs = NULL;
328 XferSourceRecovery *self = XFER_SOURCE_RECOVERY(elt);
330 if (elt->output_mech == XFER_MECH_DIRECTTCP_CONNECT) {
331 g_assert(elt->output_listen_addrs != NULL);
332 self->thread = g_thread_create(directtcp_connect_thread, (gpointer)self, FALSE, NULL);
333 return TRUE; /* we'll send XMSG_DONE */
334 } else if (elt->output_mech == XFER_MECH_DIRECTTCP_LISTEN) {
335 g_assert(elt->output_listen_addrs == NULL);
336 self->thread = g_thread_create(directtcp_listen_thread, (gpointer)self, FALSE, NULL);
337 return TRUE; /* we'll send XMSG_DONE */
339 /* nothing to prepare for - we're ready already! */
340 DBG(2, "not using DirectTCP: sending XMSG_READY immediately");
341 xfer_queue_message(elt->xfer, xmsg_new(elt, XMSG_READY, 0));
343 return FALSE; /* we won't send XMSG_DONE */
352 XferSourceRecovery *self = XFER_SOURCE_RECOVERY(elt);
358 g_assert(elt->output_mech == XFER_MECH_PULL_BUFFER);
359 g_mutex_lock(self->start_part_mutex);
362 /* make sure we have a device */
363 while (self->paused && !elt->cancelled)
364 g_cond_wait(self->start_part_cond, self->start_part_mutex);
366 /* indicate EOF on an cancel or when there are no more parts */
367 if (elt->cancelled || !self->device) {
371 /* start the timer if this is the first pull_buffer of this part */
372 if (!self->part_timer) {
373 DBG(2, "first pull_buffer of new part");
374 self->part_timer = g_timer_new();
377 /* loop until we read a full block, in case the blocks are larger than
379 if (self->block_size == 0)
380 self->block_size = (size_t)self->device->block_size;
383 buf = g_malloc(self->block_size);
384 devsize = (int)self->block_size;
385 result = device_read_block(self->device, buf, &devsize);
389 g_assert(*size > self->block_size);
390 self->block_size = devsize;
393 } while (result == 0);
395 /* if this block was successful, return it */
397 self->part_size += *size;
404 /* if we're not at EOF, it's an error */
405 if (!self->device->is_eof) {
406 xfer_cancel_with_error(elt,
407 _("error reading from %s: %s"),
408 self->device->device_name,
409 device_error_or_status(self->device));
410 wait_until_xfer_cancelled(elt->xfer);
414 /* the device has signalled EOF (really end-of-part), so clean up instance
415 * variables and report the EOP to the caller in the form of an xmsg */
416 DBG(2, "pull_buffer hit EOF; sending XMSG_PART_DONE");
417 msg = xmsg_new(XFER_ELEMENT(self), XMSG_PART_DONE, 0);
418 msg->size = self->part_size;
419 msg->duration = g_timer_elapsed(self->part_timer, NULL);
421 msg->fileno = self->device->file;
422 msg->successful = TRUE;
426 g_object_unref(self->device);
429 self->block_size = 0;
430 if (self->part_timer) {
431 g_timer_destroy(self->part_timer);
432 self->part_timer = NULL;
435 /* don't queue the XMSG_PART_DONE until we've adjusted all of our
436 * instance variables appropriately */
437 xfer_queue_message(elt->xfer, msg);
441 g_mutex_unlock(self->start_part_mutex);
444 /* initialize on first pass */
446 self->size = elt->size;
448 if (self->size == -1) {
454 if (*size > (guint64)self->size) {
455 /* return only self->size bytes */
465 g_mutex_unlock(self->start_part_mutex);
473 gboolean expect_eof G_GNUC_UNUSED)
475 XferSourceRecovery *self = XFER_SOURCE_RECOVERY(elt);
476 elt->cancelled = TRUE;
478 /* trigger the condition variable, in case the thread is waiting on it */
479 g_mutex_lock(self->start_part_mutex);
480 g_cond_broadcast(self->start_part_cond);
481 g_mutex_unlock(self->start_part_mutex);
488 XferSourceRecovery *self,
491 g_assert(!device || device->in_file);
493 DBG(2, "start_part called");
495 if (self->device_bad) {
496 /* use_device didn't like the device it got, but the xfer cancellation
497 * has not completed yet, so do nothing */
501 g_mutex_lock(self->start_part_mutex);
503 /* make sure we're ready to go */
504 g_assert(self->paused);
505 if (XFER_ELEMENT(self)->output_mech == XFER_MECH_DIRECTTCP_CONNECT
506 || XFER_ELEMENT(self)->output_mech == XFER_MECH_DIRECTTCP_LISTEN) {
507 g_assert(self->conn != NULL);
510 /* if we already have a device, it should have been given to use_device */
511 if (device && self->device)
512 g_assert(self->device == device);
515 g_object_unref(self->device);
517 g_object_ref(device);
518 self->device = device;
520 self->paused = FALSE;
522 DBG(2, "triggering condition variable");
523 g_cond_broadcast(self->start_part_cond);
524 g_mutex_unlock(self->start_part_mutex);
529 XferSourceRecovery *xdtself,
532 XferSourceRecovery *self = XFER_SOURCE_RECOVERY(xdtself);
534 g_assert(self->paused);
536 /* short-circuit if nothing is changing */
537 if (self->device == device)
541 g_object_unref(self->device);
544 /* if we already have a connection, then make this device use it */
546 if (!device_use_connection(device, self->conn)) {
547 /* queue up an error for later, and set device_bad.
548 * start_part will see this and fail silently */
549 self->device_bad = TRUE;
550 xfer_cancel_with_error(XFER_ELEMENT(self),
551 _("Cannot continue onto new volume: %s"),
552 device_error_or_status(device));
557 self->device = device;
558 g_object_ref(device);
561 static xfer_element_mech_pair_t *
565 XferSourceRecovery *self = XFER_SOURCE_RECOVERY(elt);
566 static xfer_element_mech_pair_t basic_mech_pairs[] = {
567 { XFER_MECH_NONE, XFER_MECH_PULL_BUFFER, 1, 0},
568 { XFER_MECH_NONE, XFER_MECH_NONE, 0, 0},
570 static xfer_element_mech_pair_t directtcp_mech_pairs[] = {
571 { XFER_MECH_NONE, XFER_MECH_DIRECTTCP_CONNECT, 0, 1},
572 { XFER_MECH_NONE, XFER_MECH_DIRECTTCP_LISTEN, 0, 1},
573 /* devices which support DirectTCP are usually not very efficient
574 * at delivering data via device_read_block, so this counts an extra
575 * byte operation in the cost metrics (2 here vs. 1 in basic_mech_pairs).
576 * This is a hack, but it will do for now. */
577 { XFER_MECH_NONE, XFER_MECH_PULL_BUFFER, 2, 0},
578 { XFER_MECH_NONE, XFER_MECH_NONE, 0, 0},
581 return device_directtcp_supported(self->device)?
582 directtcp_mech_pairs : basic_mech_pairs;
589 XferSourceRecovery *self = XFER_SOURCE_RECOVERY(obj_self);
592 g_object_unref(self->conn);
594 g_object_unref(self->device);
596 g_cond_free(self->start_part_cond);
597 g_mutex_free(self->start_part_mutex);
604 XferSourceRecovery *self = XFER_SOURCE_RECOVERY(elt);
607 self->start_part_cond = g_cond_new();
608 self->start_part_mutex = g_mutex_new();
613 XferSourceRecoveryClass * xsr_klass)
615 XferElementClass *klass = XFER_ELEMENT_CLASS(xsr_klass);
616 GObjectClass *gobject_klass = G_OBJECT_CLASS(xsr_klass);
618 klass->pull_buffer = pull_buffer_impl;
619 klass->cancel = cancel_impl;
620 klass->start = start_impl;
621 klass->setup = setup_impl;
622 klass->get_mech_pairs = get_mech_pairs_impl;
624 klass->perl_class = "Amanda::Xfer::Source::Recovery";
625 klass->mech_pairs = NULL; /* see get_mech_pairs_impl, above */
627 xsr_klass->start_part = start_part_impl;
628 xsr_klass->use_device = use_device_impl;
630 gobject_klass->finalize = finalize_impl;
632 parent_class = g_type_class_peek_parent(xsr_klass);
636 xfer_source_recovery_get_type (void)
638 static GType type = 0;
640 if G_UNLIKELY(type == 0) {
641 static const GTypeInfo info = {
642 sizeof (XferSourceRecoveryClass),
643 (GBaseInitFunc) NULL,
644 (GBaseFinalizeFunc) NULL,
645 (GClassInitFunc) class_init,
646 (GClassFinalizeFunc) NULL,
647 NULL /* class_data */,
648 sizeof (XferSourceRecovery),
650 (GInstanceInitFunc) instance_init,
654 type = g_type_register_static (XFER_ELEMENT_TYPE, "XferSourceRecovery", &info, 0);
661 * Public methods and stubs
665 xfer_source_recovery_start_part(
669 XferSourceRecoveryClass *klass;
670 g_assert(IS_XFER_SOURCE_RECOVERY(elt));
672 klass = XFER_SOURCE_RECOVERY_GET_CLASS(elt);
673 klass->start_part(XFER_SOURCE_RECOVERY(elt), device);
676 /* create an element of this class; prototype is in xfer-device.h */
678 xfer_source_recovery(Device *first_device)
680 XferSourceRecovery *self = (XferSourceRecovery *)g_object_new(XFER_SOURCE_RECOVERY_TYPE, NULL);
681 XferElement *elt = XFER_ELEMENT(self);
683 g_assert(first_device != NULL);
684 g_object_ref(first_device);
685 self->device = first_device;
691 xfer_source_recovery_use_device(
695 XferSourceRecoveryClass *klass;
696 g_assert(IS_XFER_SOURCE_RECOVERY(elt));
698 klass = XFER_SOURCE_RECOVERY_GET_CLASS(elt);
699 klass->use_device(XFER_SOURCE_RECOVERY(elt), device);