File: tree.c

package info (click to toggle)
reiser4progs 1.0.6-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 5,348 kB
  • ctags: 3,714
  • sloc: ansic: 33,468; sh: 8,489; makefile: 1,012
file content (3319 lines) | stat: -rw-r--r-- 91,283 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
/* Copyright (C) 2001-2005 by Hans Reiser, licensing governed by
   reiser4progs/COPYING.
   
   tree.c -- reiser4 tree code. */

#include <reiser4/libreiser4.h>

/* Return current fs blksize, which may be used in tree. */
uint32_t reiser4_tree_get_blksize(reiser4_tree_t *tree) {
	aal_assert("umka-2579", tree != NULL);
	aal_assert("umka-2580", tree->fs != NULL);
	aal_assert("umka-2581", tree->fs->master != NULL);

	return reiser4_master_get_blksize(tree->fs->master);
}

/* Returns TRUE if passed @node is tree root node. */
static bool_t reiser4_tree_root_node(reiser4_tree_t *tree,
				     reiser4_node_t *node)
{
	aal_assert("umka-2482", tree != NULL);
	aal_assert("umka-2483", node != NULL);
	
	return reiser4_tree_get_root(tree) == node->block->nr;
}

#ifndef ENABLE_MINIMAL
/* Updates root block number in format by passed @blk. Takes care about correct
   block number in loaded root node if any. */
void reiser4_tree_set_root(reiser4_tree_t *tree, blk_t blk) {
	aal_assert("umka-2409", tree != NULL);
	aal_assert("umka-2412", tree->fs != NULL);
	aal_assert("umka-2413", tree->fs->format != NULL);
	
	reiser4_format_set_root(tree->fs->format, blk);
}

/* Updates height in format by passed @height. */
void reiser4_tree_set_height(reiser4_tree_t *tree,
			     uint8_t height)
{
	aal_assert("umka-2410", tree != NULL);
	aal_assert("umka-2416", tree->fs != NULL);
	aal_assert("umka-2417", tree->fs->format != NULL);
	
	reiser4_format_set_height(tree->fs->format, height);
}

/* Unlocks @node and makes check if it is empty. If so and is not locked anymore
   it is detached from tree. */
errno_t reiser4_tree_unlock_node(reiser4_tree_t *tree, reiser4_node_t *node) {
	errno_t res;
	
	aal_assert("umka-3058", tree != NULL);
	aal_assert("umka-3059", node != NULL);
	
	reiser4_node_unlock(node);

	/* Check if we should release node as it is empty and node locked. */
	if (!reiser4_node_locked(node) && !reiser4_node_items(node) &&
	    (node->flags & NF_HEARD_BANSHEE))
	{
		/* Check if we should detach node from tree first. */
		if (reiser4_tree_attached_node(tree, node)) {
			if ((res = reiser4_tree_detach_node(tree, node,
							    SF_DEFAULT)))
			{
				return res;
			}
		}

		return reiser4_tree_release_node(tree, node);
	}
	
	return 0;
}
#endif

/* Returns tree root block number stored in format. */
blk_t reiser4_tree_get_root(reiser4_tree_t *tree) {
	aal_assert("umka-738", tree != NULL);
	aal_assert("umka-2414", tree->fs != NULL);
	aal_assert("umka-2415", tree->fs->format != NULL);

	return reiser4call(tree->fs->format, get_root);
}

#ifndef ENABLE_MINIMAL
/* Returns tree height stored in format. */
uint8_t reiser4_tree_get_height(reiser4_tree_t *tree) {
	aal_assert("umka-2411", tree != NULL);
	aal_assert("umka-2418", tree->fs != NULL);
	aal_assert("umka-2419", tree->fs->format != NULL);

	return reiser4call(tree->fs->format, get_height);
}

/* As @node already lies in @tree->nodes hash table and it is going to change
   its block number, we have to update its hash entry in @tree->nodes. This
   function does that job and also moves @node to @new_blk location. */
errno_t reiser4_tree_rehash_node(reiser4_tree_t *tree,
				 reiser4_node_t *node,
				 blk_t new_blk)
{
	blk_t old_blk;
	blk_t *set_blk;

	aal_assert("umka-3043", tree != NULL);
	aal_assert("umka-3044", node != NULL);
	aal_assert("umka-3045", reiser4_node_items(node) > 0);
	
	old_blk = node->block->nr;
	reiser4_node_move(node, new_blk);

	/* Allocating new key and assign new block number value to it. */
	if (!(set_blk = aal_calloc(sizeof(*set_blk), 0)))
		return -ENOMEM;

	*set_blk = new_blk;

	/* Remove old hash table entry and insert new allocated one. This is
	   quite cheapper operation. */
	if (aal_hash_table_remove(tree->nodes, &old_blk))
		return -EINVAL;

	return aal_hash_table_insert(tree->nodes, set_blk, node);
}
#endif

/* Puts @node to @tree->nodes hash table. */
static errno_t reiser4_tree_hash_node(reiser4_tree_t *tree,
				      reiser4_node_t *node)
{
	blk_t *blk;

	aal_assert("umka-3040", tree != NULL);
	aal_assert("umka-3041", node != NULL);
	
	/* Registering @node in @tree->nodes hash table with key equal to block
	   number of @node. */
	if (!(blk = aal_calloc(sizeof(*blk), 0)))
		return -ENOMEM;

	*blk = node->block->nr;

	return aal_hash_table_insert(tree->nodes, blk, node);
}

/* Removes @node from @tree->nodes hash table. Used when nodeis going to be
   disconnected from tree cache. */
static errno_t reiser4_tree_unhash_node(reiser4_tree_t *tree,
					reiser4_node_t *node)
{
	blk_t blk;

	aal_assert("umka-3046", tree != NULL);
	aal_assert("umka-3047", node != NULL);

	blk = node->block->nr;
	return aal_hash_table_remove(tree->nodes, &blk);
}

#ifndef ENABLE_MINIMAL
/* Acknowledles, that passed @place has nodeptr that points onto passed
   @node. This is needed for tree_rebind_node() function. */
static int tree_check_pos(reiser4_place_t *place, blk_t blocknr) {
	aal_assert("umka-3115", place != NULL);
	
	if (!(place->pos.item < reiser4_node_items(place->node)))
		return 0;
	       
	if (reiser4_place_fetch(place))
		return 0;

	if (!reiser4_item_branch(place->plug))
		return 0;

	return reiser4_item_down_link(place) == blocknr;
}

static void tree_next_child_pos(reiser4_node_t *left,
				reiser4_place_t *place)
{
	aal_assert("umka-3126", left != NULL);
	aal_assert("umka-3127", place != NULL);
	
	aal_memcpy(place, &left->p, sizeof(*place));
	place->pos.item++;
}
#endif

static errno_t tree_find_child_pos(reiser4_tree_t *tree,
				   reiser4_node_t *parent,
				   reiser4_node_t *child,
				   reiser4_place_t *place)
{
	lookup_hint_t hint;
        reiser4_key_t lkey;

#ifndef ENABLE_MINIMAL
	uint32_t i;
#endif
    
	aal_assert("umka-869", child != NULL);
	aal_assert("umka-3114", parent != NULL);
	aal_assert("umka-3038", reiser4_node_items(child) > 0);

	place->node = parent;
	
#ifndef ENABLE_MINIMAL
	/* Checking if we are in position already. */
	if (tree_check_pos(place, child->block->nr))
		goto out_correct_place;
#endif

	/* Getting position by means of using node lookup. */
        reiser4_node_leftmost_key(child, &lkey);
	hint.key = &lkey;

        if (reiser4_node_lookup(parent, &hint, FIND_EXACT,
				&place->pos) == PRESENT)
	{
#ifndef ENABLE_MINIMAL
		if (tree_check_pos(place, child->block->nr))
			goto out_correct_place;
#endif
	}

	/* Getting position by means of linear traverse. */
#ifndef ENABLE_MINIMAL
	for (i = 0; i < reiser4_node_items(place->node); i++) {
		uint32_t j;
		errno_t res;
			
		place->pos.item = i;

		if ((res = reiser4_place_fetch(place)))
			return res;

		if (!reiser4_item_branch(place->plug))
			continue;

		for (j = 0; j < reiser4_item_units(place); j++) {
			blk_t blocknr;
			
			place->pos.unit = j;

			blocknr = reiser4_item_down_link(place);

			if (child->block->nr == blocknr)
				goto out_correct_place;
		}
	}

	return -EINVAL;

out_correct_place:
#else
	if (reiser4_place_fetch(place))
		return -EINVAL;
#endif
	if (reiser4_item_units(place) == 1)
		place->pos.unit = MAX_UINT32;

	return 0;
}

/* Updates @node->p by position in parent node. */
static errno_t reiser4_tree_rebind_node(reiser4_tree_t *tree,
					reiser4_node_t *parent,
					reiser4_node_t *child)
{
	aal_assert("umka-3116", tree != NULL);
	aal_assert("umka-3117", child != NULL);
	aal_assert("umka-3122", parent != NULL);

	return tree_find_child_pos(tree, parent,
				   child, &child->p);
}

/* Loads root node and put it to @tree->nodes hash table. */
errno_t reiser4_tree_load_root(reiser4_tree_t *tree) {
	blk_t root_blk;
	
	aal_assert("umka-1870", tree != NULL);

	/* Check if root is loaded. */
	if (tree->root)
		return 0;

	/* Getting root node and loading it. */
	root_blk = reiser4_tree_get_root(tree);
	
	if (!(tree->root = reiser4_tree_load_node(tree, NULL, root_blk))) {
		aal_error("Can't load root node %llu.", root_blk);
		return -EIO;
	}

	tree->root->tree = (tree_entity_t *)tree;
	return 0;
}

#if 0
/* DEBUGGING */
static errno_t cb_count_children(reiser4_place_t *place, void *data) {
	uint32_t *count = (uint32_t *)data;
	blk_t blk;
	
	blk = reiser4_item_down_link(place);
	if (reiser4_tree_lookup_node(place->node->tree, blk))
		(*count)++;
	
	return 0;
}

/* Debugging code for catching wrong node->counter. */
uint32_t debug_node_loaded_children(reiser4_node_t *node) {
	uint32_t count = 0;
	if (!node) return 0;
	reiser4_node_trav(node, cb_count_children, &count);
	return count;
}

/* Debugging code for catching wrong order of keys. */
static errno_t debug_node_check_keys(reiser4_node_t *node) {
	reiser4_key_t key, prev;
	pos_t pos = {0, MAX_UINT32};
	uint32_t count;
	
	count = reiser4_node_items(node);

	for (pos.item = 0; pos.item < count; pos.item++) {
		plug_call(node->plug->pl.node, 
			  get_key, node, &pos, &key);

		if (pos.item && reiser4_key_compfull(&prev, &key) > 0)
			return 1;

		aa_memcpy(&prev, &key, sizeof(key));
	}

	return 0;
}

#endif

#ifndef ENABLE_MINIMAL
/* Assignes passed @node to root. Takes care about root block number and tree
   height in format. */
errno_t reiser4_tree_assign_root(reiser4_tree_t *tree,
				 reiser4_node_t *node)
{
	blk_t blk;
	uint32_t level;
	
	aal_assert("umka-1867", tree != NULL);
	aal_assert("umka-1868", node != NULL);

	/* Establishing connection between node and tree. */
	tree->root = node;
	node->tree = (tree_entity_t *)tree;
	node->p.node = NULL;

	if (reiser4_tree_connect_node(tree, NULL, node))
		return -EINVAL;
	
	/* Updating tree height. */
	level = reiser4_node_get_level(node);
	reiser4_tree_set_height(tree, level);

	/* Updating root block number. */
	blk = tree->root->block->nr;
	reiser4_tree_set_root(tree, blk);

	return 0;
}
#endif

errno_t reiser4_tree_mpressure(reiser4_tree_t *tree) {
	errno_t res;
	
	/* Check for memory pressure event. If memory pressure is uppon us, we
	   call memory cleaning function. For now we call tree_adjust() in order
	   to release not locked nodes. */
	if (!tree->mpc_func || !tree->mpc_func(tree))
		return 0;

	/* Adjusting the tree as memory pressure is here. */
	if ((res = reiser4_tree_adjust(tree))) {
		aal_error("Can't adjust tree.");
		return res;
	}

	return 0;
}

/* Registers passed node in tree and connects left and right neighbour
   nodes. This function does not do any tree modifications. */
