File: dm.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 (1340 lines) | stat: -rw-r--r-- 29,505 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
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
/*
 *   (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: dm.c
 *
 * Implementation of the Device-Mapper common-services. The code in this file
 * is independent of the DM interface. Code that is dependent on the interface
 * version is in dm-ioctl*.[ch].
 *
 * Each common-service API is routed into this file first, where common handling
 * is performed. Then the current interface version is checked, and the
 * appropriate version-specific routine is called to complete the work for the
 * service.
 */

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

#include "fullengine.h"
#include "engine.h"
#include "memman.h"
#include "dm.h"
#include "dm-ioctl.h"

/* The major number of the Device-Mapper ioctl interface. */
static int dm_version_major = 0;

/* Flag indicating whether a Device-Mapper device is suspended. */
int dm_device_suspended = FALSE;

/**
 * dm_allocate_target
 *
 * @type:	Type of target to allocate. DM_TARGET_*
 * @start:	Logical start of this target within the DM device.
 * @length:	Length of this target.
 * @num_devs:	Total number of devices when allocating a STRIPE, MIRROR,
 *		or MULTIPATH. Otherwise set to 0.
 * @num_groups:	Number of groups when allocating a MULTIPATH. Otherwise
 *		set to 0.
 *
 * Allocate a DM target and initialize the common fields.
 **/
dm_target_t *dm_allocate_target(dm_target_type type,
				u_int64_t start,
				u_int64_t length,
				u_int32_t num_devs,
				u_int32_t num_groups)
{
	dm_target_t *target = NULL;
	unsigned long size;
	int rc;

	LOG_PROC_ENTRY();

	if (type > DM_TARGET_MAX) {
		/* Bad parameters. */
		goto out;
	}

	LOG_DEBUG("Request to allocate a %s target.\n",
		  dm_target_type_info[type].name);

	target = engine_alloc(sizeof(dm_target_t));
	if (!target) {
		goto out;
	}

	size = dm_target_type_info[type].struct_size;
	if (size) {
		target->data.linear = engine_alloc(size);
		if (!target->data.linear) {
			engine_free(target);
			target = NULL;
			goto out;
		}

		rc = dm_target_type_info[type].allocate_target(target,
							       num_devs,
							       num_groups);
		if (rc) {
			engine_free(target->data.linear);
			engine_free(target);
			target = NULL;
			goto out;
		}
	}

	target->start = start;
	target->length = length;
	target->type = type;

out:
	if (!target) {
		LOG_ERROR("Error allocating memory for target.\n");
		LOG_ERROR("   Type: %d, Start %"PRIu64", Length %"PRIu64"\n",
			  type, start, length);
	}
	LOG_PROC_EXIT_PTR(target);
	return target;
}

/**
 * dm_add_target
 *
 * @target:		New target to insert in the list.
 * @target_list:	Pointer to the head of the target list.
 *
 * Add a DM target to a list of targets in preparation for an activate or
 * reactivate. The target is inserted in order based on the "start" field.
 **/
void dm_add_target(dm_target_t *target, dm_target_t **target_list)
{
	dm_target_t **t;

	LOG_PROC_ENTRY();

	for (t = target_list; *t; t = &(*t)->next) {
		if ((*t)->start > target->start) {
			break;
		}
	}

	target->next = (*t) ? (*t)->next : NULL;
	*t = target;

	LOG_PROC_EXIT_VOID();
}

/**
 * dm_deallocate_targets
 *
 * Free the memory for a list of DM targets. This assumes the targets were
 * allocated with dm_allocate_target() and linked together with dm_add_target().
 **/
void dm_deallocate_targets(dm_target_t *target_list)
{
	dm_target_t *target;

	LOG_PROC_ENTRY();

	for (target = target_list; target; target = target_list) {
		target_list = target->next;
		if (target->data.linear) {
			dm_target_type_info[target->type].deallocate_target(target);
			engine_free(target->data.linear);
		}
		engine_free(target);
	}

	LOG_PROC_EXIT_VOID();
}

/**
 * dm_deallocate_device_list
 *
 * Free the memory for a list of dm_device_list's.
 **/
void dm_deallocate_device_list(dm_device_list_t *device_list)
{
	dm_device_list_t *device;

	LOG_PROC_ENTRY();

	for (device = device_list; device; device = device_list) {
		device_list = device->next;
		engine_free(device);
	}

	LOG_PROC_EXIT_VOID();
}

