2 * Copyright (c) 2005-2008 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., 465 S Mathlida Ave, Suite 300
18 * Sunnyvale, CA 94086, 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 DeviceStatusFlags tape_is_tape_device(int fd) {
138 if (0 == ioctl(fd, MTIOCTOP, &mt)) {
139 return DEVICE_STATUS_SUCCESS;
141 } else if (errno == ENOMEDIUM) {
142 return DEVICE_STATUS_VOLUME_MISSING;
145 dbprintf("tape_is_tape_device: ioctl(MTIOCTOP/MTNOP) failed: %s\n",
148 /* some devices return EIO while the drive is busy loading */
149 return DEVICE_STATUS_DEVICE_ERROR|DEVICE_STATUS_DEVICE_BUSY;
151 return DEVICE_STATUS_DEVICE_ERROR;
156 DeviceStatusFlags tape_is_ready(int fd, TapeDevice *t_self G_GNUC_UNUSED) {
158 if (0 == ioctl(fd, MTIOCGET, &get)) {
159 #if defined(GMT_ONLINE) || defined(GMT_DR_OPEN)
162 && (t_self->broken_gmt_online || GMT_ONLINE(get.mt_gstat))
165 && !GMT_DR_OPEN(get.mt_gstat)
168 return DEVICE_STATUS_SUCCESS;
170 return DEVICE_STATUS_VOLUME_MISSING;
172 #else /* Neither macro is defined. */
173 return DEVICE_STATUS_SUCCESS;
176 return DEVICE_STATUS_VOLUME_ERROR;
180 void tape_device_detect_capabilities(TapeDevice * t_self) {
181 tape_device_set_capabilities(t_self,
182 TRUE, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT, /* fsf*/
183 TRUE, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT, /* bsf*/
184 TRUE, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT, /* fsr*/
185 TRUE, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT, /* bsr*/
186 TRUE, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT, /* eom*/
187 FALSE, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT, /* bsf_after_eom*/
188 2, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT /* final_filemarks*/