errno_t reiser4_tree_connect_node(reiser4_tree_t *tree,
				  reiser4_node_t *parent, 
				  reiser4_node_t *node)
{
	errno_t res;
	
	aal_assert("umka-1857", tree != NULL);
	aal_assert("umka-2261", node != NULL);

	node->tree = (tree_entity_t *)tree;

	if (reiser4_tree_root_node(tree, node)) {
		/* This is the case when we connect root node, that is with no
		   parent. */
		tree->root = node;
	} else if (parent) {
		/* Updating node->p parent place. */
		if (reiser4_tree_rebind_node(tree, parent, node))
			return -EINVAL;

		reiser4_node_lock(parent);
	}
	
	if (reiser4_tree_hash_node(tree, node))
		return -EINVAL;

	reiser4_node_lock(node);
	if ((res = reiser4_tree_mpressure(tree))) {
		aal_error("Can't connect node %llu.",
			  node->block->nr);

		if (parent)
			reiser4_node_unlock(parent);
		
		reiser4_tree_unhash_node(tree, node);
	}
	reiser4_node_unlock(node);
	
	return res;
}

/* Remove specified child from the node children list. Updates all neighbour
   pointers and parent pointer.*/
errno_t reiser4_tree_disconnect_node(reiser4_tree_t *tree,
				     reiser4_node_t *node)
{
	aal_assert("umka-563", node != NULL);
	aal_assert("umka-1858", tree != NULL);
	
	/* Disconnecting left and right neighbours. */
	if (node->left) {
		node->left->right = NULL;
		node->left = NULL;
	}
	
	if (node->right) {
		node->right->left = NULL;
		node->right = NULL;
	}
	
	/* Disconnecting @node from @tree->nodes hash table. */
	if (reiser4_tree_unhash_node(tree, node))
		return -EINVAL;
	
	node->tree = NULL;

	if (tree->root == node) {
		/* The case when we're disconnecting root node for some
		   reasons. And we will let to do so? Yes, why not? */
		tree->root = NULL;
	}

	/* Unlock parent node. */
	if (node->p.node) {
		reiser4_node_unlock(node->p.node);
		node->p.node = NULL;
	}

	return 0;
}

#ifndef ENABLE_MINIMAL
/* Updates all internal node loaded children positions in parent. */
static errno_t reiser4_tree_update_node(reiser4_tree_t *tree,
					reiser4_node_t *node,
					uint8_t start, uint8_t end)
{
	uint32_t i;
	errno_t res;

	aal_assert("umka-3035", tree != NULL);
	aal_assert("umka-3036", node != NULL);
	aal_assert("umka-3037", reiser4_node_items(node) > 0);
	
	for (i = start; i < end; i++) {
		blk_t blk;
		uint32_t j;

		reiser4_node_t *child;
		reiser4_place_t place;

		/* Initializing item at @i. */
		reiser4_place_assign(&place, node, i, MAX_UINT32);

		if ((res = reiser4_place_fetch(&place)))
			return res;

		if (!reiser4_item_branch(place.plug))
			continue;

		for (j = 0; j < reiser4_item_units(&place); j++) {
			place.pos.unit = j;
			
			blk = reiser4_item_down_link(&place);

			/* Getting loaded child node. If it is not loaded, we
			   don't have to update its parent pos. */
			if (!(child = reiser4_tree_lookup_node(tree, blk)))
				continue;

			/* Unlock old parent node. There are some cases when
			   parent is not set yet (like tree_growup()). So, we
			   check parent for null. */
			if (child->p.node) {
				reiser4_node_unlock(child->p.node);
				reiser4_node_lock(node);
			}
			
			/* Updating position in parent node. */
			if ((res = reiser4_tree_rebind_node(tree, node, child)))
				return res;
			
			if (reiser4_node_items(node) == 0) {
				if (node->flags & NF_HEARD_BANSHEE)
					continue;

				aal_bug("umka-3060", "Node (%llu) is empty "
					"but not marked as 'heard banshee'.",
					node->block->nr);
			}
		}
	}

	return 0;
}
#endif

reiser4_node_t *reiser4_tree_lookup_node(reiser4_tree_t *tree, blk_t blk) {
	aal_assert("umka-3002", tree != NULL);
	return aal_hash_table_lookup(tree->nodes, &blk);
}

/* Loads node from @blk and connects it to @parent. */
reiser4_node_t *reiser4_tree_load_node(reiser4_tree_t *tree,
				       reiser4_node_t *parent, blk_t blk)
{
	reiser4_node_t *node = NULL;

	aal_assert("umka-1289", tree != NULL);

	/* Checking if node in the local cache of @parent. */
	if (!(node = reiser4_tree_lookup_node(tree, blk))) {
		aal_assert("umka-3004", !reiser4_fake_ack(blk));

		/* Node is not loaded yet. Loading it and connecting to @parent
		   node cache. */
		if (!(node = reiser4_node_open(tree, blk)))
			return NULL;

		/* Connect loaded node to cache. */
		if (reiser4_tree_connect_node(tree, parent, node))
			goto error_free_node;
	}

	return node;

 error_free_node:
	reiser4_node_close(node);
	return NULL;
}

/* Unloading node and unregistering it from @tree->nodes hash table. */
errno_t reiser4_tree_unload_node(reiser4_tree_t *tree, reiser4_node_t *node) {
	errno_t res;
	
	aal_assert("umka-1840", tree != NULL);
	aal_assert("umka-1842", node != NULL);

#ifndef ENABLE_MINIMAL
	/* Check if node is dirty. */
	if (reiser4_node_isdirty(node)) {
		aal_warn("Unloading dirty node %llu.",
			 node->block->nr);
	}
#endif

	/* Disconnecting @node from its parent node. */
	if ((res = reiser4_tree_disconnect_node(tree, node))) {
		aal_error("Can't disconnect node from "
			  "tree cache.");
		return res;
	}

	/* Releasing node instance. */
	return reiser4_node_close(node);
}

/* Loads denoted by passed nodeptr @place child node */
reiser4_node_t *reiser4_tree_child_node(reiser4_tree_t *tree,
					reiser4_place_t *place)
{
	reiser4_node_t *node;
	blk_t blk;
	
	aal_assert("umka-1889", tree != NULL);
	aal_assert("umka-1890", place != NULL);
	aal_assert("umka-1891", place->node != NULL);

	/* Initializing @place. */
	if (reiser4_place_fetch(place))
		return NULL;

	/* Checking if item is a branch of tree */
	if (!reiser4_item_branch(place->plug))
		return NULL;

	blk = reiser4_item_down_link(place);
	if (!(node = reiser4_tree_load_node(tree, place->node, blk))) {
		aal_error("Can't load child node %llu.", blk);
		return NULL;
	}

	return node;
}

static int reiser4_tree_neig_place(reiser4_tree_t *tree, 
				   reiser4_place_t *place,
				   uint32_t where) 
{
	int found = 0;
	uint32_t level = 0;
	
	/* Going up to the level where corresponding neighbour node may be
	   obtained by its nodeptr item. */
        while (place->node->p.node && found == 0) {
		aal_memcpy(place, &place->node->p, sizeof(*place));

		/* Checking position. Level is found if position is not first
		   (right neighbour) and is not last one (left neighbour). */
		found = where == DIR_LEFT ? 
			reiser4_place_gtfirst(place) :
			reiser4_place_ltlast(place);

                level++;
        }

	if (!found)
		return 0;
	
	/* Position correcting. We do not use place_inc() and place_dec() here,
	   because they are not accessible in minimal mode and we do not
	   want to make it accessible because here is one place only and they
	   are quite big. */
	place->pos.item += (where == DIR_LEFT ? -1 : 1);
	return level;
}

/* Finds both left and right neighbours and connects them into the tree. */
static reiser4_node_t *reiser4_tree_ltrt_node(reiser4_tree_t *tree,
					      reiser4_node_t *node, 
					      uint32_t where)
{
        reiser4_place_t place;
        uint32_t level;
                                                                                      
	aal_assert("umka-2213", tree != NULL);
	aal_assert("umka-2214", node != NULL);

	reiser4_place_assign(&place, node, 0, MAX_UINT32);
	
        if (!(level = reiser4_tree_neig_place(tree, &place, where)))
                return NULL;

	reiser4_node_lock(node);
	
        /* Going down to the level of @node. */
        while (level > 0) {
		
                if (!(place.node = reiser4_tree_child_node(tree, &place))) {
			reiser4_node_unlock(node);
			return NULL;
		}

		if (where == DIR_LEFT) {
			if (reiser4_place_last(&place)) {
				reiser4_node_unlock(node);
				return NULL;
			}
		} else {
			if (reiser4_place_first(&place)) {
				reiser4_node_unlock(node);
				return NULL;
			}
		}
		
                level--;
        }
	
	reiser4_node_unlock(node);
		
        /* Setting up sibling pointers. */
        if (where == DIR_LEFT) {
                node->left = place.node;
                place.node->right = node;
        } else {
                node->right = place.node;
                place.node->left = node;
        }
	
	return place.node;
}

static errno_t reiser4_tree_adjust_place(reiser4_tree_t *tree, 
					 reiser4_place_t *place, 
					 reiser4_place_t *next) 
{
	/* Check if we have to get right neighbour node. */
	if (place->pos.item >= reiser4_node_items(place->node)) {
		
		/* Load the right neighbour. */
		reiser4_tree_ltrt_node(tree, place->node, DIR_RIGHT);

		if (place->node->right) {
			/* The right neighbour exists. */
			reiser4_place_assign(next, place->node->right, 0, 0);
		} else {
			/* There is no right neighbour. Get the right neighbour
			   of the above level if there is one. */
			aal_memcpy(next, place, sizeof(*place));

			if (!reiser4_tree_neig_place(tree, next, DIR_RIGHT)) {
				/* Not found. */
				aal_memset(next, 0, sizeof(*next));
				return 0;
			}
		}
	} else {
		aal_memcpy(next, place, sizeof(*place));
	}
	
	/* Initializing @place. */
	return reiser4_place_fetch(next);
}

/* Moves @place by one item to right. If node is over, returns node next to
   passed @place. Needed for moving though the tree node by node, for instance
   in directory read code. */
errno_t reiser4_tree_next_place(reiser4_tree_t *tree, 
				reiser4_place_t *place,
				reiser4_place_t *next)
{
	reiser4_node_t *node;
	
	aal_assert("umka-867", tree != NULL);
	aal_assert("umka-868", place != NULL);
	aal_assert("umka-1491", next != NULL);

	aal_memcpy(next, place, sizeof(*place));
	next->pos.item++;
	next->pos.unit = 0;
	
	if (reiser4_tree_adjust_place(tree, next, next))
		return -EINVAL;

	if (!next->node) 
		return 0;
	
	node = next->node;
	reiser4_node_lock(node);
	
	/* If nodeptr item go down. */
	while (reiser4_item_branch(next->plug)) {
		blk_t blk;
		
		blk = reiser4_item_down_link(next);
		
		next->node = reiser4_tree_load_node(tree, next->node, blk);
		
		if (!next->node) {
			aal_error("Can't load a child node %llu of the node"
				  " (%llu).", blk, node->block->nr);
			goto error;
		}

		if (reiser4_place_first(next))
			goto error;

		if (reiser4_place_fetch(next))
			goto error;
	}
	
	reiser4_node_unlock(node);

	return 0;
 error:
	reiser4_node_unlock(node);
	return -EINVAL;
}

/* Get the key of the given @place. If @place is not valid, get the key of 
   the right neighbour. */
errno_t reiser4_tree_place_key(reiser4_tree_t *tree, 
			       reiser4_place_t *place,
			       reiser4_key_t *key) 
{
	reiser4_place_t next;
	
	aal_assert("vpf-1527", tree != NULL);
	aal_assert("vpf-1528", place != NULL);
	aal_assert("vpf-1529", key != NULL);
	
	aal_memcpy(&next, place, sizeof(*place));
	
	if (next.pos.item >= reiser4_node_items(next.node)) {
		if (!reiser4_tree_neig_place(tree, &next, DIR_RIGHT)) {
			key->plug = tree->key.plug;
			reiser4_key_maximal(key);
			return 0;
		}
	}

	if (reiser4_place_fetch(&next))
		return -EINVAL;
	
	return reiser4_item_get_key(&next, key);
}

#ifndef ENABLE_MINIMAL
/* Gets the key of the next item. */
errno_t reiser4_tree_next_key(reiser4_tree_t *tree, 
			      reiser4_place_t *place, 
			      reiser4_key_t *key) 
{
	reiser4_place_t temp;
	
	aal_assert("vpf-1427", tree != NULL);
	aal_assert("vpf-1427", place != NULL);
	aal_assert("vpf-1427", key != NULL);

	temp = *place;
	temp.pos.item++;
	temp.pos.unit = MAX_UINT32;

	return reiser4_tree_place_key(tree, &temp, key);
}

