File: extent_tree.c

package info (click to toggle)
ocfs2-tools 1.8.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 6,232 kB
  • sloc: ansic: 86,865; sh: 5,781; python: 2,380; makefile: 1,305
file content (4215 lines) | stat: -rw-r--r-- 109,014 bytes parent folder | download | duplicates (3)
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
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
/* -*- mode: c; c-basic-offset: 8; -*-
 * vim: noexpandtab sw=8 ts=8 sts=0:
 *
 * extent_tree.c
 *
 * Copyright (C) 2009 Oracle.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <errno.h>
#include <assert.h>

#include "ocfs2/byteorder.h"
#include "ocfs2/ocfs2.h"
#include "extent_tree.h"

static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
					 uint64_t blkno)
{
	struct ocfs2_dinode *di = et->et_object;

	di->i_last_eb_blk = blkno;
}

static uint64_t ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
{
	struct ocfs2_dinode *di = et->et_object;

	return di->i_last_eb_blk;
}

static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
					 uint32_t clusters)
{
	struct ocfs2_dinode *di = et->et_object;

	di->i_clusters += clusters;
}
static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
{
	struct ocfs2_dinode *di = et->et_object;

	et->et_root_el = &di->id2.i_list;
}

static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
	.eo_set_last_eb_blk	= ocfs2_dinode_set_last_eb_blk,
	.eo_get_last_eb_blk	= ocfs2_dinode_get_last_eb_blk,
	.eo_update_clusters	= ocfs2_dinode_update_clusters,
	.eo_fill_root_el	= ocfs2_dinode_fill_root_el,
};

static void ocfs2_refcount_tree_fill_root_el(struct ocfs2_extent_tree *et)
{
	struct ocfs2_refcount_block *rb = et->et_object;

	et->et_root_el = &rb->rf_list;
}

static void ocfs2_refcount_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
						uint64_t blkno)
{
	struct ocfs2_refcount_block *rb = et->et_object;

	rb->rf_last_eb_blk = blkno;
}

static uint64_t
ocfs2_refcount_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
{
	struct ocfs2_refcount_block *rb = et->et_object;

	return rb->rf_last_eb_blk;
}

static void ocfs2_refcount_tree_update_clusters(struct ocfs2_extent_tree *et,
						uint32_t clusters)
{
	struct ocfs2_refcount_block *rb = et->et_object;

	rb->rf_clusters += clusters;
}

static enum ocfs2_contig_type
ocfs2_refcount_tree_extent_contig(ocfs2_filesys *fs,
				  struct ocfs2_extent_tree *et,
				  struct ocfs2_extent_rec *ext,
				  struct ocfs2_extent_rec *insert_rec)
{
	return CONTIG_NONE;
}

static struct ocfs2_extent_tree_operations ocfs2_refcount_tree_et_ops = {
	.eo_set_last_eb_blk	= ocfs2_refcount_tree_set_last_eb_blk,
	.eo_get_last_eb_blk	= ocfs2_refcount_tree_get_last_eb_blk,
	.eo_update_clusters	= ocfs2_refcount_tree_update_clusters,
	.eo_fill_root_el	= ocfs2_refcount_tree_fill_root_el,
	.eo_extent_contig	= ocfs2_refcount_tree_extent_contig,
};

static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
{
	struct ocfs2_xattr_value_root *xv = et->et_object;

	et->et_root_el = &xv->xr_list;
}

static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
					      uint64_t blkno)
{
	struct ocfs2_xattr_value_root *xv = et->et_object;

	xv->xr_last_eb_blk = blkno;
}

static uint64_t ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
{
	struct ocfs2_xattr_value_root *xv = et->et_object;

	return xv->xr_last_eb_blk;
}

static void ocfs2_xattr_value_update_clusters(struct ocfs2_extent_tree *et,
					      uint32_t clusters)
{
	struct ocfs2_xattr_value_root *xv = et->et_object;

	xv->xr_clusters += clusters;
}

static uint32_t ocfs2_xattr_value_get_clusters(struct ocfs2_extent_tree *et)
{
	struct ocfs2_xattr_value_root *xv = et->et_object;

	return xv->xr_clusters;
}

static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
	.eo_set_last_eb_blk	= ocfs2_xattr_value_set_last_eb_blk,
	.eo_get_last_eb_blk	= ocfs2_xattr_value_get_last_eb_blk,
	.eo_update_clusters	= ocfs2_xattr_value_update_clusters,
	.eo_get_clusters	= ocfs2_xattr_value_get_clusters,
	.eo_fill_root_el	= ocfs2_xattr_value_fill_root_el,
};

static void ocfs2_dx_root_set_last_eb_blk (struct ocfs2_extent_tree *et,
						uint64_t blkno)
{
	struct ocfs2_dx_root_block *dx_root = et->et_object;
	dx_root->dr_last_eb_blk = blkno;
}

static uint64_t ocfs2_dx_root_get_last_eb_blk (struct ocfs2_extent_tree *et)
{
	struct ocfs2_dx_root_block *dx_root = et->et_object;
	return dx_root->dr_last_eb_blk;
}

static void ocfs2_dx_root_update_clusters(struct ocfs2_extent_tree *et,
					uint32_t clusters)
{
	struct ocfs2_dx_root_block *dx_root = et->et_object;
	dx_root->dr_clusters += clusters;
}

static int ocfs2_dx_root_sanity_check(struct ocfs2_extent_tree *et)
{
	struct ocfs2_dx_root_block *dx_root = (struct ocfs2_dx_root_block *)et->et_object;
	assert(OCFS2_IS_VALID_DX_ROOT(dx_root));

	return 0;
}

static void ocfs2_dx_root_fill_root_el (struct ocfs2_extent_tree *et)
{
	struct ocfs2_dx_root_block *dx_root = et->et_object;

	et->et_root_el = &dx_root->dr_list;
}

static struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = {
	.eo_set_last_eb_blk	= ocfs2_dx_root_set_last_eb_blk,
	.eo_get_last_eb_blk	= ocfs2_dx_root_get_last_eb_blk,
	.eo_update_clusters	= ocfs2_dx_root_update_clusters,
	.eo_sanity_check	= ocfs2_dx_root_sanity_check,
	.eo_fill_root_el	= ocfs2_dx_root_fill_root_el,
};

static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
				     ocfs2_filesys *fs,
				     char *buf,
				     uint64_t blkno,
				     ocfs2_root_write_func write,
				     void *obj,
				     struct ocfs2_extent_tree_operations *ops)
{
	et->et_ops = ops;
	et->et_root_buf = buf;
	et->et_root_blkno = blkno;
	et->et_root_write = write;
	et->et_object = obj;

	et->et_ops->eo_fill_root_el(et);
	if (!et->et_ops->eo_fill_max_leaf_clusters)
		et->et_max_leaf_clusters = 0;
	else
		et->et_ops->eo_fill_max_leaf_clusters(fs, et);
}

void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
				   ocfs2_filesys *fs,
				   char *buf, uint64_t blkno)
{
	__ocfs2_init_extent_tree(et, fs, buf, blkno,
				 ocfs2_write_inode,
				 buf, &ocfs2_dinode_et_ops);
}

void ocfs2_init_refcount_extent_tree(struct ocfs2_extent_tree *et,
				     ocfs2_filesys *fs,
				     char *buf, uint64_t blkno)
{
	__ocfs2_init_extent_tree(et, fs, buf, blkno,
				 ocfs2_write_refcount_block,
				 buf, &ocfs2_refcount_tree_et_ops);
}

void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
					ocfs2_filesys *fs,
					char *buf, uint64_t blkno,
					ocfs2_root_write_func write,
					struct ocfs2_xattr_value_root *xv)
{
	__ocfs2_init_extent_tree(et, fs, buf, blkno, write,
				 xv, &ocfs2_xattr_value_et_ops);
}

void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et,
				    ocfs2_filesys *fs,
				    char *buf, uint64_t blkno)
{
	__ocfs2_init_extent_tree(et, fs, buf, blkno,
				ocfs2_write_dx_root,
				buf, &ocfs2_dx_root_et_ops);
}

static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
					    uint64_t new_last_eb_blk)
{
	et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
}

static inline uint64_t ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
{
	return et->et_ops->eo_get_last_eb_blk(et);
}

static inline void ocfs2_et_update_clusters(struct ocfs2_extent_tree *et,
					    uint32_t clusters)
{
	return et->et_ops->eo_update_clusters(et, clusters);
}

struct insert_ctxt {
	ocfs2_filesys *fs;
	struct ocfs2_extent_tree *et;
	struct ocfs2_extent_rec rec;
};
/*
 * Reset the actual path elements so that we can re-use the structure
 * to build another path. Generally, this involves freeing the buffer
 * heads.
 */
static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
{
	int i, start = 0, depth = 0;
	struct ocfs2_path_item *node;

	if (keep_root)
		start = 1;

	for(i = start; i < path_num_items(path); i++) {
		node = &path->p_node[i];
		if (!node->buf)
			continue;

		ocfs2_free(&node->buf);
		node->blkno = 0;
		node->buf = NULL;
		node->el = NULL;
	}

	/*
	 * Tree depth may change during truncate, or insert. If we're
	 * keeping the root extent list, then make sure that our path
	 * structure reflects the proper depth.
	 */
	if (keep_root)
		depth = path_root_el(path)->l_tree_depth;

	path->p_tree_depth = depth;
}

void ocfs2_free_path(struct ocfs2_path *path)
{
	/* We don't free the root because often in libocfs2 the root is a
	 * shared buffer such as the inode.  Caller must be responsible for
	 * handling the root of the path.
	 */
	if (path) {
		ocfs2_reinit_path(path, 1);
		ocfs2_free(&path);
	}
}

static enum ocfs2_contig_type
	ocfs2_extent_rec_contig(ocfs2_filesys *fs,
				struct ocfs2_extent_rec *ext,
				struct ocfs2_extent_rec *insert_rec);
static inline enum ocfs2_contig_type
	ocfs2_et_extent_contig(ocfs2_filesys *fs,
			       struct ocfs2_extent_tree *et,
			       struct ocfs2_extent_rec *rec,
			       struct ocfs2_extent_rec *insert_rec)
{
	if (et->et_ops->eo_extent_contig)
		return et->et_ops->eo_extent_contig(fs, et, rec, insert_rec);

	return ocfs2_extent_rec_contig(fs, rec, insert_rec);
}

/*
 * All the elements of src into dest. After this call, src could be freed
 * without affecting dest.
 *
 * Both paths should have the same root. Any non-root elements of dest
 * will be freed.
 */
static void ocfs2_cp_path(ocfs2_filesys *fs,
			  struct ocfs2_path *dest,
			  struct ocfs2_path *src)
{
	int i;
	struct ocfs2_extent_block *eb = NULL;

	assert(path_root_blkno(dest) == path_root_blkno(src));
	dest->p_tree_depth = src->p_tree_depth;

	for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
		if (!src->p_node[i].buf) {
			if (dest->p_node[i].buf)
				ocfs2_free(dest->p_node[i].buf);
			dest->p_node[i].blkno = 0;
			dest->p_node[i].buf = NULL;
			dest->p_node[i].el = NULL;
			continue;
		}

		if (!dest->p_node[i].buf)
			ocfs2_malloc_block(fs->fs_io, &dest->p_node[i].buf);

		assert(dest->p_node[i].buf);
		memcpy(dest->p_node[i].buf, src->p_node[i].buf,
		       fs->fs_blocksize);
		eb = (struct ocfs2_extent_block *)dest->p_node[i].buf;
		dest->p_node[i].el = &eb->h_list;

		dest->p_node[i].blkno = src->p_node[i].blkno;
	}
}

/*
 * Make the *dest path the same as src and re-initialize src path to
 * have a root only.
 */
static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
{
	int i;

	assert(path_root_blkno(dest) == path_root_blkno(src));

	for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
		ocfs2_free(&dest->p_node[i].buf);

		dest->p_node[i].blkno = src->p_node[i].blkno;
		dest->p_node[i].buf = src->p_node[i].buf;
		dest->p_node[i].el = src->p_node[i].el;

		src->p_node[i].blkno = 0;
		src->p_node[i].buf = NULL;
		src->p_node[i].el = NULL;
	}
}

/*
 * Insert an extent block at given index.
 *
 * Note:
 * This buf will be inserted into the path, so the caller shouldn't free it.
 */
static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
					char *buf)
{
	struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *) buf;
	/*
	 * Right now, no root buf is an extent block, so this helps
	 * catch code errors with dinode trees. The assertion can be
	 * safely removed if we ever need to insert extent block
	 * structures at the root.
	 */
	assert(index);

	path->p_node[index].blkno = eb->h_blkno;
	path->p_node[index].buf = (char *)buf;
	path->p_node[index].el = &eb->h_list;
}

static struct ocfs2_path *ocfs2_new_path(char *buf,
					 struct ocfs2_extent_list *root_el,
					 uint64_t blkno)
{
	struct ocfs2_path *path = NULL;

	assert(root_el->l_tree_depth < OCFS2_MAX_PATH_DEPTH);

	ocfs2_malloc0(sizeof(*path), &path);
	if (path) {
		path->p_tree_depth = root_el->l_tree_depth;
		path->p_node[0].blkno = blkno;
		path->p_node[0].buf = buf;
		path->p_node[0].el = root_el;
	}

	return path;
}

static struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
{
	return ocfs2_new_path(path_root_buf(path), path_root_el(path),
			      path_root_blkno(path));
}

struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
{
	return ocfs2_new_path(et->et_root_buf, et->et_root_el,
			      et->et_root_blkno);
}
/* Write all the extent block information to the disk.
 * We write all paths furthur down than subtree_index.
 * The caller will handle writing the sub_index.
 */
static errcode_t ocfs2_write_path_eb(ocfs2_filesys *fs,
				     struct ocfs2_path *path, int sub_index)
{
	errcode_t ret;
	int i;

	for (i = path->p_tree_depth; i > sub_index; i--) {
		ret = ocfs2_write_extent_block(fs,
					       path->p_node[i].blkno,
					       path->p_node[i].buf);
		if (ret)
			return ret;
	}

	return 0;
}

/* some extent blocks is modified and we need to synchronize them to the disk
 * accordingly.
 *
 * We will not update the inode if subtree_index is "0" since it should be
 * updated by the caller.
 *
 * left_path or right_path can be NULL, but they can't be NULL in the same time.
 * And if they are both not NULL, we will treat subtree_index in the right_path
 * as the right extent block information.
 */
static errcode_t ocfs2_sync_path_to_disk(ocfs2_filesys *fs,
					 struct ocfs2_path *left_path,
					 struct ocfs2_path *right_path,
					 int subtree_index)
{
	errcode_t ret = 0;
	struct ocfs2_path *path = NULL;

	assert(left_path || right_path);
	if (left_path) {
		ret = ocfs2_write_path_eb(fs, left_path, subtree_index);
		if (ret)
			goto bail;
	}

	if (right_path) {
		ret = ocfs2_write_path_eb(fs, right_path, subtree_index);
		if (ret)
			goto bail;
	}

	if (subtree_index) {
		/* subtree_index indicates an extent block. */
		path = right_path ? right_path : left_path;

		ret = ocfs2_write_extent_block(fs,
					path->p_node[subtree_index].blkno,
					path->p_node[subtree_index].buf);
		if (ret)
			goto bail;
	}
bail:
	return ret;
}

/*
 * Return the index of the extent record which contains cluster #v_cluster.
 * -1 is returned if it was not found.
 *
 * Should work fine on interior and exterior nodes.
 */
