summaryrefslogtreecommitdiffstats
path: root/private/ntos/arcinst/fdengine.c
blob: 7644f222fc779377e7be50e868059bcadcfc4280 (plain) (blame)
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
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
/*++

Copyright (c) 1991  Microsoft Corporation

Module Name:

    fdengine.c

Abstract:

    This module contains the disk partitioning engine.  The code
    in this module can be compiled for either the NT platform
    or the ARC platform (-DARC).

Author:

    Ted Miller        (tedm)    Nov-1991

Revision History:

--*/

#include "precomp.h"
#pragma hdrstop

#define ARCDBG

// Attached disk devices.

ULONG              CountOfDisks;
PCHAR             *DiskNames;

// Information about attached disks.

DISKGEOM          *DiskGeometryArray;

PPARTITION        *PrimaryPartitions,
                  *LogicalVolumes;

//
// Array keeping track of whether each disk is off line.
//
PBOOLEAN           OffLine;

// Keeps track of whether changes have been made
// to each disk's partition structure.

BOOLEAN           *ChangesMade;


//
// Value used to indicate that the partition entry has changed but in a non-
// destructive way (ie, made active/inactive).
//
#define CHANGED_DONT_ZAP ((BOOLEAN)(5))

// forward declarations


ARC_STATUS
OpenDisks(
    VOID
    );

VOID
CloseDisks(
    VOID
    );

ARC_STATUS
GetGeometry(
    VOID
    );

BOOLEAN
CheckIfDiskIsOffLine(
    IN ULONG Disk
    );

ARC_STATUS
InitializePartitionLists(
    VOID
    );

ARC_STATUS
GetRegions(
    IN  ULONG               Disk,
    IN  PPARTITION          p,
    IN  BOOLEAN             WantUsedRegions,
    IN  BOOLEAN             WantFreeRegions,
    IN  BOOLEAN             WantLogicalRegions,
    OUT PREGION_DESCRIPTOR *Region,
    OUT ULONG              *RegionCount,
    IN  REGION_TYPE         RegionType
    );

BOOLEAN
AddRegionEntry(
    IN OUT PREGION_DESCRIPTOR *Regions,
    IN OUT ULONG              *RegionCount,
    IN     ULONG               SizeMB,
    IN     REGION_TYPE         RegionType,
    IN     PPARTITION          Partition,
    IN     LARGE_INTEGER       AlignedRegionOffset,
    IN     LARGE_INTEGER       AlignedRegionSize
    );

VOID
AddPartitionToLinkedList(
    IN PARTITION **Head,
    IN PARTITION *p
    );

BOOLEAN
IsInLinkedList(
    IN PPARTITION p,
    IN PPARTITION List
    );

BOOLEAN
IsInLogicalList(
    IN ULONG      Disk,
    IN PPARTITION p
    );

BOOLEAN
IsInPartitionList(
    IN ULONG      Disk,
    IN PPARTITION p
    );

LARGE_INTEGER
AlignTowardsDiskStart(
    IN ULONG         Disk,
    IN LARGE_INTEGER Offset
    );

LARGE_INTEGER
AlignTowardsDiskEnd(
    IN ULONG         Disk,
    IN LARGE_INTEGER Offset
    );

VOID
FreeLinkedPartitionList(
    IN PARTITION **q
    );

VOID
MergeFreePartitions(
    IN PPARTITION p
    );

VOID
RenumberPartitions(
    ULONG Disk
    );

VOID
FreePartitionInfoLinkedLists(
    IN PARTITION **ListHeadArray
    );

LARGE_INTEGER
DiskLengthBytes(
    IN ULONG Disk
    );

PPARTITION
AllocatePartitionStructure(
    IN ULONG         Disk,
    IN LARGE_INTEGER Offset,
    IN LARGE_INTEGER Length,
    IN UCHAR         SysID,
    IN BOOLEAN       Update,
    IN BOOLEAN       Active,
    IN BOOLEAN       Recognized
    );



ARC_STATUS
FdiskInitialize(
    VOID
    )

/*++

Routine Description:

    This routine initializes the partitioning engine, including allocating
    arrays, determining attached disk devices, and reading their
    partition tables.

Arguments:

    None.

Return Value:

    OK_STATUS or error code.

--*/

{
    ARC_STATUS status;
    ULONG        i;


    if((status = LowQueryFdiskPathList(&DiskNames,&CountOfDisks)) != OK_STATUS) {
        return(status);
    }

#if 0
#ifdef ARCDBG
    AlPrint("Disk count = %u\r\n",CountOfDisks);
    for(i=0; i<CountOfDisks; i++) {
        AlPrint("Disk %u: %s\r\n",i,DiskNames[i]);
    }
    WaitKey();
#endif
#endif

    DiskGeometryArray = NULL;
    PrimaryPartitions = NULL;
    LogicalVolumes = NULL;

    if(((DiskGeometryArray      = AllocateMemory(CountOfDisks * sizeof(DISKGEOM  ))) == NULL)
    || ((ChangesMade            = AllocateMemory(CountOfDisks * sizeof(BOOLEAN   ))) == NULL)
    || ((PrimaryPartitions      = AllocateMemory(CountOfDisks * sizeof(PPARTITION))) == NULL)
    || ((OffLine                = AllocateMemory(CountOfDisks * sizeof(BOOLEAN   ))) == NULL)
    || ((LogicalVolumes         = AllocateMemory(CountOfDisks * sizeof(PPARTITION))) == NULL))
    {
        RETURN_OUT_OF_MEMORY;
    }

    for(i=0; i<CountOfDisks; i++) {
        PrimaryPartitions[i] = NULL;
        LogicalVolumes[i] = NULL;
        ChangesMade[i] = FALSE;
        OffLine[i] = CheckIfDiskIsOffLine(i);
        if(OffLine[i]) {
            return(ENODEV);
        }
    }

    if(((status = GetGeometry()             ) != OK_STATUS)
    || ((status = InitializePartitionLists()) != OK_STATUS))
    {
        return(status);
    }

    return(OK_STATUS);
}


VOID
FdiskCleanUp(
    VOID
    )

/*++

Routine Description:

    This routine deallocates storage used by the partitioning engine.

Arguments:

    None.

Return Value:

    None.

--*/

{
    LowFreeFdiskPathList(DiskNames,CountOfDisks);

    if(DiskGeometryArray != NULL) {
        FreeMemory(DiskGeometryArray);
    }
    if(PrimaryPartitions != NULL) {
        FreePartitionInfoLinkedLists(PrimaryPartitions);
        FreeMemory(PrimaryPartitions);
    }
    if(LogicalVolumes != NULL) {
        FreePartitionInfoLinkedLists(LogicalVolumes);
        FreeMemory(LogicalVolumes);
    }
    if(ChangesMade != NULL) {
        FreeMemory(ChangesMade);
    }
    if(OffLine != NULL) {
        FreeMemory(OffLine);
    }
}


BOOLEAN
CheckIfDiskIsOffLine(
    IN ULONG Disk
    )

/*++

Routine Description:

    Determine whether a disk is off-line by attempting to open it.
    If this is diskman, also attempt to read from it.

Arguments:

    Disk - supplies number of the disk to check

Return Value:

    TRUE if disk is off-line, FALSE is disk is on-line.

--*/

{
    ULONG Handle;
    BOOLEAN IsOffLine;

    if(LowOpenDisk(GetDiskName(Disk),&Handle) == OK_STATUS) {

        IsOffLine = FALSE;
        LowCloseDisk(Handle);

    } else {

        IsOffLine = TRUE;
    }

    return(IsOffLine);
}


ARC_STATUS
GetGeometry(
    VOID
    )

/*++

Routine Description:

    This routine determines disk geometry for each disk device.
    Disk geometry includes heads, sectors per track, cylinder count,
    and bytes per sector.  It also includes bytes per track and
    bytes per cylinder, which are calculated from the other values
    for the convenience of the rest of this module.

    Geometry information is placed in the DiskGeometryArray global variable.

    Geometry information is undefined for an off-line disk.

Arguments:

    None.

Return Value:

    OK_STATUS or error code.

--*/

{
    ULONG       i;
    ARC_STATUS status;
    ULONG       TotalSectorCount,
                SectorSize,
                SectorsPerTrack,
                Heads;

    for(i=0; i<CountOfDisks; i++) {

        if(OffLine[i]) {
            continue;
        }

        if((status = LowGetDriveGeometry(DiskNames[i],&TotalSectorCount,&SectorSize,&SectorsPerTrack,&Heads)) != OK_STATUS) {
            return(status);
        }

        DiskGeometryArray[i].BytesPerSector   = SectorSize;
        DiskGeometryArray[i].SectorsPerTrack  = SectorsPerTrack;
        DiskGeometryArray[i].Heads            = Heads;
        DiskGeometryArray[i].Cylinders.QuadPart = TotalSectorCount / (SectorsPerTrack * Heads);
        DiskGeometryArray[i].BytesPerTrack    = SectorsPerTrack * SectorSize;
        DiskGeometryArray[i].BytesPerCylinder = SectorsPerTrack * SectorSize * Heads;
    }
    return(OK_STATUS);
}


#if i386
VOID
SetPartitionActiveFlag(
    IN PREGION_DESCRIPTOR Region,
    IN UCHAR              value
    )
{
    PPARTITION p = ((PREGION_DATA)Region->Reserved)->Partition;

    if((UCHAR)p->Active != value) {

        //
        // Unfortuneately, the Update flag becomes the RewritePartition flag
        // at commit time.  This causes us to zap the boot sector.  To avoid
        // this, we use a spacial non-boolean value that can be checked for
        // at commit time and that will cause us NOT to zap the bootsector
        // even though RewritePartition will be TRUE.
        //

        p->Active = value;
        if(!p->Update) {
            p->Update = CHANGED_DONT_ZAP;
        }
        ChangesMade[p->Disk] = TRUE;
    }
}
#endif


VOID
DetermineCreateSizeAndOffset(
    IN  PREGION_DESCRIPTOR Region,
    IN  LARGE_INTEGER      MinimumSize,
    IN  ULONG              CreationSizeMB,
    IN  REGION_TYPE        Type,
    OUT PLARGE_INTEGER     CreationStart,
    OUT PLARGE_INTEGER     CreationSize
    )