/* Requests block allocator for new block and creates empty node in it. */
reiser4_node_t *reiser4_tree_alloc_node(reiser4_tree_t *tree,
					uint8_t level)
{
	reiser4_node_plug_t *plug;
	reiser4_node_t *node;
	uint32_t stamp;
	errno_t res;
	blk_t blk;
	
	reiser4_format_t *format;
    
	aal_assert("umka-756", tree != NULL);
    
	/* Allocating fake block number. */
	blk = reiser4_fake_get();
	format = tree->fs->format;

	/* Setting up of the free blocks in format. */
	if ((res = reiser4_format_dec_free(format, 1)))
		return NULL;
	
	plug = (reiser4_node_plug_t *)tree->ent.tset[TSET_NODE];
	
	/* Creating new node. */
	if (!(node = reiser4_node_create(tree, plug, blk, level))) {
		aal_error("Can't initialize new fake node.");
		return NULL;
	}

	/* Setting flush stamps to new node. */
	stamp = reiser4_format_get_stamp(format);
	reiser4_node_set_mstamp(node, stamp);
	node->tree = &tree->ent;

	return node;
}

/* Unload node and releasing it in block allocator */
errno_t reiser4_tree_release_node(reiser4_tree_t *tree,
				  reiser4_node_t *node)
{
	reiser4_alloc_t *alloc;
	reiser4_format_t *format;
	
	aal_assert("umka-1841", tree != NULL);
	aal_assert("umka-2255", node != NULL);

	alloc = tree->fs->alloc;
	format = tree->fs->format;
	reiser4_node_mkclean(node);

	/* Check if we're trying to releas a node with fake block number. If
	   not, free it in block allocator too. */
	if (!reiser4_fake_ack(node->block->nr)) {
		blk_t blk = node->block->nr;
		reiser4_alloc_release(alloc, blk, 1);
	}

	/* Setting up of the free blocks in format. */
	reiser4_format_inc_free(format, 1);

	/* Release node itself. */
	return reiser4_node_close(node);
}

/* Removes nodeptr that points to @node, disconnects it from tree and then
   releases @node itself. */
errno_t reiser4_tree_discard_node(reiser4_tree_t *tree,
				  reiser4_node_t *node)
{
	errno_t res;

	if ((res = reiser4_tree_detach_node(tree, node,
					    SF_DEFAULT)))
	{
		aal_error("Can't detach node %llu from tree.", 
			  node->block->nr);
		return res;
	}
	
	if ((res = reiser4_tree_release_node(tree, node))) {
		aal_error("Can't release node %llu.", node->block->nr);
		return res;
	}

	return res;
}

/* Helper function for freeing passed key instance tree's data hashtable entry
   is going to be removed. */
static void cb_blocks_keyrem_func(void *key) {
	reiser4_key_free((reiser4_key_t *)key);
}

/* Helper function for freeing hash value, that is, data block. */
static void cb_blocks_valrem_func(void *val) {
	aal_block_free((aal_block_t *)val);
}

/* Helper function for calculating 64-bit hash by passed key. This is used for
   tree's data hash. Note: as offset of extent blocks is divisible by blocksize
   (4096 by default) offset is shifted on 12 bits to the right to have neighbour 
   blocks in neighbour lists. */
static uint64_t cb_blocks_hash_func(void *key) {
	return (reiser4_key_get_objectid((reiser4_key_t *)key) +
		(reiser4_key_get_offset((reiser4_key_t *)key) >> 12));
}

/* Helper function for comparing two keys during tree's data hash lookups. */
static int cb_blocks_comp_func(void *key1, void *key2,
				     void *data)
{
	return reiser4_key_compfull((reiser4_key_t *)key1,
				    (reiser4_key_t *)key2);
}

/* Returns level in tree particular item should be inserted at. */
inline uint32_t reiser4_tree_target_level(reiser4_tree_t *tree,
					  reiser4_plug_t *plug)
{
	return (plug->id.group == EXTENT_ITEM) ?
		TWIG_LEVEL : LEAF_LEVEL;
}

#endif

/* Helpher function for freeing keys in @tree->nodes hash table during its
   destroying. */
static void cb_nodes_keyrem_func(void *key) {
	aal_free(key);
}

/* Return hash number from passed key value from @tree->nodes hashtable. */
static uint64_t cb_nodes_hash_func(void *key) {
	return *(uint64_t *)key;
}

/* Compares two passed keys of @tree->nodes hash table during lookup in it. */
static int cb_nodes_comp_func(void *key1, void *key2, void *data) {
	if (*(uint64_t *)key1 < *(uint64_t *)key2)
		return -1;

	if (*(uint64_t *)key1 > *(uint64_t *)key2)
		return 1;

	return 0;
}

/* Returns the key of the fake root parent */
errno_t reiser4_tree_root_key(reiser4_tree_t *tree,
			      reiser4_key_t *key)
{
	oid_t locality;
	oid_t objectid;
	
	aal_assert("umka-1949", tree != NULL);
	aal_assert("umka-1950", key != NULL);

	key->plug = (reiser4_key_plug_t *)tree->ent.tset[TSET_KEY];
	
#ifndef ENABLE_MINIMAL
	locality = reiser4_oid_root_locality(tree->fs->oid);
	objectid = reiser4_oid_root_objectid(tree->fs->oid);
#else
	locality = REISER4_ROOT_LOCALITY;
	objectid = REISER4_ROOT_OBJECTID;
#endif
	return objcall(key, build_generic, KEY_STATDATA_TYPE, 
		       locality, 0, objectid, 0);
}

#ifndef ENABLE_MINIMAL
# define TREE_NODES_TABLE_SIZE (512)
#else
# define TREE_NODES_TABLE_SIZE (32)
#endif
#define TREE_BLOCKS_TABLE_SIZE (512)

/* Initializes tree instance on passed filesystem and return it to caller. Then
   it may be used for modifying tree, making lookup, etc. */
reiser4_tree_t *reiser4_tree_init(reiser4_fs_t *fs) {
	reiser4_tree_t *tree;

	aal_assert("umka-737", fs != NULL);

	/* Allocating memory for tree instance */
	if (!(tree = aal_calloc(sizeof(*tree), 0)))
		return NULL;

	tree->fs = fs;
	tree->adjusting = 0;

	/* Initializing hash table for storing loaded formatted nodes in it. */
	if (!(tree->nodes = aal_hash_table_create(TREE_NODES_TABLE_SIZE,
						  cb_nodes_hash_func,
						  cb_nodes_comp_func,
						  cb_nodes_keyrem_func,
						  NULL)))
	{
		goto error_free_tree;
	}

#ifndef ENABLE_MINIMAL
	/* Initializing hash table for storing loaded unformatted blocks in
	   it. This uses all callbacks we described above for getting hash
	   values, lookup, etc. */
	if (!(tree->blocks = aal_hash_table_create(TREE_BLOCKS_TABLE_SIZE,
						   cb_blocks_hash_func,
						   cb_blocks_comp_func,
						   cb_blocks_keyrem_func,
						   cb_blocks_valrem_func)))
	{
		goto error_free_nodes;
	}

#endif
	/* Initializing the tset. */
	if (reiser4_tset_init(tree))
		goto error_free_data;

	/* Building tree root key. It is used in tree lookup, etc. */
	if (reiser4_tree_root_key(tree, &tree->key)) {
		aal_error("Can't build the tree root key.");
		goto error_free_data;
	}
    
	return tree;

 error_free_data:
#ifndef ENABLE_MINIMAL
	aal_hash_table_free(tree->blocks);
 error_free_nodes:
#endif
	aal_hash_table_free(tree->nodes);
 error_free_tree:
	aal_free(tree);
	return NULL;
}

/* Unloads all loaded tree nodes. */
errno_t reiser4_tree_collapse(reiser4_tree_t *tree) {
        aal_assert("umka-2265", tree != NULL);
                                                                                          
        if (!tree->root)
                return 0;
                                                                                          
        return reiser4_tree_walk_node(tree, tree->root, 
#ifndef ENABLE_MINIMAL
				      NULL, NULL,
#endif
                                      reiser4_tree_unload_node);
}

/* Closes specified tree without saving dirty nodes to device. It just thows out
   all loaded nodes without dealing with allocating etc. This may be used in
   minimal mode and/or just to free modified tree without a changes on
   device. */
void reiser4_tree_close(reiser4_tree_t *tree) {
	aal_assert("vpf-1316", tree != NULL);

	/* Close all remaining nodes. */
	reiser4_tree_collapse(tree);

	/* Releasing unformatted nodes hash table. */
#ifndef ENABLE_MINIMAL
	aal_hash_table_free(tree->blocks);
#endif

	/* Releasing fomatted nodes hash table. */
	aal_hash_table_free(tree->nodes);

	/* Freeing tree instance. */
	tree->fs->tree = NULL;
	aal_free(tree);
}
#ifndef ENABLE_MINIMAL
static errno_t cb_flags_dup(reiser4_place_t *place, void *data) {
	reiser4_item_dup_flags(place, *(uint16_t *)data);
	return 0;
}

/* Allocates extent item at passed @place. */
static errno_t reiser4_tree_alloc_extent(reiser4_tree_t *tree,
					 reiser4_place_t *place)
{
	errno_t res;
	uint32_t units;
	uint16_t flags;
	ptr_hint_t ptr;
	uint32_t blksize;
	trans_hint_t hint;

	units = reiser4_item_units(place);
	blksize = reiser4_tree_get_blksize(tree);

	/* Prepare @hint. */
	hint.count = 1;
	hint.specific = &ptr;
	hint.plug = place->plug;
	hint.region_func = NULL;
	hint.place_func = cb_flags_dup;
	hint.data = &flags;

	/* We force balancing use these flags with disables left shift
	   in order to not affect to items/units left of insert point,
	   as we allocate items/units from left to right. */
	hint.shift_flags = (SF_DEFAULT & ~SF_ALLOW_LEFT);

	for (place->pos.unit = 0; place->pos.unit < units;
	     place->pos.unit++)
	{
		uint64_t width;
		uint64_t blocks;
		uint64_t offset;
		
		reiser4_key_t key;
		int first_time = 1;


		if (objcall(place, object->fetch_units, &hint) != 1)
			return -EIO;

		/* Check if we have accessed unallocated extent. */
		if (ptr.start != EXTENT_UNALLOC_UNIT)
			continue;

		/* Getting unit key. */
		objcall(place, balance->fetch_key, &key);

		/* Loop until all units get allocated. */
		for (blocks = 0, width = ptr.width; width > 0; width -= ptr.width) {
			blk_t blk;
			uint32_t i;
			aal_block_t *block;
			
			/* Trying to allocate @ptr.width blocks. */
			if (!(ptr.width = reiser4_alloc_allocate(tree->fs->alloc,
								 &ptr.start, width)))
			{
				return -ENOSPC;
			}

			if (first_time) {
				flags = reiser4_item_get_flags(place);

				/* Updating extent unit at @place->pos.unit. */
				if (objcall(place, object->update_units, 
					    &hint) != 1)
				{
					return -EIO;
				}

				first_time = 0;
			} else {
				errno_t res;
				uint32_t level;
				reiser4_place_t iplace;

				iplace = *place;
				iplace.pos.unit++;

				/* Insert new extent units. */
				aal_memcpy(&hint.offset, &key, sizeof(key));
				level = reiser4_node_get_level(iplace.node);
				
				if ((res = reiser4_tree_insert(tree, &iplace,
							       &hint, level)) < 0)
				{
					return res;
				}

                                /* Updating @place by insert point, as it might
				   be moved due to balancing. */
				aal_memcpy(place, &iplace, sizeof(iplace));

				/* Updating @units as it might be changed after
				   balancing during tree_insert(). */
				units = reiser4_item_units(place);
			}

			/* Moving data blocks to right places, saving them and
			   releasing from the cache. */
			for (blk = ptr.start, i = 0; i < ptr.width; i++, blk++) {
				/* Getting data block by @key */
				block = aal_hash_table_lookup(tree->blocks, &key);
				aal_assert("umka-2469", block != NULL);

				/* Moving block tro @blk */
				aal_block_move(block, tree->fs->device, blk);

				/* Saving block to device. */
				if ((res = aal_block_write(block))) {
					aal_error("Can't write block "
						  "%llu.", block->nr);
					return res;
				}

				/* Releasing cache entry. */
				aal_hash_table_remove(tree->blocks, &key);

				/* Updating the key to find next data block */
				offset = objcall(&key, get_offset);

				objcall(&key, set_offset, offset + blksize);
			}
			
			blocks += ptr.width;
		}
	}

	return 0;
}

