Debian Codegraph @

device-src/ndmp-device.c

raw
1 /*
2 * Copyright (c) 2009-2012 Zmanda, Inc. All Rights Reserved.
3 * Copyright (c) 2013-2016 Carbonite, Inc. All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * Contact information: Carbonite Inc., 756 N Pastoria Ave
20 * Sunnyvale, CA 94085, or: http://www.zmanda.com
21 */
22
23 #include "amanda.h"
24 #include "amutil.h"
25 #include "device.h"
26 #include "directtcp.h"
27 #include "stream.h"
28 #include "ndmlib.h"
29 #include "ndmpconnobj.h"
30 #include "sockaddr-util.h"
31
32 /*
33 * Type checking and casting macros
34 */
35 #define TYPE_NDMP_DEVICE (ndmp_device_get_type())
36 #define NDMP_DEVICE(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), ndmp_device_get_type(), NdmpDevice)
37 #define NDMP_DEVICE_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), ndmp_device_get_type(), NdmpDevice const)
38 #define NDMP_DEVICE_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), ndmp_device_get_type(), NdmpDeviceClass)
39 #define IS_NDMP_DEVICE(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), ndmp_device_get_type ())
40 #define NDMP_DEVICE_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), ndmp_device_get_type(), NdmpDeviceClass)
41 static GType ndmp_device_get_type (void);
42
43 /*
44 * Main object structure
45 */
46
47 typedef struct NdmpDevice_ NdmpDevice;
48 struct NdmpDevice_ {
49 Device __parent__;
50
51 NDMPConnection *ndmp;
52
53 /* true if tape service is open on the NDMP server */
54 gboolean tape_open;
55 guint64 bytes_moved_before;
56
57 /* addresses the object is listening on, and how the connection
58 * was opened */
59 DirectTCPAddr *listen_addrs;
60 gboolean for_writing;
61
62 /* support for IndirectTCP */
63 int indirecttcp_sock; /* -1 if not in use */
64 int indirect;
65
66 /* Current DirectTCPConnectionNDMP */
67 struct DirectTCPConnectionNDMP_ *directtcp_conn;
68
69 /* constructor parameters and properties */
70 gchar *ndmp_hostname;
71 gint ndmp_port;
72 gchar *ndmp_device_name;
73 gchar *ndmp_username;
74 gchar *ndmp_password;
75 gchar *ndmp_auth;
76 gboolean verbose;
77 gsize read_block_size;
78
79 GMutex *abort_mutex;
80 GCond *abort_cond;
81 gboolean cancel;
82 int *cancelled;
83 };
84
85 /*
86 * Class definition
87 */
88
89 typedef struct NdmpDeviceClass_ NdmpDeviceClass;
90 struct NdmpDeviceClass_ {
91 DeviceClass __parent__;
92 };
93
94 /*
95 * A directtcp connection subclass representing a running mover on the other end of
96 * the given NDMP connection
97 */
98
99 #define TYPE_DIRECTTCP_CONNECTION_NDMP (directtcp_connection_ndmp_get_type())
100 #define DIRECTTCP_CONNECTION_NDMP(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), directtcp_connection_ndmp_get_type(), DirectTCPConnectionNDMP)
101 #define DIRECTTCP_CONNECTION_NDMP_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), directtcp_connection_ndmp_get_type(), DirectTCPConnectionNDMP const)
102 #define DIRECTTCP_CONNECTION_NDMP_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), directtcp_connection_ndmp_get_type(), DirectTCPConnectionNDMPClass)
103 #define IS_DIRECTTCP_CONNECTION_NDMP(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), directtcp_connection_ndmp_get_type ())
104 #define DIRECTTCP_CONNECTION_NDMP_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), directtcp_connection_ndmp_get_type(), DirectTCPConnectionNDMPClass)
105 GType directtcp_connection_ndmp_get_type(void);
106
107 typedef struct DirectTCPConnectionNDMP_ {
108 DirectTCPConnection __parent__;
109
110 /* NDMP connection controlling the mover */
111 NDMPConnection *ndmp;
112
113 /* mode for this operation */
114 ndmp9_mover_mode mode;
115
116 /* last reported mover position in the datastream */
117 guint64 offset;
118 } DirectTCPConnectionNDMP;
119
120 typedef struct DirectTCPConnectionNDMPClass_ {
121 DirectTCPConnectionClass __parent__;
122 } DirectTCPConnectionNDMPClass;
123
124 static DirectTCPConnectionNDMP *directtcp_connection_ndmp_new(
125 NDMPConnection *ndmp,
126 ndmp9_mover_mode mode);
127
128 /*
129 * Constants and static data
130 */
131
132 #define NDMP_DEVICE_NAME "ndmp"
133
134 /* pointer to the class of our parent */
135 static DeviceClass *parent_class = NULL;
136
137 /* robust_write results */
138 typedef enum {
139 ROBUST_WRITE_OK,
140 ROBUST_WRITE_OK_LEOM,
141 ROBUST_WRITE_ERROR, /* device error already set */
142 ROBUST_WRITE_NO_SPACE
143 } robust_write_result;
144
145 /*
146 * device-specific properties
147 */
148
149 /* Authentication information for NDMP agent. Both of these are strings. */
150 static DevicePropertyBase device_property_ndmp_username;
151 static DevicePropertyBase device_property_ndmp_password;
152 static DevicePropertyBase device_property_ndmp_auth;
153 static DevicePropertyBase device_property_indirect;
154 #define PROPERTY_NDMP_USERNAME (device_property_ndmp_username.ID)
155 #define PROPERTY_NDMP_PASSWORD (device_property_ndmp_password.ID)
156 #define PROPERTY_NDMP_AUTH (device_property_ndmp_auth.ID)
157 #define PROPERTY_INDIRECT (device_property_indirect.ID)
158
159
160 /*
161 * prototypes
162 */
163
164 void ndmp_device_register(void);
165 static void set_error_from_ndmp(NdmpDevice *self);
166
167 #define ndmp_device_read_size(self) \
168 (((NdmpDevice *)(self))->read_block_size? \
169 ((NdmpDevice *)(self))->read_block_size : ((Device *)(self))->block_size)
170
171 /*
172 * Utility functions
173 */
174
175 static gboolean
176 open_connection(
177 NdmpDevice *self)
178 {
179 if (!self->ndmp) {
180 self->ndmp = ndmp_connection_new(
181 self->ndmp_hostname,
182 self->ndmp_port,
183 self->ndmp_username,
184 self->ndmp_password,
185 self->ndmp_auth);
186
187 if (ndmp_connection_err_code(self->ndmp)) {
188 char *errmsg = ndmp_connection_err_msg(self->ndmp);
189 device_set_error(DEVICE(self),
190 g_strdup_printf("could not connect to ndmp-server '%s:%d': %s",
191 self->ndmp_hostname, self->ndmp_port, errmsg),
192 DEVICE_STATUS_DEVICE_ERROR);
193 g_object_unref(self->ndmp);
194 self->ndmp = NULL;
195 return FALSE;
196 }
197
198 if (self->verbose)
199 ndmp_connection_set_verbose(self->ndmp, TRUE);
200
201 self->tape_open = FALSE;
202 }
203
204 return TRUE;
205 }
206
207 static void
208 close_connection(
209 NdmpDevice *self)
210 {
211 /* note that this does not send NDMP_TAPE_CLOSE, as it's used in error
212 * situations too */
213 if (self->ndmp) {
214 g_object_unref(self->ndmp);
215 self->ndmp = NULL;
216 self->tape_open = FALSE;
217 }
218 }
219
220 static gboolean
221 open_tape_agent(
222 NdmpDevice *self)
223 {
224 guint64 file_num, blockno, blocksize;
225
226 /* if already open, stop now */
227 if (self->tape_open) {
228 return TRUE;
229 }
230
231 if (!open_connection(self)) {
232 /* error message set by open_connection */
233 return FALSE;
234 }
235
236 g_debug("opening tape device '%s' on NDMP server '%s:%d'",
237 self->ndmp_device_name, self->ndmp_hostname, self->ndmp_port);
238
239 /* send NDMP_TAPE_OPEN, using RAW mode so that it will open even with no tape */
240 if (!ndmp_connection_tape_open(self->ndmp,
241 self->ndmp_device_name, NDMP9_TAPE_RAW_MODE)) {
242 set_error_from_ndmp(self);
243 return FALSE;
244 }
245
246 /* check that the block sizes match */
247 if (!ndmp_connection_tape_get_state(self->ndmp,
248 &blocksize, &file_num, &blockno)) {
249 set_error_from_ndmp(self);
250 return FALSE;
251 }
252 if (blocksize != 0 && blocksize != DEVICE(self)->block_size) {
253 device_set_error(DEVICE(self),
254 g_strdup_printf("NDMP device has fixed block size %ju, but Amanda "
255 "device is configured with blocksize %ju", (uintmax_t)blocksize,
256 (uintmax_t)(DEVICE(self)->block_size)),
257 DEVICE_STATUS_DEVICE_ERROR);
258 }
259
260 self->tape_open = TRUE;
261
262 return TRUE;
263 }
264
265 static gboolean
266 close_tape_agent(
267 NdmpDevice *self)
268 {
269 if (self->tape_open) {
270 g_debug("closing tape device '%s' on NDMP server '%s:%d'",
271 self->ndmp_device_name, self->ndmp_hostname, self->ndmp_port);
272 self->tape_open = FALSE; /* count it as closed even if there is an error */
273 if (!ndmp_connection_tape_close(self->ndmp)) {
274 set_error_from_ndmp(self);
275 return FALSE;
276 }
277 }
278
279 return TRUE;
280 }
281
282 static gboolean
283 single_ndmp_mtio(
284 NdmpDevice *self,
285 ndmp9_tape_mtio_op tape_op)
286 {
287 guint resid;
288
289 if (!ndmp_connection_tape_mtio(self->ndmp, tape_op, 1, &resid)) {
290 set_error_from_ndmp(self);
291 return FALSE;
292 }
293
294 if (resid != 0) {
295 device_set_error(DEVICE(self),
296 g_strdup_printf("NDMP MTIO operation %d did not complete", tape_op),
297 DEVICE_STATUS_DEVICE_ERROR);
298 }
299
300 return TRUE;
301 }
302
303 /* get the tape state straight from the device; we try to track these things
304 * accurately in the device, but sometimes it's good to check. */
305 static gboolean
306 ndmp_get_state(
307 NdmpDevice *self)
308 {
309 Device *dself = DEVICE(self);
310 guint64 file_num, blockno, blocksize;
311
312 if (!ndmp_connection_tape_get_state(self->ndmp,
313 &blocksize, &file_num, &blockno)) {
314 set_error_from_ndmp(self);
315 return FALSE;
316 }
317
318 g_assert(file_num < INT_MAX);
319 dself->file = (int)file_num;
320 dself->block = blockno;
321
322 return TRUE;
323 }
324
325 static robust_write_result
326 robust_write(
327 NdmpDevice *self,
328 char *buf,
329 guint64 count)
330 {
331 guint64 actual;
332 robust_write_result subresult;
333
334 if (!ndmp_connection_tape_write(self->ndmp, buf, count, &actual)) {
335 switch (ndmp_connection_err_code(self->ndmp)) {
336 case NDMP9_IO_ERR:
337 /* We encountered PEOM; this only happens when the caller ignores
338 * LEOM */
339 return ROBUST_WRITE_NO_SPACE;
340
341 case NDMP9_EOM_ERR:
342 /* We encountered LEOM; retry the write (which should succeed) */
343 subresult = robust_write(self, buf, count);
344 if (subresult != ROBUST_WRITE_OK)
345 return subresult;
346 g_debug("ndmp device hit logical EOM");
347 return ROBUST_WRITE_OK_LEOM;
348
349 default:
350 set_error_from_ndmp(self);
351 return ROBUST_WRITE_ERROR;
352 }
353 }
354
355 g_assert(count == actual);
356 return ROBUST_WRITE_OK;
357 }
358
359 static void
360 set_error_from_ndmp(
361 NdmpDevice *self)
362 {
363 /* translate some error codes to the corresponding Device API status */
364 switch (ndmp_connection_err_code(self->ndmp)) {
365 case NDMP9_NO_TAPE_LOADED_ERR:
366 device_set_error(DEVICE(self),
367 g_strdup(_("no tape loaded")),
368 DEVICE_STATUS_VOLUME_MISSING);
369 break;
370
371 case NDMP9_DEVICE_BUSY_ERR:
372 device_set_error(DEVICE(self),
373 g_strdup(_("device busy")),
374 DEVICE_STATUS_DEVICE_BUSY);
375 break;
376
377 case NDMP9_IO_ERR:
378 device_set_error(DEVICE(self),
379 g_strdup(_("IO error")),
380 DEVICE_STATUS_VOLUME_UNLABELED |
381 DEVICE_STATUS_VOLUME_ERROR |
382 DEVICE_STATUS_DEVICE_ERROR);
383 break;
384
385 default:
386 device_set_error(DEVICE(self),
387 ndmp_connection_err_msg(self->ndmp),
388 DEVICE_STATUS_DEVICE_ERROR);
389 break;
390 }
391 close_connection(self);
392 }
393
394 /*
395 * Virtual function overrides
396 */
397
398 static void
399 ndmp_device_open_device(
400 Device *dself,
401 char *device_name,
402 char *device_type,
403 char *device_node)
404 {
405 NdmpDevice *self = NDMP_DEVICE(dself);
406 char *colon, *at;
407
408 /* first, extract the various parts of the device_node:
409 * HOST[:PORT]@DEVICE */
410 colon = strchr(device_node, ':');
411 at = strchr(device_node, '@');
412 if (colon > at)
413 colon = NULL; /* :PORT only counts if it's before the device name */
414 if (!at) {
415 device_set_error(dself,
416 g_strdup_printf("invalid ndmp device name '%s'", device_name),
417 DEVICE_STATUS_DEVICE_ERROR);
418 return;
419 }
420
421 if (colon) {
422 char *p = NULL;
423 long port = strtol(colon+1, &p, 10);
424
425 if (port < 0 || port >= 65536 || p != at || (!port && EINVAL == errno)) {
426 device_set_error(dself,
427 g_strdup_printf("invalid ndmp port in device name '%s'",
428 device_name),
429 DEVICE_STATUS_DEVICE_ERROR);
430 return;
431 }
432 self->ndmp_port = (gint)port;
433 self->ndmp_hostname = g_strndup(device_node, colon-device_node);
434 } else {
435 self->ndmp_port = 0; /* (use ndmjob's default, 10000) */
436 self->ndmp_hostname = g_strndup(device_node, at-device_node);
437 }
438 self->ndmp_device_name = g_strdup(at+1);
439
440 if (parent_class->open_device) {
441 parent_class->open_device(dself, device_name, device_type, device_node);
442 }
443 }
444
445 static void ndmp_device_finalize(GObject * obj_self)
446 {
447 NdmpDevice *self = NDMP_DEVICE (obj_self);
448
449 if(G_OBJECT_CLASS(parent_class)->finalize)
450 (* G_OBJECT_CLASS(parent_class)->finalize)(obj_self);
451
452 (void)close_tape_agent(self); /* ignore any error */
453
454 if (self->directtcp_conn)
455 g_object_unref(self->directtcp_conn);
456
457 if (self->listen_addrs)
458 g_free(self->listen_addrs);
459
460 close_connection(self);
461
462 if (self->ndmp_hostname)
463 g_free(self->ndmp_hostname);
464 if (self->ndmp_device_name)
465 g_free(self->ndmp_device_name);
466 if (self->ndmp_username)
467 g_free(self->ndmp_username);
468 if (self->ndmp_password)
469 g_free(self->ndmp_password);
470 if (self->ndmp_auth)
471 g_free(self->ndmp_auth);
472 if (self->indirecttcp_sock != -1)
473 close(self->indirecttcp_sock);
474 }
475
476 static DeviceStatusFlags
477 ndmp_device_read_label(
478 Device *dself)
479 {
480 NdmpDevice *self = NDMP_DEVICE(dself);
481 dumpfile_t *header = NULL;
482 gpointer buf = NULL;
483 guint64 buf_size = 0;
484 gsize read_block_size = 0;
485
486 if (self->verbose) g_debug("ndmp_device_read_label");
487 amfree(dself->volume_label);
488 amfree(dself->volume_time);
489 dumpfile_free(dself->volume_header);
490 dself->volume_header = NULL;
491
492 if (device_in_error(self)) return dself->status;
493
494 if (!open_tape_agent(self)) {
495 /* error status was set by open_tape_agent */
496 return dself->status;
497 }
498
499 if (!single_ndmp_mtio(self, NDMP9_MTIO_REW)) {
500 /* error message, if any, is set by single_ndmp_mtio */
501 return dself->status;
502 }
503
504 /* read the tape header from the NDMP server */
505 dself->status = 0;
506 read_block_size = ndmp_device_read_size(self);
507 buf = g_try_malloc(read_block_size);
508 if (buf == NULL) {
509 device_set_error(dself,
510 g_strdup(_("Cannot allocate memory")),
511 DEVICE_STATUS_DEVICE_ERROR);
512 goto read_err;
513 }
514 if (!ndmp_connection_tape_read(self->ndmp,
515 buf,
516 read_block_size,
517 &buf_size)) {
518
519 /* handle known errors */
520 switch (ndmp_connection_err_code(self->ndmp)) {
521 case NDMP9_NO_TAPE_LOADED_ERR:
522 device_set_error(dself,
523 g_strdup(_("no tape loaded")),
524 DEVICE_STATUS_VOLUME_MISSING);
525 goto read_err;
526
527 case NDMP9_IO_ERR:
528 device_set_error(dself,
529 g_strdup(_("IO error reading tape label")),
530 DEVICE_STATUS_VOLUME_UNLABELED |
531 DEVICE_STATUS_VOLUME_ERROR |
532 DEVICE_STATUS_DEVICE_ERROR);
533 goto read_err;
534
535 case NDMP9_EOM_ERR:
536 case NDMP9_EOF_ERR:
537 device_set_error(dself,
538 g_strdup(_("no tape label found")),
539 DEVICE_STATUS_VOLUME_UNLABELED);
540 header = dself->volume_header = g_new(dumpfile_t, 1);
541 fh_init(header);
542 goto read_err;
543
544 default:
545 set_error_from_ndmp(self);
546 goto read_err;
547 }
548 }
549
550 header = dself->volume_header = g_new(dumpfile_t, 1);
551 fh_init(header);
552 parse_file_header(buf, header, buf_size);
553
554 read_err:
555 g_free(buf);
556
557 if (dself->status != 0) {
558 /* error already set above */
559 return dself->status;
560 }
561
562 if (!header) {
563 device_set_error(dself,
564 g_strdup(_("no header set")),
565 DEVICE_STATUS_VOLUME_UNLABELED);
566 return dself->status;
567 }
568
569 /* handle a "weird" label */
570 if (header->type != F_TAPESTART) {
571 device_set_error(dself,
572 g_strdup(_("No tapestart header -- unlabeled device?")),
573 DEVICE_STATUS_VOLUME_UNLABELED);
574 return dself->status;
575 }
576 dself->volume_label = g_strdup(header->name);
577 dself->volume_time = g_strdup(header->datestamp);
578 /* dself->volume_header is already set */
579
580 /* note: connection is left open, as well as the tape device */
581
582 device_set_error(dself, NULL, DEVICE_STATUS_SUCCESS);
583
584 return dself->status;
585 }
586
587
588 static gboolean
589 ndmp_device_start(
590 Device *dself,
591 DeviceAccessMode mode,
592 char *label,
593 char *timestamp)
594 {
595 NdmpDevice *self = NDMP_DEVICE(dself);
596 dumpfile_t *header;
597 char *header_buf;
598
599 if (self->verbose) g_debug("ndmp_device_start");
600 if (device_in_error(self)) return FALSE;
601
602 if (!open_tape_agent(self)) {
603 /* error status was set by open_tape_agent */
604 return FALSE;
605 }
606
607 if (mode != ACCESS_WRITE && dself->volume_label == NULL) {
608 if (ndmp_device_read_label(dself) != DEVICE_STATUS_SUCCESS)
609 /* the error was set by ndmp_device_read_label */
610 return FALSE;
611 }
612
613 dself->access_mode = mode;
614 g_mutex_lock(dself->device_mutex);
615 dself->in_file = FALSE;
616 g_mutex_unlock(dself->device_mutex);
617
618 if (!single_ndmp_mtio(self, NDMP9_MTIO_REW)) {
619 /* single_ndmp_mtio already set our error message */
620 return FALSE;
621 }
622
623 /* Position the tape */
624 switch (mode) {
625 case ACCESS_APPEND:
626 device_set_error(dself,
627 g_strdup("operation not supported"),
628 DEVICE_STATUS_DEVICE_ERROR);
629 return FALSE;
630 break;
631
632 case ACCESS_READ:
633 dself->file = 0;
634 break;
635
636 case ACCESS_WRITE:
637 header = make_tapestart_header(dself, label, timestamp);
638 g_assert(header != NULL);
639
640 header_buf = device_build_amanda_header(dself, header, NULL);
641 if (header_buf == NULL) {
642 device_set_error(dself,
643 g_strdup(_("Tapestart header won't fit in a single block!")),
644 DEVICE_STATUS_DEVICE_ERROR);
645 dumpfile_free(header);
646 return FALSE;
647 }
648
649 switch (robust_write(self, header_buf, dself->block_size)) {
650 case ROBUST_WRITE_OK_LEOM:
651 dself->is_eom = TRUE;
652 /* fall through */
653 case ROBUST_WRITE_OK:
654 break;
655
656 case ROBUST_WRITE_NO_SPACE:
657 /* this would be an odd error to see writing the tape label, but
658 * oh well */
659 device_set_error(dself,
660 g_strdup(_("No space left on device")),
661 DEVICE_STATUS_VOLUME_ERROR);
662 dself->is_eom = TRUE;
663 /* fall through */
664
665 case ROBUST_WRITE_ERROR:
666 /* error was set by robust_write or above */
667 dumpfile_free(header);
668 amfree(header_buf);
669 return FALSE;
670
671 }
672 amfree(header_buf);
673
674 if (!single_ndmp_mtio(self, NDMP9_MTIO_EOF)) {
675 /* error was set by single_ndmp_mtio */
676 dumpfile_free(header);
677 return FALSE;
678 }
679
680 g_free(dself->volume_label);
681 dself->volume_label = g_strdup(label);
682 g_free(dself->volume_time);
683 dself->volume_time = g_strdup(timestamp);
684 dumpfile_free(dself->volume_header);
685 dself->volume_header = header;
686
687 /* unset the VOLUME_UNLABELED flag, if it was set */
688 device_set_error(dself, NULL, DEVICE_STATUS_SUCCESS);
689 dself->file = 0;
690 break;
691
692 default:
693 g_assert_not_reached();
694 }
695
696 return TRUE;
697 }
698
699 static gboolean
700 ndmp_device_finish(
701 Device *dself)
702 {
703 gboolean rval;
704
705 NdmpDevice *self = NDMP_DEVICE(dself);
706 rval = !device_in_error(dself);
707
708 /* we're not in a file anymore */
709 dself->access_mode = ACCESS_NULL;
710
711 if (!close_tape_agent(self)) {
712 /* error is set by close_tape_agent */
713 rval = FALSE;
714 }
715
716 if (self->ndmp)
717 close_connection(self);
718
719 return rval;
720 }
721
722 static gboolean
723 ndmp_device_eject(
724 Device *dself)
725 {
726 NdmpDevice *self = NDMP_DEVICE(dself);
727 if (device_in_error(dself)) return FALSE;
728
729 if (!open_connection(self)) {
730 /* error was set by open_connection */
731 return FALSE;
732 }
733
734 if (!single_ndmp_mtio(self, NDMP9_MTIO_OFF)) {
735 /* error was set by single_ndmp_mtio */
736 return FALSE;
737 }
738
739 return TRUE;
740 }
741
742
743 /* functions for writing */
744
745 static gboolean
746 ndmp_device_start_file(
747 Device *dself,
748 dumpfile_t *header)
749 {
750 NdmpDevice *self = NDMP_DEVICE(dself);
751 char *header_buf;
752
753 if (self->verbose) g_debug("ndmp_device_start_file");
754 if (device_in_error(self)) return FALSE;
755
756 dself->is_eof = FALSE;
757 dself->is_eom = FALSE;
758 g_mutex_lock(dself->device_mutex);
759 dself->bytes_written = 0;
760 g_mutex_unlock(dself->device_mutex);
761
762 /* set the blocksize in the header properly */
763 header->blocksize = dself->block_size;
764
765 header_buf = device_build_amanda_header(dself, header, NULL);
766 if (header_buf == NULL) {
767 device_set_error(dself,
768 g_strdup(_("Amanda file header won't fit in a single block!")),
769 DEVICE_STATUS_DEVICE_ERROR);
770 return FALSE;
771 }
772
773 switch (robust_write(self, header_buf, dself->block_size)) {
774 case ROBUST_WRITE_OK_LEOM:
775 dself->is_eom = TRUE;
776 /* fall through */
777
778 case ROBUST_WRITE_OK:
779 break;
780
781 case ROBUST_WRITE_NO_SPACE:
782 /* this would be an odd error to see writing the tape label, but
783 * oh well */
784 device_set_error(dself,
785 g_strdup(_("No space left on device")),
786 DEVICE_STATUS_VOLUME_ERROR);
787 dself->is_eom = TRUE;
788 /* fall through */
789
790 case ROBUST_WRITE_ERROR:
791 /* error was set by robust_write or above */
792 amfree(header_buf);
793 return FALSE;
794 }
795 amfree(header_buf);
796
797 /* arrange the file numbers correctly */
798 g_mutex_lock(dself->device_mutex);
799 dself->in_file = TRUE;
800 g_mutex_unlock(dself->device_mutex);
801 if (!ndmp_get_state(self)) {
802 /* error already set by ndmp_get_state */
803 return FALSE;
804 }
805
806 /* double-check that the tape agent gave us a non-bogus file number */
807 g_assert(dself->file > 0);
808
809 return TRUE;
810 }
811
812 static DeviceWriteResult
813 ndmp_device_write_block(
814 Device *dself,
815 guint size,
816 gpointer data)
817 {
818 NdmpDevice *self = NDMP_DEVICE(dself);
819 gchar *replacement_buffer = NULL;
820
821 if (device_in_error(self)) return WRITE_FAILED;
822
823 /* zero out to the end of a short block -- tape devices only write
824 * whole blocks. */
825 if (size < dself->block_size) {
826 replacement_buffer = g_try_malloc(dself->block_size);
827 if (replacement_buffer == NULL) {
828 device_set_error(dself,
829 g_strdup(_("Cannot allocate memory")),
830 DEVICE_STATUS_DEVICE_ERROR);
831 return WRITE_FAILED;
832 }
833 memcpy(replacement_buffer, data, size);
834 bzero(replacement_buffer+size, dself->block_size-size);
835
836 data = replacement_buffer;
837 size = dself->block_size;
838 }
839
840 switch (robust_write(self, data, size)) {
841 case ROBUST_WRITE_OK_LEOM:
842 dself->is_eom = TRUE;
843 /* fall through */
844
845 case ROBUST_WRITE_OK:
846 break;
847
848 case ROBUST_WRITE_NO_SPACE:
849 /* this would be an odd error to see writing the tape label, but
850 * oh well */
851 device_set_error(dself,
852 g_strdup(_("No space left on device")),
853 DEVICE_STATUS_VOLUME_ERROR);
854 dself->is_eom = TRUE;
855 /* fall through */
856
857 case ROBUST_WRITE_ERROR:
858 /* error was set by robust_write or above */
859 if (replacement_buffer) g_free(replacement_buffer);
860 return WRITE_FAILED;
861 }
862
863 dself->block++;
864 g_mutex_lock(dself->device_mutex);
865 dself->bytes_written += size;
866 g_mutex_unlock(dself->device_mutex);
867
868 if (replacement_buffer) g_free(replacement_buffer);
869 return WRITE_SUCCEED;
870 }
871
872 static gboolean
873 ndmp_device_finish_file(
874 Device *dself)
875 {
876 NdmpDevice *self = NDMP_DEVICE(dself);
877
878 if (!dself->in_file)
879 return TRUE;
880
881 /* we're not in a file anymore */
882 g_mutex_lock(dself->device_mutex);
883 dself->in_file = FALSE;
884 g_mutex_unlock(dself->device_mutex);
885
886 if (device_in_error(dself)) return FALSE;
887
888 if (!single_ndmp_mtio(self, NDMP9_MTIO_EOF)) {
889 /* error was set by single_ndmp_mtio */
890 dself->is_eom = TRUE;
891 return FALSE;
892 }
893
894 return TRUE;
895 }
896
897 /* functions for reading */
898
899 static dumpfile_t*
900 ndmp_device_seek_file(
901 Device *dself,
902 guint file)
903 {
904 NdmpDevice *self = NDMP_DEVICE(dself);
905 gint delta;
906 guint resid;
907 gpointer buf;
908 guint64 buf_size;
909 dumpfile_t *header;
910 gsize read_block_size = 0;
911
912 if (self->verbose) g_debug("ndmp_device_seek_file %d", file);
913 if (device_in_error(dself)) return FALSE;
914
915 /* file 0 is the tape header, and isn't seekable as a distinct
916 * Device-API-level file */
917 if (file == 0) {
918 device_set_error(dself,
919 g_strdup("cannot seek to file 0"),
920 DEVICE_STATUS_DEVICE_ERROR);
921 return NULL;
922 }
923
924 /* first, make sure the file and block numbers are correct */
925 if (!ndmp_get_state(self)) {
926 /* error already set by ndmp_get_state */
927 return FALSE;
928 }
929
930 /* now calculate the file delta */
931 delta = file - dself->file;
932
933 if (delta <= 0) {
934 /* Note that this algorithm will rewind to the beginning of
935 * the current part, too */
936
937 /* BSF *past* the filemark we want to seek to */
938 if (!ndmp_connection_tape_mtio(self->ndmp, NDMP9_MTIO_BSF, -delta + 1, &resid)) {
939 set_error_from_ndmp(self);
940 return NULL;
941 }
942 if (resid != 0)
943 goto incomplete_bsf;
944
945 /* now we are on the BOT side of the filemark, but we want to be
946 * on the EOT side of it. An FSF will get us there.. */
947 if (!ndmp_connection_tape_mtio(self->ndmp, NDMP9_MTIO_FSF, 1, &resid)) {
948 set_error_from_ndmp(self);
949 return NULL;
950 }
951
952 if (resid != 0) {
953 incomplete_bsf:
954 device_set_error(dself,
955 g_strdup_printf("BSF operation failed to seek by %d files", resid),
956 DEVICE_STATUS_DEVICE_ERROR);
957 return NULL;
958 }
959 } else /* (delta > 0) */ {
960 if (!ndmp_connection_tape_mtio(self->ndmp, NDMP9_MTIO_FSF, delta, &resid)) {
961 set_error_from_ndmp(self);
962 return FALSE;
963 }
964
965 /* if we didn't seek all the way there, then we're past the tapeend */
966 if (resid > 0) {
967 device_set_error(dself,
968 g_strdup_printf(_("Could not seek forward to file %d"), file),
969 DEVICE_STATUS_VOLUME_ERROR);
970 return NULL;
971 }
972 }
973
974 /* fix up status */
975 g_mutex_lock(dself->device_mutex);
976 dself->in_file = TRUE;
977 g_mutex_unlock(dself->device_mutex);
978 dself->file = file;
979 dself->block = 0;
980 g_mutex_lock(dself->device_mutex);
981 dself->bytes_read = 0;
982 g_mutex_unlock(dself->device_mutex);
983
984 /* now read the header */
985 read_block_size = ndmp_device_read_size(self);
986 buf = g_try_malloc(read_block_size);
987 if (buf == NULL) {
988 device_set_error(dself,
989 g_strdup(_("Cannot allocate memory")),
990 DEVICE_STATUS_DEVICE_ERROR);
991 return NULL;
992 }
993 if (!ndmp_connection_tape_read(self->ndmp,
994 buf, read_block_size, &buf_size)) {
995 switch (ndmp_connection_err_code(self->ndmp)) {
996 case NDMP9_EOF_ERR:
997 case NDMP9_EOM_ERR:
998 return make_tapeend_header();
999
1000 default:
1001 set_error_from_ndmp(self);
1002 g_free(buf);
1003 return NULL;
1004 }
1005 }
1006
1007 header = g_new(dumpfile_t, 1);
1008 fh_init(header);
1009 parse_file_header(buf, header, buf_size);
1010 g_free(buf);
1011
1012 return header;
1013 }
1014
1015 static gboolean
1016 ndmp_device_seek_block(
1017 Device *dself,
1018 guint64 block)
1019 {
1020 if (device_in_error(dself)) return FALSE;
1021
1022 dself->block = block;
1023
1024 device_set_error(dself, g_strdup("operation not supported"), DEVICE_STATUS_DEVICE_ERROR);
1025 return FALSE;
1026 }
1027
1028 static int
1029 ndmp_device_read_block (Device * dself, gpointer data, int *size_req, int max_block G_GNUC_UNUSED) {
1030 NdmpDevice *self = NDMP_DEVICE(dself);
1031 guint64 requested, actual;
1032 gsize read_block_size = ndmp_device_read_size(self);
1033
1034 /* We checked the NDMP device's blocksize when the device was opened, which should
1035 * catch any misalignent of server block size and Amanda block size */
1036
1037 g_assert(read_block_size < INT_MAX); /* check data type mismatch */
1038 if (!data || *size_req < (int)(read_block_size)) {
1039 *size_req = (int)(read_block_size);
1040 return 0;
1041 }
1042
1043 requested = *size_req;
1044 if (!ndmp_connection_tape_read(self->ndmp,
1045 data,
1046 requested,
1047 &actual)) {
1048
1049 /* handle known errors */
1050 switch (ndmp_connection_err_code(self->ndmp)) {
1051 case NDMP9_EOM_ERR:
1052 case NDMP9_EOF_ERR:
1053 dself->is_eof = TRUE;
1054 return -1;
1055
1056 default:
1057 set_error_from_ndmp(self);
1058 return -1;
1059 }
1060 }
1061
1062 *size_req = (int)actual; /* cast is OK - requested size was < INT_MAX too */
1063 g_mutex_lock(dself->device_mutex);
1064 dself->bytes_read += actual;
1065 g_mutex_unlock(dself->device_mutex);
1066
1067 return *size_req;
1068 }
1069
1070 static gboolean
1071 indirecttcp_listen(
1072 NdmpDevice *self,
1073 DirectTCPAddr **addrs)
1074 {
1075 in_port_t port;
1076
1077 if (self->verbose) g_debug("indirecttcp_listen");
1078 self->indirecttcp_sock = stream_server(AF_INET, &port, 0, STREAM_BUFSIZE, 0);
1079 if (self->indirecttcp_sock < 0) {
1080 device_set_error(DEVICE(self),
1081 g_strdup_printf("Could not bind indirecttcp socket: %s", strerror(errno)),
1082 DEVICE_STATUS_DEVICE_ERROR);
1083 return FALSE;
1084 }
1085
1086 /* An IndirectTCP address is 255.255.255.255:$port */
1087 self->listen_addrs = *addrs = g_new0(DirectTCPAddr, 2);
1088 addrs[0]->sin.sin_family = AF_INET;
1089 addrs[0]->sin.sin_addr.s_addr = htonl(0xffffffff);
1090 SU_SET_PORT(addrs[0], port);
1091
1092 return TRUE;
1093 }
1094
1095 static gboolean
1096 listen_impl(
1097 Device *dself,
1098 gboolean for_writing,
1099 DirectTCPAddr **addrs)
1100 {
1101 NdmpDevice *self = NDMP_DEVICE(dself);
1102
1103 if (self->verbose) g_debug("listen_impl");
1104 if (device_in_error(self)) return FALSE;
1105
1106 /* check status */
1107 g_assert(!self->listen_addrs);
1108
1109 if (!open_tape_agent(self)) {
1110 /* error message was set by open_tape_agent */
1111 return FALSE;
1112 }
1113
1114 self->for_writing = for_writing;
1115
1116 /* first, set the window to an empty span so that the mover doesn't start
1117 * reading or writing data immediately. NDMJOB tends to reset the record
1118 * size periodically (in direct contradiction to the spec), so we reset it
1119 * here as well. */
1120 if (!ndmp_connection_mover_set_record_size(self->ndmp,
1121 DEVICE(self)->block_size)) {
1122 set_error_from_ndmp(self);
1123 return FALSE;
1124 }
1125
1126 if (for_writing) {
1127 /* if we're forcing indirecttcp, just do it */
1128 if (self->indirect) {
1129 return indirecttcp_listen(self, addrs);
1130 }
1131 if (!ndmp_connection_mover_set_window(self->ndmp, 0, 0)) {
1132 /* NDMP9_ILLEGAL_ARGS_ERR means the NDMP server doesn't like a zero-byte
1133 * mover window, so we'll ignore it */
1134 if (ndmp_connection_err_code(self->ndmp) != NDMP9_ILLEGAL_ARGS_ERR) {
1135 set_error_from_ndmp(self);
1136 return FALSE;
1137 }
1138
1139 g_debug("NDMP Device: cannot set zero-length mover window; "
1140 "falling back to IndirectTCP");
1141 /* In this case, we need to set up IndirectTCP */
1142 return indirecttcp_listen(self, addrs);
1143 }
1144 } else {
1145 /* For reading, set the window to the second mover record, so that the
1146 * mover will pause immediately when it wants to read the first mover
1147 * record. */
1148 if (!ndmp_connection_mover_set_window(self->ndmp, 0, -1)) {
1149 set_error_from_ndmp(self);
1150 return FALSE;
1151 }
1152 }
1153
1154 /* then tell it to start listening */
1155 if (!ndmp_connection_mover_listen(self->ndmp,
1156 for_writing? NDMP9_MOVER_MODE_READ : NDMP9_MOVER_MODE_WRITE,
1157 NDMP9_ADDR_TCP,
1158 addrs)) {
1159 set_error_from_ndmp(self);
1160 return FALSE;
1161 }
1162 self->listen_addrs = *addrs;
1163
1164 return TRUE;
1165 }
1166
1167
1168 static gpointer
1169 accept_wait_cond(
1170 gpointer data)
1171 {
1172 NdmpDevice *self = NDMP_DEVICE(data);
1173
1174 ndmp9_mover_state state;
1175 guint64 bytes_moved;
1176
1177 gulong backoff = G_USEC_PER_SEC/20; /* 5 msec */
1178 if (self->verbose) g_debug("accept_wait_cond");
1179 g_mutex_lock(self->abort_mutex);
1180 while (1) {
1181 g_mutex_unlock(self->abort_mutex);
1182 if (!ndmp_connection_mover_get_state(self->ndmp,
1183 &state, &bytes_moved, NULL, NULL)) {
1184 g_mutex_lock(self->abort_mutex);
1185 set_error_from_ndmp(self);
1186 state = 0;
1187 break;
1188 }
1189 g_mutex_lock(self->abort_mutex);
1190
1191 if (state != NDMP9_MOVER_STATE_LISTEN)
1192 break;
1193
1194 /* back off a little bit to give the other side time to breathe,
1195 * but not more than one second */
1196 g_mutex_unlock(self->abort_mutex);
1197 g_usleep(backoff);
1198 g_mutex_lock(self->abort_mutex);
1199 if (self->cancel)
1200 break;
1201 backoff *= 2;
1202 if (backoff > G_USEC_PER_SEC)
1203 backoff = G_USEC_PER_SEC;
1204 }
1205
1206 self->cancel = TRUE;
1207 g_cond_broadcast(self->abort_cond);
1208 g_mutex_unlock(self->abort_mutex);
1209 return GINT_TO_POINTER(state);
1210 }
1211
1212 static int
1213 accept_impl(
1214 Device *dself,
1215 DirectTCPConnection **dtcpconn,
1216 gboolean *cancelled,
1217 GMutex *abort_mutex,
1218 GCond *abort_cond)
1219 {
1220 NdmpDevice *self = NDMP_DEVICE(dself);
1221 ndmp9_mover_state state;
1222 ndmp9_mover_mode mode;
1223 ndmp9_mover_halt_reason mover_halt_reason = NDMP9_MOVER_HALT_NA;
1224 ndmp9_mover_pause_reason mover_pause_reason = NDMP9_MOVER_PAUSE_NA;
1225 guint64 seek_position;
1226 GThread *wait_thread;
1227 int result;
1228 char *err;
1229
1230 if (self->verbose) g_debug("accept_impl");
1231 if (device_in_error(self)) return 1;
1232
1233 self->abort_mutex = abort_mutex;
1234 self->abort_cond = abort_cond;
1235 self->cancelled = cancelled;
1236 self->cancel = FALSE;
1237
1238 g_assert(self->listen_addrs);
1239
1240 *dtcpconn = NULL;
1241
1242 if (!self->for_writing) {
1243 /* when reading, we don't get any kind of notification that the
1244 * connection has been established, but we can't call NDMP_MOVER_READ
1245 * until the mover is active. So we have to poll, waiting for ACTIVE.
1246 * This is ugly. */
1247
1248 wait_thread = g_thread_create(accept_wait_cond, (gpointer)self, TRUE,
1249 NULL);
1250 while (!*cancelled && !self->cancel) {
1251 g_cond_wait(self->abort_cond, self->abort_mutex);
1252 }
1253 self->cancel = TRUE;
1254 g_mutex_unlock(self->abort_mutex);
1255 state = GPOINTER_TO_INT(g_thread_join(wait_thread));
1256 g_mutex_lock(self->abort_mutex);
1257
1258 if (*cancelled) {
1259 result = 2;
1260 goto accept_failed;
1261 }
1262
1263 /* double-check state */
1264 if (state != NDMP9_MOVER_STATE_ACTIVE) {
1265 device_set_error(DEVICE(self),
1266 g_strdup("mover did not enter the ACTIVE state as expected"),
1267 DEVICE_STATUS_DEVICE_ERROR);
1268 result = 1;
1269 goto accept_failed;
1270 }
1271
1272 /* now, we need to get this into the PAUSED state, since right now we
1273 * aren't allowed to perform any tape movement commands. So we issue a
1274 * MOVER_READ request for the whole darn image stream after setting the
1275 * usual empty window. Note that this means the whole dump will be read
1276 * in one MOVER_READ operation, even if it does not begin at the
1277 * beginning of a part. */
1278 if (!ndmp_connection_mover_read(self->ndmp, 0, G_MAXUINT64)) {
1279 set_error_from_ndmp(self);
1280 result = 1;
1281 goto accept_failed;
1282 }
1283
1284 /* now we should expect a notice that the mover has paused */
1285 } else {
1286 /* when writing, the mover will pause as soon as the first byte comes
1287 * in, so there's no need to do anything to trigger the pause. */
1288
1289 if (self->indirecttcp_sock == -1) {
1290 /* NDMJOB sends NDMP9_MOVER_PAUSE_SEEK to indicate that it wants to
1291 * write outside the window, while the standard specifies .._EOW,
1292 * instead. When reading to a connection, we get the appropriate
1293 * .._SEEK. It's easy enough to handle both.
1294 */
1295 result = ndmp_connection_wait_for_notify_with_cond(self->ndmp,
1296 NULL,
1297 &mover_halt_reason,
1298 &mover_pause_reason, &seek_position,
1299 cancelled,
1300 abort_mutex, abort_cond);
1301
1302 if (result == 2) {
1303 goto accept_failed;
1304 }
1305
1306 err = NULL;
1307 if (mover_pause_reason) {
1308 switch (mover_pause_reason) {
1309 case NDMP9_MOVER_PAUSE_SEEK:
1310 case NDMP9_MOVER_PAUSE_EOW:
1311 break;
1312 default:
1313 err = "got NOTIFY_MOVER_PAUSED, but not because of EOW or SEEK";
1314 break;
1315 }
1316 } else if (mover_halt_reason) {
1317 err = "unexpected NOTIFY_MOVER_HALT";
1318 }
1319
1320 if (err) {
1321 device_set_error(DEVICE(self),
1322 g_strdup_printf("waiting NDMP_MOVER_PAUSE_SEEK: %s", err),
1323 DEVICE_STATUS_DEVICE_ERROR);
1324 result = 1;
1325 goto accept_failed;
1326 }
1327 }
1328 }
1329
1330 /* NDMJOB sends NDMP9_MOVER_PAUSE_SEEK to indicate that it wants to write
1331 * outside the window, while the standard specifies .._EOW, instead. When
1332 * reading to a connection, we get the appropriate .._SEEK. It's easy
1333 * enough to handle both. */
1334
1335 if (self->indirecttcp_sock == -1) {
1336 g_free(self->listen_addrs);
1337 self->listen_addrs = NULL;
1338 }
1339
1340 if (self->for_writing)
1341 mode = NDMP9_MOVER_MODE_READ;
1342 else
1343 mode = NDMP9_MOVER_MODE_WRITE;
1344
1345 /* set up the new directtcp connection */
1346 if (self->directtcp_conn)
1347 g_object_unref(self->directtcp_conn);
1348 self->directtcp_conn =
1349 directtcp_connection_ndmp_new(self->ndmp, mode);
1350 *dtcpconn = DIRECTTCP_CONNECTION(self->directtcp_conn);
1351
1352 /* reference it for the caller */
1353 g_object_ref(*dtcpconn);
1354
1355 return 0;
1356
1357 accept_failed:
1358 if (self->indirecttcp_sock == -1) {
1359 g_free(self->listen_addrs);
1360 self->listen_addrs = NULL;
1361 }
1362 return result;
1363 }
1364
1365
1366 static int
1367 connect_impl(
1368 Device *dself,
1369 gboolean for_writing,
1370 DirectTCPAddr *addrs,
1371 DirectTCPConnection **dtcpconn,
1372 int *cancelled,
1373 GMutex *abort_mutex,
1374 GCond *abort_cond)
1375 {
1376 NdmpDevice *self = NDMP_DEVICE(dself);
1377 ndmp9_mover_mode mode;
1378 ndmp9_mover_halt_reason mover_halt_reason = NDMP9_MOVER_HALT_NA;
1379 ndmp9_mover_pause_reason mover_pause_reason = NDMP9_MOVER_PAUSE_NA;
1380 guint64 seek_position;
1381 int result;
1382
1383 if (self->verbose) g_debug("connect_impl");
1384 g_assert(!self->listen_addrs);
1385
1386 *dtcpconn = NULL;
1387 self->for_writing = for_writing;
1388
1389 if (!open_tape_agent(self)) {
1390 /* error message was set by open_tape_agent */
1391 return 1;
1392 }
1393
1394 /* first, set the window to an empty span so that the mover doesn't start
1395 * reading or writing data immediately. NDMJOB tends to reset the record
1396 * size periodically (in direct contradiction to the spec), so we reset it
1397 * here as well. */
1398 if (!ndmp_connection_mover_set_record_size(self->ndmp,
1399 DEVICE(self)->block_size)) {
1400 set_error_from_ndmp(self);
1401 return 1;
1402 }
1403
1404 if (self->for_writing) {
1405 mode = NDMP9_MOVER_MODE_READ;
1406 if (!ndmp_connection_mover_set_window(self->ndmp, 0, 0)) {
1407 set_error_from_ndmp(self);
1408 return 1;
1409 }
1410 } else {
1411 mode = NDMP9_MOVER_MODE_WRITE;
1412 if (!ndmp_connection_mover_set_window(self->ndmp, 0, -1)) {
1413 set_error_from_ndmp(self);
1414 return 1;
1415 }
1416 }
1417
1418 if (!ndmp_connection_mover_connect(self->ndmp, mode, addrs)) {
1419 set_error_from_ndmp(self);
1420 return 1;
1421 }
1422
1423 if (!self->for_writing) {
1424 /* The agent is in the ACTIVE state, and will remain so until we tell
1425 * it to do something else. The thing we want to is for it to start
1426 * reading data from the tape, which will immediately trigger an EOW or
1427 * SEEK pause. */
1428 if (!ndmp_connection_mover_read(self->ndmp, 0, G_MAXUINT64)) {
1429 set_error_from_ndmp(self);
1430 return 1;
1431 }
1432
1433 } else {
1434 /* when writing, the mover will pause as soon as the first byte comes
1435 * in, so there's no need to do anything to trigger the pause.
1436 */
1437
1438 /* NDMJOB sends NDMP9_MOVER_PAUSE_SEEK to indicate that it wants to
1439 * write outside the window, while the standard specifies .._EOW,
1440 * instead. When reading to a connection, we get the appropriate
1441 * .._SEEK. It's easy enough to handle both.
1442 */
1443
1444 result = ndmp_connection_wait_for_notify_with_cond(self->ndmp,
1445 NULL,
1446 &mover_halt_reason,
1447 &mover_pause_reason, &seek_position,
1448 cancelled,
1449 abort_mutex, abort_cond);
1450
1451 if (result == 2) {
1452 return 2;
1453 }
1454
1455 if (mover_halt_reason != NDMP9_MOVER_HALT_NA) {
1456 device_set_error(DEVICE(self),
1457 g_strdup_printf("got NDMP9_MOVER_HALT"),
1458 DEVICE_STATUS_DEVICE_ERROR);
1459 return 1;
1460 }
1461 if (mover_pause_reason != NDMP9_MOVER_PAUSE_SEEK &&
1462 mover_pause_reason != NDMP9_MOVER_PAUSE_EOW) {
1463 device_set_error(DEVICE(self),
1464 g_strdup_printf("got NOTIFY_MOVER_PAUSED, but not because of EOW or SEEK"),
1465 DEVICE_STATUS_DEVICE_ERROR);
1466 return 1;
1467 }
1468 }
1469
1470 if (self->listen_addrs) {
1471 g_free(self->listen_addrs);
1472 self->listen_addrs = NULL;
1473 }
1474
1475 /* set up the new directtcp connection */
1476 if (self->directtcp_conn)
1477 g_object_unref(self->directtcp_conn);
1478 self->directtcp_conn =
1479 directtcp_connection_ndmp_new(self->ndmp, mode);
1480 *dtcpconn = DIRECTTCP_CONNECTION(self->directtcp_conn);
1481
1482 /* reference it for the caller */
1483 g_object_ref(*dtcpconn);
1484
1485 return 0;
1486 }
1487
1488 static gboolean
1489 indirecttcp_start_writing(
1490 NdmpDevice *self)
1491 {
1492 DirectTCPAddr *real_addrs, *iter;
1493 int conn_sock;
1494
1495 /* The current state is that the other end is trying to connect to
1496 * indirecttcp_sock. The mover remains IDLE, although its window is set
1497 * correctly for the part we are about to write. */
1498
1499 g_debug("indirecttcp_start_writing, ready to accept");
1500 conn_sock = accept(self->indirecttcp_sock, NULL, NULL);
1501 if (conn_sock < 0) {
1502 device_set_error(DEVICE(self),
1503 g_strdup_printf("Could not accept indirecttcp socket: %s", strerror(errno)),
1504 DEVICE_STATUS_DEVICE_ERROR);
1505 return FALSE;
1506 }
1507 g_debug("indirecttcp_start_writing, accepted");
1508
1509 close(self->indirecttcp_sock);
1510 self->indirecttcp_sock = -1;
1511
1512 /* tell mover to start listening */
1513 g_assert(self->for_writing);
1514 if (!ndmp_connection_mover_listen(self->ndmp,
1515 NDMP9_MOVER_MODE_READ,
1516 NDMP9_ADDR_TCP,
1517 &real_addrs)) {
1518 set_error_from_ndmp(self);
1519 close(conn_sock);
1520 return FALSE;
1521 }
1522
1523 /* format the addresses and send them down the socket */
1524 for (iter = real_addrs; iter && SU_GET_FAMILY(iter) != 0; iter++) {
1525 char inet[INET_ADDRSTRLEN];
1526 const char *addr;
1527 char *addrspec;
1528
1529 addr = inet_ntop(AF_INET, &iter->sin.sin_addr.s_addr, inet, INET_ADDRSTRLEN);
1530
1531 addrspec = g_strdup_printf("%s:%d%s", addr, SU_GET_PORT(iter),
1532 SU_GET_FAMILY(iter+1) !=0? " ":"");
1533 g_debug("indirecttcp_start_writing, send %s", addrspec);
1534 if (full_write(conn_sock, addrspec, strlen(addrspec)) < strlen(addrspec)) {
1535 device_set_error(DEVICE(self),
1536 g_strdup_printf("writing to indirecttcp socket: %s", strerror(errno)),
1537 DEVICE_STATUS_DEVICE_ERROR);
1538 close(conn_sock);
1539 return FALSE;
1540 }
1541 }
1542
1543 /* close the socket for good. This ensures that the next call to
1544 * write_from_connection_impl will not go through the mover setup process.
1545 * */
1546 if (close(conn_sock) < 0) {
1547 device_set_error(DEVICE(self),
1548 g_strdup_printf("closing indirecttcp socket: %s", strerror(errno)),
1549 DEVICE_STATUS_DEVICE_ERROR);
1550 return FALSE;
1551 }
1552 conn_sock = -1;
1553
1554 /* and free the listen_addrs, since we didn't free them in accept_impl */
1555 if (self->listen_addrs) {
1556 g_free(self->listen_addrs);
1557 self->listen_addrs = NULL;
1558 }
1559
1560 /* Now it's up to the remote end to connect to the mover and start sending
1561 * data. We won't get any notification when this happens, although we could
1562 * in principle poll for such a thing. */
1563 return TRUE;
1564 }
1565
1566 static int
1567 write_from_connection_impl(
1568 Device *dself,
1569 guint64 size,
1570 guint64 *actual_size,
1571 int *cancelled,
1572 GMutex *abort_mutex,
1573 GCond *abort_cond)
1574 {
1575 NdmpDevice *self = NDMP_DEVICE(dself);
1576 DirectTCPConnectionNDMP *nconn = self->directtcp_conn;
1577 gboolean eom = FALSE, eof = FALSE, eow = FALSE;
1578 ndmp9_mover_state mover_state;
1579 ndmp9_mover_halt_reason mover_halt_reason = NDMP9_MOVER_HALT_NA;
1580 ndmp9_mover_pause_reason mover_pause_reason = NDMP9_MOVER_PAUSE_NA;
1581 guint64 bytes_moved_before, bytes_moved_after;
1582 gchar *err;
1583 int result;
1584
1585 if (device_in_error(self)) return FALSE;
1586
1587 g_debug("write_from_connection_impl");
1588 if (actual_size)
1589 *actual_size = 0;
1590
1591 /* if this is false, then the caller did not use use_connection correctly */
1592 g_assert(self->directtcp_conn != NULL);
1593 g_assert(self->ndmp == nconn->ndmp);
1594 g_assert(nconn->mode == NDMP9_MOVER_MODE_READ);
1595
1596 if (!ndmp_connection_mover_get_state(self->ndmp,
1597 &mover_state, &bytes_moved_before, NULL, NULL)) {
1598 set_error_from_ndmp(self);
1599 return 1;
1600 }
1601
1602 if (self->indirecttcp_sock != -1) {
1603 /* If we're doing IndirectTCP, then we've deferred the whole
1604 * mover_set_window mover_listen process.. until now.
1605 * So the mover should be IDLE.
1606 */
1607 g_assert(mover_state == NDMP9_MOVER_STATE_IDLE);
1608 } else {
1609 /* the mover had best be PAUSED right now */
1610 g_assert(mover_state == NDMP9_MOVER_STATE_PAUSED);
1611 }
1612
1613 /* we want to set the window regardless of whether this is directtcp or
1614 * indirecttcp
1615 */
1616 if (!ndmp_connection_mover_set_window(self->ndmp,
1617 nconn->offset,
1618 size && size != G_MAXUINT64 ? size
1619 : G_MAXUINT64 - nconn->offset)) {
1620 set_error_from_ndmp(self);
1621 return 1;
1622 }
1623
1624 /* for DirectTCP, we just tell the mover to continue; IndirectTCP is more complicated. */
1625 if (self->indirecttcp_sock != -1) {
1626 if (!indirecttcp_start_writing(self)) {
1627 return 1;
1628 }
1629 } else {
1630 if (!ndmp_connection_mover_continue(self->ndmp)) {
1631 set_error_from_ndmp(self);
1632 return 1;
1633 }
1634 }
1635
1636 /* now wait for the mover to pause itself again, or halt on EOF or an error */
1637 result = ndmp_connection_wait_for_notify_with_cond(self->ndmp,
1638 NULL,
1639 &mover_halt_reason,
1640 &mover_pause_reason, NULL,
1641 cancelled, abort_mutex, abort_cond);
1642 if (result == 2) {
1643 return 2;
1644 }
1645
1646 err = NULL;
1647 if (mover_pause_reason) {
1648 switch (mover_pause_reason) {
1649 case NDMP9_MOVER_PAUSE_EOM:
1650 eom = TRUE;
1651 break;
1652
1653 /* ndmjob sends .._SEEK when it should send .._EOW, so deal with
1654 * both equivalently */
1655 case NDMP9_MOVER_PAUSE_EOW:
1656 case NDMP9_MOVER_PAUSE_SEEK:
1657 eow = TRUE;
1658 break;
1659
1660 default:
1661 err = "got NOTIFY_MOVER_PAUSED, but not because of EOW or SEEK";
1662 break;
1663 }
1664 } else if (mover_halt_reason) {
1665 switch (mover_halt_reason) {
1666 case NDMP9_MOVER_HALT_CONNECT_CLOSED:
1667 eof = TRUE;
1668 break;
1669
1670 default:
1671 case NDMP9_MOVER_HALT_ABORTED:
1672 /* case NDMP9_MOVER_HALT_MEDIA_ERROR: <-- not in ndmjob */
1673 case NDMP9_MOVER_HALT_INTERNAL_ERROR:
1674 case NDMP9_MOVER_HALT_CONNECT_ERROR:
1675 err = "unexpected NDMP_NOTIFY_MOVER_HALTED";
1676 break;
1677 }
1678 }
1679
1680 if (err) {
1681 device_set_error(DEVICE(self),
1682 g_strdup_printf("waiting for accept: %s", err),
1683 DEVICE_STATUS_DEVICE_ERROR);
1684 return 1;
1685 }
1686
1687 /* no error, so the mover stopped due to one of EOM (volume out of space),
1688 * EOF (data connection is done), or EOW (maximum part size was written).
1689 * In any case, we want to know how many bytes were written. */
1690
1691 if (!ndmp_connection_mover_get_state(self->ndmp,
1692 &mover_state, &bytes_moved_after, NULL, NULL)) {
1693 set_error_from_ndmp(self);
1694 return 1;
1695 }
1696 size = bytes_moved_after - bytes_moved_before;
1697 nconn->offset += size;
1698
1699 if (actual_size) {
1700 *actual_size = bytes_moved_after - bytes_moved_before;
1701 }
1702
1703 if (eow) {
1704 ; /* mover finished the whole part -- nothing to report! */
1705 } else if (eof) {
1706 DEVICE(self)->is_eof = TRUE;
1707 } else if (eom) {
1708 /* this is a *lossless* EOM, so no need to set error, but
1709 * we do need to figure out the actual size */
1710 DEVICE(self)->is_eom = TRUE;
1711 } else {
1712 g_assert_not_reached();
1713 error("not reached");
1714 }
1715
1716 return 0;
1717 }
1718
1719 static int
1720 read_to_connection_impl(
1721 Device *dself,
1722 guint64 size,
1723 guint64 *actual_size,
1724 int *cancelled,
1725 GMutex *abort_mutex,
1726 GCond *abort_cond)
1727 {
1728 NdmpDevice *self = NDMP_DEVICE(dself);
1729 DirectTCPConnectionNDMP *nconn = self->directtcp_conn;
1730 gboolean eom = FALSE, eof = FALSE, eow = FALSE;
1731 ndmp9_mover_state mover_state;
1732 ndmp9_mover_halt_reason mover_halt_reason = NDMP9_MOVER_HALT_NA;
1733 ndmp9_mover_pause_reason mover_pause_reason = NDMP9_MOVER_PAUSE_NA;
1734 guint64 bytes_moved_before, bytes_moved_after;
1735 gchar *err;
1736 int result;
1737
1738 if (actual_size)
1739 *actual_size = 0;
1740
1741 g_debug("read_to_connection_impl");
1742 if (device_in_error(self)) return 1;
1743
1744 /* read_to_connection does not support IndirectTCP */
1745 g_assert(self->indirecttcp_sock == -1);
1746
1747 /* if this is false, then the caller did not use use_connection correctly */
1748 g_assert(nconn != NULL);
1749 g_assert(self->ndmp == nconn->ndmp);
1750 g_assert(nconn->mode == NDMP9_MOVER_MODE_WRITE);
1751
1752 if (!ndmp_connection_mover_get_state(self->ndmp,
1753 &mover_state, &bytes_moved_before, NULL, NULL)) {
1754 set_error_from_ndmp(self);
1755 return 1;
1756 }
1757
1758 if (mover_state == NDMP9_MOVER_STATE_PAUSED) {
1759 /* the mover had best be PAUSED right now */
1760 g_assert(mover_state == NDMP9_MOVER_STATE_PAUSED);
1761
1762 if (!ndmp_connection_mover_set_window(self->ndmp,
1763 nconn->offset,
1764 size && size != G_MAXUINT64 ? size
1765 : G_MAXUINT64 - nconn->offset)) {
1766 set_error_from_ndmp(self);
1767 return 1;
1768 }
1769
1770 if (!ndmp_connection_mover_continue(self->ndmp)) {
1771 set_error_from_ndmp(self);
1772 return 1;
1773 }
1774 }
1775
1776 /* now wait for the mover to pause itself again, or halt on EOF or an error */
1777 result = ndmp_connection_wait_for_notify_with_cond(self->ndmp,
1778 NULL,
1779 &mover_halt_reason,
1780 &mover_pause_reason, NULL,
1781 cancelled, abort_mutex, abort_cond);
1782 if (result == 2) {
1783 return 2;
1784 }
1785
1786 err = NULL;
1787 if (mover_pause_reason) {
1788 switch (mover_pause_reason) {
1789 case NDMP9_MOVER_PAUSE_EOF:
1790 eof = TRUE;
1791 break;
1792
1793 /* ndmjob sends .._SEEK when it should send .._EOW, so deal with
1794 * both equivalently */
1795 case NDMP9_MOVER_PAUSE_EOW:
1796 case NDMP9_MOVER_PAUSE_SEEK:
1797 eow = TRUE;
1798 break;
1799
1800 default:
1801 err = "got NOTIFY_MOVER_PAUSED, but not because of EOW or SEEK";
1802 break;
1803 }
1804 } else if (mover_halt_reason) {
1805 switch (mover_halt_reason) {
1806 case NDMP9_MOVER_HALT_CONNECT_CLOSED:
1807 eof = TRUE;
1808 break;
1809
1810 default:
1811 case NDMP9_MOVER_HALT_ABORTED:
1812 /* case NDMP9_MOVER_HALT_MEDIA_ERROR: <-- not in ndmjob */
1813 case NDMP9_MOVER_HALT_INTERNAL_ERROR:
1814 case NDMP9_MOVER_HALT_CONNECT_ERROR:
1815 err = "unexpected NDMP_NOTIFY_MOVER_HALTED";
1816 break;
1817 }
1818 }
1819
1820 if (err) {
1821 device_set_error(DEVICE(self),
1822 g_strdup_printf("waiting for accept: %s", err),
1823 DEVICE_STATUS_DEVICE_ERROR);
1824 return 1;
1825 }
1826
1827 /* no error, so the mover stopped due to one of EOM (volume out of space),
1828 * EOF (data connection is done), or EOW (maximum part size was written).
1829 * In any case, we want to know how many bytes were written. */
1830
1831 if (!ndmp_connection_mover_get_state(self->ndmp,
1832 &mover_state, &bytes_moved_after, NULL, NULL)) {
1833 set_error_from_ndmp(self);
1834 return 1;
1835 }
1836 size = bytes_moved_after - self->bytes_moved_before;
1837 nconn->offset += size;
1838
1839 if (actual_size) {
1840 *actual_size = size;
1841 }
1842 self->bytes_moved_before = bytes_moved_after;
1843
1844 if (eow) {
1845 ; /* mover finished the whole part -- nothing to report! */
1846 } else if (eof) {
1847 DEVICE(self)->is_eof = TRUE;
1848 } else if (eom) {
1849 /* this is a *lossless* EOM, so no need to set error, but
1850 * we do need to figure out the actual size */
1851 DEVICE(self)->is_eom = TRUE;
1852 } else {
1853 g_assert_not_reached();
1854 error("not reached");
1855 }
1856
1857 return 0;
1858 }
1859
1860 static gboolean
1861 use_connection_impl(
1862 Device *dself,
1863 DirectTCPConnection *conn)
1864 {
1865 NdmpDevice *self = NDMP_DEVICE(dself);
1866 DirectTCPConnectionNDMP *nconn;
1867
1868 if (self->verbose) g_debug("read_to_connection_impl");
1869 /* the device_use_connection_impl wrapper already made sure we're in
1870 * ACCESS_NULL, but we may have opened the tape service already to read
1871 * a label - so close it to be sure */
1872 if (!close_tape_agent(self)) {
1873 /* error was already set by close_tape_agent */
1874 return FALSE;
1875 }
1876
1877 /* we had best not be listening when this is called */
1878 g_assert(!self->listen_addrs);
1879
1880 if (!IS_DIRECTTCP_CONNECTION_NDMP(conn)) {
1881 device_set_error(DEVICE(self),
1882 g_strdup("existing DirectTCPConnection is not compatible with this device"),
1883 DEVICE_STATUS_DEVICE_ERROR);
1884 return FALSE;
1885 }
1886
1887 if (self->directtcp_conn)
1888 g_object_unref(self->directtcp_conn);
1889 self->directtcp_conn = nconn = DIRECTTCP_CONNECTION_NDMP(conn);
1890 g_object_ref(self->directtcp_conn);
1891
1892 /* if this is a different connection, use it */
1893 if (nconn->ndmp != self->ndmp) {
1894 if (self->ndmp)
1895 close_connection(self);
1896 self->ndmp = nconn->ndmp;
1897 g_object_ref(self->ndmp);
1898 }
1899
1900 return TRUE;
1901 }
1902
1903 /*
1904 * Class mechanics
1905 */
1906
1907 static gboolean
1908 ndmp_device_set_username_fn(Device *dself,
1909 DevicePropertyBase *base, GValue *val,
1910 PropertySurety surety, PropertySource source)
1911 {
1912 NdmpDevice *self = NDMP_DEVICE(dself);
1913
1914 amfree(self->ndmp_username);
1915 self->ndmp_username = g_value_dup_string(val);
1916 device_clear_volume_details(dself);
1917
1918 return device_simple_property_set_fn(dself, base, val, surety, source);
1919 }
1920
1921 static gboolean
1922 ndmp_device_set_password_fn(Device *dself,
1923 DevicePropertyBase *base, GValue *val,
1924 PropertySurety surety, PropertySource source)
1925 {
1926 NdmpDevice *self = NDMP_DEVICE(dself);
1927
1928 amfree(self->ndmp_password);
1929 self->ndmp_password = g_value_dup_string(val);
1930 device_clear_volume_details(dself);
1931
1932 return device_simple_property_set_fn(dself, base, val, surety, source);
1933 }
1934
1935 static gboolean
1936 ndmp_device_set_auth_fn(Device *dself,
1937 DevicePropertyBase *base, GValue *val,
1938 PropertySurety surety, PropertySource source)
1939 {
1940 NdmpDevice *self = NDMP_DEVICE(dself);
1941
1942 amfree(self->ndmp_auth);
1943 self->ndmp_auth = g_value_dup_string(val);
1944 device_clear_volume_details(dself);
1945
1946 return device_simple_property_set_fn(dself, base, val, surety, source);
1947 }
1948
1949 static gboolean
1950 ndmp_device_set_verbose_fn(Device *p_self, DevicePropertyBase *base,
1951 GValue *val, PropertySurety surety, PropertySource source)
1952 {
1953 NdmpDevice *self = NDMP_DEVICE(p_self);
1954
1955 self->verbose = g_value_get_boolean(val);
1956
1957 /* if the connection is active, set up verbose logging or turn it off */
1958 if (self->ndmp) {
1959 ndmp_connection_set_verbose(self->ndmp, self->verbose);
1960 }
1961
1962
1963 return device_simple_property_set_fn(p_self, base, val, surety, source);
1964 }
1965
1966 static gboolean
1967 ndmp_device_set_read_block_size_fn(Device *p_self, DevicePropertyBase *base G_GNUC_UNUSED,
1968 GValue *val, PropertySurety surety, PropertySource source)
1969 {
1970 NdmpDevice *self = NDMP_DEVICE(p_self);
1971 gsize read_block_size = g_value_get_uint(val);
1972
1973 if (read_block_size != 0 &&
1974 ((gsize)read_block_size < p_self->block_size ||
1975 (gsize)read_block_size > p_self->max_block_size)) {
1976 device_set_error(p_self,
1977 g_strdup_printf("Error setting READ-BLOCK-SIZE property to '%zu', it must be between %zu and %zu", read_block_size, p_self->block_size, p_self->max_block_size),
1978 DEVICE_STATUS_DEVICE_ERROR);
1979 return FALSE;
1980 }
1981
1982 self->read_block_size = read_block_size;
1983
1984 /* use the READ_BLOCK_SIZE, even if we're invoked to get the old READ_BUFFER_SIZE */
1985 return device_simple_property_set_fn(p_self, base,
1986 val, surety, source);
1987 }
1988
1989 static gboolean
1990 ndmp_device_set_indirect_fn(Device *dself,
1991 DevicePropertyBase *base, GValue *val,
1992 PropertySurety surety, PropertySource source)
1993 {
1994 NdmpDevice *self = NDMP_DEVICE(dself);
1995
1996 self->indirect = g_value_get_boolean(val);
1997
1998 return device_simple_property_set_fn(dself, base, val, surety, source);
1999 }
2000
2001 static gboolean
2002 ndmp_device_set_leom_fn(Device *dself,
2003 DevicePropertyBase *base G_GNUC_UNUSED, GValue *val,
2004 PropertySurety surety G_GNUC_UNUSED, PropertySource source G_GNUC_UNUSED)
2005 {
2006 gboolean leom = g_value_get_boolean(val);
2007
2008 if (!leom) {
2009 device_set_error(dself,
2010 g_strdup_printf("Error setting LEOM property, it must be TRUE"),
2011 DEVICE_STATUS_DEVICE_ERROR);
2012 return FALSE;
2013 }
2014 return TRUE;
2015 }
2016
2017 static void
2018 ndmp_device_class_init(NdmpDeviceClass * c G_GNUC_UNUSED)
2019 {
2020 GObjectClass *g_object_class = (GObjectClass*) c;
2021 DeviceClass *device_class = (DeviceClass *)c;
2022
2023 parent_class = g_type_class_ref (TYPE_DEVICE);
2024
2025 device_class->open_device = ndmp_device_open_device;
2026 device_class->read_label = ndmp_device_read_label;
2027 device_class->start = ndmp_device_start;
2028 device_class->finish = ndmp_device_finish;
2029 device_class->eject = ndmp_device_eject;
2030
2031 device_class->start_file = ndmp_device_start_file;
2032 device_class->write_block = ndmp_device_write_block;
2033 device_class->finish_file = ndmp_device_finish_file;
2034
2035 device_class->seek_file = ndmp_device_seek_file;
2036 device_class->seek_block = ndmp_device_seek_block;
2037 device_class->read_block = ndmp_device_read_block;
2038
2039 device_class->directtcp_supported = TRUE;
2040 device_class->listen = listen_impl;
2041 device_class->accept= accept_impl;
2042 device_class->connect= connect_impl;
2043 device_class->write_from_connection = write_from_connection_impl;
2044 device_class->read_to_connection = read_to_connection_impl;
2045 device_class->use_connection = use_connection_impl;
2046
2047 g_object_class->finalize = ndmp_device_finalize;
2048
2049 device_class_register_property(device_class, PROPERTY_NDMP_USERNAME,
2050 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
2051 device_simple_property_get_fn,
2052 ndmp_device_set_username_fn);
2053
2054 device_class_register_property(device_class, PROPERTY_NDMP_PASSWORD,
2055 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
2056 device_simple_property_get_fn,
2057 ndmp_device_set_password_fn);
2058
2059 device_class_register_property(device_class, PROPERTY_NDMP_AUTH,
2060 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
2061 device_simple_property_get_fn,
2062 ndmp_device_set_auth_fn);
2063
2064 device_class_register_property(device_class, PROPERTY_VERBOSE,
2065 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_MASK,
2066 device_simple_property_get_fn,
2067 ndmp_device_set_verbose_fn);
2068
2069 device_class_register_property(device_class, PROPERTY_INDIRECT,
2070 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_MASK,
2071 device_simple_property_get_fn,
2072 ndmp_device_set_indirect_fn);
2073
2074 device_class_register_property(device_class, PROPERTY_READ_BLOCK_SIZE,
2075 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
2076 device_simple_property_get_fn,
2077 ndmp_device_set_read_block_size_fn);
2078
2079 device_class_register_property(device_class, PROPERTY_LEOM,
2080 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
2081 device_simple_property_get_fn,
2082 ndmp_device_set_leom_fn);
2083
2084 }
2085
2086 static void
2087 ndmp_device_init(NdmpDevice *self)
2088 {
2089 Device *dself = DEVICE(self);
2090 GValue response;
2091
2092 /* begin unconnected */
2093 self->ndmp = NULL;
2094
2095 /* decent defaults */
2096 dself->block_size = 32768;
2097 dself->min_block_size = 32768;
2098 dself->max_block_size = SIZE_MAX;
2099 dself->allow_take_scribe_from = FALSE;
2100
2101 bzero(&response, sizeof(response));
2102
2103 g_value_init(&response, CONCURRENCY_PARADIGM_TYPE);
2104 g_value_set_enum(&response, CONCURRENCY_PARADIGM_EXCLUSIVE);
2105 device_set_simple_property(dself, PROPERTY_CONCURRENCY,
2106 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
2107 g_value_unset(&response);
2108
2109 g_value_init(&response, STREAMING_REQUIREMENT_TYPE);
2110 g_value_set_enum(&response, STREAMING_REQUIREMENT_DESIRED);
2111 device_set_simple_property(dself, PROPERTY_STREAMING,
2112 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
2113 g_value_unset(&response);
2114
2115 g_value_init(&response, G_TYPE_BOOLEAN);
2116 g_value_set_boolean(&response, FALSE);
2117 device_set_simple_property(dself, PROPERTY_APPENDABLE,
2118 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
2119 g_value_unset(&response);
2120
2121 g_value_init(&response, G_TYPE_BOOLEAN);
2122 g_value_set_boolean(&response, FALSE);
2123 device_set_simple_property(dself, PROPERTY_PARTIAL_DELETION,
2124 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
2125 g_value_unset(&response);
2126
2127 g_value_init(&response, G_TYPE_BOOLEAN);
2128 g_value_set_boolean(&response, FALSE);
2129 device_set_simple_property(dself, PROPERTY_FULL_DELETION,
2130 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
2131 g_value_unset(&response);
2132
2133 g_value_init(&response, G_TYPE_BOOLEAN);
2134 g_value_set_boolean(&response, TRUE);
2135 device_set_simple_property(dself, PROPERTY_LEOM,
2136 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
2137 g_value_unset(&response);
2138
2139 g_value_init(&response, MEDIA_ACCESS_MODE_TYPE);
2140 g_value_set_enum(&response, MEDIA_ACCESS_MODE_READ_WRITE);
2141 device_set_simple_property(dself, PROPERTY_MEDIUM_ACCESS_TYPE,
2142 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
2143 g_value_unset(&response);
2144
2145 self->read_block_size = 0;
2146 g_value_init(&response, G_TYPE_UINT);
2147 g_value_set_uint(&response, self->read_block_size);
2148 device_set_simple_property(dself, PROPERTY_READ_BLOCK_SIZE,
2149 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DEFAULT);
2150 g_value_unset(&response);
2151
2152 g_value_init(&response, G_TYPE_STRING);
2153 g_value_set_string(&response, "ndmp");
2154 device_set_simple_property(dself, PROPERTY_NDMP_USERNAME,
2155 &response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
2156 g_value_unset(&response);
2157 self->ndmp_username = g_strdup("ndmp");
2158
2159 g_value_init(&response, G_TYPE_STRING);
2160 g_value_set_string(&response, "ndmp");
2161 device_set_simple_property(dself, PROPERTY_NDMP_PASSWORD,
2162 &response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
2163 g_value_unset(&response);
2164 self->ndmp_password = g_strdup("ndmp");
2165
2166 g_value_init(&response, G_TYPE_STRING);
2167 g_value_set_string(&response, "md5");
2168 device_set_simple_property(dself, PROPERTY_NDMP_AUTH,
2169 &response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
2170 g_value_unset(&response);
2171 self->ndmp_auth = g_strdup("md5");
2172
2173 g_value_init(&response, G_TYPE_BOOLEAN);
2174 g_value_set_boolean(&response, FALSE);
2175 device_set_simple_property(dself, PROPERTY_INDIRECT,
2176 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DEFAULT);
2177 g_value_unset(&response);
2178 self->indirect = TRUE;
2179
2180 self->indirecttcp_sock = -1;
2181 }
2182
2183 static GType
2184 ndmp_device_get_type(void)
2185 {
2186 static GType type = 0;
2187
2188 if (G_UNLIKELY(type == 0)) {
2189 static const GTypeInfo info = {
2190 sizeof (NdmpDeviceClass),
2191 (GBaseInitFunc) NULL,
2192 (GBaseFinalizeFunc) NULL,
2193 (GClassInitFunc) ndmp_device_class_init,
2194 (GClassFinalizeFunc) NULL,
2195 NULL /* class_data */,
2196 sizeof (NdmpDevice),
2197 0 /* n_preallocs */,
2198 (GInstanceInitFunc) ndmp_device_init,
2199 NULL
2200 };
2201
2202 type = g_type_register_static (TYPE_DEVICE, "NdmpDevice", &info,
2203 (GTypeFlags)0);
2204 }
2205
2206 return type;
2207 }
2208
2209 static Device*
2210 ndmp_device_factory(
2211 char *device_name,
2212 char *device_type,
2213 char *device_node)
2214 {
2215 Device *rval;
2216 g_assert(g_str_equal(device_type, NDMP_DEVICE_NAME));
2217 rval = DEVICE(g_object_new(TYPE_NDMP_DEVICE, NULL));
2218
2219 device_open_device(rval, device_name, device_type, device_node);
2220 return rval;
2221 }
2222
2223 void
2224 ndmp_device_register(void)
2225 {
2226 static const char * device_prefix_list[] = { NDMP_DEVICE_NAME, NULL };
2227
2228 /* register the device itself */
2229 register_device(ndmp_device_factory, device_prefix_list);
2230
2231 device_property_fill_and_register(&device_property_ndmp_username,
2232 G_TYPE_STRING, "ndmp_username",
2233 "Username for access to the NDMP agent");
2234 device_property_fill_and_register(&device_property_ndmp_password,
2235 G_TYPE_STRING, "ndmp_password",
2236 "Password for access to the NDMP agent");
2237 device_property_fill_and_register(&device_property_ndmp_auth,
2238 G_TYPE_STRING, "ndmp_auth",
2239 "Authentication method for the NDMP agent - md5 (default), text, none, or void");
2240 device_property_fill_and_register(&device_property_indirect,
2241 G_TYPE_BOOLEAN, "indirect",
2242 "Use Indirect TCP mode, even if the NDMP server supports "
2243 "window length 0");
2244 }
2245
2246 /*
2247 * DirectTCPConnectionNDMP implementation
2248 */
2249
2250 static char *
2251 directtcp_connection_ndmp_close(DirectTCPConnection *dself)
2252 {
2253 DirectTCPConnectionNDMP *self = DIRECTTCP_CONNECTION_NDMP(dself);
2254 char *rv = NULL;
2255 ndmp9_mover_state state;
2256 guint64 bytes_moved;
2257 ndmp9_mover_halt_reason mover_halt_reason;
2258 gboolean expect_notif = FALSE;
2259
2260 /* based on the current state, we may need to abort or stop the
2261 * mover before closing it */
2262 if (!ndmp_connection_mover_get_state(self->ndmp, &state,
2263 &bytes_moved, NULL, NULL)) {
2264 rv = ndmp_connection_err_msg(self->ndmp);
2265 goto error;
2266 }
2267
2268 switch (state) {
2269 case NDMP9_MOVER_STATE_HALTED:
2270 break; /* nothing to do but ndmp_mover_close, below */
2271
2272 case NDMP9_MOVER_STATE_PAUSED:
2273 if (!ndmp_connection_mover_close(self->ndmp)) {
2274 rv = ndmp_connection_err_msg(self->ndmp);
2275 goto error;
2276 }
2277 expect_notif = TRUE;
2278 break;
2279
2280 case NDMP9_MOVER_STATE_ACTIVE:
2281 default:
2282 if (!ndmp_connection_mover_abort(self->ndmp)) {
2283 rv = ndmp_connection_err_msg(self->ndmp);
2284 goto error;
2285 }
2286 expect_notif = TRUE;
2287 break;
2288 }
2289
2290 /* the spec isn't entirely clear that mover_close and mover_abort should
2291 * generate a NOTIF_MOVER_HALTED, but ndmjob does it */
2292 if (expect_notif) {
2293 if (!ndmp_connection_wait_for_notify(self->ndmp,
2294 NULL,
2295 &mover_halt_reason, /* value is ignored.. */
2296 NULL, NULL)) {
2297 goto error;
2298 }
2299 }
2300
2301 if (!ndmp_connection_mover_stop(self->ndmp)) {
2302 rv = ndmp_connection_err_msg(self->ndmp);
2303 goto error;
2304 }
2305
2306 error:
2307
2308 /* self->ndmp is always set */
2309 g_object_unref(self->ndmp);
2310 self->ndmp = NULL;
2311
2312 return rv;
2313 }
2314
2315 static void
2316 directtcp_connection_ndmp_class_init(DirectTCPConnectionNDMPClass * c)
2317 {
2318 DirectTCPConnectionClass *connc = (DirectTCPConnectionClass *)c;
2319
2320 connc->close = directtcp_connection_ndmp_close;
2321 }
2322
2323 GType
2324 directtcp_connection_ndmp_get_type (void)
2325 {
2326 static GType type = 0;
2327
2328 if (G_UNLIKELY(type == 0)) {
2329 static const GTypeInfo info = {
2330 sizeof (DirectTCPConnectionNDMPClass),
2331 (GBaseInitFunc) NULL,
2332 (GBaseFinalizeFunc) NULL,
2333 (GClassInitFunc) directtcp_connection_ndmp_class_init,
2334 (GClassFinalizeFunc) NULL,
2335 NULL /* class_data */,
2336 sizeof (DirectTCPConnectionNDMP),
2337 0 /* n_preallocs */,
2338 (GInstanceInitFunc) NULL,
2339 NULL
2340 };
2341
2342 type = g_type_register_static(TYPE_DIRECTTCP_CONNECTION,
2343 "DirectTCPConnectionNDMP", &info, (GTypeFlags)0);
2344 }
2345
2346 return type;
2347 }
2348
2349 static DirectTCPConnectionNDMP *
2350 directtcp_connection_ndmp_new(
2351 NDMPConnection *ndmp,
2352 ndmp9_mover_mode mode)
2353 {
2354 DirectTCPConnectionNDMP *dcn = DIRECTTCP_CONNECTION_NDMP(
2355 g_object_new(TYPE_DIRECTTCP_CONNECTION_NDMP, NULL));
2356
2357 /* hang onto a copy of this NDMP connection */
2358 g_object_ref(ndmp);
2359 dcn->ndmp = ndmp;
2360 dcn->mode = mode;
2361 dcn->offset = 0;
2362
2363 return dcn;
2364 }