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
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_fileno(int fd) {
98 if (0 != ioctl(fd, MTIOCGET, &get))
99 return TAPE_POSITION_UNKNOWN;
100 if (get.mt_fileno < 0)
101 return TAPE_POSITION_UNKNOWN;
103 return get.mt_fileno;
106 gint tape_eod(int fd) {
111 if (0 != ioctl(fd, MTIOCTOP, &mt))
112 return TAPE_OP_ERROR;
114 /* Ignored result. This is just to flush buffers. */
116 ioctl(fd, MTIOCTOP, &mt);
118 if (0 != ioctl(fd, MTIOCGET, &get))
119 return TAPE_POSITION_UNKNOWN;
120 if (get.mt_fileno < 0)
121 return TAPE_POSITION_UNKNOWN;
123 return get.mt_fileno;
126 gboolean tape_weof(int fd, guint8 count) {
130 return 0 == ioctl(fd, MTIOCTOP, &mt);
133 gboolean tape_setcompression(int fd G_GNUC_UNUSED,
134 gboolean on G_GNUC_UNUSED) {
137 mt.mt_op = MTCOMPRESSION;
139 return 0 == ioctl(fd, MTIOCTOP, &mt);
145 gboolean tape_offl(int fd) {
151 if (0 == ioctl(fd, MTIOCTOP, &mt))
155 g_debug("tape_off: ioctl(MTIOCTOP/MTOFFL) failed: %s", strerror(errno));
161 DeviceStatusFlags tape_is_tape_device(int fd) {
165 if (0 == ioctl(fd, MTIOCTOP, &mt)) {
166 return DEVICE_STATUS_SUCCESS;
168 } else if (errno == ENOMEDIUM) {
169 return DEVICE_STATUS_VOLUME_MISSING;
172 g_debug("tape_is_tape_device: ioctl(MTIOCTOP/MTNOP) failed: %s",
175 /* some devices return EIO while the drive is busy loading */
176 return DEVICE_STATUS_DEVICE_ERROR|DEVICE_STATUS_DEVICE_BUSY;
178 return DEVICE_STATUS_DEVICE_ERROR;
183 DeviceStatusFlags tape_is_ready(int fd, TapeDevice *t_self G_GNUC_UNUSED) {
185 if (0 == ioctl(fd, MTIOCGET, &get)) {
186 #if defined(GMT_ONLINE) || defined(GMT_DR_OPEN)
189 && (t_self->broken_gmt_online || GMT_ONLINE(get.mt_gstat))
192 && !GMT_DR_OPEN(get.mt_gstat)
195 return DEVICE_STATUS_SUCCESS;
197 return DEVICE_STATUS_VOLUME_MISSING;
199 #else /* Neither macro is defined. */
200 return DEVICE_STATUS_SUCCESS;
203 return DEVICE_STATUS_VOLUME_ERROR;
207 void tape_device_detect_capabilities(TapeDevice * t_self) {
208 tape_device_set_capabilities(t_self,
209 TRUE, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT, /* fsf*/
210 DEFAULT_FSF_AFTER_FILEMARK, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT, /* fsf_after_filemark*/
211 TRUE, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT, /* bsf*/
212 TRUE, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT, /* fsr*/
213 TRUE, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT, /* bsr*/
214 TRUE, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT, /* eom*/
215 FALSE, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT, /* bsf_after_eom*/
216 2, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT /* final_filemarks*/