static errno_t cb_node_adjust(reiser4_tree_t *tree, reiser4_node_t *node) {
	errno_t res;
	
	aal_assert("umka-2302", tree != NULL);
	aal_assert("umka-2303", node != NULL);
	aal_assert("umka-3075", reiser4_node_items(node) > 0);

	/* Requesting block allocator to allocate the real block number
	   for fake allocated node. */
	if (reiser4_fake_ack(node->block->nr)) {
		blk_t blk;
		
		if (!reiser4_alloc_allocate(tree->fs->alloc, &blk, 1))
			return -ENOSPC;

		if (reiser4_tree_root_node(tree, node))
			reiser4_tree_set_root(tree, blk);
		
		if (node->p.node) {
			if ((res = reiser4_item_update_link(&node->p, blk)))
				return res;
		}
		
		/* Rehashing node in @tree->nodes hash table. */
		reiser4_tree_rehash_node(tree, node, blk);
	}

	return 0;
}

/* Runs through the node in @place and calls tree_adjust_node() for all
   children. */
static errno_t cb_nodeptr_adjust(reiser4_tree_t *tree, reiser4_place_t *place) {
	/* It is not good, that we reference here to particular item group. But,
	   we have to do so, considering, that this is up to tree to know about
	   items type in it. Probably this is why tree should be plugin too to
	   handle things like this in more flexible manner. */
	if (place->plug->p.id.group != EXTENT_ITEM) 
		return 0;
	
	/* Allocating unallocated extent item at @place. */
	return reiser4_tree_alloc_extent(tree, place);
}
#endif

static errno_t cb_node_unload(reiser4_tree_t *tree, reiser4_node_t *node) {
	/* If node is locked, that is not a leaf or it is used by someone, it
	   cannot be released, and thus, it does not make the sense to save it
	   to device too. */
	if (reiser4_node_locked(node))
		return 0;
	
#ifndef ENABLE_MINIMAL
	/* Okay, node is fully allocated now and ready to be saved to device if
	   it is dirty. */
	if (reiser4_node_isdirty(node) && reiser4_node_sync(node)) {
		aal_error("Can't write node %llu.", node->block->nr);
		return -EIO;
	}
#endif
	/* Unloading node from tree cache. */
	return reiser4_tree_unload_node(tree, node);
}


/* Entry point for adjsuting tree routines. */
errno_t reiser4_tree_adjust(reiser4_tree_t *tree) {
	aal_assert("umka-3034", tree != NULL);
	
	if (tree->root && !tree->adjusting) {
		errno_t res = 0;
		
		tree->adjusting = 1;

		/* Check for special case -- tree_adjust() is calling during
		   tree_growup(), when empty root is connected. */
		if (reiser4_node_items(tree->root))
#ifndef ENABLE_MINIMAL
			res = reiser4_tree_walk_node(tree, tree->root, 
						     cb_node_adjust,
						     cb_nodeptr_adjust,
						     cb_node_unload);
#else
			res = reiser4_tree_walk_node(tree, tree->root, 
						     cb_node_unload);
#endif
		
		tree->adjusting = 0;
		
		return res;
	}

	return 0;
}

/* Walking though the tree cache and closing all nodes. */
errno_t reiser4_tree_walk_node(reiser4_tree_t *tree, 
			       reiser4_node_t *node,
#ifndef ENABLE_MINIMAL
			       walk_func_t pre_func, 
			       walk_on_func_t on_func,
#endif
			       walk_func_t post_func)
{
	uint32_t i;
	errno_t res;
	
	aal_assert("umka-1933", tree != NULL);
	aal_assert("umka-1934", node != NULL);

#ifndef ENABLE_MINIMAL
	if (pre_func && (res = pre_func(tree, node)))
		return res;
#endif
	
	for (i = 0; i < reiser4_node_items(node); i++) {
		blk_t blk;
		uint32_t j;

		reiser4_node_t *child;
		reiser4_place_t place;

		/* Initializing item at @i. */
		reiser4_place_assign(&place, node, i, MAX_UINT32);

		if ((res = reiser4_place_fetch(&place)))
			return res;

#ifndef ENABLE_MINIMAL
		if (on_func && (res = on_func(tree, &place)))
			return res;
#endif

		if (!reiser4_item_branch(place.plug))
			continue;

		reiser4_node_lock(node);
		for (j = 0; j < reiser4_item_units(&place); j++) {
			/* Getting node by its nodeptr. If it is loaded, we call
			   tree_adjust_node() recursively in order to allocate
			   children. */
			place.pos.unit = j;
			
			blk = reiser4_item_down_link(&place);

			if (!(child = reiser4_tree_lookup_node(tree, blk)))
				continue;

			/* Making recursive call to tree_walk_node(). */
			if ((res = reiser4_tree_walk_node(tree, child, 
#ifndef ENABLE_MINIMAL
							  pre_func, on_func, 
#endif
							  post_func)))
			{
				reiser4_node_unlock(node);
				return res;
			}
		}
		reiser4_node_unlock(node);
	}
	
	/* Calling @walk_func for @node. */
	return post_func ? post_func(tree, node) : 0;
}

#ifndef ENABLE_MINIMAL
/* Helper function for save one unformatted block to device. Used from
   tree_sync() to save all in-memory unfromatted blocks. */
static errno_t cb_save_block( void *entry, void *data) {
	aal_hash_node_t *node = (aal_hash_node_t *)entry;
	aal_block_t *block = (aal_block_t *)node->value;

	/* Save block if it is dirty. */
	if (block->dirty) {
		errno_t res;
		
		if ((res = aal_block_write(block)))
			return res;
	}

	return 0;
}

/* Packs one level at passed @node. Moves all items and units from right node
   to left neighbour node and so on until rightmost node is reached. */
static errno_t reiser4_tree_compress_level(reiser4_tree_t *tree,
					   reiser4_node_t *node)
{
	errno_t res;
	reiser4_node_t *right;
	
	aal_assert("umka-3009", tree != NULL);
	aal_assert("umka-3010", node != NULL);

	/* Loop until rightmost node is reached. */
	while ((right = reiser4_tree_ltrt_node(tree, node, DIR_RIGHT))) {
		uint32_t flags;
		reiser4_place_t bogus;

		bogus.node = right;

		/* Left shift and merge is allowed. As this function will be
		   used mostly in the case of out of space, we do not allow to
		   allocate new nodes during shift. */
		flags = (SF_ALLOW_LEFT | SF_ALLOW_MERGE);

		/* Shift items and units from @right to @node with @flags. */
		if ((res = reiser4_tree_shift(tree, &bogus, node, flags))) {
			aal_error("Can't shift node %llu into left.",
				  right->block->nr);
			return res;
		}

		/* Check if node get empty. If so we release it. */
		if (reiser4_node_items(right) == 0) {
			if (reiser4_node_locked(right)) {
				right->flags |= NF_HEARD_BANSHEE;
			} else {
				reiser4_node_lock(node);
			
				/* Releasing @right node from tree cache and
				   from tree structures (that is remove internal
				   nodeptr item in parent node if any). */

				if ((res = reiser4_tree_discard_node(tree, right))) {
					reiser4_node_unlock(node);
					return res;
				}

				reiser4_node_unlock(node);
			}
			
			/* Here we do not move compress point to node next to
			   @right, because @node may still have enough of space
			   to move some data to it and we prefer to do nothing
			   here. That is node data will be moved to on the next
			   cycle of this loop is still the same. */
		} else {
			/* Updating @node by @right in order to move control
			   flow to right neighbour node and so on until
			   rightmost one is reached. */
			node = right;
		}
	}
	
	return 0;
}

/* Makes tree compression to make tree more compact. It shifts items/units on
   all levels to left starting from leaf level. This may be used by fsck to make
   some additional space during reparing in some corner cases. For example, if
   some tail conversion is performed and filesystem is near to be full, we can
   run out of space and this function can releases something and in such a way
   lets fsck finish its job. */
errno_t reiser4_tree_compress(reiser4_tree_t *tree) {
	errno_t res;
	uint8_t level;
	reiser4_node_t *node;

	aal_assert("umka-3000", tree != NULL);

	if (!tree->root)
		return 0;

	node = tree->root;

	/* Loop for getting to first node of leaf level. */
	for (level = reiser4_tree_get_height(tree);
	     level >= LEAF_LEVEL; level--)
	{
		reiser4_place_t place;

		/* Are we on internal level at all? */
		if (level > LEAF_LEVEL) {
			/* Getting first nodeptr on level to get node by it and
			   in such a manner to move control flow to next level
			   of tree. */
			reiser4_place_assign(&place, node, 0, 0);

			/* Getting first node of the next level. */
			if (!(node = reiser4_tree_child_node(tree, &place))) {
				aal_error("Can't get first node on level %u.",
					  level);
				return -EINVAL;
			}
		}
	}

	/* Loop for packing a level starting from leftmost node on leaf
	   level. */
	for (level = LEAF_LEVEL;
	     level < reiser4_tree_get_height(tree); level++)
	{
		reiser4_node_t *parent = node->p.node;
		
		if ((res = reiser4_tree_compress_level(tree, node)))
			return res;
		
		if (!(node = parent))
			break;
	}

	return 0;
}

/* Saves all dirty nodes in tree to device tree lies on. */
errno_t reiser4_tree_sync(reiser4_tree_t *tree) {
	errno_t res;
	
	aal_assert("umka-2259", tree != NULL);

	if (!tree->root)
		return 0;

	/* Flushing formatted nodes starting from root node with memory pressure
	   flag set to 0, that is do not check memory presure, and save
	   everything. */
	if ((res = reiser4_tree_walk_node(tree, tree->root, 
					  cb_node_adjust,
					  cb_nodeptr_adjust,
					  cb_node_unload)))
	{
		aal_error("Can't save formatted nodes to device.");
		return res;
	}

	/* Flushing unformatted blocks (extents data) attached to @tree->data
	   hash table. */
	if ((res = aal_hash_table_foreach(tree->blocks, cb_save_block, tree))) {
		aal_error("Can't save unformatted nodes to device.");
		return res;
	}
	
	return res;
}

/* Correct passed @place according to handle key collisions. */
lookup_t reiser4_tree_collision(reiser4_tree_t *tree, 
				reiser4_place_t *place,
				coll_hint_t *hint)
{
	uint32_t adjust = 0;
	uint32_t units, unit;
	lookup_t lookup = PRESENT;

	aal_assert("umka-3130", tree != NULL);
	aal_assert("vpf-1522", place != NULL);
	
	if (hint == NULL)
		return PRESENT;
	
	/* If type does not match, there is no collision found. */
	if (place->plug->p.id.group != hint->type)
		return PRESENT;

	/* Key collisions handling. Sequentional search by name. */	
	while (1) {
		units = reiser4_item_units(place);

		if (place->pos.unit != MAX_UINT32 && place->pos.unit >= units) {
			reiser4_place_t temp;
			
			/* Getting next item. */
			if ((reiser4_tree_next_place(tree, place, &temp)))
				return -EIO;
			
			/* Directory is over? */
			if (!temp.node || !plug_equal(place->plug, temp.plug)) {
				place->key.adjust = adjust;
				return ABSENT;
			}

			aal_memcpy(place, &temp, sizeof(*place));
		}
		
		unit = place->pos.unit == MAX_UINT32 ? 0 : place->pos.unit;
		
		if ((lookup = reiser4_item_collision(place, hint)) < 0)
			return lookup;
		
		adjust += place->pos.unit - unit;

		if (place->pos.unit < units)
			break;
	}
	
	place->key.adjust = adjust;
	
	return PRESENT;
}

/* Makes search of the leftmost item/unit with the same key as passed @key is
   starting from @place. This is needed to work with key collisions. */
