Debian Codegraph @

device-src/rait-device.c

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 RAIT device encapsulates some number of other devices into a single
24 * redundant device. */
25
26 #include "amanda.h"
27 #include "property.h"
28 #include "amutil.h"
29 #include <glib.h>
30 #include "glib-util.h"
31 #include "device.h"
32 #include "fileheader.h"
33 #include "amsemaphore.h"
34
35 /* Just a note about the failure mode of different operations:
36 - Recovers from a failure (enters degraded mode)
37 open_device()
38 seek_file() -- explodes if headers don't match.
39 seek_block() -- explodes if headers don't match.
40 read_block() -- explodes if data doesn't match.
41
42 - Operates in degraded mode (but dies if a new problem shows up)
43 read_label() -- but dies on label mismatch.
44 start() -- but dies when writing in degraded mode.
45 property functions
46 finish()
47
48 - Dies in degraded mode (even if remaining devices are OK)
49 start_file()
50 write_block()
51 finish_file()
52 recycle_file()
53 */
54
55 /*
56 * Type checking and casting macros
57 */
58 #define TYPE_RAIT_DEVICE (rait_device_get_type())
59 #define RAIT_DEVICE(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), rait_device_get_type(), RaitDevice)
60 #define RAIT_DEVICE_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), rait_device_get_type(), RaitDevice const)
61 #define RAIT_DEVICE_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), rait_device_get_type(), RaitDeviceClass)
62 #define IS_RAIT_DEVICE(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), rait_device_get_type ())
63
64 #define RAIT_DEVICE_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), rait_device_get_type(), RaitDeviceClass)
65 static GType rait_device_get_type (void);
66
67 /*
68 * Main object structure
69 */
70 typedef struct RaitDevice_s {
71 Device __parent__;
72
73 struct RaitDevicePrivate_s * private;
74 } RaitDevice;
75
76 /*
77 * Class definition
78 */
79 typedef struct _RaitDeviceClass RaitDeviceClass;
80 struct _RaitDeviceClass {
81 DeviceClass __parent__;
82 };
83
84 typedef enum {
85 RAIT_STATUS_COMPLETE, /* All subdevices OK. */
86 RAIT_STATUS_DEGRADED, /* One subdevice failed. */
87 RAIT_STATUS_FAILED /* Two or more subdevices failed. */
88 } RaitStatus;
89
90 /* Older versions of glib have a deadlock in their thread pool implementations,
91 * so we include a simple thread-pool implementation here to replace it.
92 *
93 * This implementation assumes that threads are used for paralellizing a single
94 * operation, so all threads run a function to completion before the main thread
95 * continues. This simplifies some of the locking semantics, and in particular
96 * there is no need to wait for stray threads to finish an operation when
97 * finalizing the RaitDevice object or when beginning a new operation.
98 */
99 #if !(GLIB_CHECK_VERSION(2,10,0))
100 #define USE_INTERNAL_THREADPOOL
101 #endif
102
103 typedef struct RaitDevicePrivate_s {
104 GPtrArray * children;
105 /* These flags are only relevant for reading. */
106 RaitStatus status;
107 /* If status == RAIT_STATUS_DEGRADED, this holds the index of the
108 failed node. It holds a negative number otherwise. */
109 int failed;
110
111 /* the child block size */
112 gsize child_block_size;
113
114 #ifdef USE_INTERNAL_THREADPOOL
115 /* array of ThreadInfo for performing parallel operations */
116 GArray *threads;
117
118 /* value of this semaphore is the number of threaded operations
119 * in progress */
120 amsemaphore_t *threads_sem;
121 #endif
122 } RaitDevicePrivate;
123
124 #ifdef USE_INTERNAL_THREADPOOL
125 typedef struct ThreadInfo {
126 GThread *thread;
127
128 /* struct fields below are protected by this mutex and condition variable */
129 GMutex *mutex;
130 GCond *cond;
131
132 gboolean die;
133 GFunc func;
134 gpointer data;
135
136 /* give threads access to active_threads and its mutex/cond */
137 struct RaitDevicePrivate_s *private;
138 } ThreadInfo;
139 #endif
140
141 /* This device uses a special sentinel node to indicate that the child devices
142 * will be set later (in rait_device_open). It contains a control character to
143 * make it difficult to enter accidentally in an Amanda config. */
144 #define DEFER_CHILDREN_SENTINEL "DEFER\1"
145
146 #define PRIVATE(o) (o->private)
147
148 #define rait_device_in_error(dev) \
149 (device_in_error((dev)) || PRIVATE(RAIT_DEVICE((dev)))->status == RAIT_STATUS_FAILED)
150
151 void rait_device_register (void);
152
153 /* here are local prototypes */
154 static void rait_device_init (RaitDevice * o);
155 static void rait_device_class_init (RaitDeviceClass * c);
156 static void rait_device_base_init (RaitDeviceClass * c);
157 static void rait_device_open_device (Device * self, char * device_name, char * device_type, char * device_node);
158 static gboolean rait_device_start (Device * self, DeviceAccessMode mode,
159 char * label, char * timestamp);
160 static gboolean rait_device_configure(Device * self, gboolean use_global_config);
161 static gboolean rait_device_start_file(Device * self, dumpfile_t * info);
162 static DeviceWriteResult rait_device_write_block(Device * self, guint size,
163 gpointer data);
164 static gboolean rait_device_finish_file (Device * self);
165 static dumpfile_t * rait_device_seek_file (Device * self, guint file);
166 static gboolean rait_device_seek_block (Device * self, guint64 block);
167 static int rait_device_read_block (Device * self, gpointer buf,
168 int * size, int max_block);
169 static gboolean rait_device_recycle_file (Device * self, guint filenum);
170 static gboolean rait_device_finish (Device * self);
171 static DeviceStatusFlags rait_device_read_label(Device * dself);
172 static void find_simple_params(RaitDevice * self, guint * num_children,
173 guint * data_children);
174
175 /* property handlers */
176
177 static gboolean property_get_block_size_fn(Device *self,
178 DevicePropertyBase *base, GValue *val,
179 PropertySurety *surety, PropertySource *source);
180
181 static gboolean property_set_block_size_fn(Device *self,
182 DevicePropertyBase *base, GValue *val,
183 PropertySurety surety, PropertySource source);
184
185 static gboolean property_get_canonical_name_fn(Device *self,
186 DevicePropertyBase *base, GValue *val,
187 PropertySurety *surety, PropertySource *source);
188
189 static gboolean property_get_concurrency_fn(Device *self,
190 DevicePropertyBase *base, GValue *val,
191 PropertySurety *surety, PropertySource *source);
192
193 static gboolean property_get_streaming_fn(Device *self,
194 DevicePropertyBase *base, GValue *val,
195 PropertySurety *surety, PropertySource *source);
196
197 static gboolean property_get_boolean_and_fn(Device *self,
198 DevicePropertyBase *base, GValue *val,
199 PropertySurety *surety, PropertySource *source);
200
201 static gboolean property_get_medium_access_type_fn(Device *self,
202 DevicePropertyBase *base, GValue *val,
203 PropertySurety *surety, PropertySource *source);
204
205 static gboolean property_get_max_volume_usage_fn(Device *self,
206 DevicePropertyBase *base, GValue *val,
207 PropertySurety *surety, PropertySource *source);
208
209 static gboolean property_set_max_volume_usage_fn(Device *self,
210 DevicePropertyBase *base, GValue *val,
211 PropertySurety surety, PropertySource source);
212
213
214 /* pointer to the class of our parent */
215 static DeviceClass *parent_class = NULL;
216
217 static GType
218 rait_device_get_type (void)
219 {
220 static GType type = 0;
221
222 if (G_UNLIKELY(type == 0)) {
223 static const GTypeInfo info = {
224 sizeof (RaitDeviceClass),
225 (GBaseInitFunc) rait_device_base_init,
226 (GBaseFinalizeFunc) NULL,
227 (GClassInitFunc) rait_device_class_init,
228 (GClassFinalizeFunc) NULL,
229 NULL /* class_data */,
230 sizeof (RaitDevice),
231 0 /* n_preallocs */,
232 (GInstanceInitFunc) rait_device_init,
233 NULL
234 };
235
236 type = g_type_register_static (TYPE_DEVICE, "RaitDevice", &info,
237 (GTypeFlags)0);
238 }
239
240 return type;
241 }
242
243 static void g_object_unref_foreach(gpointer data,
244 gpointer user_data G_GNUC_UNUSED) {
245 if (data != NULL && G_IS_OBJECT(data)) {
246 g_object_unref(data);
247 }
248 }
249
250 static void
251 rait_device_finalize(GObject *obj_self)
252 {
253 RaitDevice *self = RAIT_DEVICE (obj_self);
254 if(G_OBJECT_CLASS(parent_class)->finalize) \
255 (* G_OBJECT_CLASS(parent_class)->finalize)(obj_self);
256 if(self->private->children) {
257 g_ptr_array_foreach(self->private->children,
258 g_object_unref_foreach, NULL);
259 g_ptr_array_free (self->private->children, TRUE);
260 self->private->children = NULL;
261 }
262 #ifdef USE_INTERNAL_THREADPOOL
263 g_assert(PRIVATE(self)->threads_sem == NULL || PRIVATE(self)->threads_sem->value == 0);
264
265 if (PRIVATE(self)->threads) {
266 guint i;
267
268 for (i = 0; i < PRIVATE(self)->threads->len; i++) {
269 ThreadInfo *inf = &g_array_index(PRIVATE(self)->threads, ThreadInfo, i);
270 if (inf->thread) {
271 /* NOTE: the thread is waiting on this condition right now, not
272 * executing an operation. */
273
274 /* ask the thread to die */
275 g_mutex_lock(inf->mutex);
276 inf->die = TRUE;
277 g_cond_signal(inf->cond);
278 g_mutex_unlock(inf->mutex);
279
280 /* and wait for it to die, which should happen soon */
281 g_thread_join(inf->thread);
282 }
283
284 if (inf->mutex)
285 g_mutex_free(inf->mutex);
286 if (inf->cond)
287 g_cond_free(inf->cond);
288 }
289 }
290
291 if (PRIVATE(self)->threads_sem)
292 amsemaphore_free(PRIVATE(self)->threads_sem);
293 #endif
294 amfree(self->private);
295 }
296
297 static void
298 rait_device_init (RaitDevice * o G_GNUC_UNUSED)
299 {
300 PRIVATE(o) = g_new(RaitDevicePrivate, 1);
301 PRIVATE(o)->children = g_ptr_array_new();
302 PRIVATE(o)->status = RAIT_STATUS_COMPLETE;
303 PRIVATE(o)->failed = -1;
304 #ifdef USE_INTERNAL_THREADPOOL
305 PRIVATE(o)->threads = NULL;
306 PRIVATE(o)->threads_sem = NULL;
307 #endif
308 }
309
310 static void
311 rait_device_class_init (RaitDeviceClass * c)
312 {
313 GObjectClass *g_object_class = (GObjectClass*) c;
314 DeviceClass *device_class = (DeviceClass *)c;
315
316 parent_class = g_type_class_ref (TYPE_DEVICE);
317
318 device_class->open_device = rait_device_open_device;
319 device_class->configure = rait_device_configure;
320 device_class->start = rait_device_start;
321 device_class->start_file = rait_device_start_file;
322 device_class->write_block = rait_device_write_block;
323 device_class->finish_file = rait_device_finish_file;
324 device_class->seek_file = rait_device_seek_file;
325 device_class->seek_block = rait_device_seek_block;
326 device_class->read_block = rait_device_read_block;
327 device_class->recycle_file = rait_device_recycle_file;
328 device_class->finish = rait_device_finish;
329 device_class->read_label = rait_device_read_label;
330
331 g_object_class->finalize = rait_device_finalize;
332
333 #ifndef USE_INTERNAL_THREADPOOL
334 #if !GLIB_CHECK_VERSION(2,10,2)
335 /* Versions of glib before 2.10.2 crash if
336 * g_thread_pool_set_max_unused_threads is called before the first
337 * invocation of g_thread_pool_new. So we make up a thread pool, but don't
338 * start any threads in it, and free it */
339 {
340 GThreadPool *pool = g_thread_pool_new((GFunc)-1, NULL, -1, FALSE, NULL);
341 g_thread_pool_free(pool, TRUE, FALSE);
342 }
343 #endif
344
345 g_thread_pool_set_max_unused_threads(-1);
346 #endif
347 }
348
349 static void
350 rait_device_base_init (RaitDeviceClass * c)
351 {
352 DeviceClass *device_class = (DeviceClass *)c;
353
354 /* the RAIT device overrides most of the standard properties, so that it
355 * can calculate them by querying the same property on the children */
356 device_class_register_property(device_class, PROPERTY_BLOCK_SIZE,
357 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
358 property_get_block_size_fn,
359 property_set_block_size_fn);
360
361 device_class_register_property(device_class, PROPERTY_CANONICAL_NAME,
362 PROPERTY_ACCESS_GET_MASK,
363 property_get_canonical_name_fn, NULL);
364
365 device_class_register_property(device_class, PROPERTY_CONCURRENCY,
366 PROPERTY_ACCESS_GET_MASK,
367 property_get_concurrency_fn, NULL);
368
369 device_class_register_property(device_class, PROPERTY_STREAMING,
370 PROPERTY_ACCESS_GET_MASK,
371 property_get_streaming_fn, NULL);
372
373 device_class_register_property(device_class, PROPERTY_APPENDABLE,
374 PROPERTY_ACCESS_GET_MASK,
375 property_get_boolean_and_fn, NULL);
376
377 device_class_register_property(device_class, PROPERTY_PARTIAL_DELETION,
378 PROPERTY_ACCESS_GET_MASK,
379 property_get_boolean_and_fn, NULL);
380
381 device_class_register_property(device_class, PROPERTY_FULL_DELETION,
382 PROPERTY_ACCESS_GET_MASK,
383 property_get_boolean_and_fn, NULL);
384
385 device_class_register_property(device_class, PROPERTY_LEOM,
386 PROPERTY_ACCESS_GET_MASK,
387 property_get_boolean_and_fn, NULL);
388
389 device_class_register_property(device_class, PROPERTY_MEDIUM_ACCESS_TYPE,
390 PROPERTY_ACCESS_GET_MASK,
391 property_get_medium_access_type_fn, NULL);
392
393 device_class_register_property(device_class, PROPERTY_MAX_VOLUME_USAGE,
394 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
395 property_get_max_volume_usage_fn,
396 property_set_max_volume_usage_fn);
397 }
398
399 /* This function does something a little clever and a little
400 * complicated. It takes an array of operations and runs the given
401 * function on each element in the array. The trick is that it runs them
402 * all in parallel, in different threads. This is more efficient than it
403 * sounds because we use a GThreadPool, which means calling this function
404 * will probably not start any new threads at all, but rather use
405 * existing ones. The func is called with two gpointer arguments: The
406 * first from the array, the second is the data argument.
407 *
408 * When it returns, all the operations have been successfully
409 * executed. If you want results from your operations, do it yourself
410 * through the array.
411 */
412
413 #ifdef USE_INTERNAL_THREADPOOL
414 static gpointer rait_thread_pool_func(gpointer data) {
415 ThreadInfo *inf = data;
416
417 g_mutex_lock(inf->mutex);
418 while (TRUE) {
419 while (!inf->die && !inf->func)
420 g_cond_wait(inf->cond, inf->mutex);
421
422 if (inf->die)
423 break;
424
425 if (inf->func) {
426 /* invoke the function */
427 inf->func(inf->data, NULL);
428 inf->func = NULL;
429 inf->data = NULL;
430
431 /* indicate that we're finished; will not block */
432 amsemaphore_down(inf->private->threads_sem);
433 }
434 }
435 g_mutex_unlock(inf->mutex);
436 return NULL;
437 }
438
439 static void do_thread_pool_op(RaitDevice *self, GFunc func, GPtrArray * ops) {
440 guint i;
441
442 if (PRIVATE(self)->threads_sem == NULL)
443 PRIVATE(self)->threads_sem = amsemaphore_new_with_value(0);
444
445 if (PRIVATE(self)->threads == NULL)
446 PRIVATE(self)->threads = g_array_sized_new(FALSE, TRUE,
447 sizeof(ThreadInfo), ops->len);
448
449 g_assert(PRIVATE(self)->threads_sem->value == 0);
450
451 if (PRIVATE(self)->threads->len < ops->len)
452 g_array_set_size(PRIVATE(self)->threads, ops->len);
453
454 /* the semaphore will hit zero when each thread has decremented it */
455 amsemaphore_force_set(PRIVATE(self)->threads_sem, ops->len);
456
457 for (i = 0; i < ops->len; i++) {
458 ThreadInfo *inf = &g_array_index(PRIVATE(self)->threads, ThreadInfo, i);
459 if (!inf->thread) {
460 inf->mutex = g_mutex_new();
461 inf->cond = g_cond_new();
462 inf->private = PRIVATE(self);
463 inf->thread = g_thread_create(rait_thread_pool_func, inf, TRUE, NULL);
464 }
465
466 /* set up the info the thread needs and trigger it to start */
467 g_mutex_lock(inf->mutex);
468 inf->data = g_ptr_array_index(ops, i);
469 inf->func = func;
470 g_cond_signal(inf->cond);
471 g_mutex_unlock(inf->mutex);
472 }
473
474 /* wait until semaphore hits zero */
475 amsemaphore_wait_empty(PRIVATE(self)->threads_sem);
476 }
477
478 #else /* USE_INTERNAL_THREADPOOL */
479
480 static void do_thread_pool_op(RaitDevice *self G_GNUC_UNUSED, GFunc func, GPtrArray * ops) {
481 GThreadPool * pool;
482 guint i;
483
484 pool = g_thread_pool_new(func, NULL, -1, FALSE, NULL);
485 for (i = 0; i < ops->len; i ++) {
486 g_thread_pool_push(pool, g_ptr_array_index(ops, i), NULL);
487 }
488
489 g_thread_pool_free(pool, FALSE, TRUE);
490 }
491
492 #endif /* USE_INTERNAL_THREADPOOL */
493
494 /* This does the above, in a serial fashion (and without using threads) */
495 static void do_unthreaded_ops(RaitDevice *self G_GNUC_UNUSED, GFunc func, GPtrArray * ops) {
496 guint i;
497
498 for (i = 0; i < ops->len; i ++) {
499 func(g_ptr_array_index(ops, i), NULL);
500 }
501 }
502
503 /* This is the one that code below should call. It switches
504 automatically between do_thread_pool_op and do_unthreaded_ops,
505 depending on g_thread_supported(). */
506 static void do_rait_child_ops(RaitDevice *self, GFunc func, GPtrArray * ops) {
507 if (g_thread_supported()) {
508 do_thread_pool_op(self, func, ops);
509 } else {
510 do_unthreaded_ops(self, func, ops);
511 }
512 }
513
514 static char *
515 child_device_names_to_rait_name(RaitDevice * self) {
516 GPtrArray *kids;
517 char *braced, *result;
518 guint i;
519
520 kids = g_ptr_array_sized_new(self->private->children->len);
521 for (i = 0; i < self->private->children->len; i ++) {
522 Device *child = g_ptr_array_index(self->private->children, i);
523 const char *child_name = NULL;
524 GValue val;
525 gboolean got_prop = FALSE;
526
527 bzero(&val, sizeof(val));
528
529 if ((signed)i != self->private->failed) {
530 if (device_property_get(child, PROPERTY_CANONICAL_NAME, &val)) {
531 child_name = g_value_get_string(&val);
532 got_prop = TRUE;
533 }
534 }
535
536 if (!got_prop)
537 child_name = "MISSING";
538
539 g_ptr_array_add(kids, g_strdup(child_name));
540
541 if (got_prop)
542 g_value_unset(&val);
543 }
544
545 braced = collapse_braced_alternates(kids);
546 result = g_strdup_printf("rait:%s", braced);
547 g_free(braced);
548
549 return result;
550 }
551
552 /* Find a workable child block size, based on the block size ranges of our
553 * child devices.
554 *
555 * The algorithm is to construct the intersection of all child devices'
556 * [min,max] block size ranges, and then pick the block size closest to 32k
557 * that is in the resulting range. This avoids picking ridiculously small (1
558 * byte) or large (INT_MAX) block sizes when using devices with wide-open block
559 * size ranges.
560
561 * This function returns the calculated child block size directly, and the RAIT
562 * device's blocksize via rait_size, if not NULL. It is resilient to errors in
563 * a single child device, but sets the device's error status and returns 0 if
564 * it cannot determine an agreeable block size.
565 */
566 static gsize
567 calculate_block_size_from_children(RaitDevice * self, gsize *rait_size)
568 {
569 gsize min = 0;
570 gsize max = SIZE_MAX;
571 gboolean found_one = FALSE;
572 gsize result;
573 guint i;
574
575 for (i = 0; i < self->private->children->len; i ++) {
576 gsize child_min = SIZE_MAX, child_max = 0;
577 Device *child;
578 GValue property_result;
579 PropertySource source;
580
581 bzero(&property_result, sizeof(property_result));
582
583 if ((signed)i == self->private->failed)
584 continue;
585
586 child = g_ptr_array_index(self->private->children, i);
587 if (!device_property_get_ex(child, PROPERTY_BLOCK_SIZE,
588 &property_result, NULL, &source)) {
589 g_warning("Error getting BLOCK_SIZE from %s: %s",
590 child->device_name, device_error_or_status(child));
591 continue;
592 }
593
594 /* if the block size has been set explicitly, then we need to use that blocksize;
595 * otherwise (even if it was DETECTED), override it. */
596 if (source == PROPERTY_SOURCE_USER) {
597 child_min = child_max = g_value_get_int(&property_result);
598 } else {
599 if (!device_property_get(child, PROPERTY_MIN_BLOCK_SIZE,
600 &property_result)) {
601 g_warning("Error getting MIN_BLOCK_SIZE from %s: %s",
602 child->device_name, device_error_or_status(child));
603 continue;
604 }
605 child_min = g_value_get_uint(&property_result);
606
607 if (!device_property_get(child, PROPERTY_MAX_BLOCK_SIZE,
608 &property_result)) {
609 g_warning("Error getting MAX_BLOCK_SIZE from %s: %s",
610 child->device_name, device_error_or_status(child));
611 continue;
612 }
613 child_max = g_value_get_uint(&property_result);
614
615 if (child_min == 0 || child_max == 0 || (child_min > child_max)) {
616 g_warning("Invalid min, max block sizes from %s", child->device_name);
617 continue;
618 }
619 }
620
621 found_one = TRUE;
622 min = MAX(min, child_min);
623 max = MIN(max, child_max);
624 }
625
626 if (!found_one) {
627 device_set_error((Device*)self,
628 g_strdup(_("Could not find any child devices' block size ranges")),
629 DEVICE_STATUS_DEVICE_ERROR);
630 return 0;
631 }
632
633 if (min > max) {
634 device_set_error((Device*)self,
635 g_strdup(_("No block size is acceptable to all child devices")),
636 DEVICE_STATUS_DEVICE_ERROR);
637 return 0;
638 }
639
640 /* Now pick a number. If 32k is in range, we use that; otherwise, we use
641 * the nearest acceptable size. */
642 result = CLAMP(32768, min, max);
643
644 if (rait_size) {
645 guint data_children;
646 find_simple_params(self, NULL, &data_children);
647 *rait_size = result * data_children;
648 }
649
650 return result;
651 }
652
653 /* Set BLOCK_SIZE on all children */
654 static gboolean
655 set_block_size_on_children(RaitDevice *self, gsize child_block_size)
656 {
657 GValue val;
658 guint i;
659 PropertySource source;
660 char *r;
661
662 bzero(&val, sizeof(val));
663
664 g_assert(child_block_size < INT_MAX);
665 g_value_init(&val, G_TYPE_INT);
666 g_value_set_int(&val, (gint)child_block_size);
667
668 for (i = 0; i < self->private->children->len; i ++) {
669 Device *child;
670 GValue property_result;
671
672 bzero(&property_result, sizeof(property_result));
673
674 if ((signed)i == self->private->failed)
675 continue;
676
677 child = g_ptr_array_index(self->private->children, i);
678
679 /* first, make sure the block size is at its default, or is already
680 * correct */
681 if (device_property_get_ex(child, PROPERTY_BLOCK_SIZE,
682 &property_result, NULL, &source)) {
683 gsize from_child = g_value_get_int(&property_result);
684 g_value_unset(&property_result);
685 if (source != PROPERTY_SOURCE_DEFAULT
686 && from_child != child_block_size) {
687 device_set_error((Device *)self,
688 g_strdup_printf(_("Child device %s already has its block size set to %zd, not %zd"),
689 child->device_name, from_child, child_block_size),
690 DEVICE_STATUS_DEVICE_ERROR);
691 return FALSE;
692 }
693 } else {
694 /* failing to get the block size isn't necessarily fatal.. */
695 g_warning("Error getting BLOCK_SIZE from %s: %s",
696 child->device_name, device_error_or_status(child));
697 }
698
699 r = device_property_set(child, PROPERTY_BLOCK_SIZE, &val);
700 if (r) {
701 device_set_error((Device *)self,
702 g_strdup_printf(_("Error setting block size on %s: %s"), child->device_name, r),
703 DEVICE_STATUS_DEVICE_ERROR);
704 g_free(r);
705 return FALSE;
706 }
707 }
708
709 return TRUE;
710 }
711
712 /* The time for users to specify block sizes has ended; set this device's
713 * block-size attributes for easy access by other RAIT functions. Returns
714 * FALSE on error, with the device's error status already set. */
715 static gboolean
716 fix_block_size(RaitDevice *self)
717 {
718 Device *dself = (Device *)self;
719 gsize my_block_size, child_block_size;
720
721 if (dself->block_size_source == PROPERTY_SOURCE_DEFAULT) {
722 child_block_size = calculate_block_size_from_children(self, &my_block_size);
723 if (child_block_size == 0)
724 return FALSE;
725
726 self->private->child_block_size = child_block_size;
727 dself->block_size = my_block_size;
728 dself->block_size_surety = PROPERTY_SURETY_GOOD;
729 dself->block_size_source = PROPERTY_SOURCE_DETECTED;
730 } else {
731 guint data_children;
732
733 find_simple_params(self, NULL, &data_children);
734 g_assert((dself->block_size % data_children) == 0);
735 child_block_size = dself->block_size / data_children;
736 }
737
738 /* now tell the children we mean it */
739 if (!set_block_size_on_children(self, child_block_size))
740 return FALSE;
741
742 return TRUE;
743 }
744
745 /* This structure contains common fields for many operations. Not all
746 operations use all fields, however. */
747 typedef struct {
748 gpointer result; /* May be a pointer; may be an integer or boolean
749 stored with GINT_TO_POINTER. */
750 Device * child; /* The device in question. Used by all
751 operations. */
752 guint child_index; /* For recoverable operations (read-related
753 operations), this field provides the number
754 of this child in the self->private->children
755 array. */
756 } GenericOp;
757
758 typedef gboolean (*BooleanExtractor)(gpointer data);
759
760 /* A BooleanExtractor */
761 static gboolean extract_boolean_generic_op(gpointer data) {
762 GenericOp * op = data;
763 return GPOINTER_TO_INT(op->result);
764 }
765
766 /* A BooleanExtractor */
767 static gboolean extract_boolean_pointer_op(gpointer data) {
768 GenericOp * op = data;
769 return op->result != NULL;
770 }
771
772 /* Does the equivalent of this perl command:
773 ! (first { !extractor($_) } @_
774 That is, calls extractor on each element of the array, and returns
775 TRUE if and only if all calls to extractor return TRUE. This function
776 stops as soon as an extractor returns false, so it's best if extractor
777 functions have no side effects.
778 */
779 static gboolean g_ptr_array_and(GPtrArray * array,
780 BooleanExtractor extractor) {
781 guint i;
782 if (array == NULL || array->len <= 0)
783 return FALSE;
784
785 for (i = 0; i < array->len; i ++) {
786 if (!extractor(g_ptr_array_index(array, i)))
787 return FALSE;
788 }
789
790 return TRUE;
791 }
792
793 /* Takes a RaitDevice, and makes a GPtrArray of GenericOp. */
794 static GPtrArray * make_generic_boolean_op_array(RaitDevice* self) {
795 GPtrArray * rval;
796 guint i;
797
798 rval = g_ptr_array_sized_new(self->private->children->len);
799 for (i = 0; i < self->private->children->len; i ++) {
800 GenericOp * op;
801
802 if ((signed)i == self->private->failed) {
803 continue;
804 }
805
806 op = g_new(GenericOp, 1);
807 op->child = g_ptr_array_index(self->private->children, i);
808 op->child_index = i;
809 g_ptr_array_add(rval, op);
810 }
811
812 return rval;
813 }
814
815 /* Takes a GPtrArray of GenericOp, and a BooleanExtractor, and does
816 all the proper handling for the result of operations that allow
817 device isolation. Returns FALSE only if an unrecoverable error
818 occured. */
819 static gboolean g_ptr_array_union_robust(RaitDevice * self, GPtrArray * ops,
820 BooleanExtractor extractor) {
821 int nfailed = 0;
822 int lastfailed = 0;
823 guint i;
824
825 /* We found one or more failed elements. See which elements failed, and
826 * isolate them*/
827 for (i = 0; i < ops->len; i ++) {
828 GenericOp * op = g_ptr_array_index(ops, i);
829 if (!extractor(op)) {
830 self->private->failed = op->child_index;
831 g_warning("RAIT array %s isolated device %s: %s",
832 DEVICE(self)->device_name,
833 op->child->device_name,
834 device_error(op->child));
835 nfailed++;
836 lastfailed = i;
837 }
838 }
839
840 /* no failures? great! */
841 if (nfailed == 0)
842 return TRUE;
843
844 /* a single failure in COMPLETE just puts us in DEGRADED mode */
845 if (self->private->status == RAIT_STATUS_COMPLETE && nfailed == 1) {
846 self->private->status = RAIT_STATUS_DEGRADED;
847 self->private->failed = lastfailed;
848 g_warning("RAIT array %s DEGRADED", DEVICE(self)->device_name);
849 return TRUE;
850 } else {
851 self->private->status = RAIT_STATUS_FAILED;
852 g_warning("RAIT array %s FAILED", DEVICE(self)->device_name);
853 return FALSE;
854 }
855 }
856
857 typedef struct {
858 RaitDevice * self;
859 char *rait_name;
860 char * device_name; /* IN */
861 Device * result; /* OUT */
862 } OpenDeviceOp;
863
864 /* A GFunc. */
865 static void device_open_do_op(gpointer data,
866 gpointer user_data G_GNUC_UNUSED) {
867 OpenDeviceOp * op = data;
868
869 if (g_str_equal(op->device_name, "ERROR") ||
870 g_str_equal(op->device_name, "MISSING") ||
871 g_str_equal(op->device_name, "DEGRADED")) {
872 g_warning("RAIT device %s contains a missing element, attempting "
873 "degraded mode.\n", op->rait_name);
874 op->result = NULL;
875 } else {
876 op->result = device_open(op->device_name);
877 }
878 }
879
880 /* Returns TRUE if and only if the volume label and time are equal. */
881 static gboolean compare_volume_results(Device * a, Device * b) {
882 return (0 == compare_possibly_null_strings(a->volume_time, b->volume_time)
883 && 0 == compare_possibly_null_strings(a->volume_label, b->volume_label));
884 }
885
886 /* Stickes new_message at the end of *old_message; frees new_message and
887 * may change *old_message. */
888 static void append_message(char ** old_message, char * new_message) {
889 char * rval;
890 if (*old_message == NULL || **old_message == '\0') {
891 rval = new_message;
892 } else {
893 rval = g_strdup_printf("%s; %s", *old_message, new_message);
894 amfree(new_message);
895 }
896 amfree(*old_message);
897 *old_message = rval;
898 }
899
900 static gboolean
901 open_child_devices (Device * dself, char * device_name,
902 char * device_node) {
903 GPtrArray *device_names;
904 GPtrArray * device_open_ops;
905 guint i;
906 gboolean failure;
907 char *failure_errmsgs;
908 DeviceStatusFlags failure_flags;
909 RaitDevice * self;
910
911 self = RAIT_DEVICE(dself);
912
913 device_names = expand_braced_alternates(device_node);
914
915 if (device_names == NULL) {
916 device_set_error(dself,
917 g_strdup_printf(_("Invalid RAIT device name '%s'"), device_name),
918 DEVICE_STATUS_DEVICE_ERROR);
919 return FALSE;
920 }
921
922 /* Open devices in a separate thread, in case they have to rewind etc. */
923 device_open_ops = g_ptr_array_new();
924
925 for (i = 0; i < device_names->len; i++) {
926 OpenDeviceOp *op;
927 char *name = g_ptr_array_index(device_names, i);
928
929 op = g_new(OpenDeviceOp, 1);
930 op->device_name = name;
931 op->result = NULL;
932 op->self = self;
933 op->rait_name = device_name;
934 g_ptr_array_add(device_open_ops, op);
935 }
936
937 g_ptr_array_free(device_names, TRUE);
938 do_rait_child_ops(self, device_open_do_op, device_open_ops);
939
940 failure = FALSE;
941 failure_errmsgs = NULL;
942 failure_flags = 0;
943
944 /* Check results of opening devices. */
945 for (i = 0; i < device_open_ops->len; i ++) {
946 OpenDeviceOp *op = g_ptr_array_index(device_open_ops, i);
947
948 if (op->result != NULL &&
949 op->result->status == DEVICE_STATUS_SUCCESS) {
950 g_ptr_array_add(self->private->children, op->result);
951 } else {
952 char * this_failure_errmsg =
953 g_strdup_printf("%s: %s", op->device_name,
954 device_error_or_status(op->result));
955 DeviceStatusFlags status =
956 op->result == NULL ?
957 DEVICE_STATUS_DEVICE_ERROR : op->result->status;
958 append_message(&failure_errmsgs,
959 strdup(this_failure_errmsg));
960 failure_flags |= status;
961 if (self->private->status == RAIT_STATUS_COMPLETE) {
962 /* The first failure just puts us in degraded mode. */
963 g_warning("%s: %s",
964 device_name, this_failure_errmsg);
965 g_warning("%s: %s failed, entering degraded mode.",
966 device_name, op->device_name);
967 g_ptr_array_add(self->private->children, op->result);
968 self->private->status = RAIT_STATUS_DEGRADED;
969 self->private->failed = i;
970 } else {
971 /* The second and further failures are fatal. */
972 failure = TRUE;
973 }
974 }
975 amfree(op->device_name);
976 }
977
978 g_ptr_array_free_full(device_open_ops);
979
980 if (failure) {
981 self->private->status = RAIT_STATUS_FAILED;
982 device_set_error(dself, failure_errmsgs, failure_flags);
983 return FALSE;
984 }
985
986 return TRUE;
987 }
988
989 static void
990 rait_device_open_device (Device * dself, char * device_name,
991 char * device_type G_GNUC_UNUSED, char * device_node) {
992
993 if (!g_str_equal(device_node, DEFER_CHILDREN_SENTINEL)) {
994 if (!open_child_devices(dself, device_name, device_node))
995 return;
996
997 /* Chain up. */
998 if (parent_class->open_device) {
999 parent_class->open_device(dself, device_name, device_type, device_node);
1000 }
1001 }
1002 }
1003
1004 Device *
1005 rait_device_open_from_children (GSList *child_devices) {
1006 Device *dself;
1007 RaitDevice *self;
1008 GSList *iter;
1009 char *device_name;
1010 int nfailures;
1011 int i;
1012
1013 /* first, open a RAIT device using the DEFER_CHILDREN_SENTINEL */
1014 dself = device_open("rait:" DEFER_CHILDREN_SENTINEL);
1015 if (!IS_RAIT_DEVICE(dself)) {
1016 return dself;
1017 }
1018
1019 /* set its children */
1020 self = RAIT_DEVICE(dself);
1021 nfailures = 0;
1022 for (i=0, iter = child_devices; iter; i++, iter = iter->next) {
1023 Device *kid = iter->data;
1024
1025 /* a NULL kid is OK -- it opens the device in degraded mode */
1026 if (!kid) {
1027 nfailures++;
1028 self->private->failed = i;
1029 } else {
1030 g_assert(IS_DEVICE(kid));
1031 g_object_ref((GObject *)kid);
1032 }
1033
1034 g_ptr_array_add(self->private->children, kid);
1035 }
1036
1037 /* and set the status based on the children */
1038 switch (nfailures) {
1039 case 0:
1040 self->private->status = RAIT_STATUS_COMPLETE;
1041 break;
1042
1043 case 1:
1044 self->private->status = RAIT_STATUS_DEGRADED;
1045 break;
1046
1047 default:
1048 self->private->status = RAIT_STATUS_FAILED;
1049 device_set_error(dself,
1050 _("more than one child device is missing"),
1051 DEVICE_STATUS_DEVICE_ERROR);
1052 break;
1053 }
1054
1055 /* create a name from the children's names and use it to chain up
1056 * to open_device (we skipped this step in rait_device_open_device) */
1057 device_name = child_device_names_to_rait_name(self);
1058
1059 if (parent_class->open_device) {
1060 parent_class->open_device(dself,
1061 device_name, "rait",
1062 device_name+5); /* (+5 skips "rait:") */
1063 }
1064
1065 return dself;
1066 }
1067
1068 /* A GFunc. */
1069 static void read_label_do_op(gpointer data,
1070 gpointer user_data G_GNUC_UNUSED) {
1071 GenericOp * op = data;
1072 op->result = GINT_TO_POINTER(device_read_label(op->child));
1073 }
1074
1075 static DeviceStatusFlags rait_device_read_label(Device * dself) {
1076 RaitDevice * self;
1077 GPtrArray * ops;
1078 DeviceStatusFlags failed_result = 0;
1079 char *failed_errmsg = NULL;
1080 unsigned int i;
1081 Device * first_success = NULL;
1082
1083 self = RAIT_DEVICE(dself);
1084
1085 amfree(dself->volume_time);
1086 amfree(dself->volume_label);
1087 dumpfile_free(dself->volume_header);
1088 dself->volume_header = NULL;
1089
1090 if (rait_device_in_error(self))
1091 return dself->status | DEVICE_STATUS_DEVICE_ERROR;
1092
1093 /* nail down our block size, if we haven't already */
1094 if (!fix_block_size(self))
1095 return FALSE;
1096
1097 ops = make_generic_boolean_op_array(self);
1098
1099 do_rait_child_ops(self, read_label_do_op, ops);
1100
1101 for (i = 0; i < ops->len; i ++) {
1102 GenericOp * op = g_ptr_array_index(ops, i);
1103 DeviceStatusFlags result = GPOINTER_TO_INT(op->result);
1104 if (result == DEVICE_STATUS_SUCCESS) {
1105 if (first_success == NULL) {
1106 /* This is the first successful device. */
1107 first_success = op->child;
1108 } else if (!compare_volume_results(first_success, op->child)) {
1109 /* Doesn't match. :-( */
1110 failed_errmsg = g_strdup_printf("Inconsistent volume labels/datestamps: "
1111 "Got %s/%s on %s against %s/%s on %s.",
1112 first_success->volume_label,
1113 first_success->volume_time,
1114 first_success->device_name,
1115 op->child->volume_label,
1116 op->child->volume_time,
1117 op->child->device_name);
1118 g_warning("%s", failed_errmsg);
1119 failed_result |= DEVICE_STATUS_VOLUME_ERROR;
1120 }
1121 } else {
1122 failed_result |= result;
1123 }
1124 }
1125
1126 if (failed_result != DEVICE_STATUS_SUCCESS) {
1127 /* We had multiple failures or an inconsistency. */
1128 device_set_error(dself, failed_errmsg, failed_result);
1129 } else {
1130 /* Everything peachy. */
1131 amfree(failed_errmsg);
1132
1133 g_assert(first_success != NULL);
1134 if (first_success->volume_label != NULL) {
1135 dself->volume_label = g_strdup(first_success->volume_label);
1136 }
1137 if (first_success->volume_time != NULL) {
1138 dself->volume_time = g_strdup(first_success->volume_time);
1139 }
1140 if (first_success->volume_header != NULL) {
1141 dself->volume_header = dumpfile_copy(first_success->volume_header);
1142 }
1143 dself->header_block_size = first_success->header_block_size;
1144 }
1145
1146 g_ptr_array_free_full(ops);
1147
1148 return dself->status;
1149 }
1150
1151 typedef struct {
1152 GenericOp base;
1153 DeviceAccessMode mode; /* IN */
1154 char * label; /* IN */
1155 char * timestamp; /* IN */
1156 } StartOp;
1157
1158 /* A GFunc. */
1159 static void start_do_op(gpointer data, gpointer user_data G_GNUC_UNUSED) {
1160 DeviceClass *klass;
1161 StartOp * param = data;
1162
1163 klass = DEVICE_GET_CLASS(param->base.child);
1164 if (klass->start) {
1165 param->base.result =
1166 GINT_TO_POINTER((klass->start)(param->base.child,
1167 param->mode, param->label,
1168 param->timestamp));
1169 } else {
1170 param->base.result = FALSE;
1171 }
1172 }
1173
1174 static gboolean
1175 rait_device_configure(Device * dself, gboolean use_global_config)
1176 {
1177 RaitDevice *self = RAIT_DEVICE(dself);
1178 guint i;
1179
1180 for (i = 0; i < self->private->children->len; i ++) {
1181 Device *child;
1182
1183 if ((signed)i == self->private->failed)
1184 continue;
1185
1186 child = g_ptr_array_index(self->private->children, i);
1187 /* unconditionally configure the child without the global
1188 * configuration */
1189 if (!device_configure(child, FALSE))
1190 return FALSE;
1191 }
1192
1193 if (parent_class->configure) {
1194 return parent_class->configure(dself, use_global_config);
1195 }
1196
1197 return TRUE;
1198 }
1199
1200 static gboolean
1201 rait_device_start (Device * dself, DeviceAccessMode mode, char * label,
1202 char * timestamp) {
1203 GPtrArray * ops;
1204 guint i;
1205 gboolean success;
1206 RaitDevice * self;
1207 DeviceStatusFlags total_status;
1208 char *failure_errmsgs = NULL;
1209 char * label_from_device = NULL;
1210
1211 self = RAIT_DEVICE(dself);
1212
1213 if (rait_device_in_error(self)) return FALSE;
1214
1215 /* No starting in degraded mode. */
1216 if (self->private->status != RAIT_STATUS_COMPLETE &&
1217 (mode == ACCESS_WRITE || mode == ACCESS_APPEND)) {
1218 device_set_error(dself,
1219 g_strdup_printf(_("RAIT device %s is read-only "
1220 "because it is in degraded mode.\n"),
1221 dself->device_name),
1222 DEVICE_STATUS_DEVICE_ERROR);
1223 return FALSE;
1224 }
1225
1226 /* nail down our block size, if we haven't already */
1227 if (!fix_block_size(self))
1228 return FALSE;
1229
1230 dself->access_mode = mode;
1231 g_mutex_lock(dself->device_mutex);
1232 dself->in_file = FALSE;
1233 g_mutex_unlock(dself->device_mutex);
1234 amfree(dself->volume_label);
1235 amfree(dself->volume_time);
1236 dumpfile_free(dself->volume_header);
1237 dself->volume_header = NULL;
1238
1239 ops = g_ptr_array_sized_new(self->private->children->len);
1240 for (i = 0; i < self->private->children->len; i ++) {
1241 StartOp * op;
1242
1243 if ((signed)i == self->private->failed) {
1244 continue;
1245 }
1246
1247 op = g_new(StartOp, 1);
1248 op->base.child = g_ptr_array_index(self->private->children, i);
1249 op->mode = mode;
1250 op->label = g_strdup(label);
1251 op->timestamp = g_strdup(timestamp);
1252 g_ptr_array_add(ops, op);
1253 }
1254
1255 do_rait_child_ops(self, start_do_op, ops);
1256
1257 success = g_ptr_array_and(ops, extract_boolean_generic_op);
1258
1259 /* Check results of starting devices; this is mostly about the
1260 * VOLUME_UNLABELED flag. */
1261 total_status = 0;
1262 for (i = 0; i < ops->len; i ++) {
1263 StartOp * op = g_ptr_array_index(ops, i);
1264 Device *child = op->base.child;
1265
1266 total_status |= child->status;
1267 if (child->status != DEVICE_STATUS_SUCCESS) {
1268 /* record the error message and move on. */
1269 append_message(&failure_errmsgs,
1270 g_strdup_printf("%s: %s",
1271 child->device_name,
1272 device_error_or_status(child)));
1273 } else {
1274 if (child->volume_label != NULL && child->volume_time != NULL) {
1275 if (label_from_device) {
1276 if (!g_str_equal(child->volume_label, dself->volume_label) ||
1277 !g_str_equal(child->volume_time, dself->volume_time)) {
1278 /* Mismatch! (Two devices provided different labels) */
1279 char * this_message =
1280 g_strdup_printf("%s: Label (%s/%s) is different "
1281 "from label (%s/%s) found at "
1282 "device %s",
1283 child->device_name,
1284 child->volume_label,
1285 child->volume_time,
1286 dself->volume_label,
1287 dself->volume_time,
1288 label_from_device);
1289 append_message(&failure_errmsgs, this_message);
1290 total_status |= DEVICE_STATUS_DEVICE_ERROR;
1291 g_warning("RAIT device children have different labels or timestamps");
1292 }
1293 } else {
1294 /* First device with a volume. */
1295 dself->volume_label = g_strdup(child->volume_label);
1296 dself->volume_time = g_strdup(child->volume_time);
1297 dself->volume_header = dumpfile_copy(child->volume_header);
1298 label_from_device = g_strdup(child->device_name);
1299 }
1300 } else {
1301 /* Device problem, it says it succeeded but sets no label? */
1302 char * this_message =
1303 g_strdup_printf("%s: Says label read, but no volume "
1304 "label found.", child->device_name);
1305 g_warning("RAIT device child has NULL volume or label");
1306 append_message(&failure_errmsgs, this_message);
1307 total_status |= DEVICE_STATUS_DEVICE_ERROR;
1308 }
1309 }
1310 }
1311
1312 if (total_status == DEVICE_STATUS_SUCCESS) {
1313 StartOp * op = g_ptr_array_index(ops, 0);
1314 Device *child = op->base.child;
1315 dself->header_block_size = child->header_block_size;
1316 }
1317
1318 amfree(label_from_device);
1319 g_ptr_array_free_full(ops);
1320
1321 dself->status = total_status;
1322
1323 if (total_status != DEVICE_STATUS_SUCCESS || !success) {
1324 device_set_error(dself, failure_errmsgs, total_status);
1325 return FALSE;
1326 }
1327 amfree(failure_errmsgs);
1328 return TRUE;
1329 }
1330
1331 typedef struct {
1332 GenericOp base;
1333 dumpfile_t * info; /* IN */
1334 int fileno;
1335 } StartFileOp;
1336
1337 /* a GFunc */
1338 static void start_file_do_op(gpointer data, gpointer user_data G_GNUC_UNUSED) {
1339 StartFileOp * op = data;
1340 op->base.result = GINT_TO_POINTER(device_start_file(op->base.child,
1341 op->info));
1342 op->fileno = op->base.child->file;
1343 if (op->fileno < 1) {
1344 op->base.result = FALSE;
1345 }
1346 }
1347
1348 static gboolean
1349 rait_device_start_file (Device * dself, dumpfile_t * info) {
1350 GPtrArray * ops;
1351 guint i;
1352 gboolean success;
1353 RaitDevice * self;
1354 int actual_file = -1;
1355
1356 self = RAIT_DEVICE(dself);
1357
1358 if (rait_device_in_error(self)) return FALSE;
1359 if (self->private->status != RAIT_STATUS_COMPLETE) return FALSE;
1360
1361 ops = g_ptr_array_sized_new(self->private->children->len);
1362 for (i = 0; i < self->private->children->len; i ++) {
1363 StartFileOp * op;
1364 op = g_new(StartFileOp, 1);
1365 op->base.child = g_ptr_array_index(self->private->children, i);
1366 /* each child gets its own copy of the header, to munge as it
1367 * likes (setting blocksize, at least) */
1368 op->info = dumpfile_copy(info);
1369 g_ptr_array_add(ops, op);
1370 }
1371
1372 do_rait_child_ops(self, start_file_do_op, ops);
1373
1374 success = g_ptr_array_and(ops, extract_boolean_generic_op);
1375
1376 for (i = 0; i < self->private->children->len && success; i ++) {
1377 StartFileOp * op = g_ptr_array_index(ops, i);
1378 if (!op->base.result)
1379 continue;
1380 g_assert(op->fileno >= 1);
1381 if (actual_file < 1) {
1382 actual_file = op->fileno;
1383 }
1384 if (actual_file != op->fileno) {
1385 /* File number mismatch! Aah, my hair is on fire! */
1386 device_set_error(dself,
1387 g_strdup_printf("File number mismatch in "
1388 "rait_device_start_file(): "
1389 "Child %s reported file number "
1390 "%d, another child reported "
1391 "file number %d.",
1392 op->base.child->device_name,
1393 op->fileno, actual_file),
1394 DEVICE_STATUS_DEVICE_ERROR);
1395 success = FALSE;
1396 op->base.result = FALSE;
1397 }
1398 }
1399
1400 for (i = 0; i < ops->len && success; i ++) {
1401 StartFileOp * op = g_ptr_array_index(ops, i);
1402 if (op->info) dumpfile_free(op->info);
1403 }
1404 g_ptr_array_free_full(ops);
1405
1406 if (!success) {
1407 if (!device_in_error(dself)) {
1408 device_set_error(dself, g_strdup("One or more devices "
1409 "failed to start_file"),
1410 DEVICE_STATUS_DEVICE_ERROR);
1411 }
1412 return FALSE;
1413 }
1414
1415 g_assert(actual_file >= 1);
1416 dself->file = actual_file;
1417 g_mutex_lock(dself->device_mutex);
1418 dself->in_file = TRUE;
1419 dself->bytes_written = 0;
1420 g_mutex_unlock(dself->device_mutex);
1421
1422 return TRUE;
1423 }
1424
1425 static void find_simple_params(RaitDevice * self,
1426 guint * num_children,
1427 guint * data_children) {
1428 int num, data;
1429
1430 num = self->private->children->len;
1431 if (num > 1)
1432 data = num - 1;
1433 else
1434 data = num;
1435 if (num_children != NULL)
1436 *num_children = num;
1437 if (data_children != NULL)
1438 *data_children = data;
1439 }
1440
1441 typedef struct {
1442 GenericOp base;
1443 guint size; /* IN */
1444 gpointer data; /* IN */
1445 gboolean data_needs_free; /* bookkeeping */
1446 } WriteBlockOp;
1447
1448 /* a GFunc. */
1449 static void write_block_do_op(gpointer data,
1450 gpointer user_data G_GNUC_UNUSED) {
1451 WriteBlockOp * op = data;
1452
1453 op->base.result =
1454 GINT_TO_POINTER(device_write_block(op->base.child, op->size, op->data));
1455 }
1456
1457 /* Parity block generation. Performance of this function can be improved
1458 considerably by using larger-sized integers or
1459 assembly-coded vector instructions. Parameters are:
1460 % data - All data chunks in series (chunk_size * num_chunks bytes)
1461 % parity - Allocated space for parity block (chunk_size bytes)
1462 */
1463 static void make_parity_block(char * data, char * parity,
1464 guint chunk_size, guint num_chunks) {
1465 guint i;
1466 bzero(parity, chunk_size);
1467 for (i = 0; i < num_chunks - 1; i ++) {
1468 guint j;
1469 for (j = 0; j < chunk_size; j ++) {
1470 parity[j] ^= data[chunk_size*i + j];
1471 }
1472 }
1473 }
1474
1475 /* Does the same thing as make_parity_block, but instead of using a
1476 single memory chunk holding all chunks, it takes a GPtrArray of
1477 chunks. */
1478 static void make_parity_block_extents(GPtrArray * data, char * parity,
1479 guint chunk_size) {
1480 guint i;
1481 bzero(parity, chunk_size);
1482 for (i = 0; i < data->len; i ++) {
1483 guint j;
1484 char * data_chunk;
1485 data_chunk = g_ptr_array_index(data, i);
1486 for (j = 0; j < chunk_size; j ++) {
1487 parity[j] ^= data_chunk[j];
1488 }
1489 }
1490 }
1491
1492 /* Does the parity creation algorithm. Allocates and returns a single
1493 device block from a larger RAIT block. chunks and chunk are 1-indexed. */
1494 static char * extract_data_block(char * data, guint size,
1495 guint chunks, guint chunk) {
1496 char * rval;
1497 guint chunk_size;
1498
1499 g_assert(chunks > 0 && chunk > 0 && chunk <= chunks);
1500 g_assert(data != NULL);
1501 g_assert(size > 0 && size % (chunks - 1) == 0);
1502
1503 chunk_size = size / (chunks - 1);
1504 rval = g_malloc(chunk_size);
1505 if (chunks != chunk) {
1506 /* data block. */
1507 memcpy(rval, data + chunk_size * (chunk - 1), chunk_size);
1508 } else {
1509 make_parity_block(data, rval, chunk_size, chunks);
1510 }
1511
1512 return rval;
1513 }
1514
1515 static DeviceWriteResult
1516 rait_device_write_block (Device * dself, guint size, gpointer data) {
1517 GPtrArray * ops;
1518 guint i;
1519 DeviceWriteResult rait_result;
1520 guint data_children, num_children;
1521 gsize blocksize = dself->block_size;
1522 RaitDevice * self;
1523 gboolean last_block = (size < blocksize);
1524
1525 self = RAIT_DEVICE(dself);
1526
1527 if (rait_device_in_error(self)) return WRITE_FAILED;
1528 if (self->private->status != RAIT_STATUS_COMPLETE) return WRITE_FAILED;
1529
1530 find_simple_params(RAIT_DEVICE(self), &num_children, &data_children);
1531 num_children = self->private->children->len;
1532 if (num_children != 1)
1533 data_children = num_children - 1;
1534 else
1535 data_children = num_children;
1536
1537 g_assert(size % data_children == 0 || last_block);
1538
1539 /* zero out to the end of a short block -- tape devices only write
1540 * whole blocks. */
1541 if (last_block) {
1542 char *new_data;
1543
1544 new_data = g_malloc(blocksize);
1545 memcpy(new_data, data, size);
1546 bzero(new_data + size, blocksize - size);
1547
1548 data = new_data;
1549 size = blocksize;
1550 }
1551
1552 ops = g_ptr_array_sized_new(num_children);
1553 for (i = 0; i < self->private->children->len; i ++) {
1554 WriteBlockOp * op;
1555 op = g_malloc(sizeof(*op));
1556 op->base.child = g_ptr_array_index(self->private->children, i);
1557 op->size = size / data_children;
1558 if (num_children <= 2) {
1559 op->data = data;
1560 op->data_needs_free = FALSE;
1561 } else {
1562 op->data_needs_free = TRUE;
1563 op->data = extract_data_block(data, size, num_children, i + 1);
1564 }
1565 g_ptr_array_add(ops, op);
1566 }
1567
1568 do_rait_child_ops(self, write_block_do_op, ops);
1569
1570 rait_result = WRITE_SUCCEED;
1571 for (i = 0; i < self->private->children->len; i ++) {
1572 WriteBlockOp * op = g_ptr_array_index(ops, i);
1573 DeviceWriteResult result = GPOINTER_TO_INT(op->base.result);
1574 if (result == WRITE_SUCCEED && rait_result == WRITE_SUCCEED) {
1575 } else if (result == WRITE_SPACE && rait_result == WRITE_SUCCEED) {
1576 } else {
1577 rait_result = WRITE_FAILED;
1578 }
1579 if (op->data_needs_free)
1580 free(op->data);
1581 }
1582
1583 if (last_block) {
1584 amfree(data);
1585 }
1586
1587 g_ptr_array_free_full(ops);
1588
1589 if (rait_result != WRITE_SUCCEED) {
1590 /* TODO be more specific here */
1591 /* TODO: handle EOM here -- if one or more (or two or more??)
1592 * children have is_eom set, then reflect that in our error
1593 * status. What's more fun is when one device fails and must be isolated at
1594 * the same time another hits EOF. */
1595 device_set_error(dself,
1596 g_strdup("One or more devices failed to write_block"),
1597 DEVICE_STATUS_DEVICE_ERROR);
1598 /* this is EOM or an error, so call it EOM */
1599 dself->is_eom = TRUE;
1600 return WRITE_FAILED;
1601 } else {
1602 dself->block ++;
1603 g_mutex_lock(dself->device_mutex);
1604 dself->bytes_written += size;
1605 g_mutex_unlock(dself->device_mutex);
1606
1607 return WRITE_SUCCEED;
1608 }
1609 }
1610
1611 /* A GFunc */
1612 static void finish_file_do_op(gpointer data,
1613 gpointer user_data G_GNUC_UNUSED) {
1614 GenericOp * op = data;
1615 if (op->child) {
1616 op->result = GINT_TO_POINTER(device_finish_file(op->child));
1617 } else {
1618 op->result = FALSE;
1619 }
1620 }
1621
1622 static gboolean
1623 rait_device_finish_file (Device * dself) {
1624 GPtrArray * ops;
1625 gboolean success;
1626 RaitDevice * self = RAIT_DEVICE(dself);
1627
1628 g_assert(self != NULL);
1629 if (!dself->in_file)
1630 return TRUE;
1631
1632 if (rait_device_in_error(dself)) return FALSE;
1633 if (self->private->status != RAIT_STATUS_COMPLETE) return FALSE;
1634
1635 ops = make_generic_boolean_op_array(self);
1636
1637 do_rait_child_ops(self, finish_file_do_op, ops);
1638
1639 success = g_ptr_array_and(ops, extract_boolean_generic_op);
1640
1641 g_ptr_array_free_full(ops);
1642
1643 if (!success) {
1644 /* TODO: be more specific here */
1645 device_set_error(dself,
1646 g_strdup("One or more devices failed to finish_file"),
1647 DEVICE_STATUS_DEVICE_ERROR);
1648 return FALSE;
1649 }
1650
1651 g_mutex_lock(dself->device_mutex);
1652 dself->in_file = FALSE;
1653 g_mutex_unlock(dself->device_mutex);
1654 return TRUE;
1655 }
1656
1657 typedef struct {
1658 GenericOp base;
1659 guint requested_file; /* IN */
1660 guint actual_file; /* OUT */
1661 } SeekFileOp;
1662
1663 /* a GFunc. */
1664 static void seek_file_do_op(gpointer data, gpointer user_data G_GNUC_UNUSED) {
1665 SeekFileOp * op = data;
1666 op->base.result = device_seek_file(op->base.child, op->requested_file);
1667 op->actual_file = op->base.child->file;
1668 }
1669
1670 static dumpfile_t *
1671 rait_device_seek_file (Device * dself, guint file) {
1672 GPtrArray * ops;
1673 guint i;
1674 gboolean success;
1675 dumpfile_t * rval;
1676 RaitDevice * self = RAIT_DEVICE(dself);
1677 guint actual_file = 0;
1678 gboolean in_file = FALSE;
1679
1680 if (rait_device_in_error(self)) return NULL;
1681
1682 dself->is_eof = FALSE;
1683 dself->block = 0;
1684 g_mutex_lock(dself->device_mutex);
1685 dself->in_file = FALSE;
1686 dself->bytes_read = 0;
1687 g_mutex_unlock(dself->device_mutex);
1688
1689 ops = g_ptr_array_sized_new(self->private->children->len);
1690 for (i = 0; i < self->private->children->len; i ++) {
1691 SeekFileOp * op;
1692 if ((int)i == self->private->failed)
1693 continue; /* This device is broken. */
1694 op = g_new(SeekFileOp, 1);
1695 op->base.child = g_ptr_array_index(self->private->children, i);
1696 op->base.child_index = i;
1697 op->requested_file = file;
1698 g_ptr_array_add(ops, op);
1699 }
1700
1701 do_rait_child_ops(self, seek_file_do_op, ops);
1702
1703 /* This checks for NULL values, but we still have to check for
1704 consistant headers. */
1705 success = g_ptr_array_union_robust(RAIT_DEVICE(self),
1706 ops, extract_boolean_pointer_op);
1707
1708 rval = NULL;
1709 for (i = 0; i < ops->len; i ++) {
1710 SeekFileOp * this_op;
1711 dumpfile_t * this_result;
1712 guint this_actual_file;
1713 gboolean this_in_file;
1714
1715 this_op = (SeekFileOp*)g_ptr_array_index(ops, i);
1716
1717 if ((signed)this_op->base.child_index == self->private->failed)
1718 continue;
1719
1720 this_result = this_op->base.result;
1721 this_actual_file = this_op->actual_file;
1722 this_in_file = this_op->base.child->in_file;
1723
1724 if (rval == NULL) {
1725 rval = this_result;
1726 actual_file = this_actual_file;
1727 in_file = this_in_file;
1728 } else {
1729 if (headers_are_equal(rval, this_result) &&
1730 actual_file == this_actual_file &&
1731 in_file == this_in_file) {
1732 /* Do nothing. */
1733 } else {
1734 success = FALSE;
1735 }
1736 free(this_result);
1737 }
1738 }
1739
1740 g_ptr_array_free_full(ops);
1741
1742 if (!success) {
1743 amfree(rval);
1744 /* TODO: be more specific here */
1745 device_set_error(dself,
1746 g_strdup("One or more devices failed to seek_file"),
1747 DEVICE_STATUS_DEVICE_ERROR);
1748 return NULL;
1749 }
1750
1751 /* update our state */
1752 g_mutex_lock(dself->device_mutex);
1753 dself->in_file = in_file;
1754 g_mutex_unlock(dself->device_mutex);
1755 dself->file = actual_file;
1756
1757 return rval;
1758 }
1759
1760 typedef struct {
1761 GenericOp base;
1762 guint64 block; /* IN */
1763 } SeekBlockOp;
1764
1765 /* a GFunc. */
1766 static void seek_block_do_op(gpointer data, gpointer user_data G_GNUC_UNUSED) {
1767 SeekBlockOp * op = data;
1768 op->base.result =
1769 GINT_TO_POINTER(device_seek_block(op->base.child, op->block));
1770 }
1771
1772 static gboolean
1773 rait_device_seek_block (Device * dself, guint64 block) {
1774 GPtrArray * ops;
1775 guint i;
1776 gboolean success;
1777
1778 RaitDevice * self = RAIT_DEVICE(dself);
1779
1780 if (rait_device_in_error(self)) return FALSE;
1781
1782 ops = g_ptr_array_sized_new(self->private->children->len);
1783 for (i = 0; i < self->private->children->len; i ++) {
1784 SeekBlockOp * op;
1785 if ((int)i == self->private->failed)
1786 continue; /* This device is broken. */
1787 op = g_new(SeekBlockOp, 1);
1788 op->base.child = g_ptr_array_index(self->private->children, i);
1789 op->base.child_index = i;
1790 op->block = block;
1791 g_ptr_array_add(ops, op);
1792 }
1793
1794 do_rait_child_ops(self, seek_block_do_op, ops);
1795
1796 success = g_ptr_array_union_robust(RAIT_DEVICE(self),
1797 ops, extract_boolean_generic_op);
1798
1799 g_ptr_array_free_full(ops);
1800
1801 if (!success) {
1802 /* TODO: be more specific here */
1803 device_set_error(dself,
1804 g_strdup("One or more devices failed to seek_block"),
1805 DEVICE_STATUS_DEVICE_ERROR);
1806 return FALSE;
1807 }
1808
1809 dself->block = block;
1810 return TRUE;
1811 }
1812
1813 typedef struct {
1814 GenericOp base;
1815 gpointer buffer; /* IN */
1816 int read_size; /* IN/OUT -- note not a pointer */
1817 int desired_read_size; /* bookkeeping */
1818 int max_block;
1819 } ReadBlockOp;
1820
1821 /* a GFunc. */
1822 static void read_block_do_op(gpointer data,
1823 gpointer user_data G_GNUC_UNUSED) {
1824 ReadBlockOp * op = data;
1825 op->base.result =
1826 GINT_TO_POINTER(device_read_block(op->base.child, op->buffer,
1827 &(op->read_size), op->max_block));
1828 if (op->read_size > op->desired_read_size) {
1829 g_warning("child device %s tried to return an oversized block, which the RAIT device does not support",
1830 op->base.child->device_name);
1831 }
1832 }
1833
1834 /* A BooleanExtractor. This one checks for a successful read. */
1835 static gboolean extract_boolean_read_block_op_data(gpointer data) {
1836 ReadBlockOp * op = data;
1837 return GPOINTER_TO_INT(op->base.result) == op->desired_read_size;
1838 }
1839
1840 /* A BooleanExtractor. This one checks for EOF. */
1841 static gboolean extract_boolean_read_block_op_eof(gpointer data) {
1842 ReadBlockOp * op = data;
1843 return op->base.child->is_eof;
1844 }
1845
1846 /* Counts the number of elements in an array matching a given proposition. */
1847 static int g_ptr_array_count(GPtrArray * array, BooleanExtractor filter) {
1848 int rval;
1849 unsigned int i;
1850 rval = 0;
1851 for (i = 0; i < array->len ; i++) {
1852 if (filter(g_ptr_array_index(array, i)))
1853 rval ++;
1854 }
1855 return rval;
1856 }
1857
1858 static gboolean raid_block_reconstruction(RaitDevice * self, GPtrArray * ops,
1859 gpointer buf, size_t bufsize) {
1860 guint num_children, data_children;
1861 gsize blocksize;
1862 gsize child_blocksize;
1863 guint i;
1864 int parity_child;
1865 gpointer parity_block = NULL;
1866 gboolean success;
1867
1868 success = TRUE;
1869
1870 blocksize = DEVICE(self)->block_size;
1871 find_simple_params(self, &num_children, &data_children);
1872
1873 if (num_children > 1)
1874 parity_child = num_children - 1;
1875 else
1876 parity_child = -1;
1877
1878 child_blocksize = blocksize / data_children;
1879
1880 for (i = 0; i < ops->len; i ++) {
1881 ReadBlockOp * op = g_ptr_array_index(ops, i);
1882 if (!extract_boolean_read_block_op_data(op))
1883 continue;
1884 if ((int)(op->base.child_index) == parity_child) {
1885 parity_block = op->buffer;
1886 } else {
1887 g_assert(child_blocksize * (op->base.child_index+1) <= bufsize);
1888 memcpy((char *)buf + child_blocksize * op->base.child_index, op->buffer,
1889 child_blocksize);
1890 }
1891 }
1892 if (self->private->status == RAIT_STATUS_COMPLETE) {
1893 g_assert(parity_block != NULL); /* should have found parity_child */
1894
1895 if (num_children >= 2) {
1896 /* Verify the parity block. This code is inefficient but
1897 does the job for the 2-device case, too. */
1898 gpointer constructed_parity;
1899 GPtrArray * data_extents;
1900
1901 constructed_parity = g_malloc(child_blocksize);
1902 data_extents = g_ptr_array_sized_new(data_children);
1903 for (i = 0; i < data_children; i ++) {
1904 ReadBlockOp * op = g_ptr_array_index(ops, i);
1905 g_assert(extract_boolean_read_block_op_data(op));
1906 if ((int)op->base.child_index == parity_child)
1907 continue;
1908 g_ptr_array_add(data_extents, op->buffer);
1909 }
1910 make_parity_block_extents(data_extents, constructed_parity,
1911 child_blocksize);
1912
1913 if (0 != memcmp(parity_block, constructed_parity,
1914 child_blocksize)) {
1915 device_set_error(DEVICE(self),
1916 g_strdup(_("RAIT is inconsistent: Parity block did not match data blocks.")),
1917 DEVICE_STATUS_DEVICE_ERROR);
1918 /* TODO: can't we just isolate the device in this case? */
1919 success = FALSE;
1920 }
1921 g_ptr_array_free(data_extents, TRUE);
1922 amfree(constructed_parity);
1923 } else { /* do nothing. */ }
1924 } else if (self->private->status == RAIT_STATUS_DEGRADED) {
1925 g_assert(self->private->failed >= 0 && self->private->failed < (int)num_children);
1926 /* We are in degraded mode. What's missing? */
1927 if (self->private->failed == parity_child) {
1928 /* do nothing. */
1929 } else if (num_children >= 2) {
1930 /* Reconstruct failed block from parity block. */
1931 GPtrArray * data_extents = g_ptr_array_new();
1932
1933 for (i = 0; i < data_children; i ++) {
1934 ReadBlockOp * op = g_ptr_array_index(ops, i);
1935 if (!extract_boolean_read_block_op_data(op))
1936 continue;
1937 g_ptr_array_add(data_extents, op->buffer);
1938 }
1939
1940 /* Conveniently, the reconstruction is the same procedure
1941 as the parity generation. This even works if there is
1942 only one remaining device! */
1943 make_parity_block_extents(data_extents,
1944 (char *)buf + (child_blocksize *
1945 self->private->failed),
1946 child_blocksize);
1947
1948 /* The array members belong to our ops argument. */
1949 g_ptr_array_free(data_extents, TRUE);
1950 } else {
1951 g_assert_not_reached();
1952 }
1953 } else {
1954 /* device is already in FAILED state -- we shouldn't even be here */
1955 success = FALSE;
1956 }
1957 return success;
1958 }
1959
1960 static int
1961 rait_device_read_block (Device * dself, gpointer buf, int * size, int max_block) {
1962 GPtrArray * ops;
1963 guint i;
1964 gboolean success;
1965 guint num_children, data_children;
1966 gsize blocksize = dself->block_size;
1967 gsize child_blocksize;
1968
1969 RaitDevice * self = RAIT_DEVICE(dself);
1970
1971 if (rait_device_in_error(self)) return -1;
1972
1973 find_simple_params(self, &num_children, &data_children);
1974
1975 /* tell caller they haven't given us a big enough buffer */
1976 if (blocksize > (gsize)*size) {
1977 g_assert(blocksize < INT_MAX);
1978 *size = (int)blocksize;
1979 return 0;
1980 }
1981
1982 g_assert(blocksize % data_children == 0); /* see find_block_size */
1983 child_blocksize = blocksize / data_children;
1984
1985 ops = g_ptr_array_sized_new(num_children);
1986 for (i = 0; i < num_children; i ++) {
1987 ReadBlockOp * op;
1988 if ((int)i == self->private->failed)
1989 continue; /* This device is broken. */
1990 op = g_new(ReadBlockOp, 1);
1991 op->base.child = g_ptr_array_index(self->private->children, i);
1992 op->base.child_index = i;
1993 op->buffer = g_malloc(child_blocksize);
1994 op->desired_read_size = op->read_size = child_blocksize;
1995 op->max_block = max_block;
1996 g_ptr_array_add(ops, op);
1997 }
1998
1999 do_rait_child_ops(self, read_block_do_op, ops);
2000
2001 if (g_ptr_array_count(ops, extract_boolean_read_block_op_data)) {
2002 if (!g_ptr_array_union_robust(RAIT_DEVICE(self),
2003 ops,
2004 extract_boolean_read_block_op_data)) {
2005 /* TODO: be more specific */
2006 device_set_error(dself,
2007 g_strdup(_("Error occurred combining blocks from child devices")),
2008 DEVICE_STATUS_DEVICE_ERROR);
2009 success = FALSE;
2010 } else {
2011 /* raid_block_reconstruction sets the error status if necessary */
2012 success = raid_block_reconstruction(RAIT_DEVICE(self),
2013 ops, buf, (size_t)*size);
2014 }
2015 } else {
2016 success = FALSE;
2017 if (g_ptr_array_union_robust(RAIT_DEVICE(self),
2018 ops,
2019 extract_boolean_read_block_op_eof)) {
2020 device_set_error(dself,
2021 g_strdup(_("EOF")),
2022 DEVICE_STATUS_SUCCESS);
2023 dself->is_eof = TRUE;
2024 g_mutex_lock(dself->device_mutex);
2025 dself->in_file = FALSE;
2026 g_mutex_unlock(dself->device_mutex);
2027 } else {
2028 device_set_error(dself,
2029 g_strdup(_("All child devices failed to read, but not all are at eof")),
2030 DEVICE_STATUS_DEVICE_ERROR);
2031 }
2032 }
2033
2034 for (i = 0; i < ops->len; i ++) {
2035 ReadBlockOp * op = g_ptr_array_index(ops, i);
2036 amfree(op->buffer);
2037 }
2038 g_ptr_array_free_full(ops);
2039
2040 if (success) {
2041 dself->block++;
2042 *size = blocksize;
2043 g_mutex_lock(dself->device_mutex);
2044 dself->bytes_read += blocksize;
2045 g_mutex_unlock(dself->device_mutex);
2046 return blocksize;
2047 } else {
2048 return -1;
2049 }
2050 }
2051
2052 /* property utility functions */
2053
2054 typedef struct {
2055 GenericOp base;
2056 DevicePropertyId id; /* IN */
2057 GValue value; /* IN/OUT */
2058 PropertySurety surety; /* IN (for set) */
2059 PropertySource source; /* IN (for set) */
2060 } PropertyOp;
2061
2062 /* Creates a GPtrArray of PropertyOf for a get or set operation. */
2063 static GPtrArray * make_property_op_array(RaitDevice * self,
2064 DevicePropertyId id,
2065 GValue * value,
2066 PropertySurety surety,
2067 PropertySource source) {
2068 guint i;
2069 GPtrArray * ops;
2070 ops = g_ptr_array_sized_new(self->private->children->len);
2071 for (i = 0; i < self->private->children->len; i ++) {
2072 PropertyOp * op;
2073
2074 if ((signed)i == self->private->failed) {
2075 continue;
2076 }
2077
2078 op = g_new(PropertyOp, 1);
2079 op->base.child = g_ptr_array_index(self->private->children, i);
2080 op->id = id;
2081 bzero(&(op->value), sizeof(op->value));
2082 if (value != NULL) {
2083 g_value_unset_copy(value, &(op->value));
2084 }
2085 op->surety = surety;
2086 op->source = source;
2087 g_ptr_array_add(ops, op);
2088 }
2089
2090 return ops;
2091 }
2092
2093 /* A GFunc. */
2094 static void property_get_do_op(gpointer data,
2095 gpointer user_data G_GNUC_UNUSED) {
2096 PropertyOp * op = data;
2097
2098 bzero(&(op->value), sizeof(op->value));
2099 op->base.result =
2100 GINT_TO_POINTER(device_property_get(op->base.child, op->id,
2101 &(op->value)));
2102 }
2103
2104 /* A GFunc. */
2105 static void property_set_do_op(gpointer data,
2106 gpointer user_data G_GNUC_UNUSED) {
2107 PropertyOp * op = data;
2108
2109 op->base.result = device_property_set_ex(op->base.child, op->id,
2110 &(op->value), op->surety,
2111 op->source);
2112 g_value_unset(&(op->value));
2113 }
2114
2115 /* PropertyGetFns and PropertySetFns */
2116
2117 static gboolean
2118 property_get_block_size_fn(Device *dself,
2119 DevicePropertyBase *base G_GNUC_UNUSED, GValue *val,
2120 PropertySurety *surety, PropertySource *source)
2121 {
2122 RaitDevice *self = RAIT_DEVICE(dself);
2123 gsize my_block_size;
2124
2125 if (dself->block_size_source != PROPERTY_SOURCE_DEFAULT) {
2126 my_block_size = dself->block_size;
2127
2128 if (surety)
2129 *surety = dself->block_size_surety;
2130 } else {
2131 gsize child_block_size;
2132 child_block_size = calculate_block_size_from_children(self,
2133 &my_block_size);
2134 if (child_block_size == 0)
2135 return FALSE;
2136
2137 if (surety)
2138 *surety = PROPERTY_SURETY_BAD; /* may still change */
2139 }
2140
2141 if (val) {
2142 g_value_unset_init(val, G_TYPE_INT);
2143 g_assert(my_block_size < G_MAXINT); /* gsize -> gint */
2144 g_value_set_int(val, (gint)my_block_size);
2145 }
2146
2147 if (source)
2148 *source = dself->block_size_source;
2149
2150 return TRUE;
2151 }
2152
2153 static gboolean
2154 property_set_block_size_fn(Device *dself,
2155 DevicePropertyBase *base G_GNUC_UNUSED, GValue *val,
2156 PropertySurety surety, PropertySource source)
2157 {
2158 RaitDevice *self = RAIT_DEVICE(dself);
2159 gint my_block_size = g_value_get_int(val);
2160 guint data_children;
2161
2162 find_simple_params(self, NULL, &data_children);
2163 if ((my_block_size % data_children) != 0) {
2164 device_set_error(dself,
2165 g_strdup_printf(_("Block size must be a multiple of %d"), data_children),
2166 DEVICE_STATUS_DEVICE_ERROR);
2167 return FALSE;
2168 }
2169
2170 dself->block_size = my_block_size;
2171 dself->block_size_source = source;
2172 dself->block_size_surety = surety;
2173
2174 if (!fix_block_size(self))
2175 return FALSE;
2176
2177 return TRUE;
2178 }
2179
2180 static gboolean
2181 property_get_canonical_name_fn(Device *dself,
2182 DevicePropertyBase *base G_GNUC_UNUSED, GValue *val,
2183 PropertySurety *surety, PropertySource *source)
2184 {
2185 RaitDevice *self = RAIT_DEVICE(dself);
2186 char *canonical = child_device_names_to_rait_name(self);
2187
2188 if (val) {
2189 g_value_unset_init(val, G_TYPE_STRING);
2190 g_value_set_string(val, canonical);
2191 g_free(canonical);
2192 }
2193
2194 if (surety)
2195 *surety = PROPERTY_SURETY_GOOD;
2196
2197 if (source)
2198 *source = PROPERTY_SOURCE_DETECTED;
2199
2200 return TRUE;
2201 }
2202
2203 static gboolean
2204 property_get_concurrency_fn(Device *dself,
2205 DevicePropertyBase *base G_GNUC_UNUSED, GValue *val,
2206 PropertySurety *surety, PropertySource *source)
2207 {
2208 RaitDevice *self = RAIT_DEVICE(dself);
2209 ConcurrencyParadigm result;
2210 guint i;
2211 GPtrArray * ops;
2212 gboolean success;
2213
2214 ops = make_property_op_array(self, PROPERTY_CONCURRENCY, NULL, 0, 0);
2215 do_rait_child_ops(self, property_get_do_op, ops);
2216
2217 /* find the most restrictive paradigm acceptable to all
2218 * child devices */
2219 result = CONCURRENCY_PARADIGM_RANDOM_ACCESS;
2220 success = TRUE;
2221 for (i = 0; i < ops->len; i ++) {
2222 ConcurrencyParadigm cur;
2223 PropertyOp * op = g_ptr_array_index(ops, i);
2224
2225 if (!op->base.result
2226 || G_VALUE_TYPE(&(op->value)) != CONCURRENCY_PARADIGM_TYPE) {
2227 success = FALSE;
2228 break;
2229 }
2230
2231 cur = g_value_get_enum(&(op->value));
2232 if (result == CONCURRENCY_PARADIGM_EXCLUSIVE ||
2233 cur == CONCURRENCY_PARADIGM_EXCLUSIVE) {
2234 result = CONCURRENCY_PARADIGM_EXCLUSIVE;
2235 } else if (result == CONCURRENCY_PARADIGM_SHARED_READ ||
2236 cur == CONCURRENCY_PARADIGM_SHARED_READ) {
2237 result = CONCURRENCY_PARADIGM_SHARED_READ;
2238 } else if (result == CONCURRENCY_PARADIGM_RANDOM_ACCESS &&
2239 cur == CONCURRENCY_PARADIGM_RANDOM_ACCESS) {
2240 result = CONCURRENCY_PARADIGM_RANDOM_ACCESS;
2241 } else {
2242 success = FALSE;
2243 break;
2244 }
2245 }
2246
2247 g_ptr_array_free_full(ops);
2248
2249 if (success) {
2250 if (val) {
2251 g_value_unset_init(val, CONCURRENCY_PARADIGM_TYPE);
2252 g_value_set_enum(val, result);
2253 }
2254
2255 if (surety)
2256 *surety = PROPERTY_SURETY_GOOD;
2257
2258 if (source)
2259 *source = PROPERTY_SOURCE_DETECTED;
2260 }
2261
2262 return success;
2263 }
2264
2265 static gboolean
2266 property_get_streaming_fn(Device *dself,
2267 DevicePropertyBase *base G_GNUC_UNUSED, GValue *val,
2268 PropertySurety *surety, PropertySource *source)
2269 {
2270 RaitDevice *self = RAIT_DEVICE(dself);
2271 StreamingRequirement result;
2272 guint i;
2273 GPtrArray * ops;
2274 gboolean success;
2275
2276 ops = make_property_op_array(self, PROPERTY_STREAMING, NULL, 0, 0);
2277 do_rait_child_ops(self, property_get_do_op, ops);
2278
2279 /* combine the child streaming requirements, selecting the strongest
2280 * requirement of the bunch. */
2281 result = STREAMING_REQUIREMENT_NONE;
2282 success = TRUE;
2283 for (i = 0; i < ops->len; i ++) {
2284 StreamingRequirement cur;
2285 PropertyOp * op = g_ptr_array_index(ops, i);
2286
2287 if (!op->base.result
2288 || G_VALUE_TYPE(&(op->value)) != STREAMING_REQUIREMENT_TYPE) {
2289 success = FALSE;
2290 break;
2291 }
2292
2293 cur = g_value_get_enum(&(op->value));
2294 if (result == STREAMING_REQUIREMENT_REQUIRED ||
2295 cur == STREAMING_REQUIREMENT_REQUIRED) {
2296 result = STREAMING_REQUIREMENT_REQUIRED;
2297 } else if (result == STREAMING_REQUIREMENT_DESIRED ||
2298 cur == STREAMING_REQUIREMENT_DESIRED) {
2299 result = STREAMING_REQUIREMENT_DESIRED;
2300 } else if (result == STREAMING_REQUIREMENT_NONE &&
2301 cur == STREAMING_REQUIREMENT_NONE) {
2302 result = STREAMING_REQUIREMENT_NONE;
2303 } else {
2304 success = FALSE;
2305 break;
2306 }
2307 }
2308
2309 g_ptr_array_free_full(ops);
2310
2311 if (success) {
2312 if (val) {
2313 g_value_unset_init(val, STREAMING_REQUIREMENT_TYPE);
2314 g_value_set_enum(val, result);
2315 }
2316
2317 if (surety)
2318 *surety = PROPERTY_SURETY_GOOD;
2319
2320 if (source)
2321 *source = PROPERTY_SOURCE_DETECTED;
2322 }
2323
2324 return success;
2325 }
2326
2327 static gboolean
2328 property_get_boolean_and_fn(Device *dself,
2329 DevicePropertyBase *base, GValue *val,
2330 PropertySurety *surety, PropertySource *source)
2331 {
2332 RaitDevice *self = RAIT_DEVICE(dself);
2333 gboolean result;
2334 guint i;
2335 GPtrArray * ops;
2336 gboolean success;
2337
2338 ops = make_property_op_array(self, base->ID, NULL, 0, 0);
2339 do_rait_child_ops(self, property_get_do_op, ops);
2340
2341 /* combine the child values, applying a simple AND */
2342 result = TRUE;
2343 success = TRUE;
2344 for (i = 0; i < ops->len; i ++) {
2345 PropertyOp * op = g_ptr_array_index(ops, i);
2346
2347 if (!op->base.result || !G_VALUE_HOLDS_BOOLEAN(&(op->value))) {
2348 success = FALSE;
2349 break;
2350 }
2351
2352 if (!g_value_get_boolean(&(op->value))) {
2353 result = FALSE;
2354 break;
2355 }
2356 }
2357
2358 g_ptr_array_free_full(ops);
2359
2360 if (success) {
2361 if (val) {
2362 g_value_unset_init(val, G_TYPE_BOOLEAN);
2363 g_value_set_boolean(val, result);
2364 }
2365
2366 if (surety)
2367 *surety = PROPERTY_SURETY_GOOD;
2368
2369 if (source)
2370 *source = PROPERTY_SOURCE_DETECTED;
2371 }
2372
2373 return success;
2374 }
2375
2376 static gboolean
2377 property_get_medium_access_type_fn(Device *dself,
2378 DevicePropertyBase *base G_GNUC_UNUSED, GValue *val,
2379 PropertySurety *surety, PropertySource *source)
2380 {
2381 RaitDevice *self = RAIT_DEVICE(dself);
2382 MediaAccessMode result;
2383 guint i;
2384 GPtrArray * ops;
2385 gboolean success;
2386
2387 ops = make_property_op_array(self, PROPERTY_MEDIUM_ACCESS_TYPE, NULL, 0, 0);
2388 do_rait_child_ops(self, property_get_do_op, ops);
2389
2390 /* combine the modes as best we can */
2391 result = 0;
2392 success = TRUE;
2393 for (i = 0; i < ops->len; i ++) {
2394 MediaAccessMode cur;
2395 PropertyOp * op = g_ptr_array_index(ops, i);
2396
2397 if (!op->base.result || G_VALUE_TYPE(&(op->value)) != MEDIA_ACCESS_MODE_TYPE) {
2398 success = FALSE;
2399 break;
2400 }
2401
2402 cur = g_value_get_enum(&(op->value));
2403
2404 if (i == 0) {
2405 result = cur;
2406 } else if ((result == MEDIA_ACCESS_MODE_READ_ONLY &&
2407 cur == MEDIA_ACCESS_MODE_WRITE_ONLY) ||
2408 (result == MEDIA_ACCESS_MODE_WRITE_ONLY &&
2409 cur == MEDIA_ACCESS_MODE_READ_ONLY)) {
2410 /* Invalid combination; one device can only read, other
2411 can only write. */
2412 success = FALSE;
2413 break;
2414 } else if (result == MEDIA_ACCESS_MODE_READ_ONLY ||
2415 cur == MEDIA_ACCESS_MODE_READ_ONLY) {
2416 result = MEDIA_ACCESS_MODE_READ_ONLY;
2417 } else if (result == MEDIA_ACCESS_MODE_WRITE_ONLY ||
2418 cur == MEDIA_ACCESS_MODE_WRITE_ONLY) {
2419 result = MEDIA_ACCESS_MODE_WRITE_ONLY;
2420 } else if (result == MEDIA_ACCESS_MODE_WORM ||
2421 cur == MEDIA_ACCESS_MODE_WORM) {
2422 result = MEDIA_ACCESS_MODE_WORM;
2423 } else if (result == MEDIA_ACCESS_MODE_READ_WRITE &&
2424 cur == MEDIA_ACCESS_MODE_READ_WRITE) {
2425 result = MEDIA_ACCESS_MODE_READ_WRITE;
2426 } else {
2427 success = FALSE;
2428 break;
2429 }
2430 }
2431
2432 g_ptr_array_free_full(ops);
2433
2434 if (success) {
2435 if (val) {
2436 g_value_unset_init(val, MEDIA_ACCESS_MODE_TYPE);
2437 g_value_set_enum(val, result);
2438 }
2439
2440 if (surety)
2441 *surety = PROPERTY_SURETY_GOOD;
2442
2443 if (source)
2444 *source = PROPERTY_SOURCE_DETECTED;
2445 }
2446
2447 return success;
2448 }
2449
2450 static gboolean
2451 property_get_max_volume_usage_fn(Device *dself,
2452 DevicePropertyBase *base G_GNUC_UNUSED, GValue *val,
2453 PropertySurety *surety, PropertySource *source)
2454 {
2455 RaitDevice *self = RAIT_DEVICE(dself);
2456 guint64 result;
2457 guint i;
2458 GPtrArray * ops;
2459 guint data_children;
2460
2461 ops = make_property_op_array(self, PROPERTY_MAX_VOLUME_USAGE, NULL, 0, 0);
2462 do_rait_child_ops(self, property_get_do_op, ops);
2463
2464 /* look for the smallest value that is set */
2465 result = 0;
2466 for (i = 0; i < ops->len; i ++) {
2467 guint64 cur;
2468 PropertyOp * op = g_ptr_array_index(ops, i);
2469
2470 if (!op->base.result || !G_VALUE_HOLDS_UINT64(&(op->value))) {
2471 continue; /* ignore children without this property */
2472 }
2473
2474 cur = g_value_get_uint64(&(op->value));
2475
2476 if (!result || (cur && cur < result)) {
2477 result = cur;
2478 }
2479 }
2480
2481 g_ptr_array_free_full(ops);
2482
2483 if (result) {
2484 /* result contains the minimum usage on any child. We can use that space
2485 * on each of our data children, so the total is larger */
2486 find_simple_params(self, NULL, &data_children);
2487 result *= data_children;
2488
2489 if (val) {
2490 g_value_unset_init(val, G_TYPE_UINT64);
2491 g_value_set_uint64(val, result);
2492 }
2493
2494 if (surety)
2495 *surety = PROPERTY_SURETY_GOOD;
2496
2497 if (source)
2498 *source = PROPERTY_SOURCE_DETECTED;
2499
2500 return TRUE;
2501 } else {
2502 /* no result from any children, so we effectively don't have this property */
2503 return FALSE;
2504 }
2505 }
2506
2507 static gboolean
2508 property_set_max_volume_usage_fn(Device *dself,
2509 DevicePropertyBase *base G_GNUC_UNUSED, GValue *val,
2510 PropertySurety surety, PropertySource source)
2511 {
2512 RaitDevice *self = RAIT_DEVICE(dself);
2513 guint64 parent_usage;
2514 guint64 child_usage;
2515 GValue child_val;
2516 guint i;
2517 gboolean success;
2518 GPtrArray * ops;
2519 guint data_children;
2520
2521 parent_usage = g_value_get_uint64(val);
2522 find_simple_params(self, NULL, &data_children);
2523
2524 child_usage = parent_usage / data_children;
2525
2526 bzero(&child_val, sizeof(child_val));
2527 g_value_init(&child_val, G_TYPE_UINT64);
2528 g_value_set_uint64(&child_val, child_usage);
2529
2530 ops = make_property_op_array(self, PROPERTY_MAX_VOLUME_USAGE,
2531 &child_val, surety, source);
2532 do_rait_child_ops(self, property_set_do_op, ops);
2533
2534 /* if any of the kids succeeded, then we did too */
2535 success = FALSE;
2536 for (i = 0; i < ops->len; i ++) {
2537 PropertyOp * op = g_ptr_array_index(ops, i);
2538
2539 if (!op->base.result) {
2540 success = TRUE;
2541 break;
2542 }
2543 }
2544
2545 g_ptr_array_free_full(ops);
2546
2547 return success;
2548 }
2549
2550 typedef struct {
2551 GenericOp base;
2552 guint filenum;
2553 } RecycleFileOp;
2554
2555 /* A GFunc */
2556 static void recycle_file_do_op(gpointer data,
2557 gpointer user_data G_GNUC_UNUSED) {
2558 RecycleFileOp * op = data;
2559 op->base.result =
2560 GINT_TO_POINTER(device_recycle_file(op->base.child, op->filenum));
2561 }
2562
2563 static gboolean
2564 rait_device_recycle_file (Device * dself, guint filenum) {
2565 GPtrArray * ops;
2566 guint i;
2567 gboolean success;
2568
2569 RaitDevice * self = RAIT_DEVICE(dself);
2570
2571 if (rait_device_in_error(self)) return FALSE;
2572
2573 ops = g_ptr_array_sized_new(self->private->children->len);
2574 for (i = 0; i < self->private->children->len; i ++) {
2575 RecycleFileOp * op;
2576 op = g_new(RecycleFileOp, 1);
2577 op->base.child = g_ptr_array_index(self->private->children, i);
2578 op->filenum = filenum;
2579 g_ptr_array_add(ops, op);
2580 }
2581
2582 do_rait_child_ops(self, recycle_file_do_op, ops);
2583
2584 success = g_ptr_array_and(ops, extract_boolean_generic_op);
2585
2586 g_ptr_array_free_full(ops);
2587
2588 if (!success) {
2589 /* TODO: be more specific here */
2590 device_set_error(dself,
2591 g_strdup(_("One or more devices failed to recycle_file")),
2592 DEVICE_STATUS_DEVICE_ERROR);
2593 return FALSE;
2594 }
2595 return TRUE;
2596 }
2597
2598 /* GFunc */
2599 static void finish_do_op(gpointer data, gpointer user_data G_GNUC_UNUSED) {
2600 GenericOp * op = data;
2601 op->result = GINT_TO_POINTER(device_finish(op->child));
2602 }
2603
2604 static gboolean
2605 rait_device_finish (Device * self) {
2606 GPtrArray * ops;
2607 gboolean success;
2608 gboolean rval = TRUE;
2609
2610 rval = !rait_device_in_error(self);
2611
2612 ops = make_generic_boolean_op_array(RAIT_DEVICE(self));
2613
2614 do_rait_child_ops(RAIT_DEVICE(self), finish_do_op, ops);
2615
2616 success = g_ptr_array_and(ops, extract_boolean_generic_op);
2617 if (!success)
2618 rval = FALSE;
2619
2620 g_ptr_array_free_full(ops);
2621
2622 self->access_mode = ACCESS_NULL;
2623
2624 return rval;
2625 }
2626
2627 static Device *
2628 rait_device_factory (char * device_name, char * device_type, char * device_node) {
2629 Device * rval;
2630 g_assert(g_str_equal(device_type, "rait"));
2631 rval = DEVICE(g_object_new(TYPE_RAIT_DEVICE, NULL));
2632 device_open_device(rval, device_name, device_type, device_node);
2633 return rval;
2634 }
2635
2636 void
2637 rait_device_register (void) {
2638 static const char * device_prefix_list[] = {"rait", NULL};
2639 register_device(rait_device_factory, device_prefix_list);
2640 }