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;
135 gsize header_block_size;
137 /* surety and source for the block size; if you set block_size directly,
139 PropertySurety block_size_surety;
140 PropertySource block_size_source;
142 DevicePrivate * private;
145 /* Pointer to factory function for device types.
147 * device_name is the full name ("tape:/dev/nst0")
148 * device_prefix is the prefix ("tape")
149 * device_node is what follows the prefix ("/dev/nst0")
151 * The caller retains responsibility to free or otherwise handle
152 * the passed strings.
154 typedef Device* (*DeviceFactory)(char *device_name,
155 char * device_prefix,
158 /* This function registers a new device with the allocation system.
159 * Call it after you register your type with the GLib type system.
160 * This function assumes that the strings in device_prefix_list are
161 * statically allocated. */
162 extern void register_device(DeviceFactory factory,
163 const char ** device_prefix_list);
168 typedef struct _DeviceClass DeviceClass;
169 struct _DeviceClass {
170 GObjectClass __parent__;
171 void (* open_device) (Device * self, char * device_name,
172 char * device_prefix, char * device_node);
173 gboolean (* configure) (Device * self, gboolean use_global_config);
174 DeviceStatusFlags (* read_label)(Device * self);
175 gboolean (* start) (Device * self, DeviceAccessMode mode,
176 char * label, char * timestamp);
177 gboolean (* start_file) (Device * self, dumpfile_t * info);
178 gboolean (* write_block) (Device * self, guint size, gpointer data);
179 gboolean (* finish_file) (Device * self);
180 dumpfile_t* (* seek_file) (Device * self, guint file);
181 gboolean (* seek_block) (Device * self, guint64 block);
182 int (* read_block) (Device * self, gpointer buf, int * size);
183 gboolean (* property_get_ex) (Device * self, DevicePropertyId id,
185 PropertySurety *surety,
186 PropertySource *source);
187 gboolean (* property_set_ex) (Device * self,
190 PropertySurety surety,
191 PropertySource source);
192 gboolean (* recycle_file) (Device * self, guint filenum);
193 gboolean (* erase) (Device * self);
194 gboolean (* eject) (Device * self);
195 gboolean (* finish) (Device * self);
197 gboolean (* listen)(Device *self, gboolean for_writing, DirectTCPAddr **addrs);
198 gboolean (* accept)(Device *self, DirectTCPConnection **conn,
199 ProlongProc prolong, gpointer prolong_data);
200 gboolean (* connect)(Device *self, gboolean for_writing, DirectTCPAddr *addrs,
201 DirectTCPConnection **conn, ProlongProc prolong,
202 gpointer prolong_data);
203 gboolean (* write_from_connection)(Device *self, guint64 size, guint64 *actual_size);
204 gboolean (* read_to_connection)(Device *self, guint64 size, guint64 *actual_size);
205 gboolean (* use_connection)(Device *self, DirectTCPConnection *conn);
207 /* array of DeviceProperty objects for this class, keyed by ID */
208 GArray *class_properties;
210 /* The return value of device_property_get_list */
211 GSList * class_properties_list;
213 /* TRUE if the directtcp methods are implemented by this device class */
214 gboolean directtcp_supported;
218 * Device Instantiation
221 /* Return the unaliased device name of a device.
222 * The returned string must not be freed by the caller.
224 char* device_unaliased_name(char * device_name);
226 /* This is how you get a new Device. Pass in a device name or alias.
228 * A Device is *always* returned, even for an invalid device name. You
229 * must check the resulting device->status to know if the device is valid
230 * to be used. If device->status is not DEVICE_STATUS_SUCCESS, then there
231 * was an error opening the device.
233 * Note that the Amanda configuration must be initialized, as this function
234 * looks for device definitions and other configuration information.
236 Device* device_open (char * device_name);
238 /* As a special case, a RAIT device can be created from a collection of child
239 * devices. This is used by the RAIT changer, for example. This function is
240 * implemented in rait-device.c. */
241 Device* rait_device_open_from_children(GSList *child_devices);
243 /* Once you have a new device, you should configure it. This sets properties
244 * on the device based on the user's configuation. If USE_GLOBAL_CONFIG is
245 * true, then any global device_property parameters are processed, along with
246 * tapetype and othe relevant parameters.
248 gboolean device_configure(Device *self, gboolean use_global_config);
254 /* return the error message or the string "Unknown Device error". The
255 * string remains the responsibility of the Device, and should not
256 * be freed by the caller. */
257 char *device_error(Device * self);
259 /* return a string version of the status. The string remains the
260 * responsibility of the Device, and should not be freed by the
262 char *device_status_error(Device * self);
264 /* Return errmsg if it is set or a string version of the status. The
265 * string remains the responsibility of the Device, and should not
266 * be freed by the caller. */
267 char *device_error_or_status(Device * self);
269 /* Set the error message for this device; for use internally to the
270 * API. The string becomes the responsibility of the Device. If
271 * ERRMSG is NULL, the message is cleared. Note that the given flags
272 * are OR'd with any existing status flags. */
273 void device_set_error(Device * self, char *errmsg, DeviceStatusFlags new_flags);
275 /* Mostly for internal use, this is a boolean check to see whether a given
276 * device is in an error state. If this is TRUE, most operations on the
279 * The check is for DEVICE_STATUS_DEVICE_ERROR *alone*; if any other bits
280 * (e.g., VOLUME_UNLABELED) are set, then the device may not actually be in
283 #define device_in_error(dev) \
284 ((DEVICE(dev))->status == DEVICE_STATUS_DEVICE_ERROR)
289 * See the Amanda::Device POD for more information here
292 DeviceStatusFlags device_read_label (Device * self);
293 gboolean device_start (Device * self,
294 DeviceAccessMode mode, char * label,
296 gboolean device_finish (Device * self);
297 gboolean device_start_file (Device * self,
298 dumpfile_t * jobInfo);
299 gboolean device_write_block (Device * self,
302 gboolean device_finish_file (Device * self);
303 dumpfile_t* device_seek_file (Device * self,
305 gboolean device_seek_block (Device * self,
307 int device_read_block (Device * self, gpointer buffer, int * size);
308 const GSList * device_property_get_list (Device * self);
309 gboolean device_property_get_ex (Device * self,
312 PropertySurety *surety,
313 PropertySource *source);
314 #define device_property_get(self, id, val) \
315 device_property_get_ex((self), (id), (val), NULL, NULL)
316 gboolean device_property_set_ex (Device * self,
319 PropertySurety surety,
320 PropertySource source);
321 #define device_property_set(self, id, val) \
322 device_property_set_ex((self), (id), (val), \
323 PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_USER)
324 gboolean device_recycle_file (Device * self,
327 gboolean device_erase (Device * self);
328 gboolean device_eject (Device * self);
330 #define device_directtcp_supported(self) (DEVICE_GET_CLASS((self))->directtcp_supported)
331 gboolean device_listen(Device *self, gboolean for_writing, DirectTCPAddr **addrs);
332 gboolean device_accept(Device *self, DirectTCPConnection **conn,
333 ProlongProc prolong, gpointer prolong_data);
334 gboolean device_connect(Device *self, gboolean for_writing, DirectTCPAddr *addrs,
335 DirectTCPConnection **conn, ProlongProc prolong,
336 gpointer prolong_data);
337 gboolean device_write_from_connection(Device *self, guint64 size, guint64 *actual_size);
338 gboolean device_read_to_connection(Device *self, guint64 size, guint64 *actual_size);
339 gboolean device_use_connection(Device *self, DirectTCPConnection *conn);
341 /* Protected methods. Don't call these except in subclass implementations. */
343 /* This method provides post-construction initalization once the
344 * device name is known. It should only be used by Device
345 * factories. It is provided here as a virtual method (instead of
346 * a static function) because some devices may want to chain
347 * initilization to their parents. */
348 void device_open_device (Device * self, char *device_name, char *device_type, char *device_node);
350 /* Builds a proper header of between *size and self->block_size bytes.
351 * Returns NULL if the header does not fit in a single block. The result
352 * must be free'd. If size is NULL, the block size is used.
354 * If size is not NULL, *size is set to the actual size of the generated header.
356 char * device_build_amanda_header(Device * self, const dumpfile_t * jobinfo,
359 /* Does what you expect. You have to free the returned header. Ensures
360 that self->volume_time matches the header written to tape. */
361 dumpfile_t * make_tapestart_header(Device * self, char * label,
364 /* Does what you expect. Uses the current time. */
365 dumpfile_t * make_tapeend_header(void);
367 /* Erase any stored volume information. Use this if something happens (e.g.,
368 * a property is set) that voids previously-read volume details.
369 * This function is a NOOP unless the device is in the NULL state. */
370 void device_clear_volume_details(Device * device);
372 /* Property Handling */
374 /* Registers a property for a new device class; device drivers' GClassInitFunc
375 * should call this function for each device-specific property of the class.
376 * If either getter or setter is NULL, then the corresponding operation will
379 * Note that this will replace any existing registration (e.g., from a parent
382 void device_class_register_property(DeviceClass *klass, DevicePropertyId id,
383 PropertyAccessFlags access,
384 PropertyGetFn getter,
385 PropertySetFn setter);
387 /* Set a 'simple' property on the device. This tucks the value away in the
388 * object, to be retrieved by device_simple_property_get_fn. This is most
389 * often used in GInstanceInit functions, but can be used at any time to set or
390 * change the value of a simple property */
391 gboolean device_set_simple_property(Device *self, DevicePropertyId id,
392 GValue *val, PropertySurety surety,
393 PropertySource source);
395 /* Get a simple property set with device_set_simple_property. This is a little
396 * bit quicker than calling device_property_get_ex(), and does not affect the
397 * device's error state. Returns FALSE if the property has not been set.
398 * Surety and source are output parameters and will be ignored if they are
400 gboolean device_get_simple_property(Device *self, DevicePropertyId id,
401 GValue *val, PropertySurety *surety,
402 PropertySource *source);
404 /* A useful PropertySetFn. If your subclass also needs to intercept sets, for
405 * example to flush a cache or update a member variable, then write a stub
406 * function which "calls up" to this function. */
407 gboolean device_simple_property_set_fn(Device *self, DevicePropertyBase *base,
408 GValue *val, PropertySurety surety,
409 PropertySource source);
411 /* A useful PropertyGetFn -- returns the value, source, and surety set with
412 * device_set_simple_property */
413 gboolean device_simple_property_get_fn(Device *self, DevicePropertyBase *base,
414 GValue *val, PropertySurety *surety,
415 PropertySource *source);
417 #endif /* DEVICE_H */