/**
 * dm_deallocate_module_list
 *
 * Free the memory for a list of dm_module_list's.
 **/
void dm_deallocate_module_list(dm_module_list_t *module_list)
{
	dm_module_list_t *module;

	LOG_PROC_ENTRY();

	for (module = module_list; module; module = module_list) {
		module_list = module->next;
		engine_free(module);
	}

	LOG_PROC_EXIT_VOID();
}

/**
 * dm_suspend
 *
 * @object:	EVMS storage object to suspend/resume.
 * @suspend:	TRUE for suspend. FALSE for resume.
 *
 * Suspend or resume the Device-Mapper device representing an active EVMS
 * object.
 **/
int dm_suspend(storage_object_t *object, int suspend)
{
	int rc;

	LOG_PROC_ENTRY();

	if (!object) {
		/* Bad parameter. */
		rc = EINVAL;
		goto out;
	}

	LOG_DEBUG("Request to %s object %s\n",
		  suspend ? "suspend" : "resume", object->name);

	if (!(object->flags & SOFLAG_ACTIVE)) {
		/* Can't suspend inactive objects. */
		rc = EINVAL;
		goto out;
	}

	if (suspend && (object->flags & SOFLAG_SUSPENDED)) {
		/* Can't suspend an object that's already suspended. On the
		 * other hand, we *can* resume an object that isn't suspended.
		 */
		rc = 0;
		goto out;
	}

	switch (dm_version_major) {
	case 3:
		rc = dm_suspend_v3(object->name, suspend);
		break;
	case 4:
		rc = dm_suspend_v4(object->name, suspend);
		break;
	default:
		rc = EINVAL;
	}
	if (rc) {
		goto out;
	}

	/* Update the object's state. */
	if (suspend) {
		object->flags |= SOFLAG_SUSPENDED;
	} else {
		object->flags &= ~SOFLAG_SUSPENDED;
	}

out:
	LOG_PROC_EXIT_INT(rc);
	return rc;
}

/**
 * dm_suspend_volume
 *
 * @volume:	EVMS logical volume to suspend/resume.
 * @suspend:	TRUE for suspend. FALSE for resume.
 *
 * Suspend or resume the Device-Mapper device representing an active EVMS
 * volume.
 **/
int dm_suspend_volume(logical_volume_t *volume, int suspend)
{
	char *base_name;
	int rc;

	LOG_PROC_ENTRY();

	if (!volume) {
		/* Bad parameter. */
		rc = EINVAL;
		goto out;
	}

	LOG_DEBUG("Request to %s volume %s\n",
		  suspend ? "suspend" : "resume", volume->name);

	if (memcmp(volume->name, EVMS_DEV_NODE_PATH, EVMS_DEV_NODE_PATH_LEN)) {
		LOG_ERROR("Volume %s does not have the \"%s\" prefix.\n",
			  volume->name, EVMS_DEV_NODE_PATH);
		rc = EINVAL;
		goto out;
	}
	base_name = volume->name + EVMS_DEV_NODE_PATH_LEN;

	if (!(volume->flags & VOLFLAG_ACTIVE)) {
		/* Can't suspend inactive volumes. */
		rc = EINVAL;
		goto out;
	}

	if (suspend && (volume->flags & VOLFLAG_SUSPENDED)) {
		/* Can't suspend a volume that's already suspended. On the other
		 * hand, we *can* resume a volume that isn't suspended.
		 */
		rc = 0;
		goto out;
	}

	switch (dm_version_major) {
	case 3:
		rc = dm_suspend_v3(base_name, suspend);
		break;
	case 4:
		rc = dm_suspend_v4(base_name, suspend);
		break;
	default:
		rc = EINVAL;
	}
	if (rc) {
		goto out;
	}

	/* Update the volume's state. */
	if (suspend) {
		volume->flags |= VOLFLAG_SUSPENDED;
	} else {
		volume->flags &= ~VOLFLAG_SUSPENDED;
	}

out:
	LOG_PROC_EXIT_INT(rc);
	return rc;
}

/**
 * dm_activate
 *
 * @object:	EVMS storage object to activate.
 * @target_list:List of targets that will make up the mapped device.
 *
 * Activate (or re-activate) an EVMS object as a Device-Mapper device. After
 * activating, update the object's device major:minor and mark object as active.
 **/
