70e948002f4f72b4fa8cc199f44c53eb4bb0d50f
[debian/amanda] / device-src / tape-posix.c
1 /*
2  * Copyright (c) 2005 Zmanda, Inc.  All Rights Reserved.
3  * 
4  * This library is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 2.1 as 
6  * published by the Free Software Foundation.
7  * 
8  * This library 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 Lesser General Public
11  * License for more details.
12  * 
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, write to the Free Software Foundation,
15  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA.
16  * 
17  * Contact information: Zmanda Inc., 505 N Mathlida Ave, Suite 120
18  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
19  */
20
21 #include "amanda.h"
22 #include "util.h"
23 #include "tape-ops.h"
24 #include "property.h"
25 #include <glob.h>
26
27 /* Having one name for every operation would be too easy. */
28 #if !defined(MTCOMPRESSION) && defined(MTCOMP)
29 # define MTCOMPRESSION MTCOMP
30 #endif
31
32 #if !defined(MTSETBLK) && defined(MTSETBSIZ)
33 # define MTSETBLK MTSETBSIZ
34 #endif
35
36 #if !defined(MTEOM) && defined(MTEOD)
37 # define MTEOM MTEOD
38 #endif
39
40 #ifdef HAVE_LIMITS_H
41 # include <limits.h>
42 #endif
43
44 gboolean tape_rewind(int fd) {
45     int count = 5;
46     time_t stop_time;
47
48     /* We will retry this for up to 30 seconds or 5 retries,
49        whichever is less, because some hardware/software combinations
50        (notably EXB-8200 on FreeBSD) can fail to rewind. */
51     stop_time = time(NULL) + 30;
52
53     while (--count >= 0 && time(NULL) < stop_time) {
54         struct mtop mt;
55         mt.mt_op = MTREW;
56         mt.mt_count = 1;
57
58         if (0 == ioctl(fd, MTIOCTOP, &mt))
59             return TRUE;
60
61         sleep(3);
62     }
63
64     return FALSE;
65 }
66
67 gboolean tape_fsf(int fd, guint count) {
68     struct mtop mt;
69     mt.mt_op = MTFSF;
70     mt.mt_count = count;
71     return 0 == ioctl(fd, MTIOCTOP, &mt);
72 }
73
74 gboolean tape_bsf(int fd, guint count) {
75     struct mtop mt;
76     mt.mt_op = MTBSF;
77     mt.mt_count = count;
78     return 0 == ioctl(fd, MTIOCTOP, &mt);
79 }
80
81 gboolean tape_fsr(int fd, guint count) {
82     struct mtop mt;
83     mt.mt_op = MTFSR;
84     mt.mt_count = count;
85     return 0 == ioctl(fd, MTIOCTOP, &mt);
86 }
87
88 gboolean tape_bsr(int fd, guint count) {
89     struct mtop mt;
90     mt.mt_op = MTBSR;
91     mt.mt_count = count;
92     return 0 == ioctl(fd, MTIOCTOP, &mt);
93 }
94
95 gint tape_eod(int fd) {
96     struct mtop mt;
97     struct mtget get;
98     mt.mt_op = MTEOM;
99     mt.mt_count = 1;
100     if (0 != ioctl(fd, MTIOCTOP, &mt))
101         return TAPE_OP_ERROR;
102
103     /* Ignored result. This is just to flush buffers. */
104     mt.mt_op = MTNOP;
105     ioctl(fd, MTIOCTOP, &mt);
106
107     if (0 != ioctl(fd, MTIOCGET, &get))
108         return TAPE_POSITION_UNKNOWN;
109     if (get.mt_fileno < 0)
110         return TAPE_POSITION_UNKNOWN;
111     else
112         return get.mt_fileno;
113 }
114
115 gboolean tape_weof(int fd, guint8 count) {
116     struct mtop mt;
117     mt.mt_op = MTWEOF;
118     mt.mt_count = count;
119     return 0 == ioctl(fd, MTIOCTOP, &mt);
120 }
121
122 gboolean tape_setcompression(int fd G_GNUC_UNUSED, 
123         gboolean on G_GNUC_UNUSED) {
124 #ifdef MTCOMPRESSION
125     struct mtop mt;
126     mt.mt_op = MTCOMPRESSION;
127     mt.mt_count = on;
128     return 0 == ioctl(fd, MTIOCTOP, &mt);
129 #else
130     return 0;
131 #endif
132 }
133
134 ReadLabelStatusFlags tape_is_tape_device(int fd) {
135     struct mtop mt;
136     mt.mt_op = MTNOP;
137     mt.mt_count = 1;
138     if (0 == ioctl(fd, MTIOCTOP, &mt)) {
139         return READ_LABEL_STATUS_SUCCESS;
140     } else {
141         dbprintf("tape_is_tape_device: ioctl(MTIOCTOP/MTNOP) failed: %s",
142                  strerror(errno));
143         if (errno == EIO) {
144             /* some devices return EIO while the drive is busy loading */
145             return READ_LABEL_STATUS_DEVICE_ERROR|READ_LABEL_STATUS_VOLUME_MISSING;
146         } else {
147             return READ_LABEL_STATUS_DEVICE_ERROR;
148         }
149     }
150 }
151
152 TapeCheckResult tape_is_ready(int fd) {
153     struct mtget get;
154     if (0 == ioctl(fd, MTIOCGET, &get)) {
155 #if defined(GMT_DR_OPEN)
156         if (!GMT_DR_OPEN(get.mt_gstat)) {
157             return TAPE_CHECK_SUCCESS;
158         } else {
159             dbprintf("tape_is_read: ioctl(MTIOCGET) failed: %s", strerror(errno));
160             return TAPE_CHECK_FAILURE;
161         }
162 #else /* Neither macro is defined. */
163         return TAPE_CHECK_UNKNOWN;
164 #endif
165     } else {
166         return TAPE_CHECK_FAILURE;
167     }
168 }
169
170 void tape_device_discover_capabilities(TapeDevice * t_self) {
171     Device * self;
172     GValue val;
173
174     self = DEVICE(t_self);
175     g_return_if_fail(self != NULL);
176
177     bzero(&val, sizeof(val));
178     g_value_init(&val, FEATURE_SUPPORT_FLAGS_TYPE);
179
180     g_value_set_flags(&val,
181                       FEATURE_STATUS_ENABLED | FEATURE_SURETY_BAD |
182                       FEATURE_SOURCE_DEFAULT);
183     device_property_set(self, PROPERTY_FSF, &val);
184     
185     g_value_set_flags(&val,
186                       FEATURE_STATUS_ENABLED | FEATURE_SURETY_BAD |
187                       FEATURE_SOURCE_DEFAULT);
188     device_property_set(self, PROPERTY_BSF, &val);
189     
190     g_value_set_flags(&val,
191                       FEATURE_STATUS_ENABLED | FEATURE_SURETY_BAD |
192                       FEATURE_SOURCE_DEFAULT);
193     device_property_set(self, PROPERTY_FSR, &val);
194     
195     g_value_set_flags(&val,
196                       FEATURE_STATUS_ENABLED | FEATURE_SURETY_BAD |
197                       FEATURE_SOURCE_DEFAULT);
198     device_property_set(self, PROPERTY_BSR, &val);
199     
200     g_value_set_flags(&val,
201                       FEATURE_STATUS_ENABLED | FEATURE_SURETY_BAD |
202                       FEATURE_SOURCE_DEFAULT);
203     device_property_set(self, PROPERTY_EOM, &val);
204
205     g_value_set_flags(&val,
206                       FEATURE_STATUS_DISABLED | FEATURE_SURETY_BAD | 
207                       FEATURE_SOURCE_DEFAULT);
208     device_property_set(self, PROPERTY_BSF_AFTER_EOM, &val);
209
210     g_value_unset_init(&val, G_TYPE_UINT);
211     g_value_set_uint(&val, 2);
212     device_property_set(self, PROPERTY_FINAL_FILEMARKS, &val);
213 }