int ocfs2_search_extent_list(struct ocfs2_extent_list *el, uint32_t v_cluster)
{
	int ret = -1;
	int i;
	struct ocfs2_extent_rec *rec;
	uint32_t rec_end, rec_start, clusters;

	for(i = 0; i < el->l_next_free_rec; i++) {
		rec = &el->l_recs[i];

		rec_start = rec->e_cpos;
		clusters = ocfs2_rec_clusters(el->l_tree_depth, rec);

		rec_end = rec_start + clusters;

		if (v_cluster >= rec_start && v_cluster < rec_end) {
			ret = i;
			break;
		}
	}

	return ret;
}

/*
 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
 * ocfs2_extent_rec_contig only work properly against leaf nodes!
 */
static inline int ocfs2_block_extent_contig(ocfs2_filesys *fs,
					    struct ocfs2_extent_rec *ext,
					    uint64_t blkno)
{
	uint64_t blk_end = ext->e_blkno;

	blk_end += ocfs2_clusters_to_blocks(fs, ext->e_leaf_clusters);

	return blkno == blk_end;
}

static inline int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
					 struct ocfs2_extent_rec *right)
{
	uint32_t left_range;

	left_range = left->e_cpos + left->e_leaf_clusters;

	return (left_range == right->e_cpos);
}

static enum ocfs2_contig_type
	ocfs2_extent_rec_contig(ocfs2_filesys *fs,
				struct ocfs2_extent_rec *ext,
				struct ocfs2_extent_rec *insert_rec)
{
	uint64_t blkno = insert_rec->e_blkno;

	/*
	 * Refuse to coalesce extent records with different flag
	 * fields - we don't want to mix unwritten extents with user
	 * data.
	 */
	if (ext->e_flags != insert_rec->e_flags)
		return CONTIG_NONE;

	if (ocfs2_extents_adjacent(ext, insert_rec) &&
	    ocfs2_block_extent_contig(fs, ext, blkno))
			return CONTIG_RIGHT;

	blkno = ext->e_blkno;
	if (ocfs2_extents_adjacent(insert_rec, ext) &&
	    ocfs2_block_extent_contig(fs, insert_rec, blkno))
		return CONTIG_LEFT;

	return CONTIG_NONE;
}

/*
 * NOTE: We can have pretty much any combination of contiguousness and
 * appending.
 *
 * The usefulness of APPEND_TAIL is more in that it lets us know that
 * we'll have to update the path to that leaf.
 */
enum ocfs2_append_type {
	APPEND_NONE = 0,
	APPEND_TAIL,
};

enum ocfs2_split_type {
	SPLIT_NONE = 0,
	SPLIT_LEFT,
	SPLIT_RIGHT,
};

struct ocfs2_insert_type {
	enum ocfs2_split_type	ins_split;
	enum ocfs2_append_type	ins_appending;
	enum ocfs2_contig_type	ins_contig;
	int			ins_contig_index;
	int			ins_tree_depth;
};

struct ocfs2_merge_ctxt {
	enum ocfs2_contig_type	c_contig_type;
	int			c_has_empty_extent;
	int			c_split_covers_rec;
};

/*
 * Helper function for ocfs2_add_branch() and shift_tree_depth().
 *
 * Returns the sum of the rightmost extent rec logical offset and
 * cluster count.
 *
 * ocfs2_add_branch() uses this to determine what logical cluster
 * value should be populated into the leftmost new branch records.
 *
 * shift_tree_depth() uses this to determine the # clusters
 * value for the new topmost tree record.
 */
static inline uint32_t ocfs2_sum_rightmost_rec(struct ocfs2_extent_list  *el)
{
	uint16_t i = el->l_next_free_rec - 1;

	return el->l_recs[i].e_cpos +
		 ocfs2_rec_clusters(el->l_tree_depth, &el->l_recs[i]);

}

/*
 * Add an entire tree branch to our inode. eb_buf is the extent block
 * to start at, if we don't want to start the branch at the dinode
 * structure.
 *
 * last_eb_buf is required as we have to update it's next_leaf pointer
 * for the new last extent block.
 *
 * the new branch will be 'empty' in the sense that every block will
 * contain a single record with e_clusters == 0.
 */
static int ocfs2_add_branch(ocfs2_filesys *fs,
			    struct ocfs2_extent_tree *et,
			    char *eb_buf,
			    char **last_eb_buf)
{
	errcode_t ret;
	int new_blocks, i;
	uint64_t next_blkno, new_last_eb_blk;
	struct ocfs2_extent_block *eb;
	struct ocfs2_extent_list  *eb_el;
	struct ocfs2_extent_list  *el;
	uint32_t new_cpos;
	uint64_t *new_blknos = NULL;
	char	**new_eb_bufs = NULL;
	char *buf = NULL;

	assert(*last_eb_buf);

	if (eb_buf) {
		eb = (struct ocfs2_extent_block *) eb_buf;
		el = &eb->h_list;
	} else
		el = et->et_root_el;

	/* we never add a branch to a leaf. */
	assert(el->l_tree_depth);

	new_blocks = el->l_tree_depth;

	/* allocate the number of new eb blocks we need new_blocks should be
	 * allocated here.*/
	ret = ocfs2_malloc0(sizeof(uint64_t) * new_blocks, &new_blknos);
	if (ret)
		goto bail;
	memset(new_blknos, 0, sizeof(uint64_t) * new_blocks);

	ret = ocfs2_malloc0(sizeof(char *) * new_blocks, &new_eb_bufs);
	if (ret)
		goto bail;
	memset(new_eb_bufs, 0, sizeof(char *) * new_blocks);

	for (i = 0; i < new_blocks; i++) {
		ret = ocfs2_malloc_block(fs->fs_io, &buf);
		if (ret)
			return ret;
		new_eb_bufs[i] = buf;

		ret = ocfs2_new_extent_block(fs, &new_blknos[i]);
		if (ret)
			goto bail;

		ret = ocfs2_read_extent_block(fs, new_blknos[i], buf);
		if (ret)
			goto bail;
	}

	eb = (struct ocfs2_extent_block *)(*last_eb_buf);
	new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);

	/* Note: new_eb_bufs[new_blocks - 1] is the guy which will be
	 * linked with the rest of the tree.
	 * conversly, new_eb_bufs[0] is the new bottommost leaf.
	 *
	 * when we leave the loop, new_last_eb_blk will point to the
	 * newest leaf, and next_blkno will point to the topmost extent
	 * block.
	 */
	next_blkno = new_last_eb_blk = 0;
	for(i = 0; i < new_blocks; i++) {
		buf = new_eb_bufs[i];
		eb = (struct ocfs2_extent_block *) buf;
		eb_el = &eb->h_list;

		eb->h_next_leaf_blk = 0;
		eb_el->l_tree_depth = i;
		eb_el->l_next_free_rec = 1;
		memset(eb_el->l_recs, 0,
		       sizeof(struct ocfs2_extent_rec) * eb_el->l_count);
		/*
		 * This actually counts as an empty extent as
		 * c_clusters == 0
		 */
		eb_el->l_recs[0].e_cpos = new_cpos;
		eb_el->l_recs[0].e_blkno = next_blkno;
		/*
		 * eb_el isn't always an interior node, but even leaf
		 * nodes want a zero'd flags and reserved field so
		 * this gets the whole 32 bits regardless of use.
		 */
		eb_el->l_recs[0].e_int_clusters = 0;

		if (!eb_el->l_tree_depth)
			new_last_eb_blk = eb->h_blkno;

		next_blkno = eb->h_blkno;
	}

	/* Link the new branch into the rest of the tree (el will
	 * either be on the fe, or the extent block passed in.
	 */
	i = el->l_next_free_rec;
	el->l_recs[i].e_blkno = next_blkno;
	el->l_recs[i].e_cpos = new_cpos;
	el->l_recs[i].e_int_clusters = 0;
	el->l_next_free_rec++;

	/* fe needs a new last extent block pointer, as does the
	 * next_leaf on the previously last-extent-block.
	 */
	ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);

	/* here all the extent block and the new inode information should be
	 * written back to the disk.
	 */
	for(i = 0; i < new_blocks; i++) {
		buf = new_eb_bufs[i];
		ret = ocfs2_write_extent_block(fs, new_blknos[i], buf);
		if (ret)
			goto bail;
	}

	/* update last_eb_buf's next_leaf pointer for
	 * the new last extent block.
	 */
	eb = (struct ocfs2_extent_block *)(*last_eb_buf);
	eb->h_next_leaf_blk = new_last_eb_blk;
	ret = ocfs2_write_extent_block(fs, eb->h_blkno, *last_eb_buf);
	if (ret)
		goto bail;

	if (eb_buf) {
		eb = (struct ocfs2_extent_block *)eb_buf;
		ret = ocfs2_write_extent_block(fs, eb->h_blkno, eb_buf);
		if (ret)
			goto bail;
	}

	/*
	 * Some callers want to track the rightmost leaf so pass it
	 * back here.
	 */
	memcpy(*last_eb_buf, new_eb_bufs[0], fs->fs_blocksize);

	/* The inode information isn't updated since we use duplicated extent
	 * block in the insertion and it may fail in other steps.
	 */
	ret = 0;
bail:
	if (new_eb_bufs) {
		for (i = 0; i < new_blocks; i++)
			if (new_eb_bufs[i])
				ocfs2_free(&new_eb_bufs[i]);
		ocfs2_free(&new_eb_bufs);
	}

	if (ret && new_blknos)
		for (i = 0; i < new_blocks; i++)
			if (new_blknos[i])
				ocfs2_delete_extent_block(fs, new_blknos[i]);

	if (new_blknos)
		ocfs2_free(&new_blknos);

	return ret;
}

/*
 * Should only be called when there is no space left in any of the
 * leaf nodes. What we want to do is find the lowest tree depth
 * non-leaf extent block with room for new records. There are three
 * valid results of this search:
 *
 * 1) a lowest extent block is found, then we pass it back in
 *    *target_buf and return '0'
 *
 * 2) the search fails to find anything, but the dinode has room. We
 *    pass NULL back in *target_buf, but still return '0'
 *
 * 3) the search fails to find anything AND the dinode is full, in
 *    which case we return > 0
 *
 * return status < 0 indicates an error.
 */
static errcode_t ocfs2_find_branch_target(ocfs2_filesys *fs,
					  struct ocfs2_extent_tree *et,
					  char **target_buf)
{
	errcode_t ret = 0;
	int i;
	uint64_t blkno;
	struct ocfs2_extent_block *eb;
	char *buf = NULL, *lowest_buf = NULL;
	struct ocfs2_extent_list *el = et->et_root_el;

	*target_buf = NULL;

	ret = ocfs2_malloc_block(fs->fs_io, &buf);
	if (ret)
		return ret;

	while(el->l_tree_depth > 1) {
		if (el->l_next_free_rec == 0) {
			ret = OCFS2_ET_CORRUPT_EXTENT_BLOCK;
			goto bail;
		}
		i = el->l_next_free_rec - 1;
		blkno = el->l_recs[i].e_blkno;
		if (!blkno) {
			ret = OCFS2_ET_CORRUPT_EXTENT_BLOCK;
			goto bail;
		}

		ret = ocfs2_read_extent_block(fs, blkno, buf);
		if (ret)
			goto bail;

		eb = (struct ocfs2_extent_block *) buf;
		el = &eb->h_list;

		if (el->l_next_free_rec < el->l_count)
			lowest_buf = buf;
	}

	el = et->et_root_el;
	/* If we didn't find one and the fe doesn't have any room,
	 * then return '1' */
	if (!lowest_buf && el->l_next_free_rec == el->l_count)
		ret = 1;

	*target_buf = lowest_buf;
bail:
	if (buf && !*target_buf)
		ocfs2_free(&buf);

	return ret;
}

/*
 * This function will discard the rightmost extent record.
 */
static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
{
	int next_free = el->l_next_free_rec;
	int count = el->l_count;
	unsigned int num_bytes;

	assert(next_free);
	/* This will cause us to go off the end of our extent list. */
	assert(next_free < count);

	num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;

	memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
}

static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
			      struct ocfs2_extent_rec *insert_rec)
{
	int i, insert_index, next_free, has_empty, num_bytes;
	uint32_t insert_cpos = insert_rec->e_cpos;
	struct ocfs2_extent_rec *rec;

	next_free = el->l_next_free_rec;
	has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);

	assert(next_free);

	/* The tree code before us didn't allow enough room in the leaf. */
	if (el->l_next_free_rec == el->l_count && !has_empty)
		assert(0);

	/*
	 * The easiest way to approach this is to just remove the
	 * empty extent and temporarily decrement next_free.
	 */
	if (has_empty) {
		/*
		 * If next_free was 1 (only an empty extent), this
		 * loop won't execute, which is fine. We still want
		 * the decrement above to happen.
		 */
		for(i = 0; i < (next_free - 1); i++)
			el->l_recs[i] = el->l_recs[i+1];

		next_free--;
	}

	/* Figure out what the new record index should be. */
	for(i = 0; i < next_free; i++) {
		rec = &el->l_recs[i];

		if (insert_cpos < rec->e_cpos)
			break;
	}
	insert_index = i;

	assert(insert_index >= 0);
	assert(insert_index < el->l_count);
	assert(insert_index <= next_free);

	/* No need to memmove if we're just adding to the tail. */
	if (insert_index != next_free) {
		assert(next_free < el->l_count);

		num_bytes = next_free - insert_index;
		num_bytes *= sizeof(struct ocfs2_extent_rec);
		memmove(&el->l_recs[insert_index + 1],
			&el->l_recs[insert_index],
			num_bytes);
	}

	/*
	 * Either we had an empty extent, and need to re-increment or
	 * there was no empty extent on a non full rightmost leaf node,
	 * in which case we still need to increment.
	 */
	next_free++;
	el->l_next_free_rec = next_free;
	/* Make sure none of the math above just messed up our tree. */
	assert(el->l_next_free_rec <= el->l_count);

	el->l_recs[insert_index] = *insert_rec;
}

static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
{
	int size, num_recs = el->l_next_free_rec;

	assert(num_recs);

	if (ocfs2_is_empty_extent(&el->l_recs[0])) {
		num_recs--;
		size = num_recs * sizeof(struct ocfs2_extent_rec);
		memmove(&el->l_recs[0], &el->l_recs[1], size);
		memset(&el->l_recs[num_recs], 0,
		       sizeof(struct ocfs2_extent_rec));
		el->l_next_free_rec = num_recs;
	}
}

/*
 * Create an empty extent record .
 *
 * l_next_free_rec may be updated.
 *
 * If an empty extent already exists do nothing.
 */
static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
{
	int next_free = el->l_next_free_rec;

	assert(el->l_tree_depth == 0);

	if (next_free == 0)
		goto set_and_inc;

	if (ocfs2_is_empty_extent(&el->l_recs[0]))
		return;

	ocfs2_shift_records_right(el);

set_and_inc:
	el->l_next_free_rec += 1;
	memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
}

/*
 * For a rotation which involves two leaf nodes, the "root node" is
 * the lowest level tree node which contains a path to both leafs. This
 * resulting set of information can be used to form a complete "subtree"
 *
 * This function is passed two full paths from the dinode down to a
 * pair of adjacent leaves. It's task is to figure out which path
 * index contains the subtree root - this can be the root index itself
 * in a worst-case rotation.
 *
 * The array index of the subtree root is passed back.
 */
static int ocfs2_find_subtree_root(struct ocfs2_path *left,
				   struct ocfs2_path *right)
{
	int i = 0;

	/* Check that the caller passed in two paths from the same tree. */
	assert(path_root_blkno(left) == path_root_blkno(right));

	do {
		i++;

		/* The caller didn't pass two adjacent paths. */
 		if (i > left->p_tree_depth)
			assert(0);
	} while (left->p_node[i].blkno == right->p_node[i].blkno);

	return i - 1;
}

typedef errcode_t (path_insert_t)(void *, char *);

/*
 * Traverse a btree path in search of cpos, starting at root_el.
 *
 * This code can be called with a cpos larger than the tree, in which
 * case it will return the rightmost path.
 */