int dm_activate(storage_object_t *object, dm_target_t *target_list)
{
	int reactivate, read_only, rc;

	LOG_PROC_ENTRY();

	if (!object || !target_list) {
		/* Bad parameters. */
		rc = EINVAL;
		goto out;
	}

	reactivate = object->flags & SOFLAG_ACTIVE;
	read_only = object->flags & SOFLAG_READ_ONLY;

	LOG_DEBUG("Request to %sactivate object %s\n",
		  reactivate ? "re" : "", object->name);

	/* Create the ASCII parameter strings for all targets. */
	rc = build_target_type_params(target_list);
	if (rc) {
		goto out;
	}

	switch (dm_version_major) {
	case 3:
		rc = dm_activate_v3(object->name,
				    target_list, reactivate, read_only,
				    &object->dev_major, &object->dev_minor);
		break;
	case 4:
		rc = dm_activate_v4(object->name,
				    target_list, reactivate, read_only,
				    &object->dev_major, &object->dev_minor);
		break;
	default:
		rc = EINVAL;
	}
	if (rc) {
		goto out;
	}

	object->flags |= SOFLAG_ACTIVE;

out:
	/* Free the target parameter strings. */
	deallocate_target_type_params(target_list);
	LOG_PROC_EXIT_INT(rc);
	return rc;
}

/**
 * dm_activate_volume
 *
 * @volume:	EVMS volume to activate.
 * @target_list:List of targets that will make up the mapped device.
 *
 * Activate (or re-activate) an EVMS volume as a Device-Mapper device. After
 * activating, update the volume's device major:minor and mark volume as active.
 **/
int dm_activate_volume(logical_volume_t *volume, dm_target_t *target_list)
{
	char *base_name;
	int reactivate, read_only, rc;

	LOG_PROC_ENTRY();

	if (!volume || !target_list) {
		/* Bad parameters. */
		rc = EINVAL;
		goto out;
	}

	if (!(volume->flags & VOLFLAG_HAS_OWN_DEVICE)) {
		/* This volume doesn't need its own DM device. */
		rc = 0;
		goto out;
	}

	base_name = volume->name + EVMS_DEV_NODE_PATH_LEN;
	reactivate = volume->flags & VOLFLAG_ACTIVE;
	read_only = volume->flags & VOLFLAG_READ_ONLY;

	LOG_DEBUG("Request to %sactivate volume %s\n",
		  reactivate ? "re" : "", volume->name);

	/* Create the ASCII parameter strings for all targets. */
	rc = build_target_type_params(target_list);
	if (rc) {
		goto out;
	}

	if (memcmp(volume->name, EVMS_DEV_NODE_PATH,
		   EVMS_DEV_NODE_PATH_LEN) != 0) {
		LOG_ERROR("Volume %s does not have the \"%s\" prefix.\n",
			  volume->name, EVMS_DEV_NODE_PATH);
		rc = EINVAL;
		goto out;
	}

	switch (dm_version_major) {
	case 3:
		rc = dm_activate_v3(base_name,
				    target_list, reactivate, read_only,
				    &volume->dev_major, &volume->dev_minor);
		break;
	case 4:
		rc = dm_activate_v4(base_name,
				    target_list, reactivate, read_only,
				    &volume->dev_major, &volume->dev_minor);
		break;
	default:
		rc = EINVAL;
	}
	if (rc) {
		goto out;
	}

	volume->flags |= VOLFLAG_ACTIVE;

out:
	/* Free the target parameter strings. */
	deallocate_target_type_params(target_list);
	LOG_PROC_EXIT_INT(rc);
	return rc;
}

/**
 * dm_deactivate
 *
 * @object: EVMS storage object to deactivate.
 *
 * Deactivate the Device-Mapper device representing the EVMS storage object.
 * Update the object's information.
 **/
