File: object.c

package info (click to toggle)
evms 2.5.2-1.sarge2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 14,248 kB
  • ctags: 15,488
  • sloc: ansic: 201,340; perl: 12,421; sh: 4,262; makefile: 1,516; yacc: 316; sed: 16
file content (1135 lines) | stat: -rw-r--r-- 27,157 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
/*
 *
 *   (C) Copyright IBM Corp. 2001, 2003
 *
 *   This program is free software;  you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 *   the GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program;  if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Module: object.c
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

#include "fullengine.h"
#include "object.h"
#include "lists.h"
#include "engine.h"
#include "handlemgr.h"
#include "discover.h"
#include "common.h"
#include "volume.h"
#include "internalAPI.h"
#include "message.h"
#include "memman.h"
#include "common.h"
#include "remote.h"


int isa_valid_input_object(storage_object_t    * obj,
			   storage_container_t * disk_group) {

	LOG_PROC_ENTRY();

	if ((obj->object_type != DISK) &&
	    (obj->object_type != SEGMENT) &&
	    (obj->object_type != REGION) &&
	    (obj->object_type != EVMS_OBJECT)) {
		LOG_ERROR("This is not a storage object.\n");
		LOG_PROC_EXIT_INT(EINVAL);
		return EINVAL;
	}

	/* The object must not be corrupt. */
	if (obj->flags & SOFLAG_CORRUPT) {
		LOG_ERROR("Object %s is not a valid input object.  It is corrupt.\n", obj->name);
		LOG_PROC_EXIT_INT(EINVAL);
		return EINVAL;
	}

	/* The object must be a top level object. */
	if (!is_top_object(obj)) {
		LOG_ERROR("Object %s is not a valid input object.  It is not a top level object.\n", obj->name);
		return EINVAL;
	}

	/* The object must not insist on being the top object. */
	if (obj->flags & SOFLAG_MUST_BE_TOP) {
		LOG_ERROR("Object %s is not a valid input object.  It insists it must be a top level object.\n", obj->name);
		return EINVAL;
	}

	/* The object must be in the specified disk group. */
	if (obj->disk_group != disk_group) {
		LOG_ERROR("Object %s in disk group %s is not in disk group %s.\n", obj->name, (obj->disk_group != NULL) ? obj->disk_group->name : "(local)", (disk_group != NULL) ? disk_group->name : "(local)");
		return EINVAL;
	}

	LOG_DEBUG("Object %s is a valid input object.\n", obj->name);
	LOG_PROC_EXIT_INT(0);
	return 0;
}


static int validate_create_parameters(plugin_handle_t     plugin_handle,
				      handle_array_t    * objects,
				      plugin_record_t * * ppPlugRec,
				      list_anchor_t     * pObjectList) {
	int rc = 0;
	void * object;
	object_type_t type;

	LOG_PROC_ENTRY();

	*ppPlugRec = NULL;
	*pObjectList = NULL;

	rc = translate_handle(plugin_handle,
			      &object,
			      &type);

	if (rc == HANDLE_MANAGER_NO_ERROR) {
		if (type == PLUGIN) {
			plugin_record_t * pPlugRec = (plugin_record_t *) object;
			plugin_type_t plugin_type = GetPluginType(pPlugRec->id);

			*ppPlugRec = pPlugRec;

			if ((plugin_type == EVMS_DEVICE_MANAGER) ||
			    (plugin_type == EVMS_SEGMENT_MANAGER) ||
			    (plugin_type == EVMS_REGION_MANAGER) ||
			    (plugin_type == EVMS_FEATURE) ||
			    (plugin_type == EVMS_ASSOCIATIVE_FEATURE)) {
				list_anchor_t object_list = allocate_list();

				if (object_list != NULL) {

					rc = make_list(objects, object_list);

					if (rc == 0) {
						list_element_t iter;
						storage_object_t * obj;
						storage_container_t * disk_group = NULL;

						obj = first_thing(object_list, NULL);
						if (obj != NULL) {
							disk_group = obj->disk_group;
						}

						/*
						 * Make sure that the input object list contains
						 * top level objects and none of them insist on
						 * being a top object.
						 */
						LIST_FOR_EACH(object_list, iter, obj) {
							rc = isa_valid_input_object(obj, disk_group);
							if (rc != 0) {
								break;
							}
						}
					}

					if (rc != 0) {
						destroy_list(object_list);
					}

					*pObjectList = object_list;

				} else {
					LOG_CRITICAL("Error allocating memory for an output object list.\n");
					rc = ENOMEM;
				}

			} else {
				LOG_ERROR("The plug-in %s is not a type that manages storage objects.\n", pPlugRec->short_name);
				rc = EINVAL;
			}

		} else {
			LOG_ERROR("The plugin_handle is not for a plug-in.\n");
			rc = EINVAL;
		}
	}

	LOG_PROC_EXIT_INT(rc);
	return rc;
}