static errcode_t __ocfs2_find_path(ocfs2_filesys *fs,
				   struct ocfs2_extent_list *root_el,
				   uint32_t cpos,
				   path_insert_t *func,
				   void *data)
{
	int i, ret = 0;
	uint32_t range;
	uint64_t blkno;
	char *buf = NULL;
	struct ocfs2_extent_block *eb;
	struct ocfs2_extent_list *el;
	struct ocfs2_extent_rec *rec;

	el = root_el;
	while (el->l_tree_depth) {
		if (el->l_next_free_rec == 0) {
			ret = OCFS2_ET_CORRUPT_EXTENT_BLOCK;
			goto out;

		}


		for(i = 0; i < el->l_next_free_rec - 1; i++) {
			rec = &el->l_recs[i];

			/*
			 * In the case that cpos is off the allocation
			 * tree, this should just wind up returning the
			 * rightmost record.
			 */
			range = rec->e_cpos +
				ocfs2_rec_clusters(el->l_tree_depth, rec);
			if (cpos >= rec->e_cpos && cpos < range)
			    break;
		}

		blkno = el->l_recs[i].e_blkno;
		if (blkno == 0) {
			ret = OCFS2_ET_CORRUPT_EXTENT_BLOCK;
			goto out;
		}

		ret = ocfs2_malloc_block(fs->fs_io, &buf);
		if (ret)
			return ret;

		ret = ocfs2_read_extent_block(fs, blkno, buf);
		if (ret)
			goto out;

		eb = (struct ocfs2_extent_block *) buf;
		el = &eb->h_list;

		if (el->l_next_free_rec > el->l_count) {
			ret = OCFS2_ET_CORRUPT_EXTENT_BLOCK;
			goto out;
		}

		/* The user's callback must give us the tip for how to
		 * handle the buf we allocated by return values.
		 *
 		 * 1) return '0':
		 *    the function succeeds,and it will use the buf and
		 *    take care of the buffer release.
		 *
 		 * 2) return > 0:
		 *    the function succeeds, and there is no need for buf,
		 *    so we will release it.
		 *
		 * 3) return < 0:
		 *    the function fails.
		 */
		if (func) {
			ret = func(data, buf);

			if (ret == 0) {
				buf = NULL;
				continue;
			}
			else if (ret < 0)
				goto out;
		}
		ocfs2_free(&buf);
		buf = NULL;
	}

out:
	/* Catch any trailing buf that the loop didn't handle. */
	if (buf)
		ocfs2_free(&buf);

	return ret;
}

/*
 * Given an initialized path (that is, it has a valid root extent
 * list), this function will traverse the btree in search of the path
 * which would contain cpos.
 *
 * The path traveled is recorded in the path structure.
 *
 * Note that this will not do any comparisons on leaf node extent
 * records, so it will work fine in the case that we just added a tree
 * branch.
 */
struct find_path_data {
	int index;
	struct ocfs2_path *path;
};

static errcode_t find_path_ins(void *data, char *eb)
{
	struct find_path_data *fp = data;

	ocfs2_path_insert_eb(fp->path, fp->index, eb);
	fp->index++;

	return 0;
}

int ocfs2_find_path(ocfs2_filesys *fs, struct ocfs2_path *path,
		    uint32_t cpos)
{
	struct find_path_data data;

	data.index = 1;
	data.path = path;
	return __ocfs2_find_path(fs, path_root_el(path), cpos,
				 find_path_ins, &data);
}

/*
 * Find the leaf block in the tree which would contain cpos. No
 * checking of the actual leaf is done.
 *
 * This function doesn't handle non btree extent lists.
 */
int ocfs2_tree_find_leaf(ocfs2_filesys *fs, struct ocfs2_extent_list *el,
			 uint64_t el_blkno, char *el_blk,
			 uint32_t cpos, char **leaf_buf)
{
	int ret;
	char *buf = NULL;
	struct ocfs2_path *path = NULL;

	assert(el->l_tree_depth > 0);

	path = ocfs2_new_path(el_blk, el, el_blkno);
	if (!path) {
		ret = OCFS2_ET_NO_MEMORY;
		goto out;
	}

	ret = ocfs2_find_path(fs, path, cpos);
	if (ret)
		goto out;

	ret = ocfs2_malloc_block(fs->fs_io, &buf);
	if (ret)
		goto out;

	memcpy(buf, path_leaf_buf(path), fs->fs_blocksize);
	*leaf_buf = buf;
out:
	ocfs2_free_path(path);
	return ret;
}

int ocfs2_find_leaf(ocfs2_filesys *fs, struct ocfs2_dinode *di,
		    uint32_t cpos, char **leaf_buf)
{
	return ocfs2_tree_find_leaf(fs, &di->id2.i_list, di->i_blkno,
				    (char *)di, cpos, leaf_buf);
}

/*
 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
 *
 * Basically, we've moved stuff around at the bottom of the tree and
 * we need to fix up the extent records above the changes to reflect
 * the new changes.
 *
 * left_rec: the record on the left.
 * left_child_el: is the child list pointed to by left_rec
 * right_rec: the record to the right of left_rec
 * right_child_el: is the child list pointed to by right_rec
 *
 * By definition, this only works on interior nodes.
 */
static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
				    struct ocfs2_extent_list *left_child_el,
				    struct ocfs2_extent_rec *right_rec,
				    struct ocfs2_extent_list *right_child_el)
{
	uint32_t left_clusters, right_end;

	/*
	 * Interior nodes never have holes. Their cpos is the cpos of
	 * the leftmost record in their child list. Their cluster
	 * count covers the full theoretical range of their child list
	 * - the range between their cpos and the cpos of the record
	 * immediately to their right.
	 */
	left_clusters = right_child_el->l_recs[0].e_cpos;
	if (ocfs2_is_empty_extent(&right_child_el->l_recs[0])) {
		assert(right_child_el->l_next_free_rec > 1);
		left_clusters = right_child_el->l_recs[1].e_cpos;
	}
	left_clusters -= left_rec->e_cpos;
	left_rec->e_int_clusters = left_clusters;

	/*
	 * Calculate the rightmost cluster count boundary before
	 * moving cpos - we will need to adjust clusters after
	 * updating e_cpos to keep the same highest cluster count.
	 */
	right_end = right_rec->e_cpos;
	right_end += right_rec->e_int_clusters;

	right_rec->e_cpos = left_rec->e_cpos;
	right_rec->e_cpos += left_clusters;

	right_end -= right_rec->e_cpos;
	right_rec->e_int_clusters = right_end;
}

/*
 * Adjust the adjacent root node records involved in a
 * rotation. left_el_blkno is passed in as a key so that we can easily
 * find it's index in the root list.
 */
static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
				      struct ocfs2_extent_list *left_el,
				      struct ocfs2_extent_list *right_el,
				      uint64_t left_el_blkno)
{
	int i;

	assert(root_el->l_tree_depth > left_el->l_tree_depth);

	for(i = 0; i < root_el->l_next_free_rec - 1; i++) {
		if (root_el->l_recs[i].e_blkno == left_el_blkno)
			break;
	}

	/*
	 * The path walking code should have never returned a root and
	 * two paths which are not adjacent.
	 */
	assert(i < (root_el->l_next_free_rec - 1));

	ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
				      &root_el->l_recs[i + 1], right_el);
}

/*
 * We've changed a leaf block (in right_path) and need to reflect that
 * change back up the subtree.
 *
 * This happens in multiple places:
 *   - When we've moved an extent record from the left path leaf to the right
 *     path leaf to make room for an empty extent in the left path leaf.
 *   - When our insert into the right path leaf is at the leftmost edge
 *     and requires an update of the path immediately to it's left. This
 *     can occur at the end of some types of rotation and appending inserts.
 */
static void ocfs2_complete_edge_insert(ocfs2_filesys *fs,
				       struct ocfs2_path *left_path,
				       struct ocfs2_path *right_path,
				       int subtree_index)
{
	int i, idx;
	uint64_t blkno;
	struct ocfs2_extent_list *el, *left_el, *right_el;
	struct ocfs2_extent_rec *left_rec, *right_rec;

	/*
	 * Update the counts and position values within all the
	 * interior nodes to reflect the leaf rotation we just did.
	 *
	 * The root node is handled below the loop.
	 *
	 * We begin the loop with right_el and left_el pointing to the
	 * leaf lists and work our way up.
	 *
	 * NOTE: within this loop, left_el and right_el always refer
	 * to the *child* lists.
	 */
	left_el = path_leaf_el(left_path);
	right_el = path_leaf_el(right_path);
	for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {

		/*
		 * One nice property of knowing that all of these
		 * nodes are below the root is that we only deal with
		 * the leftmost right node record and the rightmost
		 * left node record.
		 */
		el = left_path->p_node[i].el;
		idx = left_el->l_next_free_rec - 1;
		left_rec = &el->l_recs[idx];

		el = right_path->p_node[i].el;
		right_rec = &el->l_recs[0];

		ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
					      right_el);

		/*
		 * Setup our list pointers now so that the current
		 * parents become children in the next iteration.
		 */
		left_el = left_path->p_node[i].el;
		right_el = right_path->p_node[i].el;
	}

	/*
	 * At the root node, adjust the two adjacent records which
	 * begin our path to the leaves.
	 */

	el = left_path->p_node[subtree_index].el;
	left_el = left_path->p_node[subtree_index + 1].el;
	right_el = right_path->p_node[subtree_index + 1].el;
	blkno = left_path->p_node[subtree_index + 1].blkno;

	ocfs2_adjust_root_records(el, left_el, right_el, blkno);

	/* ocfs2_adjust_root_records only update the extent block in the left
	 * path, and actually right_path->p_node[subtree_index].eb indicates the
	 * same extent block, so we must keep them the same content.
	 */
	memcpy(right_path->p_node[subtree_index].buf,
	       left_path->p_node[subtree_index].buf, fs->fs_blocksize);
}

/* Rotate the subtree to right.
 *
 * Note: After successful rotation, the extent block will be flashed
 * to disk accordingly.
 */
static errcode_t ocfs2_rotate_subtree_right(ocfs2_filesys *fs,
					    struct ocfs2_path *left_path,
					    struct ocfs2_path *right_path,
					    int subtree_index)
{
	errcode_t ret;
	int i;
	struct ocfs2_extent_list *right_el, *left_el;
	struct ocfs2_extent_rec move_rec;

	left_el = path_leaf_el(left_path);

	if (left_el->l_next_free_rec != left_el->l_count)
		return OCFS2_ET_CORRUPT_EXTENT_BLOCK;

	/*
	 * This extent block may already have an empty record, so we
	 * return early if so.
	 */
	if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
		return 0;

	assert(left_path->p_node[subtree_index].blkno ==
	       right_path->p_node[subtree_index].blkno);

	right_el = path_leaf_el(right_path);

	ocfs2_create_empty_extent(right_el);

	/* Do the copy now. */
	i = left_el->l_next_free_rec - 1;
	move_rec = left_el->l_recs[i];
	right_el->l_recs[0] = move_rec;

	/*
	 * Clear out the record we just copied and shift everything
	 * over, leaving an empty extent in the left leaf.
	 *
	 * We temporarily subtract from next_free_rec so that the
	 * shift will lose the tail record (which is now defunct).
	 */
	left_el->l_next_free_rec -= 1;
	ocfs2_shift_records_right(left_el);
	memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
	left_el->l_next_free_rec += 1;

	ocfs2_complete_edge_insert(fs, left_path, right_path, subtree_index);

	ret = ocfs2_sync_path_to_disk(fs, left_path, right_path, subtree_index);

	return ret;
}

/*
 * Given a full path, determine what cpos value would return us a path
 * containing the leaf immediately to the left of the current one.
 *
 * Will return zero if the path passed in is already the leftmost path.
 */
static int ocfs2_find_cpos_for_left_leaf(struct ocfs2_path *path,
					 uint32_t *cpos)
{
	int i, j, ret = 0;
	uint64_t blkno;
	struct ocfs2_extent_list *el;

	assert(path->p_tree_depth > 0);

	*cpos = 0;

	blkno = path_leaf_blkno(path);

	/* Start at the tree node just above the leaf and work our way up. */
	i = path->p_tree_depth - 1;
	while (i >= 0) {
		el = path->p_node[i].el;

		/* Find the extent record just before the one in our path. */
		for(j = 0; j < el->l_next_free_rec; j++) {
			if (el->l_recs[j].e_blkno == blkno) {
				if (j == 0) {
					if (i == 0) {
						/*
						 * We've determined that the
						 * path specified is already
						 * the leftmost one - return a
						 * cpos of zero.
						 */
						goto out;
					}
					/*
					 * The leftmost record points to our
					 * leaf - we need to travel up the
					 * tree one level.
					 */
					goto next_node;
				}

				*cpos = el->l_recs[j - 1].e_cpos;
				*cpos = *cpos + ocfs2_rec_clusters(
							el->l_tree_depth,
							&el->l_recs[j - 1]);
				*cpos = *cpos - 1;
				goto out;
			}
		}

		/*
		 * If we got here, we never found a valid node where
		 * the tree indicated one should be.
		 */
		ret = OCFS2_ET_CORRUPT_EXTENT_BLOCK;
		goto out;

next_node:
		blkno = path->p_node[i].blkno;
		i--;
	}

out:
	return ret;
}

/*
 * Trap the case where we're inserting into the theoretical range past
 * the _actual_ left leaf range. Otherwise, we'll rotate a record
 * whose cpos is less than ours into the right leaf.
 *
 * It's only necessary to look at the rightmost record of the left
 * leaf because the logic that calls us should ensure that the
 * theoretical ranges in the path components above the leaves are
 * correct.
 */
static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
						 uint32_t insert_cpos)
{
	struct ocfs2_extent_list *left_el;
	struct ocfs2_extent_rec *rec;
	int next_free;

	left_el = path_leaf_el(left_path);
	next_free = left_el->l_next_free_rec;
	rec = &left_el->l_recs[next_free - 1];

	if (insert_cpos > rec->e_cpos)
		return 1;
	return 0;
}

static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el,
				       uint32_t cpos)
{
	int next_free = el->l_next_free_rec;
	unsigned int range;
	struct ocfs2_extent_rec *rec;

	if (next_free == 0)
		return 0;

	rec = &el->l_recs[0];
	if (ocfs2_is_empty_extent(rec)) {
		/* Empty list. */
		if (next_free == 1)
			return 0;
		rec = &el->l_recs[1];
	}

	range = rec->e_cpos + ocfs2_rec_clusters(el->l_tree_depth, rec);
	if (cpos >= rec->e_cpos && cpos < range)
		return 1;
	return 0;
}

/*
 * Rotate all the records in a btree right one record, starting at insert_cpos.
 *
 * The path to the rightmost leaf should be passed in.
 *
 * The array is assumed to be large enough to hold an entire path (tree depth).
 *
 * Upon succesful return from this function:
 *
 * - The 'right_path' array will contain a path to the leaf block
 *   whose range contains e_cpos.
 * - That leaf block will have a single empty extent in list index 0.
 * - In the case that the rotation requires a post-insert update,
 *   *ret_left_path will contain a valid path which can be passed to
 *   ocfs2_insert_path().
 */
static int ocfs2_rotate_tree_right(ocfs2_filesys *fs,
				   enum ocfs2_split_type split,
				   uint32_t insert_cpos,
				   struct ocfs2_path *right_path,
				   struct ocfs2_path **ret_left_path)
{
	int ret, start;
	uint32_t cpos;
	struct ocfs2_path *left_path = NULL;

	*ret_left_path = NULL;

	left_path = ocfs2_new_path_from_path(right_path);
	if (!left_path) {
		ret = OCFS2_ET_NO_MEMORY;
		goto out;
	}

	ret = ocfs2_find_cpos_for_left_leaf(right_path, &cpos);
	if (ret)
		goto out;

