Debian Codegraph @

device-src/device.h

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