int evms_create(plugin_handle_t    plugin_handle,
		handle_array_t   * objects,
		option_array_t   * options,
		handle_array_t * * output_objects) {

	int rc = 0;
	plugin_record_t * pPlugRec = NULL;
	list_anchor_t object_list = NULL;
	list_anchor_t input_objects;

	LOG_PROC_ENTRY();

	rc = check_engine_write_access();
        if (rc != 0) {
		LOG_PROC_EXIT_INT(rc);
		return rc;
	}

	if (!local_focus) {
		rc = remote_create(plugin_handle, objects, options, output_objects);
		LOG_PROC_EXIT_INT(rc);
		return rc;
	}

	rc = validate_create_parameters(plugin_handle, objects,
					&pPlugRec,     &object_list);
	if (rc == 0) {
		/*
		 * Make a copy of the input objects list in case we have to
		 * clean up after an error.
		 */
		input_objects = copy_list(object_list);

		if (input_objects != NULL) {

			/*
			 * Make a list to receive the new objects from the
			 * create.
			 */
			STATIC_LIST_DECL(new_objects);

			/*
			 * We are finally setup to do the create.
			 */
			rc = pPlugRec->functions.plugin->create(object_list, options, &new_objects);

			if (rc == 0) {

				list_element_t iter;
				storage_object_t * new_obj;
				storage_object_t * child_obj;

				remove_corrupt_objects(&new_objects);

				LIST_FOR_EACH(&new_objects, iter, new_obj) {
					list_element_t tmp_iter;

					/*
					 * Now that the input objects are
					 * consumed, the parent can write
					 * on them anywhere, including over
					 * any stop data.  Forget that the
					 * child objects have stop data.
					 */
					LIST_FOR_EACH(new_obj->child_objects, tmp_iter, child_obj) {
						child_obj->flags &= ~SOFLAG_HAS_STOP_DATA;
					}

					/*
					 * Mark the new objects' feature
					 * headers dirty.
					 */
					set_feature_header_dirty(new_obj);

					/*
					 * Set the object's SOFLAG_NEEDS_ACTIVATE
					 * flag if all of its children are
					 * active or are scheduled to be
					 * activated..
					 */
					if (new_obj->data_type == DATA_TYPE) {
						new_obj->flags |= SOFLAG_NEEDS_ACTIVATE;
						if (new_obj->producing_container != NULL) {
							LIST_FOR_EACH(new_obj->producing_container->objects_consumed, tmp_iter, child_obj) {
								if (!(child_obj->flags & (SOFLAG_ACTIVE | SOFLAG_NEEDS_ACTIVATE))) {
									new_obj->flags &= ~SOFLAG_NEEDS_ACTIVATE;
									break;
								}
							}
						} else {
							LIST_FOR_EACH(new_obj->child_objects, tmp_iter, child_obj) {
								if (!(child_obj->flags & (SOFLAG_ACTIVE | SOFLAG_NEEDS_ACTIVATE))) {
									new_obj->flags &= ~SOFLAG_NEEDS_ACTIVATE;
									break;
								}
							}
						}
						if (new_obj->flags & SOFLAG_NEEDS_ACTIVATE) {
							LIST_FOR_EACH(new_obj->associated_children, tmp_iter, child_obj) {
								if (!(child_obj->flags & (SOFLAG_ACTIVE | SOFLAG_NEEDS_ACTIVATE))) {
									new_obj->flags &= ~SOFLAG_NEEDS_ACTIVATE;
									break;
								}
							}
						}
					}
				}

				propigate_cluster_info(&new_objects);

				/*
				 * Make sure the compatibility volume names
				 * are in sync.
				 */
				sync_volumes();

				/* Sort the list that was affected. */
				switch (GetPluginType(pPlugRec->id)) {
					case EVMS_DEVICE_MANAGER:
						sort_list(&disks_list, compare_objects, NULL);
						break;
					case EVMS_SEGMENT_MANAGER:
						sort_list(&segments_list, compare_objects, NULL);
						break;
					case EVMS_REGION_MANAGER:
						sort_list(&regions_list, compare_objects, NULL);
						break;
					case EVMS_FEATURE:
					case EVMS_ASSOCIATIVE_FEATURE:
						sort_list(&EVMS_objects_list, compare_objects, NULL);
						break;
					default:
						break;
				}

				/*
				 * If the user wants handles for the
				 * new objects, make the handles.
				 */
				if (output_objects != NULL) {
					/*
					 * type doesn't really
					 * matter as long as it's
					 * DISK, SEGMENT, or EVMS_OBJECT.
					 * make_handle_array() will figure out the
					 * correct type for each object in the list.
					 */
					rc = make_user_handle_array(&new_objects, output_objects);
				}

			} else {
				LOG_CRITICAL("Error returned from plugin's create routine.\n");
			}

			destroy_list(input_objects);

		} else {
			LOG_CRITICAL("Error getting memory for copying the input object list.\n");
			rc = ENOMEM;
		}

		destroy_list(object_list);
	}

	LOG_PROC_EXIT_INT(rc);
	return rc;
}


