2 * Copyright (c) 2007-2012 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
24 #include "glib-util.h"
27 * Property-specific Types, etc.
30 static const GEnumValue _concurrency_paradigm_values[] = {
31 { CONCURRENCY_PARADIGM_EXCLUSIVE,
32 "CONCURRENCY_PARADIGM_EXCLUSIVE",
34 { CONCURRENCY_PARADIGM_SHARED_READ,
35 "CONCURRENCY_PARADIGM_SHARED_READ",
37 { CONCURRENCY_PARADIGM_RANDOM_ACCESS,
38 "CONCURRENCY_PARADIGM_RANDOM_ACCESS",
43 GType concurrency_paradigm_get_type (void) {
44 static GType type = 0;
45 if (G_UNLIKELY(type == 0)) {
46 type = g_enum_register_static ("ConcurrencyParadigm",
47 _concurrency_paradigm_values);
52 static const GEnumValue _streaming_requirement_values[] = {
53 { STREAMING_REQUIREMENT_NONE,
54 "STREAMING_REQUIREMENT_NONE",
56 { STREAMING_REQUIREMENT_DESIRED,
57 "STREAMING_REQUIREMENT_DESIRED",
59 { STREAMING_REQUIREMENT_REQUIRED,
60 "STREAMING_REQUIREMENT_REQUIRED",
65 GType streaming_requirement_get_type (void) {
66 static GType type = 0;
67 if (G_UNLIKELY(type == 0)) {
68 type = g_enum_register_static ("StreamingRequirement",
69 _streaming_requirement_values);
74 static const GEnumValue _media_access_mode_values[] = {
75 { MEDIA_ACCESS_MODE_READ_ONLY,
76 "MEDIA_ACCESS_MODE_READ_ONLY",
77 (char *)"read-only" },
78 { MEDIA_ACCESS_MODE_WORM,
79 "MEDIA_ACCESS_MODE_WORM",
80 (char *)"write-once-read-many" },
81 { MEDIA_ACCESS_MODE_READ_WRITE,
82 "MEDIA_ACCESS_MODE_READ_WRITE",
83 (char *)"read-write" },
84 { MEDIA_ACCESS_MODE_WRITE_ONLY,
85 "MEDIA_ACCESS_MODE_WRITE_ONLY",
86 (char *)"write-many-read-never" },
90 GType media_access_mode_get_type (void) {
91 static GType type = 0;
92 if (G_UNLIKELY(type == 0)) {
93 type = g_enum_register_static ("MediaAccessMode",
94 _media_access_mode_values);
100 * Property registration and lookup
103 static GPtrArray *device_property_bases = NULL;
104 static GHashTable *device_property_bases_by_name = NULL;
106 DevicePropertyBase* device_property_get_by_id(DevicePropertyId id) {
107 if (!device_property_bases || id >= device_property_bases->len)
110 return g_ptr_array_index(device_property_bases, id);
113 DevicePropertyBase* device_property_get_by_name(const char *name) {
116 if (!device_property_bases_by_name)
119 rv = g_hash_table_lookup(device_property_bases_by_name, name);
121 return (DevicePropertyBase *)rv;
126 #define toupper_and_underscore(c) (((c)=='-')? '_' : g_ascii_toupper((c)))
128 device_property_hash(
131 /* modified version of glib's hash function, copyright
132 * GLib Team and others 1997-2000. */
134 guint h = toupper_and_underscore(*p);
137 for (p += 1; *p != '\0'; p++)
138 h = (h << 5) - h + toupper_and_underscore(*p);
144 device_property_equal(
148 const char *s1 = v1, *s2 = v2;
151 if (toupper_and_underscore(*s1) != toupper_and_underscore(*s2))
162 device_property_fill_and_register(DevicePropertyBase *base,
163 GType type, const char * name, const char * desc) {
165 /* create the hash table and array if necessary */
166 if (!device_property_bases) {
167 device_property_bases = g_ptr_array_new();
168 device_property_bases_by_name = g_hash_table_new(device_property_hash, device_property_equal);
171 /* check for a duplicate */
172 if (device_property_get_by_name(name)) {
173 g_critical("A property named '%s' already exists!", name);
176 /* allocate space for this DPB and fill it in */
177 base->ID = device_property_bases->len;
179 base->name = name; /* no strdup -- it's statically allocated */
180 base->description = desc; /* ditto */
182 /* add it to the array and hash table; note that its array index and its
183 * ID are the same. */
184 g_ptr_array_add(device_property_bases, base);
185 g_hash_table_insert(device_property_bases_by_name, (gpointer)name, (gpointer)base);
192 void device_property_init(void) {
193 device_property_fill_and_register(&device_property_concurrency,
194 CONCURRENCY_PARADIGM_TYPE, "concurrency",
195 "Supported concurrency mode (none, multiple readers, multiple writers)");
196 device_property_fill_and_register(&device_property_streaming,
197 STREAMING_REQUIREMENT_TYPE, "streaming",
198 "Streaming desirability (unnecessary, desired, required)");
199 device_property_fill_and_register(&device_property_compression,
200 G_TYPE_BOOLEAN, "compression",
201 "Is device performing data compression?");
202 device_property_fill_and_register(&device_property_compression_rate,
203 G_TYPE_DOUBLE, "compression_rate",
205 "averaged for some (currently undefined) period of time)");
206 device_property_fill_and_register(&device_property_block_size,
207 G_TYPE_INT, "block_size",
208 "Block size to use while writing.");
209 device_property_fill_and_register(&device_property_min_block_size,
210 G_TYPE_UINT, "min_block_size",
211 "Minimum supported blocking factor.");
212 device_property_fill_and_register(&device_property_max_block_size,
213 G_TYPE_UINT, "max_block_size",
214 "Maximum supported blocking factor.");
215 device_property_fill_and_register(&device_property_read_block_size,
216 G_TYPE_UINT, "read_block_size",
217 "Minimum size of a read for this device (maximum expected block size)");
218 device_property_fill_and_register(&device_property_appendable,
219 G_TYPE_BOOLEAN, "appendable",
220 "Does device support appending to previously-written media?");
221 device_property_fill_and_register(&device_property_canonical_name,
222 G_TYPE_STRING, "canonical_name",
223 "The most reliable device name to use to refer to this device.");
224 device_property_fill_and_register(&device_property_medium_access_type,
225 MEDIA_ACCESS_MODE_TYPE,
226 "medium_access_type",
227 "What kind of media (RO/WORM/RW/WORN) do we have here?");
228 device_property_fill_and_register(&device_property_partial_deletion,
229 G_TYPE_BOOLEAN, "partial_deletion",
230 "Does this device support recycling just part of a volume?" );
231 device_property_fill_and_register(&device_property_full_deletion,
232 G_TYPE_BOOLEAN, "full_deletion",
233 "Does this device support recycling the entire volume?" );
234 device_property_fill_and_register(&device_property_max_volume_usage,
235 G_TYPE_UINT64, "max_volume_usage",
236 "Artificial limit to data written to volume.");
237 device_property_fill_and_register(&device_property_enforce_max_volume_usage,
238 G_TYPE_BOOLEAN, "enforce_max_volume_usage",
239 "Does max_volume_usage enabled?");
240 device_property_fill_and_register(&device_property_verbose,
241 G_TYPE_BOOLEAN, "verbose",
242 "Should the device produce verbose output?");
243 device_property_fill_and_register(&device_property_comment,
244 G_TYPE_STRING, "comment",
245 "User-specified comment for the device");
246 device_property_fill_and_register(&device_property_leom,
247 G_TYPE_BOOLEAN, "leom",
248 "Does this device support LEOM?");
251 DevicePropertyBase device_property_concurrency;
252 DevicePropertyBase device_property_streaming;
253 DevicePropertyBase device_property_compression;
254 DevicePropertyBase device_property_compression_rate;
255 DevicePropertyBase device_property_block_size;
256 DevicePropertyBase device_property_min_block_size;
257 DevicePropertyBase device_property_max_block_size;
258 DevicePropertyBase device_property_read_block_size;
259 DevicePropertyBase device_property_appendable;
260 DevicePropertyBase device_property_canonical_name;
261 DevicePropertyBase device_property_medium_access_type;
262 DevicePropertyBase device_property_partial_deletion;
263 DevicePropertyBase device_property_full_deletion;
264 DevicePropertyBase device_property_max_volume_usage;
265 DevicePropertyBase device_property_enforce_max_volume_usage;
266 DevicePropertyBase device_property_comment;
267 DevicePropertyBase device_property_leom;
268 DevicePropertyBase device_property_verbose;