int dm_deactivate(storage_object_t *object)
{
	int rc;

	LOG_PROC_ENTRY();

	if (!object) {
		/* Bad parameters. */
		rc = EINVAL;
		goto out;
	}

	LOG_DEBUG("Request to deactivate object %s\n", object->name);

	if (!(object->flags & SOFLAG_ACTIVE)) {
		/* Object is not active. */
		rc = 0;
		goto out;
	}

	switch (dm_version_major) {
	case 3:
		rc = dm_deactivate_v3(object->name);
		break;
	case 4:
		rc = dm_deactivate_v4(object->name);
		break;
	default:
		rc = EINVAL;
	}
	if (rc) {
		goto out;
	}

	/* Update the object's information. */
	object->dev_major = 0;
	object->dev_minor = 0;
	object->flags &= ~SOFLAG_ACTIVE;

out:
	LOG_PROC_EXIT_INT(rc);
	return rc;
}

/**
 * dm_deactivate_volume
 *
 * @volume: EVMS logical volume to deactivate.
 *
 * Deactivate the Device-Mapper device representing the EVMS logical volume.
 * Update the volume's information.
 **/
int dm_deactivate_volume(logical_volume_t *volume)
{
	char *base_name;
	int rc;

	LOG_PROC_ENTRY();

	if (!volume) {
		/* Bad parameters. */
		rc = EINVAL;
		goto out;
	}

	LOG_DEBUG("Request to deactivate volume %s\n", volume->name);

	if (!(volume->flags & VOLFLAG_ACTIVE) ||
	    !(volume->flags & VOLFLAG_HAS_OWN_DEVICE)) {
		/* Volume is not active, or doesn't have it's own DM device. */
		rc = 0;
		goto out;
	}

	if (memcmp(volume->name, EVMS_DEV_NODE_PATH,
		   EVMS_DEV_NODE_PATH_LEN) != 0) {
		LOG_ERROR("Volume %s does not have the \"%s\" prefix.\n",
			  volume->name, EVMS_DEV_NODE_PATH);
		rc = EINVAL;
		goto out;
	}

	base_name = volume->name + EVMS_DEV_NODE_PATH_LEN;

	switch (dm_version_major) {
	case 3:
		rc = dm_deactivate_v3(base_name);
		break;
	case 4:
		rc = dm_deactivate_v4(base_name);
		break;
	default:
		rc = EINVAL;
	}
	if (rc) {
		goto out;
	}

	/* Update the volume's information. */
	volume->dev_major = 0;
	volume->dev_minor = 0;
	volume->flags &= ~VOLFLAG_ACTIVE;

out:
	LOG_PROC_EXIT_INT(rc);
	return rc;
}

/**
 * dm_rename
 *
 * @object:	The object whose name is changing
 * @old_name:	The object's old name.
 * @new_name:	The object's new name.
 *
 * Tell Device-Mapper to change the name for the specified active object. Both
 * the old name and the new name are parameters to this function, so as to not
 * force a particular ordering on when the object's actual name is changed.
 * This function does not change the name in the object, and the caller can
 * change that name before or after calling this API.
 **/
int dm_rename(storage_object_t * object,
	      char * old_name,
	      char * new_name)
{
	int rc;

	LOG_PROC_ENTRY();

	if (!object || !old_name || !new_name) {
		/* Bad parameters. */
		rc = EINVAL;
		goto out;
	}

	LOG_DEBUG("Request to rename object %s from %s to %s\n",
		  object->name, old_name, new_name);

	if (!(object->flags & SOFLAG_ACTIVE)) {
		/* Object is not active. */
		rc = EINVAL;
		goto out;
	}

	switch (dm_version_major) {
	case 3:
		rc = dm_rename_v3(old_name, new_name);
		break;
	case 4:
		rc = dm_rename_v4(old_name, new_name);
		break;
	default:
		rc = EINVAL;
	}

out:
	LOG_PROC_EXIT_INT(rc);
	return rc;
}

/**
 * dm_rename_volume
 *
 * @object:	The volume whose name is changing
 * @old_name:	The volume's old name.
 * @new_name:	The volume's new name.
 *
 * Tell Device-Mapper to change the name for the specified active volume. Both
 * the old name and the new name are parameters to this function, so as to not
 * force a particular ordering on when the volume's actual name is changed.
 * This function does not change the name in the volume, and the caller can
 * change that name before or after calling this API.
 **/
