2 * Copyright (c) 2007, 2008, 2009, 2010 Zmanda, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
22 #include "pipespawn.h"
23 #include <string.h> /* memset() */
27 #ifdef HAVE_SYS_TAPE_H
28 # include <sys/tape.h>
30 #ifdef HAVE_SYS_MTIO_H
31 # include <sys/mtio.h>
37 /* This is equal to 2*1024*1024*1024 - 16*1024*1024 - 1, but written
38 explicitly to avoid overflow issues. */
39 #define RESETOFS_THRESHOLD (0x7effffff)
41 /* Largest possible block size on SCSI systems. */
42 #define LARGEST_BLOCK_ESTIMATE (16 * 1024 * 1024)
45 * Type checking and casting macros
48 #define TYPE_TAPE_DEVICE (tape_device_get_type())
49 #define TAPE_DEVICE(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), tape_device_get_type(), TapeDevice)
50 #define TAPE_DEVICE_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), tape_device_get_type(), TapeDevice const)
51 #define TAPE_DEVICE_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), tape_device_get_type(), TapeDeviceClass)
52 #define IS_TAPE_DEVICE(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), tape_device_get_type ())
53 #define TAPE_DEVICE_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), tape_device_get_type(), TapeDeviceClass)
54 GType tape_device_get_type (void);
57 * Main object structure
59 typedef struct TapeDevicePrivate_s TapeDevicePrivate;
60 typedef struct _TapeDevice {
63 /* It should go without saying that all this stuff is
64 * look-but-don't-touch. */
66 /* characteristics of the device */
67 gboolean fsf, bsf, fsr, bsr, eom, bsf_after_eom, broken_gmt_online;
69 gboolean nonblocking_open, fsf_after_filemark;
72 /* 0 if we opened with O_RDWR; error otherwise. */
73 gboolean write_open_errno;
76 TapeDevicePrivate * private;
79 struct TapeDevicePrivate_s {
80 /* This holds the total number of bytes written to the device,
81 modulus RESETOFS_THRESHOLD. */
83 char * device_filename;
84 gsize read_block_size;
91 typedef struct _TapeDeviceClass TapeDeviceClass;
92 struct _TapeDeviceClass {
93 DeviceClass __parent__;
96 void tape_device_register(void);
98 /* useful callback for tape ops */
99 void tape_device_set_capabilities(TapeDevice *self,
100 gboolean fsf, PropertySurety fsf_surety, PropertySource fsf_source,
101 gboolean fsf_after_filemark, PropertySurety faf_surety, PropertySource faf_source,
102 gboolean bsf, PropertySurety bsf_surety, PropertySource bsf_source,
103 gboolean fsr, PropertySurety fsr_surety, PropertySource fsr_source,
104 gboolean bsr, PropertySurety bsr_surety, PropertySource bsr_source,
105 gboolean eom, PropertySurety eom_surety, PropertySource eom_source,
106 gboolean leom, PropertySurety leom_surety, PropertySource leom_source,
107 gboolean bsf_after_eom, PropertySurety bae_surety, PropertySource bae_source,
108 guint final_filemarks, PropertySurety ff_surety, PropertySource ff_source);
110 /* Real Operations (always return FALSE if not implemented) */
111 gboolean tape_rewind(int fd);
112 gboolean tape_fsf(int fd, guint count);
113 gboolean tape_bsf(int fd, guint count);
114 gboolean tape_fsr(int fd, guint count);
115 gboolean tape_bsr(int fd, guint count);
116 gint tape_fileno(int fd);
118 /* tape_fileno returns tape position file number, or one of these: */
119 #define TAPE_OP_ERROR -1
120 #define TAPE_POSITION_UNKNOWN -2
122 /* Possible (abstracted) results from a system I/O operation. */
125 RESULT_ERROR, /* Undefined error (*errmsg set) */
126 RESULT_SMALL_BUFFER, /* Tried to read with a buffer that is too
128 RESULT_NO_DATA, /* End of File, while reading */
129 RESULT_NO_SPACE, /* Out of space. Sometimes we don't know if
130 it was this or I/O error, but this is the
131 preferred explanation. */
135 /* returns a fileno like tape_fileno */
136 gint tape_eod(int fd);
138 gboolean tape_weof(int fd, guint8 count);
139 gboolean tape_setcompression(int fd, gboolean on);
141 gboolean tape_offl(int fd);
143 DeviceStatusFlags tape_is_tape_device(int fd);
144 DeviceStatusFlags tape_is_ready(int fd, TapeDevice *t_self);
146 #define tape_device_read_size(self) \
147 (((TapeDevice *)(self))->private->read_block_size? \
148 ((TapeDevice *)(self))->private->read_block_size : ((Device *)(self))->block_size)
151 * Our device-specific properties.
154 #define PROPERTY_BROKEN_GMT_ONLINE (device_property_broken_gmt_online.ID)
155 #define PROPERTY_FSF (device_property_fsf.ID)
156 #define PROPERTY_FSF_AFTER_FILEMARK (device_property_fsf_after_filemark.ID)
157 #define PROPERTY_BSF (device_property_bsf.ID)
158 #define PROPERTY_FSR (device_property_fsr.ID)
159 #define PROPERTY_BSR (device_property_bsr.ID)
160 #define PROPERTY_EOM (device_property_eom.ID)
161 #define PROPERTY_BSF_AFTER_EOM (device_property_bsf_after_eom.ID)
162 #define PROPERTY_NONBLOCKING_OPEN (device_property_nonblocking_open.ID)
163 #define PROPERTY_FINAL_FILEMARKS (device_property_final_filemarks.ID)
165 static DevicePropertyBase device_property_broken_gmt_online;
166 static DevicePropertyBase device_property_fsf;
167 static DevicePropertyBase device_property_fsf_after_filemark;
168 static DevicePropertyBase device_property_bsf;
169 static DevicePropertyBase device_property_fsr;
170 static DevicePropertyBase device_property_bsr;
171 static DevicePropertyBase device_property_eom;
172 static DevicePropertyBase device_property_bsf_after_eom;
173 static DevicePropertyBase device_property_nonblocking_open;
174 static DevicePropertyBase device_property_final_filemarks;
175 static DevicePropertyBase device_property_read_buffer_size; /* old name for READ_BLOCK_SIZE */
177 /* here are local prototypes */
178 static void tape_device_init (TapeDevice * o);
179 static void tape_device_class_init (TapeDeviceClass * c);
180 static void tape_device_base_init (TapeDeviceClass * c);
181 static gboolean tape_device_set_feature_property_fn(Device *p_self, DevicePropertyBase *base,
182 GValue *val, PropertySurety surety, PropertySource source);
183 static gboolean tape_device_set_final_filemarks_fn(Device *p_self, DevicePropertyBase *base,
184 GValue *val, PropertySurety surety, PropertySource source);
185 static gboolean tape_device_set_compression_fn(Device *p_self, DevicePropertyBase *base,
186 GValue *val, PropertySurety surety, PropertySource source);
187 static gboolean tape_device_get_read_block_size_fn(Device *p_self, DevicePropertyBase *base,
188 GValue *val, PropertySurety *surety, PropertySource *source);
189 static gboolean tape_device_set_read_block_size_fn(Device *p_self, DevicePropertyBase *base,
190 GValue *val, PropertySurety surety, PropertySource source);
191 static void tape_device_open_device (Device * self, char * device_name, char * device_type, char * device_node);
192 static Device * tape_device_factory (char * device_name, char * device_type, char * device_node);
193 static DeviceStatusFlags tape_device_read_label(Device * self);
194 static gboolean tape_device_write_block(Device * self, guint size, gpointer data);
195 static int tape_device_read_block(Device * self, gpointer buf,
197 static gboolean tape_device_start (Device * self, DeviceAccessMode mode,
198 char * label, char * timestamp);
199 static gboolean tape_device_start_file (Device * self, dumpfile_t * ji);
200 static gboolean tape_device_finish_file (Device * self);
201 static dumpfile_t * tape_device_seek_file (Device * self, guint file);
202 static gboolean tape_device_seek_block (Device * self, guint64 block);
203 static gboolean tape_device_eject (Device * self);
204 static gboolean tape_device_finish (Device * self);
205 static IoResult tape_device_robust_read (TapeDevice * self, void * buf,
206 int * count, char **errmsg);
207 static IoResult tape_device_robust_write (TapeDevice * self, void * buf, int count, char **errmsg);
208 static gboolean tape_device_fsf (TapeDevice * self, guint count);
209 static gboolean tape_device_fsr (TapeDevice * self, guint count);
210 static gboolean tape_device_bsr (TapeDevice * self, guint count, guint file, guint block);
211 static gboolean tape_device_eod (TapeDevice * self);
213 /* pointer to the class of our parent */
214 static DeviceClass *parent_class = NULL;
216 GType tape_device_get_type (void)
218 static GType type = 0;
220 if G_UNLIKELY(type == 0) {
221 static const GTypeInfo info = {
222 sizeof (TapeDeviceClass),
223 (GBaseInitFunc) tape_device_base_init,
224 (GBaseFinalizeFunc) NULL,
225 (GClassInitFunc) tape_device_class_init,
226 (GClassFinalizeFunc) NULL,
227 NULL /* class_data */,
230 (GInstanceInitFunc) tape_device_init,
234 type = g_type_register_static (TYPE_DEVICE, "TapeDevice",
235 &info, (GTypeFlags)0);
242 tape_device_init (TapeDevice * self) {
246 d_self = DEVICE(self);
247 bzero(&response, sizeof(response));
249 self->private = g_new0(TapeDevicePrivate, 1);
251 /* Clear all fields. */
252 d_self->block_size = 32768;
253 d_self->min_block_size = 32768;
254 d_self->max_block_size = LARGEST_BLOCK_ESTIMATE;
255 self->broken_gmt_online = FALSE;
259 /* set all of the feature properties to an unsure default of FALSE */
260 self->broken_gmt_online = FALSE;
267 self->bsf_after_eom = FALSE;
269 g_value_init(&response, G_TYPE_BOOLEAN);
270 g_value_set_boolean(&response, FALSE);
271 device_set_simple_property(d_self, PROPERTY_BROKEN_GMT_ONLINE,
272 &response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
273 device_set_simple_property(d_self, PROPERTY_FSF,
274 &response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
275 device_set_simple_property(d_self, PROPERTY_FSF_AFTER_FILEMARK,
276 &response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
277 device_set_simple_property(d_self, PROPERTY_BSF,
278 &response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
279 device_set_simple_property(d_self, PROPERTY_FSR,
280 &response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
281 device_set_simple_property(d_self, PROPERTY_BSR,
282 &response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
283 device_set_simple_property(d_self, PROPERTY_EOM,
284 &response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
285 device_set_simple_property(d_self, PROPERTY_LEOM,
286 &response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
287 device_set_simple_property(d_self, PROPERTY_BSF_AFTER_EOM,
288 &response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
290 #ifdef DEFAULT_TAPE_NON_BLOCKING_OPEN
291 self->nonblocking_open = TRUE;
293 self->nonblocking_open = FALSE;
295 g_value_set_boolean(&response, self->nonblocking_open);
296 device_set_simple_property(d_self, PROPERTY_NONBLOCKING_OPEN,
297 &response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
298 g_value_unset(&response);
300 self->final_filemarks = 2;
301 g_value_init(&response, G_TYPE_UINT);
302 g_value_set_uint(&response, self->final_filemarks);
303 device_set_simple_property(d_self, PROPERTY_FINAL_FILEMARKS,
304 &response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
305 g_value_unset(&response);
307 self->private->read_block_size = 0;
308 g_value_init(&response, G_TYPE_UINT);
309 g_value_set_uint(&response, self->private->read_block_size);
310 device_set_simple_property(d_self, PROPERTY_READ_BLOCK_SIZE,
311 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DEFAULT);
312 g_value_unset(&response);
314 self->private->write_count = 0;
315 self->private->device_filename = NULL;
317 /* Static properites */
318 g_value_init(&response, CONCURRENCY_PARADIGM_TYPE);
319 g_value_set_enum(&response, CONCURRENCY_PARADIGM_EXCLUSIVE);
320 device_set_simple_property(d_self, PROPERTY_CONCURRENCY,
321 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
322 g_value_unset(&response);
324 g_value_init(&response, STREAMING_REQUIREMENT_TYPE);
325 g_value_set_enum(&response, STREAMING_REQUIREMENT_DESIRED);
326 device_set_simple_property(d_self, PROPERTY_STREAMING,
327 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
328 g_value_unset(&response);
330 g_value_init(&response, G_TYPE_BOOLEAN);
331 g_value_set_boolean(&response, TRUE);
332 device_set_simple_property(d_self, PROPERTY_APPENDABLE,
333 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
334 g_value_unset(&response);
336 g_value_init(&response, G_TYPE_BOOLEAN);
337 g_value_set_boolean(&response, FALSE);
338 device_set_simple_property(d_self, PROPERTY_PARTIAL_DELETION,
339 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
340 g_value_unset(&response);
342 g_value_init(&response, G_TYPE_BOOLEAN);
343 g_value_set_boolean(&response, FALSE);
344 device_set_simple_property(d_self, PROPERTY_FULL_DELETION,
345 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
346 g_value_unset(&response);
348 g_value_init(&response, MEDIA_ACCESS_MODE_TYPE);
349 g_value_set_enum(&response, MEDIA_ACCESS_MODE_READ_WRITE);
350 device_set_simple_property(d_self, PROPERTY_MEDIUM_ACCESS_TYPE,
351 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
352 g_value_unset(&response);
355 static void tape_device_finalize(GObject * obj_self) {
356 TapeDevice * self = TAPE_DEVICE(obj_self);
358 if(G_OBJECT_CLASS(parent_class)->finalize) \
359 (* G_OBJECT_CLASS(parent_class)->finalize)(obj_self);
361 robust_close(self->fd);
363 amfree(self->private->device_filename);
364 amfree(self->private);
368 tape_device_class_init (TapeDeviceClass * c)
370 DeviceClass *device_class = (DeviceClass *)c;
371 GObjectClass *g_object_class = (GObjectClass *)c;
373 parent_class = g_type_class_ref (TYPE_DEVICE);
375 device_class->open_device = tape_device_open_device;
376 device_class->read_label = tape_device_read_label;
377 device_class->write_block = tape_device_write_block;
378 device_class->read_block = tape_device_read_block;
379 device_class->start = tape_device_start;
380 device_class->start_file = tape_device_start_file;
381 device_class->finish_file = tape_device_finish_file;
382 device_class->seek_file = tape_device_seek_file;
383 device_class->seek_block = tape_device_seek_block;
384 device_class->eject = tape_device_eject;
385 device_class->finish = tape_device_finish;
387 g_object_class->finalize = tape_device_finalize;
391 tape_device_base_init (TapeDeviceClass * c)
393 DeviceClass *device_class = (DeviceClass *)c;
395 device_class_register_property(device_class, PROPERTY_BROKEN_GMT_ONLINE,
396 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
397 device_simple_property_get_fn,
398 tape_device_set_feature_property_fn);
400 device_class_register_property(device_class, PROPERTY_FSF,
401 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
402 device_simple_property_get_fn,
403 tape_device_set_feature_property_fn);
405 device_class_register_property(device_class, PROPERTY_FSF_AFTER_FILEMARK,
406 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
407 device_simple_property_get_fn,
408 tape_device_set_feature_property_fn);
410 device_class_register_property(device_class, PROPERTY_BSF,
411 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
412 device_simple_property_get_fn,
413 tape_device_set_feature_property_fn);
415 device_class_register_property(device_class, PROPERTY_FSR,
416 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
417 device_simple_property_get_fn,
418 tape_device_set_feature_property_fn);
420 device_class_register_property(device_class, PROPERTY_BSR,
421 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
422 device_simple_property_get_fn,
423 tape_device_set_feature_property_fn);
425 device_class_register_property(device_class, PROPERTY_EOM,
426 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
427 device_simple_property_get_fn,
428 tape_device_set_feature_property_fn);
430 device_class_register_property(device_class, PROPERTY_BSF_AFTER_EOM,
431 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
432 device_simple_property_get_fn,
433 tape_device_set_feature_property_fn);
435 device_class_register_property(device_class, PROPERTY_NONBLOCKING_OPEN,
436 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
437 device_simple_property_get_fn,
438 tape_device_set_feature_property_fn);
440 device_class_register_property(device_class, PROPERTY_FINAL_FILEMARKS,
441 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
442 device_simple_property_get_fn,
443 tape_device_set_final_filemarks_fn);
445 /* We don't (yet?) support reading the device's compression state, so not
447 device_class_register_property(device_class, PROPERTY_COMPRESSION,
448 PROPERTY_ACCESS_SET_MASK,
450 tape_device_set_compression_fn);
452 device_class_register_property(device_class, PROPERTY_READ_BLOCK_SIZE,
453 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
454 tape_device_get_read_block_size_fn,
455 tape_device_set_read_block_size_fn);
457 device_class_register_property(device_class, device_property_read_buffer_size.ID,
458 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
459 tape_device_get_read_block_size_fn,
460 tape_device_set_read_block_size_fn);
462 /* add the ability to set LEOM to FALSE, for testing purposes */
463 device_class_register_property(device_class, PROPERTY_LEOM,
464 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
465 device_simple_property_get_fn,
466 tape_device_set_feature_property_fn);
470 tape_device_set_feature_property_fn(Device *p_self, DevicePropertyBase *base,
471 GValue *val, PropertySurety surety, PropertySource source)
473 TapeDevice *self = TAPE_DEVICE(p_self);
475 gboolean old_bool, new_bool;
476 PropertySurety old_surety;
477 PropertySource old_source;
479 new_bool = g_value_get_boolean(val);
481 /* get the old source and surety and see if we're willing to make this change */
482 bzero(&old_val, sizeof(old_val));
483 if (device_get_simple_property(p_self, base->ID, &old_val, &old_surety, &old_source)) {
484 old_bool = g_value_get_boolean(&old_val);
486 if (old_surety == PROPERTY_SURETY_GOOD && old_source == PROPERTY_SOURCE_DETECTED) {
487 if (new_bool != old_bool) {
488 device_set_error(p_self, vstrallocf(_(
489 "Value for property '%s' was autodetected and cannot be changed"),
491 DEVICE_STATUS_DEVICE_ERROR);
494 /* pretend we set it, but don't change surety/source */
500 /* (note: PROPERTY_* are not constants, so we can't use switch) */
501 if (base->ID == PROPERTY_BROKEN_GMT_ONLINE)
502 self->broken_gmt_online = new_bool;
503 else if (base->ID == PROPERTY_FSF)
504 self->fsf = new_bool;
505 else if (base->ID == PROPERTY_FSF_AFTER_FILEMARK)
506 self->fsf_after_filemark = new_bool;
507 else if (base->ID == PROPERTY_BSF)
508 self->bsf = new_bool;
509 else if (base->ID == PROPERTY_FSR)
510 self->fsr = new_bool;
511 else if (base->ID == PROPERTY_BSR)
512 self->bsr = new_bool;
513 else if (base->ID == PROPERTY_EOM)
514 self->eom = new_bool;
515 else if (base->ID == PROPERTY_BSF_AFTER_EOM)
516 self->bsf_after_eom = new_bool;
517 else if (base->ID == PROPERTY_NONBLOCKING_OPEN)
518 self->nonblocking_open = new_bool;
519 else if (base->ID == PROPERTY_LEOM)
520 self->leom = new_bool;
522 return FALSE; /* shouldn't happen */
524 return device_simple_property_set_fn(p_self, base, val, surety, source);
528 tape_device_set_final_filemarks_fn(Device *p_self, DevicePropertyBase *base,
529 GValue *val, PropertySurety surety, PropertySource source)
531 TapeDevice *self = TAPE_DEVICE(p_self);
533 gboolean old_int, new_int;
534 PropertySurety old_surety;
535 PropertySource old_source;
537 new_int = g_value_get_uint(val);
539 /* get the old source and surety and see if we're willing to make this change */
540 bzero(&old_val, sizeof(old_val));
541 if (device_get_simple_property(p_self, base->ID, &old_val, &old_surety, &old_source)) {
542 old_int = g_value_get_uint(&old_val);
544 if (old_surety == PROPERTY_SURETY_GOOD && old_source == PROPERTY_SOURCE_DETECTED) {
545 if (new_int != old_int) {
546 device_set_error(p_self, vstrallocf(_(
547 "Value for property '%s' was autodetected and cannot be changed"),
549 DEVICE_STATUS_DEVICE_ERROR);
552 /* pretend we set it, but don't change surety/source */
558 self->final_filemarks = new_int;
560 return device_simple_property_set_fn(p_self, base, val, surety, source);
564 tape_device_set_compression_fn(Device *p_self, DevicePropertyBase *base,
565 GValue *val, PropertySurety surety, PropertySource source)
567 TapeDevice *self = TAPE_DEVICE(p_self);
568 gboolean request = g_value_get_boolean(val);
570 /* We allow this property to be set at any time. This is mostly
571 * because setting compression is a hit-and-miss proposition
572 * at any time; some drives accept the mode setting but don't
573 * actually support compression, while others do support
574 * compression but do it via density settings or some other
575 * way. Set this property whenever you want, but all we'll do
576 * is report whether or not the ioctl succeeded. */
577 if (tape_setcompression(self->fd, request)) {
578 /* looks good .. let's start the device over, though */
579 device_clear_volume_details(p_self);
581 device_set_error(p_self,
582 g_strdup("Error setting COMPRESION property"),
583 DEVICE_STATUS_DEVICE_ERROR);
587 return device_simple_property_set_fn(p_self, base, val, surety, source);
591 tape_device_get_read_block_size_fn(Device *p_self, DevicePropertyBase *base G_GNUC_UNUSED,
592 GValue *val, PropertySurety *surety, PropertySource *source)
594 /* use the READ_BLOCK_SIZE, even if we're invoked to get the old READ_BUFFER_SIZE */
595 return device_simple_property_get_fn(p_self, &device_property_read_block_size,
596 val, surety, source);
600 tape_device_set_read_block_size_fn(Device *p_self, DevicePropertyBase *base G_GNUC_UNUSED,
601 GValue *val, PropertySurety surety, PropertySource source)
603 TapeDevice *self = TAPE_DEVICE(p_self);
604 guint read_block_size = g_value_get_uint(val);
606 if (read_block_size != 0 &&
607 ((gsize)read_block_size < p_self->block_size ||
608 (gsize)read_block_size > p_self->max_block_size))
609 device_set_error(p_self,
610 g_strdup_printf("Error setting READ-BLOCk-SIZE property to '%u', it must be between %zu and %zu", read_block_size, p_self->block_size, p_self->max_block_size),
611 DEVICE_STATUS_DEVICE_ERROR);
614 self->private->read_block_size = read_block_size;
616 /* use the READ_BLOCK_SIZE, even if we're invoked to get the old READ_BUFFER_SIZE */
617 return device_simple_property_set_fn(p_self, &device_property_read_block_size,
618 val, surety, source);
621 void tape_device_register(void) {
622 static const char * device_prefix_list[] = { "tape", NULL };
624 /* First register tape-specific properties */
625 device_property_fill_and_register(&device_property_broken_gmt_online,
626 G_TYPE_BOOLEAN, "broken_gmt_online",
627 "Does this drive support the GMT_ONLINE macro?");
629 device_property_fill_and_register(&device_property_fsf,
630 G_TYPE_BOOLEAN, "fsf",
631 "Does this drive support the MTFSF command?");
633 device_property_fill_and_register(&device_property_fsf_after_filemark,
634 G_TYPE_BOOLEAN, "fsf_after_filemark",
635 "Does this drive needs a FSF if a filemark is already read?");
637 device_property_fill_and_register(&device_property_bsf,
638 G_TYPE_BOOLEAN, "bsf",
639 "Does this drive support the MTBSF command?" );
641 device_property_fill_and_register(&device_property_fsr,
642 G_TYPE_BOOLEAN, "fsr",
643 "Does this drive support the MTFSR command?");
645 device_property_fill_and_register(&device_property_bsr,
646 G_TYPE_BOOLEAN, "bsr",
647 "Does this drive support the MTBSR command?");
649 device_property_fill_and_register(&device_property_eom,
650 G_TYPE_BOOLEAN, "eom",
651 "Does this drive support the MTEOM command?");
653 device_property_fill_and_register(&device_property_bsf_after_eom,
656 "Does this drive require an MTBSF after MTEOM in order to append?" );
658 device_property_fill_and_register(&device_property_nonblocking_open,
661 "Does this drive require a open with O_NONBLOCK?" );
663 device_property_fill_and_register(&device_property_final_filemarks,
664 G_TYPE_UINT, "final_filemarks",
665 "How many filemarks to write after the last tape file?" );
667 device_property_fill_and_register(&device_property_read_buffer_size,
668 G_TYPE_UINT, "read_buffer_size",
669 "(deprecated name for READ_BLOCK_SIZE)");
671 /* Then the device itself */
672 register_device(tape_device_factory, device_prefix_list);
675 /* Open the tape device, trying various combinations of O_RDWR and
676 O_NONBLOCK. Returns -1 and calls device_set_error for errors
677 On Linux, with O_NONBLOCK, the kernel just checks the state once,
678 whereas it checks it every second for ST_BLOCK_SECONDS if O_NONBLOCK is
679 not given. Amanda already have the code to poll, we want open to check
680 the state only once. */
682 static int try_open_tape_device(TapeDevice * self, char * device_filename) {
685 DeviceStatusFlags new_status;
690 if (self->nonblocking_open) {
691 nonblocking = O_NONBLOCK;
696 fd = robust_open(device_filename, O_RDWR | nonblocking, 0);
698 if (fd < 0 && nonblocking && (save_errno == EWOULDBLOCK || save_errno == EINVAL)) {
699 /* Maybe we don't support O_NONBLOCK for tape devices. */
700 fd = robust_open(device_filename, O_RDWR, 0);
704 fd = robust_open(device_filename, O_RDWR, 0);
708 self->write_open_errno = 0;
710 if (errno == EACCES || errno == EPERM
715 /* Device is write-protected. */
716 self->write_open_errno = errno;
718 fd = robust_open(device_filename, O_RDONLY | nonblocking, 0);
720 if (fd < 0 && nonblocking && (save_errno == EWOULDBLOCK || save_errno == EINVAL)) {
721 fd = robust_open(device_filename, O_RDONLY, 0);
725 fd = robust_open(device_filename, O_RDONLY, 0);
731 /* Clear O_NONBLOCK for operations from now on. */
732 if (fd >= 0 && nonblocking)
733 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) & ~O_NONBLOCK);
735 /* function continues after #endif */
737 #endif /* O_NONBLOCK */
740 DeviceStatusFlags status_flag = 0;
742 status_flag = DEVICE_STATUS_DEVICE_BUSY;
744 status_flag = DEVICE_STATUS_DEVICE_ERROR;
745 device_set_error(DEVICE(self),
746 vstrallocf(_("Can't open tape device %s: %s"), self->private->device_filename, strerror(errno)),
751 /* Check that this is actually a tape device. */
752 new_status = tape_is_tape_device(fd);
753 if (new_status & DEVICE_STATUS_DEVICE_ERROR) {
754 device_set_error(DEVICE(self),
755 vstrallocf(_("File %s is not a tape device"), self->private->device_filename),
760 if (new_status & DEVICE_STATUS_VOLUME_MISSING) {
761 device_set_error(DEVICE(self),
762 vstrallocf(_("Tape device %s is not ready or is empty"), self->private->device_filename),
768 new_status = tape_is_ready(fd, self);
769 if (new_status & DEVICE_STATUS_VOLUME_MISSING) {
770 device_set_error(DEVICE(self),
771 vstrallocf(_("Tape device %s is empty"), self->private->device_filename),
776 if (new_status != DEVICE_STATUS_SUCCESS) {
777 device_set_error(DEVICE(self),
778 vstrallocf(_("Tape device %s is not ready or is empty"), self->private->device_filename),
788 tape_device_open_device (Device * dself, char * device_name,
789 char * device_type, char * device_node) {
793 self = TAPE_DEVICE(dself);
796 self->private->device_filename = stralloc(device_node);
798 /* Set tape drive/OS info */
799 bzero(&val, sizeof(val));
800 g_value_init(&val, G_TYPE_BOOLEAN);
803 g_value_set_boolean(&val, self->fsf);
804 device_set_simple_property(dself, PROPERTY_FSF, &val, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
806 self->fsf_after_filemark = DEFAULT_FSF_AFTER_FILEMARK;
807 g_value_set_boolean(&val, self->fsf_after_filemark);
808 device_set_simple_property(dself, PROPERTY_FSF_AFTER_FILEMARK, &val, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
811 g_value_set_boolean(&val, self->bsf);
812 device_set_simple_property(dself, PROPERTY_BSF, &val, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
815 g_value_set_boolean(&val, self->fsr);
816 device_set_simple_property(dself, PROPERTY_FSR, &val, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
819 g_value_set_boolean(&val, self->bsr);
820 device_set_simple_property(dself, PROPERTY_BSR, &val, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
823 g_value_set_boolean(&val, self->eom);
824 device_set_simple_property(dself, PROPERTY_EOM, &val, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
827 g_value_set_boolean(&val, self->leom);
828 device_set_simple_property(dself, PROPERTY_LEOM, &val, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
830 self->bsf_after_eom = FALSE;
831 g_value_set_boolean(&val, self->bsf_after_eom);
832 device_set_simple_property(dself, PROPERTY_BSF_AFTER_EOM, &val, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
835 g_value_init(&val, G_TYPE_UINT);
837 self->final_filemarks = 2;
838 g_value_set_uint(&val, self->final_filemarks);
839 device_set_simple_property(dself, PROPERTY_FINAL_FILEMARKS, &val, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
844 if (parent_class->open_device) {
845 parent_class->open_device(dself, device_name, device_type, device_node);
850 static DeviceStatusFlags tape_device_read_label(Device * dself) {
852 char * header_buffer;
856 DeviceStatusFlags new_status;
859 self = TAPE_DEVICE(dself);
861 amfree(dself->volume_label);
862 amfree(dself->volume_time);
863 dumpfile_free(dself->volume_header);
864 dself->volume_header = NULL;
866 if (device_in_error(self)) return dself->status;
868 if (self->fd == -1) {
869 self->fd = try_open_tape_device(self, self->private->device_filename);
870 /* if the open failed, then try_open_tape_device already set the
871 * approppriate error status */
873 return dself->status;
877 if (!tape_rewind(self->fd)) {
878 device_set_error(dself,
879 vstrallocf(_("Error rewinding device %s to read label: %s"),
880 self->private->device_filename, strerror(errno)),
881 DEVICE_STATUS_DEVICE_ERROR
882 | DEVICE_STATUS_VOLUME_ERROR);
883 return dself->status;
886 buffer_len = tape_device_read_size(self);
887 header_buffer = malloc(buffer_len);
888 result = tape_device_robust_read(self, header_buffer, &buffer_len, &msg);
890 if (result != RESULT_SUCCESS) {
892 tape_rewind(self->fd);
896 msg = stralloc(_("no data"));
897 new_status = (DEVICE_STATUS_VOLUME_ERROR |
898 DEVICE_STATUS_VOLUME_UNLABELED);
899 header = dself->volume_header = g_new(dumpfile_t, 1);
903 case RESULT_SMALL_BUFFER:
904 msg = stralloc(_("block size too small"));
905 new_status = (DEVICE_STATUS_DEVICE_ERROR |
906 DEVICE_STATUS_VOLUME_ERROR);
907 header = dself->volume_header = g_new(dumpfile_t, 1);
909 header->type = F_WEIRD;
913 msg = stralloc(_("unknown error"));
915 new_status = (DEVICE_STATUS_DEVICE_ERROR |
916 DEVICE_STATUS_VOLUME_ERROR |
917 DEVICE_STATUS_VOLUME_UNLABELED);
920 device_set_error(dself,
921 g_strdup_printf(_("Error reading Amanda header: %s"),
922 msg? msg : _("unknown error")),
925 return dself->status;
928 dself->header_block_size = buffer_len;
929 header = dself->volume_header = g_new(dumpfile_t, 1);
932 parse_file_header(header_buffer, header, buffer_len);
933 amfree(header_buffer);
934 if (header->type != F_TAPESTART) {
935 device_set_error(dself,
936 stralloc(_("No tapestart header -- unlabeled device?")),
937 DEVICE_STATUS_VOLUME_UNLABELED);
938 return dself->status;
941 dself->volume_label = g_strdup(header->name);
942 dself->volume_time = g_strdup(header->datestamp);
943 /* dself->volume_header is already set */
945 device_set_error(dself, NULL, DEVICE_STATUS_SUCCESS);
947 return dself->status;
951 tape_device_write_block(Device * pself, guint size, gpointer data) {
953 char *replacement_buffer = NULL;
957 self = TAPE_DEVICE(pself);
959 g_assert(self->fd >= 0);
960 if (device_in_error(self)) return FALSE;
962 /* zero out to the end of a short block -- tape devices only write
964 if (size < pself->block_size) {
965 replacement_buffer = malloc(pself->block_size);
966 memcpy(replacement_buffer, data, size);
967 bzero(replacement_buffer+size, pself->block_size-size);
969 data = replacement_buffer;
970 size = pself->block_size;
973 result = tape_device_robust_write(self, data, size, &msg);
974 amfree(replacement_buffer);
980 case RESULT_NO_SPACE:
981 device_set_error(pself,
982 stralloc(_("No space left on device")),
983 DEVICE_STATUS_VOLUME_ERROR);
984 pself->is_eom = TRUE;
988 msg = stralloc(_("unknown error"));
990 device_set_error(pself,
991 g_strdup_printf(_("Error writing block: %s"), msg),
992 DEVICE_STATUS_DEVICE_ERROR);
1002 static int tape_device_read_block (Device * pself, gpointer buf,
1007 gssize read_block_size = tape_device_read_size(pself);
1010 self = TAPE_DEVICE(pself);
1012 g_assert(self->fd >= 0);
1013 if (device_in_error(self)) return -1;
1015 g_assert(read_block_size < INT_MAX); /* data type mismatch */
1016 if (buf == NULL || *size_req < (int)read_block_size) {
1017 /* Just a size query. */
1018 *size_req = (int)read_block_size;
1023 result = tape_device_robust_read(self, buf, &size, &msg);
1025 case RESULT_SUCCESS:
1029 case RESULT_SMALL_BUFFER: {
1033 /* If this happens, it means that we have:
1034 * (next block size) > (buffer size) >= (read_block_size)
1035 * The solution is to ask for an even bigger buffer. We also play
1036 * some games to refrain from reading above the SCSI limit or from
1037 * integer overflow. Note that not all devices will tell us about
1038 * this problem -- some will just discard the "extra" data. */
1039 new_size = MIN(INT_MAX/2 - 1, *size_req) * 2;
1040 if (new_size > LARGEST_BLOCK_ESTIMATE &&
1041 *size_req < LARGEST_BLOCK_ESTIMATE) {
1042 new_size = LARGEST_BLOCK_ESTIMATE;
1044 g_assert (new_size > (gsize)*size_req);
1046 g_info("Device %s indicated blocksize %zd was too small; using %zd.",
1047 pself->device_name, (gsize)*size_req, new_size);
1048 *size_req = (int)new_size;
1049 self->private->read_block_size = new_size;
1051 bzero(&newval, sizeof(newval));
1052 g_value_init(&newval, G_TYPE_UINT);
1053 g_value_set_uint(&newval, self->private->read_block_size);
1054 device_set_simple_property(pself, PROPERTY_READ_BLOCK_SIZE,
1055 &newval, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
1056 g_value_unset(&newval);
1060 case RESULT_NO_DATA:
1061 pself->is_eof = TRUE;
1062 pself->in_file = FALSE;
1063 device_set_error(pself,
1065 DEVICE_STATUS_SUCCESS);
1069 msg = stralloc(_("unknown error"));
1071 device_set_error(pself,
1072 vstrallocf(_("Error reading from tape device: %s"), msg),
1073 DEVICE_STATUS_VOLUME_ERROR | DEVICE_STATUS_DEVICE_ERROR);
1078 g_assert_not_reached();
1081 /* Just a helper function for tape_device_start(). */
1082 static gboolean write_tapestart_header(TapeDevice * self, char * label,
1085 dumpfile_t * header;
1087 Device * d_self = (Device*)self;
1090 tape_rewind(self->fd);
1092 header = make_tapestart_header(d_self, label, timestamp);
1093 g_assert(header != NULL);
1094 header_buf = device_build_amanda_header(d_self, header, NULL);
1095 if (header_buf == NULL) {
1096 device_set_error(d_self,
1097 stralloc(_("Tapestart header won't fit in a single block!")),
1098 DEVICE_STATUS_DEVICE_ERROR);
1099 dumpfile_free(header);
1102 dumpfile_free(d_self->volume_header);
1103 d_self->volume_header = NULL;
1105 result = tape_device_robust_write(self, header_buf, d_self->block_size, &msg);
1106 if (result != RESULT_SUCCESS) {
1107 device_set_error(d_self,
1108 g_strdup_printf(_("Error writing tapestart header: %s"),
1109 (result == RESULT_ERROR)? msg : _("out of space")),
1110 DEVICE_STATUS_DEVICE_ERROR);
1112 if (result == RESULT_NO_SPACE)
1113 d_self->is_eom = TRUE;
1116 dumpfile_free(header);
1121 d_self->header_block_size = d_self->block_size;
1124 if (!tape_weof(self->fd, 1)) {
1125 device_set_error(d_self,
1126 vstrallocf(_("Error writing filemark: %s"),
1128 DEVICE_STATUS_DEVICE_ERROR|DEVICE_STATUS_VOLUME_ERROR);
1129 /* can't tell if this was EOM or not, so assume it is */
1130 d_self->is_eom = TRUE;
1131 dumpfile_free(header);
1135 d_self->volume_header = header;
1140 tape_device_start (Device * d_self, DeviceAccessMode mode, char * label,
1144 self = TAPE_DEVICE(d_self);
1146 if (device_in_error(self)) return FALSE;
1148 if (self->fd == -1) {
1149 self->fd = try_open_tape_device(self, self->private->device_filename);
1150 /* if the open failed, then try_open_tape_device already set the
1151 * approppriate error status */
1156 if (mode != ACCESS_WRITE && d_self->volume_label == NULL) {
1157 /* we need a labeled volume for APPEND and READ */
1158 if (tape_device_read_label(d_self) != DEVICE_STATUS_SUCCESS)
1162 d_self->access_mode = mode;
1163 d_self->in_file = FALSE;
1165 if (IS_WRITABLE_ACCESS_MODE(mode)) {
1166 if (self->write_open_errno != 0) {
1167 /* We tried and failed to open the device in write mode. */
1168 device_set_error(d_self,
1169 vstrallocf(_("Can't open tape device %s for writing: %s"),
1170 self->private->device_filename, strerror(self->write_open_errno)),
1171 DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
1173 } else if (!tape_rewind(self->fd)) {
1174 device_set_error(d_self,
1175 vstrallocf(_("Error rewinding device to start: %s"), strerror(errno)),
1176 DEVICE_STATUS_DEVICE_ERROR);
1181 /* Position the tape */
1184 if (d_self->volume_label == NULL && device_read_label(d_self) != DEVICE_STATUS_SUCCESS) {
1185 /* device_read_label already set our error message */
1189 if (!tape_device_eod(self)) {
1190 device_set_error(d_self,
1191 vstrallocf(_("Couldn't seek to end of tape: %s"), strerror(errno)),
1192 DEVICE_STATUS_DEVICE_ERROR);
1198 if (d_self->volume_label == NULL && device_read_label(d_self) != DEVICE_STATUS_SUCCESS) {
1199 /* device_read_label already set our error message */
1203 if (!tape_rewind(self->fd)) {
1204 device_set_error(d_self,
1205 vstrallocf(_("Error rewinding device after reading label: %s"), strerror(errno)),
1206 DEVICE_STATUS_DEVICE_ERROR);
1213 if (!write_tapestart_header(self, label, timestamp)) {
1214 /* write_tapestart_header already set the error status */
1218 d_self->volume_label = newstralloc(d_self->volume_label, label);
1219 d_self->volume_time = newstralloc(d_self->volume_time, timestamp);
1221 /* unset the VOLUME_UNLABELED flag, if it was set */
1222 device_set_error(d_self, NULL, DEVICE_STATUS_SUCCESS);
1227 g_assert_not_reached();
1233 static gboolean tape_device_start_file(Device * d_self,
1234 dumpfile_t * info) {
1237 char * amanda_header;
1240 self = TAPE_DEVICE(d_self);
1242 g_assert(self->fd >= 0);
1243 if (device_in_error(self)) return FALSE;
1245 /* set the blocksize in the header properly */
1246 info->blocksize = d_self->block_size;
1248 /* Make the Amanda header suitable for writing to the device. */
1249 /* Then write the damn thing. */
1250 amanda_header = device_build_amanda_header(d_self, info, NULL);
1251 if (amanda_header == NULL) {
1252 device_set_error(d_self,
1253 stralloc(_("Amanda file header won't fit in a single block!")),
1254 DEVICE_STATUS_DEVICE_ERROR);
1258 result = tape_device_robust_write(self, amanda_header, d_self->block_size, &msg);
1259 if (result != RESULT_SUCCESS) {
1260 device_set_error(d_self,
1261 vstrallocf(_("Error writing file header: %s"),
1262 (result == RESULT_ERROR)? msg : _("out of space")),
1263 DEVICE_STATUS_DEVICE_ERROR);
1265 if (result == RESULT_NO_SPACE)
1266 d_self->is_eom = TRUE;
1268 amfree(amanda_header);
1272 amfree(amanda_header);
1274 /* arrange the file numbers correctly */
1275 d_self->in_file = TRUE;
1277 if (d_self->file >= 0)
1283 tape_device_finish_file (Device * d_self) {
1286 self = TAPE_DEVICE(d_self);
1287 if (device_in_error(d_self)) return FALSE;
1289 if (!tape_weof(self->fd, 1)) {
1290 device_set_error(d_self,
1291 vstrallocf(_("Error writing filemark: %s"), strerror(errno)),
1292 DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
1293 /* can't tell if this was EOM or not, so assume it is */
1294 d_self->is_eom = TRUE;
1298 d_self->in_file = FALSE;
1303 tape_device_seek_file (Device * d_self, guint file) {
1307 char * header_buffer;
1313 self = TAPE_DEVICE(d_self);
1315 if (device_in_error(self)) return NULL;
1317 difference = file - d_self->file;
1319 /* Check if we already read a filemark. */
1320 /* If we already read a filemark and the drive automaticaly goes to the
1321 next file, then we must reduce the difference by one. */
1322 if (d_self->is_eof && !self->fsf_after_filemark) {
1326 d_self->in_file = FALSE;
1327 d_self->is_eof = FALSE;
1331 if (difference > 0) {
1332 /* Seeking forwards */
1333 if (!tape_device_fsf(self, difference)) {
1334 tape_rewind(self->fd);
1335 device_set_error(d_self,
1336 vstrallocf(_("Could not seek forward to file %d"), file),
1337 DEVICE_STATUS_VOLUME_ERROR | DEVICE_STATUS_DEVICE_ERROR);
1340 } else { /* (difference <= 0) */
1341 /* Seeking backwards, or to this file itself */
1343 /* if the drive supports bsf, we can do this the fancy way */
1345 /* bsf one more than the difference */
1346 if (!tape_bsf(self->fd, -difference + 1)) {
1347 tape_rewind(self->fd);
1348 device_set_error(d_self,
1349 vstrallocf(_("Could not seek backward to file %d"), file),
1350 DEVICE_STATUS_VOLUME_ERROR | DEVICE_STATUS_DEVICE_ERROR);
1354 /* now we are on the BOT side of the desired filemark, so FSF to get to the
1356 if (!tape_device_fsf(self, 1)) {
1357 tape_rewind(self->fd);
1358 device_set_error(d_self,
1359 vstrallocf(_("Could not seek forward to file %d"), file),
1360 DEVICE_STATUS_VOLUME_ERROR | DEVICE_STATUS_DEVICE_ERROR);
1364 /* no BSF, so just rewind and seek forward */
1365 if (!tape_rewind(self->fd)) {
1366 device_set_error(d_self,
1367 vstrallocf(_("Could not rewind device while emulating BSF")),
1368 DEVICE_STATUS_VOLUME_ERROR | DEVICE_STATUS_DEVICE_ERROR);
1372 if (!tape_device_fsf(self, file)) {
1373 tape_rewind(self->fd);
1374 device_set_error(d_self,
1375 vstrallocf(_("Could not seek forward to file %d"), file),
1376 DEVICE_STATUS_VOLUME_ERROR | DEVICE_STATUS_DEVICE_ERROR);
1382 /* double-check that we're on the right fileno, if possible. This is most
1383 * likely a programming error if it occurs, but could also be due to a weird
1384 * tape drive or driver (and that would *never* happen, right?) */
1385 got_file = tape_fileno(self->fd);
1386 if (got_file >= 0 && (guint)got_file != file) {
1387 device_set_error(d_self,
1388 vstrallocf(_("Could not seek to file %d correctly; got %d"),
1390 DEVICE_STATUS_DEVICE_ERROR);
1391 d_self->file = (guint)got_file;
1395 buffer_len = tape_device_read_size(d_self);
1396 header_buffer = malloc(buffer_len);
1397 d_self->is_eof = FALSE;
1398 result = tape_device_robust_read(self, header_buffer, &buffer_len, &msg);
1400 if (result != RESULT_SUCCESS) {
1401 free(header_buffer);
1402 tape_rewind(self->fd);
1404 case RESULT_NO_DATA:
1405 /* If we read 0 bytes, that means we encountered a double
1406 * filemark, which indicates end of tape. This should
1407 * work even with QIC tapes on operating systems with
1408 * proper support. */
1409 d_self->file = file; /* other attributes are already correct */
1410 return make_tapeend_header();
1412 case RESULT_SMALL_BUFFER:
1413 msg = stralloc(_("block size too small"));
1417 msg = stralloc(_("unknown error"));
1421 device_set_error(d_self,
1422 g_strdup_printf(_("Error reading Amanda header: %s"), msg),
1423 DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
1428 rval = g_new(dumpfile_t, 1);
1429 parse_file_header(header_buffer, rval, buffer_len);
1430 amfree(header_buffer);
1431 switch (rval->type) {
1433 case F_CONT_DUMPFILE:
1434 case F_SPLIT_DUMPFILE:
1438 /* a NOOP is written on QIC tapes to avoid writing two sequential
1439 * filemarks when closing a device in WRITE or APPEND mode. In this
1440 * case, we just seek to the next file. */
1447 tape_rewind(self->fd);
1448 device_set_error(d_self,
1449 stralloc(_("Invalid amanda header while reading file header")),
1450 DEVICE_STATUS_VOLUME_ERROR);
1455 d_self->in_file = TRUE;
1456 d_self->file = file;
1462 tape_device_seek_block (Device * d_self, guint64 block) {
1466 self = TAPE_DEVICE(d_self);
1468 if (device_in_error(self)) return FALSE;
1470 difference = block - d_self->block;
1472 if (difference > 0) {
1473 if (!tape_device_fsr(self, difference)) {
1474 device_set_error(d_self,
1475 vstrallocf(_("Could not seek forward to block %ju: %s"), (uintmax_t)block, strerror(errno)),
1476 DEVICE_STATUS_VOLUME_ERROR | DEVICE_STATUS_DEVICE_ERROR);
1479 } else if (difference < 0) {
1480 if (!tape_device_bsr(self, difference, d_self->file, d_self->block)) {
1481 device_set_error(d_self,
1482 vstrallocf(_("Could not seek backward to block %ju: %s"), (uintmax_t)block, strerror(errno)),
1483 DEVICE_STATUS_VOLUME_ERROR | DEVICE_STATUS_DEVICE_ERROR);
1488 d_self->block = block;
1493 tape_device_eject (Device * d_self) {
1496 self = TAPE_DEVICE(d_self);
1498 if (device_in_error(self)) return FALSE;
1500 /* Open the device if not already opened */
1501 if (self->fd == -1) {
1502 self->fd = try_open_tape_device(self, self->private->device_filename);
1503 /* if the open failed, then try_open_tape_device already set the
1504 * approppriate error status */
1510 if (!tape_rewind(self->fd)) {
1511 device_set_error(d_self,
1512 vstrallocf(_("Error rewinding device %s before ejecting: %s"),
1513 self->private->device_filename, strerror(errno)),
1514 DEVICE_STATUS_DEVICE_ERROR
1515 | DEVICE_STATUS_VOLUME_ERROR);
1519 if (tape_offl(self->fd))
1522 device_set_error(d_self,
1523 vstrallocf(_("Error ejecting device %s: %s\n"),
1524 self->private->device_filename, strerror(errno)),
1525 DEVICE_STATUS_DEVICE_ERROR);
1531 tape_device_finish (Device * d_self) {
1535 self = TAPE_DEVICE(d_self);
1537 if (device_in_error(self))
1540 /* if we're already in ACCESS_NULL, then there are no filemarks or anything
1541 * to worry about, but we need to release the kernel device */
1542 if (d_self->access_mode == ACCESS_NULL) {
1543 robust_close(self->fd);
1548 /* Polish off this file, if relevant. */
1549 if (d_self->in_file && IS_WRITABLE_ACCESS_MODE(d_self->access_mode)) {
1550 if (!device_finish_file(d_self))
1554 /* Straighten out the filemarks. We already wrote one in finish_file, and
1555 * the device driver will write another filemark when we rewind. This means
1556 * that, if we do nothing, we'll get two filemarks. If final_filemarks is
1557 * 1, this would be wrong, so in this case we insert a F_NOOP header between
1558 * the two filemarks. */
1559 if (self->final_filemarks == 1 &&
1560 IS_WRITABLE_ACCESS_MODE(d_self->access_mode)) {
1565 /* write a F_NOOP header */
1568 header = device_build_amanda_header(d_self, &file, NULL);
1570 device_set_error(d_self,
1571 stralloc(_("Amanda file header won't fit in a single block!")),
1572 DEVICE_STATUS_DEVICE_ERROR);
1576 result = tape_device_robust_write(self, header, d_self->block_size, &msg);
1577 if (result != RESULT_SUCCESS) {
1578 device_set_error(d_self,
1579 vstrallocf(_("Error writing file header: %s"),
1580 (result == RESULT_ERROR)? msg : _("out of space")),
1581 DEVICE_STATUS_DEVICE_ERROR);
1589 /* Rewind (the kernel will write a filemark first) */
1590 if (!tape_rewind(self->fd)) {
1591 device_set_error(d_self,
1592 vstrallocf(_("Couldn't rewind device to finish: %s"), strerror(errno)),
1593 DEVICE_STATUS_DEVICE_ERROR);
1597 d_self->is_eof = FALSE;
1598 d_self->access_mode = ACCESS_NULL;
1600 /* release the kernel's device */
1601 robust_close(self->fd);
1607 d_self->access_mode = ACCESS_NULL;
1609 /* release the kernel's device */
1610 robust_close(self->fd);
1616 /* Works just like read(), except for the following:
1617 * 1) Retries on EINTR & friends.
1618 * 2) Stores count in parameter, not return value.
1619 * 3) Provides explicit return result.
1620 * *errmsg is only set on RESULT_ERROR.
1623 tape_device_robust_read (TapeDevice * self, void * buf, int * count, char **errmsg) {
1627 d_self = (Device*)self;
1629 /* Callers should ensure this. */
1630 g_assert(*count >= 0);
1633 result = read(self->fd, buf, *count);
1635 /* Success. By definition, we read a full block. */
1636 d_self->is_eof = FALSE;
1638 return RESULT_SUCCESS;
1639 } else if (result == 0) {
1640 d_self->is_eof = TRUE;
1641 return RESULT_NO_DATA;
1648 || errno == EWOULDBLOCK
1654 /* Interrupted system call */
1658 || errno == ENOMEM /* bad user-space buffer */
1661 || errno == EOVERFLOW /* bad kernel-space buffer */
1664 || errno == EINVAL /* ??? */
1667 /* Buffer too small. */
1668 g_warning("Buffer is too small (%d bytes) from %s: %s",
1669 *count, self->private->device_filename, strerror(errno));
1670 return RESULT_SMALL_BUFFER;
1672 *errmsg = g_strdup_printf(_("Error reading %d bytes from %s: %s"),
1673 *count, self->private->device_filename, strerror(errno));
1674 return RESULT_ERROR;
1679 g_assert_not_reached();
1682 /* Kernel workaround: If needed, poke the kernel so it doesn't fail.
1683 at the 2GB boundry. Parameters are G_GNUC_UNUSED in case NEED_RESETOFS
1685 static void check_resetofs(TapeDevice * self G_GNUC_UNUSED,
1686 int count G_GNUC_UNUSED) {
1687 #ifdef NEED_RESETOFS
1691 d_self = (Device*)self;
1693 self->private->write_count += count;
1694 if (self->private->write_count < RESETOFS_THRESHOLD) {
1698 result = lseek(self->fd, 0, SEEK_SET);
1700 g_warning(_("lseek() failed during kernel 2GB workaround: %s"),
1706 /* *errmsg is only set on RESULT_ERROR */
1708 tape_device_robust_write (TapeDevice * self, void * buf, int count, char **errmsg) {
1711 gboolean retry = FALSE;
1713 d_self = (Device*)self;
1715 check_resetofs(self, count);
1718 result = write(self->fd, buf, count);
1721 if (result == count)
1722 return RESULT_SUCCESS;
1725 /* write() returned a short count. This should not happen if the block sizes
1726 * are properly aligned. */
1727 *errmsg = g_strdup_printf("Short write on tape device: Tried %d, got %d. Is "
1728 "the drive using a block size smaller than %d bytes?",
1729 count, result, count);
1730 return RESULT_ERROR;
1733 /* Detect LEOM (early warning) and handle properly
1735 * FreeBSD: 0-length write; next write will succeed
1736 * http://lists.freebsd.org/pipermail/freebsd-scsi/2010-June/004414.html
1738 * Solaris: 0-length write; next write will succeed
1739 * (from Matthew Jacob on FreeBSD thread)
1741 * Linux: -1/ENOSPC; next write will succeed
1742 * http://www.mjmwired.net/kernel/Documentation/scsi/st.txt
1744 * HP/UX: -1/ENOSPC; next write will succeed
1745 * http://www.adssasia.com/Manual/IBM%203581%20tape%20autoloader.pdf
1749 || (result < 0 && errno == ENOSPC)
1752 /* if we've retried once already, then we're probably really out of space */
1754 return RESULT_NO_SPACE;
1756 d_self->is_eom = TRUE;
1758 g_debug("empty write to tape; treating as LEOM early warning and retrying");
1762 /* at this point result < 0, so an error occurred - sort out what */
1769 || errno == EWOULDBLOCK
1775 /* Interrupted system call */
1785 /* Probably EOT. Print a message if we got EIO. */
1788 g_warning(_("Got EIO on %s, assuming end of tape"),
1789 self->private->device_filename);
1792 return RESULT_NO_SPACE;
1795 *errmsg = vstrallocf(_("Kernel gave unexpected write() result of \"%s\" on device %s"),
1796 strerror(errno), self->private->device_filename);
1797 return RESULT_ERROR;
1801 g_assert_not_reached();
1804 /* Reads some number of tape blocks into the bit-bucket. If the count
1805 is negative, then we read the rest of the entire file. Returns the
1806 number of blocks read, or -1 if an error occured. If we encounter
1807 EOF (as opposed to some other error) we return the number of blocks
1809 static int drain_tape_blocks(TapeDevice * self, int count) {
1814 buffer_size = tape_device_read_size(self);
1816 buffer = malloc(buffer_size);
1818 for (i = 0; i < count || count < 0;) {
1821 result = read(self->fd, buffer, buffer_size);
1825 } else if (result == 0) {
1829 /* First check for interrupted system call. */
1835 || errno == EWOULDBLOCK
1841 /* Interrupted system call */
1845 || errno == ENOSPC /* bad user-space buffer */
1848 || errno == EOVERFLOW /* bad kernel-space buffer */
1851 || errno == EINVAL /* ??? */
1854 /* The buffer may not be big enough. But the OS is not
1855 100% clear. We double the buffer and try again, but
1856 in no case allow a buffer bigger than 32 MB. */
1859 if (buffer_size > 32*1024*1024) {
1863 buffer = realloc(buffer, buffer_size);
1875 tape_device_fsf (TapeDevice * self, guint count) {
1877 return tape_fsf(self->fd, count);
1880 for (i = 0; i < count; i ++) {
1881 if (drain_tape_blocks(self, -1) < 0)
1890 tape_device_fsr (TapeDevice * self, guint count) {
1892 return tape_fsr(self->fd, count);
1894 int result = drain_tape_blocks(self, count);
1895 return result > 0 && (int)count == result;
1899 /* Seek back the given number of blocks to block number block within
1900 * the current file, numbered file. */
1903 tape_device_bsr (TapeDevice * self, guint count, guint file, guint block) {
1905 return tape_bsr(self->fd, count);
1906 } else if (self->bsf && self->fsf) {
1907 /* BSF, FSF to the right side of the filemark, and then FSR. */
1908 if (!tape_bsf(self->fd, 1))
1911 if (!tape_fsf(self->fd, 1))
1914 return tape_device_fsr(self, block);
1916 /* rewind, FSF, and FSR */
1917 if (!tape_rewind(self->fd))
1920 if (!tape_device_fsf(self, file))
1923 return tape_device_fsr(self, block);
1925 g_assert_not_reached();
1928 /* Go to the right place to write more data, and update the file
1929 number if possible. */
1931 tape_device_eod (TapeDevice * self) {
1935 d_self = (Device*)self;
1939 result = tape_eod(self->fd);
1940 if (result == TAPE_OP_ERROR) {
1942 } else if (result != TAPE_POSITION_UNKNOWN) {
1943 /* great - we just fast-forwarded to EOD, but don't know where we are, so
1944 * now we have to rewind and drain all of that data. Warn the user so that
1945 * we can skip the fast-forward-rewind stage on the next run */
1946 g_warning("Seek to end of tape does not give an accurate tape position; set "
1947 "the EOM property to 0 to avoid useless tape movement.");
1948 /* and set the property so that next time *this* object is opened for
1949 * append, we skip this stage */
1951 /* fall through to draining blocks, below */
1953 /* We drop by 1 because Device will increment the first
1954 time the user does start_file. */
1955 d_self->file = result - 1;
1960 if (!tape_rewind(self->fd))
1965 /* We alternately read a block and FSF. If the read is
1966 successful, then we are not there yet and should FSF
1969 result = drain_tape_blocks(self, 1);
1971 /* More data, FSF. */
1972 tape_device_fsf(self, 1);
1974 } else if (result == 0) {
1976 d_self->file = count - 1;
1985 tape_device_factory (char * device_name, char * device_type, char * device_node) {
1987 g_assert(0 == strcmp(device_type, "tape"));
1988 rval = DEVICE(g_object_new(TYPE_TAPE_DEVICE, NULL));
1989 device_open_device(rval, device_name, device_type, device_node);
1994 * Tape Operations using the POSIX interface
1997 /* Having one name for every operation would be too easy. */
1998 #if !defined(MTCOMPRESSION) && defined(MTCOMP)
1999 # define MTCOMPRESSION MTCOMP
2002 #if !defined(MTSETBLK) && defined(MTSETBSIZ)
2003 # define MTSETBLK MTSETBSIZ
2006 #if !defined(MTEOM) && defined(MTEOD)
2007 # define MTEOM MTEOD
2010 gboolean tape_rewind(int fd) {
2014 /* We will retry this for up to 30 seconds or 5 retries,
2015 whichever is less, because some hardware/software combinations
2016 (notably EXB-8200 on FreeBSD) can fail to rewind. */
2017 stop_time = time(NULL) + 30;
2019 while (--count >= 0 && time(NULL) < stop_time) {
2024 if (0 == ioctl(fd, MTIOCTOP, &mt))
2033 gboolean tape_fsf(int fd, guint count) {
2036 mt.mt_count = count;
2037 return 0 == ioctl(fd, MTIOCTOP, &mt);
2040 gboolean tape_bsf(int fd, guint count) {
2043 mt.mt_count = count;
2044 return 0 == ioctl(fd, MTIOCTOP, &mt);
2047 gboolean tape_fsr(int fd, guint count) {
2050 mt.mt_count = count;
2051 return 0 == ioctl(fd, MTIOCTOP, &mt);
2054 gboolean tape_bsr(int fd, guint count) {
2057 mt.mt_count = count;
2058 return 0 == ioctl(fd, MTIOCTOP, &mt);
2061 gint tape_fileno(int fd) {
2064 if (0 != ioctl(fd, MTIOCGET, &get))
2065 return TAPE_POSITION_UNKNOWN;
2066 if (get.mt_fileno < 0)
2067 return TAPE_POSITION_UNKNOWN;
2069 return get.mt_fileno;
2072 gint tape_eod(int fd) {
2077 if (0 != ioctl(fd, MTIOCTOP, &mt))
2078 return TAPE_OP_ERROR;
2080 /* Ignored result. This is just to flush buffers. */
2082 ioctl(fd, MTIOCTOP, &mt);
2084 if (0 != ioctl(fd, MTIOCGET, &get))
2085 return TAPE_POSITION_UNKNOWN;
2086 if (get.mt_fileno < 0)
2087 return TAPE_POSITION_UNKNOWN;
2089 return get.mt_fileno;
2092 gboolean tape_weof(int fd, guint8 count) {
2095 mt.mt_count = count;
2096 return 0 == ioctl(fd, MTIOCTOP, &mt);
2099 gboolean tape_setcompression(int fd G_GNUC_UNUSED,
2100 gboolean on G_GNUC_UNUSED) {
2101 #ifdef MTCOMPRESSION
2103 mt.mt_op = MTCOMPRESSION;
2105 return 0 == ioctl(fd, MTIOCTOP, &mt);
2111 gboolean tape_offl(int fd) {
2117 if (0 == ioctl(fd, MTIOCTOP, &mt))
2121 g_debug("tape_off: ioctl(MTIOCTOP/MTOFFL) failed: %s", strerror(errno));
2127 DeviceStatusFlags tape_is_tape_device(int fd) {
2131 if (0 == ioctl(fd, MTIOCTOP, &mt)) {
2132 return DEVICE_STATUS_SUCCESS;
2134 } else if (errno == ENOMEDIUM) {
2135 return DEVICE_STATUS_VOLUME_MISSING;
2138 g_debug("tape_is_tape_device: ioctl(MTIOCTOP/MTNOP) failed: %s",
2141 /* some devices return EIO while the drive is busy loading */
2142 return DEVICE_STATUS_DEVICE_ERROR|DEVICE_STATUS_DEVICE_BUSY;
2144 return DEVICE_STATUS_DEVICE_ERROR;
2149 DeviceStatusFlags tape_is_ready(int fd, TapeDevice *t_self G_GNUC_UNUSED) {
2151 if (0 == ioctl(fd, MTIOCGET, &get)) {
2152 #if defined(GMT_ONLINE) || defined(GMT_DR_OPEN)
2155 && (t_self->broken_gmt_online || GMT_ONLINE(get.mt_gstat))
2158 && !GMT_DR_OPEN(get.mt_gstat)
2161 return DEVICE_STATUS_SUCCESS;
2163 return DEVICE_STATUS_VOLUME_MISSING;
2165 #else /* Neither macro is defined. */
2166 return DEVICE_STATUS_SUCCESS;
2169 return DEVICE_STATUS_VOLUME_ERROR;