/*
 * evms_get_object_list returns a pointer to a handle_array_t with handles for
 * storage objects, optionally filtering on the object type, the data type of
 * the object, the plug-in that manages the object, the cluster node on which
 * the object resides, and some flags.  If any of the filtering parameters is 0,
 * that filter is not used.
 */
int evms_get_object_list(object_type_t         object_type,
			 data_type_t           data_type,
			 plugin_handle_t       plugin_handle,
			 object_handle_t       disk_group_handle,
			 object_search_flags_t flags,
			 handle_array_t    * * object_handle_list) {
	int rc = 0;
	void * object = NULL;
	object_type_t type;
	plugin_record_t * plugin = NULL;
	storage_container_t * disk_group = NULL;

	LOG_PROC_ENTRY();

	rc = check_engine_read_access();
	if (rc != 0) {
		LOG_PROC_EXIT_INT(rc);
		return rc;
	}

	if (!local_focus) {
		rc = remote_get_object_list(object_type,
					    data_type,
					    plugin_handle,
					    disk_group_handle,
					    flags,
					    object_handle_list);

		LOG_PROC_EXIT_INT(rc);
		return rc;
	}

	if (plugin_handle != 0) {
		/* Translate the handle for the feature to make sure it is valid */
		/* and to get the plugin_record_t for the feature. */
		rc = translate_handle(plugin_handle,
				      &object,
				      &type);

		if (rc == HANDLE_MANAGER_NO_ERROR) {
			if (type == PLUGIN) {
				plugin = (plugin_record_t *) object;
			} else {
				rc = EINVAL;
			}
		}
	}

	if (rc == 0) {
		if (disk_group_handle != 0) {
			/*
			 * Translate the handle for the disk group
			 * to make sure it is valid and to get the
			 * disk group storage_container_t.
			 */
			rc = translate_handle(disk_group_handle,
					      &object,
					      &type);

			if (rc == HANDLE_MANAGER_NO_ERROR) {
				if (type == CONTAINER) {
					disk_group = (storage_container_t *) object;
				} else {
					rc = EINVAL;
				}
			}
		}
	}

	if (rc == 0) {
		list_anchor_t object_list;

		/*
		 * Call the internal version of engine_get_object_list.  "plugin"
		 * will be NULL if the caller did not specify a plug-in, else it
		 * will be a pointer to the plug-in's plugin_record_t.
		 */
		rc = engine_get_object_list(object_type, data_type, plugin, disk_group, flags, &object_list);

		if (rc == 0) {
			/*
			 * type doesn't really matter as long as it's
			 * DISK, SEGMENT, or EVMS_OBJECT.
			 * make_handle_array() will figure out the
			 * correct type for each object in the list.
			 */
			rc = make_user_handle_array(object_list, object_handle_list);

			/* We are finished with the list that was returned by */
			/* engine_get_object_list. */
			destroy_list(object_list);
		}
	}

	LOG_PROC_EXIT_INT(rc);
	return rc;
}