/*++

Routine Description:

    Determine the actual offset and size of the partition, given the
    size in megabytes.

Arguments:

    Region  - a region descriptor returned by GetDiskRegions().  Must
              be an unused region.

    MinimumSize - if non-0, this is the minimum size that the partition
        or logical drive can be.

    CreationSizeMB - If MinimumSize is 0, size of partition to create, in MB.

    Type    - REGION_PRIMARY, REGION_EXTENDED, or REGION_LOGICAL, for
              creating a primary partition, extended partition, or
              logical volume, respectively.

    CreationStart - receives the offset where the partition should be placed.

    CreationSize - receives the exact size for the partition.

Return Value:

    None.

--*/

{
    PREGION_DATA CreateData = Region->Reserved;
    LARGE_INTEGER CSize,CStart;
    LARGE_INTEGER Mod;
    ULONG  bpc = DiskGeometryArray[Region->Disk].BytesPerCylinder;
    ULONG  bpt = DiskGeometryArray[Region->Disk].BytesPerTrack;

    ASRT(Region->SysID == SYSID_UNUSED);

    //
    // If we are creating a partition at offset 0, adjust the aligned region
    // offset and the aligned region size, because no partition can actually
    // start at offset 0.
    //

    if(CreateData->AlignedRegionOffset.QuadPart == 0) {

        LARGE_INTEGER Delta;

        if(Type == REGION_EXTENDED) {

            Delta.QuadPart = bpc;

        } else {

            ASRT(Type == REGION_PRIMARY);
            Delta.QuadPart = bpt;
        }

        CreateData->AlignedRegionOffset = Delta;
        CreateData->AlignedRegionSize.QuadPart = (CreateData->AlignedRegionSize.QuadPart - Delta.QuadPart);
    }

    CStart = CreateData->AlignedRegionOffset;
    if(MinimumSize.QuadPart == 0) {
        CSize.QuadPart = UInt32x32To64(CreationSizeMB,ONE_MEG);
    } else {
        CSize = MinimumSize;
        if(Type == REGION_LOGICAL) {
            CSize.QuadPart = CSize.QuadPart + bpt;
        }
    }

    //
    // Decide whether to align the ending cylinder up or down.
    // If the offset of end of the partition is more than half way into the
    // final cylinder, align towrds the disk end.  Otherwise align toward
    // the disk start.
    //

    Mod.QuadPart = (CStart.QuadPart + CSize.QuadPart) % bpc;

    if(Mod.QuadPart != 0) {

        if((MinimumSize.QuadPart != 0) || (Mod.QuadPart > bpc/2)) {
            CSize.QuadPart = (CSize.QuadPart + (bpc - Mod.QuadPart));
        } else {
            CSize.QuadPart = (CSize.QuadPart - Mod.QuadPart);        // snap downwards tp cyl boundary
        }
    }

    if(CSize.QuadPart > CreateData->AlignedRegionSize.QuadPart) {

        //
        // Space available in the free space isn't large enough to accomodate
        // the request in its entirety;  just use the entire free space.
        //

        CSize  = CreateData->AlignedRegionSize;
    }

    ASRT(CStart.QuadPart != 0);
    ASRT(CSize.QuadPart != 0);

    *CreationStart = CStart;
    *CreationSize  = CSize;
}


ARC_STATUS
CreatePartitionEx(
    IN PREGION_DESCRIPTOR Region,
    IN LARGE_INTEGER      MinimumSize,
    IN ULONG              CreationSizeMB,
    IN REGION_TYPE        Type,
    IN UCHAR              SysId
    )

/*++

Routine Description:

    This routine creates a partition from a free region on the disk.  The
    partition is always created at the beginning of the free space, and any
    left over space at the end is kept on the free space list.

Arguments:

    Region  - a region descriptor returned by GetDiskRegions().  Must
              be an unused region.

    CreationSizeMB - size of partition to create, in MB.

    Type    - REGION_PRIMARY, REGION_EXTENDED, or REGION_LOGICAL, for
              creating a primary partition, extended pasrtition, or
              logical volume, respectively.

    SysId - system ID byte to be assigned to the partition

Return Value:

    OK_STATUS or error code.

--*/

{
    PPARTITION    p1,p2,p3;
    PREGION_DATA  CreateData = Region->Reserved;
    LARGE_INTEGER CreationStart,CreationSize,LeftOver;
    PPARTITION   *PartitionList;

    ASRT(Region->SysID == SYSID_UNUSED);

    DetermineCreateSizeAndOffset( Region,
                                  MinimumSize,
                                  CreationSizeMB,
                                  Type,
                                  &CreationStart,
                                  &CreationSize
                                );

    //
    // now we've got the start and size of the partition to be created.
    // If there's left-over at the beginning of the free space (after
    // alignment), make a new PARTITION structure.

    p1 = NULL;
    LeftOver.QuadPart = (CreationStart.QuadPart - CreateData->Partition->Offset.QuadPart);

    if(LeftOver.QuadPart > 0) {

        p1 = AllocatePartitionStructure(Region->Disk,
                                        CreateData->Partition->Offset,
                                        LeftOver,
                                        SYSID_UNUSED,
                                        FALSE,
                                        FALSE,
                                        FALSE
                                       );
        if(p1 == NULL) {
            RETURN_OUT_OF_MEMORY;
        }
    }

    // make a new partition structure for space being left free as
    // a result of this creation.

    p2 = NULL;
    LeftOver.QuadPart = (CreateData->Partition->Offset.QuadPart + CreateData->Partition->Length.QuadPart) -
                        (CreationStart.QuadPart + CreationSize.QuadPart);

    if(LeftOver.QuadPart != 0) {

        LARGE_INTEGER   TmpResult;

        TmpResult.QuadPart = CreationStart.QuadPart + CreationSize.QuadPart;
        p2 = AllocatePartitionStructure(Region->Disk,
                                        TmpResult,
                                        LeftOver,
                                        SYSID_UNUSED,
                                        FALSE,
                                        FALSE,
                                        FALSE
                                       );
        if(p2 == NULL) {
            RETURN_OUT_OF_MEMORY;
        }
    }

    // adjust the free partition's fields.

    CreateData->Partition->Offset = CreationStart;
    CreateData->Partition->Length = CreationSize;
    CreateData->Partition->SysID  = SysId;
    CreateData->Partition->Update = TRUE;
    CreateData->Partition->Recognized = TRUE;

    // if we just created an extended partition, show the whole thing
    // as one free logical region.

    if(Type == REGION_EXTENDED) {

        ASRT(LogicalVolumes[Region->Disk] == NULL);

        p3 = AllocatePartitionStructure(Region->Disk,
                                        CreationStart,
                                        CreationSize,
                                        SYSID_UNUSED,
                                        FALSE,
                                        FALSE,
                                        FALSE
                                       );
        if(p3 == NULL) {
            RETURN_OUT_OF_MEMORY;
        }
        AddPartitionToLinkedList(&LogicalVolumes[Region->Disk],p3);
    }

    PartitionList = (Type == REGION_LOGICAL)
                  ? &LogicalVolumes[Region->Disk]
                  : &PrimaryPartitions[Region->Disk];

    if(p1) {
        AddPartitionToLinkedList(PartitionList,p1);
    }
    if(p2) {
        AddPartitionToLinkedList(PartitionList,p2);
    }

    MergeFreePartitions(*PartitionList);
    RenumberPartitions(Region->Disk);

    ChangesMade[Region->Disk] = TRUE;

    return(OK_STATUS);
}


ARC_STATUS
CreatePartition(
    IN PREGION_DESCRIPTOR Region,
    IN ULONG              CreationSizeMB,
    IN REGION_TYPE        Type
    )
{
    LARGE_INTEGER   LargeZero;

    LargeZero.QuadPart = 0;
    return(CreatePartitionEx(Region,
                             LargeZero,
                             CreationSizeMB,
                             Type,
                             (UCHAR)((Type == REGION_EXTENDED) ? SYSID_EXTENDED
                                                               : SYSID_BIGFAT
                                    )
                            )
          );
}



ARC_STATUS
DeletePartition(
    IN PREGION_DESCRIPTOR Region
    )

/*++

Routine Description:

    This routine deletes a partition, returning its space to the
    free space on the disk.  If deleting the extended partition,
    all logical volumes within it are also deleted.

Arguments:

    Region  - a region descriptor returned by GetDiskRegions().  Must
              be a used region.

Return Value:

    OK_STATUS or error code.

--*/

{
    PREGION_DATA  RegionData = Region->Reserved;
    PPARTITION   *PartitionList;

    ASRT(    IsInPartitionList(Region->Disk,RegionData->Partition)
          || IsInLogicalList  (Region->Disk,RegionData->Partition)
        );

    if(IsExtended(Region->SysID)) {

        ASRT(IsInPartitionList(Region->Disk,RegionData->Partition));

        // Deleting extended partition.  Also delete all logical volumes.

        FreeLinkedPartitionList(&LogicalVolumes[Region->Disk]);
    }

    RegionData->Partition->SysID  = SYSID_UNUSED;
    RegionData->Partition->Update = TRUE;
    RegionData->Partition->Active = FALSE;
    RegionData->Partition->OriginalPartitionNumber = 0;

    PartitionList = (Region->RegionType == REGION_LOGICAL)
                  ? &LogicalVolumes[Region->Disk]
                  : &PrimaryPartitions[Region->Disk];

    MergeFreePartitions(*PartitionList);
    RenumberPartitions(Region->Disk);

    ChangesMade[Region->Disk] = TRUE;

    return(OK_STATUS);
}


ARC_STATUS
GetDiskRegions(
    IN  ULONG               Disk,
    IN  BOOLEAN             WantUsedRegions,
    IN  BOOLEAN             WantFreeRegions,
    IN  BOOLEAN             WantPrimaryRegions,
    IN  BOOLEAN             WantLogicalRegions,
    OUT PREGION_DESCRIPTOR *Region,
    OUT ULONG              *RegionCount
    )