	/*
	 * What we want to do here is:
	 *
	 * 1) Start with the rightmost path.
	 *
	 * 2) Determine a path to the leaf block directly to the left
         *    of that leaf.
	 *
	 * 3) Determine the 'subtree root' - the lowest level tree node
	 *    which contains a path to both leaves.
	 *
	 * 4) Rotate the subtree.
	 *
	 * 5) Find the next subtree by considering the left path to be
         *    the new right path.
	 *
	 * The check at the top of this while loop also accepts
	 * insert_cpos == cpos because cpos is only a _theoretical_
	 * value to get us the left path - insert_cpos might very well
	 * be filling that hole.
	 *
	 * Stop at a cpos of '0' because we either started at the
	 * leftmost branch (i.e., a tree with one branch and a
	 * rotation inside of it), or we've gone as far as we can in
	 * rotating subtrees.
	 */
	while (cpos && insert_cpos <= cpos) {

		ret = ocfs2_find_path(fs, left_path, cpos);
		if (ret)
			goto out;

		if (path_leaf_blkno(left_path) == path_leaf_blkno(right_path))
			assert(0);

		if (split == SPLIT_NONE &&
		    ocfs2_rotate_requires_path_adjustment(left_path,
							  insert_cpos)) {
			/*
			 * We've rotated the tree as much as we
			 * should. The rest is up to
			 * ocfs2_insert_path() to complete, after the
			 * record insertion. We indicate this
			 * situation by returning the left path.
			 *
			 * The reason we don't adjust the records here
			 * before the record insert is that an error
			 * later might break the rule where a parent
			 * record e_cpos will reflect the actual
			 * e_cpos of the 1st nonempty record of the
			 * child list.
			 */
			*ret_left_path = left_path;
			goto out_ret_path;
		}

		start = ocfs2_find_subtree_root(left_path, right_path);

		ret = ocfs2_rotate_subtree_right(fs, left_path, right_path,
						 start);
		if (ret)
			goto out;

		if (split != SPLIT_NONE &&
		    ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
						insert_cpos)) {
			/*
			 * A rotate moves the rightmost left leaf
			 * record over to the leftmost right leaf
			 * slot. If we're doing an extent split
			 * instead of a real insert, then we have to
			 * check that the extent to be split wasn't
			 * just moved over. If it was, then we can
			 * exit here, passing left_path back -
			 * ocfs2_split_extent() is smart enough to
			 * search both leaves.
			 */
			*ret_left_path = left_path;
			goto out_ret_path;
		}
		/*
		 * There is no need to re-read the next right path
		 * as we know that it'll be our current left
		 * path. Optimize by copying values instead.
		 */
		ocfs2_mv_path(right_path, left_path);

		ret = ocfs2_find_cpos_for_left_leaf(right_path, &cpos);
		if (ret)
			goto out;
	}

out:
	ocfs2_free_path(left_path);

out_ret_path:
	return ret;
}

static void ocfs2_update_edge_lengths(struct ocfs2_path *path)
{
	int i, idx;
	struct ocfs2_extent_rec *rec;
	struct ocfs2_extent_list *el;
	struct ocfs2_extent_block *eb;
	uint32_t range;

	/* Path should always be rightmost. */
	eb = (struct ocfs2_extent_block *)path_leaf_buf(path);
	assert(eb->h_next_leaf_blk == 0ULL);

	el = &eb->h_list;
	assert(el->l_next_free_rec > 0);
	idx = el->l_next_free_rec - 1;
	rec = &el->l_recs[idx];
	range = rec->e_cpos + ocfs2_rec_clusters(el->l_tree_depth, rec);

	for (i = 0; i < path->p_tree_depth; i++) {
		el = path->p_node[i].el;
		idx = el->l_next_free_rec - 1;
		rec = &el->l_recs[idx];

		rec->e_int_clusters = range;
		rec->e_int_clusters -= rec->e_cpos;
	}
}

static errcode_t ocfs2_unlink_path(ocfs2_filesys *fs,
				   struct ocfs2_path *path, int unlink_start)
{
	int ret, i;
	struct ocfs2_extent_block *eb;
	struct ocfs2_extent_list *el;
	char *buf;

	for(i = unlink_start; i < path_num_items(path); i++) {
		buf = path->p_node[i].buf;

		eb = (struct ocfs2_extent_block *)buf;
		/*
		 * Not all nodes might have had their final count
		 * decremented by the caller - handle this here.
		 */
		el = &eb->h_list;
		assert(el->l_next_free_rec <= 1);

		el->l_next_free_rec = 0;
		memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
		ret = ocfs2_delete_extent_block(fs, path->p_node[i].blkno);
		if (ret)
			return ret;
	}
	return 0;
}

/*
 * ocfs2_unlink_subtree will delete extent blocks in the "right_path"
 * from "subtree_index".
 */
static errcode_t ocfs2_unlink_subtree(ocfs2_filesys *fs,
				      struct ocfs2_path *left_path,
				      struct ocfs2_path *right_path,
				      int subtree_index)
{
	errcode_t ret;
	int i;
	struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
	struct ocfs2_extent_block *eb;

	eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].buf;

	for(i = 1; i < root_el->l_next_free_rec; i++)
		if (root_el->l_recs[i].e_blkno == eb->h_blkno)
			break;

	assert(i < root_el->l_next_free_rec);

	memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
	root_el->l_next_free_rec -= 1;

	eb = (struct ocfs2_extent_block *)path_leaf_buf(left_path);
	eb->h_next_leaf_blk = 0;

	ret = ocfs2_unlink_path(fs, right_path, subtree_index + 1);
	if (ret)
		return ret;

	return 0;
}

static int ocfs2_rotate_subtree_left(ocfs2_filesys *fs,
				     struct ocfs2_extent_tree *et,
				     struct ocfs2_path *left_path,
				     struct ocfs2_path *right_path,
				     int subtree_index,
				     int *deleted)
{
	errcode_t ret;
	int i, del_right_subtree = 0, right_has_empty = 0;
	struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
	struct ocfs2_extent_block *eb;

	*deleted = 0;

	right_leaf_el = path_leaf_el(right_path);
	left_leaf_el = path_leaf_el(left_path);
	assert(left_path->p_node[subtree_index].blkno ==
	       right_path->p_node[subtree_index].blkno);

	if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
		return 0;

	eb = (struct ocfs2_extent_block *)path_leaf_buf(right_path);
	if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
		/*
		 * It's legal for us to proceed if the right leaf is
		 * the rightmost one and it has an empty extent. There
		 * are two cases to handle - whether the leaf will be
		 * empty after removal or not. If the leaf isn't empty
		 * then just remove the empty extent up front. The
		 * next block will handle empty leaves by flagging
		 * them for unlink.
		 *
		 * Non rightmost leaves will throw EAGAIN and the
		 * caller can manually move the subtree and retry.
		 */

		if (eb->h_next_leaf_blk != 0ULL)
			return EAGAIN;

		if (right_leaf_el->l_next_free_rec > 1) {
			ocfs2_remove_empty_extent(right_leaf_el);
		} else
			right_has_empty = 1;
	}

	if (eb->h_next_leaf_blk == 0ULL &&
	    right_leaf_el->l_next_free_rec == 1) {
		/*
		 * We have to update i_last_eb_blk during the meta
		 * data delete.
		 */
		del_right_subtree = 1;
	}

	/*
	 * Getting here with an empty extent in the right path implies
	 * that it's the rightmost path and will be deleted.
	 */
	assert(!right_has_empty || del_right_subtree);

	if (!right_has_empty) {
		/*
		 * Only do this if we're moving a real
		 * record. Otherwise, the action is delayed until
		 * after removal of the right path in which case we
		 * can do a simple shift to remove the empty extent.
		 */
		ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
		memset(&right_leaf_el->l_recs[0], 0,
		       sizeof(struct ocfs2_extent_rec));
	}
	if (eb->h_next_leaf_blk == 0ULL) {
		/*
		 * Move recs over to get rid of empty extent, decrease
		 * next_free. This is allowed to remove the last
		 * extent in our leaf (setting l_next_free_rec to
		 * zero) - the delete code below won't care.
		 */
		ocfs2_remove_empty_extent(right_leaf_el);
	}

	if (del_right_subtree) {
		ocfs2_unlink_subtree(fs, left_path, right_path, subtree_index);
		ocfs2_update_edge_lengths(left_path);

		/*
		 * Now the good extent block information is stored in left_path, so
		 * synchronize the right path with it.
		 */
		for (i = 0; i <= subtree_index; i++)
			memcpy(right_path->p_node[i].buf,
			       left_path->p_node[i].buf,
			       fs->fs_blocksize);

		eb = (struct ocfs2_extent_block *)path_leaf_buf(left_path);
		ocfs2_et_set_last_eb_blk(et, eb->h_blkno);

		/*
		 * Removal of the extent in the left leaf was skipped
		 * above so we could delete the right path
		 * 1st.
		 */
		if (right_has_empty)
			ocfs2_remove_empty_extent(left_leaf_el);

		*deleted = 1;

		/*
		 * The extent block in the right path belwo subtree_index
		 * have been deleted, so we don't need to synchronize
		 * them to the disk.
		 */
		ret = ocfs2_sync_path_to_disk(fs, left_path, NULL,
					      subtree_index);
	} else {
		ocfs2_complete_edge_insert(fs, left_path, right_path,
					   subtree_index);

		ret = ocfs2_sync_path_to_disk(fs, left_path, right_path,
					      subtree_index);
	}

	return ret;
}

/*
 * Given a full path, determine what cpos value would return us a path
 * containing the leaf immediately to the right of the current one.
 *
 * Will return zero if the path passed in is already the rightmost path.
 *
 * This looks similar, but is subtly different to
 * ocfs2_find_cpos_for_left_leaf().
 */
static int ocfs2_find_cpos_for_right_leaf(ocfs2_filesys *fs,
					  struct ocfs2_path *path,
					  uint32_t *cpos)
{
	int i, j, ret = 0;
	uint64_t blkno;
	struct ocfs2_extent_list *el;

	*cpos = 0;

	if (path->p_tree_depth == 0)
		return 0;

	blkno = path_leaf_blkno(path);

	/* Start at the tree node just above the leaf and work our way up. */
	i = path->p_tree_depth - 1;
	while (i >= 0) {
		int next_free;

		el = path->p_node[i].el;

		/*
		 * Find the extent record just after the one in our
		 * path.
		 */
		next_free = el->l_next_free_rec;
		for(j = 0; j < el->l_next_free_rec; j++) {
			if (el->l_recs[j].e_blkno == blkno) {
				if (j == (next_free - 1)) {
					if (i == 0) {
						/*
						 * We've determined that the
						 * path specified is already
						 * the rightmost one - return a
						 * cpos of zero.
						 */
						goto out;
					}
					/*
					 * The rightmost record points to our
					 * leaf - we need to travel up the
					 * tree one level.
					 */
					goto next_node;
				}

				*cpos = el->l_recs[j + 1].e_cpos;
				goto out;
			}
		}

		/*
		 * If we got here, we never found a valid node where
		 * the tree indicated one should be.
		 */
		ret = OCFS2_ET_CORRUPT_EXTENT_BLOCK;
		goto out;

next_node:
		blkno = path->p_node[i].blkno;
		i--;
	}

out:
	return ret;
}

static void ocfs2_rotate_rightmost_leaf_left(ocfs2_filesys *fs,
					    struct ocfs2_extent_list *el)
{
	if (!ocfs2_is_empty_extent(&el->l_recs[0]))
		return;

	ocfs2_remove_empty_extent(el);

	return;
}

static int __ocfs2_rotate_tree_left(ocfs2_filesys *fs,
				    struct ocfs2_extent_tree *et,
				    struct ocfs2_path *path,
				    struct ocfs2_path **empty_extent_path)
{
	int i, ret, subtree_root, deleted;
	uint32_t right_cpos;
	struct ocfs2_path *left_path = NULL;
	struct ocfs2_path *right_path = NULL;

	assert(ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));

	*empty_extent_path = NULL;

	ret = ocfs2_find_cpos_for_right_leaf(fs, path, &right_cpos);
	if (ret)
		goto out;

	left_path = ocfs2_new_path_from_path(path);
	if (!left_path) {
		ret = OCFS2_ET_NO_MEMORY;
		goto out;
	}

	ocfs2_cp_path(fs, left_path, path);

	right_path = ocfs2_new_path_from_path(path);
	if (!right_path) {
		ret = OCFS2_ET_NO_MEMORY;
		goto out;
	}

	while (right_cpos) {
		ret = ocfs2_find_path(fs, right_path, right_cpos);
		if (ret)
			goto out;

		subtree_root = ocfs2_find_subtree_root(left_path,
						       right_path);

		ret = ocfs2_rotate_subtree_left(fs, et, left_path,
						right_path, subtree_root,
						&deleted);
		if (ret == EAGAIN) {
			/*
			 * The rotation has to temporarily stop due to
			 * the right subtree having an empty
			 * extent. Pass it back to the caller for a
			 * fixup.
			 */
			*empty_extent_path = right_path;
			right_path = NULL;
			goto out;
		}
		if (ret)
			goto out;

		/*
		 * The subtree rotate might have removed records on
		 * the rightmost edge. If so, then rotation is
		 * complete.
		 */
		if (deleted)
			break;

		ocfs2_mv_path(left_path, right_path);

		ret = ocfs2_find_cpos_for_right_leaf(fs, left_path,
						     &right_cpos);
		if (ret)
			goto out;
	}

out:
	ocfs2_free_path(right_path);
	ocfs2_free_path(left_path);

	/*
	 * the path's information is changed during the process of rotation,
	 * so re-read them.
	 */
	for (i = 1; i <= path->p_tree_depth; i++) {
		ret = ocfs2_read_extent_block(fs, path->p_node[i].blkno,
					      path->p_node[i].buf);
		if (ret)
			break;
	}

	return ret;
}

static int ocfs2_remove_rightmost_path(ocfs2_filesys *fs,
				       struct ocfs2_extent_tree *et,
				       struct ocfs2_path *path)
{
	int ret, subtree_index, i;
	uint32_t cpos;
	struct ocfs2_path *left_path = NULL;
	struct ocfs2_extent_block *eb;
	struct ocfs2_extent_list *el;

	ret = ocfs2_find_cpos_for_left_leaf(path, &cpos);
	if (ret)
		goto out;

	if (cpos) {
		/*
		 * We have a path to the left of this one - it needs
		 * an update too.
		 */
		left_path = ocfs2_new_path_from_path(path);
		if (!left_path) {
			ret = OCFS2_ET_NO_MEMORY;
			goto out;
		}

		ret = ocfs2_find_path(fs, left_path, cpos);
		if (ret)
			goto out;

		subtree_index = ocfs2_find_subtree_root(left_path, path);

		ocfs2_unlink_subtree(fs, left_path, path,
				     subtree_index);
		ocfs2_update_edge_lengths(left_path);

		/*
		 * Now the good extent block information is stored in left_path, so
		 * synchronize the right path with it.
		 */
		for (i = 0; i <= subtree_index; i++)
			memcpy(path->p_node[i].buf,
			       left_path->p_node[i].buf,
			       fs->fs_blocksize);
		ret = ocfs2_sync_path_to_disk(fs, left_path,
					      NULL, subtree_index);
		if (ret)
			goto out;

		eb = (struct ocfs2_extent_block *)path_leaf_buf(left_path);
		ocfs2_et_set_last_eb_blk(et, eb->h_blkno);
	} else {
		/*
		 * 'path' is also the leftmost path which
		 * means it must be the only one. This gets
		 * handled differently because we want to
		 * revert the inode back to having extents
		 * in-line.
		 */
		ocfs2_unlink_path(fs, path, 1);

		el = et->et_root_el;
		el->l_tree_depth = 0;
		el->l_next_free_rec = 0;
		memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));

		ocfs2_et_set_last_eb_blk(et, 0);
	}