/*
 * evms_assign
 */
int evms_assign(object_handle_t  object_handle,
		plugin_handle_t  plugin_handle,
		option_array_t * options) {

	int rc = 0;
	void * object = NULL;
	object_type_t type;
	storage_object_t * obj = NULL;
	plugin_record_t * plugin = NULL;

	LOG_PROC_ENTRY();

	rc = check_engine_write_access();
        if (rc != 0) {
		LOG_PROC_EXIT_INT(rc);
		return(rc);
	}

	if (!local_focus) {
		rc = remote_assign(object_handle, plugin_handle, options);
		LOG_PROC_EXIT_INT(rc);
		return(rc);
	}

	if (object_handle != 0) {
		/*
		 * Translate the handle for the object to make sure it is valid
		 * and to get the storage_object_t for the object.
		 */
		rc = translate_handle(object_handle,
				      &object,
				      &type);

		if (rc == HANDLE_MANAGER_NO_ERROR) {
			if ((type == DISK) ||
			    (type == SEGMENT) ||
			    (type == REGION) ||
			    (type == EVMS_OBJECT)) {
				storage_object_t * parent;

				obj = (storage_object_t *) object;
				parent = first_thing(obj->parent_objects, NULL);

				if (parent != NULL) {
					LOG_ERROR("Object %s already has parent objects produced by plug-in %s.\n", obj->name, parent->plugin->short_name);
				}

			} else {
				LOG_ERROR("Handle %d is not a handle for a storage object.\n", object_handle);
				rc = EINVAL;
			}
		}

	} else {
		LOG_ERROR("An object handle must be given.\n");
		rc = EINVAL;
	}

	if (plugin_handle != 0) {
		/*
		 * Translate the handle for the plug-in to make sure it is valid
		 * and to get the plugin_record_t for the plug-in.
		 */
		rc = translate_handle(plugin_handle,
				      &object,
				      &type);

		if (rc == HANDLE_MANAGER_NO_ERROR) {
			if (type == PLUGIN) {
				plugin = (plugin_record_t *) object;
			} else {
				LOG_ERROR("Handle %d is not a handle for a plug-in.\n", plugin_handle);
				rc = EINVAL;
			}
		}

	} else {
		LOG_ERROR("A plug-in handle must be given.\n");
		rc = EINVAL;
	}

	if (rc == 0) {
		rc = plugin->functions.plugin->assign(obj, options);

		if (rc == 0) {
			/*
			 * Now that the object is consumed,
			 * any stop data on it can be
			 * overwritten.  Forget that it is
			 * there.
			 */
			obj->flags &= ~SOFLAG_HAS_STOP_DATA;

			propigate_cluster_info(obj->parent_objects);

			/*
			 * If the target object of the assign is active or
			 * scheduled to be activated, mark its new parent
			 * objects for activation.
			 */
			if (obj->flags & (SOFLAG_ACTIVE | SOFLAG_NEEDS_ACTIVATE)) {
				list_element_t iter;
				storage_object_t * parent;

				LIST_FOR_EACH(obj->parent_objects, iter, parent) {
					if (parent->data_type == DATA_TYPE) {
						parent->flags |= SOFLAG_NEEDS_ACTIVATE;
					}
				}
			}
		}
	}

	LOG_PROC_EXIT_INT(rc);
	return(rc);
}