int dm_rename_volume(logical_volume_t * volume,
		     char * old_name,
		     char * new_name)
{
	char *old_base_name, *new_base_name;
	int rc;

	LOG_PROC_ENTRY();

	if (!volume || !old_name || !new_name) {
		/* Bad parameters. */
		rc = EINVAL;
		goto out;
	}

	LOG_DEBUG("Request to rename volume %s from %s to %s\n",
		  volume->name, old_name, new_name);

	if (!(volume->flags & VOLFLAG_ACTIVE)) {
		/* Volume is not active. */
		rc = EINVAL;
		goto out;
	}

	if (memcmp(old_name, EVMS_DEV_NODE_PATH, EVMS_DEV_NODE_PATH_LEN) != 0) {
		LOG_ERROR("Name %s does not have the \"%s\" prefix.\n",
			  old_name, EVMS_DEV_NODE_PATH);
		rc = EINVAL;
		goto out;
	}
        old_base_name = old_name + EVMS_DEV_NODE_PATH_LEN;
	
	if (memcmp(new_name, EVMS_DEV_NODE_PATH, EVMS_DEV_NODE_PATH_LEN) != 0) {
		LOG_ERROR("Name %s does not have the \"%s\" prefix.\n",
			  new_name, EVMS_DEV_NODE_PATH);
		rc = EINVAL;
		goto out;
	}
        new_base_name = new_name + EVMS_DEV_NODE_PATH_LEN;

	switch (dm_version_major) {
	case 3:
		rc = dm_rename_v3(old_base_name, new_base_name);
		break;
	case 4:
		rc = dm_rename_v4(old_base_name, new_base_name);
		break;
	default:
		rc = EINVAL;
	}

out:
	LOG_PROC_EXIT_INT(rc);
	return rc;
}

/**
 * dm_create
 *
 * @object: EVMS object to create a new DM device for.
 *
 * Create a new Device-Mapper device for the specified EVMS object. This
 * device will have no initial mapping, and will be created in a suspended
 * state. The device must have a mapping added using dm_load_targets and
 * then be activated by a call to dm_suspend.
 *
 * This API is only available with DM interface version 4.
 **/
int dm_create(storage_object_t * object)
{
	int rc;

	LOG_PROC_ENTRY();

	if (!object) {
		/* Bad parameters. */
		rc = EINVAL;
		goto out;
	}

	if (object->flags & SOFLAG_ACTIVE) {
		LOG_WARNING("Object %s already has an active DM device.\n",
			    object->name);
		rc = EEXIST;
		goto out;
	}

	LOG_DEBUG("Request to create inactive DM device for object %s\n",
		  object->name);

	switch (dm_version_major) {
	case 4:
		rc = dm_create_v4(object->name,
				  &object->dev_major, &object->dev_minor);
		break;
	default:
		rc = EINVAL;
	}
	if (rc) {
		goto out;
	}

	object->flags |= SOFLAG_ACTIVE;

out:
	LOG_PROC_EXIT_INT(rc);
	return rc;
}

/**
 * dm_update_status
 *
 * @object: EVMS object to retrieve status for.
 *
 * Query device-mapper for the status of this EVMS object. If the device
 * exists in the kernel, it is marked active, and the remaining object
 * information is updated. If the device does not exist in the kernel, the
 * object is marked inactive.
 **/
int dm_update_status(storage_object_t *object)
{
	int active = FALSE;
	int read_only = FALSE;
	int rc;

	LOG_PROC_ENTRY();

	if (!object) {
		/* Bad parameters. */
		rc = EINVAL;
		goto out;
	}

	LOG_DEBUG("Request to update the status of object %s\n", object->name);

	switch (dm_version_major) {
	case 3:
		rc = dm_update_status_v3(object->name,
					 &active, &read_only,
					 &object->dev_major, &object->dev_minor);
		break;
	case 4:
		rc = dm_update_status_v4(object->name,
					 &active, &read_only,
					 &object->dev_major, &object->dev_minor);
		break;
	default:
		rc = EINVAL;
	}
	if (rc) {
		goto out;
	}

	object->flags |= (active    ? SOFLAG_ACTIVE    : 0) |
			 (read_only ? SOFLAG_READ_ONLY : 0);

out:
	LOG_PROC_EXIT_INT(rc);
	return rc;
}

/**
 * dm_update_volume_status
 *
 * @volume: EVMS volume to retrieve status for.
 *
 * Query device-mapper for the status of this EVMS volume. If the device
 * exists in the kernel, it is marked active, and the remaining volume
 * information is updated. If the device does not exist in the kernel, the
 * volume is marked inactive.
 **/
