File: engine.c

package info (click to toggle)
evms 1.0.0-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 9,168 kB
  • ctags: 5,853
  • sloc: ansic: 87,317; makefile: 691; sh: 238
file content (2097 lines) | stat: -rw-r--r-- 62,981 bytes parent folder | download
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
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
/*
 *
 *   Copyright (c) International Business Machines  Corp., 2001
 *
 *   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: engine.c
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <time.h>
#include <limits.h>
#include <mntent.h>

#include <fullengine.h>
#include "engine.h"
#include "dload.h"
#include "faulthdlr.h"
#include "handlemgr.h"
#include "internalAPI.h"
#include "memman.h"
#include "commit.h"
#include "discover.h"
#include "message.h"


/*----------------------------------------------------------------------------+
+                                                                             +
+                                  GLOBAL DATA                                +
+                                                                             +
+-----------------------------------------------------------------------------*/

int evms_block_dev_handle = 0;      /* handle to EVMS block device */

dlist_t PluginList           = NULL;
dlist_t DiskList             = NULL;
dlist_t SegmentList          = NULL;
dlist_t ContainerList        = NULL;
dlist_t RegionList           = NULL;
dlist_t EVMSObjectList       = NULL;
dlist_t VolumeList           = NULL;
dlist_t KillSectorList       = NULL;
dlist_t SoftVolumeDeleteList = NULL;
dlist_t HardVolumeDeleteList = NULL;
dlist_t VolumeRemoveList     = NULL;
dlist_t VolumeDataList       = NULL;

BOOLEAN changes_pending   = FALSE;

engine_mode_t engine_mode = ENGINE_CLOSED;

debug_level_t debug_level = DEFAULT;


char * log_file_name = DEFAULT_LOG_FILE;
int    log_file = 0;
u_char log_buf[LOG_BUF_SIZE];

/* Forward references */
static int ensure_dev_node(ADDRESS object,
                           TAG     object_tag,
                           uint    object_size,
                           ADDRESS object_handle,
                           ADDRESS parameters);

/*-------------------------------------------------------------------------------------+
+                                                                                      +
+                                PRIVATE SUBROUTINES                                   +
+                                                                                      +
+-------------------------------------------------------------------------------------*/

char * build_archive_log_name(char * log_name, int index) {
    char * archive_log_file_name;
    char * pch1;
    char * pch2;

    /*
     * Get memory to build the archive file names.  Get enough for the name,
     * three bytes for the ".nn" that will be inserted/appended, plus one for
     * the null terminator.
     */
    archive_log_file_name = malloc(strlen(log_name) + 4);

    if (archive_log_file_name != NULL) {

        strcpy(archive_log_file_name, log_name);

        /*
         * The ".nn" will be inserted ahead of the last '.' in the file name.
         * If the file name doesn't have a '.' in it, the .nn will be
         * appended at the end.
         */
        pch1 = strrchr(archive_log_file_name, '.');
        if (pch1 == NULL) {
            pch1 = archive_log_file_name + strlen(archive_log_file_name);
        }

        /* Add/insert the ".nn". */
        *(pch1++) = '.';
        sprintf(pch1, "%d", index);

        /* Copy the remainder of the file name, if it exists. */
        pch2 = strrchr(log_name, '.');
        if (pch2 != NULL) {
            strcat(pch1, pch2);
        }
    }

    return archive_log_file_name;
}


#define MAX_LOG_ARCHIVES 10

static int start_logging(char * filename) {
    int rc = 0;
    int i;
    char * old_archive_log_name;
    char * new_archive_log_name;

    if (log_file == 0) {

        /* Roll back the archive log files. */
        old_archive_log_name = build_archive_log_name(filename, MAX_LOG_ARCHIVES);
        unlink(old_archive_log_name);

        for (i = MAX_LOG_ARCHIVES; i > 1; i--) {
            new_archive_log_name = build_archive_log_name(filename, i - 1);

            rename(new_archive_log_name, old_archive_log_name);

            free(old_archive_log_name);
            old_archive_log_name = new_archive_log_name;
        }

        rename(filename, old_archive_log_name);

        log_file = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0664);

        if (log_file < 0) {
            rc = log_file;
            log_file = 0;
        }

    } else {
        rc = EEXIST;
    }

    return rc;
}


static int stop_logging() {
    int rc = 0;

    if (log_file > 0) {
        close(log_file);
        log_file = 0;
    } else {
        rc = ENOENT;
    }

    return rc;
}


/*
 * Put a time stamp in the buffer.
 * This function assumes the buffer has enough room for the timestamp.
 */
void timestamp(char * buf, size_t len) {
    time_t t;
    size_t stamp_len;

    time(&t);

    strftime(buf, len, "%b %d %H:%M:%S ", localtime(&t));

    stamp_len = strlen(buf);

    gethostname(buf + stamp_len, len - stamp_len);

    strcat(buf, " ");
}


int engine_write_log_entry(debug_level_t level,
                           char        * fmt,
                           ...) {
    int rc = 0;
    int len;
    va_list args;

    if (level <= debug_level) {
        if (log_file > 0) {
            timestamp(log_buf, LOG_BUF_SIZE);

            strcat(log_buf, "Engine: ");
            len = strlen(log_buf);

            va_start(args, fmt);
            len += vsprintf(log_buf + strlen(log_buf), fmt, args);
            va_end(args);

            if (write(log_file, log_buf, len) < 0) {
                rc = errno;
            }

        } else {
            rc = ENOENT;
        }
    }

    return rc;
}