static int get_plugin(storage_object_t   * obj,
		      plugin_record_t  * * pp_plugin_record) {

	int rc = 0;

	LOG_PROC_ENTRY();

	if (*pp_plugin_record == NULL) {
		*pp_plugin_record = obj->plugin;

	} else {
		if (*pp_plugin_record != obj->plugin) {
			LOG_ERROR("Object %s does not have parent objects that are all managed by the same plug-in.\n", obj->name);
			rc = EINVAL;
		}
	}

	LOG_PROC_EXIT_INT(rc);
	return rc;
}


static int get_parent_plugin(storage_object_t * obj, plugin_record_t * * p_parent_plugin) {

	int rc = 0;

	LOG_PROC_ENTRY();

	if (!list_empty(obj->parent_objects)) {
		list_element_t iter;
		storage_object_t * parent;

		LIST_FOR_EACH(obj->parent_objects, iter, parent) {
			rc = get_plugin(parent, p_parent_plugin);
			if (rc !=0) {
				break;
			}
		}

	} else {
		LOG_ERROR("Object %s does not have any parent objects.\n", obj->name);
		rc = EINVAL;
	}

	LOG_PROC_EXIT_INT(rc);
	return rc;
}


static int is_in_use(storage_object_t * obj) {

	int rc = 0;

	LOG_PROC_ENTRY();

	if (!list_empty(obj->parent_objects)) {
		LOG_ERROR("Object %s has parent objects.\n", obj->name);
		rc = EINVAL;
	}

	if (obj->consuming_container != NULL) {
		LOG_ERROR("Object %s is part of a container.\n", obj->name);
		rc = EINVAL;
	}

	if (obj->volume != NULL) {
		LOG_ERROR("Object %s is used as volume %s.\n", obj->name, obj->volume->name);
		rc = EINVAL;
	}

	LOG_PROC_EXIT_INT(rc);
	return rc;
}


/*
 * evms_can_unassign determines if a plug-in can be unassigned from consuming
 * an object.  The parent objects must themselves not have parent objects,
 * must not be consumed by a container, and if a volume is created from them,
 * the volume must not be mounted.  If that is true, then ask the plug-in
 * if it can unassign itself from the object.
 */
int evms_can_unassign(object_handle_t object_handle) {

	int rc = 0;
	void * object = NULL;
	object_type_t type;
	storage_object_t * obj = NULL;

	LOG_PROC_ENTRY();

	rc = check_engine_write_access();
	if (rc != 0) {
		LOG_PROC_EXIT_INT(rc);
		return(rc);
	}

	if (!local_focus) {
		rc = remote_can_unassign(object_handle);
		LOG_PROC_EXIT_INT(rc);
		return(rc);
	}

	if (object_handle != 0) {
		/*
		 * Translate the handle for the object to make sure it is valid
		 * and to get the storage_object_t for the object.
		 */
		rc = translate_handle(object_handle,
				      &object,
				      &type);

		if (rc == HANDLE_MANAGER_NO_ERROR) {
			if ((type == DISK) ||
			    (type == SEGMENT) ||
			    (type == REGION) ||
			    (type == EVMS_OBJECT)) {
				obj = (storage_object_t *) object;
			} else {
				LOG_DETAILS("Handle %d is not a handle for a storage object.\n", object_handle);
				rc = EINVAL;
			}
		}

	} else {
		LOG_ERROR("An object handle must be given.\n");
		rc = EINVAL;
	}

	if (rc == 0) {
		list_element_t iter;
		storage_object_t * parent;

		/*
		 * Check to make sure the parent objects don't have parents
		 * themselves, that they are not part of a container, and
		 * that they are not part of a volume.
		 */
		LIST_FOR_EACH(obj->parent_objects, iter, parent) {
			rc = is_in_use(parent);
			if (rc != 0) {
				break;
			}
		}

		if (rc == 0) {
			plugin_record_t * parent_plugin = NULL;

			/*
			 * Get the plug-in that is managing the parents of this
			 * object.
			 */
			rc = get_parent_plugin(obj, &parent_plugin);

			if (rc == 0) {
				rc = parent_plugin->functions.plugin->can_unassign(obj);
			}
		}
	}

	LOG_PROC_EXIT_INT(rc);
	return(rc);
}