/*++

Routine Description:

    This routine returns an array of region descriptors to the caller.
    A region desscriptor describes a space on the disk, either used
    or free.  The caller can control which type of regions are returned.

    The caller must free the returned array via FreeRegionArray().

Arguments:

    Disk            - index of disk whose regions are to be returned

    WantUsedRegions - whether to return used disk regions

    WantFreeRegions - whether to return free disk regions

    WantPrimaryRegions - whether to return regions not in the
                         extended partition

    WantLogicalRegions - whether to return regions within the
                         extended partition

    Region          - where to put a pointer to the array of regions

    RegionCount     - where to put the number of items in the returned
                      Region array

Return Value:

    OK_STATUS or error code.

--*/

{
    *Region = AllocateMemory(0);
    *RegionCount = 0;

    if(WantPrimaryRegions) {
        return(GetRegions(Disk,
                          PrimaryPartitions[Disk],
                          WantUsedRegions,
                          WantFreeRegions,
                          WantLogicalRegions,
                          Region,
                          RegionCount,
                          REGION_PRIMARY
                         )
              );
    } else if(WantLogicalRegions) {
        return(GetRegions(Disk,
                          LogicalVolumes[Disk],
                          WantUsedRegions,
                          WantFreeRegions,
                          FALSE,
                          Region,
                          RegionCount,
                          REGION_LOGICAL
                         )
              );
    }
    return(OK_STATUS);
}


// workers for GetDiskRegions

ARC_STATUS
GetRegions(
    IN  ULONG               Disk,
    IN  PPARTITION          p,
    IN  BOOLEAN             WantUsedRegions,
    IN  BOOLEAN             WantFreeRegions,
    IN  BOOLEAN             WantLogicalRegions,
    OUT PREGION_DESCRIPTOR *Region,
    OUT ULONG              *RegionCount,
    IN  REGION_TYPE         RegionType
    )
{
    ARC_STATUS   status;
    LARGE_INTEGER AlignedOffset,AlignedSize;
    ULONG         SizeMB;

    while(p) {

        if(p->SysID == SYSID_UNUSED) {

            if(WantFreeRegions) {

                LARGE_INTEGER   Result1, Result2;

                AlignedOffset = AlignTowardsDiskEnd(p->Disk,p->Offset);

                Result2.QuadPart = p->Offset.QuadPart + p->Length.QuadPart;
                Result1 = AlignTowardsDiskStart(p->Disk,Result2);
                AlignedSize.QuadPart   = Result1.QuadPart - AlignedOffset.QuadPart;

                SizeMB        = SIZEMB(AlignedSize);


                //
                // Show the space free if it is greater than 1 meg, AND
                // it is not a space starting at the beginning of the disk
                // and of length <= 1 cylinder.
                // This prevents the user from seeing the first cylinder
                // of the disk as free (could otherwise happen with an
                // extended partition starting on cylinder 1 and cylinders
                // of 1 megabyte or larger).
                //

                if(    (AlignedSize.QuadPart > 0)
                    && SizeMB
                    && (    (p->Offset.QuadPart != 0)
                         || ( p->Length.QuadPart >
                              DiskGeometryArray[p->Disk].BytesPerCylinder )
                       )
                  )
                {
                    if(!AddRegionEntry(Region,
                                       RegionCount,
                                       SizeMB,
                                       RegionType,
                                       p,
                                       AlignedOffset,
                                       AlignedSize
                                      ))
                    {
                        RETURN_OUT_OF_MEMORY;
                    }
                }
            }
        } else {

            if(WantUsedRegions) {

                AlignedOffset = p->Offset;
                AlignedSize   = p->Length;
                SizeMB        = SIZEMB(AlignedSize);

                if(!AddRegionEntry(Region,
                                   RegionCount,
                                   SizeMB,
                                   RegionType,
                                   p,
                                   AlignedOffset,
                                   AlignedSize
                                  ))
                {
                    RETURN_OUT_OF_MEMORY;
                }
            }

            if(IsExtended(p->SysID) && WantLogicalRegions) {
                status = GetRegions(Disk,
                                    LogicalVolumes[Disk],
                                    WantUsedRegions,
                                    WantFreeRegions,
                                    FALSE,
                                    Region,
                                    RegionCount,
                                    REGION_LOGICAL
                                   );
                if(status != OK_STATUS) {
                    return(status);
                }
            }
        }
        p = p->Next;
    }
    return(OK_STATUS);
}


BOOLEAN
AddRegionEntry(
    OUT PREGION_DESCRIPTOR *Regions,
    OUT ULONG              *RegionCount,
    IN  ULONG               SizeMB,
    IN  REGION_TYPE         RegionType,
    IN  PPARTITION          Partition,
    IN  LARGE_INTEGER       AlignedRegionOffset,
    IN  LARGE_INTEGER       AlignedRegionSize
    )
{
    PREGION_DESCRIPTOR p;
    PREGION_DATA       data;

    p = ReallocateMemory(*Regions,((*RegionCount) + 1) * sizeof(REGION_DESCRIPTOR));
    if(p == NULL) {
        return(FALSE);
    } else {
        *Regions = p;
        (*RegionCount)++;
    }

    p = &(*Regions)[(*RegionCount)-1];

    if(!(p->Reserved = AllocateMemory(sizeof(REGION_DATA)))) {
        return(FALSE);
    }

    p->Disk                    = Partition->Disk;
    p->SysID                   = Partition->SysID;
    p->SizeMB                  = SizeMB;
    p->Active                  = Partition->Active;
    p->Recognized              = Partition->Recognized;
    p->PartitionNumber         = Partition->PartitionNumber;
    p->OriginalPartitionNumber = Partition->OriginalPartitionNumber;
    p->RegionType              = RegionType;
    p->PersistentData          = Partition->PersistentData;

    data = p->Reserved;

    data->Partition             = Partition;
    data->AlignedRegionOffset   = AlignedRegionOffset;
    data->AlignedRegionSize     = AlignedRegionSize;

    return(TRUE);
}


VOID
FreeRegionArray(
    IN PREGION_DESCRIPTOR Region,
    IN ULONG              RegionCount
    )

/*++

Routine Description:

    This routine frees a region array returned by GetDiskRegions().

Arguments:

    Region          - pointer to the array of regions to be freed

    RegionCount     - number of items in the Region array

Return Value:

    None.

--*/

{
    ULONG i;

    for(i=0; i<RegionCount; i++) {

        if(Region[i].Reserved) {
            FreeMemory(Region[i].Reserved);
        }
    }
    FreeMemory(Region);
}



VOID
AddPartitionToLinkedList(
    IN OUT PARTITION **Head,
    IN     PARTITION *p
    )

/*++

Routine Description:

    This routine adds a PARTITION structure to a doubly-linked
    list, sorted by the Offset field in ascending order.

Arguments:

    Head    - pointer to pointer to first element in list

    p       - pointer to item to be added to list

Return Value:

    None.

--*/

{
    PARTITION *cur,*prev;

    if((cur = *Head) == NULL) {
        *Head = p;
        return;
    }

    if(p->Offset.QuadPart < cur->Offset.QuadPart) {
        p->Next = cur;
        cur->Prev = p;
        *Head = p;
        return;
    }

    prev = *Head;
    cur = cur->Next;

    while(cur) {
        if(p->Offset.QuadPart < cur->Offset.QuadPart) {

            p->Next = cur;
            p->Prev = prev;
            prev->Next = p;
            cur->Prev = p;
            return;
        }
        prev = cur;
        cur = cur->Next;
    }

    prev->Next = p;
    p->Prev = prev;
    return;
}


BOOLEAN
IsInLinkedList(
    IN PPARTITION p,
    IN PPARTITION List
    )

/*++

Routine Description:

    This routine determines whether a PARTITION element is in
    a given linked list of PARTITION elements.

Arguments:

    p       - pointer to element to be checked for

    List    - first element in list to be scanned

Return Value:

    true if p found in List, false otherwise

--*/

{
    while(List) {
        if(p == List) {
            return(TRUE);
        }
        List = List->Next;
    }
    return(FALSE);
}


BOOLEAN
IsInLogicalList(
    IN ULONG      Disk,
    IN PPARTITION p
    )

/*++

Routine Description:

    This routine determines whether a PARTITION element is in
    the logical volume list for a given disk.

Arguments:

    Disk    - index of disk to be checked

    p       - pointer to element to be checked for

Return Value:

    true if p found in Disk's logical volume list, false otherwise

--*/

{
    return(IsInLinkedList(p,LogicalVolumes[Disk]));
}


BOOLEAN
IsInPartitionList(
    IN ULONG      Disk,
    IN PPARTITION p
    )

/*++

Routine Description:

    This routine determines whether a PARTITION element is in
    the primary partition list for a given disk.

Arguments:

    Disk    - index of disk to be checked

    p       - pointer to element to be checked for

Return Value:

    true if p found in Disk's primary partition list, false otherwise

--*/

{
    return(IsInLinkedList(p,PrimaryPartitions[Disk]));
}


VOID
MergeFreePartitions(
    IN PPARTITION p
    )

/*++

Routine Description:

    This routine merges adjacent free space elements in the
    given linked list of PARTITION elements.  It is designed
    to be called after adding or deleting a partition.

Arguments:

    p - pointer to first item in list whose free elements are to
        be merged.

Return Value:

    None.

--*/

{
    PPARTITION next;

    while(p && p->Next) {

        if((p->SysID == SYSID_UNUSED) && (p->Next->SysID == SYSID_UNUSED)) {

            next = p->Next;

            p->Length.QuadPart = (next->Offset.QuadPart + next->Length.QuadPart) - p->Offset.QuadPart;

            if(p->Next = next->Next) {
                next->Next->Prev = p;
            }

            FreeMemory(next);

        } else {
            p = p->Next;
        }
    }
}


VOID
RenumberPartitions(
    IN ULONG Disk
    )

