| 1 |
/* |
| 2 |
* Amanda, The Advanced Maryland Automatic Network Disk Archiver |
| 3 |
* Copyright (c) 2009-2012 Zmanda, Inc. All Rights Reserved. |
| 4 |
* Copyright (c) 2013-2016 Carbonite, Inc. All Rights Reserved. |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU General Public License |
| 8 |
* as published by the Free Software Foundation; either version 2 |
| 9 |
* of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, but |
| 12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 13 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 |
* for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License along |
| 17 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 18 |
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 |
* |
| 20 |
* Contact information: Carbonite Inc., 756 N Pastoria Ave |
| 21 |
* Sunnyvale, CA 94085, or: http://www.zmanda.com |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "amanda.h" |
| 25 |
#include "amxfer.h" |
| 26 |
#include "device.h" |
| 27 |
#include "property.h" |
| 28 |
#include "xfer-device.h" |
| 29 |
#include "conffile.h" |
| 30 |
|
| 31 |
/* |
| 32 |
* Class declaration |
| 33 |
* |
| 34 |
* This declaration is entirely private; nothing but xfer_source_recovery() references |
| 35 |
* it directly. |
| 36 |
*/ |
| 37 |
|
| 38 |
GType xfer_source_recovery_get_type(void); |
| 39 |
#define XFER_SOURCE_RECOVERY_TYPE (xfer_source_recovery_get_type()) |
| 40 |
#define XFER_SOURCE_RECOVERY(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_source_recovery_get_type(), XferSourceRecovery) |
| 41 |
#define XFER_SOURCE_RECOVERY_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_source_recovery_get_type(), XferSourceRecovery const) |
| 42 |
#define XFER_SOURCE_RECOVERY_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), xfer_source_recovery_get_type(), XferSourceRecoveryClass) |
| 43 |
#define IS_XFER_SOURCE_RECOVERY(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), xfer_source_recovery_get_type ()) |
| 44 |
#define XFER_SOURCE_RECOVERY_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), xfer_source_recovery_get_type(), XferSourceRecoveryClass) |
| 45 |
|
| 46 |
static GObjectClass *parent_class = NULL; |
| 47 |
|
| 48 |
/* |
| 49 |
* Main object structure |
| 50 |
*/ |
| 51 |
|
| 52 |
typedef struct XferSourceRecovery { |
| 53 |
XferElement __parent__; |
| 54 |
|
| 55 |
/* thread for monitoring directtcp transfers */ |
| 56 |
GThread *thread; |
| 57 |
|
| 58 |
/* this mutex in this condition variable governs all variables below */ |
| 59 |
GCond *start_part_cond; |
| 60 |
GMutex *start_part_mutex; |
| 61 |
|
| 62 |
/* is this device currently paused and awaiting a new part? */ |
| 63 |
gboolean paused; |
| 64 |
|
| 65 |
/* device to read from (refcounted) */ |
| 66 |
Device *device; |
| 67 |
|
| 68 |
/* TRUE if use_device found the device unsuitable; this makes start_part |
| 69 |
* a no-op, allowing the cancellation to be handled normally */ |
| 70 |
gboolean device_bad; |
| 71 |
|
| 72 |
/* directtcp connection (only valid after XMSG_READY) */ |
| 73 |
DirectTCPConnection *conn; |
| 74 |
gboolean listen_ok; |
| 75 |
|
| 76 |
/* and the block size for that device (reset to zero at the start of each |
| 77 |
* part) */ |
| 78 |
size_t block_size; |
| 79 |
|
| 80 |
/* bytes read for this image */ |
| 81 |
guint64 bytes_read; |
| 82 |
|
| 83 |
/* part size (potentially including any zero-padding from the |
| 84 |
* device) */ |
| 85 |
guint64 part_size; |
| 86 |
|
| 87 |
/* timer for the duration; NULL while paused or cancelled */ |
| 88 |
GTimer *part_timer; |
| 89 |
|
| 90 |
gboolean done; |
| 91 |
|
| 92 |
GCond *abort_cond; /* condition to trigger to abort ndmp command */ |
| 93 |
} XferSourceRecovery; |
| 94 |
|
| 95 |
/* |
| 96 |
* Class definition |
| 97 |
*/ |
| 98 |
|
| 99 |
typedef struct { |
| 100 |
XferElementClass __parent__; |
| 101 |
|
| 102 |
/* start reading the part at which DEVICE is positioned, sending an |
| 103 |
* XMSG_PART_DONE when the part has been read */ |
| 104 |
void (*start_part)(XferSourceRecovery *self, Device *device); |
| 105 |
|
| 106 |
/* use the given device, much like the same method for xfer-dest-taper */ |
| 107 |
void (*use_device)(XferSourceRecovery *self, Device *device); |
| 108 |
} XferSourceRecoveryClass; |
| 109 |
|
| 110 |
/* |
| 111 |
* Debug Logging |
| 112 |
*/ |
| 113 |
|
| 114 |
#define DBG(LEVEL, ...) if (debug_recovery >= LEVEL) { _xsr_dbg(__VA_ARGS__); } |
| 115 |
static void |
| 116 |
_xsr_dbg(const char *fmt, ...) |
| 117 |
{ |
| 118 |
va_list argp; |
| 119 |
char msg[1024]; |
| 120 |
|
| 121 |
arglist_start(argp, fmt); |
| 122 |
g_vsnprintf(msg, sizeof(msg), fmt, argp); |
| 123 |
arglist_end(argp); |
| 124 |
g_debug("XSR: %s", msg); |
| 125 |
} |
| 126 |
|
| 127 |
/* |
| 128 |
* Implementation |
| 129 |
*/ |
| 130 |
|
| 131 |
/* common code for both directtcp_listen_thread and directtcp_connect_thread; |
| 132 |
* this is called after self->conn is filled in and carries out the data |
| 133 |
* transfer over that connection. NOTE: start_part_mutex is HELD when this |
| 134 |
* function begins */ |
| 135 |
static gpointer |
| 136 |
directtcp_common_thread( |
| 137 |
XferSourceRecovery *self) |
| 138 |
{ |
| 139 |
XferElement *elt = XFER_ELEMENT(self); |
| 140 |
char *errmsg = NULL; |
| 141 |
int result; |
| 142 |
|
| 143 |
/* send XMSG_READY to indicate it's OK to call start_part now */ |
| 144 |
DBG(2, "directtcp_common_thread sending XMSG_READY"); |
| 145 |
xfer_queue_message(elt->xfer, xmsg_new(elt, XMSG_READY, 0)); |
| 146 |
|
| 147 |
/* now we sit around waiting for signals to write a part */ |
| 148 |
while (1) { |
| 149 |
guint64 actual_size; |
| 150 |
XMsg *msg; |
| 151 |
|
| 152 |
while (self->paused && !elt->cancelled) { |
| 153 |
DBG(9, "directtcp_common_thread waiting to be un-paused"); |
| 154 |
g_cond_wait(self->start_part_cond, self->start_part_mutex); |
| 155 |
} |
| 156 |
DBG(9, "directtcp_common_thread done waiting"); |
| 157 |
|
| 158 |
if (elt->cancelled) { |
| 159 |
g_mutex_unlock(self->start_part_mutex); |
| 160 |
goto close_conn_and_send_done; |
| 161 |
} |
| 162 |
|
| 163 |
/* if the device is NULL, we're done */ |
| 164 |
if (!self->device) |
| 165 |
break; |
| 166 |
|
| 167 |
/* read the part */ |
| 168 |
self->part_timer = g_timer_new(); |
| 169 |
|
| 170 |
while (1) { |
| 171 |
DBG(2, "directtcp_common_thread reading part from %s", self->device->device_name); |
| 172 |
result = device_read_to_connection(self->device, G_MAXUINT64, |
| 173 |
&actual_size, &elt->cancelled, |
| 174 |
self->start_part_mutex, self->abort_cond); |
| 175 |
if (result == 1 && !elt->cancelled) { |
| 176 |
xfer_cancel_with_error(elt, _("error reading from device: %s"), |
| 177 |
device_error_or_status(self->device)); |
| 178 |
g_mutex_unlock(self->start_part_mutex); |
| 179 |
goto close_conn_and_send_done; |
| 180 |
} else if (result == 2 || elt->cancelled) { |
| 181 |
g_mutex_unlock(self->start_part_mutex); |
| 182 |
goto close_conn_and_send_done; |
| 183 |
} |
| 184 |
|
| 185 |
/* break on EOF; otherwise do another read_to_connection */ |
| 186 |
if (self->device->is_eof) { |
| 187 |
break; |
| 188 |
} |
| 189 |
} |
| 190 |
DBG(2, "done reading part; sending XMSG_PART_DONE"); |
| 191 |
|
| 192 |
/* the device has signalled EOF (really end-of-part), so clean up instance |
| 193 |
* variables and report the EOP to the caller in the form of an xmsg */ |
| 194 |
msg = xmsg_new(XFER_ELEMENT(self), XMSG_PART_DONE, 0); |
| 195 |
msg->size = actual_size; |
| 196 |
msg->duration = g_timer_elapsed(self->part_timer, NULL); |
| 197 |
msg->partnum = 0; |
| 198 |
msg->fileno = self->device->file; |
| 199 |
msg->successful = TRUE; |
| 200 |
msg->eof = FALSE; |
| 201 |
|
| 202 |
self->paused = TRUE; |
| 203 |
g_object_unref(self->device); |
| 204 |
self->device = NULL; |
| 205 |
self->part_size = 0; |
| 206 |
self->block_size = 0; |
| 207 |
g_timer_destroy(self->part_timer); |
| 208 |
self->part_timer = NULL; |
| 209 |
|
| 210 |
xfer_queue_message(elt->xfer, msg); |
| 211 |
} |
| 212 |
g_mutex_unlock(self->start_part_mutex); |
| 213 |
|
| 214 |
close_conn_and_send_done: |
| 215 |
if (self->conn) { |
| 216 |
errmsg = directtcp_connection_close(self->conn); |
| 217 |
g_object_unref(self->conn); |
| 218 |
self->conn = NULL; |
| 219 |
if (errmsg) { |
| 220 |
xfer_cancel_with_error(elt, _("error closing DirectTCP connection: %s"), errmsg); |
| 221 |
wait_until_xfer_cancelled(elt->xfer); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
xfer_queue_message(elt->xfer, xmsg_new(elt, XMSG_DONE, 0)); |
| 226 |
|
| 227 |
return NULL; |
| 228 |
} |
| 229 |
|
| 230 |
static gpointer |
| 231 |
directtcp_connect_thread( |
| 232 |
gpointer data) |
| 233 |
{ |
| 234 |
XferSourceRecovery *self = XFER_SOURCE_RECOVERY(data); |
| 235 |
XferElement *elt = XFER_ELEMENT(self); |
| 236 |
int result; |
| 237 |
|
| 238 |
DBG(1, "(this is directtcp_connect_thread)") |
| 239 |
|
| 240 |
/* first, we need to accept the incoming connection; we do this while |
| 241 |
* holding the start_part_mutex, so that a part doesn't get started until |
| 242 |
* we're finished with the device */ |
| 243 |
g_mutex_lock(self->start_part_mutex); |
| 244 |
|
| 245 |
if (elt->cancelled) { |
| 246 |
g_mutex_unlock(self->start_part_mutex); |
| 247 |
goto send_done; |
| 248 |
} |
| 249 |
|
| 250 |
g_assert(self->device != NULL); /* have a device */ |
| 251 |
g_assert(elt->output_listen_addrs != NULL); /* listening on it */ |
| 252 |
g_assert(self->listen_ok); |
| 253 |
|
| 254 |
DBG(2, "accepting DirectTCP connection on device %s", self->device->device_name); |
| 255 |
result = device_accept(self->device, &self->conn, &elt->cancelled, |
| 256 |
self->start_part_mutex, self->abort_cond); |
| 257 |
if (result == 1 && !elt->cancelled) { |
| 258 |
xfer_cancel_with_error(elt, |
| 259 |
_("error accepting DirectTCP connection: %s"), |
| 260 |
device_error_or_status(self->device)); |
| 261 |
g_mutex_unlock(self->start_part_mutex); |
| 262 |
wait_until_xfer_cancelled(elt->xfer); |
| 263 |
goto send_done; |
| 264 |
} else if (result == 2 || elt->cancelled) { |
| 265 |
g_mutex_unlock(self->start_part_mutex); |
| 266 |
goto send_done; |
| 267 |
} |
| 268 |
DBG(2, "DirectTCP connection accepted"); |
| 269 |
|
| 270 |
return directtcp_common_thread(self); |
| 271 |
|
| 272 |
send_done: |
| 273 |
xfer_queue_message(elt->xfer, xmsg_new(elt, XMSG_DONE, 0)); |
| 274 |
return NULL; |
| 275 |
} |
| 276 |
|
| 277 |
static gpointer |
| 278 |
directtcp_listen_thread( |
| 279 |
gpointer data) |
| 280 |
{ |
| 281 |
XferSourceRecovery *self = XFER_SOURCE_RECOVERY(data); |
| 282 |
XferElement *elt = XFER_ELEMENT(self); |
| 283 |
int result; |
| 284 |
|
| 285 |
DBG(1, "(this is directtcp_listen_thread)"); |
| 286 |
|
| 287 |
/* we need to make an outgoing connection to downstream; we do this while |
| 288 |
* holding the start_part_mutex, so that a part doesn't get started until |
| 289 |
* we're finished with the device */ |
| 290 |
g_mutex_lock(self->start_part_mutex); |
| 291 |
|
| 292 |
if (elt->cancelled) { |
| 293 |
g_mutex_unlock(self->start_part_mutex); |
| 294 |
goto send_done; |
| 295 |
} |
| 296 |
|
| 297 |
g_assert(self->device != NULL); /* have a device */ |
| 298 |
g_assert(elt->downstream->input_listen_addrs != NULL); /* downstream listening */ |
| 299 |
|
| 300 |
DBG(2, "making DirectTCP connection on device %s", self->device->device_name); |
| 301 |
result = device_connect(self->device, FALSE, |
| 302 |
elt->downstream->input_listen_addrs, |
| 303 |
&self->conn, &elt->cancelled, |
| 304 |
self->start_part_mutex, self->abort_cond); |
| 305 |
if (result == 1 && !elt->cancelled) { |
| 306 |
xfer_cancel_with_error(elt, |
| 307 |
_("error making DirectTCP connection: %s"), |
| 308 |
device_error_or_status(self->device)); |
| 309 |
g_mutex_unlock(self->start_part_mutex); |
| 310 |
wait_until_xfer_cancelled(elt->xfer); |
| 311 |
goto send_done; |
| 312 |
} else if (result == 2 || elt->cancelled) { |
| 313 |
g_mutex_unlock(self->start_part_mutex); |
| 314 |
wait_until_xfer_cancelled(elt->xfer); |
| 315 |
goto send_done; |
| 316 |
} |
| 317 |
DBG(2, "DirectTCP connect succeeded"); |
| 318 |
|
| 319 |
return directtcp_common_thread(self); |
| 320 |
|
| 321 |
send_done: |
| 322 |
xfer_queue_message(elt->xfer, xmsg_new(elt, XMSG_DONE, 0)); |
| 323 |
return NULL; |
| 324 |
} |
| 325 |
|
| 326 |
static gboolean |
| 327 |
setup_impl( |
| 328 |
XferElement *elt) |
| 329 |
{ |
| 330 |
XferSourceRecovery *self = XFER_SOURCE_RECOVERY(elt); |
| 331 |
|
| 332 |
if (elt->output_mech == XFER_MECH_DIRECTTCP_CONNECT) { |
| 333 |
g_assert(self->device != NULL); |
| 334 |
DBG(2, "listening for DirectTCP connection on device %s", self->device->device_name); |
| 335 |
if (!device_listen(self->device, FALSE, &elt->output_listen_addrs)) { |
| 336 |
xfer_cancel_with_error(elt, |
| 337 |
_("error listening for DirectTCP connection: %s"), |
| 338 |
device_error_or_status(self->device)); |
| 339 |
return FALSE; |
| 340 |
} |
| 341 |
self->listen_ok = TRUE; |
| 342 |
} else { |
| 343 |
/* no output_listen_addrs for either XFER_MECH_DIRECTTCP_LISTEN or |
| 344 |
* XFER_MECH_PULL_BUFFER */ |
| 345 |
elt->output_listen_addrs = NULL; |
| 346 |
} |
| 347 |
|
| 348 |
return TRUE; |
| 349 |
} |
| 350 |
|
| 351 |
static gboolean |
| 352 |
start_impl( |
| 353 |
XferElement *elt) |
| 354 |
{ |
| 355 |
XferSourceRecovery *self = XFER_SOURCE_RECOVERY(elt); |
| 356 |
|
| 357 |
if (elt->output_mech == XFER_MECH_DIRECTTCP_CONNECT) { |
| 358 |
g_assert(elt->output_listen_addrs != NULL); |
| 359 |
self->thread = g_thread_create(directtcp_connect_thread, (gpointer)self, FALSE, NULL); |
| 360 |
return TRUE; /* we'll send XMSG_DONE */ |
| 361 |
} else if (elt->output_mech == XFER_MECH_DIRECTTCP_LISTEN) { |
| 362 |
g_assert(elt->output_listen_addrs == NULL); |
| 363 |
self->thread = g_thread_create(directtcp_listen_thread, (gpointer)self, FALSE, NULL); |
| 364 |
return TRUE; /* we'll send XMSG_DONE */ |
| 365 |
} else { |
| 366 |
/* nothing to prepare for - we're ready already! */ |
| 367 |
DBG(2, "not using DirectTCP: sending XMSG_READY immediately"); |
| 368 |
xfer_queue_message(elt->xfer, xmsg_new(elt, XMSG_READY, 0)); |
| 369 |
|
| 370 |
return FALSE; /* we won't send XMSG_DONE */ |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
static gpointer |
| 375 |
pull_buffer_impl( |
| 376 |
XferElement *elt, |
| 377 |
size_t *size) |
| 378 |
{ |
| 379 |
XferSourceRecovery *self = XFER_SOURCE_RECOVERY(elt); |
| 380 |
gpointer buf = NULL; |
| 381 |
int result; |
| 382 |
int devsize; |
| 383 |
XMsg *msg; |
| 384 |
|
| 385 |
g_assert(elt->output_mech == XFER_MECH_PULL_BUFFER); |
| 386 |
|
| 387 |
g_mutex_lock(self->start_part_mutex); |
| 388 |
|
| 389 |
if (elt->size == 0) { |
| 390 |
if (elt->offset == 0 && elt->orig_size == 0) { |
| 391 |
self->paused = TRUE; |
| 392 |
} else { |
| 393 |
DBG(2, "xfer-source-recovery sending XMSG_CRC message"); |
| 394 |
DBG(2, "xfer-source-recovery CRC: %08x size %lld", |
| 395 |
crc32_finish(&elt->crc), (long long)elt->crc.size); |
| 396 |
msg = xmsg_new(XFER_ELEMENT(self), XMSG_CRC, 0); |
| 397 |
msg->crc = crc32_finish(&elt->crc); |
| 398 |
msg->size = elt->crc.size; |
| 399 |
xfer_queue_message(elt->xfer, msg); |
| 400 |
|
| 401 |
/* the device has signalled EOF (really end-of-part), so clean up instance |
| 402 |
* variables and report the EOP to the caller in the form of an xmsg */ |
| 403 |
DBG(2, "pull_buffer hit EOF; sending XMSG_SEGMENT_DONE"); |
| 404 |
msg = xmsg_new(XFER_ELEMENT(self), XMSG_SEGMENT_DONE, 0); |
| 405 |
msg->size = self->part_size; |
| 406 |
if (self->part_timer) { |
| 407 |
msg->duration = g_timer_elapsed(self->part_timer, NULL); |
| 408 |
g_timer_destroy(self->part_timer); |
| 409 |
self->part_timer = NULL; |
| 410 |
} |
| 411 |
msg->partnum = 0; |
| 412 |
msg->fileno = self->device->file; |
| 413 |
msg->successful = TRUE; |
| 414 |
msg->eof = FALSE; |
| 415 |
|
| 416 |
self->paused = TRUE; |
| 417 |
device_clear_bytes_read(self->device); |
| 418 |
self->bytes_read += self->part_size; |
| 419 |
self->part_size = 0; |
| 420 |
self->block_size = 0; |
| 421 |
|
| 422 |
/* don't queue the XMSG_PART_DONE until we've adjusted all of our |
| 423 |
* instance variables appropriately */ |
| 424 |
xfer_queue_message(elt->xfer, msg); |
| 425 |
|
| 426 |
if (self->device->is_eof) { |
| 427 |
DBG(2, "pull_buffer hit EOF; sending XMSG_PART_DONE"); |
| 428 |
msg = xmsg_new(XFER_ELEMENT(self), XMSG_PART_DONE, 0); |
| 429 |
msg->size = self->part_size; |
| 430 |
if (self->part_timer) { |
| 431 |
msg->duration = g_timer_elapsed(self->part_timer, NULL); |
| 432 |
g_timer_destroy(self->part_timer); |
| 433 |
self->part_timer = NULL; |
| 434 |
} |
| 435 |
msg->partnum = 0; |
| 436 |
msg->fileno = self->device->file; |
| 437 |
msg->successful = TRUE; |
| 438 |
msg->eof = FALSE; |
| 439 |
|
| 440 |
xfer_queue_message(elt->xfer, msg); |
| 441 |
} |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
while (1) { |
| 446 |
/* make sure we have a device */ |
| 447 |
while (self->paused && !elt->cancelled) |
| 448 |
g_cond_wait(self->start_part_cond, self->start_part_mutex); |
| 449 |
|
| 450 |
/* indicate EOF on an cancel or when there are no more parts */ |
| 451 |
if (elt->cancelled) { |
| 452 |
goto error; |
| 453 |
} |
| 454 |
if (self->done) |
| 455 |
goto error; |
| 456 |
|
| 457 |
/* start the timer if this is the first pull_buffer of this part */ |
| 458 |
if (!self->part_timer) { |
| 459 |
DBG(2, "first pull_buffer of new part"); |
| 460 |
self->part_timer = g_timer_new(); |
| 461 |
} |
| 462 |
if (elt->size == 0) { |
| 463 |
result = -1; |
| 464 |
} else { |
| 465 |
/* loop until we read a full block, in case the blocks are larger |
| 466 |
* than expected */ |
| 467 |
if (self->block_size == 0) |
| 468 |
self->block_size = (size_t)self->device->block_size; |
| 469 |
|
| 470 |
do { |
| 471 |
int max_block; |
| 472 |
buf = g_malloc(self->block_size); |
| 473 |
if (buf == NULL) { |
| 474 |
xfer_cancel_with_error(elt, |
| 475 |
_("%s: cannot allocate memory"), |
| 476 |
self->device->device_name); |
| 477 |
g_mutex_unlock(self->start_part_mutex); |
| 478 |
wait_until_xfer_cancelled(elt->xfer); |
| 479 |
goto error_unlocked; |
| 480 |
} |
| 481 |
devsize = (int)self->block_size; |
| 482 |
if (elt->size < 0) |
| 483 |
max_block = -1; |
| 484 |
else |
| 485 |
max_block = (elt->size+self->block_size-1)/self->block_size; |
| 486 |
result = device_read_block(self->device, buf, &devsize, max_block); |
| 487 |
*size = devsize; |
| 488 |
|
| 489 |
if (result == 0) { |
| 490 |
g_assert(*size > self->block_size); |
| 491 |
self->block_size = devsize; |
| 492 |
amfree(buf); |
| 493 |
} |
| 494 |
} while (result == 0); |
| 495 |
|
| 496 |
if (result > 0 && |
| 497 |
(elt->offset || |
| 498 |
(elt->size > 0 && (long long unsigned)elt->size < *size))) { |
| 499 |
gpointer buf1 = g_malloc(self->block_size); |
| 500 |
if ((long long unsigned)elt->offset > *size) { |
| 501 |
g_debug("offset > *size"); |
| 502 |
} else if ((long long unsigned)elt->offset == *size) { |
| 503 |
g_debug("offset == *size"); |
| 504 |
} |
| 505 |
*size -= elt->offset; |
| 506 |
if (elt->size > 0 && (size_t)elt->size < *size) |
| 507 |
*size = elt->size; |
| 508 |
memmove(buf1, buf + elt->offset, *size); |
| 509 |
elt->offset = 0; |
| 510 |
g_free(buf); |
| 511 |
buf = buf1; |
| 512 |
} |
| 513 |
if (result > 0) |
| 514 |
elt->size -= *size; |
| 515 |
|
| 516 |
} |
| 517 |
|
| 518 |
/* if this block was successful, return it */ |
| 519 |
if (result > 0) { |
| 520 |
self->part_size += *size; |
| 521 |
break; |
| 522 |
} |
| 523 |
|
| 524 |
if (result < 0) { |
| 525 |
amfree(buf); |
| 526 |
|
| 527 |
/* if we're not at EOF, it's an error */ |
| 528 |
if (!self->device->is_eof && elt->size != 0) { |
| 529 |
g_mutex_unlock(self->start_part_mutex); |
| 530 |
xfer_cancel_with_error(elt, |
| 531 |
_("error reading from %s: %s"), |
| 532 |
self->device->device_name, |
| 533 |
device_error_or_status(self->device)); |
| 534 |
wait_until_xfer_cancelled(elt->xfer); |
| 535 |
goto error_unlocked; |
| 536 |
} |
| 537 |
|
| 538 |
DBG(2, "xfer-source-recovery sending XMSG_CRC message"); |
| 539 |
DBG(2, "xfer-source-recovery CRC: %08x size %lld", |
| 540 |
crc32_finish(&elt->crc), (long long)elt->crc.size); |
| 541 |
msg = xmsg_new(XFER_ELEMENT(self), XMSG_CRC, 0); |
| 542 |
msg->crc = crc32_finish(&elt->crc); |
| 543 |
msg->size = elt->crc.size; |
| 544 |
xfer_queue_message(elt->xfer, msg); |
| 545 |
|
| 546 |
/* the device has signalled EOF (really end-of-part), so clean up instance |
| 547 |
* variables and report the EOP to the caller in the form of an xmsg */ |
| 548 |
DBG(2, "pull_buffer hit EOF; sending XMSG_PART_DONE"); |
| 549 |
msg = xmsg_new(XFER_ELEMENT(self), XMSG_PART_DONE, 0); |
| 550 |
msg->size = self->part_size; |
| 551 |
msg->duration = g_timer_elapsed(self->part_timer, NULL); |
| 552 |
msg->partnum = 0; |
| 553 |
msg->fileno = self->device->file; |
| 554 |
msg->successful = TRUE; |
| 555 |
msg->eof = FALSE; |
| 556 |
|
| 557 |
self->paused = TRUE; |
| 558 |
self->bytes_read += self->part_size; |
| 559 |
device_clear_bytes_read(self->device); |
| 560 |
self->part_size = 0; |
| 561 |
self->block_size = 0; |
| 562 |
if (self->part_timer) { |
| 563 |
g_timer_destroy(self->part_timer); |
| 564 |
self->part_timer = NULL; |
| 565 |
} |
| 566 |
|
| 567 |
/* don't queue the XMSG_PART_DONE until we've adjusted all of our |
| 568 |
* instance variables appropriately */ |
| 569 |
xfer_queue_message(elt->xfer, msg); |
| 570 |
if (elt->size == 0) { |
| 571 |
g_mutex_unlock(self->start_part_mutex); |
| 572 |
return NULL; |
| 573 |
} |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
g_mutex_unlock(self->start_part_mutex); |
| 578 |
|
| 579 |
if (buf) { |
| 580 |
crc32_add(buf, *size, &elt->crc); |
| 581 |
} |
| 582 |
|
| 583 |
return buf; |
| 584 |
error: |
| 585 |
g_mutex_unlock(self->start_part_mutex); |
| 586 |
error_unlocked: |
| 587 |
*size = 0; |
| 588 |
return NULL; |
| 589 |
} |
| 590 |
|
| 591 |
static gboolean |
| 592 |
cancel_impl( |
| 593 |
XferElement *elt, |
| 594 |
gboolean expect_eof G_GNUC_UNUSED) |
| 595 |
{ |
| 596 |
XferSourceRecovery *self = XFER_SOURCE_RECOVERY(elt); |
| 597 |
elt->cancelled = TRUE; |
| 598 |
|
| 599 |
/* trigger the condition variable, in case the thread is waiting on it */ |
| 600 |
g_mutex_lock(self->start_part_mutex); |
| 601 |
g_cond_broadcast(self->start_part_cond); |
| 602 |
g_cond_broadcast(self->abort_cond); |
| 603 |
g_mutex_unlock(self->start_part_mutex); |
| 604 |
|
| 605 |
return TRUE; |
| 606 |
} |
| 607 |
|
| 608 |
static void |
| 609 |
start_part_impl( |
| 610 |
XferSourceRecovery *self, |
| 611 |
Device *device) |
| 612 |
{ |
| 613 |
XferElement *elt = XFER_ELEMENT(self); |
| 614 |
|
| 615 |
g_assert(!device || device->in_file); |
| 616 |
|
| 617 |
DBG(2, "start_part called"); |
| 618 |
|
| 619 |
if (self->device_bad) { |
| 620 |
/* use_device didn't like the device it got, but the xfer cancellation |
| 621 |
* has not completed yet, so do nothing */ |
| 622 |
return; |
| 623 |
} |
| 624 |
|
| 625 |
g_mutex_lock(self->start_part_mutex); |
| 626 |
|
| 627 |
/* make sure we're ready to go */ |
| 628 |
g_assert(self->paused || self->done); |
| 629 |
self->done = FALSE; |
| 630 |
if (XFER_ELEMENT(self)->output_mech == XFER_MECH_DIRECTTCP_CONNECT |
| 631 |
|| XFER_ELEMENT(self)->output_mech == XFER_MECH_DIRECTTCP_LISTEN) { |
| 632 |
g_assert(self->conn != NULL); |
| 633 |
} |
| 634 |
|
| 635 |
/* if we already have a device, it should have been given to use_device */ |
| 636 |
if (device && self->device) { |
| 637 |
g_assert(self->device == device); |
| 638 |
} else if (device) { |
| 639 |
self->device = device; |
| 640 |
g_object_ref(device); |
| 641 |
} |
| 642 |
|
| 643 |
if (!device) |
| 644 |
self->done = TRUE; |
| 645 |
|
| 646 |
if (elt->offset == 0 && elt->orig_size == 0) { |
| 647 |
self->done = TRUE; |
| 648 |
g_mutex_unlock(self->start_part_mutex); |
| 649 |
return; |
| 650 |
} |
| 651 |
|
| 652 |
if (elt->size == 0) { |
| 653 |
self->done = TRUE; |
| 654 |
g_mutex_unlock(self->start_part_mutex); |
| 655 |
return; |
| 656 |
} |
| 657 |
|
| 658 |
self->paused = FALSE; |
| 659 |
|
| 660 |
DBG(2, "triggering condition variable"); |
| 661 |
g_cond_broadcast(self->start_part_cond); |
| 662 |
g_mutex_unlock(self->start_part_mutex); |
| 663 |
} |
| 664 |
|
| 665 |
static void |
| 666 |
use_device_impl( |
| 667 |
XferSourceRecovery *xdtself, |
| 668 |
Device *device) |
| 669 |
{ |
| 670 |
XferSourceRecovery *self = XFER_SOURCE_RECOVERY(xdtself); |
| 671 |
|
| 672 |
g_assert(self->paused); |
| 673 |
|
| 674 |
/* short-circuit if nothing is changing */ |
| 675 |
if (self->device == device) |
| 676 |
return; |
| 677 |
|
| 678 |
if (self->device) |
| 679 |
g_object_unref(self->device); |
| 680 |
self->device = NULL; |
| 681 |
|
| 682 |
/* if we already have a connection, then make this device use it */ |
| 683 |
if (self->conn) { |
| 684 |
if (!device_use_connection(device, self->conn)) { |
| 685 |
/* queue up an error for later, and set device_bad. |
| 686 |
* start_part will see this and fail silently */ |
| 687 |
self->device_bad = TRUE; |
| 688 |
xfer_cancel_with_error(XFER_ELEMENT(self), |
| 689 |
_("Cannot continue onto new volume: %s"), |
| 690 |
device_error_or_status(device)); |
| 691 |
return; |
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
self->device = device; |
| 696 |
g_object_ref(device); |
| 697 |
} |
| 698 |
|
| 699 |
static xfer_element_mech_pair_t * |
| 700 |
get_mech_pairs_impl( |
| 701 |
XferElement *elt) |
| 702 |
{ |
| 703 |
XferSourceRecovery *self = XFER_SOURCE_RECOVERY(elt); |
| 704 |
static xfer_element_mech_pair_t basic_mech_pairs[] = { |
| 705 |
{ XFER_MECH_NONE, XFER_MECH_PULL_BUFFER, XFER_NROPS(1), XFER_NTHREADS(0), XFER_NALLOC(0) }, |
| 706 |
{ XFER_MECH_NONE, XFER_MECH_NONE, XFER_NROPS(0), XFER_NTHREADS(0), XFER_NALLOC(0) } |
| 707 |
}; |
| 708 |
static xfer_element_mech_pair_t directtcp_mech_pairs[] = { |
| 709 |
{ XFER_MECH_NONE, XFER_MECH_DIRECTTCP_CONNECT, XFER_NROPS(0), XFER_NTHREADS(1), XFER_NALLOC(0) }, |
| 710 |
{ XFER_MECH_NONE, XFER_MECH_DIRECTTCP_LISTEN, XFER_NROPS(0), XFER_NTHREADS(1), XFER_NALLOC(0) }, |
| 711 |
/* devices which support DirectTCP are usually not very efficient |
| 712 |
* at delivering data via device_read_block, so this counts an extra |
| 713 |
* byte operation in the cost metrics (2 here vs. 1 in basic_mech_pairs). |
| 714 |
* This is a hack, but it will do for now. */ |
| 715 |
{ XFER_MECH_NONE, XFER_MECH_PULL_BUFFER, XFER_NROPS(2), XFER_NTHREADS(0), XFER_NALLOC(0) }, |
| 716 |
{ XFER_MECH_NONE, XFER_MECH_NONE, XFER_NROPS(0), XFER_NTHREADS(0), XFER_NALLOC(0) }, |
| 717 |
}; |
| 718 |
|
| 719 |
return device_directtcp_supported(self->device)? |
| 720 |
directtcp_mech_pairs : basic_mech_pairs; |
| 721 |
} |
| 722 |
|
| 723 |
static void |
| 724 |
finalize_impl( |
| 725 |
GObject * obj_self) |
| 726 |
{ |
| 727 |
XferSourceRecovery *self = XFER_SOURCE_RECOVERY(obj_self); |
| 728 |
|
| 729 |
if (self->conn) |
| 730 |
g_object_unref(self->conn); |
| 731 |
if (self->device) |
| 732 |
g_object_unref(self->device); |
| 733 |
|
| 734 |
g_cond_free(self->start_part_cond); |
| 735 |
g_cond_free(self->abort_cond); |
| 736 |
g_mutex_free(self->start_part_mutex); |
| 737 |
} |
| 738 |
|
| 739 |
static void |
| 740 |
instance_init( |
| 741 |
XferElement *elt) |
| 742 |
{ |
| 743 |
XferSourceRecovery *self = XFER_SOURCE_RECOVERY(elt); |
| 744 |
|
| 745 |
self->paused = TRUE; |
| 746 |
self->start_part_cond = g_cond_new(); |
| 747 |
self->abort_cond = g_cond_new(); |
| 748 |
self->start_part_mutex = g_mutex_new(); |
| 749 |
crc32_init(&elt->crc); |
| 750 |
} |
| 751 |
|
| 752 |
static void |
| 753 |
class_init( |
| 754 |
XferSourceRecoveryClass * xsr_klass) |
| 755 |
{ |
| 756 |
XferElementClass *klass = XFER_ELEMENT_CLASS(xsr_klass); |
| 757 |
GObjectClass *gobject_klass = G_OBJECT_CLASS(xsr_klass); |
| 758 |
|
| 759 |
klass->pull_buffer = pull_buffer_impl; |
| 760 |
klass->cancel = cancel_impl; |
| 761 |
klass->start = start_impl; |
| 762 |
klass->setup = setup_impl; |
| 763 |
klass->get_mech_pairs = get_mech_pairs_impl; |
| 764 |
|
| 765 |
klass->perl_class = "Amanda::Xfer::Source::Recovery"; |
| 766 |
klass->mech_pairs = NULL; /* see get_mech_pairs_impl, above */ |
| 767 |
|
| 768 |
xsr_klass->start_part = start_part_impl; |
| 769 |
xsr_klass->use_device = use_device_impl; |
| 770 |
|
| 771 |
gobject_klass->finalize = finalize_impl; |
| 772 |
|
| 773 |
parent_class = g_type_class_peek_parent(xsr_klass); |
| 774 |
} |
| 775 |
|
| 776 |
GType |
| 777 |
xfer_source_recovery_get_type (void) |
| 778 |
{ |
| 779 |
static GType type = 0; |
| 780 |
|
| 781 |
if (G_UNLIKELY(type == 0)) { |
| 782 |
static const GTypeInfo info = { |
| 783 |
sizeof (XferSourceRecoveryClass), |
| 784 |
(GBaseInitFunc) NULL, |
| 785 |
(GBaseFinalizeFunc) NULL, |
| 786 |
(GClassInitFunc) class_init, |
| 787 |
(GClassFinalizeFunc) NULL, |
| 788 |
NULL /* class_data */, |
| 789 |
sizeof (XferSourceRecovery), |
| 790 |
0 /* n_preallocs */, |
| 791 |
(GInstanceInitFunc) instance_init, |
| 792 |
NULL |
| 793 |
}; |
| 794 |
|
| 795 |
type = g_type_register_static (XFER_ELEMENT_TYPE, "XferSourceRecovery", &info, 0); |
| 796 |
} |
| 797 |
|
| 798 |
return type; |
| 799 |
} |
| 800 |
|
| 801 |
/* |
| 802 |
* Public methods and stubs |
| 803 |
*/ |
| 804 |
|
| 805 |
void |
| 806 |
xfer_source_recovery_start_part( |
| 807 |
XferElement *elt, |
| 808 |
Device *device) |
| 809 |
{ |
| 810 |
XferSourceRecoveryClass *klass; |
| 811 |
g_assert(IS_XFER_SOURCE_RECOVERY(elt)); |
| 812 |
|
| 813 |
klass = XFER_SOURCE_RECOVERY_GET_CLASS(elt); |
| 814 |
klass->start_part(XFER_SOURCE_RECOVERY(elt), device); |
| 815 |
} |
| 816 |
|
| 817 |
gboolean |
| 818 |
xfer_source_recovery_cancel( |
| 819 |
XferElement *elt, |
| 820 |
gboolean expect_eof G_GNUC_UNUSED) |
| 821 |
{ |
| 822 |
XferElementClass *klass; |
| 823 |
g_assert(IS_XFER_SOURCE_RECOVERY(elt)); |
| 824 |
|
| 825 |
klass = XFER_ELEMENT_GET_CLASS(elt); |
| 826 |
return klass->cancel(XFER_ELEMENT(elt), 0); |
| 827 |
} |
| 828 |
|
| 829 |
/* create an element of this class; prototype is in xfer-device.h */ |
| 830 |
XferElement * |
| 831 |
xfer_source_recovery(Device *first_device) |
| 832 |
{ |
| 833 |
XferSourceRecovery *self = (XferSourceRecovery *)g_object_new(XFER_SOURCE_RECOVERY_TYPE, NULL); |
| 834 |
XferElement *elt = XFER_ELEMENT(self); |
| 835 |
|
| 836 |
g_assert(first_device != NULL); |
| 837 |
g_object_ref(first_device); |
| 838 |
self->device = first_device; |
| 839 |
|
| 840 |
return elt; |
| 841 |
} |
| 842 |
|
| 843 |
void |
| 844 |
xfer_source_recovery_use_device( |
| 845 |
XferElement *elt, |
| 846 |
Device *device) |
| 847 |
{ |
| 848 |
XferSourceRecoveryClass *klass; |
| 849 |
g_assert(IS_XFER_SOURCE_RECOVERY(elt)); |
| 850 |
|
| 851 |
klass = XFER_SOURCE_RECOVERY_GET_CLASS(elt); |
| 852 |
klass->use_device(XFER_SOURCE_RECOVERY(elt), device); |
| 853 |
} |
| 854 |
|
| 855 |
guint64 |
| 856 |
xfer_source_recovery_get_bytes_read( |
| 857 |
XferElement *elt) |
| 858 |
{ |
| 859 |
XferSourceRecovery *self = XFER_SOURCE_RECOVERY(elt); |
| 860 |
guint64 bytes_read = self->bytes_read; |
| 861 |
|
| 862 |
if (self->device) |
| 863 |
bytes_read += device_get_bytes_read(self->device); |
| 864 |
|
| 865 |
return bytes_read; |
| 866 |
} |
| 867 |
|