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
|
/*****************************************************************************
Copyright (c) 1995, 2016, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2017, 2023, MariaDB Corporation.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
*****************************************************************************/
/******************************************************************//**
@file fsp/fsp0fsp.cc
File space management
Created 11/29/1995 Heikki Tuuri
***********************************************************************/
#include "fsp0fsp.h"
#include "buf0buf.h"
#include "fil0fil.h"
#include "fil0crypt.h"
#include "mtr0log.h"
#include "ut0byte.h"
#include "page0page.h"
#include "srv0srv.h"
#include "srv0start.h"
#include "ibuf0ibuf.h"
#include "btr0btr.h"
#include "btr0sea.h"
#include "dict0boot.h"
#include "log0log.h"
#include "dict0mem.h"
#include "fsp0types.h"
#include "log.h"
/** Returns the first extent descriptor for a segment.
We think of the extent lists of the segment catenated in the order
FSEG_FULL -> FSEG_NOT_FULL -> FSEG_FREE.
@param[in] inode segment inode
@param[in] space tablespace
@param[in,out] mtr mini-transaction
@param[out] err error code
@return the first extent descriptor, or NULL if none */
MY_ATTRIBUTE((nonnull, warn_unused_result))
static
xdes_t*
fseg_get_first_extent(
fseg_inode_t* inode,
const fil_space_t* space,
mtr_t* mtr,
dberr_t* err);
ATTRIBUTE_COLD MY_ATTRIBUTE((nonnull, warn_unused_result))
/** Put new extents to the free list if there are free extents above the free
limit. If an extent happens to contain an extent descriptor page, the extent
is put to the FSP_FREE_FRAG list with the page marked as used.
@param[in] init_space true if this is a single-table tablespace
and we are only initializing the first extent and the first bitmap pages;
then we will not allocate more extents
@param[in,out] space tablespace
@param[in,out] header tablespace header
@param[in,out] mtr mini-transaction */
static
dberr_t
fsp_fill_free_list(
bool init_space,
fil_space_t* space,
buf_block_t* header,
mtr_t* mtr);
/** Allocates a single free page from a segment.
This function implements the intelligent allocation strategy which tries to
minimize file space fragmentation.
@param[in,out] space tablespace
@param[in,out] seg_inode segment inode
@param[in,out] iblock segment inode page
@param[in] hint hint of which page would be desirable
@param[in] direction if the new page is needed because of
an index page split, and records are inserted there in order, into which
direction they go alphabetically: FSP_DOWN, FSP_UP, FSP_NO_DIR
@param[in,out] mtr mini-transaction
@param[in,out] init_mtr mtr or another mini-transaction in
which the page should be initialized.
@param[out] err error code
@return the allocated page
@retval nullptr if no page could be allocated */
static
buf_block_t*
fseg_alloc_free_page_low(
fil_space_t* space,
fseg_inode_t* seg_inode,
buf_block_t* iblock,
uint32_t hint,
byte direction,
#ifdef UNIV_DEBUG
bool has_done_reservation,
/*!< whether the space has already been reserved */
#endif /* UNIV_DEBUG */
mtr_t* mtr,
mtr_t* init_mtr,
dberr_t* err)
MY_ATTRIBUTE((nonnull, warn_unused_result));
MY_ATTRIBUTE((nonnull, warn_unused_result))
/** Get the tablespace header block, SX-latched
@param[in] space tablespace
@param[in,out] mtr mini-transaction
@param[out] err error code
@return pointer to the space header, page x-locked
@retval nullptr if the page cannot be retrieved or is corrupted */
static buf_block_t *fsp_get_header(const fil_space_t *space, mtr_t *mtr,
dberr_t *err)
{
const page_id_t id{space->id, 0};
buf_block_t *block= mtr->get_already_latched(id, MTR_MEMO_PAGE_SX_FIX);
if (block)
*err= DB_SUCCESS;
else
{
block= buf_page_get_gen(id, space->zip_size(), RW_SX_LATCH,
nullptr, BUF_GET_POSSIBLY_FREED,
mtr, err);
if (block &&
space->id != mach_read_from_4(FSP_HEADER_OFFSET + FSP_SPACE_ID +
block->page.frame))
{
*err= DB_CORRUPTION;
block= nullptr;
}
}
return block;
}
/** Set the XDES_FREE_BIT of a page.
@tparam free desired value of XDES_FREE_BIT
@param[in] block extent descriptor block
@param[in,out] descr extent descriptor
@param[in] offset page offset within the extent
@param[in,out] mtr mini-transaction */
template<bool free>
inline void xdes_set_free(const buf_block_t &block, xdes_t *descr,
ulint offset, mtr_t *mtr)
{
ut_ad(mtr->memo_contains_flagged(&block, MTR_MEMO_PAGE_SX_FIX |
MTR_MEMO_PAGE_X_FIX));
ut_ad(offset < FSP_EXTENT_SIZE);
ut_ad(page_align(descr) == block.page.frame);
compile_time_assert(XDES_BITS_PER_PAGE == 2);
compile_time_assert(XDES_FREE_BIT == 0);
compile_time_assert(XDES_CLEAN_BIT == 1);
ulint index= XDES_BITS_PER_PAGE * offset;
byte *b= &descr[XDES_BITMAP + (index >> 3)];
/* xdes_init() should have set all XDES_CLEAN_BIT. */
ut_ad(!(~*b & 0xaa));
/* Clear or set XDES_FREE_BIT. */
byte val= free
? static_cast<byte>(*b | 1 << (index & 7))
: static_cast<byte>(*b & ~(1 << (index & 7)));
mtr->write<1>(block, b, val);
}
/**
Find a free page.
@param descr extent descriptor
@param hint page offset to start searching from (towards larger pages)
@return free page offset
@retval FIL_NULL if no page is free */
inline uint32_t xdes_find_free(const xdes_t *descr, uint32_t hint= 0)
{
const uint32_t extent_size= FSP_EXTENT_SIZE;
ut_ad(hint < extent_size);
for (uint32_t i= hint; i < extent_size; i++)
if (xdes_is_free(descr, i))
return i;
for (uint32_t i= 0; i < hint; i++)
if (xdes_is_free(descr, i))
return i;
return FIL_NULL;
}
/**
Determine the number of used pages in a descriptor.
@param descr file descriptor
@return number of pages used */
inline uint32_t xdes_get_n_used(const xdes_t *descr)
{
uint32_t count= 0;
for (uint32_t i= FSP_EXTENT_SIZE; i--; )
if (!xdes_is_free(descr, i))
count++;
return count;
}
/**
Determine whether a file extent is full.
@param descr file descriptor
@return whether all pages have been allocated */
inline bool xdes_is_full(const xdes_t *descr)
{
return FSP_EXTENT_SIZE == xdes_get_n_used(descr);
}
/** Set the state of an extent descriptor.
@param[in] block extent descriptor block
@param[in,out] descr extent descriptor
@param[in] state the state
@param[in,out] mtr mini-transaction */
inline void xdes_set_state(const buf_block_t &block, xdes_t *descr,
byte state, mtr_t *mtr)
{
ut_ad(descr && mtr);
ut_ad(state >= XDES_FREE);
ut_ad(state <= XDES_FSEG);
ut_ad(mtr->memo_contains_flagged(&block, MTR_MEMO_PAGE_SX_FIX |
MTR_MEMO_PAGE_X_FIX));
ut_ad(page_align(descr) == block.page.frame);
ut_ad(mach_read_from_4(descr + XDES_STATE) <= XDES_FSEG);
mtr->write<1>(block, XDES_STATE + 3 + descr, state);
}
/**********************************************************************//**
Gets the state of an xdes.
@return state */
UNIV_INLINE
ulint
xdes_get_state(
/*===========*/
const xdes_t* descr) /*!< in: descriptor */
{
ulint state;
ut_ad(descr);
state = mach_read_from_4(descr + XDES_STATE);
ut_ad(state - 1 < XDES_FSEG);
return(state);
}
/**********************************************************************//**
Inits an extent descriptor to the free and clean state. */
inline void xdes_init(const buf_block_t &block, xdes_t *descr, mtr_t *mtr)
{
ut_ad(mtr->memo_contains_flagged(&block, MTR_MEMO_PAGE_SX_FIX |
MTR_MEMO_PAGE_X_FIX));
mtr->memset(&block, uint16_t(descr - block.page.frame) + XDES_BITMAP,
XDES_SIZE - XDES_BITMAP, 0xff);
xdes_set_state(block, descr, XDES_FREE, mtr);
}
/** Mark a page used in an extent descriptor.
@param[in] space tablespace
@param[in,out] seg_inode segment inode
@param[in,out] iblock segment inode page
@param[in] page page number
@param[in,out] descr extent descriptor
@param[in,out] xdes extent descriptor page
@param[in,out] mtr mini-transaction
@return error code */
static MY_ATTRIBUTE((nonnull, warn_unused_result))
dberr_t
fseg_mark_page_used(const fil_space_t *space,
fseg_inode_t *seg_inode, buf_block_t *iblock,
uint32_t page, xdes_t *descr, buf_block_t *xdes, mtr_t *mtr)
{
ut_ad(fil_page_get_type(iblock->page.frame) == FIL_PAGE_INODE);
ut_ad(!((page_offset(seg_inode) - FSEG_ARR_OFFSET) % FSEG_INODE_SIZE));
ut_ad(!memcmp(FSEG_MAGIC_N_BYTES, FSEG_MAGIC_N + seg_inode, 4));
ut_ad(!memcmp(seg_inode + FSEG_ID, descr + XDES_ID, 4));
const uint16_t xoffset= uint16_t(descr - xdes->page.frame + XDES_FLST_NODE);
const uint16_t ioffset= uint16_t(seg_inode - iblock->page.frame);
const uint32_t limit= space->free_limit;
if (!xdes_get_n_used(descr))
{
/* We move the extent from the free list to the NOT_FULL list */
if (dberr_t err= flst_remove(iblock, uint16_t(FSEG_FREE + ioffset),
xdes, xoffset, limit, mtr))
return err;
if (dberr_t err= flst_add_last(iblock, uint16_t(FSEG_NOT_FULL + ioffset),
xdes, xoffset, limit, mtr))
return err;
}
if (UNIV_UNLIKELY(!xdes_is_free(descr, page % FSP_EXTENT_SIZE)))
return DB_CORRUPTION;
/* We mark the page as used */
xdes_set_free<false>(*xdes, descr, page % FSP_EXTENT_SIZE, mtr);
byte* p_not_full= seg_inode + FSEG_NOT_FULL_N_USED;
const uint32_t not_full_n_used= mach_read_from_4(p_not_full) + 1;
mtr->write<4>(*iblock, p_not_full, not_full_n_used);
if (xdes_is_full(descr))
{
/* We move the extent from the NOT_FULL list to the FULL list */
if (dberr_t err= flst_remove(iblock, uint16_t(FSEG_NOT_FULL + ioffset),
xdes, xoffset, limit, mtr))
return err;
if (dberr_t err= flst_add_last(iblock, uint16_t(FSEG_FULL + ioffset),
xdes, xoffset, limit, mtr))
return err;
mtr->write<4>(*iblock, seg_inode + FSEG_NOT_FULL_N_USED,
not_full_n_used - FSP_EXTENT_SIZE);
}
return DB_SUCCESS;
}
/** Get pointer to a the extent descriptor of a page.
@param[in,out] sp_header tablespace header page, x-latched
@param[in] space tablespace
@param[in] offset page offset
@param[in,out] mtr mini-transaction
@param[out] err error code
@param[out] desc_block descriptor block
@param[in] init_space whether the tablespace is being initialized
@return pointer to the extent descriptor, NULL if the page does not
exist in the space or if the offset exceeds free limit */
UNIV_INLINE MY_ATTRIBUTE((warn_unused_result))
xdes_t*
xdes_get_descriptor_with_space_hdr(
buf_block_t* header,
const fil_space_t* space,
uint32_t offset,
mtr_t* mtr,
dberr_t* err = nullptr,
buf_block_t** desc_block = nullptr,
bool init_space = false)
{
ut_ad(space->is_owner());
ut_ad(mtr->memo_contains_flagged(header, MTR_MEMO_PAGE_SX_FIX
| MTR_MEMO_PAGE_X_FIX));
/* Read free limit and space size */
uint32_t limit = mach_read_from_4(FSP_HEADER_OFFSET + FSP_FREE_LIMIT
+ header->page.frame);
uint32_t size = mach_read_from_4(FSP_HEADER_OFFSET + FSP_SIZE
+ header->page.frame);
ut_ad(limit == space->free_limit
|| (space->free_limit == 0
&& (init_space
|| space->purpose == FIL_TYPE_TEMPORARY
|| (srv_startup_is_before_trx_rollback_phase
&& (space->id == TRX_SYS_SPACE
|| srv_is_undo_tablespace(space->id))))));
ut_ad(size == space->size_in_header);
if (offset >= size || offset >= limit) {
return nullptr;
}
const unsigned zip_size = space->zip_size();
uint32_t descr_page_no = xdes_calc_descriptor_page(zip_size, offset);
buf_block_t* block = header;
if (descr_page_no) {
block = buf_page_get_gen(page_id_t(space->id, descr_page_no),
zip_size, RW_SX_LATCH, nullptr,
BUF_GET_POSSIBLY_FREED, mtr, err);
}
if (desc_block) {
*desc_block = block;
}
return block
? XDES_ARR_OFFSET + XDES_SIZE
* xdes_calc_descriptor_index(zip_size, offset)
+ block->page.frame
: nullptr;
}
MY_ATTRIBUTE((nonnull(1,3), warn_unused_result))
/** Get the extent descriptor of a page.
The page where the extent descriptor resides is x-locked. If the page
offset is equal to the free limit of the space, we will add new
extents from above the free limit to the space free list, if not free
limit == space size. This adding is necessary to make the descriptor
defined, as they are uninitialized above the free limit.
@param[in] space tablespace
@param[in] offset page offset; if equal to the free limit, we
try to add new extents to the space free list
@param[in,out] mtr mini-transaction
@param[out] err error code
@param[out] xdes extent descriptor page
@return the extent descriptor */
static xdes_t *xdes_get_descriptor(const fil_space_t *space, uint32_t offset,
mtr_t *mtr, dberr_t *err= nullptr,
buf_block_t **xdes= nullptr)
{
if (buf_block_t *block=
buf_page_get_gen(page_id_t(space->id, 0), space->zip_size(), RW_SX_LATCH,
nullptr, BUF_GET_POSSIBLY_FREED, mtr, err))
return xdes_get_descriptor_with_space_hdr(block, space, offset, mtr,
err, xdes);
return nullptr;
}
MY_ATTRIBUTE((nonnull(3), warn_unused_result))
/** Get a pointer to the extent descriptor. The page where the
extent descriptor resides is x-locked.
@param space tablespace
@param lst_node file address of the list node contained in the descriptor
@param mtr mini-transaction
@param err error code
@param block extent descriptor block
@return pointer to the extent descriptor */
static inline
xdes_t *xdes_lst_get_descriptor(const fil_space_t &space, fil_addr_t lst_node,
mtr_t *mtr, buf_block_t **block= nullptr,
dberr_t *err= nullptr)
{
ut_ad(mtr->memo_contains(space));
ut_ad(lst_node.boffset < space.physical_size());
buf_block_t *b;
if (!block)
block= &b;
*block= buf_page_get_gen(page_id_t{space.id, lst_node.page},
space.zip_size(), RW_SX_LATCH,
nullptr, BUF_GET_POSSIBLY_FREED, mtr, err);
if (*block)
return (*block)->page.frame + lst_node.boffset - XDES_FLST_NODE;
space.set_corrupted();
return nullptr;
}
/********************************************************************//**
Returns page offset of the first page in extent described by a descriptor.
@return offset of the first page in extent */
static uint32_t xdes_get_offset(const xdes_t *descr)
{
ut_ad(descr);
return page_get_page_no(page_align(descr)) +
uint32_t(((page_offset(descr) - XDES_ARR_OFFSET) / XDES_SIZE) *
FSP_EXTENT_SIZE);
}
/** Initialize a file page whose prior contents should be ignored.
@param[in,out] block buffer pool block */
void fsp_apply_init_file_page(buf_block_t *block)
{
memset_aligned<UNIV_PAGE_SIZE_MIN>(block->page.frame, 0, srv_page_size);
const page_id_t id(block->page.id());
mach_write_to_4(block->page.frame + FIL_PAGE_OFFSET, id.page_no());
memset_aligned<8>(block->page.frame + FIL_PAGE_PREV, 0xff, 8);
mach_write_to_4(block->page.frame + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID,
id.space());
if (page_zip_des_t* page_zip= buf_block_get_page_zip(block))
{
memset_aligned<UNIV_ZIP_SIZE_MIN>(page_zip->data, 0,
page_zip_get_size(page_zip));
static_assert(FIL_PAGE_OFFSET == 4, "compatibility");
memcpy_aligned<4>(page_zip->data + FIL_PAGE_OFFSET,
block->page.frame + FIL_PAGE_OFFSET, 4);
memset_aligned<8>(page_zip->data + FIL_PAGE_PREV, 0xff, 8);
static_assert(FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID % 4 == 2,
"not perfect alignment");
memcpy_aligned<2>(page_zip->data + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID,
block->page.frame + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID, 4);
}
}
#ifdef UNIV_DEBUG
/** Assert that the mini-transaction is compatible with
updating an allocation bitmap page.
@param[in] mtr mini-transaction */
void fil_space_t::modify_check(const mtr_t& mtr) const
{
switch (mtr.get_log_mode()) {
case MTR_LOG_NONE:
/* These modes are only allowed within a non-bitmap page
when there is a higher-level redo log record written. */
ut_ad(purpose == FIL_TYPE_TABLESPACE || purpose == FIL_TYPE_TEMPORARY);
break;
case MTR_LOG_NO_REDO:
ut_ad(purpose == FIL_TYPE_TEMPORARY || purpose == FIL_TYPE_IMPORT);
break;
default:
/* We may only write redo log for a persistent tablespace. */
ut_ad(purpose == FIL_TYPE_TABLESPACE);
ut_ad(mtr.is_named_space(id));
}
}
#endif
/** Initialize a tablespace header.
@param[in,out] space tablespace
@param[in] size current size in blocks
@param[in,out] mtr mini-transaction
@return error code */
dberr_t fsp_header_init(fil_space_t *space, uint32_t size, mtr_t *mtr)
{
const page_id_t page_id(space->id, 0);
const ulint zip_size = space->zip_size();
buf_block_t *free_block = buf_LRU_get_free_block(false);
mtr->x_lock_space(space);
buf_block_t* block = buf_page_create(space, 0, zip_size, mtr,
free_block);
if (UNIV_UNLIKELY(block != free_block)) {
buf_pool.free_block(free_block);
}
space->size_in_header = size;
space->free_len = 0;
space->free_limit = 0;
/* The prior contents of the file page should be ignored */
fsp_init_file_page(space, block, mtr);
mtr->write<2>(*block, block->page.frame + FIL_PAGE_TYPE,
FIL_PAGE_TYPE_FSP_HDR);
mtr->write<4,mtr_t::MAYBE_NOP>(*block, FSP_HEADER_OFFSET + FSP_SPACE_ID
+ block->page.frame, space->id);
ut_ad(0 == mach_read_from_4(FSP_HEADER_OFFSET + FSP_NOT_USED
+ block->page.frame));
/* recv_sys_t::parse() expects to find a WRITE record that
covers all 4 bytes. Therefore, we must specify mtr_t::FORCED
in order to avoid optimizing away any unchanged most
significant bytes of FSP_SIZE. */
mtr->write<4,mtr_t::FORCED>(*block, FSP_HEADER_OFFSET + FSP_SIZE
+ block->page.frame, size);
ut_ad(0 == mach_read_from_4(FSP_HEADER_OFFSET + FSP_FREE_LIMIT
+ block->page.frame));
if (auto f = space->flags & ~FSP_FLAGS_MEM_MASK) {
mtr->write<4,mtr_t::FORCED>(*block,
FSP_HEADER_OFFSET + FSP_SPACE_FLAGS
+ block->page.frame, f);
}
ut_ad(0 == mach_read_from_4(FSP_HEADER_OFFSET + FSP_FRAG_N_USED
+ block->page.frame));
flst_init(block, FSP_HEADER_OFFSET + FSP_FREE, mtr);
flst_init(block, FSP_HEADER_OFFSET + FSP_FREE_FRAG, mtr);
flst_init(block, FSP_HEADER_OFFSET + FSP_FULL_FRAG, mtr);
flst_init(block, FSP_HEADER_OFFSET + FSP_SEG_INODES_FULL, mtr);
flst_init(block, FSP_HEADER_OFFSET + FSP_SEG_INODES_FREE, mtr);
mtr->write<8>(*block, FSP_HEADER_OFFSET + FSP_SEG_ID
+ block->page.frame,
1U);
if (dberr_t err = fsp_fill_free_list(!is_system_tablespace(space->id),
space, block, mtr)) {
return err;
}
/* Write encryption metadata to page 0 if tablespace is
encrypted or encryption is disabled by table option. */
if (space->crypt_data &&
(space->crypt_data->should_encrypt() ||
space->crypt_data->not_encrypted())) {
space->crypt_data->write_page0(block, mtr);
}
return DB_SUCCESS;
}
/** Try to extend a single-table tablespace so that a page would fit in the
data file.
@param[in,out] space tablespace
@param[in] page_no page number
@param[in,out] header tablespace header
@param[in,out] mtr mini-transaction
@return true if success */
static ATTRIBUTE_COLD __attribute__((warn_unused_result))
bool
fsp_try_extend_data_file_with_pages(
fil_space_t* space,
uint32_t page_no,
buf_block_t* header,
mtr_t* mtr)
{
bool success;
ulint size;
ut_ad(!is_system_tablespace(space->id));
ut_d(space->modify_check(*mtr));
size = mach_read_from_4(FSP_HEADER_OFFSET + FSP_SIZE
+ header->page.frame);
ut_ad(size == space->size_in_header);
ut_a(page_no >= size);
success = fil_space_extend(space, page_no + 1);
/* The size may be less than we wanted if we ran out of disk space. */
/* recv_sys_t::parse() expects to find a WRITE record that
covers all 4 bytes. Therefore, we must specify mtr_t::FORCED
in order to avoid optimizing away any unchanged most
significant bytes of FSP_SIZE. */
mtr->write<4,mtr_t::FORCED>(*header, FSP_HEADER_OFFSET + FSP_SIZE
+ header->page.frame, space->size);
space->size_in_header = space->size;
return(success);
}
/** Calculate the number of physical pages in an extent for this file.
@param[in] physical_size page_size of the datafile
@return number of pages in an extent for this file */
inline uint32_t fsp_get_extent_size_in_pages(ulint physical_size)
{
return uint32_t((FSP_EXTENT_SIZE << srv_page_size_shift) / physical_size);
}
/** Calculate the number of pages to extend a datafile.
We extend single-table tablespaces first one extent at a time,
but 4 at a time for bigger tablespaces. It is not enough to extend always
by one extent, because we need to add at least one extent to FSP_FREE.
A single extent descriptor page will track many extents. And the extent
that uses its extent descriptor page is put onto the FSP_FREE_FRAG list.
Extents that do not use their extent descriptor page are added to FSP_FREE.
The physical page size is used to determine how many extents are tracked
on one extent descriptor page. See xdes_calc_descriptor_page().
@param[in] physical_size page size in data file
@param[in] size current number of pages in the datafile
@return number of pages to extend the file. */
static uint32_t fsp_get_pages_to_extend_ibd(unsigned physical_size,
uint32_t size)
{
uint32_t extent_size = fsp_get_extent_size_in_pages(physical_size);
/* The threshold is set at 32MiB except when the physical page
size is small enough that it must be done sooner. */
uint32_t threshold = std::min(32 * extent_size, physical_size);
if (size >= threshold) {
/* Below in fsp_fill_free_list() we assume
that we add at most FSP_FREE_ADD extents at
a time */
extent_size *= FSP_FREE_ADD;
}
return extent_size;
}
/** Try to extend the last data file of a tablespace if it is auto-extending.
@param[in,out] space tablespace
@param[in,out] header tablespace header
@param[in,out] mtr mini-transaction
@return number of pages added
@retval 0 if the tablespace was not extended */
ATTRIBUTE_COLD __attribute__((nonnull))
static
ulint
fsp_try_extend_data_file(fil_space_t *space, buf_block_t *header, mtr_t *mtr)
{
const char* OUT_OF_SPACE_MSG =
"ran out of space. Please add another file or use"
" 'autoextend' for the last file in setting";
ut_d(space->modify_check(*mtr));
if (space->id == TRX_SYS_SPACE
&& !srv_sys_space.can_auto_extend_last_file()) {
/* We print the error message only once to avoid
spamming the error log. Note that we don't need
to reset the flag to false as dealing with this
error requires server restart. */
if (!srv_sys_space.get_tablespace_full_status()) {
sql_print_error("InnoDB: The InnoDB system tablespace "
"%s" " innodb_data_file_path.",
OUT_OF_SPACE_MSG);
srv_sys_space.set_tablespace_full_status(true);
}
return(0);
} else if (space->id == SRV_TMP_SPACE_ID
&& !srv_tmp_space.can_auto_extend_last_file()) {
/* We print the error message only once to avoid
spamming the error log. Note that we don't need
to reset the flag to false as dealing with this
error requires server restart. */
if (!srv_tmp_space.get_tablespace_full_status()) {
sql_print_error("InnoDB: The InnoDB temporary"
" tablespace %s"
" innodb_temp_data_file_path.",
OUT_OF_SPACE_MSG);
srv_tmp_space.set_tablespace_full_status(true);
}
return(0);
}
uint32_t size = mach_read_from_4(FSP_HEADER_OFFSET + FSP_SIZE
+ header->page.frame);
ut_ad(size == space->size_in_header);
uint32_t size_increase;
const unsigned ps = space->physical_size();
switch (space->id) {
case TRX_SYS_SPACE:
size_increase = srv_sys_space.get_increment();
break;
case SRV_TMP_SPACE_ID:
size_increase = srv_tmp_space.get_increment();
break;
default:
uint32_t extent_pages = fsp_get_extent_size_in_pages(ps);
if (size < extent_pages) {
/* Let us first extend the file to extent_size */
if (!fsp_try_extend_data_file_with_pages(
space, extent_pages - 1, header, mtr)) {
return(0);
}
size = extent_pages;
}
size_increase = fsp_get_pages_to_extend_ibd(ps, size);
}
if (size_increase == 0) {
return(0);
}
if (!fil_space_extend(space, size + size_increase)) {
return(0);
}
/* For the system tablespace, we ignore any fragments of a
full megabyte when storing the size to the space header */
space->size_in_header = space->id
? space->size
: ut_2pow_round(space->size, (1024 * 1024) / ps);
/* recv_sys_t::parse() expects to find a WRITE record that
covers all 4 bytes. Therefore, we must specify mtr_t::FORCED
in order to avoid optimizing away any unchanged most
significant bytes of FSP_SIZE. */
mtr->write<4,mtr_t::FORCED>(*header, FSP_HEADER_OFFSET + FSP_SIZE
+ header->page.frame,
space->size_in_header);
return(size_increase);
}
/** Reset the page type.
Data files created before MySQL 5.1.48 may contain garbage in FIL_PAGE_TYPE.
In MySQL 3.23.53, only undo log pages and index pages were tagged.
Any other pages were written with uninitialized bytes in FIL_PAGE_TYPE.
@param[in] block block with invalid FIL_PAGE_TYPE
@param[in] type expected page type
@param[in,out] mtr mini-transaction */
ATTRIBUTE_COLD
void fil_block_reset_type(const buf_block_t& block, ulint type, mtr_t* mtr)
{
ib::info() << "Resetting invalid page " << block.page.id() << " type "
<< fil_page_get_type(block.page.frame) << " to " << type << ".";
mtr->write<2>(block, block.page.frame + FIL_PAGE_TYPE, type);
}
/** Put new extents to the free list if there are free extents above the free
limit. If an extent happens to contain an extent descriptor page, the extent
is put to the FSP_FREE_FRAG list with the page marked as used.
@param[in] init_space true if this is a single-table tablespace
and we are only initializing the first extent and the first bitmap pages;
then we will not allocate more extents
@param[in,out] space tablespace
@param[in,out] header tablespace header
@param[in,out] mtr mini-transaction
@return error code */
static
dberr_t
fsp_fill_free_list(
bool init_space,
fil_space_t* space,
buf_block_t* header,
mtr_t* mtr)
{
ut_d(space->modify_check(*mtr));
/* Check if we can fill free list from above the free list limit */
uint32_t size=
mach_read_from_4(FSP_HEADER_OFFSET + FSP_SIZE + header->page.frame);
uint32_t limit=
mach_read_from_4(FSP_HEADER_OFFSET + FSP_FREE_LIMIT + header->page.frame);
ut_ad(size == space->size_in_header);
ut_ad(limit == space->free_limit);
const auto zip_size= space->zip_size();
if (size < limit + FSP_EXTENT_SIZE * FSP_FREE_ADD)
{
bool skip_resize= init_space;
switch (space->id) {
case TRX_SYS_SPACE:
skip_resize= !srv_sys_space.can_auto_extend_last_file();
break;
case SRV_TMP_SPACE_ID:
skip_resize= !srv_tmp_space.can_auto_extend_last_file();
break;
}
if (!skip_resize)
{
fsp_try_extend_data_file(space, header, mtr);
size= space->size_in_header;
}
}
uint32_t count= 0;
for (uint32_t i= limit, extent_size= FSP_EXTENT_SIZE,
physical_size= space->physical_size();
(init_space && i < 1) ||
(i + extent_size <= size && count < FSP_FREE_ADD);
i += extent_size)
{
const bool init_xdes= !ut_2pow_remainder(i, physical_size);
space->free_limit= i + extent_size;
mtr->write<4>(*header, FSP_HEADER_OFFSET + FSP_FREE_LIMIT +
header->page.frame, i + extent_size);
if (init_xdes)
{
/* We are going to initialize a new descriptor page
and a new ibuf bitmap page: the prior contents of the
pages should be ignored. */
if (i)
{
buf_block_t *f= buf_LRU_get_free_block(false);
buf_block_t *block= buf_page_create(space, i, zip_size, mtr, f);
if (UNIV_UNLIKELY(block != f))
buf_pool.free_block(f);
fsp_init_file_page(space, block, mtr);
mtr->write<2>(*block, FIL_PAGE_TYPE + block->page.frame,
FIL_PAGE_TYPE_XDES);
}
if (space->purpose != FIL_TYPE_TEMPORARY)
{
buf_block_t *f= buf_LRU_get_free_block(false);
buf_block_t *block=
buf_page_create(space, i + FSP_IBUF_BITMAP_OFFSET, zip_size, mtr, f);
if (UNIV_UNLIKELY(block != f))
buf_pool.free_block(f);
fsp_init_file_page(space, block, mtr);
mtr->write<2>(*block, FIL_PAGE_TYPE + block->page.frame,
FIL_PAGE_IBUF_BITMAP);
}
}
buf_block_t *xdes= nullptr;
xdes_t *descr;
{
dberr_t err= DB_SUCCESS;
descr= xdes_get_descriptor_with_space_hdr(header, space, i, mtr,
&err, &xdes, init_space);
if (!descr)
return err;
}
if (xdes != header && !space->full_crc32())
fil_block_check_type(*xdes, FIL_PAGE_TYPE_XDES, mtr);
xdes_init(*xdes, descr, mtr);
const uint16_t xoffset=
static_cast<uint16_t>(descr - xdes->page.frame + XDES_FLST_NODE);
if (UNIV_UNLIKELY(init_xdes))
{
/* The first page in the extent is a descriptor page and the
second is an ibuf bitmap page: mark them used */
xdes_set_free<false>(*xdes, descr, 0, mtr);
xdes_set_free<false>(*xdes, descr, FSP_IBUF_BITMAP_OFFSET, mtr);
xdes_set_state(*xdes, descr, XDES_FREE_FRAG, mtr);
if (dberr_t err= flst_add_last(header, FSP_HEADER_OFFSET + FSP_FREE_FRAG,
xdes, xoffset, space->free_limit, mtr))
return err;
byte *n_used= FSP_HEADER_OFFSET + FSP_FRAG_N_USED + header->page.frame;
mtr->write<4>(*header, n_used, 2U + mach_read_from_4(n_used));
}
else
{
if (dberr_t err=
flst_add_last(header, FSP_HEADER_OFFSET + FSP_FREE,
xdes, xoffset, space->free_limit, mtr))
return err;
count++;
}
}
space->free_len+= count;
return DB_SUCCESS;
}
MY_ATTRIBUTE((nonnull, warn_unused_result))
/** Allocates a new free extent.
@param[in,out] space tablespace
@param[in] hint hint of which extent would be desirable: any
page offset in the extent goes; the hint must not be > FSP_FREE_LIMIT
@param[out] xdes extent descriptor page
@param[in,out] mtr mini-transaction
@return extent descriptor
@retval nullptr if cannot be allocated */
static xdes_t *fsp_alloc_free_extent(fil_space_t *space, uint32_t hint,
buf_block_t **xdes, mtr_t *mtr,
dberr_t *err)
{
fil_addr_t first;
xdes_t* descr;
buf_block_t* desc_block;
buf_block_t* header = fsp_get_header(space, mtr, err);
if (!header) {
corrupted:
space->set_corrupted();
return nullptr;
}
descr = xdes_get_descriptor_with_space_hdr(
header, space, hint, mtr, err, &desc_block);
if (!descr) {
goto corrupted;
}
if (desc_block != header && !space->full_crc32()) {
fil_block_check_type(*desc_block, FIL_PAGE_TYPE_XDES, mtr);
}
if (xdes_get_state(descr) == XDES_FREE) {
/* Ok, we can take this extent */
} else {
/* Take the first extent in the free list */
first = flst_get_first(FSP_HEADER_OFFSET + FSP_FREE
+ header->page.frame);
if (first.page >= space->free_limit) {
if (first.page != FIL_NULL) {
goto flst_corrupted;
}
*err = fsp_fill_free_list(false, space, header, mtr);
if (UNIV_UNLIKELY(*err != DB_SUCCESS)) {
goto corrupted;
}
first = flst_get_first(FSP_HEADER_OFFSET + FSP_FREE
+ header->page.frame);
if (first.page == FIL_NULL) {
return nullptr; /* No free extents left */
}
if (first.page >= space->free_limit) {
goto flst_corrupted;
}
}
if (first.boffset < FSP_HEADER_OFFSET + FSP_HEADER_SIZE
|| first.boffset >= space->physical_size()
- (XDES_SIZE + FIL_PAGE_DATA_END)) {
flst_corrupted:
*err = DB_CORRUPTION;
goto corrupted;
}
descr = xdes_lst_get_descriptor(*space, first, mtr,
&desc_block, err);
if (!descr) {
return descr;
}
}
*err = flst_remove(header, FSP_HEADER_OFFSET + FSP_FREE, desc_block,
static_cast<uint16_t>(descr - desc_block->page.frame
+ XDES_FLST_NODE),
space->free_limit, mtr);
if (UNIV_UNLIKELY(*err != DB_SUCCESS)) {
return nullptr;
}
space->free_len--;
*xdes = desc_block;
return(descr);
}
MY_ATTRIBUTE((nonnull, warn_unused_result))
/** Allocate a single free page.
@param[in,out] header tablespace header
@param[in,out] xdes extent descriptor page
@param[in,out] descr extent descriptor
@param[in] bit slot to allocate in the extent
@param[in] space tablespace
@param[in,out] mtr mini-transaction
@return error code */
static dberr_t
fsp_alloc_from_free_frag(buf_block_t *header, buf_block_t *xdes, xdes_t *descr,
uint32_t bit, fil_space_t *space, mtr_t *mtr)
{
if (UNIV_UNLIKELY(xdes_get_state(descr) != XDES_FREE_FRAG ||
!xdes_is_free(descr, bit)))
return DB_CORRUPTION;
xdes_set_free<false>(*xdes, descr, bit, mtr);
/* Update the FRAG_N_USED field */
byte *n_used_p= FSP_HEADER_OFFSET + FSP_FRAG_N_USED + header->page.frame;
uint32_t n_used = mach_read_from_4(n_used_p) + 1;
if (xdes_is_full(descr))
{
const uint32_t limit= space->free_limit;
/* The fragment is full: move it to another list */
const uint16_t xoffset=
static_cast<uint16_t>(descr - xdes->page.frame + XDES_FLST_NODE);
if (dberr_t err= flst_remove(header, FSP_HEADER_OFFSET + FSP_FREE_FRAG,
xdes, xoffset, limit, mtr))
return err;
if (dberr_t err= flst_add_last(header, FSP_HEADER_OFFSET + FSP_FULL_FRAG,
xdes, xoffset, limit, mtr))
return err;
xdes_set_state(*xdes, descr, XDES_FULL_FRAG, mtr);
n_used-= FSP_EXTENT_SIZE;
}
mtr->write<4>(*header, n_used_p, n_used);
return DB_SUCCESS;
}
/** Gets a buffer block for an allocated page.
@param[in,out] space tablespace
@param[in] offset page number of the allocated page
@param[in,out] mtr mini-transaction
@return block, initialized */
static buf_block_t* fsp_page_create(fil_space_t *space, uint32_t offset,
mtr_t *mtr)
{
buf_block_t *free_block= buf_LRU_get_free_block(false),
*block= buf_page_create(space, offset, space->zip_size(), mtr, free_block);
if (UNIV_UNLIKELY(block != free_block))
buf_pool.free_block(free_block);
fsp_init_file_page(space, block, mtr);
return block;
}
/** Allocates a single free page from a space.
The page is marked as used.
@param[in,out] space tablespace
@param[in] hint hint of which page would be desirable
@param[in,out] mtr mini-transaction
@param[in,out] init_mtr mini-transaction in which the page should be
initialized (may be the same as mtr)
@param[out] err error code
@return allocated block
@retval nullptr if no page could be allocated */
static MY_ATTRIBUTE((warn_unused_result, nonnull))
buf_block_t *fsp_alloc_free_page(fil_space_t *space, uint32_t hint,
mtr_t *mtr, mtr_t *init_mtr, dberr_t *err)
{
ut_d(space->modify_check(*mtr));
buf_block_t *block= fsp_get_header(space, mtr, err);
if (!block)
return block;
buf_block_t *xdes;
/* Get the hinted descriptor */
xdes_t *descr= xdes_get_descriptor_with_space_hdr(block, space, hint, mtr,
err, &xdes);
if (descr && xdes_get_state(descr) == XDES_FREE_FRAG)
/* Ok, we can take this extent */;
else if (*err != DB_SUCCESS)
{
err_exit:
space->set_corrupted();
return nullptr;
}
else
{
/* Else take the first extent in free_frag list */
fil_addr_t first = flst_get_first(FSP_HEADER_OFFSET + FSP_FREE_FRAG +
block->page.frame);
if (first.page >= space->free_limit)
{
if (first.page != FIL_NULL)
goto flst_corrupted;
/* There are no partially full fragments: allocate a free extent
and add it to the FREE_FRAG list. NOTE that the allocation may
have as a side-effect that an extent containing a descriptor
page is added to the FREE_FRAG list. But we will allocate our
page from the the free extent anyway. */
descr= fsp_alloc_free_extent(space, hint, &xdes, mtr, err);
if (!descr)
return nullptr;
*err= flst_add_last(block, FSP_HEADER_OFFSET + FSP_FREE_FRAG, xdes,
static_cast<uint16_t>(descr - xdes->page.frame +
XDES_FLST_NODE),
space->free_limit, mtr);
if (UNIV_UNLIKELY(*err != DB_SUCCESS))
return nullptr;
xdes_set_state(*xdes, descr, XDES_FREE_FRAG, mtr);
}
else
{
if (first.boffset < FSP_HEADER_OFFSET + FSP_HEADER_SIZE ||
first.boffset >= space->physical_size() -
(XDES_SIZE + FIL_PAGE_DATA_END))
{
flst_corrupted:
*err= DB_CORRUPTION;
goto err_exit;
}
descr= xdes_lst_get_descriptor(*space, first, mtr, &xdes, err);
if (!descr)
return nullptr;
/* Reset the hint */
hint= 0;
}
}
/* Now we have in descr an extent with at least one free page. Look
for a free page in the extent. */
uint32_t free= xdes_find_free(descr, hint % FSP_EXTENT_SIZE);
if (free == FIL_NULL)
{
corrupted:
*err= DB_CORRUPTION;
goto err_exit;
}
uint32_t page_no= xdes_get_offset(descr) + free;
uint32_t space_size = mach_read_from_4(FSP_HEADER_OFFSET + FSP_SIZE +
block->page.frame);
ut_ad(space_size == space->size_in_header ||
(space->id == TRX_SYS_SPACE &&
srv_startup_is_before_trx_rollback_phase));
if (space_size <= page_no)
{
/* It must be that we are extending a single-table tablespace
whose size is still < 64 pages */
ut_ad(!is_system_tablespace(space->id));
if (page_no >= FSP_EXTENT_SIZE)
{
sql_print_error("InnoDB: Trying to extend %s"
" by single page(s) though the size is " UINT32PF "."
" Page no " UINT32PF ".",
space->chain.start->name, space_size, page_no);
goto corrupted;
}
if (!fsp_try_extend_data_file_with_pages(space, page_no, block, mtr))
{
*err= DB_OUT_OF_FILE_SPACE;
return nullptr;
}
}
*err= fsp_alloc_from_free_frag(block, xdes, descr, free, space, mtr);
if (UNIV_UNLIKELY(*err != DB_SUCCESS))
goto corrupted;
return fsp_page_create(space, page_no, init_mtr);
}
MY_ATTRIBUTE((nonnull, warn_unused_result))
/** Return an extent to the free list of a space.
@param[in,out] space tablespace
@param[in] offset page number in the extent
@param[in,out] mtr mini-transaction
@return error code */
static dberr_t fsp_free_extent(fil_space_t* space, uint32_t offset,
mtr_t* mtr)
{
ut_ad(space->is_owner());
dberr_t err;
buf_block_t *block= fsp_get_header(space, mtr, &err);
if (!block)
return err;
buf_block_t *xdes;
xdes_t *descr= xdes_get_descriptor_with_space_hdr(block, space, offset, mtr,
&err, &xdes);
if (!descr)
{
ut_ad(err || space->is_stopping());
return err;
}
if (UNIV_UNLIKELY(xdes_get_state(descr) == XDES_FREE))
{
space->set_corrupted();
return DB_CORRUPTION;
}
xdes_init(*xdes, descr, mtr);
space->free_len++;
return flst_add_last(block, FSP_HEADER_OFFSET + FSP_FREE,
xdes, static_cast<uint16_t>(descr - xdes->page.frame +
XDES_FLST_NODE),
space->free_limit, mtr);
}
MY_ATTRIBUTE((nonnull))
/** Frees a single page of a space.
The page is marked as free and clean.
@param[in,out] space tablespace
@param[in] offset page number
@param[in,out] mtr mini-transaction
@return error code */
static dberr_t fsp_free_page(fil_space_t *space, uint32_t offset, mtr_t *mtr)
{
xdes_t* descr;
ulint frag_n_used;
ut_ad(mtr);
ut_d(space->modify_check(*mtr));
/* fprintf(stderr, "Freeing page %lu in space %lu\n", page, space); */
dberr_t err;
buf_block_t* header = fsp_get_header(space, mtr, &err);
if (!header) {
ut_ad(space->is_stopping());
return err;
}
buf_block_t* xdes;
descr = xdes_get_descriptor_with_space_hdr(header, space, offset, mtr,
&err, &xdes);
if (!descr) {
ut_ad(err || space->is_stopping());
return err;
}
const auto state = xdes_get_state(descr);
switch (state) {
case XDES_FREE_FRAG:
case XDES_FULL_FRAG:
if (!xdes_is_free(descr, offset % FSP_EXTENT_SIZE)) {
break;
}
/* fall through */
default:
space->set_corrupted();
return DB_CORRUPTION;
}
frag_n_used = mach_read_from_4(FSP_HEADER_OFFSET + FSP_FRAG_N_USED
+ header->page.frame);
const uint16_t xoffset= static_cast<uint16_t>(descr - xdes->page.frame
+ XDES_FLST_NODE);
const uint32_t limit = space->free_limit;
if (state == XDES_FULL_FRAG) {
/* The fragment was full: move it to another list */
err = flst_remove(header, FSP_HEADER_OFFSET + FSP_FULL_FRAG,
xdes, xoffset, limit, mtr);
if (UNIV_UNLIKELY(err != DB_SUCCESS)) {
return err;
}
err = flst_add_last(header, FSP_HEADER_OFFSET + FSP_FREE_FRAG,
xdes, xoffset, limit, mtr);
if (UNIV_UNLIKELY(err != DB_SUCCESS)) {
return err;
}
xdes_set_state(*xdes, descr, XDES_FREE_FRAG, mtr);
mtr->write<4>(*header, FSP_HEADER_OFFSET + FSP_FRAG_N_USED
+ header->page.frame,
frag_n_used + FSP_EXTENT_SIZE - 1);
} else if (UNIV_UNLIKELY(!frag_n_used)) {
return DB_CORRUPTION;
} else {
mtr->write<4>(*header, FSP_HEADER_OFFSET + FSP_FRAG_N_USED
+ header->page.frame, frag_n_used - 1);
}
mtr->free(*space, static_cast<uint32_t>(offset));
xdes_set_free<true>(*xdes, descr, offset % FSP_EXTENT_SIZE, mtr);
ut_ad(err == DB_SUCCESS);
if (!xdes_get_n_used(descr)) {
/* The extent has become free: move it to another list */
err = flst_remove(header, FSP_HEADER_OFFSET + FSP_FREE_FRAG,
xdes, xoffset, limit, mtr);
if (err == DB_SUCCESS) {
err = fsp_free_extent(space, offset, mtr);
}
}
return err;
}
/** @return Number of segment inodes which fit on a single page */
inline ulint FSP_SEG_INODES_PER_PAGE(ulint physical_size)
{
return (physical_size - FSEG_ARR_OFFSET - 10) / FSEG_INODE_SIZE;
}
/** Returns the nth inode slot on an inode page.
@param[in] page segment inode page
@param[in] i inode index on page
@return segment inode */
#define fsp_seg_inode_page_get_nth_inode(page, i) \
FSEG_ARR_OFFSET + FSEG_INODE_SIZE * i + page
/** Looks for a used segment inode on a segment inode page.
@param page segment inode page
@param physical_size page size
@return segment inode index
@retval ULINT_UNDEFINED if not found */
static
ulint
fsp_seg_inode_page_find_used(const page_t *page, ulint physical_size)
{
for (ulint i= 0; i < FSP_SEG_INODES_PER_PAGE(physical_size); i++)
{
const byte *inode= fsp_seg_inode_page_get_nth_inode(page, i);
if (mach_read_from_8(FSEG_ID + inode))
{
ut_ad(!memcmp(FSEG_MAGIC_N_BYTES, FSEG_MAGIC_N + inode, 4));
return i;
}
}
return ULINT_UNDEFINED;
}
/** Looks for an unused segment inode on a segment inode page.
@param[in] page segment inode page
@param[in] i search forward starting from this index
@param[in] physical_size page size
@return segment inode index
@retval ULINT_UNDEFINED if not found */
static
ulint
fsp_seg_inode_page_find_free(const page_t *page, ulint i, ulint physical_size)
{
for (; i < FSP_SEG_INODES_PER_PAGE(physical_size); i++)
{
const byte *inode= fsp_seg_inode_page_get_nth_inode(page, i);
if (mach_read_from_8(FSEG_ID + inode))
ut_ad(!memcmp(FSEG_MAGIC_N_BYTES, FSEG_MAGIC_N + inode, 4));
else
/* This is unused */
return i;
}
return ULINT_UNDEFINED;
}
MY_ATTRIBUTE((nonnull, warn_unused_result))
/** Allocate a file segment inode page.
@param[in,out] space tablespace
@param[in,out] header tablespace header
@param[in,out] mtr mini-transaction
@return error code */
static dberr_t fsp_alloc_seg_inode_page(fil_space_t *space,
buf_block_t *header, mtr_t *mtr)
{
ut_ad(header->page.id().space() == space->id);
dberr_t err;
buf_block_t *block= fsp_alloc_free_page(space, 0, mtr, mtr, &err);
if (!block)
return err;
ut_ad(block->page.lock.not_recursive());
mtr->write<2>(*block, block->page.frame + FIL_PAGE_TYPE, FIL_PAGE_INODE);
#ifdef UNIV_DEBUG
const byte *inode= FSEG_ID + FSEG_ARR_OFFSET + block->page.frame;
for (ulint i= FSP_SEG_INODES_PER_PAGE(space->physical_size()); i--;
inode += FSEG_INODE_SIZE)
ut_ad(!mach_read_from_8(inode));
#endif
return flst_add_last(header, FSP_HEADER_OFFSET + FSP_SEG_INODES_FREE,
block, FSEG_INODE_PAGE_NODE, space->free_limit, mtr);
}
MY_ATTRIBUTE((nonnull, warn_unused_result))
/** Allocate a file segment inode.
@param[in,out] space tablespace
@param[in,out] header tablespace header
@param[out] iblock segment inode page
@param[in,out] mtr mini-transaction
@param[out] err error code
@return segment inode
@retval nullptr on failure */
static fseg_inode_t*
fsp_alloc_seg_inode(fil_space_t *space, buf_block_t *header,
buf_block_t **iblock, mtr_t *mtr, dberr_t *err)
{
/* Allocate a new segment inode page if needed. */
if (!flst_get_len(FSP_HEADER_OFFSET + FSP_SEG_INODES_FREE +
header->page.frame))
{
*err= fsp_alloc_seg_inode_page(space, header, mtr);
if (*err != DB_SUCCESS)
return nullptr;
}
const page_id_t page_id
{
space->id,
mach_read_from_4(FSP_HEADER_OFFSET + FSP_SEG_INODES_FREE + FLST_FIRST +
FIL_ADDR_PAGE + header->page.frame)
};
buf_block_t *block=
buf_page_get_gen(page_id, space->zip_size(), RW_SX_LATCH,
nullptr, BUF_GET_POSSIBLY_FREED, mtr, err);
if (!block)
return nullptr;
if (!space->full_crc32())
fil_block_check_type(*block, FIL_PAGE_INODE, mtr);
const ulint physical_size= space->physical_size();
ulint n= fsp_seg_inode_page_find_free(block->page.frame, 0, physical_size);
if (UNIV_UNLIKELY(n >= FSP_SEG_INODES_PER_PAGE(physical_size)))
{
*err= DB_CORRUPTION;
return nullptr;
}
fseg_inode_t *inode= fsp_seg_inode_page_get_nth_inode(block->page.frame, n);
if (ULINT_UNDEFINED == fsp_seg_inode_page_find_free(block->page.frame, n + 1,
physical_size))
{
/* There are no other unused headers left on the page: move it
to another list */
const uint32_t limit= space->free_limit;
*err= flst_remove(header, FSP_HEADER_OFFSET + FSP_SEG_INODES_FREE,
block, FSEG_INODE_PAGE_NODE, limit, mtr);
if (UNIV_UNLIKELY(*err != DB_SUCCESS))
return nullptr;
*err= flst_add_last(header, FSP_HEADER_OFFSET + FSP_SEG_INODES_FULL,
block, FSEG_INODE_PAGE_NODE, limit, mtr);
if (UNIV_UNLIKELY(*err != DB_SUCCESS))
return nullptr;
}
ut_ad(!mach_read_from_8(inode + FSEG_ID) ||
!memcmp(FSEG_MAGIC_N_BYTES, FSEG_MAGIC_N + inode, 4));
*iblock= block;
return inode;
}
MY_ATTRIBUTE((nonnull))
/** Frees a file segment inode.
@param[in,out] space tablespace
@param[in,out] inode segment inode
@param[in,out] iblock segment inode page
@param[in,out] mtr mini-transaction */
static void fsp_free_seg_inode(fil_space_t *space, fseg_inode_t *inode,
buf_block_t *iblock, mtr_t *mtr)
{
ut_d(space->modify_check(*mtr));
dberr_t err;
buf_block_t *header= fsp_get_header(space, mtr, &err);
if (!header)
return;
if (UNIV_UNLIKELY(memcmp(FSEG_MAGIC_N_BYTES, FSEG_MAGIC_N + inode, 4)))
{
space->set_corrupted();
return;
}
const ulint physical_size= space->physical_size();
const uint32_t limit= space->free_limit;
if (ULINT_UNDEFINED == fsp_seg_inode_page_find_free(iblock->page.frame, 0,
physical_size))
{
/* Move the page to another list */
if (flst_remove(header, FSP_HEADER_OFFSET + FSP_SEG_INODES_FULL,
iblock, FSEG_INODE_PAGE_NODE, limit, mtr) != DB_SUCCESS)
return;
if (flst_add_last(header, FSP_HEADER_OFFSET + FSP_SEG_INODES_FREE,
iblock, FSEG_INODE_PAGE_NODE, limit, mtr) != DB_SUCCESS)
return;
}
mtr->memset(iblock, page_offset(inode) + FSEG_ID, FSEG_INODE_SIZE, 0);
if (ULINT_UNDEFINED != fsp_seg_inode_page_find_used(iblock->page.frame,
physical_size))
return;
/* There are no other used headers left on the page: free it */
if (flst_remove(header, FSP_HEADER_OFFSET + FSP_SEG_INODES_FREE,
iblock, FSEG_INODE_PAGE_NODE, limit, mtr) == DB_SUCCESS)
fsp_free_page(space, iblock->page.id().page_no(), mtr);
}
MY_ATTRIBUTE((nonnull(1,4,5), warn_unused_result))
/** Returns the file segment inode, page x-latched.
@param[in] header segment header
@param[in] space space id
@param[in] zip_size ROW_FORMAT=COMPRESSED page size, or 0
@param[in,out] mtr mini-transaction
@param[out] block inode block
@param[out] err error code
@return segment inode, page x-latched
@retrval nullptr if the inode is free or corruption was noticed */
static
fseg_inode_t*
fseg_inode_try_get(
const fseg_header_t* header,
uint32_t space,
ulint zip_size,
mtr_t* mtr,
buf_block_t** block,
dberr_t* err = nullptr)
{
if (UNIV_UNLIKELY(space != mach_read_from_4(header + FSEG_HDR_SPACE)))
{
corrupted:
if (err)
*err= DB_CORRUPTION;
return nullptr;
}
*block=
buf_page_get_gen(page_id_t(space,
mach_read_from_4(header + FSEG_HDR_PAGE_NO)),
zip_size, RW_SX_LATCH, nullptr, BUF_GET_POSSIBLY_FREED,
mtr, err);
if (!*block)
return nullptr;
const uint16_t offset= mach_read_from_2(header + FSEG_HDR_OFFSET);
if (UNIV_UNLIKELY(offset >= (*block)->physical_size()))
goto corrupted;
fseg_inode_t *inode= (*block)->page.frame + offset;
if (UNIV_UNLIKELY(!mach_read_from_8(inode + FSEG_ID) ||
memcmp(FSEG_MAGIC_N_BYTES, FSEG_MAGIC_N + inode, 4)))
goto corrupted;
return inode;
}
/** Get the page number from the nth fragment page slot.
@param inode file segment findex
@param n slot index
@return page number
@retval FIL_NULL if not in use */
static uint32_t fseg_get_nth_frag_page_no(const fseg_inode_t *inode, ulint n)
{
ut_ad(inode);
ut_ad(n < FSEG_FRAG_ARR_N_SLOTS);
ut_ad(!memcmp(FSEG_MAGIC_N_BYTES, FSEG_MAGIC_N + inode, 4));
return(mach_read_from_4(inode + FSEG_FRAG_ARR
+ n * FSEG_FRAG_SLOT_SIZE));
}
/** Set the page number in the nth fragment page slot.
@param[in,out] inode segment inode
@param[in,out] iblock segment inode page
@param[in] n slot index
@param[in] page_no page number to set
@param[in,out] mtr mini-transaction */
inline void fseg_set_nth_frag_page_no(fseg_inode_t *inode, buf_block_t *iblock,
ulint n, ulint page_no, mtr_t *mtr)
{
ut_ad(n < FSEG_FRAG_ARR_N_SLOTS);
ut_ad(mtr->memo_contains_flagged(iblock, MTR_MEMO_PAGE_SX_FIX));
ut_ad(!memcmp(FSEG_MAGIC_N_BYTES, FSEG_MAGIC_N + inode, 4));
mtr->write<4>(*iblock, inode + FSEG_FRAG_ARR + n * FSEG_FRAG_SLOT_SIZE,
page_no);
}
/**********************************************************************//**
Finds a fragment page slot which is free.
@return slot index; ULINT_UNDEFINED if none found */
static
ulint
fseg_find_free_frag_page_slot(
/*==========================*/
fseg_inode_t* inode) /*!< in: segment inode */
{
ulint i;
ulint page_no;
for (i = 0; i < FSEG_FRAG_ARR_N_SLOTS; i++) {
page_no = fseg_get_nth_frag_page_no(inode, i);
if (page_no == FIL_NULL) {
return(i);
}
}
return(ULINT_UNDEFINED);
}
/**********************************************************************//**
Finds a fragment page slot which is used and last in the array.
@return slot index; ULINT_UNDEFINED if none found */
static
ulint
fseg_find_last_used_frag_page_slot(
/*===============================*/
fseg_inode_t* inode) /*!< in: segment inode */
{
ulint i;
ulint page_no;
for (i = 0; i < FSEG_FRAG_ARR_N_SLOTS; i++) {
page_no = fseg_get_nth_frag_page_no(
inode, FSEG_FRAG_ARR_N_SLOTS - i - 1);
if (page_no != FIL_NULL) {
return(FSEG_FRAG_ARR_N_SLOTS - i - 1);
}
}
return(ULINT_UNDEFINED);
}
/** Calculate reserved fragment page slots.
@param inode file segment index
@return number of fragment pages */
static ulint fseg_get_n_frag_pages(const fseg_inode_t *inode)
{
ulint i;
ulint count = 0;
for (i = 0; i < FSEG_FRAG_ARR_N_SLOTS; i++) {
if (FIL_NULL != fseg_get_nth_frag_page_no(inode, i)) {
count++;
}
}
return(count);
}
/** Create a new segment.
@param space tablespace
@param byte_offset byte offset of the created segment header
@param mtr mini-transaction
@param err error code
@param has_done_reservation whether fsp_reserve_free_extents() was invoked
@param block block where segment header is placed,
or NULL to allocate an additional page for that
@return the block where the segment header is placed, x-latched
@retval nullptr if could not create segment */
buf_block_t*
fseg_create(fil_space_t *space, ulint byte_offset, mtr_t *mtr, dberr_t *err,
bool has_done_reservation, buf_block_t *block)
{
fseg_inode_t* inode;
ib_id_t seg_id;
uint32_t n_reserved;
bool reserved_extent = false;
DBUG_ENTER("fseg_create");
ut_ad(mtr);
ut_ad(byte_offset >= FIL_PAGE_DATA);
ut_ad(byte_offset + FSEG_HEADER_SIZE
<= srv_page_size - FIL_PAGE_DATA_END);
mtr->x_lock_space(space);
ut_d(space->modify_check(*mtr));
ut_ad(!block || block->page.id().space() == space->id);
buf_block_t* header = fsp_get_header(space, mtr, err);
if (!header) {
block = nullptr;
goto funct_exit;
}
buf_block_t* iblock;
inode_alloc:
inode = fsp_alloc_seg_inode(space, header, &iblock, mtr, err);
if (!inode) {
block = nullptr;
reserve_extent:
if (!has_done_reservation && !reserved_extent) {
*err = fsp_reserve_free_extents(&n_reserved, space, 2,
FSP_NORMAL, mtr);
if (UNIV_UNLIKELY(*err != DB_SUCCESS)) {
DBUG_RETURN(nullptr);
}
/* Extents reserved successfully. So
try allocating the page or inode */
reserved_extent = true;
if (inode) {
goto page_alloc;
}
goto inode_alloc;
}
if (inode) {
fsp_free_seg_inode(space, inode, iblock, mtr);
}
goto funct_exit;
}
/* Read the next segment id from space header and increment the
value in space header */
seg_id = mach_read_from_8(FSP_HEADER_OFFSET + FSP_SEG_ID
+ header->page.frame);
mtr->write<8>(*header,
FSP_HEADER_OFFSET + FSP_SEG_ID + header->page.frame,
seg_id + 1);
mtr->write<8>(*iblock, inode + FSEG_ID, seg_id);
ut_ad(!mach_read_from_4(inode + FSEG_NOT_FULL_N_USED));
flst_init(*iblock, inode + FSEG_FREE, mtr);
flst_init(*iblock, inode + FSEG_NOT_FULL, mtr);
flst_init(*iblock, inode + FSEG_FULL, mtr);
mtr->memcpy(*iblock, inode + FSEG_MAGIC_N, FSEG_MAGIC_N_BYTES, 4);
compile_time_assert(FSEG_FRAG_SLOT_SIZE == 4);
compile_time_assert(FIL_NULL == 0xffffffff);
mtr->memset(iblock,
uint16_t(inode - iblock->page.frame) + FSEG_FRAG_ARR,
FSEG_FRAG_SLOT_SIZE * FSEG_FRAG_ARR_N_SLOTS, 0xff);
if (!block) {
page_alloc:
block = fseg_alloc_free_page_low(space,
inode, iblock, 0, FSP_UP,
#ifdef UNIV_DEBUG
has_done_reservation,
#endif /* UNIV_DEBUG */
mtr, mtr, err);
if (!block) {
ut_ad(!has_done_reservation);
goto reserve_extent;
}
ut_d(const auto x = block->page.lock.x_lock_count());
ut_ad(x || block->page.lock.not_recursive());
ut_ad(x <= 2);
ut_ad(!fil_page_get_type(block->page.frame));
mtr->write<1>(*block, FIL_PAGE_TYPE + 1 + block->page.frame,
FIL_PAGE_TYPE_SYS);
}
mtr->write<2>(*block, byte_offset + FSEG_HDR_OFFSET
+ block->page.frame, page_offset(inode));
mtr->write<4>(*block, byte_offset + FSEG_HDR_PAGE_NO
+ block->page.frame, iblock->page.id().page_no());
mtr->write<4,mtr_t::MAYBE_NOP>(*block, byte_offset + FSEG_HDR_SPACE
+ block->page.frame, space->id);
funct_exit:
if (!has_done_reservation && reserved_extent) {
space->release_free_extents(n_reserved);
}
DBUG_RETURN(block);
}
/**********************************************************************//**
Calculates the number of pages reserved by a segment, and how many pages are
currently used.
@return number of reserved pages */
static
ulint
fseg_n_reserved_pages_low(
/*======================*/
const fseg_inode_t* inode, /*!< in: segment inode */
ulint* used) /*!< out: number of pages used (not
more than reserved) */
{
*used = mach_read_from_4(inode + FSEG_NOT_FULL_N_USED)
+ FSP_EXTENT_SIZE * flst_get_len(inode + FSEG_FULL)
+ fseg_get_n_frag_pages(inode);
return fseg_get_n_frag_pages(inode)
+ FSP_EXTENT_SIZE * flst_get_len(inode + FSEG_FREE)
+ FSP_EXTENT_SIZE * flst_get_len(inode + FSEG_NOT_FULL)
+ FSP_EXTENT_SIZE * flst_get_len(inode + FSEG_FULL);
}
/** Calculate the number of pages reserved by a segment,
and how many pages are currently used.
@param[in] block buffer block containing the file segment header
@param[in] header file segment header
@param[out] used number of pages that are used (not more than reserved)
@param[in,out] mtr mini-transaction
@return number of reserved pages */
ulint fseg_n_reserved_pages(const buf_block_t &block,
const fseg_header_t *header, ulint *used,
mtr_t *mtr)
{
ut_ad(page_align(header) == block.page.frame);
buf_block_t *iblock;
if (fseg_inode_t *inode=
fseg_inode_try_get(header, block.page.id().space(), block.zip_size(),
mtr, &iblock))
return fseg_n_reserved_pages_low(inode, used);
return *used= 0;
}
MY_ATTRIBUTE((nonnull, warn_unused_result))
/** Tries to fill the free list of a segment with consecutive free extents.
This happens if the segment is big enough to allow extents in the free list,
the free list is empty, and the extents can be allocated consecutively from
the hint onward.
@param[in] inode segment inode
@param[in,out] iblock segment inode page
@param[in] space tablespace
@param[in] hint hint which extent would be good as the first extent
@param[in,out] mtr mini-transaction */
static dberr_t fseg_fill_free_list(const fseg_inode_t *inode,
buf_block_t *iblock, fil_space_t *space,
uint32_t hint, mtr_t *mtr)
{
ulint used;
ut_ad(!((page_offset(inode) - FSEG_ARR_OFFSET) % FSEG_INODE_SIZE));
ut_d(space->modify_check(*mtr));
if (fseg_n_reserved_pages_low(inode, &used) <
FSEG_FREE_LIST_LIMIT * FSP_EXTENT_SIZE)
/* The segment is too small to allow extents in free list */
return DB_SUCCESS;
if (UNIV_UNLIKELY(memcmp(FSEG_MAGIC_N_BYTES, FSEG_MAGIC_N + inode, 4)))
{
space->set_corrupted();
return DB_CORRUPTION;
}
if (flst_get_len(inode + FSEG_FREE) > 0)
/* Free list is not empty */
return DB_SUCCESS;
for (ulint i= 0; i < FSEG_FREE_LIST_MAX_LEN; i++, hint += FSP_EXTENT_SIZE)
{
buf_block_t *xdes;
dberr_t err;
xdes_t *descr= xdes_get_descriptor(space, hint, mtr, &err, &xdes);
if (!descr || XDES_FREE != xdes_get_state(descr))
/* We cannot allocate the desired extent: stop */
return err;
descr= fsp_alloc_free_extent(space, hint, &xdes, mtr, &err);
if (UNIV_UNLIKELY(!descr))
return err;
if (dberr_t err=
flst_add_last(iblock,
static_cast<uint16_t>(inode - iblock->page.frame +
FSEG_FREE), xdes,
static_cast<uint16_t>(descr - xdes->page.frame +
XDES_FLST_NODE),
space->free_limit, mtr))
return err;
xdes_set_state(*xdes, descr, XDES_FSEG, mtr);
mtr->memcpy(*xdes, descr + XDES_ID, inode + FSEG_ID, 8);
}
return DB_SUCCESS;
}
MY_ATTRIBUTE((nonnull, warn_unused_result))
/** Allocates a free extent for the segment: looks first in the free list of
the segment, then tries to allocate from the space free list.
NOTE that the extent returned still resides in the segment free list, it is
not yet taken off it!
@param[in] inode segment inode
@param[in,out] iblock segment inode page
@param[out] xdes extent descriptor page
@param[in,out] space tablespace
@param[in,out] mtr mini-transaction
@param[out] err error code
@retval nullptr if no page could be allocated */
static
xdes_t*
fseg_alloc_free_extent(
const fseg_inode_t* inode,
buf_block_t* iblock,
buf_block_t** xdes,
fil_space_t* space,
mtr_t* mtr,
dberr_t* err)
{
ut_ad(!((page_offset(inode) - FSEG_ARR_OFFSET) % FSEG_INODE_SIZE));
ut_ad(!memcmp(FSEG_MAGIC_N_BYTES, FSEG_MAGIC_N + inode, 4));
ut_d(space->modify_check(*mtr));
if (UNIV_UNLIKELY(page_offset(inode) < FSEG_ARR_OFFSET))
{
corrupted:
*err= DB_CORRUPTION;
space->set_corrupted();
return nullptr;
}
if (flst_get_len(inode + FSEG_FREE))
{
const fil_addr_t first= flst_get_first(inode + FSEG_FREE);
if (first.page >= space->free_limit ||
first.boffset < FSP_HEADER_OFFSET + FSP_HEADER_SIZE ||
first.boffset >= space->physical_size() -
(XDES_SIZE + FIL_PAGE_DATA_END))
goto corrupted;
/* Segment free list is not empty, allocate from it */
return xdes_lst_get_descriptor(*space, first, mtr, xdes, err);
}
xdes_t* descr= fsp_alloc_free_extent(space, 0, xdes, mtr, err);
if (UNIV_UNLIKELY(!descr))
return descr;
xdes_set_state(**xdes, descr, XDES_FSEG, mtr);
mtr->memcpy<mtr_t::MAYBE_NOP>(**xdes, descr + XDES_ID, inode + FSEG_ID, 8);
*err= flst_add_last(iblock,
static_cast<uint16_t>(inode - iblock->page.frame +
FSEG_FREE), *xdes,
static_cast<uint16_t>(descr - (*xdes)->page.frame +
XDES_FLST_NODE),
space->free_limit, mtr);
if (UNIV_LIKELY(*err != DB_SUCCESS))
return nullptr;
/* Try to fill the segment free list */
*err= fseg_fill_free_list(inode, iblock, space,
xdes_get_offset(descr) + FSP_EXTENT_SIZE, mtr);
if (UNIV_UNLIKELY(*err != DB_SUCCESS))
return nullptr;
return descr;
}
/** Allocates a single free page from a segment.
This function implements the intelligent allocation strategy which tries to
minimize file space fragmentation.
@param[in,out] space tablespace
@param[in,out] seg_inode segment inode
@param[in,out] iblock segment inode page
@param[in] hint hint of which page would be desirable
@param[in] direction if the new page is needed because of
an index page split, and records are inserted there in order, into which
direction they go alphabetically: FSP_DOWN, FSP_UP, FSP_NO_DIR
@param[in,out] mtr mini-transaction
@param[in,out] init_mtr mtr or another mini-transaction in
which the page should be initialized.
@param[out] err error code
@return the allocated page
@retval nullptr if no page could be allocated */
static
buf_block_t*
fseg_alloc_free_page_low(
fil_space_t* space,
fseg_inode_t* seg_inode,
buf_block_t* iblock,
uint32_t hint,
byte direction,
#ifdef UNIV_DEBUG
bool has_done_reservation,
/*!< whether the space has already been reserved */
#endif /* UNIV_DEBUG */
mtr_t* mtr,
mtr_t* init_mtr,
dberr_t* err)
{
ib_id_t seg_id;
ulint used;
ulint reserved;
xdes_t* descr; /*!< extent of the hinted page */
uint32_t ret_page; /*!< the allocated page offset, FIL_NULL
if could not be allocated */
xdes_t* ret_descr; /*!< the extent of the allocated page */
buf_block_t* xdes;
ulint n;
ut_ad((direction >= FSP_UP) && (direction <= FSP_NO_DIR));
ut_ad(!memcmp(FSEG_MAGIC_N_BYTES, FSEG_MAGIC_N + seg_inode, 4));
ut_ad(!((page_offset(seg_inode) - FSEG_ARR_OFFSET) % FSEG_INODE_SIZE));
seg_id = mach_read_from_8(seg_inode + FSEG_ID);
ut_ad(seg_id);
ut_d(space->modify_check(*mtr));
ut_ad(fil_page_get_type(page_align(seg_inode)) == FIL_PAGE_INODE);
reserved = fseg_n_reserved_pages_low(seg_inode, &used);
buf_block_t* header = fsp_get_header(space, mtr, err);
if (!header) {
return header;
}
descr = xdes_get_descriptor_with_space_hdr(header, space, hint, mtr,
err, &xdes);
if (!descr) {
if (*err != DB_SUCCESS) {
return nullptr;
}
/* Hint outside space or too high above free limit: reset
hint */
/* The file space header page is always allocated. */
hint = 0;
descr = xdes_get_descriptor(space, hint, mtr, err, &xdes);
if (!descr) {
return nullptr;
}
}
const uint32_t extent_size = FSP_EXTENT_SIZE;
ret_descr = descr;
/* Try to get the page from extent which belongs to segment */
if (xdes_get_state(descr) == XDES_FSEG
&& mach_read_from_8(descr + XDES_ID) == seg_id) {
/* Get the page from the segment extent */
if (xdes_is_free(descr, hint % extent_size)) {
take_hinted_page:
ret_page = hint;
goto got_hinted_page;
} else if (!xdes_is_full(descr)) {
/* Take the page from the same extent as the
hinted page (and the extent already belongs to
the segment) */
ret_page = xdes_find_free(descr, hint % extent_size);
if (ret_page == FIL_NULL) {
ut_ad(!has_done_reservation);
return nullptr;
}
ret_page += xdes_get_offset(ret_descr);
goto alloc_done;
}
}
/** If the number of unused but reserved pages in a segment is
esser than minimum value of 1/8 of reserved pages or
4 * FSP_EXTENT_SIZE and there are at least half of extent size
used pages, then we allow a new empty extent to be added to
the segment in fseg_alloc_free_page_general(). Otherwise, we use
unused pages of the segment. */
if (used < extent_size / 2 ||
reserved - used >= reserved / 8 ||
reserved - used >= extent_size * 4) {
} else if (xdes_get_state(descr) == XDES_FREE) {
/* Allocate the free extent from space and can
take the hinted page */
ret_descr = fsp_alloc_free_extent(space, hint, &xdes,
mtr, err);
if (UNIV_UNLIKELY(ret_descr != descr)) {
if (*err != DB_SUCCESS) {
*err = DB_CORRUPTION;
}
return nullptr;
}
xdes_set_state(*xdes, ret_descr, XDES_FSEG, mtr);
mtr->write<8,mtr_t::MAYBE_NOP>(*xdes, ret_descr + XDES_ID,
seg_id);
*err = flst_add_last(
iblock,
static_cast<uint16_t>(seg_inode - iblock->page.frame
+ FSEG_FREE), xdes,
static_cast<uint16_t>(ret_descr
- xdes->page.frame
+ XDES_FLST_NODE),
space->free_limit, mtr);
if (UNIV_UNLIKELY(*err != DB_SUCCESS)) {
return nullptr;
}
/* Try to fill the segment free list */
*err = fseg_fill_free_list(seg_inode, iblock, space,
hint + extent_size, mtr);
if (UNIV_UNLIKELY(*err != DB_SUCCESS)) {
return nullptr;
}
goto take_hinted_page;
} else if (direction != FSP_NO_DIR) {
ret_descr = fseg_alloc_free_extent(seg_inode, iblock,
&xdes, space, mtr, err);
if (!ret_descr) {
ut_ad(*err != DB_SUCCESS);
return nullptr;
}
/* Take any free extent (which was already assigned
above in the if-condition to ret_descr) and take the
lowest or highest page in it, depending on the direction */
ret_page = xdes_get_offset(ret_descr);
if (direction == FSP_DOWN) {
ret_page += extent_size - 1;
}
goto alloc_done;
}
/* Try to take individual page from the segment or tablespace */
if (reserved - used > 0) {
/* Take any unused page from the segment */
fil_addr_t first;
if (flst_get_len(seg_inode + FSEG_NOT_FULL) > 0) {
first = flst_get_first(seg_inode + FSEG_NOT_FULL);
} else if (flst_get_len(seg_inode + FSEG_FREE) > 0) {
first = flst_get_first(seg_inode + FSEG_FREE);
} else {
ut_ad(!has_done_reservation);
return nullptr;
}
if (first.page >= space->free_limit
|| first.boffset < FSP_HEADER_OFFSET + FSP_HEADER_SIZE
|| first.boffset >= space->physical_size()
- (XDES_SIZE + FIL_PAGE_DATA_END)) {
*err= DB_CORRUPTION;
return nullptr;
}
ret_descr = xdes_lst_get_descriptor(*space, first, mtr, &xdes);
if (!ret_descr) {
return nullptr;
}
ret_page = xdes_find_free(ret_descr);
if (ret_page == FIL_NULL) {
ut_ad(!has_done_reservation);
} else {
ret_page += xdes_get_offset(ret_descr);
}
} else if (used < extent_size / 2) {
/* Allocate an individual page from the space */
buf_block_t* block = fsp_alloc_free_page(
space, hint, mtr, init_mtr, err);
ut_ad(block || !has_done_reservation || *err);
if (block) {
/* Put the page in the fragment page array of the
segment */
n = fseg_find_free_frag_page_slot(seg_inode);
if (UNIV_UNLIKELY(n == ULINT_UNDEFINED)) {
*err = DB_CORRUPTION;
return nullptr;
}
fseg_set_nth_frag_page_no(
seg_inode, iblock, n,
block->page.id().page_no(), mtr);
}
/* fsp_alloc_free_page() invoked fsp_init_file_page()
already. */
return(block);
} else {
/* In worst case, try to allocate a new extent
and take its first page */
ret_descr = fseg_alloc_free_extent(seg_inode, iblock, &xdes,
space, mtr, err);
if (!ret_descr) {
ut_ad(!has_done_reservation || *err);
return nullptr;
} else {
ret_page = xdes_get_offset(ret_descr);
}
}
if (ret_page == FIL_NULL) {
/* Page could not be allocated */
ut_ad(!has_done_reservation);
return nullptr;
}
alloc_done:
if (space->size <= ret_page && !is_predefined_tablespace(space->id)) {
/* It must be that we are extending a single-table
tablespace whose size is still < 64 pages */
if (ret_page >= extent_size) {
sql_print_error("InnoDB: Trying to extend '%s'"
" by single page(s) though the"
" space size " UINT32PF "."
" Page no " UINT32PF ".",
space->chain.start->name, space->size,
ret_page);
ut_ad(!has_done_reservation);
return nullptr;
}
if (!fsp_try_extend_data_file_with_pages(
space, ret_page, header, mtr)) {
/* No disk space left */
ut_ad(!has_done_reservation);
return nullptr;
}
}
/* Skip the check for extending the tablespace.
If the page hint were not within the size of the tablespace,
descr set to nullptr above and reset the hint and the block
was allocated from free_frag (XDES_FREE_FRAG) */
if (ret_descr != NULL) {
got_hinted_page:
/* At this point we know the extent and the page offset.
The extent is still in the appropriate list (FSEG_NOT_FULL
or FSEG_FREE), and the page is not yet marked as used. */
ut_d(buf_block_t* xxdes);
ut_ad(xdes_get_descriptor(space, ret_page, mtr, err, &xxdes)
== ret_descr);
ut_ad(xdes == xxdes);
ut_ad(xdes_is_free(ret_descr, ret_page % extent_size));
*err = fseg_mark_page_used(space, seg_inode, iblock, ret_page,
ret_descr, xdes, mtr);
if (UNIV_UNLIKELY(*err != DB_SUCCESS)) {
return nullptr;
}
}
return fsp_page_create(space, ret_page, init_mtr);
}
/**********************************************************************//**
Allocates a single free page from a segment. This function implements
the intelligent allocation strategy which tries to minimize file space
fragmentation.
@retval NULL if no page could be allocated */
buf_block_t*
fseg_alloc_free_page_general(
/*=========================*/
fseg_header_t* seg_header,/*!< in/out: segment header */
uint32_t hint, /*!< in: hint of which page would be
desirable */
byte direction,/*!< in: if the new page is needed because
of an index page split, and records are
inserted there in order, into which
direction they go alphabetically: FSP_DOWN,
FSP_UP, FSP_NO_DIR */
bool has_done_reservation, /*!< in: true if the caller has
already done the reservation for the page
with fsp_reserve_free_extents, then there
is no need to do the check for this individual
page */
mtr_t* mtr, /*!< in/out: mini-transaction */
mtr_t* init_mtr,/*!< in/out: mtr or another mini-transaction
in which the page should be initialized. */
dberr_t* err) /*!< out: error code */
{
fseg_inode_t* inode;
fil_space_t* space;
buf_block_t* iblock;
buf_block_t* block;
uint32_t n_reserved;
const uint32_t space_id = page_get_space_id(page_align(seg_header));
space = mtr->x_lock_space(space_id);
inode = fseg_inode_try_get(seg_header, space_id, space->zip_size(),
mtr, &iblock, err);
if (!inode) {
return nullptr;
}
if (!space->full_crc32()) {
fil_block_check_type(*iblock, FIL_PAGE_INODE, mtr);
}
if (!has_done_reservation) {
*err = fsp_reserve_free_extents(&n_reserved, space, 2,
FSP_NORMAL, mtr);
if (*err != DB_SUCCESS) {
return nullptr;
}
}
block = fseg_alloc_free_page_low(space,
inode, iblock, hint, direction,
#ifdef UNIV_DEBUG
has_done_reservation,
#endif /* UNIV_DEBUG */
mtr, init_mtr, err);
/* The allocation cannot fail if we have already reserved a
space for the page. */
ut_ad(block || !has_done_reservation || *err);
if (!has_done_reservation) {
space->release_free_extents(n_reserved);
}
return(block);
}
MY_ATTRIBUTE((nonnull, warn_unused_result))
/** Check that we have at least n_pages frag pages free in the first extent
of a single-table tablespace, and they are also physically initialized to
the data file. That is we have already extended the data file so that those
pages are inside the data file. If not, this function extends the tablespace
with pages.
@param[in,out] space tablespace
@param[in,out] header tablespace header, x-latched
@param[in] size tablespace size in pages, less than FSP_EXTENT_SIZE
@param[in,out] mtr mini-transaction
@param[in] n_pages number of pages to reserve
@return error code */
static
dberr_t
fsp_reserve_free_pages(
fil_space_t* space,
buf_block_t* header,
ulint size,
mtr_t* mtr,
uint32_t n_pages)
{
ut_ad(space != fil_system.sys_space && space != fil_system.temp_space);
ut_ad(size < FSP_EXTENT_SIZE);
dberr_t err= DB_OUT_OF_FILE_SPACE;
const xdes_t *descr=
xdes_get_descriptor_with_space_hdr(header, space, 0, mtr, &err);
if (!descr)
return err;
const uint32_t n_used= xdes_get_n_used(descr);
if (size >= n_used + n_pages)
return DB_SUCCESS;
if (n_used > size)
return DB_CORRUPTION;
return fsp_try_extend_data_file_with_pages(space, n_used + n_pages - 1,
header, mtr)
? DB_SUCCESS
: DB_OUT_OF_FILE_SPACE;
}
/** Reserves free pages from a tablespace. All mini-transactions which may
use several pages from the tablespace should call this function beforehand
and reserve enough free extents so that they certainly will be able
to do their operation, like a B-tree page split, fully. Reservations
must be released with function fil_space_t::release_free_extents()!
The alloc_type below has the following meaning: FSP_NORMAL means an
operation which will probably result in more space usage, like an
insert in a B-tree; FSP_UNDO means allocation to undo logs: if we are
deleting rows, then this allocation will in the long run result in
less space usage (after a purge); FSP_CLEANING means allocation done
in a physical record delete (like in a purge) or other cleaning operation
which will result in less space usage in the long run. We prefer the latter
two types of allocation: when space is scarce, FSP_NORMAL allocations
will not succeed, but the latter two allocations will succeed, if possible.
The purpose is to avoid dead end where the database is full but the
user cannot free any space because these freeing operations temporarily
reserve some space.
Single-table tablespaces whose size is < FSP_EXTENT_SIZE pages are a special
case. In this function we would liberally reserve several extents for
every page split or merge in a B-tree. But we do not want to waste disk space
if the table only occupies < FSP_EXTENT_SIZE pages. That is why we apply
different rules in that special case, just ensuring that there are n_pages
free pages available.
@param[out] n_reserved number of extents actually reserved; if we
return true and the tablespace size is <
FSP_EXTENT_SIZE pages, then this can be 0,
otherwise it is n_ext
@param[in,out] space tablespace
@param[in] n_ext number of extents to reserve
@param[in] alloc_type page reservation type (FSP_BLOB, etc)
@param[in,out] mtr the mini transaction
@param[in] n_pages for small tablespaces (tablespace size is
less than FSP_EXTENT_SIZE), number of free
pages to reserve.
@return error code
@retval DB_SUCCESS if we were able to make the reservation */
dberr_t
fsp_reserve_free_extents(
uint32_t* n_reserved,
fil_space_t* space,
uint32_t n_ext,
fsp_reserve_t alloc_type,
mtr_t* mtr,
uint32_t n_pages)
{
ulint reserve;
ut_ad(mtr);
*n_reserved = n_ext;
const uint32_t extent_size = FSP_EXTENT_SIZE;
mtr->x_lock_space(space);
const unsigned physical_size = space->physical_size();
dberr_t err;
buf_block_t* header = fsp_get_header(space, mtr, &err);
if (!header) {
return err;
}
try_again:
uint32_t size = mach_read_from_4(FSP_HEADER_OFFSET + FSP_SIZE
+ header->page.frame);
ut_ad(size == space->size_in_header);
if (size < extent_size && n_pages < extent_size / 2) {
/* Use different rules for small single-table tablespaces */
*n_reserved = 0;
return fsp_reserve_free_pages(space, header, size,
mtr, n_pages);
}
uint32_t n_free_list_ext = flst_get_len(FSP_HEADER_OFFSET + FSP_FREE
+ header->page.frame);
ut_ad(space->free_len == n_free_list_ext);
uint32_t free_limit = mach_read_from_4(FSP_HEADER_OFFSET
+ FSP_FREE_LIMIT
+ header->page.frame);
ut_ad(space->free_limit == free_limit);
/* Below we play safe when counting free extents above the free limit:
some of them will contain extent descriptor pages, and therefore
will not be free extents */
uint32_t n_free_up;
if (size >= free_limit) {
n_free_up = (size - free_limit) / extent_size;
if (n_free_up) {
n_free_up--;
n_free_up -= n_free_up / (physical_size / extent_size);
}
} else {
ut_ad(alloc_type == FSP_BLOB);
n_free_up = 0;
}
uint32_t n_free = n_free_list_ext + n_free_up;
switch (alloc_type) {
case FSP_NORMAL:
/* We reserve 1 extent + 0.5 % of the space size to undo logs
and 1 extent + 0.5 % to cleaning operations; NOTE: this source
code is duplicated in the function below! */
reserve = 2 + ((size / extent_size) * 2) / 200;
if (n_free <= reserve + n_ext) {
goto try_to_extend;
}
break;
case FSP_UNDO:
/* We reserve 0.5 % of the space size to cleaning operations */
reserve = 1 + ((size / extent_size) * 1) / 200;
if (n_free <= reserve + n_ext) {
goto try_to_extend;
}
break;
case FSP_CLEANING:
case FSP_BLOB:
reserve = 0;
break;
default:
ut_error;
}
if (space->reserve_free_extents(n_free, n_ext)) {
return DB_SUCCESS;
}
try_to_extend:
if (fsp_try_extend_data_file(space, header, mtr)) {
goto try_again;
}
return DB_OUT_OF_FILE_SPACE;
}
MY_ATTRIBUTE((nonnull, warn_unused_result))
/** Frees a single page of a segment.
@param[in] seg_inode segment inode
@param[in,out] space tablespace
@param[in] offset page number
@param[in,out] mtr mini-transaction
@param[in] ahi Drop adaptive hash index
@return error code */
static
dberr_t
fseg_free_page_low(
fseg_inode_t* seg_inode,
buf_block_t* iblock,
fil_space_t* space,
uint32_t offset,
mtr_t* mtr
#ifdef BTR_CUR_HASH_ADAPT
,bool ahi=false
#endif /* BTR_CUR_HASH_ADAPT */
)
{
ut_ad(!memcmp(FSEG_MAGIC_N_BYTES, FSEG_MAGIC_N + seg_inode, 4));
ut_ad(!((page_offset(seg_inode) - FSEG_ARR_OFFSET) % FSEG_INODE_SIZE));
ut_ad(iblock->page.frame == page_align(seg_inode));
ut_d(space->modify_check(*mtr));
#ifdef BTR_CUR_HASH_ADAPT
if (ahi) {
btr_search_drop_page_hash_when_freed(
page_id_t(space->id, offset));
}
#endif /* BTR_CUR_HASH_ADAPT */
const uint32_t extent_size = FSP_EXTENT_SIZE;
ut_ad(ut_is_2pow(extent_size));
buf_block_t* xdes;
dberr_t err;
xdes_t* descr = xdes_get_descriptor(space, offset, mtr, &err, &xdes);
if (!descr) {
return err;
}
if (UNIV_UNLIKELY(xdes_is_free(descr, offset & (extent_size - 1)))) {
corrupted:
space->set_corrupted();
return DB_CORRUPTION;
}
if (xdes_get_state(descr) != XDES_FSEG) {
/* The page is in the fragment pages of the segment */
for (ulint i = 0;; i++) {
if (fseg_get_nth_frag_page_no(seg_inode, i)
!= offset) {
continue;
}
compile_time_assert(FIL_NULL == 0xffffffff);
mtr->memset(iblock, uint16_t(seg_inode
- iblock->page.frame)
+ FSEG_FRAG_ARR
+ i * FSEG_FRAG_SLOT_SIZE, 4, 0xff);
break;
}
return fsp_free_page(space, offset, mtr);
}
/* If we get here, the page is in some extent of the segment */
if (UNIV_UNLIKELY(memcmp(descr + XDES_ID, seg_inode + FSEG_ID, 8))) {
goto corrupted;
}
byte* p_not_full = seg_inode + FSEG_NOT_FULL_N_USED;
uint32_t not_full_n_used = mach_read_from_4(p_not_full);
const uint16_t xoffset= uint16_t(descr - xdes->page.frame
+ XDES_FLST_NODE);
const uint16_t ioffset= uint16_t(seg_inode - iblock->page.frame);
const uint32_t limit = space->free_limit;
if (xdes_is_full(descr)) {
/* The fragment is full: move it to another list */
err = flst_remove(iblock,
static_cast<uint16_t>(FSEG_FULL + ioffset),
xdes, xoffset, limit, mtr);
if (UNIV_UNLIKELY(err != DB_SUCCESS)) {
return err;
}
err = flst_add_last(iblock, static_cast<uint16_t>(FSEG_NOT_FULL
+ ioffset),
xdes, xoffset, limit, mtr);
if (UNIV_UNLIKELY(err != DB_SUCCESS)) {
return err;
}
not_full_n_used += extent_size - 1;
} else {
if (!not_full_n_used) {
goto corrupted;
}
not_full_n_used--;
}
mtr->write<4>(*iblock, p_not_full, not_full_n_used);
xdes_set_free<true>(*xdes, descr, offset & (extent_size - 1), mtr);
if (!xdes_get_n_used(descr)) {
err = flst_remove(iblock, static_cast<uint16_t>(FSEG_NOT_FULL
+ ioffset),
xdes, xoffset, limit, mtr);
if (UNIV_UNLIKELY(err != DB_SUCCESS)) {
return err;
}
err = fsp_free_extent(space, offset, mtr);
if (UNIV_UNLIKELY(err != DB_SUCCESS)) {
return err;
}
}
mtr->free(*space, static_cast<uint32_t>(offset));
return DB_SUCCESS;
}
/** Free a page in a file segment.
@param[in,out] seg_header file segment header
@param[in,out] space tablespace
@param[in] offset page number
@param[in,out] mtr mini-transaction
@param[in] have_latch whether space->x_lock() was already called
@return error code */
dberr_t fseg_free_page(fseg_header_t *seg_header, fil_space_t *space,
uint32_t offset, mtr_t *mtr, bool have_latch)
{
buf_block_t *iblock;
if (have_latch)
ut_ad(space->is_owner());
else
mtr->x_lock_space(space);
DBUG_PRINT("fseg_free_page",
("space_id: " ULINTPF ", page_no: %u", space->id, offset));
dberr_t err;
if (fseg_inode_t *seg_inode= fseg_inode_try_get(seg_header,
space->id, space->zip_size(),
mtr, &iblock, &err))
{
if (!space->full_crc32())
fil_block_check_type(*iblock, FIL_PAGE_INODE, mtr);
return fseg_free_page_low(seg_inode, iblock, space, offset, mtr);
}
return err;
}
/** Determine whether a page is allocated.
@param space tablespace
@param page page number
@return error code
@retval DB_SUCCESS if the page is marked as free
@retval DB_SUCCESS_LOCKED_REC if the page is marked as allocated */
dberr_t fseg_page_is_allocated(fil_space_t *space, unsigned page)
{
mtr_t mtr;
uint32_t dpage= xdes_calc_descriptor_page(space->zip_size(), page);
const unsigned zip_size= space->zip_size();
dberr_t err= DB_SUCCESS;
mtr.start();
if (!space->is_owner())
mtr.x_lock_space(space);
if (page >= space->free_limit || page >= space->size_in_header);
else if (const buf_block_t *b=
buf_page_get_gen(page_id_t(space->id, dpage), space->zip_size(),
RW_S_LATCH, nullptr, BUF_GET_POSSIBLY_FREED,
&mtr, &err))
{
if (!dpage &&
(space->free_limit !=
mach_read_from_4(FSP_FREE_LIMIT + FSP_HEADER_OFFSET +
b->page.frame) ||
space->size_in_header !=
mach_read_from_4(FSP_SIZE + FSP_HEADER_OFFSET + b->page.frame)))
err= DB_CORRUPTION;
else
err= xdes_is_free(b->page.frame + XDES_ARR_OFFSET + XDES_SIZE
* xdes_calc_descriptor_index(zip_size, page),
page & (FSP_EXTENT_SIZE - 1))
? DB_SUCCESS
: DB_SUCCESS_LOCKED_REC;
}
mtr.commit();
return err;
}
MY_ATTRIBUTE((nonnull, warn_unused_result))
/** Free an extent of a segment to the space free list.
@param[in,out] seg_inode segment inode
@param[in,out] space tablespace
@param[in] page page number in the extent
@param[in,out] mtr mini-transaction
@return error code */
static
dberr_t
fseg_free_extent(
fseg_inode_t* seg_inode,
buf_block_t* iblock,
fil_space_t* space,
uint32_t page,
mtr_t* mtr
#ifdef BTR_CUR_HASH_ADAPT
,bool ahi=false
#endif /* BTR_CUR_HASH_ADAPT */
)
{
buf_block_t* xdes;
dberr_t err;
xdes_t* descr = xdes_get_descriptor(space, page, mtr, &err, &xdes);
if (!descr) {
return err;
}
if (UNIV_UNLIKELY(xdes_get_state(descr) != XDES_FSEG
|| memcmp(descr + XDES_ID, seg_inode + FSEG_ID, 8)
|| memcmp(FSEG_MAGIC_N_BYTES, FSEG_MAGIC_N
+ seg_inode, 4))) {
return DB_CORRUPTION;
}
ut_d(space->modify_check(*mtr));
const uint32_t first_page_in_extent = page - (page % FSP_EXTENT_SIZE);
const uint16_t xoffset= uint16_t(descr - xdes->page.frame
+ XDES_FLST_NODE);
const uint16_t ioffset= uint16_t(seg_inode - iblock->page.frame);
#ifdef BTR_CUR_HASH_ADAPT
if (ahi) {
for (uint32_t i = 0; i < FSP_EXTENT_SIZE; i++) {
if (!xdes_is_free(descr, i)) {
/* Drop search system page hash index
if the page is found in the pool and
is hashed */
btr_search_drop_page_hash_when_freed(
page_id_t(space->id,
first_page_in_extent + i));
}
}
}
#endif /* BTR_CUR_HASH_ADAPT */
uint16_t lst;
uint32_t limit = space->free_limit;
if (xdes_is_full(descr)) {
lst = static_cast<uint16_t>(FSEG_FULL + ioffset);
remove:
err = flst_remove(iblock, lst, xdes, xoffset, limit, mtr);
if (UNIV_UNLIKELY(err != DB_SUCCESS)) {
return err;
}
} else if (!xdes_get_n_used(descr)) {
lst = static_cast<uint16_t>(FSEG_FREE + ioffset);
goto remove;
} else {
err = flst_remove(
iblock, static_cast<uint16_t>(FSEG_NOT_FULL + ioffset),
xdes, xoffset, limit, mtr);
if (UNIV_UNLIKELY(err != DB_SUCCESS)) {
return err;
}
uint32_t not_full_n_used = mach_read_from_4(
FSEG_NOT_FULL_N_USED + seg_inode);
uint32_t descr_n_used = xdes_get_n_used(descr);
if (not_full_n_used < descr_n_used) {
return DB_CORRUPTION;
}
mtr->write<4>(*iblock, seg_inode + FSEG_NOT_FULL_N_USED,
not_full_n_used - descr_n_used);
}
std::vector<uint8_t> going_to_free;
static_assert(FSP_EXTENT_SIZE_MIN == 256, "compatibility");
static_assert(FSP_EXTENT_SIZE_MAX == 64, "compatibility");
for (uint32_t i = 0; i < FSP_EXTENT_SIZE; i++) {
if (!xdes_is_free(descr, i)) {
going_to_free.emplace_back(uint8_t(i));
}
}
if (dberr_t err = fsp_free_extent(space, page, mtr)) {
return err;
}
for (uint32_t i : going_to_free) {
mtr->free(*space, first_page_in_extent + i);
buf_page_free(space, first_page_in_extent + i, mtr);
}
return DB_SUCCESS;
}
/** Frees part of a segment. This function can be used to free
a segment by repeatedly calling this function in different
mini-transactions. Doing the freeing in a single mini-transaction
might result in too big a mini-transaction.
@param header segment header; NOTE: if the header resides on first
page of the frag list of the segment, this pointer
becomes obsolete after the last freeing step
@param mtr mini-transaction
@param ahi Drop the adaptive hash index
@return whether the freeing was completed */
bool
fseg_free_step(
fseg_header_t* header,
mtr_t* mtr
#ifdef BTR_CUR_HASH_ADAPT
,bool ahi
#endif /* BTR_CUR_HASH_ADAPT */
)
{
ulint n;
fseg_inode_t* inode;
const uint32_t space_id = page_get_space_id(page_align(header));
const uint32_t header_page = page_get_page_no(page_align(header));
fil_space_t* space = mtr->x_lock_space(space_id);
xdes_t* descr = xdes_get_descriptor(space, header_page, mtr);
if (!descr) {
return true;
}
/* Check that the header resides on a page which has not been
freed yet */
if (UNIV_UNLIKELY(xdes_is_free(descr,
header_page & (FSP_EXTENT_SIZE - 1)))) {
/* Some corruption was detected: stop the freeing
in order to prevent a crash. */
return true;
}
buf_block_t* iblock;
const ulint zip_size = space->zip_size();
inode = fseg_inode_try_get(header, space_id, zip_size, mtr, &iblock);
if (!inode || space->is_stopping()) {
return true;
}
if (!space->full_crc32()) {
fil_block_check_type(*iblock, FIL_PAGE_INODE, mtr);
}
dberr_t err;
descr = fseg_get_first_extent(inode, space, mtr, &err);
if (descr) {
/* Free the extent held by the segment */
return fseg_free_extent(inode, iblock, space,
xdes_get_offset(descr), mtr
#ifdef BTR_CUR_HASH_ADAPT
, ahi
#endif /* BTR_CUR_HASH_ADAPT */
) != DB_SUCCESS;
}
if (err != DB_SUCCESS || space->is_stopping()) {
return true;
}
/* Free a frag page */
n = fseg_find_last_used_frag_page_slot(inode);
if (n == ULINT_UNDEFINED) {
/* Freeing completed: free the segment inode */
fsp_free_seg_inode(space, inode, iblock, mtr);
return true;
}
uint32_t page_no = fseg_get_nth_frag_page_no(inode, n);
if (fseg_free_page_low(inode, iblock, space, page_no, mtr
#ifdef BTR_CUR_HASH_ADAPT
, ahi
#endif /* BTR_CUR_HASH_ADAPT */
) != DB_SUCCESS) {
return true;
}
buf_page_free(space, page_no, mtr);
n = fseg_find_last_used_frag_page_slot(inode);
if (n == ULINT_UNDEFINED) {
/* Freeing completed: free the segment inode */
fsp_free_seg_inode(space, inode, iblock, mtr);
return true;
}
return false;
}
bool
fseg_free_step_not_header(
fseg_header_t* header,
mtr_t* mtr
#ifdef BTR_CUR_HASH_ADAPT
,bool ahi
#endif /* BTR_CUR_HASH_ADAPT */
)
{
fseg_inode_t* inode;
const uint32_t space_id = page_get_space_id(page_align(header));
ut_ad(mtr->is_named_space(space_id));
fil_space_t* space = mtr->x_lock_space(space_id);
buf_block_t* iblock;
inode = fseg_inode_try_get(header, space_id, space->zip_size(),
mtr, &iblock);
if (space->is_stopping()) {
return true;
}
if (!inode) {
ib::warn() << "Double free of "
<< page_id_t(space_id,
page_get_page_no(page_align(header)));
return true;
}
if (!space->full_crc32()) {
fil_block_check_type(*iblock, FIL_PAGE_INODE, mtr);
}
dberr_t err;
if (xdes_t* descr = fseg_get_first_extent(inode, space, mtr, &err)) {
/* Free the extent held by the segment */
return fseg_free_extent(inode, iblock, space,
xdes_get_offset(descr),
mtr
#ifdef BTR_CUR_HASH_ADAPT
, ahi
#endif /* BTR_CUR_HASH_ADAPT */
) != DB_SUCCESS;
} else if (err != DB_SUCCESS) {
return true;
}
/* Free a frag page */
ulint n = fseg_find_last_used_frag_page_slot(inode);
if (UNIV_UNLIKELY(n == ULINT_UNDEFINED)) {
return true;
}
uint32_t page_no = fseg_get_nth_frag_page_no(inode, n);
if (page_no == page_get_page_no(page_align(header))) {
return true;
}
if (fseg_free_page_low(inode, iblock, space, page_no, mtr
#ifdef BTR_CUR_HASH_ADAPT
, ahi
#endif /* BTR_CUR_HASH_ADAPT */
) != DB_SUCCESS) {
return true;
}
buf_page_free(space, page_no, mtr);
return false;
}
/** Returns the first extent descriptor for a segment.
We think of the extent lists of the segment catenated in the order
FSEG_FULL -> FSEG_NOT_FULL -> FSEG_FREE.
@param[in] inode segment inode
@param[in] space tablespace
@param[in,out] mtr mini-transaction
@return the first extent descriptor
@retval nullptr if none, or on corruption */
MY_ATTRIBUTE((nonnull, warn_unused_result))
static
xdes_t*
fseg_get_first_extent(
fseg_inode_t* inode,
const fil_space_t* space,
mtr_t* mtr,
dberr_t* err)
{
if (UNIV_UNLIKELY(space->id != page_get_space_id(page_align(inode)) ||
memcmp(inode + FSEG_MAGIC_N, FSEG_MAGIC_N_BYTES, 4)))
{
corrupted:
*err= DB_CORRUPTION;
return nullptr;
}
fil_addr_t first;
if (flst_get_len(inode + FSEG_FULL))
first= flst_get_first(inode + FSEG_FULL);
else if (flst_get_len(inode + FSEG_NOT_FULL))
first= flst_get_first(inode + FSEG_NOT_FULL);
else if (flst_get_len(inode + FSEG_FREE))
first= flst_get_first(inode + FSEG_FREE);
else
{
*err= DB_SUCCESS;
return nullptr;
}
if (first.page >= space->free_limit ||
first.boffset < FSP_HEADER_OFFSET + FSP_HEADER_SIZE ||
first.boffset >= space->physical_size() -
(XDES_SIZE + FIL_PAGE_DATA_END))
goto corrupted;
return xdes_lst_get_descriptor(*space, first, mtr, nullptr, err);
}
#ifdef UNIV_BTR_PRINT
/*******************************************************************//**
Writes info of a segment. */
static void fseg_print_low(const fseg_inode_t *inode)
{
ulint space;
ulint n_used;
ulint n_frag;
ulint n_free;
ulint n_not_full;
ulint n_full;
ulint reserved;
ulint used;
ulint page_no;
ib_id_t seg_id;
space = page_get_space_id(page_align(inode));
page_no = page_get_page_no(page_align(inode));
reserved = fseg_n_reserved_pages_low(inode, &used);
seg_id = mach_read_from_8(inode + FSEG_ID);
n_used = mach_read_from_4(inode + FSEG_NOT_FULL_N_USED);
n_frag = fseg_get_n_frag_pages(inode);
n_free = flst_get_len(inode + FSEG_FREE);
n_not_full = flst_get_len(inode + FSEG_NOT_FULL);
n_full = flst_get_len(inode + FSEG_FULL);
ib::info() << "SEGMENT id " << seg_id
<< " space " << space << ";"
<< " page " << page_no << ";"
<< " res " << reserved << " used " << used << ";"
<< " full ext " << n_full << ";"
<< " fragm pages " << n_frag << ";"
<< " free extents " << n_free << ";"
<< " not full extents " << n_not_full << ": pages " << n_used;
ut_ad(!memcmp(FSEG_MAGIC_N_BYTES, FSEG_MAGIC_N + inode, 4));
}
/*******************************************************************//**
Writes info of a segment. */
void
fseg_print(
/*=======*/
fseg_header_t* header, /*!< in: segment header */
mtr_t* mtr) /*!< in/out: mini-transaction */
{
const fil_space_t *space=
mtr->x_lock_space(page_get_space_id(page_align(header)));
buf_block_t *block;
if (fseg_inode_t *inode=
fseg_inode_try_get(header, space->id, space->zip_size(), mtr, &block))
fseg_print_low(inode);
}
#endif /* UNIV_BTR_PRINT */
#ifdef UNIV_DEBUG
std::ostream &fseg_header::to_stream(std::ostream &out) const
{
out << "[fseg_header_t: space="
<< mach_read_from_4(m_header + FSEG_HDR_SPACE)
<< ", page=" << mach_read_from_4(m_header + FSEG_HDR_PAGE_NO)
<< ", offset=" << mach_read_from_2(m_header + FSEG_HDR_OFFSET) << "]";
return out;
}
#endif /* UNIV_DEBUG */
|