/*
 * evms_unassign unassigns a plug-in from consuming an object.  This is most
 * useful for segment managers that claim a disk or segment.
 */
int evms_unassign(object_handle_t object_handle) {

	int rc = 0;
	void * object = NULL;
	object_type_t type;
	storage_object_t * obj = NULL;

	LOG_PROC_ENTRY();

	rc = check_engine_write_access();
        if (rc != 0) {
		LOG_PROC_EXIT_INT(rc);
		return(rc);
	}

	if (!local_focus) {
		rc = remote_unassign(object_handle);
		LOG_PROC_EXIT_INT(rc);
		return(rc);
	}

	if (object_handle != 0) {
		/*
		 * Translate the handle for the object to make sure it is valid
		 * and to get the storage_object_t for the object.
		 */
		rc = translate_handle(object_handle,
				      &object,
				      &type);

		if (rc == HANDLE_MANAGER_NO_ERROR) {
			if ((type == DISK) ||
			    (type == SEGMENT) ||
			    (type == REGION) ||
			    (type == EVMS_OBJECT)) {
				obj = (storage_object_t *) object;
			} else {
				LOG_ERROR("Handle %d is not a handle for a storage object.\n", object_handle);
				rc = EINVAL;
			}
		}

	} else {
		LOG_ERROR("An object handle must be given.\n");
		rc = EINVAL;
	}

	if (rc == 0) {
		list_element_t iter;
		storage_object_t * parent;

		/*
		 * Check to make sure the parent objects don't have parents
		 * themselves, that they are not part of a container, and
		 * that they are not part of a volume.
		 */
		LIST_FOR_EACH(obj->parent_objects, iter, parent) {
			rc = is_in_use(parent);
			if (rc != 0) {
				break;
			}
		}

		if (rc == 0) {
			plugin_record_t * parent_plugin = NULL;

			/*
			 * Get the plug-in that is managing the parents of this
			 * object.
			 */
			rc = get_parent_plugin(obj, &parent_plugin);

			if (rc == 0) {
				/*
				 * If this object has parent objects that are
				 * data objects, warn the user that the data
				 * objects will be destroyed.
				 */
				LIST_FOR_EACH(obj->parent_objects, iter, parent) {
					if (parent->data_type == DATA_TYPE) {
						rc = EEXIST;
						break;
					}
				}

				if (rc != 0) {
					char * choices[] = {"Continue", "Cancel", NULL};
					int answer = 0;	    /* Default is "Continue" */

					rc = engine_user_message(&answer, choices,
								 _("WARNING:  Plug-in %s is producing data objects from object %s.  "
								   "Unassigning plug-in %s from object %s will destroy the data objects.\n"),
								 parent_plugin->short_name, obj->name,
								 parent_plugin->short_name, obj->name);

					if (answer == 1) {
						/* User wants to cancel. */
						rc = E_CANCELED;
					}
				}

				if (rc == 0) {
					rc = parent_plugin->functions.plugin->unassign(obj);

					if (rc == 0) {
						/*
						 * The previous owner may
						 * have trashed stop data while the
						 * object was consumed.
						 */
						obj->flags &= ~SOFLAG_HAS_STOP_DATA;
					}
				}
			}
		}
	}

	LOG_PROC_EXIT_INT(rc);
	return(rc);
}