static errno_t reiser4_tree_collision_start(reiser4_tree_t *tree,
					    reiser4_place_t *place,
					    reiser4_key_t *key)
{
        reiser4_place_t walk;
                                                                                     
        aal_assert("umka-2396", key != NULL);
        aal_assert("umka-2388", tree != NULL);
        aal_assert("umka-2389", place != NULL);
                                                                                     
        if (reiser4_place_fetch(place))
                return -EINVAL;
	
        aal_memcpy(&walk, place, sizeof(*place));
                                                                                     
        /* Main loop until leftmost node reached. */
        while (walk.node) {
                int32_t i;
                                                                                     
                /* Loop through the items of the current node */
                for (i = walk.pos.item - 1; i >= 0; i--) {
                        walk.pos.item = i;
                                                                                     
                        /* Fetching item info */
                        if (reiser4_place_fetch(&walk))
                                return -EINVAL;
                                                                                     
                        /* If items of different objects, get out here. */
                        if (reiser4_key_compshort(&walk.key, key))
                                return 0;
                        
			/* If the maxreal key does not match anymore, 
			   get out here. This clause is needed for the case 
			   of corruption when not directory item has a NAME 
			   minor. */
			if (walk.plug->balance->maxposs_key) {
				reiser4_key_t maxkey;

				reiser4_item_maxposs_key(&walk, &maxkey);
				
				if (reiser4_key_compfull(&maxkey, key) < 0)
					return 0;
			}
			
                        /* If item's lookup is implemented, we use it. Item key
                           comparing is used otherwise. */
                        if (walk.plug->balance->lookup) {
				lookup_hint_t lhint;

				lhint.key = key;
				
                                switch (objcall(&walk, balance->lookup, 
						&lhint, FIND_EXACT))
                                {
                                case PRESENT:
                                        aal_memcpy(place, &walk, sizeof(*place));
                                        break;
                                default:
                                        return 0;
                                }
                        } else if (!reiser4_key_compfull(&walk.key, key)) {
				aal_memcpy(place, &walk, sizeof(*place));
                        } else {
				return 0;
                        }
                }
                                                                                     
                /* Getting left neighbour node. */
                reiser4_node_lock(place->node);
                reiser4_tree_ltrt_node(tree, walk.node, DIR_LEFT);
                reiser4_node_unlock(place->node);
                                                                                     
                /* Initializing @walk by neighbour node and last item. */
                if ((walk.node = walk.node->left)) {
                        int32_t items = reiser4_node_items(walk.node);
                                                                                     
                        /* Here should be namely @items, not @items - 1, because
                           we will access @walk.item - 1 on the next cycle */
                        POS_INIT(&walk.pos, items, MAX_UINT32);
                }
        }
                                                                                     
        return 0;
}
#endif

#define restore_and_exit(res) \
	do {hint->key = saved; return res;} while (0)

/* Makes search in the tree by specified @key. Fills passed place by data of
   found item. That is body pointer, plugin, etc. */
lookup_t reiser4_tree_lookup(reiser4_tree_t *tree, lookup_hint_t *hint,
			     lookup_bias_t bias, reiser4_place_t *place)
{
	lookup_t res;
	reiser4_key_t *saved;
	reiser4_key_t wanted;

	aal_assert("umka-742", hint != NULL);
	aal_assert("umka-1760", tree != NULL);
	aal_assert("umka-2057", place != NULL);
	aal_assert("umka-3088", hint->key != NULL);

	/* We store @key in @wanted. All consequence code will use @wan. This is
	   needed, because @key might point to @place->key in @place and will be
	   corrupted during lookup. */
	aal_memcpy(&wanted, hint->key, sizeof(wanted));

	/* Setting hint->key to stored local key in order to keep not corrupted
	   if it points to @place->key and will be chnaged after @place is
	   modified. It will be restored after lookup is finished. */
	saved = hint->key;
	hint->key = &wanted;

	/* Zeroing place just of rcase it was not initialized before to prevent
	   having some garbage in it. */
	aal_memset(place, 0, sizeof(*place));

#ifndef ENABLE_MINIMAL
	/* Making sure that root exists. If not, getting out with @place
	   initialized by NULL root. */
	if (reiser4_tree_fresh(tree)) {
		reiser4_place_assign(place, NULL, 0, MAX_UINT32);
		return ABSENT;
	} else {
#endif
		if ((res = reiser4_tree_load_root(tree)) < 0)
			return res;
		
		reiser4_place_assign(place, tree->root, 0, MAX_UINT32);
#ifndef ENABLE_MINIMAL
	}
#endif

	while (1) {
		uint32_t clevel;
		lookup_bias_t cbias;
		blk_t blk;

		clevel = reiser4_node_get_level(place->node);
		cbias = (clevel > hint->level ? FIND_EXACT : bias);
		
		/* Looking up for key inside node. Result of lookuping will be
		   stored in &place->pos. */
		res = reiser4_node_lookup(place->node, hint,
					  cbias, &place->pos);

		/* Check if we should finish lookup because we reach stop level
		   or some error occurred during last node lookup. */
		if (clevel <= hint->level || res < 0) {
			if (res == PRESENT) {
#ifndef ENABLE_MINIMAL
				if (reiser4_tree_collision_start(tree, place,
								 &wanted))
				{
					restore_and_exit(-EIO);
				}

				/* Handle collisions. */
				if (hint->collision) {
					tree_entity_t *t;
					
					t = (tree_entity_t *)tree;
					
					res = hint->collision(t, place, 
							      hint->hint);
					
					if (res < 0)
						restore_and_exit(res);
				}
#endif
				/* Fetching item at @place if key is found */
				if (reiser4_place_fetch(place))
					restore_and_exit(-EIO);

			}
			
			restore_and_exit(res);
		}

		/* Initializing @place. This should be done before using any
		   item methods or access @place fields. */
		if (!reiser4_place_valid(place))
			restore_and_exit(ABSENT);
		
		if (reiser4_place_fetch(place))
			restore_and_exit(-EIO);
		
		/* Checking is item at @place is nodeptr one. If not, we correct
		   posision back. */
		if (!reiser4_item_branch(place->plug))
			restore_and_exit(res);
		
		/* Loading node by its nodeptr item at @place. */
		blk = reiser4_item_down_link(place);
		if (!(place->node = reiser4_tree_load_node(tree, place->node,
							   blk)))
		{
			aal_error("Can't load child node %llu.", blk);
			restore_and_exit(-EIO);
		}

		/* Zero the plug pointer. */
		place->plug = NULL;
	}
	
	restore_and_exit(ABSENT);
}

#ifndef ENABLE_MINIMAL
/* Returns 1 if passed @tree has minimal possible height and thus cannot be
   dried out. Othersize 0 is returned. */
bool_t reiser4_tree_minimal(reiser4_tree_t *tree) {
	return (reiser4_tree_get_height(tree) <= 2);
}

/* Returns 1 if root node contain one item, that is, tree is singular and should
   be dried out. Otherwise 0 is returned. */
bool_t reiser4_tree_singular(reiser4_tree_t *tree) {
	return (reiser4_node_items(tree->root) == 1);
}

/* Returns 1 if tree has not root node and 0 otherwise. Tree has not root just
   after format instance is created and tree is initialized on fs with it. And
   thus tree has not any nodes in it. */
bool_t reiser4_tree_fresh(reiser4_tree_t *tree) {
	aal_assert("umka-1930", tree != NULL);
	return (reiser4_tree_get_root(tree) == INVAL_BLK);
}

/* Updates key at passed @place by passed @key by means of using
   node_update_key() function in recursive maner. This function is used for
   update all internal left delimiting keys after balancing on underlying
   levels. */
errno_t reiser4_tree_update_keys(reiser4_tree_t *tree,
				 reiser4_place_t *place,
				 reiser4_key_t *key)
{
	errno_t res;
	reiser4_key_t pkey;
	
	aal_assert("umka-1892", tree != NULL);
	aal_assert("umka-1893", place != NULL);
	aal_assert("umka-1894", key != NULL);

	/* Small improvement. Do not update keys if this is not really
	   needed. */
	reiser4_item_get_key(place, &pkey);

	if (!reiser4_key_compfull(&pkey, key))
		return 0;
	
	aal_memcpy(&place->key, key, sizeof(*key));

	/* Check if we should update keys on higher levels of tree. */
	if (reiser4_place_leftmost(place) && place->node->p.node) {
		reiser4_place_t *parent = &place->node->p;
		
		if ((res = reiser4_tree_update_keys(tree, parent, key)))
			return res;
	}

	/* Actual key updating in @place->node. */
	return reiser4_node_update_key(place->node, &place->pos, key);
}

/* Returns 1 for attached node and 0 otherwise. */
bool_t reiser4_tree_attached_node(reiser4_tree_t *tree,
				  reiser4_node_t *node)
{
	aal_assert("umka-3128", tree != NULL);
	aal_assert("umka-3129", node != NULL);

	if (reiser4_tree_fresh(tree))
		return 0;

	if (reiser4_tree_root_node(tree, node))
		return 1;

	return (node->p.node != NULL && 
		(reiser4_tree_t *)node->tree == tree);
}

/* This function inserts new nodeptr item to the tree and in such way attaches
   passed @node to tree. It also connects passed @node into tree cache. */
errno_t reiser4_tree_attach_node(reiser4_tree_t *tree, reiser4_node_t *node,
				 reiser4_place_t *place, uint32_t flags)
{
	errno_t res;
	uint8_t level;
	ptr_hint_t ptr;
	trans_hint_t hint;

	aal_assert("umka-913", tree != NULL);
	aal_assert("umka-916", node != NULL);
	aal_assert("umka-3104", place != NULL);

	aal_memset(&hint, 0, sizeof(hint));
	
	hint.count = 1;
	hint.specific = &ptr;
	hint.shift_flags = flags;
	hint.plug = (reiser4_item_plug_t *)tree->ent.tset[TSET_NODEPTR];

	ptr.width = 1;
	ptr.start = node->block->nr;
	
	level = reiser4_node_get_level(node) + 1;
	reiser4_node_leftmost_key(node, &hint.offset);

	/* Inserting node ptr into tree. */
	if ((res = reiser4_tree_insert(tree, place, &hint, level)) < 0) {
		aal_error("Can't insert nodeptr item to the tree.");
		return res;
	}

	/* Connecting node to tree cache. */
	if ((res = reiser4_tree_connect_node(tree, place->node, node))) {
		aal_error("Can't connect node %llu to tree cache.",
			  node->block->nr);
		return res;
	}

	/* This is needed to update sibling pointers, as new attached node may
	   be inserted between two nodes, that has established sibling links and
	   they should be changed. */
	reiser4_tree_ltrt_node(tree, node, DIR_LEFT);
	reiser4_tree_ltrt_node(tree, node, DIR_RIGHT);
	
	return 0;
}

/* Removes passed @node from the on-disk tree and cache structures. That is
   removes nodeptr item from the tree and node instance itself from its parent
   children list. */
errno_t reiser4_tree_detach_node(reiser4_tree_t *tree,
				 reiser4_node_t *node,
				 uint32_t flags)
{
	errno_t res;
	reiser4_place_t parent;
	
	aal_assert("umka-1726", tree != NULL);
	aal_assert("umka-1727", node != NULL);

	/* Save parent pos, because it will be needed later and it is destroed
	   by tree_disconnect_node(). */
	parent = node->p;

	/* Disconnecting @node from tree. This should be done before removing
	   nodeptr item in parent, as parent may get empty and we will unable to
	   release it as it is locked by connect @node. */
	if ((res = reiser4_tree_disconnect_node(tree, node))) {
		aal_error("Can't disconnect node %llu "
			  "from tree during its detaching.",
			  node->block->nr);
		return res;
	}
	
        /* Disconnecting node from parent node if any. */
	if (!reiser4_tree_root_node(tree, node)) {
		trans_hint_t hint;
		
		hint.count = 1;
		hint.place_func = NULL;
		hint.region_func = NULL;
		hint.shift_flags = flags;

		/* Removing nodeptr item/unit at @parent. */
		return reiser4_tree_remove(tree, &parent, &hint);
	} else {
		/* Putting INVAL_BLK into root block number in super block to
		   let know that old root is detached. */
		reiser4_tree_set_root(tree, INVAL_BLK);
	}
	
	return 0;
}

/* This function forces tree to grow by one level and sets it up after the
   growing. This occurs when after next balancing root node needs to accept new
   nodeptr item, but has not free space enough.  */
errno_t reiser4_tree_growup(reiser4_tree_t *tree) {
	errno_t res;
	uint32_t height;
	reiser4_place_t aplace;
	reiser4_node_t *new_root;
	reiser4_node_t *old_root;

	aal_assert("umka-1701", tree != NULL);
	aal_assert("umka-1736", tree->root != NULL);
	
	height = reiser4_tree_get_height(tree) + 1;

	/* Allocating new root node. */
	if (!(new_root = reiser4_tree_alloc_node(tree, height)))
		return -ENOSPC;

	if ((res = reiser4_tree_load_root(tree)))
		goto error_free_new_root;
	
	old_root = tree->root;

	/* Detaching old root from tree first. */
	if ((res = reiser4_tree_detach_node(tree, old_root,
					    SF_DEFAULT)))
	{
		aal_error("Can't detach old root node %llu from "
			  "tree during tree growing up.",
			  old_root->block->nr);
		goto error_return_root;
	}
	
	/* Assign new root node, changing tree height and root node blk in
	   format used in fs instance tree belongs to. */
	if ((res = reiser4_tree_assign_root(tree, new_root))) {
		aal_error("Can't assign new root node "
			  "durring tree growing up.");
		goto error_free_new_root;
	}

	/* Attaching old root to tree back. Now it should be attached to new
	   root node, not to virtual super block. */
	reiser4_node_lock(new_root);

	/* Place old root will be attached. */
	reiser4_place_assign(&aplace, new_root, 0, MAX_UINT32);

	if ((res = reiser4_tree_attach_node(tree, old_root,
					    &aplace, SF_DEFAULT)))
	{
		aal_error("Can't attach node %llu to tree during"
			  "tree growing up.", old_root->block->nr);
		reiser4_node_unlock(new_root);
		goto error_return_root;
	}

	reiser4_node_unlock(new_root);

	return 0;

error_return_root:
	reiser4_tree_assign_root(tree, old_root);
error_free_new_root:
	reiser4_tree_release_node(tree, new_root);
	return res;
}