static int create_evms_dlists(void) {

    int error = ENOMEM;

    LOG_PROC_ENTRY();

    PluginList = CreateList();
    if (PluginList!=NULL) {

        DiskList = CreateList();
        if (DiskList!=NULL) {

            SegmentList = CreateList();
            if (SegmentList != NULL) {
                ContainerList = CreateList();
                if (ContainerList!=NULL) {

                    RegionList = CreateList();
                    if (RegionList != NULL) {
                        EVMSObjectList = CreateList();
                        if (EVMSObjectList!=NULL) {

                            VolumeList = CreateList();
                            if (VolumeList!=NULL) {

                                KillSectorList = CreateList();
                                if (KillSectorList!=NULL) {

                                    SoftVolumeDeleteList = CreateList();
                                    if (SoftVolumeDeleteList != NULL) {

                                        HardVolumeDeleteList = CreateList();
                                        if (HardVolumeDeleteList != NULL) {

                                            VolumeRemoveList = CreateList();
                                            if (VolumeRemoveList != NULL) {

                                                error = 0;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    LOG_PROC_EXIT_INT(error);
    return error;
}


static void free_list_items(dlist_t list) {

    int rc = 0;
    uint size;
    TAG tag;
    ADDRESS obj = NULL;

    rc = BlindGetObject(list,
                        &size,
                        &tag,
                        NULL,
                        FALSE,
                        &obj);

    while ((rc == DLIST_SUCCESS) && (obj != NULL)) {
        switch (tag) {
            case DISK_TAG:
                engine_free_logical_disk((storage_object_t *) obj);
                break;

            case SEGMENT_TAG:
                engine_free_segment((storage_object_t *) obj);
                break;

            case REGION_TAG:
                engine_free_region((storage_object_t *) obj);
                break;

            case EVMS_OBJECT_TAG:
                engine_free_evms_object((storage_object_t *) obj);
                break;

            case CONTAINER_TAG:
                engine_free_container((storage_container_t *) obj);
                break;

            case VOLUME_TAG:
                {
                    logical_volume_t * vol = (logical_volume_t *) obj;

                    destroy_handle(vol->app_handle);
                    engine_unregister_name(vol->name);

                    /*
                     * The volume may also be on the SoftVolumeDeleteList and/or
                     * the HardVolumeDeleteList.  If we are processing the
                     * VolumeList, remove the volume from the
                     * SoftVolumeDeleteList and the HardVolumeDeleteList, if it
                     * is on either of those lists.
                     */
                    if (list == VolumeList) {
                        DeleteObject(SoftVolumeDeleteList, vol);
                        DeleteObject(HardVolumeDeleteList, vol);
                    }
                }

                /*
                 * Fall through to remove the logical_volume from
                 * the list and free its memory.
                 */

            default:
                /*
                 * Don't know what the object is.  Remove the item from the
                 * list and free its memory
                 */
                DeleteObject(list, obj);
                free(obj);
                break;
        }

        /* Get another object from the list. */
        rc = BlindGetObject(list,
                            &size,
                            &tag,
                            NULL,
                            FALSE,
                            &obj);
    }
}


static void destroy_evms_dlists(void) {

    LOG_PROC_ENTRY();

    if (PluginList != NULL) {
        DestroyList(&PluginList, TRUE);
    }

    if (DiskList != NULL) {
        free_list_items(DiskList);
        DestroyList(&DiskList, TRUE);
    }

    if (SegmentList != NULL) {
        free_list_items(SegmentList);
        DestroyList(&SegmentList, TRUE);
    }

    if (ContainerList != NULL) {
        free_list_items(ContainerList);
        DestroyList(&ContainerList, TRUE);
    }

    if (RegionList != NULL) {
        free_list_items(RegionList);
        DestroyList(&RegionList, TRUE);
    }

    if (EVMSObjectList != NULL) {
        free_list_items(EVMSObjectList);
        DestroyList(&EVMSObjectList, TRUE);
    }

    if (VolumeList != NULL) {
        free_list_items(VolumeList);
        DestroyList(&VolumeList, TRUE);
    }

    if (KillSectorList != NULL) {
        DestroyList(&KillSectorList, TRUE);
    }

    if (SoftVolumeDeleteList != NULL) {
        free_list_items(SoftVolumeDeleteList);
        DestroyList(&SoftVolumeDeleteList, TRUE);
    }

    if (HardVolumeDeleteList != NULL) {
        free_list_items(HardVolumeDeleteList);
        DestroyList(&HardVolumeDeleteList, TRUE);
    }

    if (VolumeRemoveList != NULL) {
        free_list_items(VolumeRemoveList);
        DestroyList(&VolumeRemoveList, TRUE);
    }

    if (VolumeDataList != NULL) {
        DestroyList(&VolumeDataList, TRUE);
    }

    LOG_PROC_EXIT_VOID();
}


/*
 *  Called to open the EVMS Common Device Driver so we can send ioctl
 *  commands to the EVMS kernel runtime.
 *
 *  Input:  nothing
 *
 *  Output: rc > 0 if successful, rc = errno if any errors occur
 *
 */

int open_evms_block_dev( void ) {
    dev_t devt;
    int rc = 0;
    struct stat statbuf;
    struct flock lockinfo = {0};

    LOG_PROC_ENTRY();

    /* Only open if we haven't done so already. */
    if (evms_block_dev_handle == 0) {

        /* check for the /dev/evms subdir, and create if it does not exist.
         *
         * If devfs is running and mounted on /dev, these checks will all pass,
         * so a new node will not be created.
         */
        devt = makedev(EVMS_MAJOR, 0) ;
        rc = stat(EVMS_DEV_NODE_PATH, &statbuf);
        if (rc) {
            if (errno == ENOENT) {
                /* dev node does not exist. */
                rc = mkdir(EVMS_DEV_NODE_PATH, (S_IFDIR | S_IRWXU |
                                                S_IRGRP | S_IXGRP |
                                                S_IROTH | S_IXOTH));
            } else {
                LOG_CRITICAL("Problem with EVMS dev directory.  Error code from stat() is %d\n\n", errno);
            }

        } else {
            if (!(statbuf.st_mode & S_IFDIR)) {
                rc = unlink(EVMS_DEV_NODE_PATH);
                if (!rc) {
                    rc = mkdir(EVMS_DEV_NODE_PATH, (S_IFDIR | S_IRWXU |
                                                    S_IRGRP | S_IXGRP |
                                                    S_IROTH | S_IXOTH));
                }
            }
        }

        /*
         * Check for the /dev/evms/block_device node, and create if it does not
         * exist.
         */
        rc = stat(EVMS_DEVICE_NAME, &statbuf);
        if (rc) {
            if (errno == ENOENT) {
                /* dev node does not exist */
                rc = mknod(EVMS_DEVICE_NAME, (S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP), devt);
            } else {
                LOG_CRITICAL("Problem with EVMS block device node directory.  Error code form stat() is %d\n\n", errno);
            }

        } else {
            if (!(statbuf.st_mode & S_IFBLK)) {
                rc = unlink(EVMS_DEVICE_NAME);
                if (!rc) {
                    rc = mknod(EVMS_DEVICE_NAME, (S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP), devt);
                }
            }
        }

        evms_block_dev_handle = open(EVMS_DEVICE_NAME, O_RDWR|O_NONBLOCK);

        if (evms_block_dev_handle > 0) {

            /*
             * Lock our access to the Engine.  Figure out whether to lock for
             * reading or for writing based on the engine_mode.
             */
            if (engine_mode == ENGINE_READONLY) {
                lockinfo.l_type = F_RDLCK;
            } else {
                lockinfo.l_type = F_WRLCK;
            }

            /* Lock the whole thing */
            lockinfo.l_whence = SEEK_SET;
            lockinfo.l_start = 0;
            lockinfo.l_len = 0;

            rc = fcntl(evms_block_dev_handle, F_SETLK, &lockinfo);

            if (rc != 0) {
                char buf1[PATH_MAX + 1];

                /*
                 * We could not lock the Engine.  It must be in use by some
                 * other process.  Try to find out what process it is and tell
                 * the user.
                 */
                rc = fcntl(evms_block_dev_handle, F_GETLK, &lockinfo);

                if (rc == 0) {
                    char buf2[PATH_MAX + 1];
                    int bytes;

                    /*
                     * The file name for the pid is symlinked at
                     * /proc/{pid}/exe
                     */
                    sprintf(buf1, "/proc/%d/exe", lockinfo.l_pid);

                    bytes = readlink(buf1, buf2, sizeof(buf2));

                    if (bytes > 0) {
                        /*
                         * readlink() does not null terminate the returned
                         * file name.
                         */
                        buf2[bytes] = '\0';

                        engine_user_message(NULL, NULL, "The EVMS Engine is currently in use by process %d (%s).\n", lockinfo.l_pid, buf2);

                    } else {
                        /*
                         * Couldn't get the name for the process ID.
                         * Log a message with just the PID.
                         */
                        engine_user_message(NULL, NULL, "The EVMS Engine is currently in use by process %d.\n", lockinfo.l_pid);
                    }

                } else {
                    /*
                     * Couldn't get the lockinfo of the locking process.
                     * Log a generic message.
                     */
                    engine_user_message(NULL, NULL, "The EVMS Engine is currently in use by another process.\n");
                }

                close(evms_block_dev_handle);
                evms_block_dev_handle = EACCES;
            }
        }
    }

    LOG_PROC_EXIT_INT(evms_block_dev_handle);
    return evms_block_dev_handle;
}


void close_evms_block_dev(void) {
    if (evms_block_dev_handle > 0) {
        close(evms_block_dev_handle);
        evms_block_dev_handle = 0;
    }
}


/*
 * Get the kernel volume data for each volume in the system and put it on the
 * VolumeDataList.
 */
int get_kernel_volume_data() {
    int rc = 0;
    int status;

    LOG_PROC_ENTRY();

    if (VolumeDataList != NULL) {
        DestroyList(&VolumeDataList, TRUE);
    }

    VolumeDataList = CreateList();
    if (VolumeDataList != NULL) {
        evms_user_minor_t minor_info;

        /* Run a loop getting a minor code from the EVMS kernel. */

        minor_info.command = EVMS_FIRST_VOLUME;
        minor_info.status = EVMS_VOLUME_VALID;

        status = ioctl(evms_block_dev_handle, EVMS_GET_MINOR, &minor_info);

        /* Set the command to "next volume" for the loop below. */
        minor_info.command = EVMS_NEXT_VOLUME;

        while ((status == 0) && (rc == 0) && (minor_info.status == EVMS_VOLUME_VALID)) {
            evms_volume_data_t * vol_data;

            LOG_DEBUG("Got minor number %d.\n", minor_info.minor);

            /*
             * Allocate a volume data structure and have the kernel fill in the
             * volume data for the minor number.
             */

            vol_data = (evms_volume_data_t *) malloc(sizeof(evms_volume_data_t));
            if (vol_data != NULL) {
                vol_data->minor = minor_info.minor;
                vol_data->status = 0;   /* Not used, but zeroed for safety. */

                status = ioctl(evms_block_dev_handle, EVMS_GET_VOLUME_DATA, vol_data);

                if ((status == 0) && (vol_data->status == 0)) {
                    ADDRESS handle;

                    LOG_DEBUG("Minor number %d is for volume %s.\n", minor_info.minor, vol_data->volume_name);

                    /* Put the volume data structure into our own list. */

                    rc = InsertObject(VolumeDataList,
                                      sizeof(evms_volume_data_t),
                                      vol_data,
                                      VOLUME_DATA_TAG,
                                      NULL,
                                      AppendToList,
                                      FALSE,
                                      &handle);

                    if (rc == DLIST_SUCCESS) {
                        /* Get the next minor number. */
                        status = ioctl(evms_block_dev_handle, EVMS_GET_MINOR, &minor_info);

                    } else {
                        LOG_WARNING("Error code %d when inserting a volume_data structure into the VolumeDataList.\n", rc);
                    }

                } else {
                    LOG_WARNING("Error getting volume data for minor %d.  status is %d.  errno is %d.  vol_data.status is %d.\n", minor_info.minor, status, errno, vol_data->status);
                }

            } else {
                LOG_CRITICAL("Error allocating memory for a evms_volume_data structure.\n");
                rc = ENOMEM;
            }
        }

        if (status != 0) {
            rc = -errno;
            LOG_WARNING("Error from ioctl to get kernel volume minor number.  status is %d.  errno is %d.\n", status, errno);
        }

    } else {
        LOG_CRITICAL("Error allocating memory for the VolumeDataList.\n");
        rc = ENOMEM;
    }

    LOG_PROC_EXIT_INT(rc);
    return rc;
}


/*
 * Check if the Engine is currently allowing read access.  The Engine must be
 * open and a commit must not be in progress.
 */
int check_engine_read_access() {

    int rc = 0;

    LOG_PROC_ENTRY();

    if ((engine_mode == ENGINE_CLOSED) ||
        commit_in_progress) {
        if (engine_mode == ENGINE_CLOSED) {
            LOG_ERROR("The Engine is not open.\n");
        }
        if (commit_in_progress) {
            LOG_ERROR("The Engine is currently committing changes.\n");
        }
        rc = EACCES;
    }

    LOG_PROC_EXIT_INT(rc);
    return rc;
}


/*
 * Check if the Engine is allowing write access, i.e., data structures, etc.
 * can be updated.  The Engine must be opened in a mode that allows writing
 * and a commit must not be in progress.
 */
int check_engine_write_access() {

    int rc = 0;

    LOG_PROC_ENTRY();

    if ((engine_mode == ENGINE_CLOSED) ||
        (engine_mode == ENGINE_READONLY) ||
        commit_in_progress) {
        if (engine_mode == ENGINE_CLOSED) {
            LOG_ERROR("The Engine is not open.\n");
        } else {
            if (engine_mode == ENGINE_READONLY) {
                LOG_ERROR("The Engine is open for reading only.\n");
            }
        }
        if (commit_in_progress) {
            LOG_ERROR("The Engine is currently committing changes.\n");
        }
        rc = EACCES;
    }

    LOG_PROC_EXIT_INT(rc);
    return rc;
}


/*-------------------------------------------------------------------------------------+
+                                                                                      +
+                                 General Interface                                    +
+                                                                                      +
+  Subroutines:  Open, Close, ChangesPending, Rediscover, getting and setting debug    +
+                levels                                                                +
+                                                                                      +
+-------------------------------------------------------------------------------------*/

void evms_get_api_version(evms_version_t * version) {
    version->major      = ENGINE_API_MAJOR_VERSION;
    version->minor      = ENGINE_API_MINOR_VERSION;
    version->patchlevel = ENGINE_API_PATCH_LEVEL;
}


static int check_kernel_ioctl_version(void) {

    int rc = 0;
    int status;
    evms_version_t kernel_ioctl_version;

    LOG_PROC_ENTRY();

    status = ioctl(evms_block_dev_handle, EVMS_GET_IOCTL_VERSION, &kernel_ioctl_version);

    if (status == 0) {
        if (kernel_ioctl_version.major != EVMS_IOCTL_INTERFACE_MAJOR) {
            rc = ENOSYS;

        } else {
            if (kernel_ioctl_version.minor < EVMS_IOCTL_INTERFACE_MINOR) {
                rc = ENOSYS;

            } else {
                if (kernel_ioctl_version.minor == EVMS_IOCTL_INTERFACE_MINOR) {
                    if (kernel_ioctl_version.patchlevel < EVMS_IOCTL_INTERFACE_PATCHLEVEL) {
                        rc = ENOSYS;
                    }
                }
            }
        }

        if (rc != 0) {
            engine_user_message(NULL, NULL, "The EVMS kernel does not supply a version of its ioctl interface that is compatible with this Engine.\n");
            engine_user_message(NULL, NULL, "The Engine requires EVMS kernel ioctl version %d.%d.%d.\n", \
                                EVMS_IOCTL_INTERFACE_MAJOR, EVMS_IOCTL_INTERFACE_MINOR, EVMS_IOCTL_INTERFACE_PATCHLEVEL);
            engine_user_message(NULL, NULL, "The kernel's EVMS ioctl version number is %d.%d.%d.\n", \
                                kernel_ioctl_version.major, kernel_ioctl_version.minor, kernel_ioctl_version.patchlevel);
        } else {
            LOG_DEBUG("The Engine requires EVMS kernel ioctl version %d.%d.%d.\n", \
                      EVMS_IOCTL_INTERFACE_MAJOR, EVMS_IOCTL_INTERFACE_MINOR, EVMS_IOCTL_INTERFACE_PATCHLEVEL);
            LOG_DEBUG("The kernel's EVMS ioctl version number is %d.%d.%d.\n", \
                      kernel_ioctl_version.major, kernel_ioctl_version.minor, kernel_ioctl_version.patchlevel);
        }

    } else {
        /* The ioctl failed.  Get the error number. */
        rc = errno;
    }

    LOG_PROC_EXIT_INT(rc)
    return rc;
}


int evms_open_engine(engine_mode_t                mode,
                     engine_message_functions_t * callbacks,
                     debug_level_t                level,
                     char                       * log_name) {
    int rc = 0;

    ui_callbacks = callbacks;

    debug_level = level;

    if (log_name != NULL) {
        log_file_name = log_name;
    }

    start_logging(log_file_name);

    LOG_PROC_ENTRY();

    if ((mode != ENGINE_READONLY) &&
        (mode != ENGINE_READWRITE)) {
        LOG_ERROR("Open mode of %d is not valid.\n", mode);

        LOG_PROC_EXIT_INT(EINVAL);
        return EINVAL;
    }

    if (engine_mode == ENGINE_CLOSED) {

        engine_mode = mode;

        /* Initialize the random number generator. */
        srand(time(NULL) + getpid());

        /* Make sure we can access the EVMS kernel code. */
        rc = open_evms_block_dev();

        if (rc > 0) {

            rc = check_kernel_ioctl_version();

            if (rc == 0) {
                /* Install signal handlers. */
                install_signal_handlers();

                /* Create the EVMS linked lists. */
                rc = create_evms_dlists();

                if (rc == 0) {
                    /*
                     * Get information on all the kernel exported EVMS
                     * volumes.
                     */
                    rc = get_kernel_volume_data();

                    /* Initialize the Handle Manager. */
                    if (rc == 0) {

                        /*
                         * Make sure the dev nodes are in sync with the
                         * kernel's view of the volumes.
                         */
                        evms_verbose_level_t verbose = NO_MESSAGES;

                        ForEachItem(VolumeDataList,
                                    ensure_dev_node,
                                    &verbose,
                                    TRUE);

                        if (initialize_handle_manager()) {

                            /* Load the EVMS plug-ins. */
                            rc = load_plugins(PluginList);

                            /* Discover what's in the disk system */
                            if (rc == 0) {
                                rc = do_discovery();

                                if (rc == 0) {
                                    /*
                                     * If the Engine is open for read only
                                     * access, close the kernel device handle.
                                     * This lets any other instance of the
                                     * Engine open for write (exclusive).
                                     */
                                    if (mode == ENGINE_READONLY) {
                                        close_evms_block_dev();
                                    }

                                } else {
                                    /*
                                     * Discovery failed.
                                     * Unload the plug-ins.
                                     */
                                    unload_plugins(PluginList);
                                }

                            } else {
                                LOG_DEBUG("Return code from load_plugins is %d.\n", rc);
                            }

                        } else {
                            LOG_CRITICAL("Handle Manager failed to initialize.\n");
                        }

                    } else {
                        LOG_CRITICAL("get_kernel_volume_data failed with return code %d.\n", rc);
                    }

                    if (rc != 0) {
                        /*
                         * Something went wrong.
                         * Destroy the lists we created.
                         */
                        destroy_evms_dlists();
                    }

                } else {
                    LOG_CRITICAL("create_evms_dlists failed with return code %d.\n", rc);
                }
            }

            if (rc != 0) {
                /* Something went wrong.  Close the EVMS block device. */
                close_evms_block_dev();
            }
        }

        if (rc != 0) {
            /* Something went wrong.  Mark the Engine closed. */
            engine_mode = ENGINE_CLOSED;
        }

    } else {
        rc = EACCES;
    }

    LOG_PROC_EXIT_INT(rc);
    return rc;
}


int evms_close_engine(void) {

    LOG_PROC_ENTRY();

    if (engine_mode != ENGINE_CLOSED) {
        engine_mode = ENGINE_CLOSED;
        unload_plugins(PluginList);
        destroy_evms_dlists();
        destroy_all_handles();
        clear_name_registry();
        close_evms_block_dev();
        remove_signal_handlers();

    } else {
        LOG_DEBUG("The Engine is already closed.\n");
    }

    LOG_PROC_EXIT_INT(0);

    stop_logging();

    return 0;
}


BOOLEAN evms_changes_pending(void) {

    LOG_PROC_ENTRY();

    LOG_PROC_EXIT_BOOLEAN(changes_pending);
    return changes_pending;
}


/*
 * evms_rediscover only forces a kernel rediscover.  It does not make the
 * Engine go through discovery again.
 */
int evms_rediscover(void) {

    int rc = 0;

    LOG_PROC_ENTRY();

    rc = kernel_rediscover();

    LOG_PROC_EXIT_INT(rc);
    return rc;
}


/*
 * Get the Engine's current setting of the debug level.
 */
int evms_get_debug_level(debug_level_t * level) {

    int rc = 0;

    LOG_PROC_ENTRY();

    rc = check_engine_read_access();

    if (rc == 0) {
        *level = debug_level;
    }

    LOG_PROC_EXIT_INT(0)
    return 0;
}


/*
 * Set the Engine's debug level.
 */
int evms_set_debug_level(debug_level_t level) {

    int rc = 0;

    LOG_PROC_ENTRY();

    /*
     * People that opened the Engine for read-only are allowed to set their
     * debug level.
     */
    rc = check_engine_read_access();

    if (rc == 0) {

        if ((level >= CRITICAL) &&
            (level <= EVERYTHING)) {
            debug_level = level;

        } else {
            LOG_ERROR("Debug level is out of range.  It must be between %d and %d, inclusive.\n", CRITICAL, EVERYTHING);
            rc = EINVAL;
        }
    }

    LOG_PROC_EXIT_INT(rc)
    return(rc);
}


/*
 * Get the EVMS kernel's current setting of its info level.
 * This API can be called without having the Engine open.
 */
int evms_get_kernel_info_level(debug_level_t * level) {

    int rc = 0;
    int status;

    LOG_PROC_ENTRY();

    if (evms_block_dev_handle >= 0) {
        if (evms_block_dev_handle > 0) {

            /* Call the kernel to get the info level. */
            status = ioctl(evms_block_dev_handle, EVMS_GET_INFO_LEVEL, level);

            /* If something went wrong with the ioctl, return the error code. */
            if (status) {
                rc = errno;
            }

        } else {
            /*
             * The Engine does not have the kernel block device open.
             * Open it.
             */
            rc = open_evms_block_dev();

            if (rc == 0) {

                /* Call the kernel to get the info level. */
                status = ioctl(evms_block_dev_handle, EVMS_GET_INFO_LEVEL, level);

                /* If something went wrong with the ioctl, return the error code. */
                if (status) {
                    rc = errno;
                }

                close_evms_block_dev();
            }
        }

    } else {
        /*
         * The Engine previously attempted to open the EVMS kernel block device
         * and got an error.  evms_block_dev_handle contains the error code.
         */
        rc = evms_block_dev_handle;
    }

    LOG_PROC_EXIT_INT(rc)
    return rc;
}

/*
 * Set the EVMS kernel's info level.
 */
int evms_set_kernel_info_level(debug_level_t level) {

    int rc = 0;
    int status;

    LOG_PROC_ENTRY();

    /*
     * The user must have opened the Engine for readwrite.  Since only one
     * writer is allowed at a time, this guarantees that no other Engine process
     * has the ability to change the kernel's info level.
     */
    rc = check_engine_write_access();

    if (rc == 0) {

        if ((level >= CRITICAL) &&
            (level <= EVERYTHING)) {

            /* Call the kernel to set the info level. */
            status = ioctl(evms_block_dev_handle, EVMS_SET_INFO_LEVEL, &level);

            /* If something went wrong with the ioctl, return the error code. */
            if (status) {
                rc = errno;
            }

        } else {
            LOG_ERROR("Info level is out of range.  It must be between %d and %d, inclusive.\n", CRITICAL, EVERYTHING);
            rc = EINVAL;
        }
    }

    LOG_PROC_EXIT_INT(rc)
    return rc;
}


/*-----------------------------------------------------------------------------+
+                                                                              +
+                              Logging Functions                               +
+                                                                              +
+-----------------------------------------------------------------------------*/

int evms_write_log_entry(debug_level_t level,
                         char        * module_name,
                         char        * fmt,
                         ...) {
    int rc = 0;
    int len;
    va_list args;

    rc = check_engine_read_access();
    if (rc == 0) {
        if (level <= debug_level) {
            if (log_file > 0) {
                timestamp(log_buf, LOG_BUF_SIZE);
                strcat(log_buf, module_name);
                strcat(log_buf, ":  ");
                len = strlen(log_buf);

                va_start(args, fmt);
                len += vsprintf(log_buf + strlen(log_buf), fmt, args);
                va_end(args);

                if (write(log_file, log_buf, len) < 0) {
                    rc = errno;
                }

            } else {
                rc = ENOENT;
            }
        }
    }

    return rc;
}


/*-----------------------------------------------------------------------------+
+                                                                              +
+                             Some List Handlers                               +
+                                                                              +
+-----------------------------------------------------------------------------*/

/*
 * Make sure this object has an app_handle.
 */
int ensure_app_handle(void * object, object_type_t object_type, object_handle_t * app_handle) {
    int rc = 0;

    LOG_PROC_ENTRY();

    if (*app_handle == 0) {
        LOG_DEBUG("Create a handle for a thing of type %d.\n", object_type);
        rc = create_handle(object,
                           object_type,
                           app_handle);
        if (rc == 0) {
            LOG_DEBUG("Handle is %d.\n", *app_handle);
        }
    }

    LOG_PROC_EXIT_INT(rc);
    return rc;
}


/*
 * Add the app_handle for a given object to a handle_array_t.
 * The parameters of this function are structured so that it can be called by
 * ForEachItem().
 */
int make_handle_entry(ADDRESS Object, TAG Tag, uint ObjectSize, ADDRESS ObjectHandle, ADDRESS Parameters) {
    int rc = 0;

    handle_array_t * ha = (handle_array_t *) Parameters;

    LOG_PROC_ENTRY();

    LOG_DEBUG("Current number of entries in handle array:  %d.\n", ha->count);
    switch (Tag) {
        case PLUGIN_TAG:
            {
                plugin_record_t * pPlugRec = (plugin_record_t *) Object;

                LOG_DEBUG("Add entry for plug-in %s.\n", pPlugRec->short_name);
                rc = ensure_app_handle(Object,
                                       PLUGIN,
                                       &pPlugRec->app_handle);
                if (rc == 0) {
                    ha->handle[ha->count] = pPlugRec->app_handle;
                    ha->count++;
                }
            }
            break;

        case DISK_TAG:
        case SEGMENT_TAG:
        case REGION_TAG:
        case EVMS_OBJECT_TAG:
            {
                storage_object_t * pObj = (storage_object_t *) Object;

                LOG_DEBUG("Add entry for storage object %s.\n", pObj->name);
                rc = ensure_app_handle(Object,
                                       pObj->object_type,
                                       &pObj->app_handle);
                if (rc == 0) {
                    ha->handle[ha->count] = pObj->app_handle;
                    ha->count++;
                }
            }
            break;

        case CONTAINER_TAG:
            {
                storage_container_t * pCon = (storage_container_t *) Object;

                LOG_DEBUG("Add entry for container %s.\n", pCon->name);
                rc = ensure_app_handle(Object,
                                       CONTAINER,
                                       &pCon->app_handle);
                if (rc == 0) {
                    ha->handle[ha->count] = pCon->app_handle;
                    ha->count++;
                }
            }
            break;

        case VOLUME_TAG:
            {
                logical_volume_t * pVol = (logical_volume_t *) Object;

                LOG_DEBUG("Add entry for volume %s.\n", pVol->name);
                rc = ensure_app_handle(Object,
                                       VOLUME,
                                       &pVol->app_handle);
                if (rc == 0) {
                    ha->handle[ha->count] = pVol->app_handle;
                    ha->count++;
                }
            }
            break;

        default:
            /* It's nothing for which we hand out app handles.  Skip it. */
            LOG_WARNING("Attempt to make an app handle for an object of tag %ld.\n", Tag);
            break;
    }

    LOG_PROC_EXIT_INT(rc);
    return rc;
}


/*
 * Make a user (it has a Engine memory block header) array of handles
 * (handle_array_t) for the objects in a dlist_t.
 */
int make_user_handle_array(dlist_t list, handle_array_t * * ha) {
    int rc = 0;
    uint count;
    uint size;

    LOG_PROC_ENTRY();

    rc = GetListSize(list, &count);

    if (rc == 0) {
        LOG_DEBUG("Number of objects in the list:  %d\n", count);
        if (count > 1) {
            size = sizeof(handle_array_t) + ((count -1) * sizeof(object_handle_t));
        } else {
            size = sizeof(handle_array_t);
        }

        *ha = alloc_app_struct(size, NULL);
        if (*ha != NULL) {
            rc = ForEachItem(list,
                             make_handle_entry,
                             *ha,
                             TRUE);
        } else {
            rc = ENOMEM;
        }
    }

    LOG_PROC_EXIT_INT(rc);
    return rc;
}


/*
 * Get a list of handles for plug-ins, optionally filtering by plug-in type.
 */
int evms_get_plugin_list(evms_plugin_code_t    type,
                         plugin_search_flags_t flags,
                         handle_array_t    * * plugin_handle_list) {
    int rc = 0;
    dlist_t plugin_list;

    LOG_PROC_ENTRY();

    rc = check_engine_read_access();
    if (rc == 0) {

        if (plugin_handle_list != NULL) {
            rc = engine_get_plugin_list(type, flags, &plugin_list);

            if (rc == 0) {
                rc = make_user_handle_array(plugin_list, plugin_handle_list);

                DestroyList(&plugin_list, FALSE);
            }

        } else {
            LOG_DEBUG("User specified NULL pointer for plugin_handle_list. I'm not doing anything since there is nowhere to put the results.\n");
        }
    }

    LOG_PROC_EXIT_INT(rc);
    return rc;
}


/*
 * Get the handle for a plug-in with a given ID.
 */
int evms_get_plugin_by_ID(plugin_id_t       plugin_ID,
                          object_handle_t * plugin_handle) {
    int rc = 0;
    plugin_record_t * pPlugRec;

    LOG_PROC_ENTRY();

    rc = check_engine_read_access();
    if (rc == 0) {
        rc = engine_get_plugin_by_ID(plugin_ID, &pPlugRec);

        if (rc == 0) {
            rc = ensure_app_handle(pPlugRec,
                                   PLUGIN,
                                   &pPlugRec->app_handle);

            if (rc == 0) {
                *plugin_handle = pPlugRec->app_handle;
            }
        }
    }

    LOG_PROC_EXIT_INT(rc);
    return rc;
}


/*
 * Get the handle for a plug-in with a given short name.
 */
int evms_get_plugin_by_name(char            * plugin_name,
                            object_handle_t * plugin_handle) {
    int rc = 0;

    plugin_record_t * pPlugRec;

    LOG_PROC_ENTRY();

    rc = check_engine_read_access();
    if (rc == 0) {
        rc = engine_get_plugin_by_name(plugin_name, &pPlugRec);

        if (rc == 0) {
            rc = ensure_app_handle(pPlugRec,
                                   PLUGIN,
                                   &pPlugRec->app_handle);

            if (rc == 0) {
                *plugin_handle = pPlugRec->app_handle;
            }
        }
    }

    LOG_PROC_EXIT_INT(rc);
    return rc;
}


/********************************************/
/* Functions for updating the dev node tree */
/********************************************/

/*
 * Check if a volume is mounted.  Return the name of the mount point if
 * requested (mount_name != NULL).
 */
BOOLEAN is_mounted(char * volume_name, char * * mount_name) {

    BOOLEAN result = FALSE;
    FILE *          mount_records;  /* Pointer for system's mount records */
    struct mntent * mount_entry;    /* Holds data for entry in mount list */

    LOG_PROC_ENTRY();

    /*
     * If the caller wants a mount name, initialize it to NULL in case we don't
     * find a mount point for the volume.
     */
    if (mount_name != NULL) {
        *mount_name = NULL;
    }

    mount_records = setmntent(MOUNTED, "r");

    if (mount_records == NULL) {

        /*
         * Unable to access list of mounted devices in /etc/mtab !
         * Attempt to open /proc/mounts for access.
         */
        mount_records = setmntent("/proc/mounts", "r");
    }

    if (mount_records != NULL) {

        /* Scan the mount entries to see if this volume is mounted. */
        while ((!result) && ((mount_entry = getmntent(mount_records)) != NULL)) {
            if (strcmp(mount_entry->mnt_fsname, volume_name) == 0) {
                result = TRUE;

                /* If the user wants to know the mount point, make a copy */
                if (mount_name != NULL) {
                    *mount_name = strdup(mount_entry->mnt_dir);
                }
            }
        }

        /* Close the stream. */
        endmntent(mount_records);

    } else {
        LOG_WARNING("Could not obtain a list of mounted devices from neither /proc/mounts nor " MOUNTED ".\n");
    }

    /* Check if the volume is mounted as swap. */
    if (!result) {
        FILE * proc_swaps;

        proc_swaps = fopen("/proc/swaps", "r");
        if (proc_swaps != NULL) {
            char buffer[EVMS_VOLUME_NAME_SIZE+1];
            int volume_name_len = strlen(volume_name);

            while (!result && (fgets(buffer, EVMS_VOLUME_NAME_SIZE+1, proc_swaps) != NULL)) {

                if (strncmp(buffer, volume_name, volume_name_len) == 0) {
                    result = TRUE;

                    /* If the user wants to know the mount point, make a copy */
                    if (mount_name != NULL) {
                        *mount_name = strdup("swap");
                    }
                }
            }

            fclose(proc_swaps);

        } else {
            LOG_WARNING("Could not open /proc/swaps.\n");
        }
    }

    LOG_PROC_EXIT_BOOLEAN(result);
    return result;
}


/*
 * Make a directory in the EVMS dev node tree.
 * This function is a utility function for make_evms_dev_node() below.
 */
static int make_evms_dir_entry(char * dir_name, evms_verbose_level_t verbose) {
    char name_buf[EVMS_VOLUME_NAME_SIZE];
    char * tmp_ptr = name_buf;
    struct stat statbuf;
    int rc = 0;

    LOG_PROC_ENTRY();

    /* See if this directory exists */
    rc = stat(dir_name, &statbuf);
    if (rc != 0) {
        if (errno == ENOENT) {
            /*
             * This directory doesn't exist.  Peel off the top-most directory
             * name, and try again.
             */
            strcpy(name_buf, dir_name);
            tmp_ptr = strrchr(name_buf, '/');

            if (tmp_ptr != NULL) {
                *tmp_ptr = 0;

                /* Call myself recursively to make the parent directory. */
                rc = make_evms_dir_entry(name_buf, verbose);
                if (rc == 0) {
                    rc = mkdir(dir_name, (S_IFDIR | S_IRWXU |
                                          S_IRGRP | S_IXGRP |
                                          S_IROTH | S_IXOTH));

                    if (rc != 0) {
                        LOG_WARNING("mkdir(%s) failed with error code %d.\n", dir_name, rc);
                        if (verbose >= ERRORS_ONLY) {
                            fprintf(stderr, "mkdir(%s) failed with error code %d.\n", dir_name, rc);
                        }
                    }
                }

            } else {
                /*
                 * Either we were passed a relative path name, or the root
                 * file system doesn't exist.
                 */
                rc = ENODEV;
            }

        } else {
            LOG_WARNING("stat(%s) failed with error code %d.\n", dir_name, errno);
            if (verbose >= ERRORS_ONLY) {
                fprintf(stderr, "stat(%s) failed with error code %d.\n", dir_name, errno);
            }
        }

    } else {
        /* Make sure this is a directory */
        if (!(statbuf.st_mode & S_IFDIR)) {
            LOG_ERROR("%s is a non-directory file\n", dir_name);
            if (verbose >= ERRORS_ONLY) {
                fprintf(stderr, "%s is a non-directory file\n", dir_name);
            }
            rc = EINVAL;
        }
    }

    LOG_PROC_EXIT_INT(rc);
    return rc;
}


/*
 * Make a dev node for a volume.
 */
static int make_evms_dev_node(char * volume_name, uint minor_number, evms_verbose_level_t verbose) {
    dev_t devt;
    char name_buf[EVMS_VOLUME_NAME_SIZE];
    char * tmp_ptr = name_buf;
    int rc = 0;

    LOG_PROC_ENTRY();

    /* Make any directory entries that are necessary */
    strcpy(name_buf, volume_name);
    tmp_ptr = strrchr(name_buf, '/');
    *tmp_ptr = '\0';

    rc = make_evms_dir_entry(name_buf, verbose);
    if (rc == 0) {
        devt = makedev(EVMS_MAJOR, minor_number) ;
        rc = mknod(volume_name, S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, devt);
        if (rc == 0) {
            LOG_DEBUG("Made a node for %s (minor number %d).\n", volume_name, minor_number);
            if (verbose >= ERRORS_AND_INFO) {
                printf("Made a node for %s (minor number %d).\n", volume_name, minor_number);
            }

        } else {
            LOG_WARNING("mknod(%s) failed with error code %d.\n", volume_name, rc);
            if (verbose >= ERRORS_ONLY) {
                fprintf(stderr, "mknod(%s) failed with error code %d.\n", volume_name, rc);
            }
        }

    } else {
        LOG_WARNING("Failure making directory %s (%d)\n", name_buf, rc);
        if (verbose >= ERRORS_ONLY) {
            fprintf(stderr, "Failure making directory %s (%d)\n", name_buf, rc);
        }
    }

    LOG_PROC_EXIT_INT(rc);
    return rc;
}


/*
 * Check if a dev node exists for a volume and that is has the correct minor
 * number.
 * Returns:
 * 0        dev node exists with the correct minor number
 * ENOENT   dev node does not exist
 * EEXIST   dev node exists but has the wrong minor number
 * other    some unexpected error code from stat()
 */
int hasa_dev_node(char * volume_name, uint minor) {

    int rc = 0;
    struct stat statbuf;

    LOG_PROC_ENTRY();

    rc = stat(volume_name, &statbuf);

    if (rc == 0) {

        /* dev node exists. Check if it has the right minor number. */
        dev_t devt;

        devt = makedev(EVMS_MAJOR, minor);
        if (statbuf.st_rdev != devt) {

            /* dev node exists but it has the wrong minor number. */
            rc = EEXIST;
        }

    } else {
        /* stat() failed.  Return the error code. */
        rc = errno;
    }

    LOG_PROC_EXIT_INT(rc);
    return rc;
}


/*
 * Given a kernel volume data structure, make sure there is a dev node for the
 * volume.
 * This function has its parameters structured so that it can be called by the
 * ForEachItem() dlist processor.
 */
static int ensure_dev_node(ADDRESS object,
                           TAG     object_tag,
                           uint    object_size,
                           ADDRESS object_handle,
                           ADDRESS parameters) {

    int rc = 0;
    evms_volume_data_t * vol_data = (evms_volume_data_t *) object;
    evms_verbose_level_t * verbose = (evms_verbose_level_t *) parameters;

    /* Safety check */
    if (object_tag == VOLUME_DATA_TAG) {
        rc = hasa_dev_node(vol_data->volume_name, vol_data->minor);

        switch (rc) {
            case 0:
                if (*verbose >= ERRORS_AND_INFO) {
                    printf("Device node for %s (minor number %d) already exists and is correct.\n", vol_data->volume_name, vol_data->minor);
                }
                break;

            case ENOENT:
                /* /dev node does not exist. */
                rc = make_evms_dev_node(vol_data->volume_name, vol_data->minor, *verbose);
                if (rc) {
                    LOG_WARNING("Failure when making device node for %s.  Return code was %d.\n", vol_data->volume_name, rc);
                    if (*verbose >= ERRORS_ONLY) {
                        fprintf(stderr, "Failure when making device node for %s.  Return code was %d.\n", vol_data->volume_name, rc);
                    }
                }
                break;

            case EEXIST:
                /*
                 * Dev-node minor doesn't match kernel.  Remove and recreate.
                 */
                if (*verbose >= ERRORS_AND_INFO) {
                    printf("The minor number for %s is out of sync with the kernel.  Deleting and recreating.\n", vol_data->volume_name);
                }
                rc = unlink(vol_data->volume_name);
                if (rc == 0) {
                    rc = make_evms_dev_node(vol_data->volume_name, vol_data->minor, *verbose);
                    if (rc != 0) {
                        LOG_WARNING("Failure when making device node for %s.  Return code was %d.\n", vol_data->volume_name, rc);
                    }

                } else {
                    LOG_WARNING("unlink(%s) failed with error code %d.\n", vol_data->volume_name, errno);
                    if (*verbose >= ERRORS_ONLY) {
                        fprintf(stderr, "unlink(%s) failed with error code %d.\n", vol_data->volume_name, errno);
                    }
                }
                break;

            default:
                /*
                 * The stat() command returned an unexpected error when
                 * checking for the dev node.
                 */
                LOG_WARNING("stat(%s) failed with error code %d.\n", vol_data->volume_name, errno);
                if (*verbose >= ERRORS_ONLY) {
                    fprintf(stderr, "stat(%s) failed with error code %d.\n", vol_data->volume_name, errno);
                }
                break;
        }
    }

    return DLIST_SUCCESS;
}


/*
 * Check if a given volume name matches the volume name in a volume data
 * record obtained from the kernel.
 * This function has its parameters structured so that it can be called by
 * the ForEachItem() dlist processor.
 * This function returns 0 if the name does not match, EEXIST if the name does
 * match.  A zero return code will allow ForEachItem() to continue processing
 * the list.  A non-zero return code stops ForEachItem(), which is what we want
 * to do if the volume name matches.
 */
static int is_kernel_volume(ADDRESS object,
                            TAG     object_tag,
                            uint    object_size,
                            ADDRESS object_handle,
                            ADDRESS parameters) {
    int rc = 0;
    evms_volume_data_t * vol_data = (evms_volume_data_t *) object;
    char * vol_name = (char *) parameters;

    LOG_PROC_ENTRY();

    /* Safety check */
    if (object_tag == VOLUME_DATA_TAG) {
        if (strcmp(vol_name,vol_data->volume_name) == 0) {
            rc = EEXIST;
        }
    }

    LOG_PROC_EXIT_INT(rc);
    return rc;
}


/*
 * Given a dev node, delete it if it is not currently exported as a volume by
 * the kernel.
 */
static void remove_dev_node_if_not_kernel_volume(char * dev_node, evms_verbose_level_t verbose) {

    int rc;

    /*
     * Check if we have a kernel volume that matches the one in the dev tree.
     */
    rc = ForEachItem(VolumeDataList,
                     is_kernel_volume,
                     dev_node,
                     TRUE);
    /*
     * is_kernel_volume() returns 0 when it doesn't find a match.  Returning 0
     * enables ForEachItem() to continue processing the list.
     * is_kernel_volume() returns non-zero when it finds a match, which aborts
     * the ForEachItem() which halts the search.  Getting 0 from ForEachItem()
     * means a kernel volume with the same name was not found.
     */
    if (rc == 0) {

        if (!is_mounted(dev_node, NULL)) {
            LOG_DEBUG("Removing unused device node %s.\n", dev_node);
            if (verbose >= ERRORS_AND_INFO) {
                printf("Removing unused device node %s.\n", dev_node);
            }

            rc = unlink(dev_node);

            if (rc != 0) {
                LOG_WARNING("unlink(%s) failed with error code %d.\n", dev_node, errno);
                if (verbose >= ERRORS_ONLY) {
                    fprintf(stderr, "unlink(%s) failed with error code %d.\n", dev_node, errno);
                }
            }

        } else {
            LOG_WARNING("Volume %s is not exported by the kernel but its device node cannot be deleted because it is mounted.\n", dev_node);
            if (verbose >= ERRORS_ONLY) {
                fprintf(stderr, "Volume %s is not exported by the kernel but its device node cannot be deleted because it is mounted.\n", dev_node);
            }
        }
    }
}


/*
 * Given a directory in the /dev/evms tree Remove any nodes and directories that
 * are not currently exported as volumes by the kernel.  If this directory ends
 * up having no nodes, then the directory is removed.
 * This function calls itself recursively to prune subdirectories that are in
 * this directory.
 */
static void prune_dev_tree(char * dir_name, evms_verbose_level_t verbose) {

    int   rc;
    DIR * pDir;
    char  full_name[EVMS_VOLUME_NAME_SIZE + 1];
    uint  name_offset;

    strcpy(full_name, dir_name);
    strcat(full_name, "/");
    name_offset = strlen(full_name);

    pDir = opendir(dir_name);
    if (pDir != NULL) {
        BOOLEAN dir_is_empty;
        struct dirent * pDirent = readdir(pDir);

        while (pDirent != NULL) {

            if ((strcmp(pDirent->d_name, ".") != 0) &&
                (strcmp(pDirent->d_name, "..") != 0)) {

                /* Build the full path to the directory entry. */
                strcpy(full_name + name_offset, pDirent->d_name);

                if (pDirent->d_type == DT_DIR) {
                    prune_dev_tree(full_name, verbose);
                } else {
                    remove_dev_node_if_not_kernel_volume(full_name, verbose);
                }
            }

            pDirent = readdir(pDir);
        }

        /* Check if the directory is empty. */
        dir_is_empty = TRUE;

        rewinddir(pDir);

        pDirent = readdir(pDir);
        while ((pDirent != NULL) && dir_is_empty) {
            if ((strcmp(pDirent->d_name, ".") != 0) &&
                (strcmp(pDirent->d_name, "..") != 0)) {
                dir_is_empty = FALSE;
            }

            pDirent = readdir(pDir);
        }

        closedir(pDir);

        /* Remove the directory if it is empty. */
        if (dir_is_empty) {

            LOG_DEBUG("Removing empty directory %s.\n", dir_name);
            if (verbose >= ERRORS_AND_INFO) {
                printf("Removing empty directory %s.\n", dir_name);
            }
            rc = rmdir(dir_name);

            if (rc != 0) {
                LOG_WARNING("rmdir(%s) failed with error code %d.\n", dir_name, errno);
                if (verbose >= ERRORS_ONLY) {
                    fprintf(stderr, "rmdir(%s) failed with error code %d.\n", dir_name, errno);
                }
            }
        }
    }
}


/*
 * Remove any nodes and directories in the /dev/evms tree that are not
 * currently exported as volumes by the kernel.
 */
static void remove_unused_dev_nodes(evms_verbose_level_t verbose) {

    DIR           * pDir;
    struct dirent * pDirent;
    char            full_name[EVMS_VOLUME_NAME_SIZE + 1];
    uint            name_offset;

    strcpy(full_name, DEV_PATH "/" EVMS_DIR_NAME);
    strcat(full_name, "/");
    name_offset = strlen(full_name);

    pDir = opendir(DEV_PATH "/" EVMS_DIR_NAME);
    if (pDir != NULL) {

        pDirent = readdir(pDir);
        while (pDirent != NULL) {

            if ((strcmp(pDirent->d_name, ".") != 0) &&
                (strcmp(pDirent->d_name, "..") != 0) &&
                (strcmp(pDirent->d_name, EVMS_DEV_NAME) != 0)) {

                /* Build the full path to the directory entry. */
                strcpy(full_name + name_offset, pDirent->d_name);

                if (pDirent->d_type == DT_DIR) {
                    prune_dev_tree(full_name, verbose);
                } else {
                    remove_dev_node_if_not_kernel_volume(full_name, verbose);
                }
            }

            pDirent = readdir(pDir);
        }

        closedir(pDir);
    }
}


/*
 * Check if devfs is installed.
 */
BOOLEAN is_devfs_installed() {

    int rc = 0;
    struct stat statbuf;

    LOG_PROC_ENTRY();

    rc = stat("/dev/.devfsd", &statbuf);

    LOG_PROC_EXIT_BOOLEAN((rc == 0));
    return(rc == 0);
}


/*
 * Update the /dev/evms tree so that its nodes reflect the volumes that
 * are exported by the EVMS kernel.  This command will remove nodes and
 * directories that are no longer exported by the EVMS kernel.  It will
 * create/update the nodes in the tree so that there is a node with the
 * correct minor number for each volume exported by the EVMS kernel.
 * This API can be run without calling evms_open_engine() first.
 * The verbose_level says how much information to display.
 */
int evms_update_evms_dev_tree(evms_verbose_level_t verbose) {

    int rc = 0;
    engine_mode_t prev_engine_mode = engine_mode;

    LOG_PROC_ENTRY();

    /*
     * Check for devfs.  If devfs is installed, then the dev tree is always
     * up to date and there is nothing to do.
     */

    if (!is_devfs_installed()) {

        /* Does this process already have the EVMS block device open? */
        if (evms_block_dev_handle == 0) {

            /*
             * This process does not have the EVMS block device opened.
             * Either the process didn't open the Engine at all or it opened
             * it in read-only mode (which closes the block device when it
             * is not in use).  Open the block device.
             */
            open_evms_block_dev();

            if (evms_block_dev_handle >= ERRORS_ONLY) {

                /*
                 * Mark the Engine open for read-only mode.  The Engine will
                 * either be closed or already opened in read only mode.  If the
                 * Engine was opened in readwrite mode we wouldn't be here
                 * because the block device would already be open.  We saved the
                 * previous Engine mode above (closed or read-only).
                 */
                engine_mode = ENGINE_READWRITE;
            }
        }

        if (evms_block_dev_handle > 0) {

            /*
             * Get a list of volumes from the kernel point of view if we don't
             * have one already.
             */
            if (VolumeDataList == NULL) {
                rc = get_kernel_volume_data();
            }

            if (rc == 0) {

                /* Prune out unused dev nodes. */
                if (verbose >= ERRORS_AND_INFO) {
                    printf("Removing unused EVMS device nodes and directories...\n");
                }

                remove_unused_dev_nodes(verbose);

                /*
                 * Make sure there is a dev node for each kernel volume and
                 * that its minor number is correct.
                 */
                if (verbose >= ERRORS_AND_INFO) {
                    printf("Making device nodes for EVMS kernel volumes...\n");
                }

                ForEachItem(VolumeDataList,
                            ensure_dev_node,
                            &verbose,
                            TRUE);
            }

            /*
             * If the Engine mode is read-only that means we opened the EVMS
             * block device.  Close it and restore the Engine mode.
             */
            if (engine_mode == ENGINE_READONLY) {
                close_evms_block_dev();
                engine_mode = prev_engine_mode;
            }

        } else {
            /*
             * We couldn't open the EVMS block device.  evms_block_dev_handle
             * contains the error code.
             */
            rc = evms_block_dev_handle;
        }

    } else {
        LOG_DEBUG("devfs is installed.  I have no work to do.\n");
        if (verbose >= ERRORS_AND_INFO) {
            printf("devfs is installed.  I have no work to do.\n");
        }
    }

    LOG_PROC_EXIT_INT(rc);
    return rc;
}