cada501153dc983d315bd3f45f7658dc493cbd7d
[debian/amanda] / device-src / tape-posix.c
1 /*
2  * Copyright (c) 2005-2008 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., 465 S Mathlida Ave, Suite 300
18  * Sunnyvale, CA 94086, 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 DeviceStatusFlags 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 DEVICE_STATUS_SUCCESS;
140 #ifdef ENOMEDIUM
141     } else if (errno == ENOMEDIUM) {
142         return DEVICE_STATUS_VOLUME_MISSING;
143 #endif
144     } else {
145         dbprintf("tape_is_tape_device: ioctl(MTIOCTOP/MTNOP) failed: %s\n",
146                  strerror(errno));
147         if (errno == EIO) {
148             /* some devices return EIO while the drive is busy loading */
149             return DEVICE_STATUS_DEVICE_ERROR|DEVICE_STATUS_DEVICE_BUSY;
150         } else {
151             return DEVICE_STATUS_DEVICE_ERROR;
152         }
153     }
154 }
155
156 DeviceStatusFlags tape_is_ready(int fd, TapeDevice *t_self G_GNUC_UNUSED) {
157     struct mtget get;
158     if (0 == ioctl(fd, MTIOCGET, &get)) {
159 #if defined(GMT_ONLINE) || defined(GMT_DR_OPEN)
160         if (1
161 #ifdef GMT_ONLINE
162             && (t_self->broken_gmt_online || GMT_ONLINE(get.mt_gstat))
163 #endif
164 #ifdef GMT_DR_OPEN
165             && !GMT_DR_OPEN(get.mt_gstat)
166 #endif
167             ) {
168             return DEVICE_STATUS_SUCCESS;
169         } else {
170             return DEVICE_STATUS_VOLUME_MISSING;
171         }
172 #else /* Neither macro is defined. */
173         return DEVICE_STATUS_SUCCESS;
174 #endif
175     } else {
176         return DEVICE_STATUS_VOLUME_ERROR;
177     }
178 }
179
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         DEFAULT_FSF_AFTER_FILEMARK, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT, /* fsf_after_filemark*/
184         TRUE,  PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT, /* bsf*/
185         TRUE,  PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT, /* fsr*/
186         TRUE,  PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT, /* bsr*/
187         TRUE,  PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT, /* eom*/
188         FALSE, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT, /* bsf_after_eom*/
189         2,     PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT  /* final_filemarks*/
190         );
191 }