Imported Upstream version 3.2.0
[debian/amanda] / device-src / device.h
1 /*
2  * Copyright (c) 2007, 2008, 2009, 2010 Zmanda, Inc.  All Rights Reserved.
3  *
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.
7  *
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
11  * for more details.
12  *
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
16  *
17  * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
19  */
20
21 /* The Device API abstracts device workings, interaction, properties, and
22  * capabilities from the rest of the Amanda code base. It supports
23  * pluggable modules for different kinds of devices. */
24
25 #ifndef DEVICE_H
26 #define DEVICE_H
27
28 #include <glib.h>
29 #include <glib-object.h>
30
31 #include "property.h"
32 #include "fileheader.h"
33 #include "directtcp.h"
34 #include "directtcp-connection.h"
35
36 /* Device API version. */
37 #define DEVICE_API_VERSION 0
38
39 extern void device_api_init(void);
40
41 /* Different access modes */
42 typedef enum {
43     ACCESS_NULL, /* Device is not yet opened. */
44     ACCESS_READ,
45     ACCESS_WRITE,
46     ACCESS_APPEND
47 } DeviceAccessMode;
48
49 #define IS_WRITABLE_ACCESS_MODE(mode) ((mode) == ACCESS_WRITE || \
50                                        (mode) == ACCESS_APPEND)
51
52 /* Device object definition follows. */
53
54 /*
55  * Type checking and casting macros
56  */
57 GType   device_get_type (void);
58 #define TYPE_DEVICE     (device_get_type())
59 #define DEVICE(obj)     G_TYPE_CHECK_INSTANCE_CAST((obj), device_get_type(), Device)
60 #define DEVICE_CONST(obj)       G_TYPE_CHECK_INSTANCE_CAST((obj), device_get_type(), Device const)
61 #define DEVICE_CLASS(klass)     G_TYPE_CHECK_CLASS_CAST((klass), device_get_type(), DeviceClass)
62 #define IS_DEVICE(obj)  G_TYPE_CHECK_INSTANCE_TYPE((obj), device_get_type ())
63 #define DEVICE_GET_CLASS(obj)   G_TYPE_INSTANCE_GET_CLASS((obj), device_get_type(), DeviceClass)
64
65 typedef struct DevicePrivate_s DevicePrivate;
66
67 /* See Amanda::Device POD for a description of these constants */
68 typedef enum {
69     DEVICE_STATUS_SUCCESS          = 0,
70     DEVICE_STATUS_DEVICE_ERROR     = (1 << 0),
71     DEVICE_STATUS_DEVICE_BUSY      = (1 << 1),
72     DEVICE_STATUS_VOLUME_MISSING   = (1 << 2),
73     DEVICE_STATUS_VOLUME_UNLABELED = (1 << 3),
74     DEVICE_STATUS_VOLUME_ERROR     = (1 << 4),
75     DEVICE_STATUS_FLAGS_MAX        = (1 << 5)
76 } DeviceStatusFlags;
77
78 #define DEVICE_STATUS_FLAGS_MASK (DEVICE_STATUS_MAX-1)
79 #define DEVICE_STATUS_FLAGS_TYPE (device_status_flags_get_type())
80 GType device_status_flags_get_type(void);
81
82 /* a callback to prolong an operation */
83 typedef gboolean (* ProlongProc)(gpointer data);
84
85 /*
86  * Main object structure
87  */
88 typedef struct Device {
89     GObject __parent__;
90
91     /* You can peek at the stuff below, but only subclasses should
92        change these values.*/
93
94     /* What file, block are we at? (and are we in the middle of a file?) */
95     int file;
96     guint64 block;
97     gboolean in_file;
98
99     /* Holds the user-specified device name, which may be an alias */
100     char * device_name;
101
102     /* Holds the user-specified access-mode, or ACCESS_NULL if the device
103      * has not yet been started*/
104     DeviceAccessMode access_mode;
105
106     /* In reading mode, FALSE unless all the data from the current file
107      * was successfully read.  In writing mode, TRUE if the last byte
108      * of a file has been written by write_from_connection. */
109     gboolean is_eof;
110
111     /* In writing mode, indicates that the volume is at (or near, if possible)
112      * EOM. */
113     gboolean is_eom;
114
115     /* Holds the label and time of the currently-inserted volume,
116      * or NULL if it has not been read/written yet. */
117     char * volume_label;
118     char * volume_time;
119
120     /* The most recently read volume header, or NULL if no header was
121      * read from this device.  Callers can use this to glean information
122      * about the volume beyond volume_label and volume_time.  */
123     dumpfile_t *volume_header;
124
125     /* The latest status for the device */
126     DeviceStatusFlags status;
127
128     /* device block-size ranges.  These are also available as properties,
129      * and by default users can set block_size via property BLOCK_SIZE.
130      * Writers should use block_size, and readers should initially use
131      * block_size, and expand buffers as directed by read_block. */
132     gsize min_block_size;
133     gsize max_block_size;
134     gsize block_size;
135
136     /* surety and source for the block size; if you set block_size directly,
137      * set these, too! */
138     PropertySurety block_size_surety;
139     PropertySource block_size_source;
140
141     DevicePrivate * private;
142 } Device;
143
144 /* Pointer to factory function for device types.
145  *
146  * device_name is the full name ("tape:/dev/nst0")
147  * device_prefix is the prefix ("tape")
148  * device_node is what follows the prefix ("/dev/nst0")
149  *
150  * The caller retains responsibility to free or otherwise handle
151  * the passed strings.
152  */
153 typedef Device* (*DeviceFactory)(char *device_name,
154                                  char * device_prefix,
155                                  char * device_node);
156
157 /* This function registers a new device with the allocation system.
158  * Call it after you register your type with the GLib type system.
159  * This function assumes that the strings in device_prefix_list are
160  * statically allocated. */
161 extern void register_device(DeviceFactory factory,
162                             const char ** device_prefix_list);
163
164 /*
165  * Class definition
166  */
167 typedef struct _DeviceClass DeviceClass;
168 struct _DeviceClass {
169     GObjectClass __parent__;
170     void (* open_device) (Device * self, char * device_name,
171                     char * device_prefix, char * device_node);
172     gboolean (* configure) (Device * self, gboolean use_global_config);
173     DeviceStatusFlags (* read_label)(Device * self);
174     gboolean (* start) (Device * self, DeviceAccessMode mode,
175                         char * label, char * timestamp);
176     gboolean (* start_file) (Device * self, dumpfile_t * info);
177     gboolean (* write_block) (Device * self, guint size, gpointer data);
178     gboolean (* finish_file) (Device * self);
179     dumpfile_t* (* seek_file) (Device * self, guint file);
180     gboolean (* seek_block) (Device * self, guint64 block);
181     int (* read_block) (Device * self, gpointer buf, int * size);
182     gboolean (* property_get_ex) (Device * self, DevicePropertyId id,
183                                   GValue * val,
184                                   PropertySurety *surety,
185                                   PropertySource *source);
186     gboolean (* property_set_ex) (Device * self,
187                                   DevicePropertyId id,
188                                   GValue * val,
189                                   PropertySurety surety,
190                                   PropertySource source);
191     gboolean (* recycle_file) (Device * self, guint filenum);
192     gboolean (* erase) (Device * self);
193     gboolean (* eject) (Device * self);
194     gboolean (* finish) (Device * self);
195
196     gboolean (* listen)(Device *self, gboolean for_writing, DirectTCPAddr **addrs);
197     gboolean (* accept)(Device *self, DirectTCPConnection **conn,
198                         ProlongProc prolong, gpointer prolong_data);
199     gboolean (* connect)(Device *self, gboolean for_writing, DirectTCPAddr *addrs,
200                         DirectTCPConnection **conn, ProlongProc prolong,
201                         gpointer prolong_data);
202     gboolean (* write_from_connection)(Device *self, guint64 size, guint64 *actual_size);
203     gboolean (* read_to_connection)(Device *self, guint64 size, guint64 *actual_size);
204     gboolean (* use_connection)(Device *self, DirectTCPConnection *conn);
205
206     /* array of DeviceProperty objects for this class, keyed by ID */
207     GArray *class_properties;
208
209     /* The return value of device_property_get_list */
210     GSList * class_properties_list;
211
212     /* TRUE if the directtcp methods are implemented by this device class */
213     gboolean directtcp_supported;
214 };
215
216 /*
217  * Device Instantiation
218  */
219
220 /* Return the unaliased device name of a device.
221  * The returned string must not be freed by the caller.
222  */
223 char*           device_unaliased_name(char * device_name);
224
225 /* This is how you get a new Device. Pass in a device name or alias.
226  *
227  * A Device is *always* returned, even for an invalid device name. You
228  * must check the resulting device->status to know if the device is valid
229  * to be used. If device->status is not DEVICE_STATUS_SUCCESS, then there
230  * was an error opening the device.
231  *
232  * Note that the Amanda configuration must be initialized, as this function
233  * looks for device definitions and other configuration information.
234  */
235 Device*         device_open     (char * device_name);
236
237 /* As a special case, a RAIT device can be created from a collection of child
238  * devices.  This is used by the RAIT changer, for example.  This function is
239  * implemented in rait-device.c.  */
240 Device*         rait_device_open_from_children(GSList *child_devices);
241
242 /* Once you have a new device, you should configure it.  This sets properties
243  * on the device based on the user's configuation.  If USE_GLOBAL_CONFIG is
244  * true, then any global device_property parameters are processed, along with
245  * tapetype and othe relevant parameters.
246  */
247 gboolean device_configure(Device *self, gboolean use_global_config);
248
249 /*
250  * Error Handling
251  */
252
253 /* return the error message or the string "Unknown Device error".  The
254  * string remains the responsibility of the Device, and should not
255  * be freed by the caller. */
256 char *device_error(Device * self);
257
258 /* return a string version of the status.  The string remains the
259  * responsibility of the Device, and should not be freed by the
260  * caller. */
261 char *device_status_error(Device * self);
262
263 /* Return errmsg if it is set or a string version of the status.  The
264  * string remains the responsibility of the Device, and should not
265  * be freed by the caller. */
266 char *device_error_or_status(Device * self);
267
268 /* Set the error message for this device; for use internally to the
269  * API.  The string becomes the responsibility of the Device.  If
270  * ERRMSG is NULL, the message is cleared.  Note that the given flags
271  * are OR'd with any existing status flags. */
272 void device_set_error(Device * self, char *errmsg, DeviceStatusFlags new_flags);
273
274 /* Mostly for internal use, this is a boolean check to see whether a given
275  * device is in an error state.  If this is TRUE, most operations on the
276  * device will fail.
277  *
278  * The check is for DEVICE_STATUS_DEVICE_ERROR *alone*; if any other bits
279  * (e.g., VOLUME_UNLABELED) are set, then the device may not actually be in
280  * an error state.
281  */
282 #define device_in_error(dev) \
283     ((DEVICE(dev))->status == DEVICE_STATUS_DEVICE_ERROR)
284
285 /*
286  * Public methods
287  *
288  * See the Amanda::Device POD for more information here
289  */
290
291 DeviceStatusFlags        device_read_label (Device * self);
292 gboolean        device_start    (Device * self,
293                                  DeviceAccessMode mode, char * label,
294                                  char * timestamp);
295 gboolean        device_finish   (Device * self);
296 gboolean        device_start_file       (Device * self,
297                                          dumpfile_t * jobInfo);
298 gboolean        device_write_block      (Device * self,
299                                          guint size,
300                                          gpointer data);
301 gboolean        device_finish_file      (Device * self);
302 dumpfile_t*     device_seek_file        (Device * self,
303                                         guint file);
304 gboolean        device_seek_block       (Device * self,
305                                         guint64 block);
306 int     device_read_block       (Device * self, gpointer buffer, int * size);
307 const GSList *  device_property_get_list        (Device * self);
308 gboolean        device_property_get_ex  (Device * self,
309                                          DevicePropertyId id,
310                                          GValue * val,
311                                          PropertySurety *surety,
312                                          PropertySource *source);
313 #define         device_property_get(self, id, val) \
314     device_property_get_ex((self), (id), (val), NULL, NULL)
315 gboolean        device_property_set_ex  (Device * self,
316                                          DevicePropertyId id,
317                                          GValue * val,
318                                          PropertySurety surety,
319                                          PropertySource source);
320 #define         device_property_set(self, id, val) \
321     device_property_set_ex((self), (id), (val), \
322             PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_USER)
323 gboolean        device_recycle_file     (Device * self,
324                                         guint filenum);
325
326 gboolean        device_erase    (Device * self);
327 gboolean        device_eject    (Device * self);
328
329 #define device_directtcp_supported(self) (DEVICE_GET_CLASS((self))->directtcp_supported)
330 gboolean device_listen(Device *self, gboolean for_writing, DirectTCPAddr **addrs);
331 gboolean device_accept(Device *self, DirectTCPConnection **conn,
332                 ProlongProc prolong, gpointer prolong_data);
333 gboolean device_connect(Device *self, gboolean for_writing, DirectTCPAddr *addrs,
334                         DirectTCPConnection **conn, ProlongProc prolong,
335                         gpointer prolong_data);
336 gboolean device_write_from_connection(Device *self, guint64 size, guint64 *actual_size);
337 gboolean device_read_to_connection(Device *self, guint64 size, guint64 *actual_size);
338 gboolean device_use_connection(Device *self, DirectTCPConnection *conn);
339
340 /* Protected methods. Don't call these except in subclass implementations. */
341
342 /* This method provides post-construction initalization once the
343  * device name is known. It should only be used by Device
344  * factories. It is provided here as a virtual method (instead of
345  * a static function) because some devices may want to chain
346  * initilization to their parents. */
347 void device_open_device (Device * self, char *device_name, char *device_type, char *device_node);
348
349 /* Builds a proper header of between *size and self->block_size bytes.
350  * Returns NULL if the header does not fit in a single block.  The result
351  * must be free'd.  If size is NULL, the block size is used.
352  *
353  * If size is not NULL, *size is set to the actual size of the generated header.
354  */
355 char * device_build_amanda_header(Device * self, const dumpfile_t * jobinfo,
356                                   size_t *size);
357
358 /* Does what you expect. You have to free the returned header. Ensures
359    that self->volume_time matches the header written to tape. */
360 dumpfile_t * make_tapestart_header(Device * self, char * label,
361                                    char * timestamp);
362
363 /* Does what you expect. Uses the current time. */
364 dumpfile_t * make_tapeend_header(void);
365
366 /* Erase any stored volume information. Use this if something happens (e.g.,
367  * a property is set) that voids previously-read volume details.
368  * This function is a NOOP unless the device is in the NULL state. */
369 void device_clear_volume_details(Device * device);
370
371 /* Property Handling */
372
373 /* Registers a property for a new device class; device drivers' GClassInitFunc
374  * should call this function for each device-specific property of the class.
375  * If either getter or setter is NULL, then the corresponding operation will
376  * return FALSE.
377  *
378  * Note that this will replace any existing registration (e.g., from a parent
379  * class).
380  */
381 void device_class_register_property(DeviceClass *klass, DevicePropertyId id,
382                                     PropertyAccessFlags access,
383                                     PropertyGetFn getter,
384                                     PropertySetFn setter);
385
386 /* Set a 'simple' property on the device.  This tucks the value away in the
387  * object, to be retrieved by device_simple_property_get_fn.  This is most
388  * often used in GInstanceInit functions, but can be used at any time to set or
389  * change the value of a simple property */
390 gboolean device_set_simple_property(Device *self, DevicePropertyId id,
391                                 GValue *val, PropertySurety surety,
392                                 PropertySource source);
393
394 /* Get a simple property set with device_set_simple_property.  This is a little
395  * bit quicker than calling device_property_get_ex(), and does not affect the
396  * device's error state.  Returns FALSE if the property has not been set.
397  * Surety and source are output parameters and will be ignored if they are
398  * NULL. */
399 gboolean device_get_simple_property(Device *self, DevicePropertyId id,
400                                     GValue *val, PropertySurety *surety,
401                                     PropertySource *source);
402
403 /* A useful PropertySetFn.  If your subclass also needs to intercept sets, for
404  * example to flush a cache or update a member variable, then write a stub
405  * function which "calls up" to this function. */
406 gboolean device_simple_property_set_fn(Device *self, DevicePropertyBase *base,
407                                        GValue *val, PropertySurety surety,
408                                        PropertySource source);
409
410 /* A useful PropertyGetFn -- returns the value, source, and surety set with
411  * device_set_simple_property */
412 gboolean device_simple_property_get_fn(Device *self, DevicePropertyBase *base,
413                                        GValue *val, PropertySurety *surety,
414                                        PropertySource *source);
415
416 #endif /* DEVICE_H */