void free_function_info_array_contents(void * object) {

	int i;
	function_info_array_t * function_array = (function_info_array_t *) object;

	for (i = 0; i < function_array->count; i++) {
		if (function_array->info[i].title != NULL) {
			engine_free(function_array->info[i].title);
		}
		if (function_array->info[i].verb != NULL) {
			engine_free(function_array->info[i].verb);
		}
		if (function_array->info[i].help != NULL) {
			engine_free(function_array->info[i].help);
		}
	}
}

/*
 * Get an array of private actions that are supported by the plug-in that is
 * managing "thing".
 */
int evms_get_plugin_functions(engine_handle_t           thing_handle,
			      function_info_array_t * * actions) {

	int rc = 0;
	void * object = NULL;
	object_type_t type;
	function_info_array_t * function_array = NULL;

	LOG_PROC_ENTRY();

	rc = check_engine_read_access();
        if (rc != 0) {
		LOG_PROC_EXIT_INT(rc);
		return rc;
	}

	if (!local_focus) {
		rc = remote_get_plugin_functions(thing_handle, actions);
		LOG_PROC_EXIT_INT(rc);
		return rc;
	}

	if (thing_handle == 0) {
		LOG_ERROR("A handle must be given.\n");
		rc = EINVAL;
	}

	rc = translate_handle(thing_handle,
			      &object,
			      &type);

	if (rc != HANDLE_MANAGER_NO_ERROR) {
		LOG_PROC_EXIT_INT(rc);
		return rc;
	}
	switch (type) {
		case PLUGIN:
			{
				plugin_record_t * plugin = (plugin_record_t *) object;

				switch (GetPluginType(plugin->id)) {
					case EVMS_DEVICE_MANAGER:
					case EVMS_SEGMENT_MANAGER:
					case EVMS_REGION_MANAGER:
					case EVMS_FEATURE:
					case EVMS_ASSOCIATIVE_FEATURE:
						rc = plugin->functions.plugin->get_plugin_functions(NULL, &function_array);
						break;

					case EVMS_FILESYSTEM_INTERFACE_MODULE:
						rc = plugin->functions.fsim->get_plugin_functions(NULL, &function_array);
						break;

					case EVMS_CLUSTER_MANAGER_INTERFACE_MODULE:
						rc = plugin->functions.cluster->get_plugin_functions(NULL, &function_array);
						break;

					default:
						LOG_ERROR("Plug-in %s has an unknown type of %#x.  Can't get plug-in functions for that type of plug-in.\n", plugin->short_name, GetPluginType(plugin->id));
						rc = ENOSYS;
						break;
				}
			}
			break;

		case EVMS_OBJECT:
		case REGION:
		case SEGMENT:
		case DISK:
			{
				storage_object_t * obj = (storage_object_t *) object;

				rc = obj->plugin->functions.plugin->get_plugin_functions(obj, &function_array);
			}
			break;

		case CONTAINER:
			{
				storage_container_t * con = (storage_container_t *) object;

				rc = con->plugin->container_functions->get_plugin_functions(con, &function_array);
			}
			break;

		case VOLUME:
			{
				logical_volume_t * vol = (logical_volume_t *) object;

				if (vol->file_system_manager != NULL) {
					rc = vol->file_system_manager->functions.fsim->get_plugin_functions(vol, &function_array);

				} else {
					rc = ENOSYS;
				}
			}
			break;

		default:
			LOG_ERROR("Handle %d is of type %d which is not valid for this function.\n", thing_handle, type);
			rc = EINVAL;
	}

	if (rc == 0) {
		/*
		 * Allocate a user memory block and copy the action info
		 * to the user memory.
		 */
		uint size = sizeof(function_info_array_t) + function_array->count * sizeof(function_info_t);

		*actions = alloc_app_struct(size, free_function_info_array_contents);

		if (*actions != NULL) {
			/* We do not copy the strings, just the pointers to the strings. */
			memcpy(*actions, function_array, size);
		} else {
			rc = ENOMEM;
		}

		/*
		 * Free the action array returned by the plug-in but don't
		 * free the strings.  The user copy now has the pointers to the strings.
		 * the strings will be freed when the user frees the user copy of
		 * the function_info_array_t.
		 */
		engine_free(function_array);
	}

	LOG_PROC_EXIT_INT(rc);
	return rc;
}