/*++

Routine Description:

    This routine determines the partition number for each region
    on a disk.  For a used region, the partition number is the number
    that the system will assign to the partition.  All partitions
    (except the extended partition) are numbered first starting at 1,
    and then all logical volumes in the extended partition.
    For a free region, the partition number is the number that the
    system WOULD assign to the partition if the space were to be
    converted to a partition and all other regions on the disk were
    left as is.

    The partition numbers are stored in the PARTITION elements.

Arguments:

    Disk - index of disk whose partitions are to be renumbered.

Return Value:

    None.

--*/

{
    PPARTITION p = PrimaryPartitions[Disk];
    ULONG      n = 1;

    while(p) {
        if(!IsExtended(p->SysID)) {
            p->PartitionNumber = n;
            if(p->SysID != SYSID_UNUSED) {
                n++;
            }
        }
        p = p->Next;
    }
    p = LogicalVolumes[Disk];
    while(p) {
        p->PartitionNumber = n;
        if(p->SysID != SYSID_UNUSED) {
            n++;
        }
        p = p->Next;
    }
}


PPARTITION
FindPartitionElement(
    IN ULONG Disk,
    IN ULONG Partition
    )

/*++

Routine Description:

    This routine locates a PARTITION element for a disk/partition
    number pair.  The partition number is the number that the
    system assigns to the partition.

Arguments:

    Disk - index of relevent disk

    Partition - partition number of partition to find

Return Value:

    pointer to PARTITION element, or NULL if not found.

--*/

{
    PPARTITION p;

    ASRT(Partition);

    p = PrimaryPartitions[Disk];
    while(p) {
        if((p->SysID != SYSID_UNUSED)
        && !IsExtended(p->SysID)
        && (p->PartitionNumber == Partition))
        {
            return(p);
        }
        p = p->Next;
    }
    p = LogicalVolumes[Disk];
    while(p) {
        if((p->SysID != SYSID_UNUSED)
        && (p->PartitionNumber == Partition))
        {
            return(p);
        }
        p = p->Next;
    }
    return(NULL);
}


VOID
SetSysID(
    IN ULONG Disk,
    IN ULONG Partition,
    IN UCHAR SysID
    )

/*++

Routine Description:

    This routine sets the system id of the given partition
    on the given disk.

Arguments:

    Disk - index of relevent disk

    Partition - partition number of relevent partition

    SysID - new system ID for Partition on Disk

Return Value:

    None.

--*/

{
    PPARTITION p = FindPartitionElement(Disk,Partition);

    ASRT(p);

    if(p) {
        p->SysID = SysID;
        if(!p->Update) {
            p->Update = CHANGED_DONT_ZAP;
        }
        ChangesMade[p->Disk] = TRUE;
    }
}


VOID
SetSysID2(
    IN PREGION_DESCRIPTOR Region,
    IN UCHAR              SysID
    )
{
    PPARTITION p = ((PREGION_DATA)(Region->Reserved))->Partition;

    p->SysID = SysID;
    if(!p->Update) {
        p->Update = CHANGED_DONT_ZAP;
    }
    ChangesMade[p->Disk] = TRUE;
}


ULONG
GetHiddenSectorCount(
    IN ULONG Disk,
    IN ULONG Partition
    )

/*++

Routine Description:

    This routine determines the hidden sector count for a
    partition.  This value is used by format and is placed in
    the BPB of a FAT partition when the partition is formatted.

Arguments:

    Disk - index of relevent disk

    Partition - partition number of relevent partition

Return Value:

    The number of hidden sectors (will be 0 if the partition
    does not exist).

--*/

{
    PPARTITION p = FindPartitionElement(Disk,Partition);
    ULONG      HiddenSectorCount = 0;
    LARGE_INTEGER   Result;

    ASRT(p);

    if(p) {

        if(IsInLogicalList(Disk,p)) {
            HiddenSectorCount = DiskGeometryArray[Disk].SectorsPerTrack;
        } else {
            ASRT(IsInPartitionList(Disk,p));
            Result.QuadPart = p->Offset.QuadPart / DiskGeometryArray[Disk].BytesPerSector;
            HiddenSectorCount = LOWPART(Result);
        }
    }
    return(HiddenSectorCount);
}


VOID
FreeLinkedPartitionList(
    IN OUT PPARTITION *q
    )

/*++

Routine Description:

    This routine frees a linked list of PARTITION elements. The head
    pointer is set to NULL.

Arguments:

    p - pointer to pointer to first element of list to free.

Return Value:

    None.

--*/

{
    PARTITION *n;
    PARTITION *p = *q;

    while(p) {
        n = p->Next;
        FreeMemory(p);
        p = n;
    }
    *q = NULL;
}


VOID
FreePartitionInfoLinkedLists(
    IN PPARTITION *ListHeadArray
    )

/*++

Routine Description:

    This routine frees the linked lists of PARTITION elements
    for each disk.

Arguments:

    ListHeadArray - pointer to array of pointers to first elements of
                    PARTITION element lists.

Return Value:

    None.

--*/

{
    ULONG i;

    for(i=0; i<CountOfDisks; i++) {

        FreeLinkedPartitionList(&ListHeadArray[i]);
    }
}


PPARTITION
AllocatePartitionStructure(
    IN ULONG         Disk,
    IN LARGE_INTEGER Offset,
    IN LARGE_INTEGER Length,
    IN UCHAR         SysID,
    IN BOOLEAN       Update,
    IN BOOLEAN       Active,
    IN BOOLEAN       Recognized
    )

/*++

Routine Description:

    This routine allocates space for, and initializes a PARTITION
    structure.

Arguments:

    Disk    - index of disk, one of whose regions the new PARTITION
              strucure describes.

    Offset  - byte offset of region on the disk

    Length  - length in bytes of the region

    SysID   - system id of region, of SYSID_UNUSED of this PARTITION
              is actually a free space.

    Update  - whether this PARTITION is dirty, ie, has changed and needs
              to be written to disk.

    Active  - flag for the BootIndicator field in a partition table entry,
              indicates to the x86 master boot program which partition
              is active.

    Recognized - whether the partition is a type recognized by NT

Return Value:

    NULL if allocation failed, or new initialized PARTITION strucure.

--*/

{
    PPARTITION p = AllocateMemory(sizeof(PARTITION));

    if(p) {
        p->Next                    = NULL;
        p->Prev                    = NULL;
        p->Offset                  = Offset;
        p->Length                  = Length;
        p->Disk                    = Disk;
        p->Update                  = Update;
        p->Active                  = Active;
        p->Recognized              = Recognized;
        p->SysID                   = SysID;
        p->OriginalPartitionNumber = 0;
        p->PartitionNumber         = 0;
        p->PersistentData          = 0;
    }
    return(p);
}


ARC_STATUS
InitializeFreeSpace(
    IN ULONG             Disk,
    IN PPARTITION       *PartitionList,      // list the free space goes in
    IN LARGE_INTEGER     StartOffset,
    IN LARGE_INTEGER     Length
    )

/*++

Routine Description:

    This routine determines all the free spaces within a given area
    on a disk, allocates PARTITION structures to describe them,
    and adds these structures to the relevent partition list
    (primary partitions or logical volumes).

    No rounding or alignment is performed here.  Spaces of even one
    byte will be counted and inserted in the partition list.

Arguments:

    Disk    - index of disk whose free spaces are being sought.

    PartitionList - pointer to first element on PARTITION list that
                    the free spaces will go in.

    StartOffset - start offset of area on disk to consider (ie, 0 for
                  primary spaces or the first byte of the extended
                  partition for logical spaces).

    Length - length of area on disk to consider (ie, size of disk
             for primary spaces or size of extended partition for
             logical spaces).

Return Value:

    OK_STATUS or error code.

--*/

{
    PPARTITION              p = *PartitionList,q;
    LARGE_INTEGER           Start,Size;

    Start = StartOffset;

    while(p) {

        Size.QuadPart = p->Offset.QuadPart - Start.QuadPart;

        if(Size.QuadPart > 0) {

            if(!(q = AllocatePartitionStructure(Disk,
                                                Start,
                                                Size,
                                                SYSID_UNUSED,
                                                FALSE,
                                                FALSE,
                                                FALSE
                                               )
                )
              )
            {
                RETURN_OUT_OF_MEMORY;
            }

            AddPartitionToLinkedList(PartitionList,q);
        }

        Start.QuadPart = p->Offset.QuadPart + p->Length.QuadPart;

        p = p->Next;

    }

    Size.QuadPart = (StartOffset.QuadPart + Length.QuadPart) - Start.QuadPart;

    if(Size.QuadPart > 0) {

        if(!(q = AllocatePartitionStructure(Disk,
                                            Start,
                                            Size,
                                            SYSID_UNUSED,
                                            FALSE,
                                            FALSE,
                                            FALSE
                                           )
            )
          )
        {
            RETURN_OUT_OF_MEMORY;
        }

        AddPartitionToLinkedList(PartitionList,q);
    }

    return(OK_STATUS);
}


ARC_STATUS
InitializeLogicalVolumeList(
    IN ULONG                      Disk,
    IN PDRIVE_LAYOUT_INFORMATION  DriveLayout,
    IN ULONG                      PartitionNumber
    )

/*++

Routine Description:

    This routine creates the logical volume linked list of
    PARTITION structures for the given disk.

Arguments:

    Disk    - index of disk

    DriveLayout - pointer to structure describing the raw partition
                  layout of the disk.

    PartitionNumber - number to assign to the first logical volume
                      on the disk.

Return Value:

    OK_STATUS or error code.

--*/