int dm_update_volume_status(logical_volume_t *volume)
{
	char *base_name;
	int active = FALSE;
	int read_only = FALSE;
	int rc;

	LOG_PROC_ENTRY();

	if (!volume) {
		/* Bad parameters. */
		rc = EINVAL;
		goto out;
	}

	LOG_DEBUG("Request to update the status of volume %s\n", volume->name);

	if (memcmp(volume->name, EVMS_DEV_NODE_PATH,
		   EVMS_DEV_NODE_PATH_LEN) != 0) {
		LOG_ERROR("Volume %s does not have the \"%s\" prefix.\n",
			  volume->name, EVMS_DEV_NODE_PATH);
		rc = EINVAL;
		goto out;
	}

	base_name = volume->name + EVMS_DEV_NODE_PATH_LEN;

	switch (dm_version_major) {
	case 3:
		rc = dm_update_status_v3(base_name,
					 &active, &read_only,
					 &volume->dev_major, &volume->dev_minor);
		break;
	case 4:
		rc = dm_update_status_v4(base_name,
					 &active, &read_only,
					 &volume->dev_major, &volume->dev_minor);
		break;
	default:
		rc = EINVAL;
	}
	if (rc) {
		goto out;
	}

	volume->flags |= (active    ? VOLFLAG_ACTIVE    : 0) |
			 (read_only ? VOLFLAG_READ_ONLY : 0);

out:
	LOG_PROC_EXIT_INT(rc);
	return rc;
}

/**
 * dm_get_targets
 *
 * @object:	Object to retrieve target information for.
 * @target_list:Return pointer to list of targets.
 *
 * Query device-mapper for the set of targets that comprise the specified
 * object. This function sets target_list to point at a list of targets. When
 * the caller is done using this list, it must be freed with a call to
 * dm_deallocate_targets().
 **/
int dm_get_targets(storage_object_t *object, dm_target_t **target_list)
{
	int rc;

	LOG_PROC_ENTRY();

	if (!object || !target_list) {
		/* Bad parameters. */
		rc = EINVAL;
		goto out;
	}

	LOG_DEBUG("Request to get the targets for object %s\n", object->name);

	switch (dm_version_major) {
	case 3:
		rc = dm_get_targets_v3(object->name, target_list);
		break;
	case 4:
		rc = dm_get_targets_v4(object->name, target_list);
		break;
	default:
		rc = EINVAL;
	}

out:
	LOG_PROC_EXIT_INT(rc);
	return rc;
}

/**
 * dm_get_volume_targets
 *
 * @volume:	Volume to retrieve target information for.
 * @target_list:Return pointer to list of targets.
 *
 * Query device-mapper for the set of targets that comprise the specified
 * volume. This function sets target_list to point at a list of targets. When
 * the caller is done using this list, it must be freed with a call to
 * dm_deallocate_targets().
 **/
int dm_get_volume_targets(logical_volume_t *volume, dm_target_t **target_list)
{
	int rc;
	char *base_name;

	LOG_PROC_ENTRY();

	if (!volume || !target_list) {
		/* Bad parameters. */
		rc = EINVAL;
		goto out;
	}

	LOG_DEBUG("Request to get the targets for volume %s\n", volume->name);

	if (memcmp(volume->name, EVMS_DEV_NODE_PATH,
		   EVMS_DEV_NODE_PATH_LEN) != 0) {
		LOG_ERROR("Volume %s does not have the \"%s\" prefix.\n",
			  volume->name, EVMS_DEV_NODE_PATH);
		rc = EINVAL;
		goto out;
	}

	base_name = volume->name + EVMS_DEV_NODE_PATH_LEN;

	switch (dm_version_major) {
	case 3:
		rc = dm_get_targets_v3(base_name, target_list);
		break;
	case 4:
		rc = dm_get_targets_v4(base_name, target_list);
		break;
	default:
		rc = EINVAL;
	}

out:
	LOG_PROC_EXIT_INT(rc);
	return rc;
}

/**
 * dm_load_targets
 *
 * @object:		Object to load new mappings for.
 * @target_list:	List of targets for the new mapping.
 *
 * Load new mappings into the Device-Mapper device for the specified object.
 * The mappings will not be activated until a subsequent call to dm_resume.
 *
 * This API is only available with DM interface version 4.
 **/