out:
	ocfs2_free_path(left_path);
	return ret;
}

/*
 * Left rotation of btree records.
 *
 * In many ways, this is (unsurprisingly) the opposite of right
 * rotation. We start at some non-rightmost path containing an empty
 * extent in the leaf block. The code works its way to the rightmost
 * path by rotating records to the left in every subtree.
 *
 * This is used by any code which reduces the number of extent records
 * in a leaf. After removal, an empty record should be placed in the
 * leftmost list position.
 *
 * This won't handle a length update of the rightmost path records if
 * the rightmost tree leaf record is removed so the caller is
 * responsible for detecting and correcting that.
 */
static int ocfs2_rotate_tree_left(ocfs2_filesys *fs,
				  struct ocfs2_extent_tree *et,
				  struct ocfs2_path *path)
{
	int ret = 0;
	struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
	struct ocfs2_extent_block *eb;
	struct ocfs2_extent_list *el;

	el = path_leaf_el(path);
	if (!ocfs2_is_empty_extent(&el->l_recs[0]))
		return 0;

	if (path->p_tree_depth == 0) {
rightmost_no_delete:
		/*
		 * In-inode extents. This is trivially handled, so do
		 * it up front.
		 */
		ocfs2_rotate_rightmost_leaf_left(fs,
						 path_leaf_el(path));

		/* we have to synchronize the modified extent block to disk. */
		if (path->p_tree_depth > 0) {
			ret = ocfs2_write_extent_block(fs,
						       path_leaf_blkno(path),
						       path_leaf_buf(path));
		}

		goto out;
	}

	/*
	 * Handle rightmost branch now. There's several cases:
	 *  1) simple rotation leaving records in there. That's trivial.
	 *  2) rotation requiring a branch delete - there's no more
	 *     records left. Two cases of this:
	 *     a) There are branches to the left.
	 *     b) This is also the leftmost (the only) branch.
	 *
	 *  1) is handled via ocfs2_rotate_rightmost_leaf_left()
	 *  2a) we need the left branch so that we can update it with the unlink
	 *  2b) we need to bring the inode back to inline extents.
	 */

	eb = (struct ocfs2_extent_block *)path_leaf_buf(path);
	el = &eb->h_list;
	if (eb->h_next_leaf_blk == 0) {
		/*
		 * This gets a bit tricky if we're going to delete the
		 * rightmost path. Get the other cases out of the way
		 * 1st.
		 */
		if (el->l_next_free_rec > 1)
			goto rightmost_no_delete;

		if (el->l_next_free_rec == 0) {
			ret = OCFS2_ET_CORRUPT_EXTENT_BLOCK;
			goto out;
		}

		/*
		 * XXX: The caller can not trust "path" any more after
		 * this as it will have been deleted. What do we do?
		 *
		 * In theory the rotate-for-merge code will never get
		 * here because it'll always ask for a rotate in a
		 * nonempty list.
		 */

		ret = ocfs2_remove_rightmost_path(fs, et, path);
		goto out;
	}

	/*
	 * Now we can loop, remembering the path we get from EAGAIN
	 * and restarting from there.
	 */
try_rotate:
	ret = __ocfs2_rotate_tree_left(fs, et, path, &restart_path);
	if (ret && ret != EAGAIN) {
		goto out;
	}

	while (ret == EAGAIN) {
		tmp_path = restart_path;
		restart_path = NULL;

		ret = __ocfs2_rotate_tree_left(fs, et, tmp_path, &restart_path);
		if (ret && ret != EAGAIN) {
			goto out;
		}

		ocfs2_free_path(tmp_path);
		tmp_path = NULL;

		if (ret == 0)
			goto try_rotate;
	}

out:
	ocfs2_free_path(tmp_path);
	ocfs2_free_path(restart_path);
	return ret;
}

static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
				int index)
{
	struct ocfs2_extent_rec *rec = &el->l_recs[index];
	unsigned int size;

	if (rec->e_leaf_clusters == 0) {
		/*
		 * We consumed all of the merged-from record. An empty
		 * extent cannot exist anywhere but the 1st array
		 * position, so move things over if the merged-from
		 * record doesn't occupy that position.
		 *
		 * This creates a new empty extent so the caller
		 * should be smart enough to have removed any existing
		 * ones.
		 */
		if (index > 0) {
			assert(!ocfs2_is_empty_extent(&el->l_recs[0]));
			size = index * sizeof(struct ocfs2_extent_rec);
			memmove(&el->l_recs[1], &el->l_recs[0], size);
		}

		/*
		 * Always memset - the caller doesn't check whether it
		 * created an empty extent, so there could be junk in
		 * the other fields.
		 */
		memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
	}
}

/*
 * Remove split_rec clusters from the record at index and merge them
 * onto the beginning of the record at index + 1.
 */
static int ocfs2_merge_rec_right(ocfs2_filesys *fs,
				struct ocfs2_extent_rec *split_rec,
				struct ocfs2_extent_list *el, int index)
{
	unsigned int split_clusters = split_rec->e_leaf_clusters;
	struct ocfs2_extent_rec *left_rec;
	struct ocfs2_extent_rec *right_rec;

	assert(index < el->l_next_free_rec);

	left_rec = &el->l_recs[index];
	right_rec = &el->l_recs[index + 1];

	left_rec->e_leaf_clusters -= split_clusters;

	right_rec->e_cpos -= split_clusters;
	right_rec->e_blkno -=
			ocfs2_clusters_to_blocks(fs, split_clusters);
	right_rec->e_leaf_clusters += split_clusters;

	ocfs2_cleanup_merge(el, index);

	return 0;
}

/*
 * Remove split_rec clusters from the record at index and merge them
 * onto the tail of the record at index - 1.
 */
static int ocfs2_merge_rec_left(ocfs2_filesys *fs,
				struct ocfs2_extent_rec *split_rec,
				struct ocfs2_extent_list *el, int index)
{
	int has_empty_extent = 0;
	unsigned int split_clusters = split_rec->e_leaf_clusters;
	struct ocfs2_extent_rec *left_rec;
	struct ocfs2_extent_rec *right_rec;

	assert(index > 0);

	left_rec = &el->l_recs[index - 1];
	right_rec = &el->l_recs[index];
	if (ocfs2_is_empty_extent(&el->l_recs[0]))
		has_empty_extent = 1;

	if (has_empty_extent && index == 1) {
		/*
		 * The easy case - we can just plop the record right in.
		 */
		*left_rec = *split_rec;

		has_empty_extent = 0;
	} else {
		left_rec->e_leaf_clusters += split_clusters;
	}

	right_rec->e_cpos += split_clusters;
	right_rec->e_blkno +=
		ocfs2_clusters_to_blocks(fs, split_clusters);
	right_rec->e_leaf_clusters -= split_clusters;

	ocfs2_cleanup_merge(el, index);

	return 0;
}

static int ocfs2_try_to_merge_extent(ocfs2_filesys *fs,
				     struct ocfs2_extent_tree *et,
				     struct ocfs2_path *left_path,
				     int split_index,
				     struct ocfs2_extent_rec *split_rec,
				     struct ocfs2_merge_ctxt *ctxt)

{
	int ret = 0;
	struct ocfs2_extent_list *el = path_leaf_el(left_path);
	struct ocfs2_extent_rec *rec = &el->l_recs[split_index];

	assert(ctxt->c_contig_type != CONTIG_NONE);

	if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
		/*
		 * The merge code will need to create an empty
		 * extent to take the place of the newly
		 * emptied slot. Remove any pre-existing empty
		 * extents - having more than one in a leaf is
		 * illegal.
		 */
		ret = ocfs2_rotate_tree_left(fs, et, left_path);
		if (ret)
			goto out;

		split_index--;
		rec = &el->l_recs[split_index];
	}

	if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
		/*
		 * Left-right contig implies this.
		 */
		assert(ctxt->c_split_covers_rec);
		assert(split_index != 0);

		/*
		 * Since the leftright insert always covers the entire
		 * extent, this call will delete the insert record
		 * entirely, resulting in an empty extent record added to
		 * the extent block.
		 *
		 * Since the adding of an empty extent shifts
		 * everything back to the right, there's no need to
		 * update split_index here.
		 */
		ret = ocfs2_merge_rec_left(fs, split_rec, el, split_index);
		if (ret)
			goto out;

		/*
		 * We can only get this from logic error above.
		 */
		assert(ocfs2_is_empty_extent(&el->l_recs[0]));

		/*
		 * The left merge left us with an empty extent, remove
		 * it.
		 */
		ret = ocfs2_rotate_tree_left(fs, et, left_path);
		if (ret)
			goto out;

		split_index--;
		rec = &el->l_recs[split_index];

		/*
		 * Note that we don't pass split_rec here on purpose -
		 * we've merged it into the left side.
		 */
		ret = ocfs2_merge_rec_right(fs, rec, el, split_index);
		if (ret)
			goto out;

		assert(ocfs2_is_empty_extent(&el->l_recs[0]));

		ret = ocfs2_rotate_tree_left(fs, et, left_path);
		/*
		 * Error from this last rotate is not critical, so
		 * don't bubble it up.
		 */
		ret = 0;
	} else {
		/*
		 * Merge a record to the left or right.
		 *
		 * 'contig_type' is relative to the existing record,
		 * so for example, if we're "right contig", it's to
		 * the record on the left (hence the left merge).
		 */
		if (ctxt->c_contig_type == CONTIG_RIGHT) {
			ret = ocfs2_merge_rec_left(fs,
						   split_rec, el,
						   split_index);
			if (ret)
				goto out;
		} else {
			ret = ocfs2_merge_rec_right(fs,
						    split_rec, el,
						    split_index);
			if (ret)
				goto out;
		}

		/* we have to synchronize the modified extent block to disk. */
		if (left_path->p_tree_depth > 0) {
			ret = ocfs2_write_extent_block(fs,
					       path_leaf_blkno(left_path),
					       path_leaf_buf(left_path));
			if (ret)
				goto out;
		}

		if (ctxt->c_split_covers_rec) {
			/*
			 * The merge may have left an empty extent in
			 * our leaf. Try to rotate it away.
			 */
			ret = ocfs2_rotate_tree_left(fs, et, left_path);
			ret = 0;
		}
	}

out:
	return ret;
}

static void ocfs2_subtract_from_rec(ocfs2_filesys *fs,
				    enum ocfs2_split_type split,
				    struct ocfs2_extent_rec *rec,
				    struct ocfs2_extent_rec *split_rec)
{
	uint64_t len_blocks;

	len_blocks = ocfs2_clusters_to_blocks(fs, split_rec->e_leaf_clusters);

	if (split == SPLIT_LEFT) {
		/*
		 * Region is on the left edge of the existing
		 * record.
		 */
		rec->e_cpos += split_rec->e_leaf_clusters;
		rec->e_blkno += len_blocks;
		rec->e_leaf_clusters -= split_rec->e_leaf_clusters;
	} else {
		/*
		 * Region is on the right edge of the existing
		 * record.
		 */
		rec->e_leaf_clusters -= split_rec->e_leaf_clusters;
	}
}

/*
 * Change the depth of the tree. That means allocating an extent block,
 * copying all extent records from the dinode into the extent block,
 * and then pointing the dinode to the new extent_block.
 */
static errcode_t shift_tree_depth(ocfs2_filesys *fs,
				  struct ocfs2_extent_tree *et,
				  char **new_eb)
{
	errcode_t ret;
	char *buf = NULL;
	uint64_t blkno;
	struct ocfs2_extent_list *el = et->et_root_el;
	struct ocfs2_extent_block *eb;
	uint32_t new_clusters;

	if (el->l_next_free_rec != el->l_count)
		return OCFS2_ET_INTERNAL_FAILURE;

	ret = ocfs2_malloc_block(fs->fs_io, &buf);
	if (ret)
		return ret;

	ret = ocfs2_new_extent_block(fs, &blkno);
	if (ret)
		goto out;

	ret = ocfs2_read_extent_block(fs, blkno, buf);
	if (ret)
		goto out;

	eb = (struct ocfs2_extent_block *)buf;
	eb->h_list.l_tree_depth = el->l_tree_depth;
	eb->h_list.l_next_free_rec = el->l_next_free_rec;
	memcpy(eb->h_list.l_recs, el->l_recs,
	       sizeof(struct ocfs2_extent_rec) * el->l_count);

	new_clusters = ocfs2_sum_rightmost_rec(&eb->h_list);

	el->l_tree_depth++;
	memset(el->l_recs, 0,
	       sizeof(struct ocfs2_extent_rec) * el->l_count);
	el->l_recs[0].e_cpos = 0;
	el->l_recs[0].e_blkno = blkno;
	el->l_recs[0].e_int_clusters = new_clusters;
	el->l_next_free_rec = 1;

	if (el->l_tree_depth == 1)
		ocfs2_et_set_last_eb_blk(et, blkno);

	ret = ocfs2_write_extent_block(fs, blkno, buf);
	if (!ret)
		*new_eb = buf;
out:
	if (buf && !*new_eb)
		ocfs2_free(&buf);

	return ret;
}

static enum ocfs2_contig_type
ocfs2_figure_merge_contig_type(ocfs2_filesys *fs,
			       struct ocfs2_extent_tree *et,
			       struct ocfs2_extent_list *el, int index,
			       struct ocfs2_extent_rec *split_rec)
{
	struct ocfs2_extent_rec *rec;
	enum ocfs2_contig_type ret = CONTIG_NONE;

	/*
	 * We're careful to check for an empty extent record here -
	 * the merge code will know what to do if it sees one.
	 */

	if (index > 0) {
		rec = &el->l_recs[index - 1];
		if (index == 1 && ocfs2_is_empty_extent(rec)) {
			if (split_rec->e_cpos == el->l_recs[index].e_cpos)
				ret = CONTIG_RIGHT;
		} else {
			ret = ocfs2_et_extent_contig(fs, et, rec, split_rec);
		}
	}

	if (index < el->l_next_free_rec - 1) {
		enum ocfs2_contig_type contig_type;

		rec = &el->l_recs[index + 1];
		contig_type = ocfs2_et_extent_contig(fs, et, rec, split_rec);

		if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
			ret = CONTIG_LEFTRIGHT;
		else if (ret == CONTIG_NONE)
			ret = contig_type;
	}

	return ret;
}

static void ocfs2_figure_contig_type(ocfs2_filesys *fs,
				     struct ocfs2_extent_tree *et,
				     struct ocfs2_insert_type *insert,
				     struct ocfs2_extent_list *el,
				     struct ocfs2_extent_rec *insert_rec)
{
	int i;
	enum ocfs2_contig_type contig_type = CONTIG_NONE;

	assert(el->l_tree_depth == 0);

	for(i = 0; i < el->l_next_free_rec; i++) {
		contig_type = ocfs2_et_extent_contig(fs, et, &el->l_recs[i],
						     insert_rec);
		if (contig_type != CONTIG_NONE) {
			insert->ins_contig_index = i;
			break;
		}
	}
	insert->ins_contig = contig_type;
}

/*
 * This should only be called against the righmost leaf extent list.
 *
 * ocfs2_figure_appending_type() will figure out whether we'll have to
 * insert at the tail of the rightmost leaf.
 *
 * This should also work against the dinode list for tree's with 0
 * depth. If we consider the dinode list to be the rightmost leaf node
 * then the logic here makes sense.
 */
static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
					struct ocfs2_extent_list *el,
					struct ocfs2_extent_rec *insert_rec)
{
	int i;
	uint32_t cpos = insert_rec->e_cpos;
	struct ocfs2_extent_rec *rec;

	insert->ins_appending = APPEND_NONE;

	assert(el->l_tree_depth == 0);