{
    PPARTITION             p,q;
    ULONG                   i,j;
    PPARTITION_INFORMATION d;
    LARGE_INTEGER          HiddenBytes;
    ULONG                  BytesPerSector = DiskGeometryArray[Disk].BytesPerSector;
    LARGE_INTEGER          Result1, Result2;

    FreeLinkedPartitionList(&LogicalVolumes[Disk]);

    p = PrimaryPartitions[Disk];
    while(p) {
        if(IsExtended(p->SysID)) {
            break;
        }
        p = p->Next;
    }

    if(p) {
        for(i=ENTRIES_PER_BOOTSECTOR; i<DriveLayout->PartitionCount; i+=ENTRIES_PER_BOOTSECTOR) {

            for(j=i; j<i+ENTRIES_PER_BOOTSECTOR; j++) {

                d = &DriveLayout->PartitionEntry[j];

                if((d->PartitionType != SYSID_UNUSED) && (d->PartitionType != SYSID_EXTENDED)) {

                    HiddenBytes.QuadPart = UInt32x32To64(d->HiddenSectors,BytesPerSector);

                    Result1.QuadPart = d->StartingOffset.QuadPart - HiddenBytes.QuadPart;
                    Result2.QuadPart = d->PartitionLength.QuadPart + HiddenBytes.QuadPart;
                    if(!(q = AllocatePartitionStructure(Disk,
                                                        Result1,
                                                        Result2,
                                                        d->PartitionType,
                                                        FALSE,
                                                        d->BootIndicator,
                                                        d->RecognizedPartition
                                                       )
                        )
                      )
                    {
                        RETURN_OUT_OF_MEMORY;
                    }

                    q->OriginalPartitionNumber = PartitionNumber++;
                    AddPartitionToLinkedList(&LogicalVolumes[Disk],q);

                    break;
                }
            }
        }
        return(InitializeFreeSpace(Disk,
                                   &LogicalVolumes[Disk],
                                   p->Offset,
                                   p->Length
                                  )
              );
    }
    return(OK_STATUS);
}


ARC_STATUS
InitializePrimaryPartitionList(
    IN  ULONG                     Disk,
    IN  PDRIVE_LAYOUT_INFORMATION DriveLayout,
    OUT PULONG                    NextPartitionNumber
    )

/*++

Routine Description:

    This routine creates the primary partition linked list of
    PARTITION structures for the given disk.

Arguments:

    Disk    - index of disk

    DriveLayout - pointer to structure describing the raw partition
                  layout of the disk.

    NextPartitionNumber - where to put partition number that should be
                          assigned to the first logical volume on the
                          disk.

Return Value:

    OK_STATUS or error code.

--*/

{
    ULONG                  i;
    PPARTITION             p;
    PPARTITION_INFORMATION d;
    ULONG                  PartitionNumber = 1;
    LARGE_INTEGER          LargeZero;

    FreeLinkedPartitionList(&PrimaryPartitions[Disk]);

    if(DriveLayout->PartitionCount >= ENTRIES_PER_BOOTSECTOR) {

        for(i=0; i<ENTRIES_PER_BOOTSECTOR; i++) {

            d = &DriveLayout->PartitionEntry[i];

            if(d->PartitionType != SYSID_UNUSED) {

                if(!(p = AllocatePartitionStructure(Disk,
                                                    d->StartingOffset,
                                                    d->PartitionLength,
                                                    d->PartitionType,
                                                    FALSE,
                                                    d->BootIndicator,
                                                    d->RecognizedPartition
                                                   )
                    )
                  )
                {
                    RETURN_OUT_OF_MEMORY;
                }

                p->OriginalPartitionNumber = IsExtended(p->SysID)
                                           ? 0
                                           : PartitionNumber++;

                AddPartitionToLinkedList(&PrimaryPartitions[Disk],p);
            }
        }
    }
    *NextPartitionNumber = PartitionNumber;
    LargeZero.QuadPart = 0;
    return(InitializeFreeSpace(Disk,
                               &PrimaryPartitions[Disk],
                               LargeZero,
                               DiskLengthBytes(Disk)
                              )
          );
}


ARC_STATUS
InitializePartitionLists(
    VOID
    )

/*++

Routine Description:

    This routine scans the PARTITION_INFO array returned for each disk
    by the OS.  A linked list of PARTITION structures is layered on top
    of each array;  the net result is a sorted list that covers an entire
    disk, because free spaces are also factored in as 'dummy' partitions.

Arguments:

    None.

Return Value:

    OK_STATUS or error code.

--*/

{
    ARC_STATUS               status;
    ULONG                     Disk;
    PDRIVE_LAYOUT_INFORMATION DriveLayout;
    ULONG                     PNum;


    for(Disk=0; Disk<CountOfDisks; Disk++) {

        if(OffLine[Disk]) {
            continue;
        }

        if((status = LowGetDiskLayout(DiskNames[Disk],&DriveLayout)) != OK_STATUS) {
            return(status);
        }

        if((status = InitializePrimaryPartitionList(Disk,DriveLayout,&PNum)) != OK_STATUS) {
            FreeMemory(DriveLayout);
            return(status);
        }
        if((status = InitializeLogicalVolumeList(Disk,DriveLayout,PNum)) != OK_STATUS) {
            FreeMemory(DriveLayout);
            return(status);
        }

        FreeMemory(DriveLayout);
        RenumberPartitions(Disk);
    }
    return(OK_STATUS);
}



LARGE_INTEGER
DiskLengthBytes(
    IN ULONG Disk
    )

/*++

Routine Description:

    This routine determines the disk length in bytes.  This value
    is calculated from the disk geometry information.

Arguments:

    Disk - index of disk whose size is desired

Return Value:

    Size of Disk.

--*/

{
    LARGE_INTEGER l;

    l.QuadPart = (ULONGLONG)DiskGeometryArray[Disk].Cylinders.QuadPart
               * (ULONGLONG)DiskGeometryArray[Disk].BytesPerCylinder;

    return(l);
}


ULONG
SIZEMB(
    IN LARGE_INTEGER ByteCount
    )

/*++

Routine Description:

    Calculate the size in megabytes of a given byte count. The value is
    properly rounded (ie, not merely truncated).

    This function replaces a macro of the same name that was truncating
    instead of rounding.

Arguments:

    ByteCount - supplies number of bytes

Return Value:

    Size in MB equivalent to ByteCount.

--*/

{
    ULONG Remainder;
    ULONG SizeMB;

    SizeMB = (ULONG)((ULONGLONG)ByteCount.QuadPart / (ULONGLONG)ONE_MEG);
    Remainder = (ULONG)((ULONGLONG)ByteCount.QuadPart % (ULONGLONG)ONE_MEG);

    if(Remainder >= ONE_MEG/2) {
        SizeMB++;
    }

    return(SizeMB);
}

ULONG
DiskSizeMB(
    IN ULONG Disk
    )

/*++

Routine Description:

    This routine determines the disk length in megabytes.  The returned
    value is rounded down after division by 1024*1024.

Arguments:

    Disk - index of disk whose size is desired

Return Value:

    Size of Disk.

--*/

{
    return(SIZEMB(DiskLengthBytes(Disk)));
}


LARGE_INTEGER
AlignTowardsDiskStart(
    IN ULONG         Disk,
    IN LARGE_INTEGER Offset
    )

/*++

Routine Description:

    This routine snaps a byte offset to a cylinder boundary, towards
    the start of the disk.

Arguments:

    Disk - index of disk whose offset is to be snapped

    Offset - byte offset to be aligned (snapped to cylinder boundary)

Return Value:

    Aligned offset.

--*/

{
    LARGE_INTEGER mod;
    LARGE_INTEGER Result;

    mod.QuadPart = Offset.QuadPart % DiskGeometryArray[Disk].BytesPerCylinder;
    Result.QuadPart = Offset.QuadPart - mod.QuadPart;
    return(Result);
}


LARGE_INTEGER
AlignTowardsDiskEnd(
    IN ULONG         Disk,
    IN LARGE_INTEGER Offset
    )

/*++

Routine Description:

    This routine snaps a byte offset to a cylinder boundary, towards
    the end of the disk.

Arguments:

    Disk - index of disk whose offset is to be snapped

    Offset - byte offset to be aligned (snapped to cylinder boundary)

Return Value:

    Aligned offset.

--*/

{
    LARGE_INTEGER mod;
    LARGE_INTEGER LargeInteger;

    mod.QuadPart = Offset.QuadPart % DiskGeometryArray[Disk].BytesPerCylinder;
    if(mod.QuadPart != 0) {

        LargeInteger.QuadPart = Offset.QuadPart + DiskGeometryArray[Disk].BytesPerCylinder;
        Offset = AlignTowardsDiskStart(Disk,
                                       LargeInteger
                                      );
    }
    return(Offset);
}


BOOLEAN
IsExtended(
    IN UCHAR SysID
    )

/*++

Routine Description:

    This routine determines whether a given system id is for an
    extended type (ie, link) entry.

Arguments:

    SysID - system id to be tested.

Return Value:

    true/false based on whether SysID is for an extended type.

--*/

{
    return((BOOLEAN)(SysID == SYSID_EXTENDED));
}


ARC_STATUS
IsAnyCreationAllowed(
    IN  ULONG    Disk,
    IN  BOOLEAN  AllowMultiplePrimaries,
    OUT PBOOLEAN AnyAllowed,
    OUT PBOOLEAN PrimaryAllowed,
    OUT PBOOLEAN ExtendedAllowed,
    OUT PBOOLEAN LogicalAllowed
    )

/*++

Routine Description:

    This routine determines whether any partition may be created on a
    given disk, based on three sub-queries -- whether creation is allowed
    of a primary partition, an extended partition, or a logical volume.

Arguments:

    Disk            - index of disk to check

    AllowMultiplePrimaries - whether to allow multiple primary partitions

    AnyAllowed - returns whether any creation is allowed

    PrimaryAllowed - returns whether creation of a primary partition
                     is allowed

    ExtendedAllowed - returns whether creation of an extended partition
                      is allowed

    Logical Allowed - returns whether creation of a logical volume is allowed.

Return Value:

    OK_STATUS or error code

--*/

{
    ARC_STATUS status;

    if((status = IsCreationOfPrimaryAllowed(Disk,AllowMultiplePrimaries,PrimaryAllowed)) != OK_STATUS) {
        return(status);
    }
    if((status = IsCreationOfExtendedAllowed(Disk,ExtendedAllowed)) != OK_STATUS) {
        return(status);
    }
    if((status = IsCreationOfLogicalAllowed(Disk,LogicalAllowed)) != OK_STATUS) {
        return(status);
    }
    *AnyAllowed = (BOOLEAN)(*PrimaryAllowed || *ExtendedAllowed || *LogicalAllowed);
    return(OK_STATUS);
}


ARC_STATUS
IsCreationOfPrimaryAllowed(
    IN  ULONG    Disk,
    IN  BOOLEAN  AllowMultiplePrimaries,
    OUT BOOLEAN *Allowed
    )