/*
 * Execute a plug-in private action on a "thing".
 */
int evms_do_plugin_function(engine_handle_t  thing_handle,
			    task_action_t    action,
			    handle_array_t * objects,
			    option_array_t * options) {

	int rc = 0;
	void * object = NULL;
	object_type_t type;
	list_anchor_t object_list;

	LOG_PROC_ENTRY();

	rc = check_engine_write_access();
        if (rc != 0) {
		LOG_PROC_EXIT_INT(rc);
		return rc;
	}

	if (!local_focus) {
		rc = remote_do_plugin_function(thing_handle, action, object, options);
		LOG_PROC_EXIT_INT(rc);
		return rc;
	}

	if (thing_handle == 0) {
		LOG_ERROR("A handle must be given.\n");
		LOG_PROC_EXIT_INT(EINVAL);
		return EINVAL;
	}

	rc = translate_handle(thing_handle,
			      &object,
			      &type);

	if (rc != HANDLE_MANAGER_NO_ERROR) {
		LOG_PROC_EXIT_INT(rc);
		return rc;
	}
	object_list = allocate_list();

	if (object_list != NULL) {

		rc = make_list(objects, object_list);

		if (rc == 0) {

			switch (type) {
				case PLUGIN:
					{
						plugin_record_t * plugin = (plugin_record_t *) object;

						if (GetPluginType(plugin->id) == EVMS_FILESYSTEM_INTERFACE_MODULE) {
							rc = plugin->functions.fsim->plugin_function(NULL, action, object_list, options);
						} else {
							rc = plugin->functions.plugin->plugin_function(NULL, action, object_list, options);
						}
					}
					break;

				case EVMS_OBJECT:
				case REGION:
				case SEGMENT:
				case DISK:
					{
						storage_object_t * obj = (storage_object_t *) object;

						rc = obj->plugin->functions.plugin->plugin_function(obj, action, object_list, options);
					}
					break;

				case CONTAINER:
					{
						storage_container_t * con = (storage_container_t *) object;

						rc = con->plugin->container_functions->plugin_function(con, action, object_list, options);
					}
					break;

				case VOLUME:
					{
						logical_volume_t * vol = (logical_volume_t *) object;

						if (vol->file_system_manager != NULL) {
							rc = vol->file_system_manager->functions.fsim->plugin_function(vol, action, object_list, options);

						} else {
							rc = ENOSYS;
						}
					}
					break;

				default:
					LOG_ERROR("Handle %d is of type %d which is not valid for this function.\n", thing_handle, type);
					rc = EINVAL;
			}
		}

		destroy_list(object_list);

	} else {
		LOG_CRITICAL("Error allocating memory for an output object list.\n");
		rc = ENOMEM;
	}

	if (rc == 0) {
		/*
		 * A plug-in function can add or remove child objects to/from
		 * the given object.  Since we don't know what went on inside
		 * the set_info() and how it affected other objects, update the
		 * stop data state on all the objects that can have stop data.
		 */
		update_all_stop_data_states();
	}

	LOG_PROC_EXIT_INT(rc);
	return rc;
}