Debian Codegraph @

device-src/xfer-dest-taper-directtcp.c

raw
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 "xfer-device.h"
27 #include "conffile.h"
28
29 /* A transfer destination that writes and entire dumpfile to one or more files
30 * on one or more devices via DirectTCP, handling the work of spanning a
31 * directtcp connection over multiple devices. Note that this assumes the
32 * devices support early EOM warning. */
33
34 /*
35 * Xfer Dest Taper DirectTCP
36 */
37
38 static GType xfer_dest_taper_directtcp_get_type(void);
39 #define XFER_DEST_TAPER_DIRECTTCP_TYPE (xfer_dest_taper_directtcp_get_type())
40 #define XFER_DEST_TAPER_DIRECTTCP(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_dest_taper_directtcp_get_type(), XferDestTaperDirectTCP)
41 #define XFER_DEST_TAPER_DIRECTTCP_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_dest_taper_directtcp_get_type(), XferDestTaperDirectTCP const)
42 #define XFER_DEST_TAPER_DIRECTTCP_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), xfer_dest_taper_directtcp_get_type(), XferDestTaperDirectTCPClass)
43 #define IS_XFER_DEST_TAPER_DIRECTTCP(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), xfer_dest_taper_directtcp_get_type ())
44 #define XFER_DEST_TAPER_DIRECTTCP_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), xfer_dest_taper_directtcp_get_type(), XferDestTaperDirectTCPClass)
45
46 static GObjectClass *parent_class = NULL;
47
48 typedef struct XferDestTaperDirectTCP {
49 XferDestTaper __parent__;
50
51 /* constructor parameters */
52 guint64 part_size; /* (bytes) */
53
54 /* thread */
55 GThread *worker_thread;
56
57 /* state (governs everything below) */
58 GMutex *state_mutex;
59
60 /* part parameters */
61 Device *volatile device; /* device to write to (refcounted) */
62 dumpfile_t *volatile part_header;
63
64 /* did the device listen proceed without error? */
65 gboolean listen_ok;
66
67 /* part number in progress */
68 volatile guint64 partnum;
69
70 /* connection we're writing to (refcounted) */
71 DirectTCPConnection *conn;
72
73 /* is the element paused, waiting to start a new part? this is set to FALSE
74 * by the main thread to start a part, and the worker thread waits on the
75 * corresponding condition variable. */
76 volatile gboolean paused;
77 GCond *paused_cond;
78 GCond *abort_cond; /* condition to trigger to abort an NDMP command */
79
80 } XferDestTaperDirectTCP;
81
82 typedef struct {
83 XferDestTaperClass __parent__;
84 } XferDestTaperDirectTCPClass;
85
86 /*
87 * Debug logging
88 */
89
90 #define DBG(LEVEL, ...) if (debug_taper >= LEVEL) { _xdt_dbg(__VA_ARGS__); }
91 static void
92 _xdt_dbg(const char *fmt, ...)
93 {
94 va_list argp;
95 char msg[1024];
96
97 arglist_start(argp, fmt);
98 g_vsnprintf(msg, sizeof(msg), fmt, argp);
99 arglist_end(argp);
100 g_debug("XDTD: %s", msg);
101 }
102 /*
103 * Worker Thread
104 */
105
106 static gpointer
107 worker_thread(
108 gpointer data)
109 {
110 XferElement *elt = (XferElement *)data;
111 XferDestTaperDirectTCP *self = (XferDestTaperDirectTCP *)data;
112 GTimer *timer = g_timer_new();
113 int result;
114
115 /* This thread's job is to accept() an incoming connection, then call
116 * write_from_connection for each part, and then close the connection */
117
118 /* If the device_listen failed, then we will soon be cancelled, so wait
119 * for that to occur and then send XMSG_DONE */
120 if (!self->listen_ok) {
121 DBG(2, "listen failed; waiting for cancellation without attempting an accept");
122 wait_until_xfer_cancelled(elt->xfer);
123 goto send_xmsg_done;
124 }
125
126 g_mutex_lock(self->state_mutex);
127
128 /* first, accept a new connection from the device */
129 DBG(2, "accepting DirectTCP connection on device %s", self->device->device_name);
130 result = device_accept(self->device, &self->conn, &elt->cancelled,
131 self->state_mutex, self->abort_cond);
132 if (result == 1 && !elt->cancelled) {
133 xfer_cancel_with_error(XFER_ELEMENT(self),
134 "accepting DirectTCP connection: %s",
135 device_error_or_status(self->device));
136 g_mutex_unlock(self->state_mutex);
137 wait_until_xfer_cancelled(elt->xfer);
138 goto send_xmsg_done;
139 } else if (result == 2 || elt->cancelled) {
140 g_mutex_unlock(self->state_mutex);
141 wait_until_xfer_cancelled(elt->xfer);
142 goto send_xmsg_done;
143 }
144
145 DBG(2, "connection accepted; sending XMSG_READY");
146 xfer_queue_message(elt->xfer, xmsg_new(elt, XMSG_READY, 0));
147
148 /* round the part size up to the next multiple of the block size */
149 if (self->part_size) {
150 self->part_size += self->device->block_size-1;
151 self->part_size -= self->part_size % self->device->block_size;
152 }
153
154 /* now loop until we're out of parts */
155 while (1) {
156 guint64 size;
157 int fileno;
158 XMsg *msg = NULL;
159 gboolean eom, eof;
160
161 /* wait to be un-paused */
162 while (!elt->cancelled && self->paused) {
163 DBG(9, "waiting to be un-paused");
164 g_cond_wait(self->paused_cond, self->state_mutex);
165 }
166 DBG(9, "worker_thread done waiting");
167
168 if (elt->cancelled)
169 break;
170
171 DBG(2, "writing part to %s", self->device->device_name);
172 if (!device_start_file(self->device, self->part_header) || self->device->is_eom) {
173 /* this is not fatal to the transfer, since no data was lost. We
174 * just need a new device. The scribe special-cases 0-byte parts, and will
175 * not record this in the catalog. */
176
177 /* clean up */
178 dumpfile_free(self->part_header);
179 self->part_header = NULL;
180
181 goto empty_part;
182 }
183
184 dumpfile_free(self->part_header);
185 self->part_header = NULL;
186
187 fileno = self->device->file;
188 g_assert(fileno > 0);
189
190 /* write the part */
191 g_timer_start(timer);
192 result = device_write_from_connection(self->device,
193 self->part_size, &size, &elt->cancelled,
194 self->state_mutex, self->abort_cond);
195 if (result == 1 && !elt->cancelled) {
196 /* even if this is just a physical EOM, we may have lost data, so
197 * the whole transfer is dead. */
198 xfer_cancel_with_error(XFER_ELEMENT(self),
199 "Error writing from DirectTCP connection: %s",
200 device_error_or_status(self->device));
201 device_finish_file(self->device);
202 goto cancelled;
203 } else if (result == 2 || elt->cancelled) {
204 device_finish_file(self->device);
205 goto cancelled;
206 }
207 g_timer_stop(timer);
208
209 eom = self->device->is_eom;
210 eof = self->device->is_eof;
211
212 /* finish the file, even if we're at EOM, but if this fails then we may
213 * have lost data */
214 if (!device_finish_file(self->device)) {
215 xfer_cancel_with_error(XFER_ELEMENT(self),
216 "Error finishing tape file: %s",
217 device_error_or_status(self->device));
218 goto cancelled;
219 }
220
221 /* if we wrote zero bytes and reached EOM, then this is an empty part */
222 if (eom && !eof && size == 0) {
223 goto empty_part;
224 }
225
226 msg = xmsg_new(XFER_ELEMENT(self), XMSG_PART_DONE, 0);
227 msg->size = size;
228 msg->duration = g_timer_elapsed(timer, NULL);
229 msg->partnum = self->partnum;
230 msg->fileno = fileno;
231 msg->successful = TRUE;
232 msg->eom = eom;
233 msg->eof = eof;
234
235 /* time runs backward on some test boxes, so make sure this is positive */
236 if (msg->duration < 0) msg->duration = 0;
237
238 xfer_queue_message(elt->xfer, msg);
239
240 self->partnum++;
241
242 /* we're done at EOF */
243 if (eof)
244 break;
245
246 /* wait to be unpaused again */
247 self->paused = TRUE;
248 continue;
249
250 empty_part:
251 msg = xmsg_new(XFER_ELEMENT(self), XMSG_PART_DONE, 0);
252 msg->size = 0;
253 msg->duration = 0;
254 msg->partnum = 0;
255 msg->fileno = 0;
256 msg->successful = TRUE;
257 msg->eom = TRUE;
258 msg->eof = FALSE;
259 xfer_queue_message(elt->xfer, msg);
260
261 /* wait to be unpaused again */
262 self->paused = TRUE;
263 continue;
264
265 cancelled:
266 /* drop the mutex and wait until all elements have been cancelled
267 * before closing the connection */
268 g_mutex_unlock(self->state_mutex);
269 wait_until_xfer_cancelled(elt->xfer);
270 g_mutex_lock(self->state_mutex);
271 break;
272 }
273
274 /* close the DirectTCP connection */
275 directtcp_connection_close(self->conn);
276 g_object_unref(self->conn);
277 self->conn = NULL;
278
279 g_mutex_unlock(self->state_mutex);
280 g_timer_destroy(timer);
281
282 send_xmsg_done:
283 xfer_queue_message(elt->xfer, xmsg_new(XFER_ELEMENT(self), XMSG_DONE, 0));
284
285 return NULL;
286 }
287
288 /*
289 * Element mechanics
290 */
291
292 static gboolean
293 setup_impl(
294 XferElement *elt)
295 {
296 XferDestTaperDirectTCP *self = (XferDestTaperDirectTCP *)elt;
297
298 /* start the device listening, and get the addresses */
299 if (!device_listen(self->device, TRUE, &elt->input_listen_addrs)) {
300 elt->input_listen_addrs = NULL;
301 xfer_cancel_with_error(XFER_ELEMENT(self),
302 "Error starting DirectTCP listen: %s",
303 device_error_or_status(self->device));
304 self->listen_ok = FALSE;
305 return FALSE;
306 }
307
308 self->listen_ok = TRUE;
309 return TRUE;
310 }
311
312 static gboolean
313 start_impl(
314 XferElement *elt)
315 {
316 XferDestTaperDirectTCP *self = (XferDestTaperDirectTCP *)elt;
317 GError *error = NULL;
318
319 self->paused = TRUE;
320
321 /* start up the thread */
322 self->worker_thread = g_thread_create(worker_thread, (gpointer)self, TRUE, &error);
323 if (!self->worker_thread) {
324 g_critical(_("Error creating new thread: %s (%s)"),
325 error->message, errno? strerror(errno) : _("no error code"));
326 }
327
328 return TRUE;
329 }
330
331 static gboolean
332 cancel_impl(
333 XferElement *elt,
334 gboolean expect_eof)
335 {
336 XferDestTaperDirectTCP *self = XFER_DEST_TAPER_DIRECTTCP(elt);
337 gboolean rv;
338
339 /* chain up first */
340 rv = XFER_ELEMENT_CLASS(parent_class)->cancel(elt, expect_eof);
341
342 /* signal all of the condition variables to realize that we're no
343 * longer paused */
344 g_mutex_lock(self->state_mutex);
345 g_cond_broadcast(self->paused_cond);
346 g_cond_broadcast(self->abort_cond);
347 g_mutex_unlock(self->state_mutex);
348
349 return rv;
350 }
351
352 static void
353 start_part_impl(
354 XferDestTaper *xdtself,
355 gboolean retry_part,
356 dumpfile_t *header)
357 {
358 XferDestTaperDirectTCP *self = XFER_DEST_TAPER_DIRECTTCP(xdtself);
359
360 /* the only way self->device can become NULL is if use_device fails, in
361 * which case an error is already queued up, so just return silently */
362 if (self->device == NULL)
363 return;
364
365 g_assert(!self->device->in_file);
366 g_assert(header != NULL);
367
368 DBG(1, "start_part(retry_part=%d)", retry_part);
369
370 g_mutex_lock(self->state_mutex);
371 g_assert(self->paused);
372
373 if (self->part_header)
374 dumpfile_free(self->part_header);
375 self->part_header = dumpfile_copy(header);
376
377 DBG(1, "unpausing");
378 self->paused = FALSE;
379 g_cond_broadcast(self->paused_cond);
380
381 g_mutex_unlock(self->state_mutex);
382 }
383
384 static void
385 use_device_impl(
386 XferDestTaper *xdtself,
387 Device *device)
388 {
389 XferDestTaperDirectTCP *self = XFER_DEST_TAPER_DIRECTTCP(xdtself);
390
391 /* short-circuit if nothing is changing */
392 if (self->device == device)
393 return;
394
395 g_mutex_lock(self->state_mutex);
396
397 if (self->device)
398 g_object_unref(self->device);
399 self->device = NULL;
400
401 /* if we already have a connection, then make this device use it */
402 if (self->conn) {
403 if (!device_use_connection(device, self->conn)) {
404 /* queue up an error for later, and leave the device NULL.
405 * start_part will see this and fail silently */
406 xfer_cancel_with_error(XFER_ELEMENT(self),
407 _("Failed part was not cached; cannot retry"));
408 return;
409 }
410 }
411
412 self->device = device;
413 g_object_ref(device);
414
415 g_mutex_unlock(self->state_mutex);
416 }
417
418 static guint64
419 get_part_bytes_written_impl(
420 XferDestTaper *xdtself G_GNUC_UNUSED)
421 {
422 /* This operation is not supported for this taper dest. Maybe someday. */
423 return 0;
424 }
425
426 static void
427 instance_init(
428 XferElement *elt)
429 {
430 XferDestTaperDirectTCP *self = XFER_DEST_TAPER_DIRECTTCP(elt);
431 elt->can_generate_eof = FALSE;
432
433 self->worker_thread = NULL;
434 self->paused = TRUE;
435 self->conn = NULL;
436 self->state_mutex = g_mutex_new();
437 self->paused_cond = g_cond_new();
438 self->abort_cond = g_cond_new();
439 }
440
441 static void
442 finalize_impl(
443 GObject * obj_self)
444 {
445 XferDestTaperDirectTCP *self = XFER_DEST_TAPER_DIRECTTCP(obj_self);
446
447 if (self->conn)
448 g_object_unref(self->conn);
449 self->conn = NULL;
450
451 if (self->device)
452 g_object_unref(self->device);
453 self->device = NULL;
454
455 if (self->device)
456 g_object_unref(self->device);
457 self->device = NULL;
458
459 g_mutex_free(self->state_mutex);
460 g_cond_free(self->paused_cond);
461 g_cond_free(self->abort_cond);
462
463 if (self->part_header)
464 dumpfile_free(self->part_header);
465 self->part_header = NULL;
466
467 /* chain up */
468 G_OBJECT_CLASS(parent_class)->finalize(obj_self);
469 }
470
471 static void
472 class_init(
473 XferDestTaperDirectTCPClass * selfc)
474 {
475 XferElementClass *klass = XFER_ELEMENT_CLASS(selfc);
476 XferDestTaperClass *xdt_klass = XFER_DEST_TAPER_CLASS(selfc);
477 GObjectClass *goc = G_OBJECT_CLASS(selfc);
478 static xfer_element_mech_pair_t mech_pairs[] = {
479 { XFER_MECH_DIRECTTCP_LISTEN, XFER_MECH_NONE, XFER_NROPS(0), XFER_NTHREADS(0), XFER_NALLOC(0) },
480 { XFER_MECH_NONE, XFER_MECH_NONE, XFER_NROPS(0), XFER_NTHREADS(0), XFER_NALLOC(0) }
481 };
482
483 klass->start = start_impl;
484 klass->setup = setup_impl;
485 klass->cancel = cancel_impl;
486 xdt_klass->start_part = start_part_impl;
487 xdt_klass->use_device = use_device_impl;
488 xdt_klass->get_part_bytes_written = get_part_bytes_written_impl;
489 goc->finalize = finalize_impl;
490
491 klass->perl_class = "Amanda::Xfer::Dest::Taper::DirectTCP";
492 klass->mech_pairs = mech_pairs;
493
494 parent_class = g_type_class_peek_parent(selfc);
495 }
496
497 static GType
498 xfer_dest_taper_directtcp_get_type (void)
499 {
500 static GType type = 0;
501
502 if (G_UNLIKELY(type == 0)) {
503 static const GTypeInfo info = {
504 sizeof (XferDestTaperDirectTCPClass),
505 (GBaseInitFunc) NULL,
506 (GBaseFinalizeFunc) NULL,
507 (GClassInitFunc) class_init,
508 (GClassFinalizeFunc) NULL,
509 NULL /* class_data */,
510 sizeof (XferDestTaperDirectTCP),
511 0 /* n_preallocs */,
512 (GInstanceInitFunc) instance_init,
513 NULL
514 };
515
516 type = g_type_register_static (XFER_DEST_TAPER_TYPE, "XferDestTaperDirectTCP", &info, 0);
517 }
518
519 return type;
520 }
521
522 /*
523 * Constructor
524 */
525
526 XferElement *
527 xfer_dest_taper_directtcp(Device *first_device, guint64 part_size)
528 {
529 XferDestTaperDirectTCP *self = (XferDestTaperDirectTCP *)g_object_new(XFER_DEST_TAPER_DIRECTTCP_TYPE, NULL);
530
531 g_assert(device_directtcp_supported(first_device));
532
533 self->part_size = part_size;
534 self->device = first_device;
535 self->partnum = 1;
536 g_object_ref(self->device);
537
538 return XFER_ELEMENT(self);
539 }