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
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. */
29 #include <glib-object.h>
32 #include "fileheader.h"
33 #include "directtcp.h"
34 #include "directtcp-connection.h"
36 /* Device API version. */
37 #define DEVICE_API_VERSION 0
39 extern void device_api_init(void);
41 /* Different access modes */
43 ACCESS_NULL, /* Device is not yet opened. */
49 #define IS_WRITABLE_ACCESS_MODE(mode) ((mode) == ACCESS_WRITE || \
50 (mode) == ACCESS_APPEND)
52 /* Device object definition follows. */
55 * Type checking and casting macros
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)
65 typedef struct DevicePrivate_s DevicePrivate;
67 /* See Amanda::Device POD for a description of these constants */
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)
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);
82 /* a callback to prolong an operation */
83 typedef gboolean (* ProlongProc)(gpointer data);
86 * Main object structure
88 typedef struct Device {
91 /* You can peek at the stuff below, but only subclasses should
92 change these values.*/
94 /* What file, block are we at? (and are we in the middle of a file?) */
99 /* Holds the user-specified device name, which may be an alias */
102 /* Holds the user-specified access-mode, or ACCESS_NULL if the device
103 * has not yet been started*/
104 DeviceAccessMode access_mode;
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. */
111 /* In writing mode, indicates that the volume is at (or near, if possible)
115 /* Holds the label and time of the currently-inserted volume,
116 * or NULL if it has not been read/written yet. */
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;
125 /* The latest status for the device */
126 DeviceStatusFlags status;
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;
136 /* surety and source for the block size; if you set block_size directly,
138 PropertySurety block_size_surety;
139 PropertySource block_size_source;
141 DevicePrivate * private;
144 /* Pointer to factory function for device types.
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")
150 * The caller retains responsibility to free or otherwise handle
151 * the passed strings.
153 typedef Device* (*DeviceFactory)(char *device_name,
154 char * device_prefix,
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);
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,
184 PropertySurety *surety,
185 PropertySource *source);
186 gboolean (* property_set_ex) (Device * self,
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);
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);
206 /* array of DeviceProperty objects for this class, keyed by ID */
207 GArray *class_properties;
209 /* The return value of device_property_get_list */
210 GSList * class_properties_list;
212 /* TRUE if the directtcp methods are implemented by this device class */
213 gboolean directtcp_supported;
217 * Device Instantiation
220 /* Return the unaliased device name of a device.
221 * The returned string must not be freed by the caller.
223 char* device_unaliased_name(char * device_name);
225 /* This is how you get a new Device. Pass in a device name or alias.
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.
232 * Note that the Amanda configuration must be initialized, as this function
233 * looks for device definitions and other configuration information.
235 Device* device_open (char * device_name);
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);
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.
247 gboolean device_configure(Device *self, gboolean use_global_config);
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);
258 /* return a string version of the status. The string remains the
259 * responsibility of the Device, and should not be freed by the
261 char *device_status_error(Device * self);
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);
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);
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
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
282 #define device_in_error(dev) \
283 ((DEVICE(dev))->status == DEVICE_STATUS_DEVICE_ERROR)
288 * See the Amanda::Device POD for more information here
291 DeviceStatusFlags device_read_label (Device * self);
292 gboolean device_start (Device * self,
293 DeviceAccessMode mode, char * label,
295 gboolean device_finish (Device * self);
296 gboolean device_start_file (Device * self,
297 dumpfile_t * jobInfo);
298 gboolean device_write_block (Device * self,
301 gboolean device_finish_file (Device * self);
302 dumpfile_t* device_seek_file (Device * self,
304 gboolean device_seek_block (Device * self,
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,
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,
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,
326 gboolean device_erase (Device * self);
327 gboolean device_eject (Device * self);
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);
340 /* Protected methods. Don't call these except in subclass implementations. */
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);
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.
353 * If size is not NULL, *size is set to the actual size of the generated header.
355 char * device_build_amanda_header(Device * self, const dumpfile_t * jobinfo,
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,
363 /* Does what you expect. Uses the current time. */
364 dumpfile_t * make_tapeend_header(void);
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);
371 /* Property Handling */
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
378 * Note that this will replace any existing registration (e.g., from a parent
381 void device_class_register_property(DeviceClass *klass, DevicePropertyId id,
382 PropertyAccessFlags access,
383 PropertyGetFn getter,
384 PropertySetFn setter);
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);
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
399 gboolean device_get_simple_property(Device *self, DevicePropertyId id,
400 GValue *val, PropertySurety *surety,
401 PropertySource *source);
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);
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);
416 #endif /* DEVICE_H */