	if (!el->l_next_free_rec)
		goto set_tail_append;

	if (ocfs2_is_empty_extent(&el->l_recs[0])) {
		/* Were all records empty? */
		if (el->l_next_free_rec == 1)
			goto set_tail_append;
	}

	i = el->l_next_free_rec - 1;
	rec = &el->l_recs[i];

	if (cpos >= (rec->e_cpos + rec->e_leaf_clusters))
		goto set_tail_append;

	return;

set_tail_append:
	insert->ins_appending = APPEND_TAIL;
}

/*
 * Helper function called at the begining of an insert.
 *
 * This computes a few things that are commonly used in the process of
 * inserting into the btree:
 *   - Whether the new extent is contiguous with an existing one.
 *   - The current tree depth.
 *   - Whether the insert is an appending one.
 *   - The total # of free records in the tree.
 *
 * All of the information is stored on the ocfs2_insert_type
 * structure.
 */
static int ocfs2_figure_insert_type(struct insert_ctxt *ctxt,
				    char **last_eb_buf,
				    int *free_records,
				    struct ocfs2_insert_type *insert)
{
	int ret;
	struct ocfs2_extent_block *eb;
	struct ocfs2_extent_list *el;
	struct ocfs2_extent_rec *insert_rec = &ctxt->rec;
	ocfs2_filesys *fs = ctxt->fs;
	struct ocfs2_extent_tree *et = ctxt->et;
	struct ocfs2_path *path = NULL;
	char *buf = *last_eb_buf;
	uint64_t last_eb_blk = ocfs2_et_get_last_eb_blk(et);

	insert->ins_split = SPLIT_NONE;

	el = et->et_root_el;
	insert->ins_tree_depth = el->l_tree_depth;

	if (el->l_tree_depth) {
		/*
		 * If we have tree depth, we read in the
		 * rightmost extent block ahead of time as
		 * ocfs2_figure_insert_type() and ocfs2_add_branch()
		 * may want it later.
		 */
		assert(buf);
		ret = ocfs2_read_extent_block(fs, last_eb_blk, buf);
		if (ret)
			goto out;

		eb = (struct ocfs2_extent_block *) buf;
		el = &eb->h_list;
	}
	/*
	 * Unless we have a contiguous insert, we'll need to know if
	 * there is room left in our allocation tree for another
	 * extent record.
	 *
	 * XXX: This test is simplistic, we can search for empty
	 * extent records too.
	 */
	*free_records = el->l_count - el->l_next_free_rec;

	if (!insert->ins_tree_depth) {
		ocfs2_figure_contig_type(fs, et, insert, el, insert_rec);
		ocfs2_figure_appending_type(insert, el, insert_rec);
		return 0;
	}

	path = ocfs2_new_path_from_et(et);
	if (!path) {
		ret = OCFS2_ET_NO_MEMORY;
		goto out;
	}
	/*
	 * In the case that we're inserting past what the tree
	 * currently accounts for, ocf2_find_path() will return for
	 * us the rightmost tree path. This is accounted for below in
	 * the appending code.
	 */
	ret = ocfs2_find_path(fs, path, insert_rec->e_cpos);
	if (ret)
		goto out;

	el = path_leaf_el(path);

	/*
	 * Now that we have the path, there's two things we want to determine:
	 * 1) Contiguousness (also set contig_index if this is so)
	 *
	 * 2) Are we doing an append? We can trivially break this up
         *     into two types of appends: simple record append, or a
         *     rotate inside the tail leaf.
	 */
	ocfs2_figure_contig_type(fs, et, insert, el, insert_rec);

	/*
	 * The insert code isn't quite ready to deal with all cases of
	 * left contiguousness. Specifically, if it's an insert into
	 * the 1st record in a leaf, it will require the adjustment of
	 * e_clusters on the last record of the path directly to it's
	 * left. For now, just catch that case and fool the layers
	 * above us. This works just fine for tree_depth == 0, which
	 * is why we allow that above.
	 */
	if (insert->ins_contig == CONTIG_LEFT &&
	    insert->ins_contig_index == 0)
		insert->ins_contig = CONTIG_NONE;

	/*
	 * Ok, so we can simply compare against last_eb to figure out
	 * whether the path doesn't exist. This will only happen in
	 * the case that we're doing a tail append, so maybe we can
	 * take advantage of that information somehow.
	 */
	if (last_eb_blk == path_leaf_blkno(path)) {
		/*
		 * Ok, ocfs2_find_path() returned us the rightmost
		 * tree path. This might be an appending insert. There are
		 * two cases:
		 *    1) We're doing a true append at the tail:
		 *	-This might even be off the end of the leaf
		 *    2) We're "appending" by rotating in the tail
		 */
		ocfs2_figure_appending_type(insert, el, insert_rec);
	}

out:
	ocfs2_free_path(path);

	return ret;
}

/*
 * Do the final bits of extent record insertion at the target leaf
 * list. If this leaf is part of an allocation tree, it is assumed
 * that the tree above has been prepared.
 */
static void ocfs2_insert_at_leaf(ocfs2_filesys *fs,
				 struct ocfs2_extent_rec *insert_rec,
				 struct ocfs2_extent_list *el,
				 struct ocfs2_insert_type *insert)
{
	int i = insert->ins_contig_index;
	unsigned int range;
	struct ocfs2_extent_rec *rec;

	assert(el->l_tree_depth == 0);

	if (insert->ins_split != SPLIT_NONE) {
		i = ocfs2_search_extent_list(el, insert_rec->e_cpos);
		assert(i != -1);
		rec = &el->l_recs[i];
		ocfs2_subtract_from_rec(fs, insert->ins_split, rec,
					insert_rec);
		goto rotate;
	}

	/*
	 * Contiguous insert - either left or right.
	 */
	if (insert->ins_contig != CONTIG_NONE) {
		rec = &el->l_recs[i];
		if (insert->ins_contig == CONTIG_LEFT) {
			rec->e_blkno = insert_rec->e_blkno;
			rec->e_cpos = insert_rec->e_cpos;
		}
		rec->e_leaf_clusters += insert_rec->e_leaf_clusters;
		return;
	}

	/*
	 * Handle insert into an empty leaf.
	 */
	if (el->l_next_free_rec == 0 ||
	    (el->l_next_free_rec == 1 &&
	     ocfs2_is_empty_extent(&el->l_recs[0]))) {
		el->l_recs[0] = *insert_rec;
		el->l_next_free_rec = 1;
		return;
	}

	/*
	 * Appending insert.
	 */
	if (insert->ins_appending == APPEND_TAIL) {
		i = el->l_next_free_rec - 1;
		rec = &el->l_recs[i];
		range = rec->e_cpos + rec->e_leaf_clusters;
		assert(insert_rec->e_cpos >= range);

		i++;
		el->l_recs[i] = *insert_rec;
		el->l_next_free_rec += 1;
		return;
	}

rotate:
	/*
	 * Ok, we have to rotate.
	 *
	 * At this point, it is safe to assume that inserting into an
	 * empty leaf and appending to a leaf have both been handled
	 * above.
	 *
	 * This leaf needs to have space, either by the empty 1st
	 * extent record, or by virtue of an l_next_rec < l_count.
	 */
	ocfs2_rotate_leaf(el, insert_rec);
}

static int ocfs2_adjust_rightmost_records(ocfs2_filesys *fs,
					  struct ocfs2_path *path,
					  struct ocfs2_extent_rec *insert_rec)
{
	int i, next_free;
	struct ocfs2_extent_list *el;
	struct ocfs2_extent_rec *rec;

	/*
	 * Update everything except the leaf block.
	 */
	for (i = 0; i < path->p_tree_depth; i++) {
		el = path->p_node[i].el;

		next_free = el->l_next_free_rec;
		if (next_free == 0)
			return OCFS2_ET_CORRUPT_EXTENT_BLOCK;

		rec = &el->l_recs[next_free - 1];

		rec->e_int_clusters = insert_rec->e_cpos;
		rec->e_int_clusters += insert_rec->e_leaf_clusters;
		rec->e_int_clusters -= rec->e_cpos;
	}

	return 0;
}

static int ocfs2_append_rec_to_path(ocfs2_filesys *fs,
				    struct ocfs2_extent_rec *insert_rec,
				    struct ocfs2_path *right_path,
				    struct ocfs2_path **ret_left_path)
{
	int ret, next_free, i;
	struct ocfs2_extent_list *el;
	struct ocfs2_path *left_path = NULL;

	*ret_left_path = NULL;

	/*
	 * This shouldn't happen for non-trees. The extent rec cluster
	 * count manipulation below only works for interior nodes.
	 */
	assert(right_path->p_tree_depth > 0);

	/*
	 * If our appending insert is at the leftmost edge of a leaf,
	 * then we might need to update the rightmost records of the
	 * neighboring path.
	 */

	el = path_leaf_el(right_path);
	next_free = el->l_next_free_rec;
	if (next_free == 0 ||
	    (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
		uint32_t left_cpos;

		ret = ocfs2_find_cpos_for_left_leaf(right_path, &left_cpos);
		if (ret)
			goto out;
		/*
		 * No need to worry if the append is already in the
		 * leftmost leaf.
		 */
		if (left_cpos) {
			left_path = ocfs2_new_path_from_path(right_path);
			if (!left_path) {
				ret = OCFS2_ET_NO_MEMORY;
				goto out;
			}

			ret = ocfs2_find_path(fs, left_path, left_cpos);
			if (ret)
				goto out;
		}
	}

	ret = ocfs2_adjust_rightmost_records(fs, right_path, insert_rec);
	if (ret)
		goto out;

	if (left_path) {
		/*
		 * Userspace sepcially.
		 * In case we have changed some blocks that is also in
		 * right_path, we have to update them in the left_path.
		 */
		i = 0;
		while (i++ < left_path->p_tree_depth)
			if (left_path->p_node[i].blkno ==
			    right_path->p_node[i].blkno)
				memcpy(left_path->p_node[i].buf,
				       right_path->p_node[i].buf,
				       fs->fs_blocksize);
	}

	*ret_left_path = left_path;
	ret = 0;
out:
	if (ret)
		ocfs2_free_path(left_path);
	return ret;
}

static void ocfs2_split_record(ocfs2_filesys *fs,
			       struct ocfs2_path *left_path,
			       struct ocfs2_path *right_path,
			       struct ocfs2_extent_rec *split_rec,
			       enum ocfs2_split_type split)
{
	int index;
	uint32_t cpos = split_rec->e_cpos;
	struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
	struct ocfs2_extent_rec *rec, *tmprec;

	right_el = path_leaf_el(right_path);;
	if (left_path)
		left_el = path_leaf_el(left_path);

	el = right_el;
	insert_el = right_el;
	index = ocfs2_search_extent_list(el, cpos);
	if (index != -1) {
		if (index == 0 && left_path) {
			assert(!ocfs2_is_empty_extent(&el->l_recs[0]));

			/*
			 * This typically means that the record
			 * started in the left path but moved to the
			 * right as a result of rotation. We either
			 * move the existing record to the left, or we
			 * do the later insert there.
			 *
			 * In this case, the left path should always
			 * exist as the rotate code will have passed
			 * it back for a post-insert update.
			 */

			if (split == SPLIT_LEFT) {
				/*
				 * It's a left split. Since we know
				 * that the rotate code gave us an
				 * empty extent in the left path, we
				 * can just do the insert there.
				 */
				insert_el = left_el;
			} else {
				/*
				 * Right split - we have to move the
				 * existing record over to the left
				 * leaf. The insert will be into the
				 * newly created empty extent in the
				 * right leaf.
				 */
				tmprec = &right_el->l_recs[index];
				ocfs2_rotate_leaf(left_el, tmprec);
				el = left_el;

				memset(tmprec, 0, sizeof(*tmprec));
				index = ocfs2_search_extent_list(left_el, cpos);
				assert(index != -1);
			}
		}
	} else {
		assert(left_path);
		assert(ocfs2_is_empty_extent(&left_el->l_recs[0]));
		/*
		 * Left path is easy - we can just allow the insert to
		 * happen.
		 */
		el = left_el;
		insert_el = left_el;
		index = ocfs2_search_extent_list(el, cpos);
		assert(index != -1);
	}

	rec = &el->l_recs[index];
	ocfs2_subtract_from_rec(fs, split, rec, split_rec);
	ocfs2_rotate_leaf(insert_el, split_rec);
}

/*
 * This function only does inserts on an allocation b-tree. For dinode
 * lists, ocfs2_insert_at_leaf() is called directly.
 *
 * right_path is the path we want to do the actual insert
 * in. left_path should only be passed in if we need to update that
 * portion of the tree after an edge insert.
 */
static int ocfs2_insert_path(ocfs2_filesys *fs,
			     struct ocfs2_path *left_path,
			     struct ocfs2_path *right_path,
			     struct ocfs2_extent_rec *insert_rec,
			     struct ocfs2_insert_type *insert)
{
	int ret, subtree_index;

	if (insert->ins_split != SPLIT_NONE) {
		/*
		 * We could call ocfs2_insert_at_leaf() for some types
		 * of splits, but it's easier to just let one seperate
		 * function sort it all out.
		 */
		ocfs2_split_record(fs, left_path, right_path,
				   insert_rec, insert->ins_split);
	} else
		ocfs2_insert_at_leaf(fs, insert_rec, path_leaf_el(right_path),
				     insert);

	if (left_path) {
		/*
		 * The rotate code has indicated that we need to fix
		 * up portions of the tree after the insert.
		 */
		subtree_index = ocfs2_find_subtree_root(left_path, right_path);
		ocfs2_complete_edge_insert(fs, left_path,
				        right_path, subtree_index);
	} else
		subtree_index = 0;

	ret = ocfs2_sync_path_to_disk(fs, left_path,
				      right_path, subtree_index);
	if (ret)
		goto out;

	ret = 0;
out:
	return ret;
}

static int ocfs2_do_insert_extent(struct insert_ctxt* ctxt,
				  struct ocfs2_insert_type *type)
{
	int ret, rotate = 0;
	uint32_t cpos;
	struct ocfs2_path *right_path = NULL;
	struct ocfs2_path *left_path = NULL;
	struct ocfs2_extent_rec *insert_rec = &ctxt->rec;
	ocfs2_filesys *fs = ctxt->fs;
	struct ocfs2_extent_list *el = ctxt->et->et_root_el;

	if (el->l_tree_depth == 0) {
		ocfs2_insert_at_leaf(fs, insert_rec, el, type);
		goto out_update_clusters;
	}

	right_path = ocfs2_new_path_from_et(ctxt->et);
	if (!right_path) {
		ret = OCFS2_ET_NO_MEMORY;
		goto out;
	}

	/*
	 * Determine the path to start with. Rotations need the
	 * rightmost path, everything else can go directly to the
	 * target leaf.
	 */
	cpos = insert_rec->e_cpos;
	if (type->ins_appending == APPEND_NONE &&
	    type->ins_contig == CONTIG_NONE) {
		rotate = 1;
		cpos = UINT_MAX;
	}

	ret = ocfs2_find_path(fs, right_path, cpos);
	if (ret)
		goto out;

	/*
	 * Rotations and appends need special treatment - they modify
	 * parts of the tree's above them.
	 *
	 * Both might pass back a path immediate to the left of the
	 * one being inserted to. This will be cause
	 * ocfs2_insert_path() to modify the rightmost records of
	 * left_path to account for an edge insert.
	 *
	 * XXX: When modifying this code, keep in mind that an insert
	 * can wind up skipping both of these two special cases...
	 */

	if (rotate) {
		ret = ocfs2_rotate_tree_right(fs, type->ins_split,
					      insert_rec->e_cpos,
					      right_path, &left_path);
		if (ret)
			goto out;
	} else if (type->ins_appending == APPEND_TAIL
		   && type->ins_contig != CONTIG_LEFT) {
		ret = ocfs2_append_rec_to_path(fs, insert_rec,
					       right_path, &left_path);
		if (ret)
			goto out;
 	}