/*++

Routine Description:

    This routine determines whether creation of a primary partition is
    allowed.  This is true when there is a free entry in the MBR and
    there is free primary space on the disk.  If multiple primaries
    are not allowed, then there must also not exist any primary partitions
    in order for a primary creation to be allowed.

Arguments:

    Disk            - index of disk to check

    AllowMultiplePrimaries - whether existnace of primary partition
                             disallows creation of a primary partition

    Allowed - returns whether creation of a primary partition
              is allowed

Return Value:

    OK_STATUS or error code

--*/

{
    PREGION_DESCRIPTOR Regions;
    ULONG              RegionCount;
    ULONG              UsedCount,RecogCount,i;
    ARC_STATUS        status;
    BOOLEAN            FreeSpace = FALSE;

    status = GetPrimaryDiskRegions(Disk,&Regions,&RegionCount);
    if(status != OK_STATUS) {
        return(status);
    }

    for(UsedCount = RecogCount = i = 0; i<RegionCount; i++) {
        ASRT(Regions[i].RegionType != REGION_LOGICAL);
        if(Regions[i].SysID == SYSID_UNUSED) {
            FreeSpace = TRUE;
        } else {
            UsedCount++;
            if(!IsExtended(Regions[i].SysID) && Regions[i].Recognized) {
                RecogCount++;
            }
        }
    }
    ASRT(UsedCount <= ENTRIES_PER_BOOTSECTOR);
    ASRT(RecogCount <= ENTRIES_PER_BOOTSECTOR);
    ASRT(RecogCount <= UsedCount);

    if((UsedCount < ENTRIES_PER_BOOTSECTOR)
    && FreeSpace
    && (!RecogCount || AllowMultiplePrimaries))
    {
        *Allowed = TRUE;
    } else {
        *Allowed = FALSE;
    }

    FreeRegionArray(Regions,RegionCount);
    return(OK_STATUS);
}


ARC_STATUS
IsCreationOfExtendedAllowed(
    IN  ULONG    Disk,
    OUT BOOLEAN *Allowed
    )

/*++

Routine Description:

    This routine determines whether creation of an extended partition is
    allowed.  This is true when there is a free entry in the MBR,
    there is free primary space on the disk, and there is no existing
    extended partition.

Arguments:

    Disk            - index of disk to check

    Allowed - returns whether creation of an extended partition
              is allowed

Return Value:

    OK_STATUS or error code

--*/

{
    PREGION_DESCRIPTOR Regions;
    ULONG              RegionCount;
    ULONG              UsedCount,FreeCount,i;
    ARC_STATUS        status;

    status = GetPrimaryDiskRegions(Disk,&Regions,&RegionCount);
    if(status != OK_STATUS) {
        return(status);
    }

    for(UsedCount = FreeCount = i = 0; i<RegionCount; i++) {
        ASRT(Regions[i].RegionType != REGION_LOGICAL);
        if(Regions[i].SysID == SYSID_UNUSED) {
            // BUGBUG should adjust the size here and see if it's non0 first
            // (ie, take into account that the extended partition can't
            // start on cyl 0).
            FreeCount++;
        } else {
            UsedCount++;
            if(IsExtended(Regions[i].SysID)) {
                FreeRegionArray(Regions,RegionCount);
                *Allowed = FALSE;
                return(OK_STATUS);
            }
        }
    }
    *Allowed = (BOOLEAN)((UsedCount < ENTRIES_PER_BOOTSECTOR) && FreeCount);
    FreeRegionArray(Regions,RegionCount);
    return(OK_STATUS);
}


ARC_STATUS
IsCreationOfLogicalAllowed(
    IN  ULONG    Disk,
    OUT BOOLEAN *Allowed
    )

/*++

Routine Description:

    This routine determines whether creation of a logical volume is
    allowed.  This is true when there is an extended partition and
    free space within it.

Arguments:

    Disk            - index of disk to check

    Allowed - returns whether creation of a logical volume is allowed

Return Value:

    OK_STATUS or error code

--*/

{
    PREGION_DESCRIPTOR Regions;
    ULONG              RegionCount;
    ULONG              i;
    ARC_STATUS        status;
    BOOLEAN            ExtendedExists;

    *Allowed = FALSE;

    status = DoesExtendedExist(Disk,&ExtendedExists);
    if(status != OK_STATUS) {
        return(status);
    }
    if(!ExtendedExists) {
        return(OK_STATUS);
    }

    status = GetLogicalDiskRegions(Disk,&Regions,&RegionCount);
    if(status != OK_STATUS) {
        return(status);
    }

    for(i = 0; i<RegionCount; i++) {
        ASRT(Regions[i].RegionType == REGION_LOGICAL);
        if(Regions[i].SysID == SYSID_UNUSED) {
            *Allowed = TRUE;
            break;
        }
    }
    FreeRegionArray(Regions,RegionCount);
    return(OK_STATUS);
}



ARC_STATUS
DoesAnyPartitionExist(
    IN  ULONG    Disk,
    OUT PBOOLEAN AnyExists,
    OUT PBOOLEAN PrimaryExists,
    OUT PBOOLEAN ExtendedExists,
    OUT PBOOLEAN LogicalExists
    )

/*++

Routine Description:

    This routine determines whether any partition exists on a given disk.
    This is based on three sub queries: whether there are any primary or
    extended partitions, or logical volumes on the disk.

Arguments:

    Disk            - index of disk to check

    AnyExists - returns whether any partitions exist on Disk

    PrimaryExists - returns whether any primary partitions exist on Disk

    ExtendedExists - returns whether there is an extended partition on Disk

    LogicalExists - returns whether any logical volumes exist on Disk

Return Value:

    OK_STATUS or error code

--*/

{
    ARC_STATUS status;

    if((status = DoesAnyPrimaryExist(Disk,PrimaryExists )) != OK_STATUS) {
        return(status);
    }
    if((status = DoesExtendedExist  (Disk,ExtendedExists)) != OK_STATUS) {
        return(status);
    }
    if((status = DoesAnyLogicalExist(Disk,LogicalExists )) != OK_STATUS) {
        return(status);
    }
    *AnyExists = (BOOLEAN)(*PrimaryExists || *ExtendedExists || *LogicalExists);
    return(OK_STATUS);
}


ARC_STATUS
DoesAnyPrimaryExist(
    IN  ULONG    Disk,
    OUT BOOLEAN *Exists
    )

/*++

Routine Description:

    This routine determines whether any non-extended primary partition exists
    on a given disk.

Arguments:

    Disk   - index of disk to check

    Exists - returns whether any primary partitions exist on Disk

Return Value:

    OK_STATUS or error code

--*/

{
    PREGION_DESCRIPTOR Regions;
    ULONG               RegionCount,i;
    ARC_STATUS        status;

    status = GetUsedPrimaryDiskRegions(Disk,&Regions,&RegionCount);
    if(status != OK_STATUS) {
        return(status);
    }

    *Exists = FALSE;

    for(i=0; i<RegionCount; i++) {
        ASRT(Regions[i].RegionType != REGION_LOGICAL);
        ASRT(Regions[i].SysID != SYSID_UNUSED);
        if(!IsExtended(Regions[i].SysID)) {
            *Exists = TRUE;
            break;
        }
    }
    FreeRegionArray(Regions,RegionCount);
    return(OK_STATUS);
}


ARC_STATUS
DoesExtendedExist(
    IN  ULONG    Disk,
    OUT BOOLEAN *Exists
    )

/*++

Routine Description:

    This routine determines whether an extended partition exists
    on a given disk.

Arguments:

    Disk   - index of disk to check

    Exists - returns whether an extended partition exists on Disk

Return Value:

    OK_STATUS or error code

--*/

{
    PREGION_DESCRIPTOR Regions;
    ULONG               RegionCount,i;
    ARC_STATUS        status;

    status = GetUsedPrimaryDiskRegions(Disk,&Regions,&RegionCount);
    if(status != OK_STATUS) {
        return(status);
    }

    *Exists = FALSE;

    for(i=0; i<RegionCount; i++) {
        ASRT(Regions[i].RegionType != REGION_LOGICAL);
        ASRT(Regions[i].SysID != SYSID_UNUSED);
        if(IsExtended(Regions[i].SysID)) {
            *Exists = TRUE;
            break;
        }
    }
    FreeRegionArray(Regions,RegionCount);
    return(OK_STATUS);
}


ARC_STATUS
DoesAnyLogicalExist(
    IN  ULONG    Disk,
    OUT BOOLEAN *Exists
    )

/*++

Routine Description:

    This routine determines whether any logical volumes exist
    on a given disk.

Arguments:

    Disk   - index of disk to check

    Exists - returns whether any logical volumes exist on Disk

Return Value:

    OK_STATUS or error code

--*/

{
    PREGION_DESCRIPTOR Regions;
    ULONG               RegionCount;
    ARC_STATUS        status;

    status = GetUsedLogicalDiskRegions(Disk,&Regions,&RegionCount);
    if(status != OK_STATUS) {
        return(status);
    }

    *Exists = (BOOLEAN)(RegionCount != 0);
    FreeRegionArray(Regions,RegionCount);
    return(OK_STATUS);
}


ULONG
GetDiskCount(
    VOID
    )

/*++

Routine Description:

    This routine returns the number of attached partitionable disk
    devices.  The returned value is one greater than the maximum index
    allowed for Disk parameters to partitioning engine routines.

Arguments:

    None.

Return Value:

    Count of disks.

--*/

{
    return(CountOfDisks);
}


PCHAR
GetDiskName(
    ULONG Disk
    )

/*++

Routine Description:

    This routine returns the system name for the disk device whose
    index is given.

Arguments:

    Disk - index of disk whose name is desired.

Return Value:

    System name for the disk device.  The caller must not attempt to
    free this buffer or modify it.

--*/

{
    return(DiskNames[Disk]);
}


// sys ID type names

char    UNKNOWN[] = "Unknown type";

