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 */
92 XferElementClass __parent__;
94 /* start reading the part at which DEVICE is positioned, sending an
95 * XMSG_PART_DONE when the part has been read */
96 void (*start_part)(XferSourceRecovery *self, Device *device);
98 /* use the given device, much like the same method for xfer-dest-taper */
99 void (*use_device)(XferSourceRecovery *self, Device *device);
100 } XferSourceRecoveryClass;
106 #define DBG(LEVEL, ...) if (debug_recovery >= LEVEL) { _xsr_dbg(__VA_ARGS__); }
108 _xsr_dbg(const char *fmt, ...)
113 arglist_start(argp, fmt);
114 g_vsnprintf(msg, sizeof(msg), fmt, argp);
116 g_debug("XSR thd-%p: %s", g_thread_self(), msg);
127 XferSourceRecovery *self = XFER_SOURCE_RECOVERY(data);
128 XferElement *elt = XFER_ELEMENT(self);
131 /* first, we need to accept the incoming connection; we do this while
132 * holding the start_part_mutex, so that a part doesn't get started until
133 * we're finished with the device */
134 g_mutex_lock(self->start_part_mutex);
136 if (elt->cancelled) {
137 g_mutex_unlock(self->start_part_mutex);
141 g_assert(self->device != NULL); /* have a device */
142 g_assert(elt->output_listen_addrs != NULL); /* listening on it */
143 g_assert(self->listen_ok);
145 DBG(2, "accepting DirectTCP connection on device %s", self->device->device_name);
146 if (!device_accept(self->device, &self->conn, NULL, NULL)) {
147 xfer_cancel_with_error(elt,
148 _("error accepting DirectTCP connection: %s"),
149 device_error_or_status(self->device));
150 g_mutex_unlock(self->start_part_mutex);
151 wait_until_xfer_cancelled(elt->xfer);
155 /* send XMSG_READY to indicate it's OK to call start_part now */
156 DBG(2, "connection accepted; sending XMSG_READY");
157 xfer_queue_message(elt->xfer, xmsg_new(elt, XMSG_READY, 0));
159 /* now we sit around waiting for signals to write a part */
164 while (self->paused && !elt->cancelled) {
165 DBG(9, "waiting to be un-paused");
166 g_cond_wait(self->start_part_cond, self->start_part_mutex);
168 DBG(9, "done waiting");
170 if (elt->cancelled) {
171 g_mutex_unlock(self->start_part_mutex);
172 goto close_conn_and_send_done;
175 /* if the device is NULL, we're done */
180 self->part_timer = g_timer_new();
183 DBG(2, "reading part from %s", self->device->device_name);
184 if (!device_read_to_connection(self->device, G_MAXUINT64, &actual_size)) {
185 xfer_cancel_with_error(elt, _("error reading from device: %s"),
186 device_error_or_status(self->device));
187 g_mutex_unlock(self->start_part_mutex);
188 goto close_conn_and_send_done;
191 /* break on EOF; otherwise do another read_to_connection */
192 if (self->device->is_eof) {
196 DBG(2, "done reading part; sending XMSG_PART_DONE");
198 /* the device has signalled EOF (really end-of-part), so clean up instance
199 * variables and report the EOP to the caller in the form of an xmsg */
200 msg = xmsg_new(XFER_ELEMENT(self), XMSG_PART_DONE, 0);
201 msg->size = actual_size;
202 msg->duration = g_timer_elapsed(self->part_timer, NULL);
204 msg->fileno = self->device->file;
205 msg->successful = TRUE;
209 g_object_unref(self->device);
212 self->block_size = 0;
213 g_timer_destroy(self->part_timer);
214 self->part_timer = NULL;
216 xfer_queue_message(elt->xfer, msg);
218 g_mutex_unlock(self->start_part_mutex);
220 close_conn_and_send_done:
222 errmsg = directtcp_connection_close(self->conn);
223 g_object_unref(self->conn);
226 xfer_cancel_with_error(elt, _("error closing DirectTCP connection: %s"), errmsg);
227 wait_until_xfer_cancelled(elt->xfer);
232 xfer_queue_message(elt->xfer, xmsg_new(elt, XMSG_DONE, 0));
241 XferSourceRecovery *self = XFER_SOURCE_RECOVERY(elt);
243 if (elt->output_mech == XFER_MECH_DIRECTTCP_CONNECT) {
244 g_assert(self->device != NULL);
245 DBG(2, "listening for DirectTCP connection on device %s", self->device->device_name);
246 if (!device_listen(self->device, FALSE, &elt->output_listen_addrs)) {
247 xfer_cancel_with_error(elt,
248 _("error listening for DirectTCP connection: %s"),
249 device_error_or_status(self->device));
252 self->listen_ok = TRUE;
254 elt->output_listen_addrs = NULL;
264 XferSourceRecovery *self = XFER_SOURCE_RECOVERY(elt);
266 if (elt->output_mech == XFER_MECH_DIRECTTCP_CONNECT) {
267 g_assert(elt->output_listen_addrs != NULL);
268 self->thread = g_thread_create(directtcp_thread, (gpointer)self, FALSE, NULL);
269 return TRUE; /* we'll send XMSG_DONE */
271 /* nothing to prepare for - we're ready already! */
272 DBG(2, "not using DirectTCP: sending XMSG_READY immediately");
273 xfer_queue_message(elt->xfer, xmsg_new(elt, XMSG_READY, 0));
275 return FALSE; /* we won't send XMSG_DONE */
284 XferSourceRecovery *self = XFER_SOURCE_RECOVERY(elt);
290 g_assert(elt->output_mech == XFER_MECH_PULL_BUFFER);
291 g_mutex_lock(self->start_part_mutex);
294 /* make sure we have a device */
295 while (self->paused && !elt->cancelled)
296 g_cond_wait(self->start_part_cond, self->start_part_mutex);
298 /* indicate EOF on an cancel or when there are no more parts */
299 if (elt->cancelled || !self->device) {
303 /* start the timer if this is the first pull_buffer of this part */
304 if (!self->part_timer) {
305 DBG(2, "first pull_buffer of new part");
306 self->part_timer = g_timer_new();
309 /* loop until we read a full block, in case the blocks are larger than
311 if (self->block_size == 0)
312 self->block_size = (size_t)self->device->block_size;
315 buf = g_malloc(self->block_size);
316 devsize = (int)self->block_size;
317 result = device_read_block(self->device, buf, &devsize);
321 g_assert(*size > self->block_size);
322 self->block_size = devsize;
325 } while (result == 0);
327 /* if this block was successful, return it */
329 self->part_size += *size;
336 /* if we're not at EOF, it's an error */
337 if (!self->device->is_eof) {
338 xfer_cancel_with_error(elt,
339 _("error reading from %s: %s"),
340 self->device->device_name,
341 device_error_or_status(self->device));
342 wait_until_xfer_cancelled(elt->xfer);
346 /* the device has signalled EOF (really end-of-part), so clean up instance
347 * variables and report the EOP to the caller in the form of an xmsg */
348 DBG(2, "pull_buffer hit EOF; sending XMSG_PART_DONE");
349 msg = xmsg_new(XFER_ELEMENT(self), XMSG_PART_DONE, 0);
350 msg->size = self->part_size;
351 msg->duration = g_timer_elapsed(self->part_timer, NULL);
353 msg->fileno = self->device->file;
354 msg->successful = TRUE;
358 g_object_unref(self->device);
361 self->block_size = 0;
362 if (self->part_timer) {
363 g_timer_destroy(self->part_timer);
364 self->part_timer = NULL;
367 /* don't queue the XMSG_PART_DONE until we've adjusted all of our
368 * instance variables appropriately */
369 xfer_queue_message(elt->xfer, msg);
373 g_mutex_unlock(self->start_part_mutex);
377 g_mutex_unlock(self->start_part_mutex);
385 gboolean expect_eof G_GNUC_UNUSED)
387 XferSourceRecovery *self = XFER_SOURCE_RECOVERY(elt);
388 elt->cancelled = TRUE;
390 /* trigger the condition variable, in case the thread is waiting on it */
391 g_mutex_lock(self->start_part_mutex);
392 g_cond_broadcast(self->start_part_cond);
393 g_mutex_unlock(self->start_part_mutex);
400 XferSourceRecovery *self,
403 g_assert(!device || device->in_file);
405 DBG(2, "start_part called");
407 if (self->device_bad) {
408 /* use_device didn't like the device it got, but the xfer cancellation
409 * has not completed yet, so do nothing */
413 g_mutex_lock(self->start_part_mutex);
415 /* make sure we're ready to go */
416 g_assert(self->paused);
417 if (XFER_ELEMENT(self)->output_mech == XFER_MECH_DIRECTTCP_CONNECT) {
418 g_assert(self->conn != NULL);
421 /* if we already have a device, it should have been given to use_device */
422 if (device && self->device)
423 g_assert(self->device == device);
426 g_object_unref(self->device);
428 g_object_ref(device);
429 self->device = device;
431 self->paused = FALSE;
433 DBG(2, "triggering condition variable");
434 g_cond_broadcast(self->start_part_cond);
435 g_mutex_unlock(self->start_part_mutex);
440 XferSourceRecovery *xdtself,
443 XferSourceRecovery *self = XFER_SOURCE_RECOVERY(xdtself);
445 g_assert(self->paused);
447 /* short-circuit if nothing is changing */
448 if (self->device == device)
452 g_object_unref(self->device);
455 /* if we already have a connection, then make this device use it */
457 if (!device_use_connection(device, self->conn)) {
458 /* queue up an error for later, and set device_bad.
459 * start_part will see this and fail silently */
460 self->device_bad = TRUE;
461 xfer_cancel_with_error(XFER_ELEMENT(self),
462 _("Cannot continue onto new volume: %s"),
463 device_error_or_status(device));
468 self->device = device;
469 g_object_ref(device);
472 static xfer_element_mech_pair_t *
476 XferSourceRecovery *self = XFER_SOURCE_RECOVERY(elt);
477 static xfer_element_mech_pair_t basic_mech_pairs[] = {
478 { XFER_MECH_NONE, XFER_MECH_PULL_BUFFER, 1, 0},
479 { XFER_MECH_NONE, XFER_MECH_NONE, 0, 0},
481 static xfer_element_mech_pair_t directtcp_mech_pairs[] = {
482 { XFER_MECH_NONE, XFER_MECH_DIRECTTCP_CONNECT, 0, 1},
483 /* devices which support DirectTCP are usually not very efficient
484 * at delivering data via device_read_block, so this counts an extra
485 * byte operation in the cost metrics (2 here vs. 1 in basic_mech_pairs).
486 * This is a hack, but it will do for now. */
487 { XFER_MECH_NONE, XFER_MECH_PULL_BUFFER, 2, 0},
488 { XFER_MECH_NONE, XFER_MECH_NONE, 0, 0},
491 return device_directtcp_supported(self->device)?
492 directtcp_mech_pairs : basic_mech_pairs;
499 XferSourceRecovery *self = XFER_SOURCE_RECOVERY(obj_self);
502 g_object_unref(self->conn);
504 g_object_unref(self->device);
506 g_cond_free(self->start_part_cond);
507 g_mutex_free(self->start_part_mutex);
514 XferSourceRecovery *self = XFER_SOURCE_RECOVERY(elt);
517 self->start_part_cond = g_cond_new();
518 self->start_part_mutex = g_mutex_new();
523 XferSourceRecoveryClass * xsr_klass)
525 XferElementClass *klass = XFER_ELEMENT_CLASS(xsr_klass);
526 GObjectClass *gobject_klass = G_OBJECT_CLASS(xsr_klass);
528 klass->pull_buffer = pull_buffer_impl;
529 klass->cancel = cancel_impl;
530 klass->start = start_impl;
531 klass->setup = setup_impl;
532 klass->get_mech_pairs = get_mech_pairs_impl;
534 klass->perl_class = "Amanda::Xfer::Source::Recovery";
535 klass->mech_pairs = NULL; /* see get_mech_pairs_impl, above */
537 xsr_klass->start_part = start_part_impl;
538 xsr_klass->use_device = use_device_impl;
540 gobject_klass->finalize = finalize_impl;
542 parent_class = g_type_class_peek_parent(xsr_klass);
546 xfer_source_recovery_get_type (void)
548 static GType type = 0;
550 if G_UNLIKELY(type == 0) {
551 static const GTypeInfo info = {
552 sizeof (XferSourceRecoveryClass),
553 (GBaseInitFunc) NULL,
554 (GBaseFinalizeFunc) NULL,
555 (GClassInitFunc) class_init,
556 (GClassFinalizeFunc) NULL,
557 NULL /* class_data */,
558 sizeof (XferSourceRecovery),
560 (GInstanceInitFunc) instance_init,
564 type = g_type_register_static (XFER_ELEMENT_TYPE, "XferSourceRecovery", &info, 0);
571 * Public methods and stubs
575 xfer_source_recovery_start_part(
579 XferSourceRecoveryClass *klass;
580 g_assert(IS_XFER_SOURCE_RECOVERY(elt));
582 klass = XFER_SOURCE_RECOVERY_GET_CLASS(elt);
583 klass->start_part(XFER_SOURCE_RECOVERY(elt), device);
586 /* create an element of this class; prototype is in xfer-device.h */
588 xfer_source_recovery(Device *first_device)
590 XferSourceRecovery *self = (XferSourceRecovery *)g_object_new(XFER_SOURCE_RECOVERY_TYPE, NULL);
591 XferElement *elt = XFER_ELEMENT(self);
593 g_assert(first_device != NULL);
594 g_object_ref(first_device);
595 self->device = first_device;
601 xfer_source_recovery_use_device(
605 XferSourceRecoveryClass *klass;
606 g_assert(IS_XFER_SOURCE_RECOVERY(elt));
608 klass = XFER_SOURCE_RECOVERY_GET_CLASS(elt);
609 klass->use_device(XFER_SOURCE_RECOVERY(elt), device);