Debian Codegraph @

device-src/tape-device.c

raw
1 /*
2 * Copyright (c) 2007-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 "pipespawn.h"
25 #include <string.h> /* memset() */
26 #include "amutil.h"
27 #include "device.h"
28
29 #ifdef HAVE_SYS_TAPE_H
30 # include <sys/tape.h>
31 #endif
32 #ifdef HAVE_SYS_MTIO_H
33 # include <sys/mtio.h>
34 #endif
35 #ifdef HAVE_LIMITS_H
36 # include <limits.h>
37 #endif
38
39 /* This is equal to 2*1024*1024*1024 - 16*1024*1024 - 1, but written
40 explicitly to avoid overflow issues. */
41 #define RESETOFS_THRESHOLD (0x7effffff)
42
43 /* Largest possible block size on SCSI systems. */
44 #define LARGEST_BLOCK_ESTIMATE (16 * 1024 * 1024)
45
46 /*
47 * Type checking and casting macros
48 */
49
50 #define TYPE_TAPE_DEVICE (tape_device_get_type())
51 #define TAPE_DEVICE(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), tape_device_get_type(), TapeDevice)
52 #define TAPE_DEVICE_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), tape_device_get_type(), TapeDevice const)
53 #define TAPE_DEVICE_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), tape_device_get_type(), TapeDeviceClass)
54 #define IS_TAPE_DEVICE(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), tape_device_get_type ())
55 #define TAPE_DEVICE_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), tape_device_get_type(), TapeDeviceClass)
56 GType tape_device_get_type (void);
57
58 /*
59 * Main object structure
60 */
61 typedef struct TapeDevicePrivate_s TapeDevicePrivate;
62 typedef struct _TapeDevice {
63 Device __parent__;
64
65 /* It should go without saying that all this stuff is
66 * look-but-don't-touch. */
67
68 /* characteristics of the device */
69 gboolean fsf, bsf, fsr, bsr, eom, bsf_after_eom, broken_gmt_online;
70 gboolean leom;
71 gboolean nonblocking_open, fsf_after_filemark;
72 int final_filemarks;
73
74 /* 0 if we opened with O_RDWR; error otherwise. */
75 gboolean write_open_errno;
76 int fd;
77
78 TapeDevicePrivate * private;
79 } TapeDevice;
80
81 struct TapeDevicePrivate_s {
82 /* This holds the total number of bytes written to the device,
83 modulus RESETOFS_THRESHOLD. */
84 int write_count;
85 char * device_filename;
86 gsize read_block_size;
87 };
88
89 /*
90 * Class definition
91 */
92
93 typedef struct _TapeDeviceClass TapeDeviceClass;
94 struct _TapeDeviceClass {
95 DeviceClass __parent__;
96 };
97
98 void tape_device_register(void);
99
100 /* useful callback for tape ops */
101 void tape_device_set_capabilities(TapeDevice *self,
102 gboolean fsf, PropertySurety fsf_surety, PropertySource fsf_source,
103 gboolean fsf_after_filemark, PropertySurety faf_surety, PropertySource faf_source,
104 gboolean bsf, PropertySurety bsf_surety, PropertySource bsf_source,
105 gboolean fsr, PropertySurety fsr_surety, PropertySource fsr_source,
106 gboolean bsr, PropertySurety bsr_surety, PropertySource bsr_source,
107 gboolean eom, PropertySurety eom_surety, PropertySource eom_source,
108 gboolean leom, PropertySurety leom_surety, PropertySource leom_source,
109 gboolean bsf_after_eom, PropertySurety bae_surety, PropertySource bae_source,
110 guint final_filemarks, PropertySurety ff_surety, PropertySource ff_source);
111
112 /* Real Operations (always return FALSE if not implemented) */
113 gboolean tape_rewind(int fd);
114 gboolean tape_fsf(int fd, guint count);
115 gboolean tape_bsf(int fd, guint count);
116 gboolean tape_fsr(int fd, guint count);
117 gboolean tape_bsr(int fd, guint count);
118 gint tape_fileno(int fd);
119
120 /* tape_fileno returns tape position file number, or one of these: */
121 #define TAPE_OP_ERROR -1
122 #define TAPE_POSITION_UNKNOWN -2
123
124 /* Possible (abstracted) results from a system I/O operation. */
125 typedef enum {
126 RESULT_SUCCESS,
127 RESULT_ERROR, /* Undefined error (*errmsg set) */
128 RESULT_SMALL_BUFFER, /* Tried to read with a buffer that is too
129 small. */
130 RESULT_NO_DATA, /* End of File, while reading */
131 RESULT_NO_SPACE, /* Out of space. Sometimes we don't know if
132 it was this or I/O error, but this is the
133 preferred explanation. */
134 RESULT_MAX
135 } IoResult;
136
137 /* returns a fileno like tape_fileno */
138 gint tape_eod(int fd);
139
140 gboolean tape_weof(int fd, guint8 count);
141 gboolean tape_setcompression(int fd, gboolean on);
142
143 gboolean tape_offl(int fd);
144
145 DeviceStatusFlags tape_is_tape_device(int fd);
146 DeviceStatusFlags get_tape_blocksize(int fd, guint64 *tape_blocksize);
147 DeviceStatusFlags tape_is_ready(int fd, TapeDevice *t_self);
148
149 #define tape_device_read_size(self) \
150 (((TapeDevice *)(self))->private->read_block_size? \
151 ((TapeDevice *)(self))->private->read_block_size : ((Device *)(self))->block_size)
152
153 /*
154 * Our device-specific properties.
155 */
156
157 #define PROPERTY_BROKEN_GMT_ONLINE (device_property_broken_gmt_online.ID)
158 #define PROPERTY_FSF (device_property_fsf.ID)
159 #define PROPERTY_FSF_AFTER_FILEMARK (device_property_fsf_after_filemark.ID)
160 #define PROPERTY_BSF (device_property_bsf.ID)
161 #define PROPERTY_FSR (device_property_fsr.ID)
162 #define PROPERTY_BSR (device_property_bsr.ID)
163 #define PROPERTY_EOM (device_property_eom.ID)
164 #define PROPERTY_BSF_AFTER_EOM (device_property_bsf_after_eom.ID)
165 #define PROPERTY_NONBLOCKING_OPEN (device_property_nonblocking_open.ID)
166 #define PROPERTY_FINAL_FILEMARKS (device_property_final_filemarks.ID)
167
168 static DevicePropertyBase device_property_broken_gmt_online;
169 static DevicePropertyBase device_property_fsf;
170 static DevicePropertyBase device_property_fsf_after_filemark;
171 static DevicePropertyBase device_property_bsf;
172 static DevicePropertyBase device_property_fsr;
173 static DevicePropertyBase device_property_bsr;
174 static DevicePropertyBase device_property_eom;
175 static DevicePropertyBase device_property_bsf_after_eom;
176 static DevicePropertyBase device_property_nonblocking_open;
177 static DevicePropertyBase device_property_final_filemarks;
178 static DevicePropertyBase device_property_read_buffer_size; /* old name for READ_BLOCK_SIZE */
179
180 /* here are local prototypes */
181 static void tape_device_init (TapeDevice * o);
182 static void tape_device_class_init (TapeDeviceClass * c);
183 static void tape_device_base_init (TapeDeviceClass * c);
184 static gboolean tape_device_set_feature_property_fn(Device *p_self, DevicePropertyBase *base,
185 GValue *val, PropertySurety surety, PropertySource source);
186 static gboolean tape_device_set_final_filemarks_fn(Device *p_self, DevicePropertyBase *base,
187 GValue *val, PropertySurety surety, PropertySource source);
188 static gboolean tape_device_set_compression_fn(Device *p_self, DevicePropertyBase *base,
189 GValue *val, PropertySurety surety, PropertySource source);
190 static gboolean tape_device_get_read_block_size_fn(Device *p_self, DevicePropertyBase *base,
191 GValue *val, PropertySurety *surety, PropertySource *source);
192 static gboolean tape_device_set_read_block_size_fn(Device *p_self, DevicePropertyBase *base,
193 GValue *val, PropertySurety surety, PropertySource source);
194 static void tape_device_open_device (Device * self, char * device_name, char * device_type, char * device_node);
195 static Device * tape_device_factory (char * device_name, char * device_type, char * device_node);
196 static DeviceStatusFlags tape_device_read_label(Device * self);
197 static DeviceWriteResult tape_device_write_block(Device * self, guint size,
198 gpointer data);
199 static int tape_device_read_block(Device * self, gpointer buf,
200 int * size_req, int max_block);
201 static gboolean tape_device_start (Device * self, DeviceAccessMode mode,
202 char * label, char * timestamp);
203 static gboolean tape_device_start_file (Device * self, dumpfile_t * ji);
204 static gboolean tape_device_finish_file (Device * self);
205 static dumpfile_t * tape_device_seek_file (Device * self, guint file);
206 static gboolean tape_device_seek_block (Device * self, guint64 block);
207 static gboolean tape_device_eject (Device * self);
208 static gboolean tape_device_finish (Device * self);
209 static gboolean tape_device_check_writable(Device *self);
210 static IoResult tape_device_robust_read (TapeDevice * self, void * buf,
211 int * count, char **errmsg);
212 static IoResult tape_device_robust_write (TapeDevice * self, void * buf, int count, char **errmsg);
213 static gboolean tape_device_fsf (TapeDevice * self, guint count);
214 static gboolean tape_device_fsr (TapeDevice * self, guint count);
215 static gboolean tape_device_bsr (TapeDevice * self, guint count, guint file, guint block);
216 static gboolean tape_device_eod (TapeDevice * self);
217
218 /* pointer to the class of our parent */
219 static DeviceClass *parent_class = NULL;
220
221 GType tape_device_get_type (void)
222 {
223 static GType type = 0;
224
225 if (G_UNLIKELY(type == 0)) {
226 static const GTypeInfo info = {
227 sizeof (TapeDeviceClass),
228 (GBaseInitFunc) tape_device_base_init,
229 (GBaseFinalizeFunc) NULL,
230 (GClassInitFunc) tape_device_class_init,
231 (GClassFinalizeFunc) NULL,
232 NULL /* class_data */,
233 sizeof (TapeDevice),
234 0 /* n_preallocs */,
235 (GInstanceInitFunc) tape_device_init,
236 NULL
237 };
238
239 type = g_type_register_static (TYPE_DEVICE, "TapeDevice",
240 &info, (GTypeFlags)0);
241 }
242
243 return type;
244 }
245
246 static void
247 tape_device_init (TapeDevice * self) {
248 Device * d_self;
249 GValue response;
250
251 d_self = DEVICE(self);
252 bzero(&response, sizeof(response));
253
254 self->private = g_new0(TapeDevicePrivate, 1);
255
256 /* Clear all fields. */
257 d_self->block_size = 32768;
258 d_self->min_block_size = 32768;
259 d_self->max_block_size = LARGEST_BLOCK_ESTIMATE;
260 self->broken_gmt_online = FALSE;
261
262 self->fd = -1;
263
264 /* set all of the feature properties to an unsure default of FALSE */
265 self->broken_gmt_online = FALSE;
266 self->fsf = FALSE;
267 self->bsf = FALSE;
268 self->fsr = FALSE;
269 self->bsr = FALSE;
270 self->eom = FALSE;
271 self->leom = FALSE;
272 self->bsf_after_eom = FALSE;
273
274 g_value_init(&response, G_TYPE_BOOLEAN);
275 g_value_set_boolean(&response, FALSE);
276 device_set_simple_property(d_self, PROPERTY_BROKEN_GMT_ONLINE,
277 &response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
278 device_set_simple_property(d_self, PROPERTY_FSF,
279 &response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
280 device_set_simple_property(d_self, PROPERTY_FSF_AFTER_FILEMARK,
281 &response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
282 device_set_simple_property(d_self, PROPERTY_BSF,
283 &response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
284 device_set_simple_property(d_self, PROPERTY_FSR,
285 &response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
286 device_set_simple_property(d_self, PROPERTY_BSR,
287 &response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
288 device_set_simple_property(d_self, PROPERTY_EOM,
289 &response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
290 device_set_simple_property(d_self, PROPERTY_LEOM,
291 &response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
292 device_set_simple_property(d_self, PROPERTY_BSF_AFTER_EOM,
293 &response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
294
295 #ifdef DEFAULT_TAPE_NON_BLOCKING_OPEN
296 self->nonblocking_open = TRUE;
297 #else
298 self->nonblocking_open = FALSE;
299 #endif
300 g_value_set_boolean(&response, self->nonblocking_open);
301 device_set_simple_property(d_self, PROPERTY_NONBLOCKING_OPEN,
302 &response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
303 g_value_unset(&response);
304
305 self->final_filemarks = 2;
306 g_value_init(&response, G_TYPE_UINT);
307 g_value_set_uint(&response, self->final_filemarks);
308 device_set_simple_property(d_self, PROPERTY_FINAL_FILEMARKS,
309 &response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
310 g_value_unset(&response);
311
312 self->private->read_block_size = 0;
313 g_value_init(&response, G_TYPE_UINT);
314 g_value_set_uint(&response, self->private->read_block_size);
315 device_set_simple_property(d_self, PROPERTY_READ_BLOCK_SIZE,
316 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DEFAULT);
317 g_value_unset(&response);
318
319 self->private->write_count = 0;
320 self->private->device_filename = NULL;
321
322 /* Static properites */
323 g_value_init(&response, CONCURRENCY_PARADIGM_TYPE);
324 g_value_set_enum(&response, CONCURRENCY_PARADIGM_EXCLUSIVE);
325 device_set_simple_property(d_self, PROPERTY_CONCURRENCY,
326 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
327 g_value_unset(&response);
328
329 g_value_init(&response, STREAMING_REQUIREMENT_TYPE);
330 g_value_set_enum(&response, STREAMING_REQUIREMENT_DESIRED);
331 device_set_simple_property(d_self, PROPERTY_STREAMING,
332 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
333 g_value_unset(&response);
334
335 g_value_init(&response, G_TYPE_BOOLEAN);
336 g_value_set_boolean(&response, TRUE);
337 device_set_simple_property(d_self, PROPERTY_APPENDABLE,
338 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
339 g_value_unset(&response);
340
341 g_value_init(&response, G_TYPE_BOOLEAN);
342 g_value_set_boolean(&response, FALSE);
343 device_set_simple_property(d_self, PROPERTY_PARTIAL_DELETION,
344 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
345 g_value_unset(&response);
346
347 g_value_init(&response, G_TYPE_BOOLEAN);
348 g_value_set_boolean(&response, FALSE);
349 device_set_simple_property(d_self, PROPERTY_FULL_DELETION,
350 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
351 g_value_unset(&response);
352
353 g_value_init(&response, MEDIA_ACCESS_MODE_TYPE);
354 g_value_set_enum(&response, MEDIA_ACCESS_MODE_READ_WRITE);
355 device_set_simple_property(d_self, PROPERTY_MEDIUM_ACCESS_TYPE,
356 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
357 g_value_unset(&response);
358 }
359
360 static void tape_device_finalize(GObject * obj_self) {
361 TapeDevice * self = TAPE_DEVICE(obj_self);
362
363 if(G_OBJECT_CLASS(parent_class)->finalize) \
364 (* G_OBJECT_CLASS(parent_class)->finalize)(obj_self);
365
366 robust_close(self->fd);
367 self->fd = -1;
368 amfree(self->private->device_filename);
369 amfree(self->private);
370 }
371
372 static void
373 tape_device_class_init (TapeDeviceClass * c)
374 {
375 DeviceClass *device_class = (DeviceClass *)c;
376 GObjectClass *g_object_class = (GObjectClass *)c;
377
378 parent_class = g_type_class_ref (TYPE_DEVICE);
379
380 device_class->open_device = tape_device_open_device;
381 device_class->read_label = tape_device_read_label;
382 device_class->write_block = tape_device_write_block;
383 device_class->read_block = tape_device_read_block;
384 device_class->start = tape_device_start;
385 device_class->start_file = tape_device_start_file;
386 device_class->finish_file = tape_device_finish_file;
387 device_class->seek_file = tape_device_seek_file;
388 device_class->seek_block = tape_device_seek_block;
389 device_class->eject = tape_device_eject;
390 device_class->finish = tape_device_finish;
391 device_class->check_writable = tape_device_check_writable;
392
393 g_object_class->finalize = tape_device_finalize;
394 }
395
396 static void
397 tape_device_base_init (TapeDeviceClass * c)
398 {
399 DeviceClass *device_class = (DeviceClass *)c;
400
401 device_class_register_property(device_class, PROPERTY_BROKEN_GMT_ONLINE,
402 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
403 device_simple_property_get_fn,
404 tape_device_set_feature_property_fn);
405
406 device_class_register_property(device_class, PROPERTY_FSF,
407 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
408 device_simple_property_get_fn,
409 tape_device_set_feature_property_fn);
410
411 device_class_register_property(device_class, PROPERTY_FSF_AFTER_FILEMARK,
412 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
413 device_simple_property_get_fn,
414 tape_device_set_feature_property_fn);
415
416 device_class_register_property(device_class, PROPERTY_BSF,
417 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
418 device_simple_property_get_fn,
419 tape_device_set_feature_property_fn);
420
421 device_class_register_property(device_class, PROPERTY_FSR,
422 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
423 device_simple_property_get_fn,
424 tape_device_set_feature_property_fn);
425
426 device_class_register_property(device_class, PROPERTY_BSR,
427 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
428 device_simple_property_get_fn,
429 tape_device_set_feature_property_fn);
430
431 device_class_register_property(device_class, PROPERTY_EOM,
432 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
433 device_simple_property_get_fn,
434 tape_device_set_feature_property_fn);
435
436 device_class_register_property(device_class, PROPERTY_BSF_AFTER_EOM,
437 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
438 device_simple_property_get_fn,
439 tape_device_set_feature_property_fn);
440
441 device_class_register_property(device_class, PROPERTY_NONBLOCKING_OPEN,
442 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
443 device_simple_property_get_fn,
444 tape_device_set_feature_property_fn);
445
446 device_class_register_property(device_class, PROPERTY_FINAL_FILEMARKS,
447 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
448 device_simple_property_get_fn,
449 tape_device_set_final_filemarks_fn);
450
451 /* We don't (yet?) support reading the device's compression state, so not
452 * gettable. */
453 device_class_register_property(device_class, PROPERTY_COMPRESSION,
454 PROPERTY_ACCESS_SET_MASK,
455 NULL,
456 tape_device_set_compression_fn);
457
458 device_class_register_property(device_class, PROPERTY_READ_BLOCK_SIZE,
459 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
460 tape_device_get_read_block_size_fn,
461 tape_device_set_read_block_size_fn);
462
463 device_class_register_property(device_class, device_property_read_buffer_size.ID,
464 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
465 tape_device_get_read_block_size_fn,
466 tape_device_set_read_block_size_fn);
467
468 /* add the ability to set LEOM to FALSE, for testing purposes */
469 device_class_register_property(device_class, PROPERTY_LEOM,
470 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
471 device_simple_property_get_fn,
472 tape_device_set_feature_property_fn);
473 }
474
475 static gboolean
476 tape_device_set_feature_property_fn(Device *p_self, DevicePropertyBase *base,
477 GValue *val, PropertySurety surety, PropertySource source)
478 {
479 TapeDevice *self = TAPE_DEVICE(p_self);
480 GValue old_val;
481 gboolean old_bool, new_bool;
482 PropertySurety old_surety;
483 PropertySource old_source;
484
485 new_bool = g_value_get_boolean(val);
486
487 /* get the old source and surety and see if we're willing to make this change */
488 bzero(&old_val, sizeof(old_val));
489 if (device_get_simple_property(p_self, base->ID, &old_val, &old_surety, &old_source)) {
490 old_bool = g_value_get_boolean(&old_val);
491
492 if (old_surety == PROPERTY_SURETY_GOOD && old_source == PROPERTY_SOURCE_DETECTED) {
493 if (new_bool != old_bool) {
494 device_set_error(p_self, g_strdup_printf(_(
495 "Value for property '%s' was autodetected and cannot be changed"),
496 base->name),
497 DEVICE_STATUS_DEVICE_ERROR);
498 return FALSE;
499 } else {
500 /* pretend we set it, but don't change surety/source */
501 return TRUE;
502 }
503 }
504 }
505
506 /* (note: PROPERTY_* are not constants, so we can't use switch) */
507 if (base->ID == PROPERTY_BROKEN_GMT_ONLINE)
508 self->broken_gmt_online = new_bool;
509 else if (base->ID == PROPERTY_FSF)
510 self->fsf = new_bool;
511 else if (base->ID == PROPERTY_FSF_AFTER_FILEMARK)
512 self->fsf_after_filemark = new_bool;
513 else if (base->ID == PROPERTY_BSF)
514 self->bsf = new_bool;
515 else if (base->ID == PROPERTY_FSR)
516 self->fsr = new_bool;
517 else if (base->ID == PROPERTY_BSR)
518 self->bsr = new_bool;
519 else if (base->ID == PROPERTY_EOM)
520 self->eom = new_bool;
521 else if (base->ID == PROPERTY_BSF_AFTER_EOM)
522 self->bsf_after_eom = new_bool;
523 else if (base->ID == PROPERTY_NONBLOCKING_OPEN)
524 self->nonblocking_open = new_bool;
525 else if (base->ID == PROPERTY_LEOM)
526 self->leom = new_bool;
527 else
528 return FALSE; /* shouldn't happen */
529
530 return device_simple_property_set_fn(p_self, base, val, surety, source);
531 }
532
533 static gboolean
534 tape_device_set_final_filemarks_fn(Device *p_self, DevicePropertyBase *base,
535 GValue *val, PropertySurety surety, PropertySource source)
536 {
537 TapeDevice *self = TAPE_DEVICE(p_self);
538 GValue old_val;
539 gboolean old_int, new_int;
540 PropertySurety old_surety;
541 PropertySource old_source;
542
543 new_int = g_value_get_uint(val);
544
545 /* get the old source and surety and see if we're willing to make this change */
546 bzero(&old_val, sizeof(old_val));
547 if (device_get_simple_property(p_self, base->ID, &old_val, &old_surety, &old_source)) {
548 old_int = g_value_get_uint(&old_val);
549
550 if (old_surety == PROPERTY_SURETY_GOOD && old_source == PROPERTY_SOURCE_DETECTED) {
551 if (new_int != old_int) {
552 device_set_error(p_self, g_strdup_printf(_(
553 "Value for property '%s' was autodetected and cannot be changed"),
554 base->name),
555 DEVICE_STATUS_DEVICE_ERROR);
556 return FALSE;
557 } else {
558 /* pretend we set it, but don't change surety/source */
559 return TRUE;
560 }
561 }
562 }
563
564 self->final_filemarks = new_int;
565
566 return device_simple_property_set_fn(p_self, base, val, surety, source);
567 }
568
569 static gboolean
570 tape_device_set_compression_fn(Device *p_self, DevicePropertyBase *base,
571 GValue *val, PropertySurety surety, PropertySource source)
572 {
573 TapeDevice *self = TAPE_DEVICE(p_self);
574 gboolean request = g_value_get_boolean(val);
575
576 /* We allow this property to be set at any time. This is mostly
577 * because setting compression is a hit-and-miss proposition
578 * at any time; some drives accept the mode setting but don't
579 * actually support compression, while others do support
580 * compression but do it via density settings or some other
581 * way. Set this property whenever you want, but all we'll do
582 * is report whether or not the ioctl succeeded. */
583 if (tape_setcompression(self->fd, request)) {
584 /* looks good .. let's start the device over, though */
585 device_clear_volume_details(p_self);
586 } else {
587 device_set_error(p_self,
588 g_strdup("Error setting COMPRESSION property"),
589 DEVICE_STATUS_DEVICE_ERROR);
590 return FALSE;
591 }
592
593 return device_simple_property_set_fn(p_self, base, val, surety, source);
594 }
595
596 static gboolean
597 tape_device_get_read_block_size_fn(Device *p_self, DevicePropertyBase *base G_GNUC_UNUSED,
598 GValue *val, PropertySurety *surety, PropertySource *source)
599 {
600 /* use the READ_BLOCK_SIZE, even if we're invoked to get the old READ_BUFFER_SIZE */
601 return device_simple_property_get_fn(p_self, &device_property_read_block_size,
602 val, surety, source);
603 }
604
605 static gboolean
606 tape_device_set_read_block_size_fn(Device *p_self, DevicePropertyBase *base G_GNUC_UNUSED,
607 GValue *val, PropertySurety surety, PropertySource source)
608 {
609 TapeDevice *self = TAPE_DEVICE(p_self);
610 guint read_block_size = g_value_get_uint(val);
611
612 if (read_block_size != 0 &&
613 ((gsize)read_block_size < p_self->block_size ||
614 (gsize)read_block_size > p_self->max_block_size)) {
615 device_set_error(p_self,
616 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),
617 DEVICE_STATUS_DEVICE_ERROR);
618 return FALSE;
619 }
620
621 self->private->read_block_size = read_block_size;
622
623 /* use the READ_BLOCK_SIZE, even if we're invoked to get the old READ_BUFFER_SIZE */
624 return device_simple_property_set_fn(p_self, &device_property_read_block_size,
625 val, surety, source);
626 }
627
628 void tape_device_register(void) {
629 static const char * device_prefix_list[] = { "tape", NULL };
630
631 /* First register tape-specific properties */
632 device_property_fill_and_register(&device_property_broken_gmt_online,
633 G_TYPE_BOOLEAN, "broken_gmt_online",
634 "Does this drive support the GMT_ONLINE macro?");
635
636 device_property_fill_and_register(&device_property_fsf,
637 G_TYPE_BOOLEAN, "fsf",
638 "Does this drive support the MTFSF command?");
639
640 device_property_fill_and_register(&device_property_fsf_after_filemark,
641 G_TYPE_BOOLEAN, "fsf_after_filemark",
642 "Does this drive needs a FSF if a filemark is already read?");
643
644 device_property_fill_and_register(&device_property_bsf,
645 G_TYPE_BOOLEAN, "bsf",
646 "Does this drive support the MTBSF command?" );
647
648 device_property_fill_and_register(&device_property_fsr,
649 G_TYPE_BOOLEAN, "fsr",
650 "Does this drive support the MTFSR command?");
651
652 device_property_fill_and_register(&device_property_bsr,
653 G_TYPE_BOOLEAN, "bsr",
654 "Does this drive support the MTBSR command?");
655
656 device_property_fill_and_register(&device_property_eom,
657 G_TYPE_BOOLEAN, "eom",
658 "Does this drive support the MTEOM command?");
659
660 device_property_fill_and_register(&device_property_bsf_after_eom,
661 G_TYPE_BOOLEAN,
662 "bsf_after_eom",
663 "Does this drive require an MTBSF after MTEOM in order to append?" );
664
665 device_property_fill_and_register(&device_property_nonblocking_open,
666 G_TYPE_BOOLEAN,
667 "nonblocking_open",
668 "Does this drive require a open with O_NONBLOCK?" );
669
670 device_property_fill_and_register(&device_property_final_filemarks,
671 G_TYPE_UINT, "final_filemarks",
672 "How many filemarks to write after the last tape file?" );
673
674 device_property_fill_and_register(&device_property_read_buffer_size,
675 G_TYPE_UINT, "read_buffer_size",
676 "(deprecated name for READ_BLOCK_SIZE)");
677
678 /* Then the device itself */
679 register_device(tape_device_factory, device_prefix_list);
680 }
681
682 /* Open the tape device, trying various combinations of O_RDWR and
683 O_NONBLOCK. Returns -1 and calls device_set_error for errors
684 On Linux, with O_NONBLOCK, the kernel just checks the state once,
685 whereas it checks it every second for ST_BLOCK_SECONDS if O_NONBLOCK is
686 not given. Amanda already have the code to poll, we want open to check
687 the state only once. */
688
689 static int try_open_tape_device(TapeDevice * self, char * device_filename) {
690 int fd;
691 int save_errno;
692 DeviceStatusFlags new_status;
693 guint64 tape_blocksize;
694
695 #ifdef O_NONBLOCK
696 int nonblocking = 0;
697
698 if (self->nonblocking_open) {
699 nonblocking = O_NONBLOCK;
700 }
701 #endif
702
703 #ifdef O_NONBLOCK
704 fd = robust_open(device_filename, O_RDWR | nonblocking, 0);
705 save_errno = errno;
706 if (fd < 0 && nonblocking && (save_errno == EWOULDBLOCK || save_errno == EINVAL)) {
707 /* Maybe we don't support O_NONBLOCK for tape devices. */
708 fd = robust_open(device_filename, O_RDWR, 0);
709 save_errno = errno;
710 }
711 #else
712 fd = robust_open(device_filename, O_RDWR, 0);
713 save_errno = errno;
714 #endif
715 if (fd >= 0) {
716 self->write_open_errno = 0;
717 } else {
718 if (errno == EACCES || errno == EPERM
719 #ifdef EROFS
720 || errno == EROFS
721 #endif
722 ) {
723 /* Device is write-protected. */
724 self->write_open_errno = errno;
725 #ifdef O_NONBLOCK
726 fd = robust_open(device_filename, O_RDONLY | nonblocking, 0);
727 save_errno = errno;
728 if (fd < 0 && nonblocking && (save_errno == EWOULDBLOCK || save_errno == EINVAL)) {
729 fd = robust_open(device_filename, O_RDONLY, 0);
730 save_errno = errno;
731 }
732 #else
733 fd = robust_open(device_filename, O_RDONLY, 0);
734 save_errno = errno;
735 #endif
736 }
737 }
738 #ifdef O_NONBLOCK
739 /* Clear O_NONBLOCK for operations from now on. */
740 if (fd >= 0 && nonblocking) {
741 int r = fcntl(fd, F_GETFL, 0);
742 if (r < 0) {
743 device_set_error(DEVICE(self),
744 g_strdup_printf("Can't fcntl(F_GETFL) on %s: %s", self->private->device_filename, strerror(errno)),
745 DEVICE_STATUS_DEVICE_BUSY | DEVICE_STATUS_DEVICE_ERROR);
746 }
747
748 r = fcntl(fd, F_SETFL, r & ~O_NONBLOCK);
749 if (r < 0) {
750 device_set_error(DEVICE(self),
751 g_strdup_printf("Can't fcntl(F_SETFL) on %s: %s", self->private->device_filename, strerror(errno)),
752 DEVICE_STATUS_DEVICE_BUSY | DEVICE_STATUS_DEVICE_ERROR);
753 }
754 }
755 errno = save_errno;
756 /* function continues after #endif */
757
758 #endif /* O_NONBLOCK */
759
760 if (fd < 0) {
761 DeviceStatusFlags status_flag = 0;
762 if (errno == EBUSY)
763 status_flag = DEVICE_STATUS_DEVICE_BUSY;
764 else
765 status_flag = DEVICE_STATUS_DEVICE_ERROR;
766 device_set_error(DEVICE(self),
767 g_strdup_printf(_("Can't open tape device %s: %s"), self->private->device_filename, strerror(errno)),
768 status_flag);
769 return -1;
770 }
771
772 /* Check that this is actually a tape device. */
773 new_status = tape_is_tape_device(fd);
774 if (new_status & DEVICE_STATUS_DEVICE_ERROR) {
775 device_set_error(DEVICE(self),
776 g_strdup_printf(_("File %s is not a tape device"), self->private->device_filename),
777 new_status);
778 robust_close(fd);
779 return -1;
780 }
781 if (new_status & DEVICE_STATUS_VOLUME_MISSING) {
782 device_set_error(DEVICE(self),
783 g_strdup_printf(_("Tape device %s is not ready or is empty"), self->private->device_filename),
784 new_status);
785 robust_close(fd);
786 return -1;
787 }
788
789 new_status = tape_is_ready(fd, self);
790 if (new_status & DEVICE_STATUS_VOLUME_MISSING) {
791 device_set_error(DEVICE(self),
792 g_strdup_printf(_("Tape device %s is empty"), self->private->device_filename),
793 new_status);
794 robust_close(fd);
795 return -1;
796 }
797 if (new_status != DEVICE_STATUS_SUCCESS) {
798 device_set_error(DEVICE(self),
799 g_strdup_printf(_("Tape device %s is not ready or is empty"), self->private->device_filename),
800 new_status);
801 robust_close(fd);
802 return -1;
803 }
804
805 new_status = get_tape_blocksize(fd, &tape_blocksize);
806 if (new_status != DEVICE_STATUS_SUCCESS) {
807 device_set_error(DEVICE(self),
808 g_strdup_printf(_("Can't get the blocksize of the device %s"),
809 self->private->device_filename),
810 new_status);
811 robust_close(fd);
812 return -1;
813 }
814 if (tape_blocksize > 0 && tape_blocksize != tape_device_read_size(self)) {
815 device_set_error(DEVICE(self),
816 g_strdup_printf(_("Device %s use fixed block size of %lld and tapetype use %lld"),
817 self->private->device_filename,
818 (long long)tape_blocksize,
819 (long long)tape_device_read_size(self)),
820 DEVICE_STATUS_VOLUME_ERROR|DEVICE_STATUS_DEVICE_ERROR);
821 robust_close(fd);
822 return -1;
823 }
824 if (tape_blocksize == 0) {
825 g_debug("Device is in variable block size");
826 } else {
827 g_debug("Device is in fixed block size of %lld", (long long)tape_blocksize);
828 }
829
830 return fd;
831 }
832
833 static void
834 tape_device_open_device (Device * dself, char * device_name,
835 char * device_type, char * device_node) {
836 TapeDevice * self;
837 GValue val;
838
839 self = TAPE_DEVICE(dself);
840
841 self->fd = -1;
842 self->private->device_filename = g_strdup(device_node);
843
844 /* Set tape drive/OS info */
845 bzero(&val, sizeof(val));
846 g_value_init(&val, G_TYPE_BOOLEAN);
847
848 self->fsf = TRUE;
849 g_value_set_boolean(&val, self->fsf);
850 device_set_simple_property(dself, PROPERTY_FSF, &val, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
851
852 self->fsf_after_filemark = DEFAULT_FSF_AFTER_FILEMARK;
853 g_value_set_boolean(&val, self->fsf_after_filemark);
854 device_set_simple_property(dself, PROPERTY_FSF_AFTER_FILEMARK, &val, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
855
856 self->bsf = TRUE;
857 g_value_set_boolean(&val, self->bsf);
858 device_set_simple_property(dself, PROPERTY_BSF, &val, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
859
860 self->fsr = TRUE;
861 g_value_set_boolean(&val, self->fsr);
862 device_set_simple_property(dself, PROPERTY_FSR, &val, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
863
864 self->bsr = TRUE;
865 g_value_set_boolean(&val, self->bsr);
866 device_set_simple_property(dself, PROPERTY_BSR, &val, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
867
868 self->eom = TRUE;
869 g_value_set_boolean(&val, self->eom);
870 device_set_simple_property(dself, PROPERTY_EOM, &val, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
871
872 self->leom = FALSE;
873 g_value_set_boolean(&val, self->leom);
874 device_set_simple_property(dself, PROPERTY_LEOM, &val, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
875
876 self->bsf_after_eom = FALSE;
877 g_value_set_boolean(&val, self->bsf_after_eom);
878 device_set_simple_property(dself, PROPERTY_BSF_AFTER_EOM, &val, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
879
880 g_value_unset(&val);
881 g_value_init(&val, G_TYPE_UINT);
882
883 self->final_filemarks = 2;
884 g_value_set_uint(&val, self->final_filemarks);
885 device_set_simple_property(dself, PROPERTY_FINAL_FILEMARKS, &val, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
886
887 g_value_unset(&val);
888
889 /* Chain up */
890 if (parent_class->open_device) {
891 parent_class->open_device(dself, device_name, device_type, device_node);
892 }
893 }
894
895
896 static DeviceStatusFlags tape_device_read_label(Device * dself) {
897 TapeDevice * self;
898 char * header_buffer;
899 int buffer_len;
900 IoResult result;
901 dumpfile_t *header;
902 DeviceStatusFlags new_status;
903 char *msg = NULL;
904
905 self = TAPE_DEVICE(dself);
906
907 amfree(dself->volume_label);
908 amfree(dself->volume_time);
909 dumpfile_free(dself->volume_header);
910 dself->volume_header = NULL;
911
912 if (device_in_error(self)) return dself->status;
913
914 if (self->fd == -1) {
915 self->fd = try_open_tape_device(self, self->private->device_filename);
916 /* if the open failed, then try_open_tape_device already set the
917 * approppriate error status */
918 if (self->fd == -1)
919 return dself->status;
920 }
921
922 /* Rewind it. */
923 if (!tape_rewind(self->fd)) {
924 device_set_error(dself,
925 g_strdup_printf(_("Error rewinding device %s to read label: %s"),
926 self->private->device_filename, strerror(errno)),
927 DEVICE_STATUS_DEVICE_ERROR
928 | DEVICE_STATUS_VOLUME_ERROR);
929 return dself->status;
930 }
931
932 buffer_len = tape_device_read_size(self);
933 header_buffer = g_try_malloc(buffer_len);
934 if (header_buffer == NULL) {
935 device_set_error(dself,
936 g_strdup(_("Failed to allocate memory")),
937 DEVICE_STATUS_DEVICE_ERROR
938 | DEVICE_STATUS_VOLUME_ERROR);
939 return dself->status;
940 }
941 result = tape_device_robust_read(self, header_buffer, &buffer_len, &msg);
942
943 if (result != RESULT_SUCCESS) {
944 free(header_buffer);
945 tape_rewind(self->fd);
946 /* I/O error. */
947 switch (result) {
948 case RESULT_NO_DATA:
949 msg = g_strdup(_("no data"));
950 new_status = (DEVICE_STATUS_VOLUME_ERROR |
951 DEVICE_STATUS_VOLUME_UNLABELED);
952 header = dself->volume_header = g_new(dumpfile_t, 1);
953 fh_init(header);
954 break;
955
956 case RESULT_SMALL_BUFFER:
957 msg = g_strdup(_("block size too small"));
958 new_status = (DEVICE_STATUS_DEVICE_ERROR |
959 DEVICE_STATUS_VOLUME_ERROR);
960 header = dself->volume_header = g_new(dumpfile_t, 1);
961 fh_init(header);
962 header->type = F_WEIRD;
963 break;
964
965 default:
966 msg = g_strdup(_("unknown error"));
967 // fallthrough
968 case RESULT_ERROR:
969 new_status = (DEVICE_STATUS_DEVICE_ERROR |
970 DEVICE_STATUS_VOLUME_ERROR |
971 DEVICE_STATUS_VOLUME_UNLABELED);
972 break;
973 }
974 device_set_error(dself,
975 g_strdup_printf(_("Error reading Amanda header: %s"),
976 msg? msg : _("unknown error")),
977 new_status);
978 amfree(msg);
979 return dself->status;
980 }
981
982 if (buffer_len < 32768) {
983 device_set_error(dself,
984 g_strdup_printf(_("header is too small: %d bytes"), buffer_len),
985 DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
986 free(header_buffer);
987 return dself->status;
988 }
989
990 dself->header_block_size = buffer_len;
991 header = dself->volume_header = g_new(dumpfile_t, 1);
992 fh_init(header);
993
994 parse_file_header(header_buffer, header, buffer_len);
995 amfree(header_buffer);
996 if (header->type != F_TAPESTART) {
997 device_set_error(dself,
998 g_strdup(_("No tapestart header -- unlabeled device?")),
999 DEVICE_STATUS_VOLUME_UNLABELED);
1000 return dself->status;
1001 }
1002
1003 dself->volume_label = g_strdup(header->name);
1004 dself->volume_time = g_strdup(header->datestamp);
1005 /* dself->volume_header is already set */
1006
1007 device_set_error(dself, NULL, DEVICE_STATUS_SUCCESS);
1008
1009 return dself->status;
1010 }
1011
1012 static DeviceWriteResult
1013 tape_device_write_block(Device * pself, guint size, gpointer data) {
1014 TapeDevice * self;
1015 char *replacement_buffer = NULL;
1016 IoResult result;
1017 char *msg = NULL;
1018
1019 self = TAPE_DEVICE(pself);
1020
1021 g_assert(self->fd >= 0);
1022 if (device_in_error(self)) return WRITE_FAILED;
1023
1024 /* zero out to the end of a short block -- tape devices only write
1025 * whole blocks. */
1026 if (size < pself->block_size) {
1027 replacement_buffer = g_try_malloc(pself->block_size);
1028 if (replacement_buffer == NULL) {
1029 device_set_error(pself,
1030 g_strdup(_("failed to allocate memory")),
1031 DEVICE_STATUS_DEVICE_ERROR);
1032 return WRITE_FAILED;
1033 }
1034 memcpy(replacement_buffer, data, size);
1035 bzero(replacement_buffer+size, pself->block_size-size);
1036
1037 data = replacement_buffer;
1038 size = pself->block_size;
1039 }
1040
1041 result = tape_device_robust_write(self, data, size, &msg);
1042 amfree(replacement_buffer);
1043
1044 switch (result) {
1045 case RESULT_SUCCESS:
1046 break;
1047
1048 case RESULT_NO_SPACE:
1049 device_set_error(pself,
1050 g_strdup(_("No space left on device")),
1051 DEVICE_STATUS_VOLUME_ERROR);
1052 pself->is_eom = TRUE;
1053 return WRITE_FAILED;
1054
1055 default:
1056 msg = g_strdup(_("unknown error"));
1057 // fallthrough
1058 case RESULT_ERROR:
1059 device_set_error(pself,
1060 g_strdup_printf(_("Error writing block: %s"), msg),
1061 DEVICE_STATUS_DEVICE_ERROR);
1062 amfree(msg);
1063 return WRITE_FAILED;
1064 }
1065
1066 pself->block++;
1067 g_mutex_lock(pself->device_mutex);
1068 pself->bytes_written += size;
1069 g_mutex_unlock(pself->device_mutex);
1070
1071 return WRITE_SUCCEED;
1072 }
1073
1074 static int tape_device_read_block (Device * pself, gpointer buf,
1075 int * size_req, int max_block G_GNUC_UNUSED) {
1076 TapeDevice * self;
1077 int size;
1078 IoResult result;
1079 gssize read_block_size = tape_device_read_size(pself);
1080 char *msg = NULL;
1081
1082 self = TAPE_DEVICE(pself);
1083
1084 g_assert(self->fd >= 0);
1085 if (device_in_error(self)) return -1;
1086
1087 g_assert(read_block_size < INT_MAX); /* data type mismatch */
1088 if (buf == NULL || *size_req < (int)read_block_size) {
1089 /* Just a size query. */
1090 *size_req = (int)read_block_size;
1091 return 0;
1092 }
1093
1094 size = *size_req;
1095 result = tape_device_robust_read(self, buf, &size, &msg);
1096 switch (result) {
1097 case RESULT_SUCCESS:
1098 *size_req = size;
1099 pself->block++;
1100 g_mutex_lock(pself->device_mutex);
1101 pself->bytes_read += size;
1102 g_mutex_unlock(pself->device_mutex);
1103 return size;
1104 case RESULT_SMALL_BUFFER: {
1105 gsize new_size;
1106 GValue newval;
1107
1108 /* If this happens, it means that we have:
1109 * (next block size) > (buffer size) >= (read_block_size)
1110 * The solution is to ask for an even bigger buffer. We also play
1111 * some games to refrain from reading above the SCSI limit or from
1112 * integer overflow. Note that not all devices will tell us about
1113 * this problem -- some will just discard the "extra" data. */
1114 new_size = MIN(INT_MAX/2 - 1, *size_req) * 2;
1115 if (new_size > LARGEST_BLOCK_ESTIMATE &&
1116 *size_req < LARGEST_BLOCK_ESTIMATE) {
1117 new_size = LARGEST_BLOCK_ESTIMATE;
1118 }
1119 g_assert (new_size > (gsize)*size_req);
1120
1121 g_info("Device %s indicated blocksize %zd was too small; using %zd.",
1122 pself->device_name, (gsize)*size_req, new_size);
1123 *size_req = (int)new_size;
1124 self->private->read_block_size = new_size;
1125
1126 bzero(&newval, sizeof(newval));
1127 g_value_init(&newval, G_TYPE_UINT);
1128 g_value_set_uint(&newval, self->private->read_block_size);
1129 device_set_simple_property(pself, PROPERTY_READ_BLOCK_SIZE,
1130 &newval, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
1131 g_value_unset(&newval);
1132
1133 return 0;
1134 }
1135 case RESULT_NO_DATA:
1136 pself->is_eof = TRUE;
1137 g_mutex_lock(pself->device_mutex);
1138 pself->in_file = FALSE;
1139 g_mutex_unlock(pself->device_mutex);
1140 device_set_error(pself,
1141 g_strdup(_("EOF")),
1142 DEVICE_STATUS_SUCCESS);
1143 return -1;
1144
1145 default:
1146 msg = g_strdup(_("unknown error"));
1147 // fallthrough
1148 case RESULT_ERROR:
1149 device_set_error(pself,
1150 g_strdup_printf(_("Error reading from tape device: %s"), msg),
1151 DEVICE_STATUS_VOLUME_ERROR | DEVICE_STATUS_DEVICE_ERROR);
1152 amfree(msg);
1153 return -1;
1154 }
1155
1156 g_assert_not_reached();
1157 }
1158
1159 /* Just a helper function for tape_device_start(). */
1160 static gboolean write_tapestart_header(TapeDevice * self, char * label,
1161 char * timestamp) {
1162 IoResult result;
1163 dumpfile_t * header;
1164 char * header_buf;
1165 Device * d_self = (Device*)self;
1166 char *msg = NULL;
1167
1168 tape_rewind(self->fd);
1169
1170 header = make_tapestart_header(d_self, label, timestamp);
1171 g_assert(header != NULL);
1172 header_buf = device_build_amanda_header(d_self, header, NULL);
1173 if (header_buf == NULL) {
1174 device_set_error(d_self,
1175 g_strdup(_("Tapestart header won't fit in a single block!")),
1176 DEVICE_STATUS_DEVICE_ERROR);
1177 dumpfile_free(header);
1178 return FALSE;
1179 }
1180 dumpfile_free(d_self->volume_header);
1181 d_self->volume_header = NULL;
1182
1183 result = tape_device_robust_write(self, header_buf, d_self->block_size, &msg);
1184 if (result != RESULT_SUCCESS) {
1185 device_set_error(d_self,
1186 g_strdup_printf(_("Error writing tapestart header: %s"),
1187 (result == RESULT_ERROR)? msg : _("out of space")),
1188 DEVICE_STATUS_DEVICE_ERROR);
1189
1190 if (result == RESULT_NO_SPACE)
1191 d_self->is_eom = TRUE;
1192
1193 amfree(msg);
1194 dumpfile_free(header);
1195 amfree(header_buf);
1196 return FALSE;
1197 }
1198
1199 d_self->header_block_size = d_self->block_size;
1200 amfree(header_buf);
1201
1202 if (!tape_weof(self->fd, 1)) {
1203 device_set_error(d_self,
1204 g_strdup_printf(_("Error writing filemark: %s"),
1205 strerror(errno)),
1206 DEVICE_STATUS_DEVICE_ERROR|DEVICE_STATUS_VOLUME_ERROR);
1207 /* can't tell if this was EOM or not, so assume it is */
1208 d_self->is_eom = TRUE;
1209 dumpfile_free(header);
1210 return FALSE;
1211 }
1212
1213 d_self->volume_header = header;
1214 return TRUE;
1215 }
1216
1217 static gboolean
1218 tape_device_start (Device * d_self, DeviceAccessMode mode, char * label,
1219 char * timestamp) {
1220 TapeDevice * self;
1221
1222 self = TAPE_DEVICE(d_self);
1223
1224 if (device_in_error(self)) return FALSE;
1225
1226 if (self->fd == -1) {
1227 self->fd = try_open_tape_device(self, self->private->device_filename);
1228 /* if the open failed, then try_open_tape_device already set the
1229 * approppriate error status */
1230 if (self->fd == -1)
1231 return FALSE;
1232 }
1233
1234 if (mode != ACCESS_WRITE && d_self->volume_label == NULL) {
1235 /* we need a labeled volume for APPEND and READ */
1236 if (tape_device_read_label(d_self) != DEVICE_STATUS_SUCCESS)
1237 return FALSE;
1238 }
1239
1240 d_self->access_mode = mode;
1241 g_mutex_lock(d_self->device_mutex);
1242 d_self->in_file = FALSE;
1243 g_mutex_unlock(d_self->device_mutex);
1244
1245 if (IS_WRITABLE_ACCESS_MODE(mode)) {
1246 if (self->write_open_errno != 0) {
1247 /* We tried and failed to open the device in write mode. */
1248 device_set_error(d_self,
1249 g_strdup_printf(_("Can't open tape device %s for writing: %s"),
1250 self->private->device_filename, strerror(self->write_open_errno)),
1251 DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
1252 return FALSE;
1253 } else {
1254 if (!tape_device_check_writable(d_self)) {
1255 return FALSE;
1256 }
1257 if (!tape_rewind(self->fd)) {
1258 device_set_error(d_self,
1259 g_strdup_printf(_("Error rewinding device to start: %s"), strerror(errno)),
1260 DEVICE_STATUS_DEVICE_ERROR);
1261 return FALSE;
1262 }
1263 }
1264 }
1265
1266 /* Position the tape */
1267 switch (mode) {
1268 case ACCESS_APPEND:
1269 if (d_self->volume_label == NULL && device_read_label(d_self) != DEVICE_STATUS_SUCCESS) {
1270 /* device_read_label already set our error message */
1271 return FALSE;
1272 }
1273
1274 if (!tape_device_eod(self)) {
1275 device_set_error(d_self,
1276 g_strdup_printf(_("Couldn't seek to end of tape: %s"), strerror(errno)),
1277 DEVICE_STATUS_DEVICE_ERROR);
1278 return FALSE;
1279 }
1280 break;
1281
1282 case ACCESS_READ:
1283 if (d_self->volume_label == NULL && device_read_label(d_self) != DEVICE_STATUS_SUCCESS) {
1284 /* device_read_label already set our error message */
1285 return FALSE;
1286 }
1287
1288 if (!tape_rewind(self->fd)) {
1289 device_set_error(d_self,
1290 g_strdup_printf(_("Error rewinding device after reading label: %s"), strerror(errno)),
1291 DEVICE_STATUS_DEVICE_ERROR);
1292 return FALSE;
1293 }
1294 d_self->file = 0;
1295 break;
1296
1297 case ACCESS_WRITE:
1298 if (!write_tapestart_header(self, label, timestamp)) {
1299 /* write_tapestart_header already set the error status */
1300 return FALSE;
1301 }
1302
1303 g_free(d_self->volume_label);
1304 d_self->volume_label = g_strdup(label);
1305 g_free(d_self->volume_time);
1306 d_self->volume_time = g_strdup(timestamp);
1307
1308 /* unset the VOLUME_UNLABELED flag, if it was set */
1309 device_set_error(d_self, NULL, DEVICE_STATUS_SUCCESS);
1310 d_self->file = 0;
1311 break;
1312
1313 default:
1314 g_assert_not_reached();
1315 }
1316
1317 return TRUE;
1318 }
1319
1320 static gboolean tape_device_start_file(Device * d_self,
1321 dumpfile_t * info) {
1322 TapeDevice * self;
1323 IoResult result;
1324 char * amanda_header;
1325 char *msg = NULL;
1326
1327 self = TAPE_DEVICE(d_self);
1328
1329 g_assert(self->fd >= 0);
1330 if (device_in_error(self)) return FALSE;
1331
1332 /* set the blocksize in the header properly */
1333 info->blocksize = d_self->block_size;
1334
1335 /* Make the Amanda header suitable for writing to the device. */
1336 /* Then write the damn thing. */
1337 amanda_header = device_build_amanda_header(d_self, info, NULL);
1338 if (amanda_header == NULL) {
1339 device_set_error(d_self,
1340 g_strdup(_("Amanda file header won't fit in a single block!")),
1341 DEVICE_STATUS_DEVICE_ERROR);
1342 return FALSE;
1343 }
1344
1345 result = tape_device_robust_write(self, amanda_header, d_self->block_size, &msg);
1346 if (result != RESULT_SUCCESS) {
1347 device_set_error(d_self,
1348 g_strdup_printf(_("Error writing file header: %s"),
1349 (result == RESULT_ERROR)? msg : _("out of space")),
1350 DEVICE_STATUS_DEVICE_ERROR);
1351
1352 if (result == RESULT_NO_SPACE)
1353 d_self->is_eom = TRUE;
1354
1355 amfree(amanda_header);
1356 amfree(msg);
1357 return FALSE;
1358 }
1359 amfree(amanda_header);
1360
1361 /* arrange the file numbers correctly */
1362 d_self->block = 0;
1363 if (d_self->file >= 0)
1364 d_self->file ++;
1365 g_mutex_lock(d_self->device_mutex);
1366 d_self->in_file = TRUE;
1367 d_self->bytes_written = 0;
1368 g_mutex_unlock(d_self->device_mutex);
1369 return TRUE;
1370 }
1371
1372 static gboolean
1373 tape_device_finish_file (Device * d_self) {
1374 TapeDevice * self;
1375
1376 self = TAPE_DEVICE(d_self);
1377
1378 if (!d_self->in_file)
1379 return TRUE;
1380
1381 g_mutex_lock(d_self->device_mutex);
1382 d_self->in_file = FALSE;
1383 g_mutex_unlock(d_self->device_mutex);
1384
1385 if (device_in_error(d_self)) return FALSE;
1386
1387 if (!tape_weof(self->fd, 1)) {
1388 device_set_error(d_self,
1389 g_strdup_printf(_("Error writing filemark: %s"), strerror(errno)),
1390 DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
1391 /* can't tell if this was EOM or not, so assume it is */
1392 d_self->is_eom = TRUE;
1393 return FALSE;
1394 }
1395
1396 return TRUE;
1397 }
1398
1399 static dumpfile_t *
1400 tape_device_seek_file (Device * d_self, guint file) {
1401 TapeDevice * self;
1402 gint got_file;
1403 int difference;
1404 char * header_buffer;
1405 dumpfile_t * rval;
1406 int buffer_len;
1407 IoResult result;
1408 char *msg;
1409
1410 self = TAPE_DEVICE(d_self);
1411
1412 if (device_in_error(self)) return NULL;
1413
1414 difference = file - d_self->file;
1415
1416 /* Check if we already read a filemark. */
1417 /* If we already read a filemark and the drive automaticaly goes to the
1418 next file, then we must reduce the difference by one. */
1419 if (d_self->is_eof && !self->fsf_after_filemark) {
1420 difference --;
1421 }
1422
1423 d_self->is_eof = FALSE;
1424 d_self->block = 0;
1425 g_mutex_lock(d_self->device_mutex);
1426 d_self->in_file = FALSE;
1427 d_self->bytes_read = 0;
1428 g_mutex_unlock(d_self->device_mutex);
1429
1430 reseek:
1431 if (difference > 0) {
1432 /* Seeking forwards */
1433 if (!tape_device_fsf(self, difference)) {
1434 tape_rewind(self->fd);
1435 device_set_error(d_self,
1436 g_strdup_printf(_("Could not seek forward to file %d"), file),
1437 DEVICE_STATUS_VOLUME_ERROR | DEVICE_STATUS_DEVICE_ERROR);
1438 return NULL;
1439 }
1440 } else { /* (difference <= 0) */
1441 /* Seeking backwards, or to this file itself */
1442
1443 /* if the drive supports bsf, we can do this the fancy way */
1444 if (self->bsf) {
1445 /* bsf one more than the difference */
1446 if (!tape_bsf(self->fd, -difference + 1)) {
1447 tape_rewind(self->fd);
1448 device_set_error(d_self,
1449 g_strdup_printf(_("Could not seek backward to file %d"), file),
1450 DEVICE_STATUS_VOLUME_ERROR | DEVICE_STATUS_DEVICE_ERROR);
1451 return NULL;
1452 }
1453
1454 /* now we are on the BOT side of the desired filemark, so FSF to get to the
1455 * EOT side of it */
1456 if (!tape_device_fsf(self, 1)) {
1457 tape_rewind(self->fd);
1458 device_set_error(d_self,
1459 g_strdup_printf(_("Could not seek forward to file %d"), file),
1460 DEVICE_STATUS_VOLUME_ERROR | DEVICE_STATUS_DEVICE_ERROR);
1461 return NULL;
1462 }
1463 } else {
1464 /* no BSF, so just rewind and seek forward */
1465 if (!tape_rewind(self->fd)) {
1466 device_set_error(d_self,
1467 g_strdup(_("Could not rewind device while emulating BSF")),
1468 DEVICE_STATUS_VOLUME_ERROR | DEVICE_STATUS_DEVICE_ERROR);
1469 return FALSE;
1470 }
1471
1472 if (!tape_device_fsf(self, file)) {
1473 tape_rewind(self->fd);
1474 device_set_error(d_self,
1475 g_strdup_printf(_("Could not seek forward to file %d"), file),
1476 DEVICE_STATUS_VOLUME_ERROR | DEVICE_STATUS_DEVICE_ERROR);
1477 return NULL;
1478 }
1479 }
1480 }
1481
1482 /* double-check that we're on the right fileno, if possible. This is most
1483 * likely a programming error if it occurs, but could also be due to a weird
1484 * tape drive or driver (and that would *never* happen, right?) */
1485 got_file = tape_fileno(self->fd);
1486 if (got_file >= 0 && (guint)got_file != file) {
1487 device_set_error(d_self,
1488 g_strdup_printf(_("Could not seek to file %d correctly; got %d"),
1489 file, got_file),
1490 DEVICE_STATUS_DEVICE_ERROR);
1491 d_self->file = (guint)got_file;
1492 return NULL;
1493 }
1494
1495 buffer_len = tape_device_read_size(d_self);
1496 header_buffer = g_try_malloc(buffer_len);
1497 if (header_buffer == NULL) {
1498 device_set_error(d_self,
1499 g_strdup(_("failed to allocate memory")),
1500 DEVICE_STATUS_DEVICE_ERROR);
1501 return NULL;
1502 }
1503
1504 d_self->is_eof = FALSE;
1505 result = tape_device_robust_read(self, header_buffer, &buffer_len, &msg);
1506
1507 if (result != RESULT_SUCCESS) {
1508 free(header_buffer);
1509 tape_rewind(self->fd);
1510 switch (result) {
1511 case RESULT_NO_DATA:
1512 /* If we read 0 bytes, that means we encountered a double
1513 * filemark, which indicates end of tape. This should
1514 * work even with QIC tapes on operating systems with
1515 * proper support. */
1516 d_self->file = file; /* other attributes are already correct */
1517 return make_tapeend_header();
1518
1519 case RESULT_SMALL_BUFFER:
1520 msg = g_strdup(_("block size too small"));
1521 break;
1522
1523 default:
1524 msg = g_strdup(_("unknown error"));
1525 case RESULT_ERROR:
1526 break;
1527 }
1528 device_set_error(d_self,
1529 g_strdup_printf(_("Error reading Amanda header: %s"), msg),
1530 DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
1531 amfree(msg);
1532 return NULL;
1533 }
1534
1535 if (buffer_len < 32768) {
1536 device_set_error(d_self,
1537 g_strdup_printf(_("header is too small: %d bytes"), buffer_len),
1538 DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
1539 free(header_buffer);
1540 return NULL;
1541 }
1542
1543 rval = g_new(dumpfile_t, 1);
1544 parse_file_header(header_buffer, rval, buffer_len);
1545 amfree(header_buffer);
1546 switch (rval->type) {
1547 case F_DUMPFILE:
1548 case F_CONT_DUMPFILE:
1549 case F_SPLIT_DUMPFILE:
1550 break;
1551
1552 case F_NOOP:
1553 /* a NOOP is written on QIC tapes to avoid writing two sequential
1554 * filemarks when closing a device in WRITE or APPEND mode. In this
1555 * case, we just seek to the next file. */
1556 amfree(rval);
1557 file++;
1558 difference = 1;
1559 goto reseek;
1560
1561 default:
1562 tape_rewind(self->fd);
1563 device_set_error(d_self,
1564 g_strdup(_("Invalid amanda header while reading file header")),
1565 DEVICE_STATUS_VOLUME_ERROR);
1566 amfree(rval);
1567 return NULL;
1568 }
1569
1570 g_mutex_lock(d_self->device_mutex);
1571 d_self->in_file = TRUE;
1572 g_mutex_unlock(d_self->device_mutex);
1573 d_self->file = file;
1574
1575 return rval;
1576 }
1577
1578 static gboolean
1579 tape_device_seek_block (Device * d_self, guint64 block) {
1580 TapeDevice * self;
1581 int difference;
1582
1583 self = TAPE_DEVICE(d_self);
1584
1585 if (device_in_error(self)) return FALSE;
1586
1587 difference = block - d_self->block;
1588
1589 if (difference > 0) {
1590 if (!tape_device_fsr(self, difference)) {
1591 device_set_error(d_self,
1592 g_strdup_printf(_("Could not seek forward to block %ju: %s"), (uintmax_t)block, strerror(errno)),
1593 DEVICE_STATUS_VOLUME_ERROR | DEVICE_STATUS_DEVICE_ERROR);
1594 return FALSE;
1595 }
1596 } else if (difference < 0) {
1597 if (!tape_device_bsr(self, difference, d_self->file, d_self->block)) {
1598 device_set_error(d_self,
1599 g_strdup_printf(_("Could not seek backward to block %ju: %s"), (uintmax_t)block, strerror(errno)),
1600 DEVICE_STATUS_VOLUME_ERROR | DEVICE_STATUS_DEVICE_ERROR);
1601 return FALSE;
1602 }
1603 }
1604
1605 d_self->block = block;
1606 return TRUE;
1607 }
1608
1609 static gboolean
1610 tape_device_eject (Device * d_self) {
1611 TapeDevice * self;
1612 gboolean eject_opened_tape = FALSE;
1613 gboolean result = FALSE;
1614
1615 self = TAPE_DEVICE(d_self);
1616
1617 if (device_in_error(self)) return FALSE;
1618
1619 /* Open the device if not already opened */
1620 if (self->fd == -1) {
1621 self->fd = try_open_tape_device(self, self->private->device_filename);
1622 /* if the open failed, then try_open_tape_device already set the
1623 * approppriate error status */
1624 if (self->fd == -1)
1625 return FALSE;
1626 eject_opened_tape = TRUE;
1627 }
1628
1629 /* Rewind it. */
1630 if (!tape_rewind(self->fd)) {
1631 device_set_error(d_self,
1632 g_strdup_printf(_("Error rewinding device %s before ejecting: %s"),
1633 self->private->device_filename, strerror(errno)),
1634 DEVICE_STATUS_DEVICE_ERROR
1635 | DEVICE_STATUS_VOLUME_ERROR);
1636 result = FALSE;
1637 goto eject_finish;
1638 }
1639
1640 if (tape_offl(self->fd)) {
1641 result = TRUE;
1642 goto eject_finish;
1643 }
1644
1645 device_set_error(d_self,
1646 g_strdup_printf(_("Error ejecting device %s: %s\n"),
1647 self->private->device_filename, strerror(errno)),
1648 DEVICE_STATUS_DEVICE_ERROR);
1649
1650 eject_finish:
1651 if (eject_opened_tape) {
1652 device_finish(d_self);
1653 }
1654 return result;
1655 }
1656
1657 static gboolean
1658 tape_device_finish (Device * d_self) {
1659 TapeDevice * self;
1660 char *msg = NULL;
1661
1662 self = TAPE_DEVICE(d_self);
1663
1664 if (device_in_error(self))
1665 goto finish_error;
1666
1667 /* if we're already in ACCESS_NULL, then there are no filemarks or anything
1668 * to worry about, but we need to release the kernel device */
1669 if (d_self->access_mode == ACCESS_NULL) {
1670 robust_close(self->fd);
1671 self->fd = -1;
1672 return TRUE;
1673 }
1674
1675 /* Polish off this file, if relevant. */
1676 g_mutex_lock(d_self->device_mutex);
1677 if (d_self->in_file && IS_WRITABLE_ACCESS_MODE(d_self->access_mode)) {
1678 g_mutex_unlock(d_self->device_mutex);
1679 if (!device_finish_file(d_self)) {
1680 goto finish_error;
1681 }
1682 } else {
1683 g_mutex_unlock(d_self->device_mutex);
1684 }
1685
1686 /* Straighten out the filemarks. We already wrote one in finish_file, and
1687 * the device driver will write another filemark when we rewind. This means
1688 * that, if we do nothing, we'll get two filemarks. If final_filemarks is
1689 * 1, this would be wrong, so in this case we insert a F_NOOP header between
1690 * the two filemarks. */
1691 if (self->final_filemarks == 1 &&
1692 IS_WRITABLE_ACCESS_MODE(d_self->access_mode)) {
1693 dumpfile_t file;
1694 char *header;
1695 int result;
1696
1697 /* write a F_NOOP header */
1698 fh_init(&file);
1699 file.type = F_NOOP;
1700 header = device_build_amanda_header(d_self, &file, NULL);
1701 if (!header) {
1702 device_set_error(d_self,
1703 g_strdup(_("Amanda file header won't fit in a single block!")),
1704 DEVICE_STATUS_DEVICE_ERROR);
1705 goto finish_error;
1706 }
1707
1708 result = tape_device_robust_write(self, header, d_self->block_size, &msg);
1709 if (result != RESULT_SUCCESS) {
1710 device_set_error(d_self,
1711 g_strdup_printf(_("Error writing file header: %s"),
1712 (result == RESULT_ERROR)? msg : _("out of space")),
1713 DEVICE_STATUS_DEVICE_ERROR);
1714 amfree(header);
1715 amfree(msg);
1716 goto finish_error;
1717 }
1718 amfree(header);
1719 }
1720
1721 /* Rewind (the kernel will write a filemark first) */
1722 if (!tape_rewind(self->fd)) {
1723 device_set_error(d_self,
1724 g_strdup_printf(_("Couldn't rewind device to finish: %s"), strerror(errno)),
1725 DEVICE_STATUS_DEVICE_ERROR);
1726 goto finish_error;
1727 }
1728
1729 d_self->is_eof = FALSE;
1730 d_self->access_mode = ACCESS_NULL;
1731
1732 /* release the kernel's device */
1733 robust_close(self->fd);
1734 self->fd = -1;
1735
1736 return TRUE;
1737
1738 finish_error:
1739 d_self->access_mode = ACCESS_NULL;
1740
1741 /* release the kernel's device */
1742 robust_close(self->fd);
1743 self->fd = -1;
1744
1745 return FALSE;
1746 }
1747
1748 static gboolean
1749 tape_device_check_writable(Device * d_self)
1750 {
1751 TapeDevice *self;
1752 #ifdef GMT_WR_PROT
1753 struct mtget get;
1754 #endif
1755
1756 self = TAPE_DEVICE(d_self);
1757
1758 if (device_in_error(self))
1759 return TRUE;
1760
1761 #ifdef GMT_WR_PROT
1762 if (ioctl(self->fd, MTIOCGET, &get) == 0) {
1763 if (GMT_WR_PROT(get.mt_gstat)) {
1764 device_set_error(d_self,
1765 g_strdup_printf("Device '%s' is write protected", self->private->device_filename),
1766 DEVICE_STATUS_VOLUME_ERROR);
1767 return FALSE;
1768 }
1769 } else {
1770 device_set_error(d_self,
1771 g_strdup_printf("ioctl failed on device '%s'", self->private->device_filename),
1772 DEVICE_STATUS_VOLUME_ERROR);
1773 return FALSE;
1774 }
1775 #endif
1776
1777 return TRUE;
1778 }
1779
1780 /* Works just like read(), except for the following:
1781 * 1) Retries on EINTR & friends.
1782 * 2) Stores count in parameter, not return value.
1783 * 3) Provides explicit return result.
1784 * *errmsg is only set on RESULT_ERROR.
1785 */
1786 static IoResult
1787 tape_device_robust_read (TapeDevice * self, void * buf, int * count, char **errmsg) {
1788 Device * d_self;
1789 int result;
1790
1791 d_self = (Device*)self;
1792
1793 /* Callers should ensure this. */
1794 g_assert(*count >= 0);
1795
1796 for (;;) {
1797 result = read(self->fd, buf, *count);
1798 if (result > 0) {
1799 /* Success. By definition, we read a full block. */
1800 d_self->is_eof = FALSE;
1801 *count = result;
1802 return RESULT_SUCCESS;
1803 } else if (result == 0) {
1804 d_self->is_eof = TRUE;
1805 return RESULT_NO_DATA;
1806 } else {
1807 if (0
1808 #ifdef EAGAIN
1809 || errno == EAGAIN
1810 #endif
1811 #ifdef EWOULDBLOCK
1812 || errno == EWOULDBLOCK
1813 #endif
1814 #ifdef EINTR
1815 || errno == EINTR
1816 #endif
1817 ) {
1818 /* Interrupted system call */
1819 continue;
1820 } else if ((0
1821 #ifdef ENOMEM
1822 || errno == ENOMEM /* bad user-space buffer */
1823 #endif
1824 #ifdef EOVERFLOW
1825 || errno == EOVERFLOW /* bad kernel-space buffer */
1826 #endif
1827 #ifdef EINVAL
1828 || errno == EINVAL /* ??? */
1829 #endif
1830 )) {
1831 /* Buffer too small. */
1832 g_warning("Buffer is too small (%d bytes) from %s: %s",
1833 *count, self->private->device_filename, strerror(errno));
1834 return RESULT_SMALL_BUFFER;
1835 } else {
1836 *errmsg = g_strdup_printf(_("Error reading %d bytes from %s: %s"),
1837 *count, self->private->device_filename, strerror(errno));
1838 return RESULT_ERROR;
1839 }
1840 }
1841 }
1842
1843 g_assert_not_reached();
1844 }
1845
1846 /* Kernel workaround: If needed, poke the kernel so it doesn't fail.
1847 at the 2GB boundry. Parameters are G_GNUC_UNUSED in case NEED_RESETOFS
1848 is not defined. */
1849 static void check_resetofs(TapeDevice * self G_GNUC_UNUSED,
1850 int count G_GNUC_UNUSED) {
1851 #ifdef NEED_RESETOFS
1852 Device * d_self;
1853 int result;
1854
1855 d_self = (Device*)self;
1856
1857 self->private->write_count += count;
1858 if (self->private->write_count < RESETOFS_THRESHOLD) {
1859 return;
1860 }
1861
1862 result = lseek(self->fd, 0, SEEK_SET);
1863 if (result < 0) {
1864 g_warning(_("lseek() failed during kernel 2GB workaround: %s"),
1865 strerror(errno));
1866 }
1867 #endif
1868 }
1869
1870 /* *errmsg is only set on RESULT_ERROR */
1871 static IoResult
1872 tape_device_robust_write (TapeDevice * self, void * buf, int count, char **errmsg) {
1873 Device * d_self;
1874 int result;
1875 gboolean retry = FALSE;
1876
1877 d_self = (Device*)self;
1878
1879 check_resetofs(self, count);
1880
1881 for (;;) {
1882 result = write(self->fd, buf, count);
1883
1884 /* Success. */
1885 if (result == count)
1886 return RESULT_SUCCESS;
1887
1888 if (result > 0) {
1889 /* write() returned a short count. This should not happen if the block sizes
1890 * are properly aligned. */
1891 *errmsg = g_strdup_printf("Short write on tape device: Tried %d, got %d. Is "
1892 "the drive using a block size smaller than %d bytes?",
1893 count, result, count);
1894 return RESULT_ERROR;
1895 }
1896
1897 /* Detect LEOM (early warning) and handle properly
1898 *
1899 * FreeBSD: 0-length write; next write will succeed
1900 * http://lists.freebsd.org/pipermail/freebsd-scsi/2010-June/004414.html
1901 *
1902 * Solaris: 0-length write; next write will succeed
1903 * (from Matthew Jacob on FreeBSD thread)
1904 *
1905 * Linux: -1/ENOSPC; next write will succeed
1906 * http://www.mjmwired.net/kernel/Documentation/scsi/st.txt
1907 *
1908 * HP/UX: -1/ENOSPC; next write will succeed
1909 * http://www.adssasia.com/Manual/IBM%203581%20tape%20autoloader.pdf
1910 */
1911 if (result == 0
1912 #ifdef ENOSPC
1913 || (result < 0 && errno == ENOSPC)
1914 #endif
1915 ) {
1916 /* if we've retried once already, then we're probably really out of space */
1917 if (retry || !self->leom)
1918 return RESULT_NO_SPACE;
1919 retry = TRUE;
1920 d_self->is_eom = TRUE;
1921
1922 g_debug("empty write to tape; treating as LEOM early warning and retrying");
1923 continue;
1924 }
1925
1926 /* at this point result < 0, so an error occurred - sort out what */
1927
1928 if (0
1929 #ifdef EAGAIN
1930 || errno == EAGAIN
1931 #endif
1932 #ifdef EWOULDBLOCK
1933 || errno == EWOULDBLOCK
1934 #endif
1935 #ifdef EINTR
1936 || errno == EINTR
1937 #endif
1938 ) {
1939 /* Interrupted system call */
1940 continue;
1941 } else if (0
1942 #ifdef ENOSPC
1943 || errno == ENOSPC
1944 #endif
1945 #ifdef EIO
1946 || errno == EIO
1947 #endif
1948 ) {
1949 /* Probably EOT. Print a message if we got EIO. */
1950 #ifdef EIO
1951 if (errno == EIO) {
1952 g_warning(_("Got EIO on %s, assuming end of tape"),
1953 self->private->device_filename);
1954 }
1955 #endif
1956 return RESULT_NO_SPACE;
1957 } else if (errno == EPERM) {
1958 *errmsg = g_strdup_printf("write to the '%s' device failed: %s, maybe the tab on the tape is set at the read-only possition?",
1959 self->private->device_filename, strerror(errno));
1960 return RESULT_ERROR;
1961 } else {
1962 /* WTF */
1963 *errmsg = g_strdup_printf("write to the '%s' device failed: %s",
1964 strerror(errno), self->private->device_filename);
1965 return RESULT_ERROR;
1966 }
1967 }
1968
1969 g_assert_not_reached();
1970 }
1971
1972 /* Reads some number of tape blocks into the bit-bucket. If the count
1973 is negative, then we read the rest of the entire file. Returns the
1974 number of blocks read, or -1 if an error occured. If we encounter
1975 EOF (as opposed to some other error) we return the number of blocks
1976 actually read. */
1977 static int drain_tape_blocks(TapeDevice * self, int count) {
1978 char * buffer;
1979 gsize buffer_size;
1980 int i;
1981
1982 buffer_size = tape_device_read_size(self);
1983
1984 buffer = malloc(buffer_size);
1985
1986 for (i = 0; i < count || count < 0;) {
1987 int result;
1988
1989 result = read(self->fd, buffer, buffer_size);
1990 if (result > 0) {
1991 i ++;
1992 continue;
1993 } else if (result == 0) {
1994 amfree(buffer);
1995 return i;
1996 } else {
1997 /* First check for interrupted system call. */
1998 if (0
1999 #ifdef EAGAIN
2000 || errno == EAGAIN
2001 #endif
2002 #ifdef EWOULDBLOCK
2003 || errno == EWOULDBLOCK
2004 #endif
2005 #ifdef EINTR
2006 || errno == EINTR
2007 #endif
2008 ) {
2009 /* Interrupted system call */
2010 continue;
2011 } else if (0
2012 #ifdef ENOSPC
2013 || errno == ENOSPC /* bad user-space buffer */
2014 #endif
2015 #ifdef EOVERFLOW
2016 || errno == EOVERFLOW /* bad kernel-space buffer */
2017 #endif
2018 #ifdef EINVAL
2019 || errno == EINVAL /* ??? */
2020 #endif
2021 ) {
2022 /* The buffer may not be big enough. But the OS is not
2023 100% clear. We double the buffer and try again, but
2024 in no case allow a buffer bigger than 32 MB. */
2025 buffer_size *= 2;
2026
2027 if (buffer_size > 32*1024*1024) {
2028 amfree(buffer);
2029 return -1;
2030 } else {
2031 char *new_buffer = realloc(buffer, buffer_size);
2032 if (new_buffer == NULL) {
2033 amfree(buffer);
2034 return -1;
2035 }
2036 buffer = new_buffer;
2037 continue;
2038 }
2039 }
2040 }
2041 }
2042
2043 amfree(buffer);
2044 return count;
2045 }
2046
2047 static gboolean
2048 tape_device_fsf (TapeDevice * self, guint count) {
2049 if (self->fsf) {
2050 return tape_fsf(self->fd, count);
2051 } else {
2052 guint i;
2053 for (i = 0; i < count; i ++) {
2054 if (drain_tape_blocks(self, -1) < 0)
2055 return FALSE;
2056 }
2057 return TRUE;
2058 }
2059 }
2060
2061
2062 static gboolean
2063 tape_device_fsr (TapeDevice * self, guint count) {
2064 if (self->fsr) {
2065 return tape_fsr(self->fd, count);
2066 } else {
2067 int result = drain_tape_blocks(self, count);
2068 return result > 0 && (int)count == result;
2069 }
2070 }
2071
2072 /* Seek back the given number of blocks to block number block within
2073 * the current file, numbered file. */
2074
2075 static gboolean
2076 tape_device_bsr (TapeDevice * self, guint count, guint file, guint block) {
2077 if (self->bsr) {
2078 return tape_bsr(self->fd, count);
2079 } else if (self->bsf && self->fsf) {
2080 /* BSF, FSF to the right side of the filemark, and then FSR. */
2081 if (!tape_bsf(self->fd, 1))
2082 return FALSE;
2083
2084 if (!tape_fsf(self->fd, 1))
2085 return FALSE;
2086
2087 return tape_device_fsr(self, block);
2088 } else {
2089 /* rewind, FSF, and FSR */
2090 if (!tape_rewind(self->fd))
2091 return FALSE;
2092
2093 if (!tape_device_fsf(self, file))
2094 return FALSE;
2095
2096 return tape_device_fsr(self, block);
2097 }
2098 g_assert_not_reached();
2099 }
2100
2101 /* Go to the right place to write more data, and update the file
2102 number if possible. */
2103 static gboolean
2104 tape_device_eod (TapeDevice * self) {
2105 Device * d_self;
2106 int count;
2107
2108 d_self = (Device*)self;
2109
2110 if (self->eom) {
2111 int result;
2112 result = tape_eod(self->fd);
2113 if (result == TAPE_OP_ERROR) {
2114 return FALSE;
2115 } else if (result != TAPE_POSITION_UNKNOWN) {
2116 /* great - we just fast-forwarded to EOD, but don't know where we are, so
2117 * now we have to rewind and drain all of that data. Warn the user so that
2118 * we can skip the fast-forward-rewind stage on the next run */
2119 g_warning("Seek to end of tape does not give an accurate tape position; set "
2120 "the EOM property to 0 to avoid useless tape movement.");
2121 /* and set the property so that next time *this* object is opened for
2122 * append, we skip this stage */
2123 self->eom = FALSE;
2124 /* fall through to draining blocks, below */
2125 } else {
2126 /* We drop by 1 because Device will increment the first
2127 time the user does start_file. */
2128 d_self->file = result - 1;
2129 return TRUE;
2130 }
2131 }
2132
2133 if (!tape_rewind(self->fd))
2134 return FALSE;
2135
2136 count = 0;
2137 for (;;) {
2138 /* We alternately read a block and FSF. If the read is
2139 successful, then we are not there yet and should FSF
2140 again. */
2141 int result;
2142 result = drain_tape_blocks(self, 1);
2143 if (result == 1) {
2144 /* More data, FSF. */
2145 tape_device_fsf(self, 1);
2146 count ++;
2147 } else if (result == 0) {
2148 /* Finished. */
2149 d_self->file = count - 1;
2150 return TRUE;
2151 } else {
2152 return FALSE;
2153 }
2154 }
2155 }
2156
2157 static Device *
2158 tape_device_factory (char * device_name, char * device_type, char * device_node) {
2159 Device * rval;
2160 g_assert(g_str_equal(device_type, "tape"));
2161 rval = DEVICE(g_object_new(TYPE_TAPE_DEVICE, NULL));
2162 device_open_device(rval, device_name, device_type, device_node);
2163 return rval;
2164 }
2165
2166 /*
2167 * Tape Operations using the POSIX interface
2168 */
2169
2170 /* Having one name for every operation would be too easy. */
2171 #if !defined(MTCOMPRESSION) && defined(MTCOMP)
2172 # define MTCOMPRESSION MTCOMP
2173 #endif
2174
2175 #if !defined(MTSETBLK) && defined(MTSETBSIZ)
2176 # define MTSETBLK MTSETBSIZ
2177 #endif
2178
2179 #if !defined(MTEOM) && defined(MTEOD)
2180 # define MTEOM MTEOD
2181 #endif
2182
2183 gboolean tape_rewind(int fd) {
2184 int count = 5;
2185 time_t stop_time;
2186
2187 /* We will retry this for up to 30 seconds or 5 retries,
2188 whichever is less, because some hardware/software combinations
2189 (notably EXB-8200 on FreeBSD) can fail to rewind. */
2190 stop_time = time(NULL) + 30;
2191
2192 while (--count >= 0 && time(NULL) < stop_time) {
2193 struct mtop mt;
2194 mt.mt_op = MTREW;
2195 mt.mt_count = 1;
2196
2197 if (0 == ioctl(fd, MTIOCTOP, &mt))
2198 return TRUE;
2199
2200 sleep(3);
2201 }
2202
2203 return FALSE;
2204 }
2205
2206 gboolean tape_fsf(int fd, guint count) {
2207 struct mtop mt;
2208 mt.mt_op = MTFSF;
2209 mt.mt_count = count;
2210 return 0 == ioctl(fd, MTIOCTOP, &mt);
2211 }
2212
2213 gboolean tape_bsf(int fd, guint count) {
2214 struct mtop mt;
2215 mt.mt_op = MTBSF;
2216 mt.mt_count = count;
2217 return 0 == ioctl(fd, MTIOCTOP, &mt);
2218 }
2219
2220 gboolean tape_fsr(int fd, guint count) {
2221 struct mtop mt;
2222 mt.mt_op = MTFSR;
2223 mt.mt_count = count;
2224 return 0 == ioctl(fd, MTIOCTOP, &mt);
2225 }
2226
2227 gboolean tape_bsr(int fd, guint count) {
2228 struct mtop mt;
2229 mt.mt_op = MTBSR;
2230 mt.mt_count = count;
2231 return 0 == ioctl(fd, MTIOCTOP, &mt);
2232 }
2233
2234 gint tape_fileno(int fd) {
2235 struct mtget get;
2236
2237 if (0 != ioctl(fd, MTIOCGET, &get))
2238 return TAPE_POSITION_UNKNOWN;
2239 if (get.mt_fileno < 0)
2240 return TAPE_POSITION_UNKNOWN;
2241 else
2242 return get.mt_fileno;
2243 }
2244
2245 gint tape_eod(int fd) {
2246 struct mtop mt;
2247 struct mtget get;
2248 mt.mt_op = MTEOM;
2249 mt.mt_count = 1;
2250 if (0 != ioctl(fd, MTIOCTOP, &mt))
2251 return TAPE_OP_ERROR;
2252
2253 /* Ignored result. This is just to flush buffers. */
2254 mt.mt_op = MTNOP;
2255 (void)ioctl(fd, MTIOCTOP, &mt);
2256
2257 if (0 != ioctl(fd, MTIOCGET, &get))
2258 return TAPE_POSITION_UNKNOWN;
2259 if (get.mt_fileno < 0)
2260 return TAPE_POSITION_UNKNOWN;
2261 else
2262 return get.mt_fileno;
2263 }
2264
2265 gboolean tape_weof(int fd, guint8 count) {
2266 struct mtop mt;
2267 mt.mt_op = MTWEOF;
2268 mt.mt_count = count;
2269 return 0 == ioctl(fd, MTIOCTOP, &mt);
2270 }
2271
2272 gboolean tape_setcompression(int fd G_GNUC_UNUSED,
2273 gboolean on G_GNUC_UNUSED) {
2274 #ifdef MTCOMPRESSION
2275 struct mtop mt;
2276 mt.mt_op = MTCOMPRESSION;
2277 mt.mt_count = on;
2278 return 0 == ioctl(fd, MTIOCTOP, &mt);
2279 #else
2280 return 0;
2281 #endif
2282 }
2283
2284 gboolean tape_offl(int fd) {
2285 struct mtop mt;
2286 int safe_errno;
2287
2288 mt.mt_op = MTOFFL;
2289 mt.mt_count = 1;
2290 if (0 == ioctl(fd, MTIOCTOP, &mt))
2291 return TRUE;
2292
2293 safe_errno = errno;
2294 g_debug("tape_off: ioctl(MTIOCTOP/MTOFFL) failed: %s", strerror(errno));
2295 errno = safe_errno;
2296
2297 return FALSE;
2298 }
2299
2300 DeviceStatusFlags tape_is_tape_device(int fd) {
2301 struct mtop mt;
2302 mt.mt_op = MTNOP;
2303 mt.mt_count = 1;
2304 if (0 == ioctl(fd, MTIOCTOP, &mt)) {
2305 return DEVICE_STATUS_SUCCESS;
2306 #ifdef ENOMEDIUM
2307 } else if (errno == ENOMEDIUM) {
2308 return DEVICE_STATUS_VOLUME_MISSING;
2309 #endif
2310 } else {
2311 g_debug("tape_is_tape_device: ioctl(MTIOCTOP/MTNOP) failed: %s",
2312 strerror(errno));
2313 if (errno == EIO) {
2314 /* some devices return EIO while the drive is busy loading */
2315 return DEVICE_STATUS_DEVICE_ERROR|DEVICE_STATUS_DEVICE_BUSY;
2316 } else {
2317 return DEVICE_STATUS_DEVICE_ERROR;
2318 }
2319 }
2320 }
2321
2322 DeviceStatusFlags get_tape_blocksize(int fd, guint64 *tape_blocksize) {
2323 struct mtget status;
2324
2325 if (ioctl(fd, MTIOCGET, (char *)&status) < 0) {
2326 g_debug("get_tape_blocksize: ioctl(MTIOCGET) failed: %s",
2327 strerror(errno));
2328 *tape_blocksize = -1;
2329 return DEVICE_STATUS_DEVICE_ERROR;
2330 }
2331
2332 *tape_blocksize = 0;
2333 #if defined MT_ST_BLKSIZE_MASK && defined MT_ST_BLKSIZE_SHIFT
2334 if (
2335 #ifdef MT_ISSCSI1
2336 status.mt_type == MT_ISSCSI1 ||
2337 #endif
2338 #ifdef MT_ISSCSI2
2339 status.mt_type == MT_ISSCSI2 ||
2340 #endif
2341 0) {
2342 *tape_blocksize = ((status.mt_dsreg & MT_ST_BLKSIZE_MASK) >> MT_ST_BLKSIZE_SHIFT);
2343 }
2344 #endif
2345 return DEVICE_STATUS_SUCCESS;
2346 }
2347
2348 DeviceStatusFlags tape_is_ready(int fd, TapeDevice *t_self G_GNUC_UNUSED) {
2349 struct mtget get;
2350 if (0 == ioctl(fd, MTIOCGET, &get)) {
2351 #if defined(GMT_ONLINE) || defined(GMT_DR_OPEN)
2352 if (1
2353 #ifdef GMT_ONLINE
2354 && (t_self->broken_gmt_online || GMT_ONLINE(get.mt_gstat))
2355 #endif
2356 #ifdef GMT_DR_OPEN
2357 && !GMT_DR_OPEN(get.mt_gstat)
2358 #endif
2359 ) {
2360 return DEVICE_STATUS_SUCCESS;
2361 } else {
2362 return DEVICE_STATUS_VOLUME_MISSING;
2363 }
2364 #else /* Neither macro is defined. */
2365 return DEVICE_STATUS_SUCCESS;
2366 #endif
2367 } else {
2368 return DEVICE_STATUS_VOLUME_ERROR;
2369 }
2370 }
2371