PCHAR   SysIDStrings[256] = { "Free Space","FAT","XENIX1","XENIX2", // 00-03
                              "FAT", "Extended", "FAT", "HPFS/NTFS",// 04-07
                              UNKNOWN, UNKNOWN,                     // 08-09
                              "OS/2 Boot Manager", UNKNOWN,         // 0a-0b
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 0c-0f
                              UNKNOWN, UNKNOWN,                     // 10-11
                              "EISA Utilities", UNKNOWN,            // 12-13
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 14-17
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 18-1b
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 1c-1f
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 20-23
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 24-27
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 28-2b
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 2c-2f
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 30-33
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 34-37
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 38-3b
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 3c-3f
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 40-43
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 44-47
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 48-4b
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 4c-4f
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 50-53
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 54-57
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 58-5b
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 5c-5f
                              UNKNOWN, UNKNOWN, UNKNOWN, "Unix",    // 60-63
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 64-67
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 68-6b
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 6c-6f
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 70-73
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 74-77
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 78-7b
                              UNKNOWN, UNKNOWN, UNKNOWN, "Unused",  // 7c-7f
                              UNKNOWN, "Windows NT Fault Tolerance",// 80-81
                              UNKNOWN, UNKNOWN,                     // 82-83
                              "Windows NT Fault Tolerance", UNKNOWN,// 84-85
                              "Windows NT Fault Tolerance",         // 86-86
                              "Windows NT Fault Tolerance",         // 87-87
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 88-8b
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 8c-8f
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 90-93
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 94-97
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 98-9b
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // 9c-9f
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // a0-a3
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // a4-a7
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // a8-ab
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // ac-af
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // b0-b3
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // b4-b7
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // b8-bb
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // bc-bf
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // c0-c3
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // c4-c7
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // c8-cb
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // cc-cf
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // d0-d3
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // d4-d7
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // d8-db
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // dc-df
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // e0-e3
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // e4-e7
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // e8-eb
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // ec-ef
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // f0-f3
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // f4-f7
                              UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,   // f8-fb
                              UNKNOWN, UNKNOWN, UNKNOWN, "Table",   // fc-ff
                            };


PCHAR
GetSysIDName(
    UCHAR SysID
    )

/*++

Routine Description:

    This routine returns the name for a given system ID.

Arguments:

    SysID - system id in question

Return Value:

    Name of system id.  The caller must not attempt to free or
    modify this buffer.

--*/

{
    return(SysIDStrings[SysID]);
}


// worker routines for WriteDriveLayout

VOID
UnusedEntryFill(
    IN PPARTITION_INFORMATION pinfo,
    IN ULONG                  EntryCount
    )
{
    ULONG         i;
    LARGE_INTEGER Zero;

    Zero.QuadPart = 0;

    for(i=0; i<EntryCount; i++) {

        pinfo[i].StartingOffset   = Zero;
        pinfo[i].PartitionLength  = Zero;
        pinfo[i].HiddenSectors    = 0;
        pinfo[i].PartitionType    = SYSID_UNUSED;
        pinfo[i].BootIndicator    = FALSE;
        pinfo[i].RewritePartition = TRUE;
    }
}


LARGE_INTEGER
MakeBootRec(
    ULONG                  Disk,
    PPARTITION_INFORMATION pinfo,
    PPARTITION             pLogical,
    PPARTITION             pNextLogical
    )
{
    ULONG         Entry = 0;
    LARGE_INTEGER BytesPerTrack;
    LARGE_INTEGER SectorsPerTrack;
    LARGE_INTEGER StartingOffset;

    BytesPerTrack.QuadPart = (DiskGeometryArray[Disk].BytesPerTrack);
    SectorsPerTrack.QuadPart = (DiskGeometryArray[Disk].SectorsPerTrack);
    StartingOffset.QuadPart = 0;

//  ASRT(pLogical || pNextLogical);

    if(pLogical) {

        pinfo[Entry].StartingOffset.QuadPart   = pLogical->Offset.QuadPart + BytesPerTrack.QuadPart;
        pinfo[Entry].PartitionLength.QuadPart  = pLogical->Length.QuadPart - BytesPerTrack.QuadPart;
        pinfo[Entry].HiddenSectors    = SectorsPerTrack.LowPart;
        pinfo[Entry].RewritePartition = pLogical->Update;
        pinfo[Entry].BootIndicator    = pLogical->Active;
        pinfo[Entry].PartitionType    = pLogical->SysID;

        if(pinfo[Entry].RewritePartition) {
            StartingOffset = pinfo[Entry].StartingOffset;
        }

        Entry++;
    }

    if(pNextLogical) {

        pinfo[Entry].StartingOffset   = pNextLogical->Offset;
        pinfo[Entry].PartitionLength  = pNextLogical->Length;
        pinfo[Entry].HiddenSectors    = 0;
        pinfo[Entry].RewritePartition = TRUE;
        pinfo[Entry].BootIndicator    = FALSE;
        pinfo[Entry].PartitionType    = SYSID_EXTENDED;

        Entry++;
    }

    UnusedEntryFill(pinfo+Entry,ENTRIES_PER_BOOTSECTOR-Entry);
    return(StartingOffset);
}


ARC_STATUS
ZapSector(
    ULONG         Disk,
    LARGE_INTEGER Offset
    )

/*++

Routine Description:

    This routine writes zeros into a sector at a given offset.  This is
    used to clear out a new partition's filesystem boot record, so that
    no previous filesystem appears in a new partition; or to clear out the
    first EBR in the extended partition if there are to be no logical vols.

Arguments:

    Disk - disk to write to

    Offset - byte offset to a newly created partition on Disk

Return Value:

    OK_STATUS or error code.

--*/

{
    ULONG       SectorSize = DiskGeometryArray[Disk].BytesPerSector;
    ULONG       i;
    PCHAR       SectorBuffer,AlignedSectorBuffer;
    ARC_STATUS status;
    ULONG    Handle;
    LARGE_INTEGER   LargeInteger;

    if((SectorBuffer = AllocateMemory(2*SectorSize)) == NULL) {
        RETURN_OUT_OF_MEMORY;
    }

    AlignedSectorBuffer = (PCHAR)(((ULONG)SectorBuffer+SectorSize) & ~(SectorSize-1));

    for(i=0; i<SectorSize; AlignedSectorBuffer[i++] = 0);

    if((status = LowOpenDisk(GetDiskName(Disk),&Handle)) != OK_STATUS) {
        FreeMemory(SectorBuffer);
        return(status);
    }

    LargeInteger.QuadPart = Offset.QuadPart / SectorSize;
    status = LowWriteSectors(Handle,
                             SectorSize,
                             LOWPART(LargeInteger),
                             1,
                             AlignedSectorBuffer
                            );

    LowCloseDisk(Handle);
    FreeMemory(SectorBuffer);

    return(status);
}


ARC_STATUS
WriteDriveLayout(
    IN ULONG Disk
    )

/*++

Routine Description:

    This routine writes the current partition layout for a given disk
    out to disk.  The high-level PARTITION lists are transformed into
    a DRIVE_LAYOUT_INFORMATION structure before being passed down
    to the low-level partition table writing routine.

Arguments:

    Disk - index of disk whose on-disk partition structure is to be updated.

Return Value:

    OK_STATUS or error code.

--*/

{
    PDRIVE_LAYOUT_INFORMATION DriveLayout;
    PPARTITION_INFORMATION    pinfo;
    PPARTITION                n,p;
    ULONG                     EntryCount;
    ARC_STATUS               status;
    LARGE_INTEGER             StartingOffset;
    LARGE_INTEGER             ExtendedStartingOffset;

    ExtendedStartingOffset.QuadPart = 0;

    // allocate a huge buffer now to avoid complicated dynamic
    // reallocation schemes later.

    if(!(DriveLayout = AllocateMemory(250 * sizeof(PARTITION_INFORMATION)))) {
        RETURN_OUT_OF_MEMORY;
    }

    pinfo = &DriveLayout->PartitionEntry[0];

    // first do the mbr.

    EntryCount=0;
    p = PrimaryPartitions[Disk];

    while(p) {

        if(p->SysID != SYSID_UNUSED) {

            ASRT(EntryCount < ENTRIES_PER_BOOTSECTOR);

            if(IsExtended(p->SysID)) {
                ExtendedStartingOffset = p->Offset;
            }

            pinfo[EntryCount].StartingOffset   = p->Offset;
            pinfo[EntryCount].PartitionLength  = p->Length;
            pinfo[EntryCount].PartitionType    = p->SysID;
            pinfo[EntryCount].BootIndicator    = p->Active;
            pinfo[EntryCount].RewritePartition = p->Update;

            // BUGBUG we know that this field is not used by the
            //        set drive layout IOCTL or the ARC stub.

            pinfo[EntryCount].HiddenSectors    = 0;

            // if we're creating this partition, clear out the
            // filesystem boot sector.

            if(pinfo[EntryCount].RewritePartition
            && (p->Update != CHANGED_DONT_ZAP)
            && !IsExtended(pinfo[EntryCount].PartitionType))
            {
                status = ZapSector(Disk,pinfo[EntryCount].StartingOffset);
                if(status != OK_STATUS) {
                    FreeMemory(DriveLayout);
                    return(status);
                }
            }

            EntryCount++;
        }
        p = p->Next;
    }

    // fill the remainder of the MBR with unused entries.
    // NOTE that there will thus always be an MBR even if there
    // are no partitions defined.

    UnusedEntryFill(pinfo+EntryCount,ENTRIES_PER_BOOTSECTOR - EntryCount);
    EntryCount = ENTRIES_PER_BOOTSECTOR;

    //
    // now handle the logical volumes.
    // first check to see whether we need a dummy EBR at the beginning
    // of the extended partition.  This is the case when there is
    // free space at the beginning of the extended partition.
#if 0
    // Also handle the case where we are creating an empty extended
    // partition -- need to zap the first sector to eliminate any residue
    // that might start an EBR chain.
#else
    // BUGBUG 4/24/92 tedm:  Currently the io subsystem returns an error
    // status (status_bad_master_boot_record) if any mbr or ebr is bad.
    // Zeroing the first sector of the extended partition therefore causes
    // the whole disk to be seen as empty.  So create a blank, but valid,
    // EBR in the 'empty extended partition' case.  Code is in the 'else'
    // part of the #if 0, below.
#endif
    //

    if((p = LogicalVolumes[Disk]) && (p->SysID == SYSID_UNUSED)) {
        if(p->Next) {
            ASRT(p->Next->SysID != SYSID_UNUSED);

            MakeBootRec(Disk,pinfo+EntryCount,NULL,p->Next);
            EntryCount += ENTRIES_PER_BOOTSECTOR;
            p = p->Next;
        } else {
            ASRT(ExtendedStartingOffset.QuadPart != 0);
#if 0
            status = ZapSector(Disk,ExtendedStartingOffset);
            if(status != OK_STATUS) {
                FreeMemory(DriveLayout);
                return(status);
            }
#else
            MakeBootRec(Disk,pinfo+EntryCount,NULL,NULL);
            EntryCount += ENTRIES_PER_BOOTSECTOR;
#endif
        }
    }

    while(p) {
        if(p->SysID != SYSID_UNUSED) {

            // find the next logical volume.

            n = p->Next;
            while(n) {
                if(n->SysID != SYSID_UNUSED) {
                    break;
                }
                n = n->Next;
            }

            StartingOffset = MakeBootRec(Disk,pinfo+EntryCount,p,n);

            // if we're creating a volume, clear out its filesystem
            // boot sector so it starts out fresh.

            if((StartingOffset.QuadPart != 0) && (p->Update != CHANGED_DONT_ZAP)) {
                status = ZapSector(Disk,StartingOffset);
                if(status != OK_STATUS) {
                    FreeMemory(DriveLayout);
                    return(status);
                }
            }

            EntryCount += ENTRIES_PER_BOOTSECTOR;
        }
        p = p->Next;
    }

    DriveLayout->PartitionCount = EntryCount;
    status = LowSetDiskLayout(DiskNames[Disk],DriveLayout);

    FreeMemory(DriveLayout);

    return(status);
}