int dm_load_targets(storage_object_t * object,
		    dm_target_t * target_list)
{
	int rc;

	LOG_PROC_ENTRY();

	if (!object || !target_list) {
		/* Bad parameters. */
		rc = EINVAL;
		goto out;
	}

	if (!(object->flags & SOFLAG_ACTIVE)) {
		/* Can't load mappings into an inactive device. */
		rc = EINVAL;
		goto out;
	}

	LOG_DEBUG("Request to load new targets for object %s\n", object->name);

	/* Create the ASCII parameter strings for all targets. */
	rc = build_target_type_params(target_list);
	if (rc) {
		goto out;
	}

	switch (dm_version_major) {
	case 4:
		rc = dm_load_targets_v4(object->name, target_list,
					(object->flags & SOFLAG_READ_ONLY));
		break;
	default:
		rc = EINVAL;
	}

out:
	/* Free the target parameter strings. */
	deallocate_target_type_params(target_list);
	LOG_PROC_EXIT_INT(rc);
	return rc;
}

/**
 * dm_clear_targets
 *
 * @object: Object to clear an inactive mapping for.
 *
 * Clear the mappings that were added with dm_load_targets. This can only be
 * called before the device has been resumed.
 *
 * This API is only available with DM interface version 4.
 **/
int dm_clear_targets(storage_object_t * object)
{
	int rc;

	LOG_PROC_ENTRY();

	if (!object) {
		/* Bad parameters. */
		rc = EINVAL;
		goto out;
	}

	if (!(object->flags & SOFLAG_ACTIVE)) {
		/* Can't clear mappings for an inactive device. */
		rc = EINVAL;
		goto out;
	}

	LOG_DEBUG("Request to clear the targets for object %s\n", object->name);

	switch (dm_version_major) {
	case 4:
		rc = dm_clear_targets_v4(object->name);
		break;
	default:
		rc = EINVAL;
	}

out:
	LOG_PROC_EXIT_INT(rc);
	return rc;
}

/**
 * dm_get_info
 *
 * @object:	Object to retrieve target information for.
 * @info:	Return pointer to info string.
 *
 * Query device-mapper for the latest status info for the specified object.
 * This function sets info to point at a newly-allocated string. When the
 * caller is done using that string, it must be freed with a call to the
 * engine_free service. Status info is target-dependent. The caller should
 * know how to interpret the returned string.

 * Currently, this function assumes the desired device has only one target.
 * Thus, only one string will be returned. May change this later to return
 * an array of strings, one for each target in the device. However, since
 * currently only snapshot provides any meaningful info for this call, there
 * is no need for multi-target info.
 **/
int dm_get_info(storage_object_t *object, char ** info)
{
	int rc;

	LOG_PROC_ENTRY();

	if (!object || !info) {
		/* Bad parameters. */
		rc = EINVAL;
		goto out;
	}

	LOG_DEBUG("Request to get info for object %s\n", object->name);

	switch (dm_version_major) {
	case 3:
		rc = dm_get_info_v3(object->name, info);
		break;
	case 4:
		rc = dm_get_info_v4(object->name, info);
		break;
	default:
		rc = EINVAL;
	}

out:
	LOG_PROC_EXIT_INT(rc);
	return rc;
}

/**
 * dm_get_devices
 *
 * @device_list: List of devices returned to caller.
 *
 * Get a list of all devices current active in Device-Mapper.
 *
 * This API is only available with DM interface version 4.
 **/
int dm_get_devices(dm_device_list_t ** device_list)
{
	int rc;

	LOG_PROC_ENTRY();

	if (!device_list) {
		/* Bad parameters. */
		rc = EINVAL;
		goto out;
	}

	switch (dm_version_major) {
	case 4:
		rc = dm_get_devices_v4(device_list);
		break;
	default:
		rc = EINVAL;
	}

out:
	LOG_PROC_EXIT_INT(rc);
	return rc;
}

/**
 * dm_get_modules
 *
 * @modules_list: List of modules returned to caller.
 *
 * Get a list of all target-modules current loaded in Device-Mapper.
 *
 * This API is only available with DM interface version 4.
 **/
int dm_get_modules(dm_module_list_t ** module_list)
{
	int rc;

	LOG_PROC_ENTRY();

	if (!module_list) {
		/* Bad parameters. */
		rc = EINVAL;
		goto out;
	}

	switch (dm_version_major) {
	case 4:
		rc = dm_get_modules_v4(module_list);
		break;
	default:
		rc = EINVAL;
	}

out:
	LOG_PROC_EXIT_INT(rc);
	return rc;
}

