2 * Copyright (c) 2005 Zmanda, Inc. All Rights Reserved.
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.
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.
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.
17 * Contact information: Zmanda Inc., 505 N Mathlida Ave, Suite 120
18 * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
27 /* Having one name for every operation would be too easy. */
28 #if !defined(MTCOMPRESSION) && defined(MTCOMP)
29 # define MTCOMPRESSION MTCOMP
32 #if !defined(MTSETBLK) && defined(MTSETBSIZ)
33 # define MTSETBLK MTSETBSIZ
36 #if !defined(MTEOM) && defined(MTEOD)
44 gboolean tape_rewind(int fd) {
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;
53 while (--count >= 0 && time(NULL) < stop_time) {
58 if (0 == ioctl(fd, MTIOCTOP, &mt))
67 gboolean tape_fsf(int fd, guint count) {
71 return 0 == ioctl(fd, MTIOCTOP, &mt);
74 gboolean tape_bsf(int fd, guint count) {
78 return 0 == ioctl(fd, MTIOCTOP, &mt);
81 gboolean tape_fsr(int fd, guint count) {
85 return 0 == ioctl(fd, MTIOCTOP, &mt);
88 gboolean tape_bsr(int fd, guint count) {
92 return 0 == ioctl(fd, MTIOCTOP, &mt);
95 gint tape_eod(int fd) {
100 if (0 != ioctl(fd, MTIOCTOP, &mt))
101 return TAPE_OP_ERROR;
103 /* Ignored result. This is just to flush buffers. */
105 ioctl(fd, MTIOCTOP, &mt);
107 if (0 != ioctl(fd, MTIOCGET, &get))
108 return TAPE_POSITION_UNKNOWN;
109 if (get.mt_fileno < 0)
110 return TAPE_POSITION_UNKNOWN;
112 return get.mt_fileno;
115 gboolean tape_weof(int fd, guint8 count) {
119 return 0 == ioctl(fd, MTIOCTOP, &mt);
122 gboolean tape_setcompression(int fd G_GNUC_UNUSED,
123 gboolean on G_GNUC_UNUSED) {
126 mt.mt_op = MTCOMPRESSION;
128 return 0 == ioctl(fd, MTIOCTOP, &mt);
134 ReadLabelStatusFlags tape_is_tape_device(int fd) {
138 if (0 == ioctl(fd, MTIOCTOP, &mt)) {
139 return READ_LABEL_STATUS_SUCCESS;
141 dbprintf("tape_is_tape_device: ioctl(MTIOCTOP/MTNOP) failed: %s",
144 /* some devices return EIO while the drive is busy loading */
145 return READ_LABEL_STATUS_DEVICE_ERROR|READ_LABEL_STATUS_VOLUME_MISSING;
147 return READ_LABEL_STATUS_DEVICE_ERROR;
152 TapeCheckResult tape_is_ready(int fd) {
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;
159 dbprintf("tape_is_read: ioctl(MTIOCGET) failed: %s", strerror(errno));
160 return TAPE_CHECK_FAILURE;
162 #else /* Neither macro is defined. */
163 return TAPE_CHECK_UNKNOWN;
166 return TAPE_CHECK_FAILURE;
170 void tape_device_discover_capabilities(TapeDevice * t_self) {
174 self = DEVICE(t_self);
175 g_return_if_fail(self != NULL);
177 bzero(&val, sizeof(val));
178 g_value_init(&val, FEATURE_SUPPORT_FLAGS_TYPE);
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);
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);
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);
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);
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);
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);
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);