ARC_STATUS
CommitPartitionChanges(
    IN ULONG Disk
    )

/*++

Routine Description:

    This routine is the entry point for updating the on-disk partition
    structures of a disk.  The disk is only written to if the partition
    structure has been changed by adding or deleting partitions.

Arguments:

    Disk - index of disk whose on-disk partition structure is to be updated.

Return Value:

    OK_STATUS or error code.

--*/

{
    PPARTITION  p;
    ARC_STATUS status;

    ASRT(!OffLine[Disk]);

    if(!HavePartitionsBeenChanged(Disk)) {
        return(OK_STATUS);
    }

    if((status = WriteDriveLayout(Disk)) != OK_STATUS) {
        return(status);
    }

    // BUGBUG for ARC and NT MIPS, update NVRAM vars so partitions are right.
    //        Do that here, before partition numbers are reassigned.

    p = PrimaryPartitions[Disk];
    while(p) {
        p->Update = FALSE;
        p->OriginalPartitionNumber = p->PartitionNumber;
        p = p->Next;
    }
    p = LogicalVolumes[Disk];
    while(p) {
        p->Update = FALSE;
        p->OriginalPartitionNumber = p->PartitionNumber;
        p = p->Next;
    }

    ChangesMade[Disk] = FALSE;
    return(OK_STATUS);
}


BOOLEAN
HavePartitionsBeenChanged(
    IN ULONG Disk
    )

/*++

Routine Description:

    This routine returns TRUE if the given disk's partition structures
    have been modified by adding or deleting partitions, since the
    on-disk structures were last written by a call to CommitPartitionChanges
    (or first read).

Arguments:

    Disk - index of disk to check

Return Value:

    true if Disk's partition structure has changed.

--*/

{
    if(OffLine[Disk]) {
        ASRT(!ChangesMade[Disk]);
    }
    return(ChangesMade[Disk]);
}

VOID
FdMarkDiskDirty(
    IN ULONG Disk
    )
{
    ASRT(!OffLine[Disk]);

    ChangesMade[Disk] = TRUE;
}

VOID
FdSetPersistentData(
    IN PREGION_DESCRIPTOR Region,
    IN ULONG              Data
    )
{
    ((PREGION_DATA)(Region->Reserved))->Partition->PersistentData = Data;
}


ULONG
FdGetMinimumSizeMB(
    IN ULONG Disk
    )

/*++

Routine Description:

    Return the minimum size for a partition on a given disk.

    This is the rounded size of one cylinder or 1, whichever is greater.

Arguments:

    Region - region describing the partition to check.

Return Value:

    Actual offset

--*/

{
    LARGE_INTEGER   LargeInteger;

    LargeInteger.QuadPart = DiskGeometryArray[Disk].BytesPerCylinder;
    return(max(SIZEMB(LargeInteger),1));
}


ULONG
FdGetMaximumSizeMB(
    IN PREGION_DESCRIPTOR Region,
    IN REGION_TYPE        CreationType
    )
{
    PREGION_DATA CreateData = Region->Reserved;
    LARGE_INTEGER MaxSize = CreateData->AlignedRegionSize;

    ASRT(Region->SysID == SYSID_UNUSED);

    if(CreateData->AlignedRegionOffset.QuadPart == 0) {

        ULONG Delta;

        ASRT((CreationType == REGION_EXTENDED) || (CreationType == REGION_PRIMARY))

        Delta = (CreationType == REGION_EXTENDED)
              ? DiskGeometryArray[Region->Disk].BytesPerCylinder
              : DiskGeometryArray[Region->Disk].BytesPerTrack;

        MaxSize.QuadPart = MaxSize.QuadPart - Delta;
    }

    return(SIZEMB(MaxSize));
}


LARGE_INTEGER
FdGetExactSize(
    IN PREGION_DESCRIPTOR Region,
    IN BOOLEAN            ForExtended
    )
{
    PREGION_DATA  RegionData = Region->Reserved;
    LARGE_INTEGER LargeSize = RegionData->AlignedRegionSize;
    LARGE_INTEGER BytesPerTrack;
    LARGE_INTEGER BytesPerCylinder;

    BytesPerTrack.QuadPart = (DiskGeometryArray[Region->Disk].BytesPerTrack);
    BytesPerCylinder.QuadPart = (DiskGeometryArray[Region->Disk].BytesPerCylinder);

    if(Region->RegionType == REGION_LOGICAL) {

        //
        // The region is within the extended partition.  It doesn't matter
        // whether it's free space or used -- in either case, we need to
        // account for the reserved EBR track.
        //

        LargeSize.QuadPart = LargeSize.QuadPart - BytesPerTrack.QuadPart;

    } else if(Region->SysID == SYSID_UNUSED) {

        //
        // The region is unused space not inside the extended partition.
        // We must know whether the caller will put a primary or extended
        // partition there -- a primary partition can use all the space, but
        // a logical volume in the extended partition won't include the first
        // track.  If the free space starts at offset 0 on the disk, a special
        // calculation must be used to move the start of the partition to
        // skip a track for a primary or a cylinder and a track for an
        // extended+logical.
        //

        if((RegionData->AlignedRegionOffset.QuadPart == 0) || ForExtended) {
            LargeSize.QuadPart = LargeSize.QuadPart - BytesPerTrack.QuadPart;
        }

        if((RegionData->AlignedRegionOffset.QuadPart == 0) && ForExtended) {
            LargeSize.QuadPart = LargeSize.QuadPart - BytesPerCylinder.QuadPart;
        }
    }

    return(LargeSize);
}


LARGE_INTEGER
FdGetExactOffset(
    IN PREGION_DESCRIPTOR Region
    )

/*++

Routine Description:

    Determine where a given partition _actually_ starts, which may be
    different than where is appears because of EBR reserved tracks, etc.

    NOTE: This routine is not meant to operate on unused regions or
    extended partitions.  In these cases, it just returns the apparant offset.

Arguments:

    Region - region describing the partition to check.

Return Value:

    Actual offset

--*/

{
    LARGE_INTEGER Offset = ((PREGION_DATA)(Region->Reserved))->Partition->Offset;

    if((Region->SysID != SYSID_UNUSED) && (Region->RegionType == REGION_LOGICAL)) {

        //
        // The region is a logical volume.
        // Account for the reserved EBR track.
        //

        Offset.QuadPart = Offset.QuadPart +
                                 DiskGeometryArray[Region->Disk].BytesPerTrack;
    }

    return(Offset);
}


BOOLEAN
FdCrosses1024Cylinder(
    IN PREGION_DESCRIPTOR Region,
    IN ULONG              CreationSizeMB,
    IN REGION_TYPE        RegionType
    )

/*++

Routine Description:

    Determine whether a used region corsses the 1024th cylinder, or whether
    a partition created within a free space will cross the 1024th cylinder.

Arguments:

    Region - region describing the partition to check.

    CreationSizeMB - if the Region is for a free space, this is the size of
        the partition to be checked.

    RegionType - one of REGION_PRIMARY, REGION_EXTENDED, or REGION_LOGICAL

Return Value:

    TRUE if the end cylinder >= 1024.

--*/

{
    LARGE_INTEGER Start,Size,End, LargeZero;

    if(Region->SysID == SYSID_UNUSED) {

        //
        // Determine the exact size and offset of the partition, according
        // to how CreatePartitionEx() will do it.
        //

        LargeZero.QuadPart = 0;
        DetermineCreateSizeAndOffset( Region,
                                      LargeZero,
                                      CreationSizeMB,
                                      RegionType,
                                      &Start,
                                      &Size
                                    );

    } else {

        Start = ((PREGION_DATA)(Region->Reserved))->Partition->Offset;
        Size  = ((PREGION_DATA)(Region->Reserved))->Partition->Length;
    }

    End.QuadPart = (Start.QuadPart + Size.QuadPart) - 1;

    //
    // End is the last byte in the partition.  Divide by the number of
    // bytes in a cylinder and see whether the result is > 1023.
    //

    return( (End.QuadPart / DiskGeometryArray[Region->Disk].BytesPerCylinder) > 1023 );
}


BOOLEAN
IsDiskOffLine(
    IN ULONG Disk
    )
{
    return(OffLine[Disk]);
}