/* Decreases tree height by one level. This occurs when tree gets singular (root
   has one nodeptr item) after one of removals. */
errno_t reiser4_tree_dryout(reiser4_tree_t *tree) {
	errno_t res;
	reiser4_place_t place;
	reiser4_node_t *new_root;
	reiser4_node_t *old_root;

	aal_assert("umka-1731", tree != NULL);
	aal_assert("umka-1737", tree->root != NULL);

	if (reiser4_tree_minimal(tree))
		return -EINVAL;

	/* Rasing up the root node if it exists. */
	if ((res = reiser4_tree_load_root(tree)))
		return res;

	old_root = tree->root;
	
	/* Getting new root as the first child of the old root node. */
	reiser4_place_assign(&place, old_root, 0, 0);

	if (!(new_root = reiser4_tree_child_node(tree, &place))) {
		aal_error("Can't load new root during "
			  "drying tree out.");
		return -EINVAL;
	}

	/* Detaching new root from its parent (old_root). This will also release
	   parent node from tree, as it will be empty. */
	if ((res = reiser4_tree_detach_node(tree, new_root,
					    SF_DEFAULT)))
	{
		aal_error("Can't detach new root from "
			  "tree during tree drying out.");
		return res;
	}

	/* Assign new root node. Setting tree height to new root level and root
	   block number to new root block number. */
	if ((res = reiser4_tree_assign_root(tree, new_root))) {
		aal_error("Can't assign new root node "
			  "durring tree drying out.");
		return res;
	}

	return 0;
}

/* Tries to shift items and units from @place to passed @neig node. After that
   it's finished, place will contain new insert point, which may be used for
   inserting item/unit to it. */
errno_t reiser4_tree_shift(reiser4_tree_t *tree, reiser4_place_t *place,
			   reiser4_node_t *neig, uint32_t flags)
{
	errno_t res;
	reiser4_node_t *node;
	reiser4_node_t *right;

	shift_hint_t hint;
	reiser4_key_t lkey;
	
	uint8_t start;

	aal_assert("umka-1225", tree != NULL);
	aal_assert("umka-1226", place != NULL);
	aal_assert("umka-1227", neig != NULL);
    
	aal_memset(&hint, 0, sizeof(hint));

	/* Prepares shift hint. Initializing shift flags (shift direction, is it
	   allowed to create new nodes, etc) and insert point. */
	node = place->node;
	hint.control = flags;
	hint.pos = place->pos;

	/* Needed for the left shift. */
	start = reiser4_node_items(neig);
	start = start ? start - 1 : 0;
	
	/* Perform node shift from @node to @neig. */
	if ((res = reiser4_node_shift(node, neig, &hint)))
		return res;

	/* Check if insert point was moved to neighbour node. If so, assign
	   neightbour node to insert point coord. */
	if (hint.result & SF_MOVE_POINT)
		place->node = neig;

	/* Updating @place->pos by hint->pos if there is permission flag. */
	if (hint.control & SF_UPDATE_POINT)
		place->pos = hint.pos;

	right = (hint.control & SF_ALLOW_LEFT) ? node : neig;

	/* Check if we need update key in insert part of tree. That is if source
	   node is not empty and there was actually moved at least one item or
	   unit. */
	if (reiser4_node_items(right) > 0 && hint.update) {
		/* Check if node is connected to tree or it is not root and
		   updating left delimiting keys if it makes sense at all. */
		if (right->p.node != NULL) {
			reiser4_place_t parent;

			/* Getting leftmost key from @right. */
			reiser4_node_leftmost_key(right, &lkey);

			/* Recursive updating of all internal keys that supposed
			   to be updated. */
			aal_memcpy(&parent, &right->p, sizeof(parent));
				
			if ((res = reiser4_tree_update_keys(tree, &parent, &lkey)))
				return res;
		}
	}

	/* Updating @node and @neig children's parent position. */
	if (reiser4_node_get_level(node) > LEAF_LEVEL) {
		reiser4_node_t *left = (hint.control & SF_ALLOW_LEFT) ? neig : NULL;

		if (left && reiser4_node_items(left) > 0) {
			if ((res = reiser4_tree_update_node(tree, left, start, 
							    reiser4_node_items(left))))
			{
				return res;
			}
		}
	
		if (reiser4_node_items(right) > 0) {
			if ((res = reiser4_tree_update_node(tree, right, 0, 
							    reiser4_node_items(right))))
			{
				return res;
			}
		}
	}
	
	return 0;
}

/* Shifts data from passed @place to one of neighbour nodes basing on passed
   @flags. */
static errno_t tree_shift_todir(reiser4_tree_t *tree, reiser4_place_t *place,
				uint32_t flags, int direction)
{
	errno_t res;
	uint32_t shift_flags = 0;
	reiser4_node_t *neighbour;
	reiser4_node_t *old_node;

	if (direction == DIR_LEFT && (SF_ALLOW_LEFT & flags))
		shift_flags = SF_ALLOW_LEFT;

	if (direction == DIR_RIGHT && (SF_ALLOW_RIGHT & flags))
		shift_flags = SF_ALLOW_RIGHT;

	/* Setting up shift flags. */
	shift_flags |= SF_UPDATE_POINT;

	if (SF_ALLOW_MERGE & flags)
		shift_flags |= SF_ALLOW_MERGE;
	
	if (SF_HOLD_POS & flags)
		shift_flags |= SF_HOLD_POS;

	old_node = place->node;

	/* Getting neighbour. */
	neighbour = direction == DIR_LEFT ?
		place->node->left : place->node->right;

	aal_assert("umka-3096", neighbour != NULL);
	
	/* Shift items from @place to @left neighbour. */
	if ((res = reiser4_tree_shift(tree, place, neighbour, shift_flags)))
		return res;

	if (reiser4_node_items(old_node) == 0 &&
	    old_node != place->node)
	{
		if (reiser4_node_locked(old_node)) {
			old_node->flags |= NF_HEARD_BANSHEE;
		} else {
			reiser4_node_lock(place->node);
			
			if ((res = reiser4_tree_discard_node(tree, old_node))) {
				reiser4_node_unlock(place->node);
				return res;
			}

			reiser4_node_unlock(place->node);
		}
	}

	return 0;
}

/* This calculates if space in passed @needed is enough for passed @needed. */
static inline int32_t tree_calc_space(reiser4_place_t *place, 
				      uint32_t ioverh) 
{
	uint32_t overh = reiser4_node_overhead(place->node);
	
	return reiser4_node_space(place->node) - 
		(place->pos.unit == MAX_UINT32 ? (overh + ioverh) : 0);
}

/* Makes space in tree to insert @ilen bytes of data. Returns space in insert
   point, or negative value for errors. */
int32_t reiser4_tree_expand(reiser4_tree_t *tree, reiser4_place_t *place,
			    reiser4_place_t *parent, uint32_t ilen, 
			    uint32_t ioverh, uint32_t flags)
{
	int alloc;
	errno_t res;
	uint8_t level;
	int32_t enough;

	aal_assert("umka-929", tree != NULL);
	aal_assert("umka-766", place != NULL);
	
	aal_assert("umka-3064", place->node != NULL);
	aal_assert("vpf-1543", ilen + ioverh <= 
		   reiser4_node_maxspace(place->node));

	if (reiser4_tree_fresh(tree))
		return -EINVAL;

	/* Check if there is enough space in insert point node. If so -- do
	   nothing but exit. Here is also check if node is empty. Then we exit
	   too and return available space in it. */
	if ((enough = tree_calc_space(place, ioverh)) >= (int32_t)ilen)
		return enough;

	/* Shifting data into left neighbour if it exists and left shift
	   allowing flag is specified. */
	if ((SF_ALLOW_LEFT & flags) && 
	    reiser4_tree_ltrt_node(tree, place->node, DIR_LEFT)) 
	{
		if ((res = tree_shift_todir(tree, place, flags, DIR_LEFT)))
			return res;
		
		if ((enough = tree_calc_space(place, ioverh)) >= (int32_t)ilen)
			return enough;
	}

	/* Shifting data into right neighbour if it exists and right shift
	   allowing flag is specified. */
	if ((SF_ALLOW_RIGHT & flags) && 
	    reiser4_tree_ltrt_node(tree, place->node, DIR_RIGHT)) 
	{
		if ((res = tree_shift_todir(tree, place, flags, DIR_RIGHT)))
			return res;
		
		if ((enough = tree_calc_space(place, ioverh)) >= (int32_t)ilen)
			return enough;
	}

	/* Check if we allowed to allocate new nodes if there still not enough
	   of space for insert @ilen bytes of data to tree. */
	if (!(SF_ALLOW_ALLOC & flags))
		return tree_calc_space(place, ioverh);
	
	/* Here we still have not enough free space for inserting item/unit into
	   the tree. Allocating new nodes and trying to shift data into
	   them. There are possible two tries to allocate new node and shift
	   insert point to one of them. */
	for (alloc = 0; enough < (int32_t)ilen && alloc < 2; alloc++) {
		reiser4_place_t save;
		reiser4_node_t *node;
		uint32_t shift_flags;
		uint32_t left_items;
		reiser4_place_t aplace;

		/* Saving place as it will be usefull for us later */
		aal_memcpy(&save, place, sizeof(*place));

		/* Allocating new node of @level */
		level = reiser4_node_get_level(place->node);

		if (!(node = reiser4_tree_alloc_node(tree, level)))
			return -ENOSPC;

		/* reiser4_place_leftmost cannot be used here because unit == 
		   0 mean we should stay on this item -- writing to the extent
		   with holes or inserting a SD extention. */
		if (place->pos.unit == MAX_UINT32 && place->pos.item == 0) {
			/* Do not shift anything for the leftmost position.
			   Just insert the new node and move the insert point
			   there. */
			aal_memcpy(&aplace, &place->node->p, sizeof(aplace));
			place->node = node;
			place->pos.item = 0;
			place->pos.unit = MAX_UINT32;
		} else {
			/* Setting up shift flags. */
			shift_flags = (SF_ALLOW_RIGHT | SF_UPDATE_POINT);

			if (SF_ALLOW_MERGE & flags)
				shift_flags |= SF_ALLOW_MERGE;

			if (SF_HOLD_POS & flags)
				shift_flags |= SF_HOLD_POS;
			
			/* We will allow to move insert point to neighbour node 
			   if we at first iteration in this loop or if place 
			   points behind the last unit of last item in current 
			   node. */
			if (alloc > 0 || reiser4_place_rightmost(place))
				shift_flags |= SF_MOVE_POINT;

			/* Shift data from @place to @node. Updating @place by 
			   new insert point. */
			if ((res = reiser4_tree_shift(tree, place, node, 
						      shift_flags)))
			{
				return res;
			}

			/* Preparing new @node parent place. */
			tree_next_child_pos(save.node, &aplace);
		}
		
		left_items = reiser4_node_items(save.node);
		
		if (left_items == 0) {
			reiser4_node_lock(save.node);

			/* If evth was moved to the new allocated node, it will
			   be attached below. Do not pack the tree here, avoid
			   2 balancings. */
			shift_flags = SF_DEFAULT & ~SF_ALLOW_PACK;
			
			if ((res = reiser4_tree_detach_node(tree, save.node,
							    shift_flags)))
			{
				reiser4_node_unlock(save.node);
				return res;
			}

			reiser4_node_unlock(save.node);
			aplace.pos.item--;
		}
		
		if (reiser4_node_items(node) > 0) {
			reiser4_node_lock(save.node);
			
			/* Growing tree in the case we splitted the root
			   node. */
			if (reiser4_tree_root_node(tree, save.node)) {
				reiser4_node_t *old_root = tree->root;
				
				if ((res = reiser4_tree_growup(tree))) {
					reiser4_node_unlock(save.node);
					return res;
				}

				tree_next_child_pos(old_root, &aplace);
			}

			/* Attach new node to tree if it is not empty. */
			if ((res = reiser4_tree_attach_node(tree, node,
							    &aplace,
							    SF_DEFAULT)))
			{
				reiser4_node_unlock(save.node);
				return res;
			}

			reiser4_node_unlock(save.node);
			/* Update the parent to the just attached node's parent.
			   Needed as @save.node may be detached already. */
			aal_memcpy(parent, &node->p, sizeof(*parent));
		} else {
			/* As node was not attached here, it will be attached in
			   caller function, so we needd to update @parent by
			   coord of attach. */

			aal_memcpy(parent, &aplace, sizeof(aplace));
		}
		
		/* If we are still in the same node, but on not-esistent 
		   anymore item, set unit position to MAX_UINT32. */
		if (save.node == place->node && place->pos.item >= left_items) {
			aal_assert("vpf-1793", place->pos.unit == MAX_UINT32);
		}
	
		/* Checking if it is enough of space in @place. */
		enough = tree_calc_space(place, ioverh);

		if (enough < (int32_t)ilen && save.node != place->node) {
			aal_bug("vpf-1796", "When the insertion point is "
				"moved to the new node, it is the second "
				"node allocation and there must be enother "
				"space.");
		}

		/* Releasing new allocated @node if it is empty and insert point
		   is not inside it. */
		if (reiser4_node_items(node) == 0 && node != place->node)
			reiser4_tree_release_node(tree, node);
		
		/* Releasing save.@node if it is empty and insert point is not
		   inside it. */
		if (left_items == 0 && save.node != place->node) {
			aal_bug("vpf-1795", "The insert point cannot "
				"move to the new node with all items.");
		}
	}

	return enough;
}