/**
 * dm_wait
 *
 * @object:	Object to wait for.
 * @event_nr:	Number of event to wait for.
 * @info:	Return pointer to info string.
 *
 * Wait for an event from a Device-Mapper device. This call will wait in the
 * kernel until the event occurs. The caller must specify the event-number they
 * wish to wait for. After the wait completes, the event-number will be filled
 * in with the next event-number that will occur for this device. Also, the
 * "info" string will be filled in with the status-info for the device (in the
 * same format used for dm_get_info).
 *
 * This API is only available with DM interface version 4.
 **/
int dm_wait(storage_object_t * object, unsigned int * event_nr, char ** info)
{
	int rc;

	LOG_PROC_ENTRY();

	if (!object || !event_nr || !info) {
		/* Bad parameters. */
		rc = EINVAL;
		goto out;
	}

	LOG_DEBUG("Request to wait on object %s\n", object->name);

	switch (dm_version_major) {
	case 4:
		rc = dm_wait_v4(object->name, event_nr, info);
		break;
	default:
		rc = EINVAL;
	}

out:
	LOG_PROC_EXIT_INT(rc);
	return rc;
}

/**
 * dm_run_command
 *
 * Send an ioctl to the Device-Mapper driver.
 **/
int dm_run_command(void *dmi, unsigned long command)
{
	int rc;

	LOG_PROC_ENTRY();

	switch (dm_version_major) {
	case 3:
		rc = run_command_v3(dmi, command);
		break;
	case 4:
		rc = run_command_v4(dmi, command);
		break;
	default:
		rc = EINVAL;
	}

	LOG_PROC_EXIT_INT(rc);
	return rc;
}

/**
 * dm_check_version
 *
 * Query device-mapper for the current version of the ioctl interface. If the
 * kernel's version of the interface does not match what EVMS requires, return
 * an error. This function should only be called by the engine core, so it is
 * not exported as a common-service API.
 *
 * This now checks for both version 3 and 4, and sets the dm_version_major
 * global variable appropriately.
 **/
int dm_check_version(void)
{
	int major = 0, minor, patch;
	int rc;

	LOG_PROC_ENTRY();

	/* Use version 4 to get the version number. If that returns an error,
	 * try the same command with version 3. If version 3 is running, it
	 * will print a harmless warning message in syslog. Hopefully users
	 * will see this warning and start migrating to the new kernel
	 * interface. :)
	 */
	rc = dm_get_version_v4(&major, &minor, &patch);
	if (rc) {
		rc = dm_get_version_v3(&major, &minor, &patch);
	}

	LOG_DEFAULT("Device-Mapper interface version: %d.%d.%d\n",
		    major, minor, patch);

	/* EVMS works with version 3 or 4. */
	if (!(major == 3 || major == 4)) {
		LOG_WARNING("Device-Mapper interface version mismatch.\n");
		LOG_WARNING("  EVMS requires: 3.x.x or 4.x.x\n");
		dm_version_major = 0;
		rc = EINVAL;
	} else {
		dm_version_major = major;
	}

	LOG_PROC_EXIT_INT(rc);
	return rc;
}

/**
 * dm_get_version
 *
 * Simple API to get the current DM version. The plugins will be able to use
 * this in case they have things they can do that require version 4. A return
 * value of zero means DM isn't open.
 **/
int dm_get_version(void)
{
	LOG_PROC_ENTRY();
	LOG_PROC_EXIT_INT(dm_version_major);
	return dm_version_major;
}

/**
 * dm_set_suspended_flag
 *
 * @suspended: TRUE if any device is suspended, FALSE if otherwise.
 *
 * Set a global flag indicating there is a DM device in a suspended state. The
 * engine will use this flag to determine if it is allowed to do things like
 * print log entries or user messages.
 *
 * This function should be called *before* the device is suspended, and
 * *after* the device is resumed.
 **/
void dm_set_suspended_flag(int suspended)
{
	LOG_PROC_ENTRY();
	LOG_DETAILS("Setting dm_device_suspended to TRUE.\n");
	dm_device_suspended = suspended;
	LOG_DETAILS("Setting dm_device_suspended to FALSE.\n");
	LOG_PROC_EXIT_VOID();
}