	ret = ocfs2_insert_path(fs, left_path, right_path, insert_rec, type);
	if (ret)
		goto out;

out_update_clusters:
	if (type->ins_split == SPLIT_NONE)
		ocfs2_et_update_clusters(ctxt->et, insert_rec->e_leaf_clusters);
	ret = 0;

out:
	ocfs2_free_path(left_path);
	ocfs2_free_path(right_path);

	return ret;
}

struct duplicate_ctxt {
	struct ocfs2_extent_tree *et;
	uint64_t next_leaf_blk;
};

static errcode_t duplicate_extent_block(ocfs2_filesys *fs,
					struct ocfs2_extent_list *old_el,
					struct ocfs2_extent_list *new_el,
					struct duplicate_ctxt *ctxt)
{
	int i;
	errcode_t ret;
	uint64_t blkno, new_blkno;
	struct ocfs2_extent_rec *rec = NULL;
	char *eb_buf = NULL, *new_eb_buf = NULL;
	struct ocfs2_extent_block *eb = NULL;
	struct ocfs2_extent_list *child_old_el = NULL, *child_new_el = NULL;

	assert (old_el->l_tree_depth > 0);

	/* empty the whole extent list at first. */
	*new_el = *old_el;
	new_el->l_next_free_rec = 0;
	memset(new_el->l_recs, 0,
	       sizeof(struct ocfs2_extent_rec) * new_el->l_count);

	if (old_el->l_next_free_rec == 0) {
		/* XXX:
		 * We have a tree depth > 0 and no extent record in it,
		 * should it be a corrupted block?
		 */
		ret = OCFS2_ET_CORRUPT_EXTENT_BLOCK;
		goto bail;
	}

	ret = ocfs2_malloc_block(fs->fs_io, &eb_buf);
	if (ret)
		goto bail;
	ret = ocfs2_malloc_block(fs->fs_io, &new_eb_buf);
	if (ret)
		goto bail;

	/* we iterate the extent list from the last one for recording
	 * the next_leaf_blk for the previous leaf.
	 */
	for (i = old_el->l_next_free_rec - 1; i >= 0; i--) {
		rec = &old_el->l_recs[i];

		if (!ocfs2_rec_clusters(old_el->l_tree_depth, rec))
			continue;

		blkno = rec->e_blkno;
		ret = ocfs2_read_extent_block(fs, blkno, eb_buf);
		if (ret)
			goto bail;

		/* First make the new_buf the same as the old buf. */
		memcpy(new_eb_buf, eb_buf, fs->fs_blocksize);

		eb = (struct ocfs2_extent_block *)eb_buf;
		child_old_el = &eb->h_list;
		eb = (struct ocfs2_extent_block *)new_eb_buf;
		child_new_el = &eb->h_list;

		if (child_old_el->l_tree_depth > 0) {
			/* the extent record in our list still has child extent
			 * block, so we have to iterate it.
			 */
			ret = duplicate_extent_block(fs,
						     child_old_el,
						     child_new_el,
						     ctxt);
			if (ret)
				goto bail;
		}

		/* now we allocate a new extent block and save it. */
		ret = ocfs2_new_extent_block(fs, &new_blkno);
		if (ret)
			goto bail;

		eb = (struct ocfs2_extent_block *)new_eb_buf;
		eb->h_blkno = new_blkno;
		if (child_old_el->l_tree_depth == 0) {
			/*
			 * This is the leaf blkno, we have to set its
			 * h_next_leaf_blk and then record itself for
			 * future use.
			 */
			eb->h_next_leaf_blk = ctxt->next_leaf_blk;
			ctxt->next_leaf_blk = new_blkno;
		}

		ret = ocfs2_write_extent_block(fs, new_blkno, new_eb_buf);
		if (ret)
			goto bail;

		memcpy(&new_el->l_recs[i], rec, sizeof(struct ocfs2_extent_rec));
		new_el->l_recs[i].e_blkno = new_blkno;

		eb = (struct ocfs2_extent_block *)new_eb_buf;
		/* set the new i_last_eb_blk in the new dinode. */
		if (ocfs2_et_get_last_eb_blk(ctxt->et) == blkno)
			ocfs2_et_set_last_eb_blk(ctxt->et, new_blkno);
	}

	new_el->l_next_free_rec = old_el->l_next_free_rec;
	ret = 0;

bail:
	if (eb_buf)
		ocfs2_free(&eb_buf);
	if (new_eb_buf)
		ocfs2_free(&new_eb_buf);
	/* Free all the extent block we allocate. */
	if (ret) {
		for (i = 0; i < old_el->l_next_free_rec; i++) {
			rec = &new_el->l_recs[i];
			if (rec->e_blkno)
				ocfs2_delete_extent_block(fs, rec->e_blkno);
		}
	}

	return ret;
}

static errcode_t duplicate_extent_block_et(ocfs2_filesys *fs,
					   struct ocfs2_extent_tree *et)
{
	errcode_t ret = 0;
	struct ocfs2_extent_list *old_el = NULL, *new_el = NULL;
	char *old_buf, *new_buf;
	struct duplicate_ctxt ctxt;

	ret = ocfs2_malloc_block(fs->fs_io, &old_buf);
	if (ret)
		return ret;

	memcpy(old_buf, et->et_root_buf, fs->fs_blocksize);
	new_buf = et->et_root_buf;
	new_el = et->et_root_el;
	old_el = (struct ocfs2_extent_list *)
			(old_buf + ((char *)new_el - new_buf));

	assert(old_el->l_tree_depth > 0);

	/* empty the whole extent list at first. */
	*new_el = *old_el;
	memset(new_el->l_recs, 0,
	       sizeof(struct ocfs2_extent_rec) * new_el->l_count);
	new_el->l_next_free_rec = 0;

	memset(&ctxt, 0, sizeof(ctxt));
	ctxt.et = et;
	ctxt.next_leaf_blk = 0;
	ret = duplicate_extent_block(fs, old_el, new_el, &ctxt);

	ocfs2_free(&old_buf);
	return ret;
}

static void free_duplicated_extent_block(ocfs2_filesys *fs,
					struct ocfs2_extent_list *el)
{
	int i;
	errcode_t ret;
	char *buf = NULL;
	struct ocfs2_extent_rec *rec;
	struct ocfs2_extent_list *child_el;
	struct ocfs2_extent_block *eb;

	assert(el->l_tree_depth > 0);

	ret = ocfs2_malloc_block(fs->fs_io, &buf);
	if (ret)
		return;

	for (i = 0; i < el->l_next_free_rec; i ++) {
		rec = &el->l_recs[i];

		if (!ocfs2_rec_clusters(el->l_tree_depth, rec))
			continue;

		ret = ocfs2_read_extent_block(fs, rec->e_blkno, buf);
		if (ret)
			continue;

		eb = (struct ocfs2_extent_block *)buf;
		child_el = &eb->h_list;
		if (child_el->l_tree_depth > 0)
			free_duplicated_extent_block(fs, child_el);

		ocfs2_delete_extent_block(fs, rec->e_blkno);
	}

	if(buf)
		ocfs2_free(&buf);
}

/*
 * Grow a b-tree so that it has more records.
 *
 * We might shift the tree depth in which case existing paths should
 * be considered invalid.
 *
 * Tree depth after the grow is returned via *final_depth.
 *
 * *last_eb will be updated by ocfs2_add_branch().
 */
static int ocfs2_grow_tree(ocfs2_filesys *fs,
			   struct ocfs2_extent_tree *et,
			   int *final_depth, char **last_eb)
{
	errcode_t ret;
	char *eb_buf = NULL;
	int shift;
	struct ocfs2_extent_list *el = et->et_root_el;
	int depth = el->l_tree_depth;

	shift = ocfs2_find_branch_target(fs, et, &eb_buf);
	if (shift < 0) {
		ret = shift;
		goto out;
	}

	/* We traveled all the way to the bottom of the allocation tree
	 * and didn't find room for any more extents - we need to add
	 * another tree level */
	if (shift) {

		/* shift_tree_depth will return us a buffer with
		 * the new extent block (so we can pass that to
		 * ocfs2_add_branch). */
		ret = shift_tree_depth(fs, et, &eb_buf);
		if (ret)
			goto out;

		depth++;
		if (depth == 1) {
			/*
			 * Special case: we have room now if we shifted from
			 * tree_depth 0, so no more work needs to be done.
			 *
			 * We won't be calling add_branch, so pass
			 * back *last_eb as the new leaf.
			 */
			assert(*last_eb);
			memcpy(*last_eb, eb_buf, fs->fs_blocksize);
			goto out;
		}
	}

	/* call ocfs2_add_branch to add the final part of the tree with
	 * the new data. */
	ret = ocfs2_add_branch(fs, et, eb_buf, last_eb);

out:
	if (final_depth)
		*final_depth = depth;
	return ret;
}

errcode_t ocfs2_tree_insert_extent(ocfs2_filesys *fs,
				   struct ocfs2_extent_tree *et,
				   uint32_t cpos, uint64_t c_blkno,
				   uint32_t clusters, uint16_t flag)
{
	errcode_t ret;
	struct insert_ctxt ctxt;
	struct ocfs2_insert_type insert = {0, };
	char *last_eb = NULL;
	char *backup_buf = NULL;
	char *root_buf = et->et_root_buf;
	int free_records = 0;

	ctxt.fs = fs;
	ctxt.et = et;

	/* In order to orderize the written block sequence and avoid
	 * the corruption for the b-tree, we duplicate the extent block
	 * here and do the insertion in the duplicated ones.
	 *
	 * Note: we only do this in case the b-tree has extent blocks.
	 * And if the duplicate process fails, we should go on the normal
	 * insert process.
	 */
	if (et->et_root_el->l_tree_depth) {
		ret = ocfs2_malloc_block(fs->fs_io, &backup_buf);
		if (ret)
			goto bail;

		memcpy(backup_buf, root_buf, fs->fs_blocksize);

		/* duplicate the extent block. If it succeeds, di_buf
		 * will point to the new allocated extent blocks, and
		 * the following insertion will happens to the new ones.
		 */
		ret = duplicate_extent_block_et(fs, et);
		if (ret) {
			memcpy(root_buf, backup_buf, fs->fs_blocksize);
			ocfs2_free(&backup_buf);
			backup_buf = NULL;
		}
	}

	memset(&ctxt.rec, 0, sizeof(struct ocfs2_extent_rec));
	ctxt.rec.e_cpos = cpos;
	ctxt.rec.e_blkno = c_blkno;
	ctxt.rec.e_leaf_clusters = clusters;
	ctxt.rec.e_flags = flag;

	ret = ocfs2_malloc_block(fs->fs_io, &last_eb);
	if (ret)
		return ret;

	ret = ocfs2_figure_insert_type(&ctxt, &last_eb, &free_records, &insert);
	if (ret)
		goto bail;

	if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
		ret = ocfs2_grow_tree(fs, ctxt.et,
				      &insert.ins_tree_depth, &last_eb);
		if (ret)
			goto bail;
	}

	/* Finally, we can add clusters. This might rotate the tree for us. */
	ret = ocfs2_do_insert_extent(&ctxt, &insert);
	if (ret)
		goto bail;

bail:
	if (backup_buf) {
		struct ocfs2_extent_list *free_el;
		int offset = (char *)et->et_root_el - et->et_root_buf;
		/* we have duplicated the extent block during the insertion.
		 * so if it succeeds, we should free the old ones, and if fails,
		 * the duplicate ones should be freed.
		 */

		if (ret)
			free_el = (struct ocfs2_extent_list *)
						(et->et_root_buf + offset);
		else
			free_el = (struct ocfs2_extent_list *)
						(backup_buf + offset);

		free_duplicated_extent_block(fs, free_el);
		ocfs2_free(&backup_buf);
	}

	if (last_eb)
		ocfs2_free(&last_eb);

	/*
	 * Write the root buffer here.
	 * If the caller don't initialize the write function, it should
	 * be responsible for write the root buffer.
	 */
	if (!ret && et->et_root_write)
		ret = et->et_root_write(fs, et->et_root_blkno, root_buf);

	return ret;
}

static void ocfs2_make_right_split_rec(ocfs2_filesys *fs,
				       struct ocfs2_extent_rec *split_rec,
				       uint32_t cpos,
				       struct ocfs2_extent_rec *rec)
{
	uint32_t rec_cpos = rec->e_cpos;
	uint32_t rec_range = rec_cpos + rec->e_leaf_clusters;

	memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));

	split_rec->e_cpos = cpos;
	split_rec->e_leaf_clusters = rec_range - cpos;

	split_rec->e_blkno = rec->e_blkno;
	split_rec->e_blkno += ocfs2_clusters_to_blocks(fs, cpos - rec_cpos);

	split_rec->e_flags = rec->e_flags;
}

static int ocfs2_split_and_insert(struct insert_ctxt *ctxt,
				  struct ocfs2_path *path,
				  char **last_eb_buf,
				  int split_index,
				  struct ocfs2_extent_rec *orig_split_rec)
{
	int ret = 0, depth;
	unsigned int insert_range, rec_range, do_leftright = 0;
	struct ocfs2_extent_rec tmprec;
	struct ocfs2_extent_list *rightmost_el;
	struct ocfs2_extent_rec rec;
	struct ocfs2_insert_type insert;
	struct ocfs2_extent_block *eb;

leftright:
	/*
	 * Store a copy of the record on the stack - it might move
	 * around as the tree is manipulated below.
	 */
	rec = path_leaf_el(path)->l_recs[split_index];

	rightmost_el = ctxt->et->et_root_el;

	depth = rightmost_el->l_tree_depth;
	if (depth) {
		assert(*last_eb_buf);
		eb = (struct ocfs2_extent_block *) (*last_eb_buf);
		rightmost_el = &eb->h_list;
	}

	if (rightmost_el->l_next_free_rec == rightmost_el->l_count) {
		ret = ocfs2_grow_tree(ctxt->fs, ctxt->et,
				      &depth, last_eb_buf);
		if (ret)
			goto out;
	}

	memset(&insert, 0, sizeof(struct ocfs2_insert_type));
	insert.ins_appending = APPEND_NONE;
	insert.ins_contig = CONTIG_NONE;
	insert.ins_tree_depth = depth;

	insert_range = ctxt->rec.e_cpos + ctxt->rec.e_leaf_clusters;
	rec_range = rec.e_cpos + rec.e_leaf_clusters;

	if (ctxt->rec.e_cpos == rec.e_cpos) {
		insert.ins_split = SPLIT_LEFT;
	} else if (insert_range == rec_range) {
		insert.ins_split = SPLIT_RIGHT;
	} else {
		/*
		 * Left/right split. We fake this as a right split
		 * first and then make a second pass as a left split.
		 */
		insert.ins_split = SPLIT_RIGHT;

		ocfs2_make_right_split_rec(ctxt->fs, &tmprec, insert_range,
					   &rec);

		ctxt->rec = tmprec;

		assert(!do_leftright);
		do_leftright = 1;
	}

	ret = ocfs2_do_insert_extent(ctxt, &insert);
	if (ret)
		goto out;

	if (do_leftright == 1) {
		uint32_t cpos;
		struct ocfs2_extent_list *el;

		do_leftright++;
		ctxt->rec = *orig_split_rec;

		ocfs2_reinit_path(path, 1);

		cpos = ctxt->rec.e_cpos;
		ret = ocfs2_find_path(ctxt->fs, path, cpos);
		if (ret)
			goto out;

		el = path_leaf_el(path);
		split_index = ocfs2_search_extent_list(el, cpos);
		goto leftright;
	}
out:

	return ret;
}