/* Packs node in @place by means of using shift to left. */
errno_t reiser4_tree_shrink(reiser4_tree_t *tree, reiser4_place_t *place) {
	errno_t res;
	uint32_t flags;
	reiser4_node_t *left, *right;

	aal_assert("umka-1784", tree != NULL);
	aal_assert("umka-1783", place != NULL);

	/* Shift flags to be used in packing. */
	flags = (SF_ALLOW_LEFT | SF_ALLOW_MERGE);
	
	/* Packing node in order to keep the tree in well packed state
	   anyway. Here we will shift data from the target node to its left
	   neighbour node. */
	if ((left = reiser4_tree_ltrt_node(tree, place->node, DIR_LEFT))) {
		if ((res = reiser4_tree_shift(tree, place, left, flags))) {
			aal_error("Can't pack node %llu into left.",
				  place_blknr(place));
			return res;
		}
	}
		
	if (reiser4_node_items(place->node) > 0) {
		/* Shifting the data from the right neigbour node into the
		   target node. */
		if ((right = reiser4_tree_ltrt_node(tree, place->node,
						    DIR_RIGHT)))
		{
			reiser4_place_t bogus;

			reiser4_place_assign(&bogus, right, 0, MAX_UINT32);
	    
			if ((res = reiser4_tree_shift(tree, &bogus,
						      place->node, flags)))
			{
				aal_error("Can't pack node %llu into right.",
					  right->block->nr);
				return res;
			}

			/* Check if @bogus.node got empty. If so -- it will be
			   released from tree and tree cache. */
			if (reiser4_node_items(bogus.node) == 0) {
				if (reiser4_node_locked(bogus.node)) {
					bogus.node->flags |= NF_HEARD_BANSHEE;
				}else {
					reiser4_tree_discard_node(tree, bogus.node);
				}
			}
		}
	} else {
		/* Release node, because it got empty. */
		if (reiser4_node_locked(place->node)) {
			place->node->flags |= NF_HEARD_BANSHEE;
		} else {
			if ((res = reiser4_tree_discard_node(tree, place->node)))
				return res;
				
			place->node = NULL;
		}
	}

	/* Drying tree up in the case root node has only one item. */
	if (tree->root && reiser4_tree_singular(tree) &&
	    !reiser4_tree_minimal(tree))
	{
		if ((res = reiser4_tree_dryout(tree)))
			return res;
	}

	return 0;
}

/* Splits out the tree from passed @place up until passed @level is
   reached. This is used in fsck and in extents write code. */
static errno_t reiser4_tree_split(reiser4_tree_t *tree, 
				  reiser4_place_t *place,
				  uint8_t level)
{
	errno_t res;
	uint32_t flags;
	uint32_t clevel;
	reiser4_node_t *node;
	
	aal_assert("vpf-674", level > 0);
	aal_assert("vpf-672", tree != NULL);
	aal_assert("vpf-673", place != NULL);
	aal_assert("vpf-813", place->node != NULL);

	clevel = reiser4_node_get_level(place->node);
	aal_assert("vpf-680", clevel < level);

	/* Loop until desired @level is reached.*/
	while (clevel < level) {
		aal_assert("vpf-676", place->node->p.node != NULL);

		/* Check if @place points inside node. That is should we split
		   node or not. */
		if (!reiser4_place_leftmost(place) &&
		    !reiser4_place_rightmost(place))
		{
			reiser4_place_t aplace;
			
			/* We are not on the border, split @place->node. That is
			   allocate new right neighbour node and move all item
			   right to @place->pos to new allocated node. */
			if (!(node = reiser4_tree_alloc_node(tree, clevel))) {
				aal_error("Tree failed to allocate "
					  "a new node.");
				return -EINVAL;
			}

			/* Flags allowed to be used during shift. */
			flags = (SF_ALLOW_RIGHT | SF_UPDATE_POINT |
				 SF_ALLOW_MERGE);

			/* Perform shift from @place->node to @node. */
			if ((res = reiser4_tree_shift(tree, place, node,
						      flags)))
			{
				aal_error("Tree failed to shift into a "
					  "new allocated node.");
				goto error_free_node;
			}

			aal_assert("umka-3048", reiser4_node_items(node) > 0);
			
			reiser4_node_lock(place->node);

			/* Check if we should grow up the tree. */
			if (reiser4_tree_root_node(tree, place->node)) {
				reiser4_node_t *old_root = tree->root;
				
				if ((res = reiser4_tree_growup(tree))) {
					reiser4_node_unlock(place->node);
					goto error_free_node;
				}

				tree_next_child_pos(old_root, &aplace);
			} else
				tree_next_child_pos(place->node, &aplace);

			/* Attach new node to tree. */
			if ((res = reiser4_tree_attach_node(tree, node,
							    &aplace,
							    SF_DEFAULT)))
			{
				aal_error("Tree is failed to attach "
					  "node during split opeartion.");
				reiser4_node_unlock(place->node);
				goto error_free_node;
			}
			
			reiser4_node_unlock(place->node);

			/* Updating @place by parent coord from @place. */
			aal_memcpy(place, &node->p, sizeof(*place));
		} else {
			int rightmost = reiser4_place_rightmost(place);
			
			/* There is nothing to move out. We are on node border
			   (rightmost or leftmost). Here we should just go up by
			   one level and increment position if @place was at
			   rightmost position in the node. */
			aal_memcpy(place, &place->node->p, sizeof(*place));

			if (rightmost) {
				bool_t whole;
				
				whole = (place->pos.unit == MAX_UINT32);
				reiser4_place_inc(place, whole);
			}
		}

		clevel++;
	}
	
	return 0;
	
 error_free_node:
	reiser4_tree_release_node(tree, node);
	return res;
}

/* This grows tree until requested level reached. */
static inline errno_t tree_growup_level(reiser4_tree_t *tree,
					uint8_t level)
{
	if (level <= reiser4_tree_get_height(tree))
		return 0;
	
	if (reiser4_tree_fresh(tree))
		return -EINVAL;
		
	while (level > reiser4_tree_get_height(tree)) {
		errno_t res;
		
		if ((res = reiser4_tree_growup(tree)))
			return res;
	}
		
	return 0;
}

/* This function prepares the tree for insert. That is it allocated root and
   first leaf if needed, splits the tree, etc. */
static inline errno_t tree_prep_modify(reiser4_tree_t *tree,
				       reiser4_place_t *parent,
				       reiser4_place_t *place,
				       uint8_t level)
{
	reiser4_node_t *root;
	uint32_t height;
	errno_t res;

	/* Check if tree has at least one node. If so -- load root node. Tree
	   has not nodes just after it is created. Root node and first leaf will
	   be created on demand then. */
	if (!reiser4_tree_fresh(tree)) {
		aal_assert("umka-3124", place->node != NULL);
		
		reiser4_node_lock(place->node);
		
		if ((res = reiser4_tree_load_root(tree))) {
			reiser4_node_unlock(place->node);
			return res;
		}

		reiser4_node_unlock(place->node);
	}

	/* Checking if we have the tree with height less than requested
	   level. If so, we should grow the tree up to requested level. */
	if (level > reiser4_tree_get_height(tree)) {
		reiser4_node_lock(place->node);

		if ((res = tree_growup_level(tree, level))) {
			reiser4_node_unlock(place->node);
			return res;
		}

		reiser4_node_unlock(place->node);
	}

	if (!reiser4_tree_fresh(tree)) {
		if (level < reiser4_node_get_level(place->node)) {
			aal_memcpy(parent, place, sizeof(*place));
			
			/* Allocating node of requested level and assign place
			   for insert to it. This is the case, when we insert a
			   tail among extents. That is previous lookup stoped on
			   twig level and now we have to allocate a node of
			   requested level, insert tail to it and then attach
			   new node to tree. */
			if (!(place->node = reiser4_tree_alloc_node(tree, level)))
				return -ENOSPC;

			POS_INIT(&place->pos, 0, MAX_UINT32);
		} else {
			if (level > reiser4_node_get_level(place->node)) {
				/* Prepare the tree for insertion at the @level. 
				   Here is case when extent is going to inserted.
				   As lookup goes to the leaf level, we split 
				   tree from leaf level up to requested @level,
				   which is level new extent should be inserted. */
				if ((res = reiser4_tree_split(tree, place, level)))
					return res;
			}

			/* Here we do not need to update @parent, as there is no
			   new not attached nodes. */
		}
	} else {
		aal_assert("umka-3055", place->node == NULL);
		
		/* Preparing tree for insert first item (allocating root,
		   etc). */
		height = reiser4_tree_get_height(tree);

		if (!(root = reiser4_tree_alloc_node(tree, height)))
			return -ENOSPC;

		if ((res = reiser4_tree_assign_root(tree, root)))
			return res;

		if (level == reiser4_node_get_level(root)) {
			place->node = root;
		} else {
			if (!(place->node = reiser4_tree_alloc_node(tree, level)))
				return -ENOMEM;
		}
	
		POS_INIT(&place->pos, 0, MAX_UINT32);
		reiser4_place_init(parent, root, &place->pos);
	}
	
	return 0;
}

static inline errno_t tree_post_modify(reiser4_tree_t *tree, 
				       reiser4_place_t *place) 
{
	uint32_t items;
	errno_t res;

	/* Nothing to be done if no item is left in the node. */
	if (!(items = reiser4_node_items(place->node)))
		return 0;

	/* Initializing insert point place. */
	if ((res = reiser4_place_fetch(place)))
		return res;

	/* Parent keys will be updated if we inserted item/unit into leftmost
	   pos and if target node has parent. */
	if (reiser4_place_leftmost(place) && place->node->p.node) {
		/* We will not update keys if node is not attached to tree
		   yet. This will be done later on its attach. */
		reiser4_place_t *parent = &place->node->p;
		reiser4_key_t lkey;

		reiser4_item_get_key(place, &lkey);

		if ((res = reiser4_tree_update_keys(tree, parent, &lkey)))
			return res;
	}

	/* Update @place->node children's parent pointers. */
	if (reiser4_node_get_level(place->node) > LEAF_LEVEL) {
		if ((res = reiser4_tree_update_node(tree, place->node, 
						    place->pos.item, items)))
		{
			return res;
		}
	}
	
	return 0;
}

/* Main function for tree modifications. It is used for inserting data to tree
   (stat data items, directries) or writting (tails, extents). */
int64_t reiser4_tree_modify(reiser4_tree_t *tree, reiser4_place_t *place,
			    trans_hint_t *hint, uint8_t level,
			    estimate_func_t estimate_func,
			    modify_func_t modify_func)
{
	bool_t mode;
	errno_t res;
	int32_t space;
	int32_t write;
	reiser4_place_t parent;

	aal_assert("umka-2673", tree != NULL);
	aal_assert("umka-2674", hint != NULL);
	aal_assert("umka-1645", hint->plug != NULL);
	aal_assert("umka-1644", place != NULL);
	
	aal_assert("umka-2676", modify_func != NULL);
	aal_assert("umka-2675", estimate_func != NULL);

	/* Preparing the tree modification. This may include splitting, 
	   allocating root, etc. */
	if ((res = tree_prep_modify(tree, &parent, place, level)))
		return res;

	/* Estimating item/unit to inserted/written to tree. */
	if ((res = estimate_func(place, hint)))
		return res;
	
	/* Needed space to be prepared in tree. */
	mode = (place->pos.unit == MAX_UINT32);
		
	/* Preparing space in tree. */
	space = reiser4_tree_expand(tree, place, &parent, hint->len, 
				    hint->overhead, hint->shift_flags);
	
	/* Needed space is the length of the item + an item overhead on 
	   the item creation if needed. */
	if (place->pos.unit != MAX_UINT32)
		hint->overhead = 0;

	if (space < 0) {
		aal_error("Can't prepare space in tree.");
		return space;
	} else {
		/* Checking if we still have less space than needed. This is
		   ENOSPC case if we tried to insert data. */
		if (space < hint->len)
			return -ENOSPC;
	}

	reiser4_node_lock(place->node);
	
	/* Inserting/writing data to node. */
	if ((write = modify_func(place->node, &place->pos, hint)) < 0) {
		aal_error("Can't insert data to node %llu.",
			  place_blknr(place));
		return write;
	}
	
	reiser4_node_unlock(place->node);

	if ((res = tree_post_modify(tree, place)))
		return res;
	
        /* If make space function allocates new node, we should attach it to the
	   tree. Also, here we should handle the special case, when tree root
	   should be changed. */
	if (place->node != tree->root && !place->node->p.node) {
		/* Check if insert was on root node level. If so -- growing tree
		   up by one. */
		if (level == reiser4_tree_get_height(tree)) {
			reiser4_node_t *old_root = tree->root;
			
			reiser4_node_lock(place->node);

			if ((res = reiser4_tree_growup(tree))) {
				aal_error("Can't grow tree up during "
					  "modifying it.");
				reiser4_node_unlock(place->node);
				return res;
			}
			
			reiser4_node_unlock(place->node);

			/* Handling tree growth case. */
			tree_next_child_pos(old_root, &parent);
		}
		
		/* Attaching new node to the tree. */
		if ((res = reiser4_tree_attach_node(tree, place->node, &parent,
						    hint->shift_flags)))
		{
			aal_error("Can't attach node %llu to tree.",
				  place_blknr(place));
			return res;
		}
	}

	/* Calling @hint->place_func if a new item was created. */
	if (hint->place_func && place->pos.unit == MAX_UINT32) {
		if ((res = hint->place_func(place, hint->data)))
			return res;
	}

	return write;
}

/* Estimates how many bytes is needed to insert data described by @hint. */
static errno_t cb_tree_prep_insert(reiser4_place_t *place, trans_hint_t *hint) {
	aal_assert("umka-2440", hint != NULL);
	aal_assert("umka-2439", place != NULL);

	hint->len = 0;
	hint->overhead = 0;

	return plugcall(hint->plug->object, prep_insert, place, hint);
}

static errno_t cb_tree_insert(reiser4_node_t *node, pos_t *pos, 
			      trans_hint_t *hint) 
{
	return objcall(node, insert, pos, hint);
}

/* Inserts data to the tree. This function is used for inserting items which are
   not file body items, that is statdata, directory, etc. */
int64_t reiser4_tree_insert(reiser4_tree_t *tree, reiser4_place_t *place,
			    trans_hint_t *hint, uint8_t level)
{
	return reiser4_tree_modify(tree, place, hint, level, 
				   cb_tree_prep_insert, cb_tree_insert);
}

/* Estimates how many bytes is needed to write data described by @hint. */
static errno_t cb_tree_prep_write(reiser4_place_t *place, trans_hint_t *hint) {
	aal_assert("umka-3007", hint != NULL);
	aal_assert("umka-3008", place != NULL);

	hint->len = 0;
	hint->overhead = 0;

	return plugcall(hint->plug->object, prep_write, place, hint);
}

static errno_t cb_tree_write(reiser4_node_t *node, 
			     pos_t *pos, trans_hint_t *hint) 
{
	return objcall(node, write, pos, hint);
}

int64_t reiser4_tree_write(reiser4_tree_t *tree, reiser4_place_t *place,
			   trans_hint_t *hint, uint8_t level)
{
	return reiser4_tree_modify(tree, place, hint, level,
				   cb_tree_prep_write, cb_tree_write);
}

/* Removes item/unit at @place. */
errno_t reiser4_tree_remove(reiser4_tree_t *tree, 
			    reiser4_place_t *place,
			    trans_hint_t *hint)
{
	errno_t res;

	aal_assert("umka-2055", tree != NULL);
	aal_assert("umka-2056", place != NULL);
	aal_assert("umka-2392", hint != NULL);

	/* Removing iten/unit from the node. */
	if ((res = reiser4_node_remove(place->node, &place->pos, hint)))
		return res;

	if ((res = tree_post_modify(tree, place)))
		return res;
	
	/* Checking if the node became empty. If so, we release it, otherwise we
	   pack the tree about it. */
	if (reiser4_node_items(place->node) == 0) {
		if (reiser4_node_locked(place->node)) {
			place->node->flags |= NF_HEARD_BANSHEE;
		} else {
			if ((res = reiser4_tree_discard_node(tree, place->node)))
				return res;

			place->node = NULL;
		}
	} else {
		if (hint->shift_flags & SF_ALLOW_PACK) {
			if ((res = reiser4_tree_shrink(tree, place)))
				return res;
		}
	}

	/* Drying tree up in the case root node exists and tree is singular,
	   that is has only one item. Tree also should not be minimal of
	   height. Here root may be NULL due to nested call from
	   tree_dryout(). */
	if (tree->root && reiser4_tree_singular(tree) && 
	    !reiser4_tree_minimal(tree) && (hint->shift_flags & SF_ALLOW_PACK))
	{
		if ((res = reiser4_tree_dryout(tree)))
			return res;
	}
	
	return 0;
}

/* Traverses @node with passed callback functions as actions. */
errno_t reiser4_tree_trav_node(reiser4_tree_t *tree,
			       reiser4_node_t *node,
			       tree_open_func_t open_func,
			       node_func_t before_func,
			       place_func_t update_func,
			       node_func_t after_func,
			       void *data)
{
	errno_t res = 0;
	reiser4_place_t place;
	pos_t *pos = &place.pos;
 
	aal_assert("vpf-390", node != NULL);
	aal_assert("umka-1935", tree != NULL);
	
	if (open_func == NULL)
		open_func = (tree_open_func_t)reiser4_tree_child_node;

	/* Locking @node to make sure, that it will not be released while we are
	   working with it. Of course, it should be unlocked after we
	   finished. */
	reiser4_node_lock(node);

	if ((before_func && (res = before_func(node, data))))
		goto error_unlock_node;

	/* The loop though the items of current node */
	for (pos->item = 0; pos->item < reiser4_node_items(node);
	     pos->item++)
	{
		pos->unit = MAX_UINT32; 

		/* If there is a suspicion of a corruption, it must be checked
		   in before_func. All items must be opened here. */
		if (reiser4_place_open(&place, node, pos)) {
			aal_error("Node (%llu), item (%u): Can't open item "
				  "by place.", node->block->nr, pos->item);
			goto error_after_func;
		}

		if (!reiser4_item_branch(place.plug))
			continue;

		/* The loop though the units of the current item */
		for (pos->unit = 0; pos->unit < reiser4_item_units(&place);
		     pos->unit++)
		{
			reiser4_node_t *child = NULL;
			
			/* Opening the node by its pointer kept in @place */
			if ((child = open_func(tree, &place, data)) == INVAL_PTR)
				goto error_after_func;

			if (!child)
				goto update;

			/* Traversing the node */
			if ((res = reiser4_tree_trav_node(tree, child, open_func, 
							  before_func, update_func, 
							  after_func, data)) < 0)
			{
				goto error_after_func;
			}
			
		update:	
			if (update_func && (res = update_func(&place, data)))
				goto error_after_func;
		}
	}
	
	if (after_func)
		res = after_func(node, data);

	reiser4_tree_unlock_node(tree, node);
	return res;

 error_after_func:
	if (after_func)
		after_func(node, data);

 error_unlock_node:
	reiser4_tree_unlock_node(tree, node);
	return res;
}

/* Traverses tree with passed callback functions for each event. This is used
   for all tree traverse related operations like copy, measurements, etc. */
errno_t reiser4_tree_trav(reiser4_tree_t *tree,
			  tree_open_func_t open_func,
			  node_func_t before_func,
			  place_func_t update_func,
			  node_func_t after_func,
			  void *data)
{
	errno_t res;
	
	aal_assert("umka-1768", tree != NULL);

	if ((res = reiser4_tree_load_root(tree)))
		return res;
	
	return reiser4_tree_trav_node(tree, tree->root, open_func,
				      before_func, update_func,
				      after_func, data);
}

errno_t reiser4_tree_scan(reiser4_tree_t *tree, 
			  node_func_t pre_func, 
			  place_func_t func, 
			  void *data) 
{
        reiser4_key_t key, max;
        errno_t res;

        aal_assert("vpf-1423", tree != NULL);
        aal_assert("vpf-1424", func != NULL);

        if (reiser4_tree_fresh(tree))
                return -EINVAL;

        if ((res = reiser4_tree_load_root(tree)))
                return res;

        if (tree->root == NULL)
                return -EINVAL;

        /* Prepare the start and the end keys. */
        key.plug = max.plug = tree->key.plug;
        reiser4_key_minimal(&key);
        reiser4_key_maximal(&max);

        /* While not the end of the tree. */
        while (reiser4_key_compfull(&key, &max)) {
                reiser4_place_t place;
		lookup_hint_t hint;
                lookup_t lookup;
		pos_t *pos;

                /* Some items can be handled a few times due to key 
		   collisions. */
		hint.key = &key;
		hint.level = LEAF_LEVEL;
		hint.collision = NULL;

                /* Lookup the key. FIXME: it is possible to spped it up. */
                if ((lookup = reiser4_tree_lookup(tree, &hint, FIND_EXACT,
						  &place)) < 0)
		{
                        return lookup;
		}

		pos = &place.pos;

		if (pre_func) {
			if ((res = pre_func(place.node, data)) < 0)
				return res;
		
			/* If res != 0, lookup is needed. */
			if (res) continue;
		}
		
		/* Count may get change in func (e.g. after item fusing), so 
		   get it on every loop */
		while (1) {
			if (pos->item >= reiser4_node_items(place.node)) {
				/* All items are handled whithin this node, but 
				   lookup is not needed. To avoid infinite loop 
				   in the case of key collision, get the next item 
				   instead of another lookup call. */
				if ((res = reiser4_tree_next_place(tree, &place,
								   &place))) 
				{
					aal_error("Failed to get the next node.");
					return res;
				}

				if (!place.node)
					return 0;
			}
				
                        if ((res = reiser4_place_fetch(&place)))
                                return res;
			
			/* Go down to the child if branch. */
			if ((res = reiser4_item_branch(place.plug))) {
				if (!(place.node = 
				      reiser4_tree_child_node(tree, &place)))
				{
					return -EIO;
				}
		
				if (pre_func) {
					if ((res = pre_func(place.node, 
							    data)) < 0)
					{
						return res;
					}

					/* If res != 0, lookup is needed. */
					if (res) break;
				}

				place.pos.item = -1;
				continue;
			}
			
                        /* Get the key of the next item. */
                        if ((res = reiser4_tree_next_key(tree, &place, &key)))
                                return res;

                        /* Call func for the item. */
                        if ((res = func(&place, data)) < 0)
                                return res;

                        /* If res != 0, lookup is needed. */
                        if (res) break;

			pos->item++;
                }
        }

        return 0;
}

/* Makes copy of @src_tree to @dst_tree */
errno_t reiser4_tree_copy(reiser4_tree_t *src_tree,
			  reiser4_tree_t *dst_tree)
{
	aal_assert("umka-2304", src_tree != NULL);
	aal_assert("umka-2305", dst_tree != NULL);

	aal_error("Sorry, not implemented yet!");
	return -EINVAL;
}

/* Resizes @tree by @blocks */
errno_t reiser4_tree_resize(reiser4_tree_t *tree,
			    count_t blocks)
{
	aal_assert("umka-2323", tree != NULL);

	aal_error("Sorry, not implemented yet!");
	return -EINVAL;
}
#endif