/*
 * Split part or all of the extent record at split_index in the leaf
 * pointed to by path. Merge with the contiguous extent record if needed.
 *
 * Care is taken to handle contiguousness so as to not grow the tree.
 *
 * last_eb_buf should be the rightmost leaf block for any inode with a
 * btree. Since a split may grow the tree or a merge might shrink it,
 * the caller cannot trust the contents of that buffer after this call.
 *
 * This code is optimized for readability - several passes might be
 * made over certain portions of the tree.
 */
static int ocfs2_split_extent(struct insert_ctxt *insert_ctxt,
			      struct ocfs2_path *path,
			      int split_index)
{
	int ret = 0;
	struct ocfs2_extent_rec split_rec = insert_ctxt->rec;
	struct ocfs2_extent_list *el = path_leaf_el(path);
	char *last_eb_buf = NULL;
	struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
	struct ocfs2_merge_ctxt merge_ctxt;
	ocfs2_filesys *fs = insert_ctxt->fs;

	if (rec->e_cpos > split_rec.e_cpos ||
	    ((rec->e_cpos + rec->e_leaf_clusters) <
	     (split_rec.e_cpos + split_rec.e_leaf_clusters))) {
		ret = OCFS2_ET_INVALID_ARGUMENT;
		goto out;
	}

	merge_ctxt.c_contig_type =
		ocfs2_figure_merge_contig_type(fs, insert_ctxt->et, el,
					       split_index, &split_rec);

	/*
	 * We have to allocate the last_eb_buf no matter the current tree
	 * depth is since we may shift the tree depth from 0 to 1 in
	 * ocfs2_split_and_insert and use last_eb_buf to store.
	 */
	ret = ocfs2_malloc_block(fs->fs_io, &last_eb_buf);
	if (ret)
		goto out;
	/*
	 * The core merge / split code wants to know how much room is
	 * left in this inodes allocation tree, so we pass the
	 * rightmost extent list.
	 */
	if (path->p_tree_depth) {
		ret = ocfs2_read_extent_block(fs,
				ocfs2_et_get_last_eb_blk(insert_ctxt->et),
				last_eb_buf);
		if (ret)
			goto out;
	}

	if (rec->e_cpos == split_rec.e_cpos &&
	    rec->e_leaf_clusters == split_rec.e_leaf_clusters)
		merge_ctxt.c_split_covers_rec = 1;
	else
		merge_ctxt.c_split_covers_rec = 0;

	merge_ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);

	if (merge_ctxt.c_contig_type == CONTIG_NONE) {
		if (merge_ctxt.c_split_covers_rec) {
			el->l_recs[split_index] = split_rec;
			/*
			 * We only write the leaf block, and leave
			 * the write of the root to the caller.
			 */
			if (path->p_tree_depth)
				ret = ocfs2_write_extent_block(fs,
							path_leaf_blkno(path),
							path_leaf_buf(path));
		} else
			ret = ocfs2_split_and_insert(insert_ctxt, path,
						     &last_eb_buf, split_index,
						     &split_rec);
	} else {
		ret = ocfs2_try_to_merge_extent(fs, insert_ctxt->et, path,
						split_index, &split_rec,
						&merge_ctxt);
	}

out:
	if (last_eb_buf)
		ocfs2_free(&last_eb_buf);
	return ret;
}

/*
 * Change the flags of the already-existing extent at cpos for len clusters.
 *
 * new_flags: the flags we want to set.
 * clear_flags: the flags we want to clear.
 * p_blkno: the new physical offset we want this new extent starts from.
 *
 * If the existing extent is larger than the request, initiate a
 * split. An attempt will be made at merging with adjacent extents.
 */
int ocfs2_change_extent_flag(ocfs2_filesys *fs,
			     struct ocfs2_extent_tree *et,
			     uint32_t cpos, uint32_t len,
			     uint64_t p_blkno,
			     int new_flags, int clear_flags)
{
	int ret, index;
	struct ocfs2_path *left_path = NULL;
	struct ocfs2_extent_list *el;
	struct insert_ctxt ctxt;
	struct ocfs2_extent_rec *rec;
	char *backup_buf = NULL;

	/* In order to orderize the written block sequence and avoid
	 * the corruption for the inode, we duplicate the extent block
	 * here and do the insertion in the duplicated ones.
	 *
	 * Note: we only do this in case the file has extent blocks.
	 * And if the duplicate process fails, we should go on the normal
	 * insert process.
	 */
	if (et->et_root_el->l_tree_depth) {
		ret = ocfs2_malloc_block(fs->fs_io, &backup_buf);
		if (ret)
			goto out;

		memcpy(backup_buf, et->et_root_buf, fs->fs_blocksize);

		/* duplicate the extent block. If it succeeds, di_buf
		 * will point to the new allocated extent blocks, and
		 * the following insertion will happens to the new ones.
		 */
		ret = duplicate_extent_block_et(fs, et);
		if (ret) {
			memcpy(et->et_root_buf, backup_buf, fs->fs_blocksize);
			ocfs2_free(&backup_buf);
			backup_buf = NULL;
		}
	}

	left_path = ocfs2_new_path_from_et(et);
	if (!left_path) {
		ret = OCFS2_ET_NO_MEMORY;
		goto out;
	}

	ret = ocfs2_find_path(fs, left_path, cpos);
	if (ret)
		goto out;

	el = path_leaf_el(left_path);

	index = ocfs2_search_extent_list(el, cpos);
	if (index == -1 || index >= el->l_next_free_rec) {
		ret = OCFS2_ET_CORRUPT_EXTENT_BLOCK;
		goto out;
	}

	ctxt.fs = fs;
	ctxt.et = et;

	ret = OCFS2_ET_IO;
	rec = &el->l_recs[index];
	if (new_flags && (rec->e_flags & new_flags))
		goto out;

	if (clear_flags && !(rec->e_flags & clear_flags))
		goto out;

	memset(&ctxt.rec, 0, sizeof(struct ocfs2_extent_rec));
	ctxt.rec.e_cpos = cpos;
	ctxt.rec.e_leaf_clusters = len;
	ctxt.rec.e_blkno = p_blkno;
	ctxt.rec.e_flags = rec->e_flags;
	if (new_flags)
		ctxt.rec.e_flags |= new_flags;
	if (clear_flags)
		ctxt.rec.e_flags &= ~clear_flags;

	ret = ocfs2_split_extent(&ctxt, left_path, index);
	if (ret)
		goto out;

	/*
	 * Write the root buffer here.
	 * If the caller don't initialize the write function, it should
	 * be responsible for write the root buffer.
	 */
	if (!ret && et->et_root_write)
		ret = et->et_root_write(fs, et->et_root_blkno, et->et_root_buf);

out:
	if (backup_buf) {
		struct ocfs2_extent_list *free_el;
		int offset = (char *)et->et_root_el - et->et_root_buf;
		/* we have duplicated the extent block during the insertion.
		 * so if it succeeds, we should free the old ones, and if fails,
		 * the duplicate ones should be freed.
		 */

		if (ret)
			free_el = (struct ocfs2_extent_list *)
					(et->et_root_buf + offset);
		else
			free_el = (struct ocfs2_extent_list *)
						(backup_buf + offset);

		free_duplicated_extent_block(fs, free_el);
		ocfs2_free(&backup_buf);
	}
	ocfs2_free_path(left_path);
	return ret;
}

static int ocfs2_split_tree(ocfs2_filesys *fs,
			    struct ocfs2_extent_tree *et,
			    struct ocfs2_path *path,
			    int index, uint32_t new_range)
{
	errcode_t ret;
	int depth;
	char *last_eb_buf = NULL;
	struct ocfs2_extent_block *eb;
	struct ocfs2_extent_list *rightmost_el, *el;
	struct ocfs2_extent_rec *rec;
	struct ocfs2_insert_type insert;
	struct insert_ctxt ctxt;

	ctxt.fs = fs;
	ctxt.et = et;

	ret = ocfs2_malloc_block(fs->fs_io, &last_eb_buf);
	if (ret)
		return ret;
	/*
	 * Setup the record to split before we grow the tree.
	 */
	el = path_leaf_el(path);
	rec = &el->l_recs[index];
	ocfs2_make_right_split_rec(fs, &ctxt.rec, new_range, rec);

	depth = path->p_tree_depth;
	if (depth > 0) {
		ret = ocfs2_read_extent_block(fs,
					      ocfs2_et_get_last_eb_blk(et),
					      last_eb_buf);
		if (ret)
			goto out;

		eb = (struct ocfs2_extent_block *)last_eb_buf;
		rightmost_el = &eb->h_list;
	} else
		rightmost_el = path_leaf_el(path);

	if (rightmost_el->l_next_free_rec == rightmost_el->l_count) {
		ret = ocfs2_grow_tree(fs, et, &depth, &last_eb_buf);
		if (ret)
			goto out;
	}

	memset(&insert, 0, sizeof(struct ocfs2_insert_type));
	insert.ins_appending = APPEND_NONE;
	insert.ins_contig = CONTIG_NONE;
	insert.ins_split = SPLIT_RIGHT;
	insert.ins_tree_depth = depth;

	ret = ocfs2_do_insert_extent(&ctxt, &insert);

out:
	if (last_eb_buf)
		ocfs2_free(&last_eb_buf);
	return ret;
}

static int ocfs2_truncate_rec(ocfs2_filesys *fs,
			      struct ocfs2_extent_tree *et,
			      struct ocfs2_path *path, int index,
			      uint32_t cpos, uint32_t len)
{
	errcode_t ret;
	uint32_t left_cpos, rec_range, trunc_range;
	int is_rightmost_tree_rec = 0;
	struct ocfs2_path *left_path = NULL;
	struct ocfs2_extent_list *el = path_leaf_el(path);
	struct ocfs2_extent_rec *rec;
	struct ocfs2_extent_block *eb;

	if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
		ret = ocfs2_rotate_tree_left(fs, et, path);
		if (ret)
			goto out;

		index--;
	}

	if (index == el->l_next_free_rec - 1 && path->p_tree_depth) {
		/*
		 * Check whether this is the rightmost tree record. If
		 * we remove all of this record or part of its right
		 * edge then an update of the record lengths above it
		 * will be required.
		 */
		eb = (struct ocfs2_extent_block *)path_leaf_buf(path);
		if (eb->h_next_leaf_blk == 0)
			is_rightmost_tree_rec = 1;
	}

	rec = &el->l_recs[index];
	if (index == 0 && path->p_tree_depth && rec->e_cpos == cpos) {
		/*
		 * Changing the leftmost offset (via partial or whole
		 * record truncate) of an interior (or rightmost) path
		 * means we have to update the subtree that is formed
		 * by this leaf and the one to it's left.
		 *
		 * There are two cases we can skip:
		 *   1) Path is the leftmost one in our btree.
		 *   2) The leaf is rightmost and will be empty after
		 *      we remove the extent record - the rotate code
		 *      knows how to update the newly formed edge.
		 */

		ret = ocfs2_find_cpos_for_left_leaf(path, &left_cpos);
		if (ret)
			goto out;

		if (left_cpos && el->l_next_free_rec > 1) {
			left_path = ocfs2_new_path_from_path(path);
			if (!left_path) {
				ret = OCFS2_ET_NO_MEMORY;
				goto out;
			}

			ret = ocfs2_find_path(fs, left_path, left_cpos);
			if (ret)
				goto out;
		}
	}

	rec_range = rec->e_cpos + ocfs2_rec_clusters(el->l_tree_depth, rec);
	trunc_range = cpos + len;

	if (rec->e_cpos == cpos && rec_range == trunc_range) {
		int next_free;

		memset(rec, 0, sizeof(*rec));
		ocfs2_cleanup_merge(el, index);

		next_free = el->l_next_free_rec;
		if (is_rightmost_tree_rec && next_free > 1) {
			/*
			 * We skip the edge update if this path will
			 * be deleted by the rotate code.
			 */
			rec = &el->l_recs[next_free - 1];
			ocfs2_adjust_rightmost_records(fs, path, rec);
		}
	} else if (rec->e_cpos == cpos) {
		/* Remove leftmost portion of the record. */
		rec->e_cpos += len;
		rec->e_blkno += ocfs2_clusters_to_blocks(fs, len);
		rec->e_leaf_clusters -= len;
	} else if (rec_range == trunc_range) {
		/* Remove rightmost portion of the record */
		rec->e_leaf_clusters -= len;
		if (is_rightmost_tree_rec)
			ocfs2_adjust_rightmost_records(fs, path, rec);
	} else {
		/* Caller should have trapped this. */
		assert(0);
	}

	if (left_path) {
		int subtree_index;

		subtree_index = ocfs2_find_subtree_root(left_path, path);
		ocfs2_complete_edge_insert(fs, left_path, path,
					   subtree_index);
	}

	ret = ocfs2_rotate_tree_left(fs, et, path);
out:
	ocfs2_free_path(left_path);
	return ret;
}

int ocfs2_remove_extent(ocfs2_filesys *fs,
			struct ocfs2_extent_tree *et,
			uint32_t cpos, uint32_t len)
{
	int ret, index;
	uint32_t rec_range, trunc_range;
	struct ocfs2_extent_rec *rec;
	struct ocfs2_extent_list *el;
	struct ocfs2_path *path = NULL;

	path = ocfs2_new_path_from_et(et);
	if (!path) {
		ret = OCFS2_ET_NO_MEMORY;
		goto out;
	}

	ret = ocfs2_find_path(fs, path, cpos);
	if (ret)
		goto out;

	el = path_leaf_el(path);
	index = ocfs2_search_extent_list(el, cpos);
	if (index == -1 || index >= el->l_next_free_rec) {
		ret = OCFS2_ET_CORRUPT_EXTENT_BLOCK;
		goto out;
	}

	/*
	 * We have 3 cases of extent removal:
	 *   1) Range covers the entire extent rec
	 *   2) Range begins or ends on one edge of the extent rec
	 *   3) Range is in the middle of the extent rec (no shared edges)
	 *
	 * For case 1 we remove the extent rec and left rotate to
	 * fill the hole.
	 *
	 * For case 2 we just shrink the existing extent rec, with a
	 * tree update if the shrinking edge is also the edge of an
	 * extent block.
	 *
	 * For case 3 we do a right split to turn the extent rec into
	 * something case 2 can handle.
	 */
	rec = &el->l_recs[index];
	rec_range = rec->e_cpos + ocfs2_rec_clusters(el->l_tree_depth, rec);
	trunc_range = cpos + len;

	assert(cpos >= rec->e_cpos && trunc_range <= rec_range);

	if (rec->e_cpos == cpos || rec_range == trunc_range) {
		ret = ocfs2_truncate_rec(fs, et, path, index, cpos, len);
		if (ret)
			goto out;
	} else {
		ret = ocfs2_split_tree(fs, et, path, index, trunc_range);
		if (ret)
			goto out;

		/*
		 * The split could have manipulated the tree enough to
		 * move the record location, so we have to look for it again.
		 */
		ocfs2_reinit_path(path, 1);

		ret = ocfs2_find_path(fs, path, cpos);
		if (ret)
			goto out;

		el = path_leaf_el(path);
		index = ocfs2_search_extent_list(el, cpos);
		if (index == -1 || index >= el->l_next_free_rec) {
			ret = OCFS2_ET_CORRUPT_EXTENT_BLOCK;
			goto out;
		}

		/*
		 * Double check our values here. If anything is fishy,
		 * it's easier to catch it at the top level.
		 */
		rec = &el->l_recs[index];
		rec_range = rec->e_cpos +
				ocfs2_rec_clusters(el->l_tree_depth, rec);
		if (rec_range != trunc_range) {
			ret = OCFS2_ET_CORRUPT_EXTENT_BLOCK;
			goto out;
		}

		ret = ocfs2_truncate_rec(fs, et, path, index, cpos, len);
		if (ret)
			goto out;
	}

out:
	ocfs2_free_path(path);
	return ret;
}