summaryrefslogtreecommitdiffstats
path: root/src/rgw/driver/rados/rgw_sal_rados.cc
blob: 4c05421653b0f76b4e6fe0c859450b1578b4ac26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab ft=cpp

/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2020 Red Hat, Inc.
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation. See file COPYING.
 *
 */

#include <asm-generic/errno-base.h>
#include <errno.h>
#include <fmt/core.h>
#include <stdlib.h>
#include <string>
#include <system_error>
#include <filesystem>
#include <unistd.h>
#include <sstream>
#include <boost/algorithm/string.hpp>
#include <boost/process.hpp>

#include "common/async/blocked_completion.h"
#include "include/function2.hpp"

#include "common/Clock.h"
#include "common/ceph_time.h"
#include "common/errno.h"

#include "role.h"
#include "rgw_obj_types.h"
#include "rgw_rados.h"
#include "rgw_sal.h"
#include "rgw_sal_rados.h"
#include "rgw_bucket.h"
#include "rgw_multi.h"
#include "rgw_acl.h"
#include "rgw_acl_s3.h"
#include "rgw_aio.h"
#include "rgw_aio_throttle.h"
#include "rgw_tools.h"
#include "rgw_tracer.h"
#include "rgw_oidc_provider.h"

#include "rgw_zone.h"
#include "rgw_rest_conn.h"
#include "rgw_service.h"
#include "rgw_lc.h"
#include "rgw_lc_tier.h"
#include "rgw_mdlog.h"
#include "rgw_rest_admin.h"
#include "rgw_rest_bucket.h"
#include "rgw_rest_metadata.h"
#include "rgw_rest_log.h"
#include "rgw_rest_config.h"
#include "rgw_rest_ratelimit.h"
#include "rgw_rest_realm.h"
#include "rgw_rest_user.h"
#include "rgw_lc_tier.h"
#include "rgw_bucket_logging.h"
#include "services/svc_sys_obj.h"
#include "services/svc_mdlog.h"
#include "services/svc_cls.h"
#include "services/svc_bilog_rados.h"
#include "services/svc_bi_rados.h"
#include "services/svc_zone.h"
#include "services/svc_tier_rados.h"
#include "services/svc_quota.h"
#include "services/svc_config_key.h"
#include "services/svc_zone_utils.h"
#include "services/svc_user.h"
#include "services/svc_sys_obj_cache.h"
#include "cls/rgw/cls_rgw_client.h"

#include "account.h"
#include "buckets.h"
#include "group.h"
#include "groups.h"
#include "roles.h"
#include "users.h"
#include "rgw_pubsub.h"
#include "topic.h"
#include "topics.h"

#define dout_subsys ceph_subsys_rgw

using namespace std;

static string mp_ns = RGW_OBJ_NS_MULTIPART;

namespace rgw::sal {

// default number of entries to list with each bucket listing call
// (use marker to bridge between calls)
static constexpr size_t listing_max_entries = 1000;
const std::string pubsub_oid_prefix = "pubsub.";
const std::string pubsub_bucket_oid_infix  = ".bucket.";

static int drain_aio(std::list<librados::AioCompletion*>& handles)
{
  int ret = 0;
  while (!handles.empty()) {
    librados::AioCompletion* handle = handles.front();
    handles.pop_front();
    handle->wait_for_complete();
    int r = handle->get_return_value();
    handle->release();
    if (r < 0) {
      ret = r;
    }
  }
  return ret;
}

// return the {user}.buckets or {account}.buckets object
static rgw_raw_obj get_owner_buckets_obj(RGWSI_User* svc_user,
                                         RGWSI_Zone* svc_zone,
                                         const rgw_owner& owner)
{
  struct visitor {
    RGWSI_User* svc_user;
    RGWSI_Zone* svc_zone;

    rgw_raw_obj operator()(const rgw_user& user) {
      return svc_user->get_buckets_obj(user);
    }
    rgw_raw_obj operator()(const rgw_account_id& id) {
      const RGWZoneParams& zone = svc_zone->get_zone_params();
      return rgwrados::account::get_buckets_obj(zone, id);
    }
  };
  return std::visit(visitor{svc_user, svc_zone}, owner);
}

int RadosStore::list_buckets(const DoutPrefixProvider* dpp,
                             const rgw_owner& owner, const std::string& tenant,
                             const std::string& marker, const std::string& end_marker,
                             uint64_t max, bool need_stats,
                             BucketList& listing, optional_yield y)
{
  librados::Rados& rados = *getRados()->get_rados_handle();
  const rgw_raw_obj& obj = get_owner_buckets_obj(svc()->user, svc()->zone, owner);

  int ret = rgwrados::buckets::list(dpp, y, rados, obj, tenant,
                                    marker, end_marker, max, listing);
  if (ret < 0) {
    return ret;
  }

  if (need_stats) {
    ret = ctl()->bucket->read_buckets_stats(listing.buckets, y, dpp);
    if (ret < 0 && ret != -ENOENT) {
      ldpp_dout(dpp, 0) << "ERROR: could not get stats for buckets" << dendl;
      return ret;
    }
  }
  return 0;
}

int RadosBucket::create(const DoutPrefixProvider* dpp,
                        const CreateParams& params,
                        optional_yield y)
{
  rgw_bucket key = get_key();
  key.marker = params.marker;
  key.bucket_id = params.bucket_id;

  int ret = store->getRados()->create_bucket(
      dpp, y, key, params.owner, params.zonegroup_id,
      params.placement_rule, params.zone_placement, params.attrs,
      params.obj_lock_enabled, params.swift_ver_location,
      params.quota, params.creation_time, &bucket_version, info);

  bool existed = false;
  if (ret == -EEXIST) {
    existed = true;
    /* bucket already existed, might have raced with another bucket creation,
     * or might be partial bucket creation that never completed. Read existing
     * bucket info, verify that the reported bucket owner is the current user.
     * If all is ok then update the user's list of buckets.  Otherwise inform
     * client about a name conflict.
     */
    if (info.owner != params.owner) {
      return -ERR_BUCKET_EXISTS;
    }
    ret = 0;
  } else if (ret != 0) {
    return ret;
  }

  ret = link(dpp, params.owner, y, false);
  if (ret && !existed && ret != -EEXIST) {
    /* if it exists (or previously existed), don't remove it! */
    ret = unlink(dpp, params.owner, y);
    if (ret < 0) {
      ldpp_dout(dpp, 0) << "WARNING: failed to unlink bucket: ret=" << ret
		       << dendl;
    }
  } else if (ret == -EEXIST || (ret == 0 && existed)) {
    ret = -ERR_BUCKET_EXISTS;
  }

  return ret;
}

int RadosUser::read_attrs(const DoutPrefixProvider* dpp, optional_yield y)
{
  return store->ctl()->user->get_attrs_by_uid(dpp, get_id(), &attrs, y, &objv_tracker);
}

int RadosUser::merge_and_store_attrs(const DoutPrefixProvider* dpp, Attrs& new_attrs, optional_yield y)
{
  for(auto& it : new_attrs) {
	  attrs[it.first] = it.second;
  }
  return store_user(dpp, y, false);
}

int RadosUser::read_usage(const DoutPrefixProvider *dpp, uint64_t start_epoch, uint64_t end_epoch,
			       uint32_t max_entries, bool* is_truncated,
			       RGWUsageIter& usage_iter,
			       map<rgw_user_bucket, rgw_usage_log_entry>& usage)
{
  std::string bucket_name;
  return store->getRados()->read_usage(dpp, get_id(), bucket_name, start_epoch,
				       end_epoch, max_entries, is_truncated,
				       usage_iter, usage);
}

int RadosUser::trim_usage(const DoutPrefixProvider *dpp, uint64_t start_epoch, uint64_t end_epoch, optional_yield y)
{
  std::string bucket_name;

  return store->getRados()->trim_usage(dpp, get_id(), bucket_name, start_epoch, end_epoch, y);
}

int RadosUser::load_user(const DoutPrefixProvider* dpp, optional_yield y)
{
    return store->ctl()->user->get_info_by_uid(dpp, info.user_id, &info, y, RGWUserCtl::GetParams().set_objv_tracker(&objv_tracker).set_attrs(&attrs));
}

int RadosUser::store_user(const DoutPrefixProvider* dpp, optional_yield y, bool exclusive, RGWUserInfo* old_info)
{
    return store->ctl()->user->store_info(dpp, info, y,
					  RGWUserCtl::PutParams().set_objv_tracker(&objv_tracker)
					  .set_exclusive(exclusive)
					  .set_attrs(&attrs)
					  .set_old_info(old_info));
}

int RadosUser::remove_user(const DoutPrefixProvider* dpp, optional_yield y)
{
    return store->ctl()->user->remove_info(dpp, info, y,
					  RGWUserCtl::RemoveParams().set_objv_tracker(&objv_tracker));
}

int RadosUser::verify_mfa(const std::string& mfa_str, bool* verified,
			  const DoutPrefixProvider* dpp, optional_yield y)
{
  vector<string> params;
  get_str_vec(mfa_str, " ", params);

  if (params.size() != 2) {
    ldpp_dout(dpp, 5) << "NOTICE: invalid mfa string provided: " << mfa_str << dendl;
    return -EINVAL;
  }

  string& serial = params[0];
  string& pin = params[1];

  auto i = info.mfa_ids.find(serial);
  if (i == info.mfa_ids.end()) {
    ldpp_dout(dpp, 5) << "NOTICE: user does not have mfa device with serial=" << serial << dendl;
    return -EACCES;
  }

  int ret = store->svc()->cls->mfa.check_mfa(dpp, info.user_id, serial, pin, y);
  if (ret < 0) {
    ldpp_dout(dpp, 20) << "NOTICE: failed to check MFA, serial=" << serial << dendl;
    return -EACCES;
  }

  *verified = true;

  return 0;
}

int RadosUser::list_groups(const DoutPrefixProvider* dpp, optional_yield y,
                           std::string_view marker, uint32_t max_items,
                           GroupList& listing)
{
  RGWSI_SysObj& sysobj = *store->svc()->sysobj;
  const RGWZoneParams& zone = store->svc()->zone->get_zone_params();

  const auto& ids = info.group_ids;
  for (auto id = ids.lower_bound(marker); id != ids.end(); ++id) {
    if (listing.groups.size() >= max_items) {
      listing.next_marker = *id;
      return 0;
    }

    RGWGroupInfo info;
    Attrs attrs_ignored;
    ceph::real_time mtime_ignored;
    RGWObjVersionTracker objv_ignored;
    int r = rgwrados::group::read(dpp, y, sysobj, zone, *id, info,
                                  attrs_ignored, mtime_ignored, objv_ignored);
    if (r == -ENOENT) {
      continue;
    }
    if (r < 0) {
      return r;
    }
    listing.groups.push_back(std::move(info));
  }

  listing.next_marker.clear();
  return 0;
}

RadosBucket::~RadosBucket() {}

int RadosBucket::remove(const DoutPrefixProvider* dpp,
			bool delete_children,
			optional_yield y)
{
  int ret;

  // Refresh info
  ret = load_bucket(dpp, y);
  if (ret < 0) {
    return ret;
  }

  ListParams params;
  params.list_versions = true;
  params.allow_unordered = true;

  ListResults results;

  do {
    results.objs.clear();

    ret = list(dpp, params, 1000, results, y);
    if (ret < 0) {
      return ret;
    }

    if (!results.objs.empty() && !delete_children) {
      ldpp_dout(dpp, -1) << "ERROR: could not remove non-empty bucket " << info.bucket.name <<
	dendl;
      return -ENOTEMPTY;
    }

    for (const auto& obj : results.objs) {
      rgw_obj_key key(obj.key);
      /* xxx dang */
      ret = rgw_remove_object(dpp, store, this, key, y);
      if (ret < 0 && ret != -ENOENT) {
	return ret;
      }
    }
  } while(results.is_truncated);

  ret = abort_multiparts(dpp, store->ctx(), y);
  if (ret < 0) {
    return ret;
  }

  // remove lifecycle config, if any (XXX note could be made generic)
  if (get_attrs().count(RGW_ATTR_LC)) {
    constexpr bool merge_attrs = false; // don't update xattrs, we're deleting
    (void) store->getRados()->get_lc()->remove_bucket_config(
      dpp, y, this, get_attrs(), merge_attrs);
  }

  // remove bucket-topic mapping
  auto iter = get_attrs().find(RGW_ATTR_BUCKET_NOTIFICATION);
  if (iter != get_attrs().end()) {
    rgw_pubsub_bucket_topics bucket_topics;
    try {
      const auto& bl = iter->second;
      auto biter = bl.cbegin();
      bucket_topics.decode(biter);
    } catch (buffer::error& err) {
      ldpp_dout(dpp, 1) << "ERROR: failed to decode bucket topics for bucket: "
                        << get_name() << dendl;
    }
    if (!bucket_topics.topics.empty()) {
      ret = store->remove_bucket_mapping_from_topics(
          bucket_topics, rgw_make_bucket_entry_name(get_tenant(), get_name()),
          y, dpp);
      if (ret < 0) {
        ldpp_dout(dpp, 1)
            << "ERROR: unable to remove notifications from bucket "
            << get_name() << ". ret=" << ret << dendl;
      }
    }
  }

  librados::Rados& rados = *store->getRados()->get_rados_handle();
  ret = store->ctl()->bucket->sync_owner_stats(dpp, rados, info.owner, info, y, nullptr);
  if (ret < 0) {
     ldout(store->ctx(), 1) << "WARNING: failed sync user stats before bucket delete. ret=" <<  ret << dendl;
  }

  RGWObjVersionTracker ot;

  // if we deleted children above we will force delete, as any that
  // remain is detritus from a prior bug
  ret = store->getRados()->delete_bucket(info, ot, y, dpp, !delete_children);
  if (ret < 0) {
    ldpp_dout(dpp, -1) << "ERROR: could not remove bucket " <<
      info.bucket.name << dendl;
    return ret;
  }

  // if bucket has notification definitions associated with it
  // they should be removed (note that any pending notifications on the bucket are still going to be sent)
  const RGWPubSub ps(store, info.bucket.tenant, *store->svc()->site);
  const RGWPubSub::Bucket ps_bucket(ps, this);
  const auto ps_ret = ps_bucket.remove_notifications(dpp, y);
  if (ps_ret < 0 && ps_ret != -ENOENT) {
    ldpp_dout(dpp, -1) << "ERROR: unable to remove notifications from bucket. ret=" << ps_ret << dendl;
  }

  if (ret = rgw::bucketlogging::bucket_deletion_cleanup(dpp, store, this, y); ret < 0) {
    ldpp_dout(dpp, 1) << "WARNING: could not cleanup bucket logging configuration and pending objects, ret = " << ret << dendl;
  }

  ret = store->ctl()->bucket->unlink_bucket(rados, info.owner,
                                            info.bucket, y, dpp, false);
  if (ret < 0) {
    ldpp_dout(dpp, -1) << "ERROR: unable to remove user bucket information" << dendl;
  }

  return ret;
}

int RadosBucket::remove_bypass_gc(int concurrent_max, bool
				  keep_index_consistent,
				  optional_yield y, const
				  DoutPrefixProvider *dpp)
{
  int ret;
  map<RGWObjCategory, RGWStorageStats> stats;
  map<string, bool> common_prefixes;
  RGWObjectCtx obj_ctx(store);
  CephContext *cct = store->ctx();

  string bucket_ver, master_ver;

  ret = load_bucket(dpp, y);
  if (ret < 0)
    return ret;

  const auto& index = info.get_current_index();
  ret = read_stats(dpp, index, RGW_NO_SHARD, &bucket_ver, &master_ver, stats, NULL);
  if (ret < 0)
    return ret;

  ret = abort_multiparts(dpp, cct, y);
  if (ret < 0) {
    return ret;
  }

  rgw::sal::Bucket::ListParams params;
  rgw::sal::Bucket::ListResults results;

  params.list_versions = true;
  params.allow_unordered = true;

  std::list<librados::AioCompletion*> handles;

  int max_aio = concurrent_max;
  results.is_truncated = true;

  while (results.is_truncated) {
    ret = list(dpp, params, listing_max_entries, results, y);
    if (ret < 0)
      return ret;

    std::vector<rgw_bucket_dir_entry>::iterator it = results.objs.begin();
    for (; it != results.objs.end(); ++it) {
      RGWObjState *astate = NULL;
      RGWObjManifest *amanifest = nullptr;
      rgw_obj obj{get_key(), it->key};

      ret = store->getRados()->get_obj_state(dpp, &obj_ctx, get_info(),
					     obj, &astate, &amanifest,
					     false, y);
      if (ret == -ENOENT) {
        ldpp_dout(dpp, 1) << "WARNING: cannot find obj state for obj " << obj << dendl;
        continue;
      }
      if (ret < 0) {
        ldpp_dout(dpp, -1) << "ERROR: get obj state returned with error " << ret << dendl;
        return ret;
      }

      if (amanifest) {
        RGWObjManifest& manifest = *amanifest;
        RGWObjManifest::obj_iterator miter = manifest.obj_begin(dpp);
        const rgw_obj head_obj = manifest.get_obj();
        rgw_raw_obj raw_head_obj;
        store->get_raw_obj(manifest.get_head_placement_rule(), head_obj, &raw_head_obj);

        for (; miter != manifest.obj_end(dpp) && max_aio--; ++miter) {
          if (!max_aio) {
            ret = drain_aio(handles);
            if (ret < 0) {
              ldpp_dout(dpp, -1) << "ERROR: could not drain handles as aio completion returned with " << ret << dendl;
              return ret;
            }
            max_aio = concurrent_max;
          }

          rgw_raw_obj last_obj = miter.get_location().get_raw_obj(store->getRados());
          if (last_obj == raw_head_obj) {
            // have the head obj deleted at the end
            continue;
          }

          ret = store->getRados()->delete_raw_obj_aio(dpp, last_obj, handles);
          if (ret < 0) {
            ldpp_dout(dpp, -1) << "ERROR: delete obj aio failed with " << ret << dendl;
            return ret;
          }
        } // for all shadow objs

        ret = store->getRados()->delete_obj_aio(dpp, head_obj, get_info(), astate,
                                                handles, keep_index_consistent, y);
        if (ret < 0) {
          ldpp_dout(dpp, -1) << "ERROR: delete obj aio failed with " << ret << dendl;
          return ret;
        }
      }

      if (!max_aio) {
        ret = drain_aio(handles);
        if (ret < 0) {
          ldpp_dout(dpp, -1) << "ERROR: could not drain handles as aio completion returned with " << ret << dendl;
          return ret;
        }
        max_aio = concurrent_max;
      }
      obj_ctx.invalidate(obj);
    } // for all RGW objects in results
  } // while is_truncated

  ret = drain_aio(handles);
  if (ret < 0) {
    ldpp_dout(dpp, -1) << "ERROR: could not drain handles as aio completion returned with " << ret << dendl;
    return ret;
  }

  sync_owner_stats(dpp, y, nullptr);
  if (ret < 0) {
     ldpp_dout(dpp, 1) << "WARNING: failed sync user stats before bucket delete. ret=" <<  ret << dendl;
  }

  RGWObjVersionTracker objv_tracker;

  // this function can only be run if caller wanted children to be
  // deleted, so we can ignore the check for children as any that
  // remain are detritus from a prior bug
  ret = remove(dpp, true, y);
  if (ret < 0) {
    ldpp_dout(dpp, -1) << "ERROR: could not remove bucket " << this << dendl;
    return ret;
  }

  return ret;
}

int RadosBucket::load_bucket(const DoutPrefixProvider* dpp, optional_yield y)
{
  int ret;

  RGWObjVersionTracker ep_ot;
  if (info.bucket.bucket_id.empty()) {
    ret = store->ctl()->bucket->read_bucket_info(info.bucket, &info, y, dpp,
				      RGWBucketCtl::BucketInstance::GetParams()
				      .set_mtime(&mtime)
				      .set_attrs(&attrs),
				      &ep_ot);
  } else {
    ret  = store->ctl()->bucket->read_bucket_instance_info(info.bucket, &info, y, dpp,
				      RGWBucketCtl::BucketInstance::GetParams()
				      .set_mtime(&mtime)
				      .set_attrs(&attrs));
  }
  if (ret != 0) {
    return ret;
  }

  bucket_version = ep_ot.read_version;

  return ret;
}

int RadosBucket::read_stats(const DoutPrefixProvider *dpp,
			    const bucket_index_layout_generation& idx_layout,
			    int shard_id, std::string* bucket_ver, std::string* master_ver,
			    std::map<RGWObjCategory, RGWStorageStats>& stats,
			    std::string* max_marker, bool* syncstopped)
{
  return store->getRados()->get_bucket_stats(dpp, info, idx_layout, shard_id, bucket_ver, master_ver, stats, max_marker, syncstopped);
}

int RadosBucket::read_stats_async(const DoutPrefixProvider *dpp,
				  const bucket_index_layout_generation& idx_layout,
				  int shard_id, boost::intrusive_ptr<ReadStatsCB> ctx)
{
  return store->getRados()->get_bucket_stats_async(dpp, get_info(), idx_layout, shard_id, ctx);
}

int RadosBucket::sync_owner_stats(const DoutPrefixProvider *dpp, optional_yield y,
                                  RGWBucketEnt* ent)
{
  librados::Rados& rados = *store->getRados()->get_rados_handle();
  return store->ctl()->bucket->sync_owner_stats(dpp, rados, info.owner, info, y, ent);
}

int RadosBucket::check_bucket_shards(const DoutPrefixProvider* dpp,
                                     uint64_t num_objs, optional_yield y)
{
  return store->getRados()->check_bucket_shards(info, num_objs, dpp, y);
}

int RadosBucket::link(const DoutPrefixProvider* dpp, const rgw_owner& new_owner,
                      optional_yield y, bool update_entrypoint, RGWObjVersionTracker* objv)
{
  RGWBucketEntryPoint ep;
  ep.bucket = info.bucket;
  ep.owner = new_owner;
  ep.creation_time = get_creation_time();
  ep.linked = true;
  Attrs ep_attrs;
  rgw_ep_info ep_data{ep, ep_attrs};

  librados::Rados& rados = *store->getRados()->get_rados_handle();
  int r = store->ctl()->bucket->link_bucket(rados, new_owner, info.bucket,
					    get_creation_time(), y, dpp, update_entrypoint,
					    &ep_data);
  if (r < 0)
    return r;

  if (objv)
    *objv = ep_data.ep_objv;

  return r;
}

int RadosBucket::unlink(const DoutPrefixProvider* dpp, const rgw_owner& owner, optional_yield y, bool update_entrypoint)
{
  librados::Rados& rados = *store->getRados()->get_rados_handle();
  return store->ctl()->bucket->unlink_bucket(rados, owner, info.bucket,
                                             y, dpp, update_entrypoint);
}

int RadosBucket::chown(const DoutPrefixProvider* dpp, const rgw_owner& new_owner, optional_yield y)
{
  // unlink from the owner, but don't update the entrypoint until link()
  int r = this->unlink(dpp, info.owner, y, false);
  if (r < 0) {
    return r;
  }

  r = this->link(dpp, new_owner, y);
  if (r < 0) {
    return r;
  }

  // write updated owner to bucket instance metadata
  info.owner = new_owner;

  // update ACLOwner
  if (auto i = attrs.find(RGW_ATTR_ACL); i != attrs.end()) {
    try {
      auto p = i->second.cbegin();

      RGWAccessControlPolicy acl;
      decode(acl, p);

      acl.get_owner().id = new_owner;

      bufferlist bl;
      encode(acl, bl);

      i->second = std::move(bl);
    } catch (const buffer::error&) {
      // not fatal
    }
  }

  constexpr bool exclusive = false;
  return put_info(dpp, exclusive, ceph::real_clock::now(), y);
}

int RadosBucket::put_info(const DoutPrefixProvider* dpp, bool exclusive, ceph::real_time _mtime, optional_yield y)
{
  mtime = _mtime;
  return store->getRados()->put_bucket_instance_info(info, exclusive, mtime, &attrs, dpp, y);
}

int RadosBucket::check_empty(const DoutPrefixProvider* dpp, optional_yield y)
{
  return store->getRados()->check_bucket_empty(dpp, info, y);
}

int RadosBucket::check_quota(const DoutPrefixProvider *dpp, RGWQuota& quota, uint64_t obj_size,
				optional_yield y, bool check_size_only)
{
    return store->getRados()->check_quota(dpp, info.owner, get_key(),
					  quota, obj_size, y, check_size_only);
}

int RadosBucket::merge_and_store_attrs(const DoutPrefixProvider* dpp, Attrs& new_attrs, optional_yield y)
{
  for(auto& it : new_attrs) {
	  attrs[it.first] = it.second;
  }
  return store->ctl()->bucket->set_bucket_instance_attrs(get_info(),
				attrs, &get_info().objv_tracker, y, dpp);
}

int RadosBucket::try_refresh_info(const DoutPrefixProvider* dpp, ceph::real_time* pmtime, optional_yield y)
{
  return store->getRados()->try_refresh_bucket_info(info, pmtime, dpp, y, &attrs);
}

int RadosBucket::read_usage(const DoutPrefixProvider *dpp, uint64_t start_epoch, uint64_t end_epoch,
			       uint32_t max_entries, bool* is_truncated,
			       RGWUsageIter& usage_iter,
			       map<rgw_user_bucket, rgw_usage_log_entry>& usage)
{
  const rgw_user* user = std::get_if<rgw_user>(&info.owner);
  if (!user) {
    return -ENOTSUP; // not supported for account owners
  }
  return store->getRados()->read_usage(dpp, *user, get_name(), start_epoch,
				       end_epoch, max_entries, is_truncated,
				       usage_iter, usage);
}

int RadosBucket::trim_usage(const DoutPrefixProvider *dpp, uint64_t start_epoch, uint64_t end_epoch, optional_yield y)
{
  const rgw_user* user = std::get_if<rgw_user>(&info.owner);
  if (!user) {
    return -ENOTSUP; // not supported for account owners
  }
  return store->getRados()->trim_usage(dpp, *user, get_name(), start_epoch, end_epoch, y);
}

int RadosBucket::remove_objs_from_index(const DoutPrefixProvider *dpp, std::list<rgw_obj_index_key>& objs_to_unlink)
{
  return store->getRados()->remove_objs_from_index(dpp, info, objs_to_unlink);
}

int RadosBucket::check_index(const DoutPrefixProvider *dpp, std::map<RGWObjCategory, RGWStorageStats>& existing_stats, std::map<RGWObjCategory, RGWStorageStats>& calculated_stats)
{
  return store->getRados()->bucket_check_index(dpp, info, &existing_stats, &calculated_stats);
}

int RadosBucket::rebuild_index(const DoutPrefixProvider *dpp)
{
  return store->getRados()->bucket_rebuild_index(dpp, info);
}

int RadosBucket::set_tag_timeout(const DoutPrefixProvider *dpp, uint64_t timeout)
{
  return store->getRados()->cls_obj_set_bucket_tag_timeout(dpp, info, timeout);
}

int RadosBucket::purge_instance(const DoutPrefixProvider* dpp, optional_yield y)
{
  int max_shards = (info.layout.current_index.layout.normal.num_shards > 0 ? info.layout.current_index.layout.normal.num_shards : 1);
  for (int i = 0; i < max_shards; i++) {
    RGWRados::BucketShard bs(store->getRados());
    int shard_id = (info.layout.current_index.layout.normal.num_shards > 0  ? i : -1);
    int ret = bs.init(dpp, info, info.layout.current_index, shard_id, y);
    if (ret < 0) {
      cerr << "ERROR: bs.init(bucket=" << info.bucket << ", shard=" << shard_id
           << "): " << cpp_strerror(-ret) << std::endl;
      return ret;
    }
    ret = store->getRados()->bi_remove(dpp, bs);
    if (ret < 0) {
      cerr << "ERROR: failed to remove bucket index object: "
           << cpp_strerror(-ret) << std::endl;
      return ret;
    }
  }
  return 0;
}

int RadosBucket::set_acl(const DoutPrefixProvider* dpp, RGWAccessControlPolicy &acl, optional_yield y)
{
  bufferlist aclbl;

  acls = acl;
  acl.encode(aclbl);
  map<string, bufferlist>& attrs = get_attrs();

  attrs[RGW_ATTR_ACL] = aclbl;
  info.owner = acl.get_owner().id;

  int r = store->ctl()->bucket->store_bucket_instance_info(info.bucket,
                 info, y, dpp,
                 RGWBucketCtl::BucketInstance::PutParams().set_attrs(&attrs));
  if (r < 0) {
    cerr << "ERROR: failed to set bucket owner: " << cpp_strerror(-r) << std::endl;
    return r;
  }

  return 0;
}

std::unique_ptr<Object> RadosBucket::get_object(const rgw_obj_key& k)
{
  return std::make_unique<RadosObject>(this->store, k, this);
}

int RadosBucket::list(const DoutPrefixProvider* dpp, ListParams& params, int max, ListResults& results, optional_yield y)
{
  RGWRados::Bucket target(store->getRados(), get_info());
  if (params.shard_id >= 0) {
    target.set_shard_id(params.shard_id);
  }
  RGWRados::Bucket::List list_op(&target);

  list_op.params.prefix = params.prefix;
  list_op.params.delim = params.delim;
  list_op.params.marker = params.marker;
  list_op.params.ns = params.ns;
  list_op.params.end_marker = params.end_marker;
  list_op.params.ns = params.ns;
  list_op.params.enforce_ns = params.enforce_ns;
  list_op.params.access_list_filter = params.access_list_filter;
  list_op.params.force_check_filter = params.force_check_filter;
  list_op.params.list_versions = params.list_versions;
  list_op.params.allow_unordered = params.allow_unordered;

  int ret = list_op.list_objects(dpp, max, &results.objs, &results.common_prefixes, &results.is_truncated, y);
  if (ret >= 0) {
    results.next_marker = list_op.get_next_marker();
    params.marker = results.next_marker;
  }

  return ret;
}

std::unique_ptr<MultipartUpload> RadosBucket::get_multipart_upload(
				  const std::string& oid,
				  std::optional<std::string> upload_id,
				  ACLOwner owner, ceph::real_time mtime)
{
  return std::make_unique<RadosMultipartUpload>(this->store, this, oid, upload_id,
						std::move(owner), mtime);
}

int RadosBucket::list_multiparts(const DoutPrefixProvider *dpp,
				 const string& prefix,
				 string& marker,
				 const string& delim,
				 const int& max_uploads,
				 vector<std::unique_ptr<MultipartUpload>>& uploads,
				 map<string, bool> *common_prefixes,
				 bool *is_truncated, optional_yield y)
{
  rgw::sal::Bucket::ListParams params;
  rgw::sal::Bucket::ListResults results;

  params.prefix = prefix;
  params.delim = delim;
  params.marker = marker;
  params.ns = RGW_OBJ_NS_MULTIPART;
  params.access_list_filter = MultipartMetaFilter;

  int ret = list(dpp, params, max_uploads, results, y);

  if (ret < 0)
    return ret;

  if (!results.objs.empty()) {
    for (const rgw_bucket_dir_entry& dentry : results.objs) {
      rgw_obj_key key(dentry.key);
      const ACLOwner owner{
        .id = rgw_user(dentry.meta.owner),
        .display_name = dentry.meta.owner_display_name
      };
      uploads.push_back(this->get_multipart_upload(key.name,
			std::nullopt, std::move(owner), dentry.meta.mtime));
    }
  }
  if (common_prefixes) {
    *common_prefixes = std::move(results.common_prefixes);
  }
  *is_truncated = results.is_truncated;
  marker = params.marker.name;

  return 0;
}

int RadosBucket::abort_multiparts(const DoutPrefixProvider* dpp,
				  CephContext* cct, optional_yield y)
{
  constexpr int max = 1000;
  int ret, num_deleted = 0;
  vector<std::unique_ptr<MultipartUpload>> uploads;
  string marker;
  bool is_truncated;

  const std::string empty_delim;
  const std::string empty_prefix;

  do {
    ret = list_multiparts(dpp, empty_prefix, marker, empty_delim,
			  max, uploads, nullptr, &is_truncated, y);
    if (ret < 0) {
      ldpp_dout(dpp, 0) << __func__ <<
	" ERROR : calling list_bucket_multiparts; ret=" << ret <<
	"; bucket=\"" << this << "\"" << dendl;
      return ret;
    }
    ldpp_dout(dpp, 20) << __func__ <<
      " INFO: aborting and cleaning up multipart upload(s); bucket=\"" <<
      this << "\"; uploads.size()=" << uploads.size() <<
      "; is_truncated=" << is_truncated << dendl;

    if (!uploads.empty()) {
      for (const auto& upload : uploads) {
	ret = upload->abort(dpp, cct, y);
        if (ret < 0) {
	  // we're doing a best-effort; if something cannot be found,
	  // log it and keep moving forward
	  if (ret != -ENOENT && ret != -ERR_NO_SUCH_UPLOAD) {
	    ldpp_dout(dpp, 0) << __func__ <<
	      " ERROR : failed to abort and clean-up multipart upload \"" <<
	      upload->get_meta() << "\"" << dendl;
	    return ret;
	  } else {
	    ldpp_dout(dpp, 10) << __func__ <<
	      " NOTE : unable to find part(s) of "
	      "aborted multipart upload of \"" << upload->get_meta() <<
	      "\" for cleaning up" << dendl;
	  }
        }
        num_deleted++;
      }
      if (num_deleted) {
        ldpp_dout(dpp, 0) << __func__ <<
	  " WARNING : aborted " << num_deleted <<
	  " incomplete multipart uploads" << dendl;
      }
    }
  } while (is_truncated);

  return 0;
}

std::string RadosBucket::topics_oid() const {
  return pubsub_oid_prefix + get_tenant() + pubsub_bucket_oid_infix + get_name() + "/" + get_marker();
}

int RadosBucket::read_topics(rgw_pubsub_bucket_topics& notifications,
    RGWObjVersionTracker* objv_tracker, optional_yield y, const DoutPrefixProvider *dpp)
{
  // read from cache
  auto cache = store->getRados()->get_topic_cache();
  const std::string key = store->svc()->zone->get_zone_params().log_pool.to_str() + topics_oid();
  if (auto e = cache->find(key)) {
    notifications = e->info;
    return 0;
  }

  bufferlist bl;
  rgw_cache_entry_info cache_info;
  const int ret = rgw_get_system_obj(store->svc()->sysobj,
                               store->svc()->zone->get_zone_params().log_pool,
                               topics_oid(),
                               bl,
                               objv_tracker, nullptr,
                               y, dpp, nullptr, &cache_info);
  if (ret < 0) {
    return ret;
  }

  auto iter = bl.cbegin();
  try {
    decode(notifications, iter);
  } catch (buffer::error& err) {
    ldpp_dout(dpp, 20) << " failed to decode bucket notifications from oid: " << topics_oid() << ". for bucket: "
      << get_name() << ". error: " << err.what() << dendl;
    return -EIO;
  }

  pubsub_bucket_topics_entry e;
  e.info = notifications;
  if (!cache->put(dpp, store->getRados()->svc.cache, key, &e, { &cache_info })) {
    ldpp_dout(dpp, 10) << "couldn't put bucket topics cache entry" << dendl;
  }
  return 0;
}

int RadosBucket::write_topics(const rgw_pubsub_bucket_topics& notifications,
    RGWObjVersionTracker* objv_tracker, optional_yield y, const DoutPrefixProvider *dpp) {
  bufferlist bl;
  encode(notifications, bl);

  return rgw_put_system_obj(dpp, store->svc()->sysobj,
      store->svc()->zone->get_zone_params().log_pool,
      topics_oid(),
      bl, false, objv_tracker, real_time(), y);
}

int RadosBucket::remove_topics(RGWObjVersionTracker* objv_tracker,
    optional_yield y, const DoutPrefixProvider *dpp) {
  return rgw_delete_system_obj(dpp, store->svc()->sysobj,
      store->svc()->zone->get_zone_params().log_pool,
      topics_oid(),
      objv_tracker, y);
}

int RadosBucket::get_logging_object_name(std::string& obj_name,
    const std::string& prefix,
    optional_yield y,
    const DoutPrefixProvider *dpp,
    RGWObjVersionTracker* objv_tracker) {
  rgw_pool data_pool;
  const auto obj_name_oid = bucketlogging::object_name_oid(this, prefix);
  if (!store->getRados()->get_obj_data_pool(get_placement_rule(), rgw_obj{get_key(), obj_name_oid}, &data_pool)) {
    ldpp_dout(dpp, 1) << "ERROR: failed to get data pool for bucket '" << get_name() <<
      "' when getting logging object name" << dendl;
    return -EIO;
  }
  bufferlist bl;
  const int ret = rgw_get_system_obj(store->svc()->sysobj,
                               data_pool,
                               obj_name_oid,
                               bl,
                               objv_tracker,
                               nullptr,
                               y,
                               dpp,
                               nullptr,
                               nullptr);
  if (ret < 0) {
    ldpp_dout(dpp, 1) << "ERROR: failed to get logging object name from '" << obj_name_oid << "'. ret = " << ret << dendl;
    return ret;
  }
  obj_name = bl.to_str();
  return 0;
}

int RadosBucket::set_logging_object_name(const std::string& obj_name,
    const std::string& prefix,
    optional_yield y,
    const DoutPrefixProvider *dpp,
    bool new_obj,
    RGWObjVersionTracker* objv_tracker) {
  rgw_pool data_pool;
  const auto obj_name_oid = bucketlogging::object_name_oid(this, prefix);
  if (!store->getRados()->get_obj_data_pool(get_placement_rule(), rgw_obj{get_key(), obj_name_oid}, &data_pool)) {
    ldpp_dout(dpp, 1) << "ERROR: failed to get data pool for bucket '" << get_name() <<
      "' when setting logging object name"  << dendl;
    return -EIO;
  }
  bufferlist bl;
  bl.append(obj_name);
  const int ret = rgw_put_system_obj(dpp, store->svc()->sysobj,
                               data_pool,
                               obj_name_oid,
                               bl,
                               new_obj,
                               objv_tracker,
                               ceph::real_time::clock::now(),
                               y,
                               nullptr);
  if (ret == -EEXIST) {
    ldpp_dout(dpp, 20) << "INFO: race detected in initializing '" << obj_name_oid << "' with logging object name:'" << obj_name  << "'. ret = " << ret << dendl;
  } else if (ret == -ECANCELED) {
    ldpp_dout(dpp, 20) << "INFO: race detected in updating logging object name '" << obj_name << "' at '" << obj_name_oid << "'. ret = " << ret << dendl;
  } else if (ret < 0) {
    ldpp_dout(dpp, 1) << "ERROR: failed to set logging object name '" << obj_name << "' at '" << obj_name_oid << "'. ret = " << ret << dendl;
  }
  return ret;
}

int RadosBucket::remove_logging_object_name(const std::string& prefix,
    optional_yield y,
    const DoutPrefixProvider *dpp,
    RGWObjVersionTracker* objv_tracker) {
  rgw_pool data_pool;
  const auto obj_name_oid = bucketlogging::object_name_oid(this, prefix);
  if (!store->getRados()->get_obj_data_pool(get_placement_rule(), rgw_obj{get_key(), obj_name_oid}, &data_pool)) {
    ldpp_dout(dpp, 1) << "ERROR: failed to get data pool for bucket '" << get_name() <<
      "' when setting logging object name"  << dendl;
    return -EIO;
  }
  return rgw_delete_system_obj(dpp, store->svc()->sysobj,
      data_pool,
      obj_name_oid,
      objv_tracker,
      y);
}

std::string to_temp_object_name(const rgw::sal::Bucket* bucket, const std::string& obj_name) {
  return fmt::format("{}__shadow_{}0",
      bucket->get_bucket_id(),
      obj_name);
}

int RadosBucket::remove_logging_object(const std::string& obj_name, optional_yield y, const DoutPrefixProvider *dpp) {
  rgw_pool data_pool;
  const rgw_obj head_obj{get_key(), obj_name};
  const auto placement_rule = get_placement_rule();

  if (!store->getRados()->get_obj_data_pool(placement_rule, head_obj, &data_pool)) {
    ldpp_dout(dpp, 1) << "ERROR: failed to get data pool for bucket '" << get_name() <<
      "' when deleting logging object"  << dendl;
    return -EIO;
  }

  const auto temp_obj_name = to_temp_object_name(this, obj_name);
  return rgw_delete_system_obj(dpp, store->svc()->sysobj,
      data_pool,
      temp_obj_name,
      nullptr,
      y);
}

int RadosBucket::commit_logging_object(const std::string& obj_name, optional_yield y, const DoutPrefixProvider *dpp) {
  rgw_pool data_pool;
  const rgw_obj head_obj{get_key(), obj_name};
  const auto placement_rule = get_placement_rule();

  if (!store->getRados()->get_obj_data_pool(placement_rule, head_obj, &data_pool)) {
    ldpp_dout(dpp, 1) << "ERROR: failed to get data pool for bucket '" << get_name() <<
      "' when comitting logging object"  << dendl;
    return -EIO;
  }

  const auto temp_obj_name = to_temp_object_name(this, obj_name);
  std::map<string, bufferlist> obj_attrs;
  ceph::real_time mtime;
  bufferlist bl_data;
  if (const auto ret = rgw_get_system_obj(store->svc()->sysobj,
                     data_pool,
                     temp_obj_name,
                     bl_data,
                     nullptr,
                     &mtime,
                     y,
                     dpp,
                     &obj_attrs,
                     nullptr); ret < 0 && ret != -ENOENT) {
    ldpp_dout(dpp, 1) << "ERROR: failed to read logging data when comitting object '" << temp_obj_name
      << ". error: " << ret << dendl;
    return ret;
  } else if (ret == -ENOENT) {
    ldpp_dout(dpp, 1) << "WARNING: temporary logging object '" << temp_obj_name << "' does not exists" << dendl;
    return 0;
  }

  uint64_t size = bl_data.length();
  const uint64_t max_obj_size = store->ctx()->_conf->osd_max_object_size;
  RGWObjManifest manifest;
  manifest.set_prefix(obj_name);
  manifest.set_trivial_rule(0, max_obj_size);
  RGWObjManifest::generator manifest_gen;
  if (const auto ret = manifest_gen.create_begin(store->ctx(), &manifest,
                                placement_rule,
                                nullptr, // no special placment for tail
                                get_key(),
                                head_obj); ret < 0) {
    ldpp_dout(dpp, 1) << "ERROR: failed to create manifest when comitting logging object. error: " <<
      ret << dendl;
    return ret;
  }

  if (const auto ret = manifest_gen.create_next(size); ret < 0) {
    ldpp_dout(dpp, 1) << "ERROR: failed to add object to manifest when comitting logging object. error: " <<
      ret << dendl;
    return ret;
  }

  if (const auto expected_temp_obj = manifest_gen.get_cur_obj(store->getRados());
      temp_obj_name != expected_temp_obj.oid) {
    // TODO: cleanup temporary object, commit would never succeed
    ldpp_dout(dpp, 1) << "ERROR: temporary logging object name mismatch: '" <<
      temp_obj_name << "' != '" << expected_temp_obj.oid << "'" << dendl;
    return -EINVAL;
  }

  RGWObjectCtx obj_ctx(store);
  obj_ctx.set_atomic(head_obj);
  const auto& bucket_info = get_info();
  RGWRados::Object rgw_head_obj(store->getRados(),
      bucket_info,
      obj_ctx,
      head_obj);
  // disable versioning on the logging objects
  rgw_head_obj.set_versioning_disabled(true);
  RGWRados::Object::Write head_obj_wop(&rgw_head_obj);
  head_obj_wop.meta.manifest = &manifest;
  head_obj_wop.meta.bucket_owner = bucket_info.owner;
  head_obj_wop.meta.flags = PUT_OBJ_CREATE;
  head_obj_wop.meta.mtime = &mtime;
  // TODO: head_obj_wop.meta.ptag
  // the owner of the logging object is the bucket owner
  // not the user that wrote the log that triggered the commit
  const ACLOwner owner{bucket_info.owner, ""}; // TODO: missing display name
  head_obj_wop.meta.owner = owner;
  const auto etag = TOPNSPC::crypto::digest<TOPNSPC::crypto::MD5>(bl_data).to_str();
  bufferlist bl_etag;
  bl_etag.append(etag.c_str());
  obj_attrs.emplace(RGW_ATTR_ETAG, std::move(bl_etag));
  const req_context rctx{dpp, y, nullptr};
  jspan_context trace{false, false};
  if (const auto ret = head_obj_wop.write_meta(0, size, obj_attrs, rctx, trace); ret < 0) {
  ldpp_dout(dpp, 1) << "ERROR: failed to commit logging object '" << temp_obj_name <<
    "' to bucket id '" << get_info().bucket <<"'. error: " << ret << dendl;
    return ret;
  }
  ldpp_dout(dpp, 20) << "INFO: committed logging object '" << temp_obj_name <<
    "' with size of " << size << " bytes, to bucket '" << get_key() << "' as '" <<
    obj_name << "'" << dendl;
  return 0;
}

struct BucketLoggingCompleteArg {
    BucketLoggingCompleteArg(const std::string& _obj_name, size_t _size, CephContext* _cct)
            : obj_name{_obj_name}, size{_size}, cct{_cct} {}
    const std::string obj_name;
    const size_t size;
    CephContext* cct;
};

void bucket_logging_completion(rados_completion_t completion, void* args) {
  auto* aio_comp = reinterpret_cast<librados::AioCompletionImpl*>(completion);
  std::unique_ptr<BucketLoggingCompleteArg> logging_args(reinterpret_cast<BucketLoggingCompleteArg*>(args));
  if (aio_comp->get_return_value() < 0) {
    ldout(logging_args->cct, 1) << "ERROR: failed to complete append to logging object '" << logging_args->obj_name <<
      "'. ret = " << aio_comp->get_return_value() << dendl;
  } else {
    ldout(logging_args->cct, 20) << "INFO: wrote " << logging_args->size << " bytes to logging object '" <<
      logging_args->obj_name << "'" << dendl;
  }
}

int RadosBucket::write_logging_object(const std::string& obj_name,
    const std::string& record,
    optional_yield y,
    const DoutPrefixProvider *dpp,
    bool async_completion) {
  const auto temp_obj_name = to_temp_object_name(this, obj_name);
  rgw_pool data_pool;
  rgw_obj obj{get_key(), obj_name};
  if (!store->getRados()->get_obj_data_pool(get_placement_rule(), obj, &data_pool)) {
    ldpp_dout(dpp, 1) << "ERROR: failed to get data pool for bucket '" << get_name() <<
      "' when writing logging object" << dendl;
    return -EIO;
  }
  librados::IoCtx io_ctx;
  if (const auto ret = rgw_init_ioctx(dpp, store->getRados()->get_rados_handle(), data_pool, io_ctx); ret < 0) {
    ldpp_dout(dpp, 1) << "ERROR: failed to get IO context for logging object from data pool:" << data_pool.to_str() << dendl;
    return -EIO;
  }
  bufferlist bl;
  bl.append(record);
  bl.append("\n");
  // append the record to the temporary object
  // if this is the first record, the object will be created
  librados::ObjectWriteOperation op;
  op.append(bl);
  if (async_completion) {
    aio_completion_ptr completion{librados::Rados::aio_create_completion()};
    auto arg = std::make_unique<BucketLoggingCompleteArg>(temp_obj_name, record.length(), store->ctx());
    completion->set_complete_callback(arg.get(), bucket_logging_completion);
    if (const auto ret = io_ctx.aio_operate(temp_obj_name, completion.get(), &op); ret < 0) {
      ldpp_dout(dpp, 1) << "ERROR: failed to append to logging object '" << temp_obj_name <<
        "'. ret = " << ret << dendl;
      return ret;
    }
    std::ignore = arg.release();
    std::ignore = completion.release();
    return 0;
  }
  if (const auto ret = rgw_rados_operate(dpp, io_ctx, temp_obj_name, &op, y); ret < 0) {
    ldpp_dout(dpp, 1) << "ERROR: failed to append to logging object '" << temp_obj_name <<
      "'. ret = " << ret << dendl;
    return ret;
  }
  ldpp_dout(dpp, 20) << "INFO: wrote " << record.length() << " bytes to logging object '" <<
    temp_obj_name << "'" << dendl;
  return 0;
}

std::unique_ptr<User> RadosStore::get_user(const rgw_user &u)
{
  return std::make_unique<RadosUser>(this, u);
}

std::string RadosStore::get_cluster_id(const DoutPrefixProvider* dpp,  optional_yield y)
{
  return getRados()->get_cluster_fsid(dpp, y);
}

int RadosStore::get_user_by_access_key(const DoutPrefixProvider* dpp, const std::string& key, optional_yield y, std::unique_ptr<User>* user)
{
  RGWUserInfo uinfo;
  User* u;
  RGWObjVersionTracker objv_tracker;
  Attrs attrs;

  int r = ctl()->user->get_info_by_access_key(
      dpp, key, &uinfo, y,
      RGWUserCtl::GetParams().set_objv_tracker(&objv_tracker)
                             .set_attrs(&attrs));
  if (r < 0)
    return r;

  u = new RadosUser(this, uinfo);
  if (!u)
    return -ENOMEM;

  u->get_version_tracker() = objv_tracker;
  u->get_attrs() = std::move(attrs);

  user->reset(u);
  return 0;
}

int RadosStore::get_user_by_email(const DoutPrefixProvider* dpp, const std::string& email, optional_yield y, std::unique_ptr<User>* user)
{
  RGWUserInfo uinfo;
  User* u;
  RGWObjVersionTracker objv_tracker;
  Attrs attrs;

  int r = ctl()->user->get_info_by_email(
      dpp, email, &uinfo, y,
      RGWUserCtl::GetParams().set_objv_tracker(&objv_tracker)
                             .set_attrs(&attrs));
  if (r < 0)
    return r;

  u = new RadosUser(this, uinfo);
  if (!u)
    return -ENOMEM;

  u->get_version_tracker() = objv_tracker;
  u->get_attrs() = std::move(attrs);

  user->reset(u);
  return 0;
}

int RadosStore::get_user_by_swift(const DoutPrefixProvider* dpp, const std::string& user_str, optional_yield y, std::unique_ptr<User>* user)
{
  RGWUserInfo uinfo;
  User* u;
  RGWObjVersionTracker objv_tracker;
  Attrs attrs;

  int r = ctl()->user->get_info_by_swift(
      dpp, user_str, &uinfo, y,
      RGWUserCtl::GetParams().set_objv_tracker(&objv_tracker)
                             .set_attrs(&attrs));
  if (r < 0)
    return r;

  u = new RadosUser(this, uinfo);
  if (!u)
    return -ENOMEM;

  u->get_version_tracker() = objv_tracker;
  u->get_attrs() = std::move(attrs);

  user->reset(u);
  return 0;
}

int RadosStore::load_account_by_id(const DoutPrefixProvider* dpp,
                                   optional_yield y,
                                   std::string_view id,
                                   RGWAccountInfo& info,
                                   Attrs& attrs,
                                   RGWObjVersionTracker& objv)
{
  ceph::real_time mtime; // ignored
  return rgwrados::account::read(
      dpp, y, *svc()->sysobj,
      svc()->zone->get_zone_params(),
      id, info, attrs, mtime, objv);
}

int RadosStore::load_account_by_name(const DoutPrefixProvider* dpp,
                                     optional_yield y,
                                     std::string_view tenant,
                                     std::string_view name,
                                     RGWAccountInfo& info,
                                     Attrs& attrs,
                                     RGWObjVersionTracker& objv)
{
  return rgwrados::account::read_by_name(
      dpp, y, *svc()->sysobj,
      svc()->zone->get_zone_params(),
      tenant, name, info, attrs, objv);
}

int RadosStore::load_account_by_email(const DoutPrefixProvider* dpp,
                                      optional_yield y,
                                      std::string_view email,
                                      RGWAccountInfo& info,
                                      Attrs& attrs,
                                      RGWObjVersionTracker& objv)
{
  return rgwrados::account::read_by_email(
      dpp, y, *svc()->sysobj,
      svc()->zone->get_zone_params(),
      email, info, attrs, objv);
}

static int write_mdlog_entry(const DoutPrefixProvider* dpp, optional_yield y,
                             RGWSI_MDLog& mdlog_svc,
                             const std::string& section,
                             const std::string& key,
                             const RGWObjVersionTracker& objv)
{
  RGWMetadataLogData entry;
  entry.read_version = objv.read_version;
  entry.write_version = objv.write_version;
  entry.status = MDLOG_STATUS_COMPLETE;

  bufferlist bl;
  encode(entry, bl);

  const std::string hash_key = fmt::format("{}:{}", section, key);
  return mdlog_svc.add_entry(dpp, hash_key, section, key, bl, y);
}

int RadosStore::store_account(const DoutPrefixProvider* dpp,
                              optional_yield y, bool exclusive,
                              const RGWAccountInfo& info,
                              const RGWAccountInfo* old_info,
                              const Attrs& attrs,
                              RGWObjVersionTracker& objv)
{
  ceph::real_time mtime = ceph::real_clock::now();
  int r = rgwrados::account::write(
      dpp, y, *svc()->sysobj, svc()->zone->get_zone_params(),
      info, old_info, attrs, mtime, exclusive, objv);
  if (r < 0) {
    return r;
  }

  return write_mdlog_entry(dpp, y, *svc()->mdlog, "account", info.id, objv);
}

int RadosStore::delete_account(const DoutPrefixProvider* dpp,
                               optional_yield y,
                               const RGWAccountInfo& info,
                               RGWObjVersionTracker& objv)
{
  int r = rgwrados::account::remove(
      dpp, y, *svc()->sysobj,
      svc()->zone->get_zone_params(),
      info, objv);
  if (r < 0) {
    return r;
  }

  return write_mdlog_entry(dpp, y, *svc()->mdlog, "account", info.id, objv);
}

int RadosStore::load_stats(const DoutPrefixProvider* dpp,
                           optional_yield y,
                           const rgw_owner& owner,
                           RGWStorageStats& stats,
                           ceph::real_time& last_synced,
                           ceph::real_time& last_updated)
{
  librados::Rados& rados = *getRados()->get_rados_handle();
  const rgw_raw_obj& obj = get_owner_buckets_obj(svc()->user, svc()->zone, owner);
  return rgwrados::buckets::read_stats(dpp, y, rados, obj, stats,
                                       &last_synced, &last_updated);
}

int RadosStore::load_stats_async(const DoutPrefixProvider* dpp,
                                 const rgw_owner& owner,
                                 boost::intrusive_ptr<ReadStatsCB> cb)
{
  librados::Rados& rados = *getRados()->get_rados_handle();
  const rgw_raw_obj& obj = get_owner_buckets_obj(svc()->user, svc()->zone, owner);
  return rgwrados::buckets::read_stats_async(dpp, rados, obj, std::move(cb));
}

int RadosStore::reset_stats(const DoutPrefixProvider *dpp,
                            optional_yield y,
                            const rgw_owner& owner)
{
  librados::Rados& rados = *getRados()->get_rados_handle();
  const rgw_raw_obj& obj = get_owner_buckets_obj(svc()->user, svc()->zone, owner);
  return rgwrados::buckets::reset_stats(dpp, y, rados, obj);
}

int RadosStore::complete_flush_stats(const DoutPrefixProvider* dpp,
                                     optional_yield y,
                                     const rgw_owner& owner)
{
  librados::Rados& rados = *getRados()->get_rados_handle();
  const rgw_raw_obj& obj = get_owner_buckets_obj(svc()->user, svc()->zone, owner);
  return rgwrados::buckets::complete_flush_stats(dpp, y, rados, obj);
}

int RadosStore::load_owner_by_email(const DoutPrefixProvider* dpp,
                                    optional_yield y,
                                    std::string_view email,
                                    rgw_owner& owner)
{
  // the email index stores ids which can either be a user or account
  RGWUID uid;
  int r = svc()->user->read_email_index(dpp, y, email, uid);
  if (r < 0) {
    return r;
  }
  owner = parse_owner(uid.id);
  return 0;
}

int RadosStore::count_account_roles(const DoutPrefixProvider* dpp,
                                    optional_yield y,
                                    std::string_view account_id,
                                    uint32_t& count)
{
  librados::Rados& rados = *getRados()->get_rados_handle();
  const RGWZoneParams& zone = svc()->zone->get_zone_params();
  const rgw_raw_obj& obj = rgwrados::account::get_roles_obj(zone, account_id);
  return rgwrados::account::resource_count(dpp, y, rados, obj, count);
}

int RadosStore::list_account_roles(const DoutPrefixProvider* dpp,
                                   optional_yield y,
                                   std::string_view account_id,
                                   std::string_view path_prefix,
                                   std::string_view marker,
                                   uint32_t max_items,
                                   RoleList& listing)
{
  // fetch the list of role ids from cls_role
  librados::Rados& rados = *getRados()->get_rados_handle();
  const RGWZoneParams& zone = svc()->zone->get_zone_params();
  const rgw_raw_obj& obj = rgwrados::account::get_roles_obj(zone, account_id);
  std::vector<std::string> ids;
  int r = rgwrados::roles::list(dpp, y, rados, obj, marker, path_prefix,
                                max_items, ids, listing.next_marker);
  if (r < 0) {
    return r;
  }

  // load the role metadata for each
  for (const auto& id : ids) {
    RGWRoleInfo info;
    r = rgwrados::role::read_by_id(dpp, y, *svc()->sysobj, zone, id,
                                   info, nullptr, nullptr, nullptr);
    if (r == -ENOENT) {
      continue;
    }
    if (r < 0) {
      return r;
    }
    listing.roles.push_back(std::move(info));
  }

  return 0;
}

int RadosStore::load_account_user_by_name(const DoutPrefixProvider* dpp,
                                          optional_yield y,
                                          std::string_view account_id,
                                          std::string_view tenant,
                                          std::string_view username,
                                          std::unique_ptr<User>* user)
{
  rgw_user uid;
  uid.tenant = tenant;

  librados::Rados& rados = *getRados()->get_rados_handle();
  const RGWZoneParams& zone = svc()->zone->get_zone_params();
  const rgw_raw_obj& obj = rgwrados::account::get_users_obj(zone, account_id);
  int r = rgwrados::users::get(dpp, y, rados, obj, username, uid.id);
  if (r < 0) {
    ldpp_dout(dpp, 20) << "failed to find account username " << username
        << ": " << cpp_strerror(r) << dendl;
    return r;
  }

  std::unique_ptr<User> u = get_user(uid);
  r = u->load_user(dpp, y);
  if (r < 0) {
    ldpp_dout(dpp, 20) << "failed to load account user " << uid
        << ": " << cpp_strerror(r) << dendl;
    return r;
  }
  *user = std::move(u);
  return 0;
}

int RadosStore::count_account_users(const DoutPrefixProvider* dpp,
                                    optional_yield y,
                                    std::string_view account_id,
                                    uint32_t& count)
{
  librados::Rados& rados = *getRados()->get_rados_handle();
  const RGWZoneParams& zone = svc()->zone->get_zone_params();
  const rgw_raw_obj& obj = rgwrados::account::get_users_obj(zone, account_id);
  return rgwrados::account::resource_count(dpp, y, rados, obj, count);
}

int RadosStore::list_account_users(const DoutPrefixProvider* dpp,
                                   optional_yield y,
                                   std::string_view account_id,
                                   std::string_view tenant,
                                   std::string_view path_prefix,
                                   std::string_view marker,
                                   uint32_t max_items,
                                   UserList& listing)
{
  // fetch the list of user ids from cls_user
  librados::Rados& rados = *getRados()->get_rados_handle();
  const RGWZoneParams& zone = svc()->zone->get_zone_params();
  const rgw_raw_obj& obj = rgwrados::account::get_users_obj(zone, account_id);
  std::vector<std::string> ids;
  int r = rgwrados::users::list(dpp, y, rados, obj, marker, path_prefix,
                                max_items, ids, listing.next_marker);
  if (r < 0) {
    return r;
  }

  // load the user metadata for each
  for (auto& id : ids) {
    rgw_user uid;
    uid.tenant = tenant;
    uid.id = std::move(id);

    RGWUserInfo info;
    r = ctl()->user->get_info_by_uid(dpp, uid, &info, y);
    if (r == -ENOENT) {
      continue;
    }
    if (r < 0) {
      return r;
    }
    listing.users.push_back(std::move(info));
  }

  return 0;
}

int RadosStore::load_group_by_id(const DoutPrefixProvider* dpp,
                                 optional_yield y,
                                 std::string_view id,
                                 RGWGroupInfo& info, Attrs& attrs,
                                 RGWObjVersionTracker& objv)
{
  const RGWZoneParams& zone = svc()->zone->get_zone_params();
  ceph::real_time mtime_ignored;
  return rgwrados::group::read(dpp, y, *svc()->sysobj, zone, id,
                               info, attrs, mtime_ignored, objv);
}

int RadosStore::load_group_by_name(const DoutPrefixProvider* dpp,
                                   optional_yield y,
                                   std::string_view account_id,
                                   std::string_view name,
                                   RGWGroupInfo& info, Attrs& attrs,
                                   RGWObjVersionTracker& objv)
{
  const RGWZoneParams& zone = svc()->zone->get_zone_params();
  return rgwrados::group::read_by_name(dpp, y, *svc()->sysobj, zone, account_id,
                                       name, info, attrs, objv);
}

int RadosStore::store_group(const DoutPrefixProvider* dpp, optional_yield y,
                            const RGWGroupInfo& info, const Attrs& attrs,
                            RGWObjVersionTracker& objv, bool exclusive,
                            const RGWGroupInfo* old_info)
{
  librados::Rados& rados = *getRados()->get_rados_handle();
  const RGWZoneParams& zone = svc()->zone->get_zone_params();
  ceph::real_time mtime = ceph::real_clock::now();
  int r = rgwrados::group::write(dpp, y, *svc()->sysobj, rados, zone, info,
                                 old_info, attrs, mtime, exclusive, objv);
  if (r < 0) {
    return r;
  }

  return write_mdlog_entry(dpp, y, *svc()->mdlog, "group", info.id, objv);
}

int RadosStore::remove_group(const DoutPrefixProvider* dpp, optional_yield y,
                             const RGWGroupInfo& info,
                             RGWObjVersionTracker& objv)
{
  librados::Rados& rados = *getRados()->get_rados_handle();
  const RGWZoneParams& zone = svc()->zone->get_zone_params();
  int r = rgwrados::group::remove(dpp, y, *svc()->sysobj, rados, zone, info, objv);
  if (r < 0) {
    return r;
  }

  return write_mdlog_entry(dpp, y, *svc()->mdlog, "group", info.id, objv);
}

int RadosStore::list_group_users(const DoutPrefixProvider* dpp,
                                 optional_yield y,
                                 std::string_view tenant,
                                 std::string_view id,
                                 std::string_view marker,
                                 uint32_t max_items,
                                 UserList& listing)
{
  // fetch the list of user ids from cls_user
  librados::Rados& rados = *getRados()->get_rados_handle();
  const RGWZoneParams& zone = svc()->zone->get_zone_params();
  const rgw_raw_obj& obj = rgwrados::group::get_users_obj(zone, id);
  const std::string path_prefix; // empty
  std::vector<std::string> ids;
  int r = rgwrados::users::list(dpp, y, rados, obj, marker, path_prefix,
                                max_items, ids, listing.next_marker);
  if (r < 0) {
    return r;
  }

  // load the user metadata for each
  for (auto& id : ids) {
    rgw_user uid;
    uid.tenant = tenant;
    uid.id = std::move(id);

    RGWUserInfo info;
    r = ctl()->user->get_info_by_uid(dpp, uid, &info, y);
    if (r == -ENOENT) {
      continue;
    }
    if (r < 0) {
      return r;
    }
    listing.users.push_back(std::move(info));
  }

  return 0;
}

int RadosStore::count_account_groups(const DoutPrefixProvider* dpp,
                                     optional_yield y,
                                     std::string_view account_id,
                                     uint32_t& count)
{
  librados::Rados& rados = *getRados()->get_rados_handle();
  const RGWZoneParams& zone = svc()->zone->get_zone_params();
  const rgw_raw_obj& obj = rgwrados::account::get_groups_obj(zone, account_id);
  return rgwrados::account::resource_count(dpp, y, rados, obj, count);
}

int RadosStore::list_account_groups(const DoutPrefixProvider* dpp,
                                    optional_yield y,
                                    std::string_view account_id,
                                    std::string_view path_prefix,
                                    std::string_view marker,
                                    uint32_t max_items,
                                    GroupList& listing)
{
  // fetch the list of group ids from cls_user
  librados::Rados& rados = *getRados()->get_rados_handle();
  const RGWZoneParams& zone = svc()->zone->get_zone_params();
  const rgw_raw_obj& obj = rgwrados::account::get_groups_obj(zone, account_id);
  std::vector<std::string> ids;
  int r = rgwrados::groups::list(dpp, y, rados, obj, marker, path_prefix,
                                 max_items, ids, listing.next_marker);
  if (r < 0) {
    return r;
  }

  // load the group metadata for each
  for (auto& id : ids) {
    RGWGroupInfo info;
    Attrs attrs;
    ceph::real_time mtime_ignored;
    RGWObjVersionTracker objv;
    r = rgwrados::group::read(dpp, y, *svc()->sysobj, zone, id,
                              info, attrs, mtime_ignored, objv);
    if (r == -ENOENT) {
      continue;
    }
    if (r < 0) {
      return r;
    }
    listing.groups.push_back(std::move(info));
  }

  return 0;
}

std::unique_ptr<Object> RadosStore::get_object(const rgw_obj_key& k)
{
  return std::make_unique<RadosObject>(this, k);
}

std::unique_ptr<Bucket> RadosStore::get_bucket(const RGWBucketInfo& i)
{
  /* Don't need to fetch the bucket info, use the provided one */
  return std::make_unique<RadosBucket>(this, i);
}

int RadosStore::load_bucket(const DoutPrefixProvider* dpp, const rgw_bucket& b,
                            std::unique_ptr<Bucket>* bucket, optional_yield y)
{
  *bucket = std::make_unique<RadosBucket>(this, b);
  return (*bucket)->load_bucket(dpp, y);
}

bool RadosStore::is_meta_master()
{
  return svc()->zone->is_meta_master();
}

std::string RadosStore::zone_unique_id(uint64_t unique_num)
{
  return svc()->zone_utils->unique_id(unique_num);
}

std::string RadosStore::zone_unique_trans_id(const uint64_t unique_num)
{
  return svc()->zone_utils->unique_trans_id(unique_num);
}

int RadosStore::get_zonegroup(const std::string& id,
			      std::unique_ptr<ZoneGroup>* zonegroup)
{
  ZoneGroup* zg;
  RGWZoneGroup rzg;
  int r = svc()->zone->get_zonegroup(id, rzg);
  if (r < 0)
    return r;

  zg = new RadosZoneGroup(this, rzg);
  if (!zg)
    return -ENOMEM;

  zonegroup->reset(zg);
  return 0;
}

int RadosStore::list_all_zones(const DoutPrefixProvider* dpp, std::list<std::string>& zone_ids)
{
  return svc()->zone->list_zones(dpp, zone_ids);
}

int RadosStore::cluster_stat(RGWClusterStat& stats)
{
  rados_cluster_stat_t rados_stats;
  int ret;

  ret = rados->get_rados_handle()->cluster_stat(rados_stats);
  if (ret < 0)
    return ret;

  stats.kb = rados_stats.kb;
  stats.kb_used = rados_stats.kb_used;
  stats.kb_avail = rados_stats.kb_avail;
  stats.num_objects = rados_stats.num_objects;

  return ret;
}

std::unique_ptr<Lifecycle> RadosStore::get_lifecycle(void)
{
  return std::make_unique<RadosLifecycle>(this);
}

std::unique_ptr<Notification> RadosStore::get_notification(
  rgw::sal::Object* obj, rgw::sal::Object* src_obj, req_state* s, rgw::notify::EventType event_type, optional_yield y, const std::string* object_name)
{
  return std::make_unique<RadosNotification>(s, this, obj, src_obj, s, event_type, y, object_name);
}

std::unique_ptr<Notification> RadosStore::get_notification(
    const DoutPrefixProvider* dpp,
    rgw::sal::Object* obj,
    rgw::sal::Object* src_obj,
    const rgw::notify::EventTypeList& event_types,
    rgw::sal::Bucket* _bucket,
    std::string& _user_id,
    std::string& _user_tenant,
    std::string& _req_id,
    optional_yield y) {
  return std::make_unique<RadosNotification>(dpp, this, obj, src_obj,
                                             event_types, _bucket, _user_id,
                                             _user_tenant, _req_id, y);
}

std::string RadosStore::topics_oid(const std::string& tenant) const {
  return pubsub_oid_prefix + tenant;
}

int RadosStore::read_topics(const std::string& tenant, rgw_pubsub_topics& topics, RGWObjVersionTracker* objv_tracker,
        optional_yield y, const DoutPrefixProvider *dpp) {
  bufferlist bl;
  const int ret = rgw_get_system_obj(svc()->sysobj,
                               svc()->zone->get_zone_params().log_pool,
                               topics_oid(tenant),
                               bl,
                               objv_tracker,
                               nullptr, y, dpp, nullptr);
  if (ret < 0) {
    return ret;
  }

  auto iter = bl.cbegin();
  try {
    decode(topics, iter);
  } catch (buffer::error& err) {
    ldpp_dout(dpp, 20) << " failed to decode topics from oid: " << topics_oid(tenant) <<
      ". error: " << err.what() << dendl;
    return -EIO;
  }

  return 0;
}

int RadosStore::stat_topics_v1(const std::string& tenant, optional_yield y, const DoutPrefixProvider *dpp) {
  return rgw_stat_system_obj(dpp, svc()->sysobj, svc()->zone->get_zone_params().log_pool, topics_oid(tenant), nullptr, nullptr, nullptr, y, nullptr);
}

int RadosStore::write_topics(const std::string& tenant, const rgw_pubsub_topics& topics, RGWObjVersionTracker* objv_tracker,
	optional_yield y, const DoutPrefixProvider *dpp) {
  bufferlist bl;
  encode(topics, bl);

  return rgw_put_system_obj(dpp, svc()->sysobj,
      svc()->zone->get_zone_params().log_pool,
      topics_oid(tenant),
      bl, false, objv_tracker, real_time(), y);
}

int RadosStore::remove_topics(const std::string& tenant, RGWObjVersionTracker* objv_tracker,
        optional_yield y, const DoutPrefixProvider *dpp) {
  return rgw_delete_system_obj(dpp, svc()->sysobj,
      svc()->zone->get_zone_params().log_pool,
      topics_oid(tenant),
      objv_tracker, y);
}

int RadosStore::read_topic_v2(const std::string& topic_name,
                              const std::string& tenant,
                              rgw_pubsub_topic& topic,
                              RGWObjVersionTracker* objv_tracker,
                              optional_yield y,
                              const DoutPrefixProvider* dpp)
{
  const RGWZoneParams& zone = svc()->zone->get_zone_params();
  const std::string key = get_topic_metadata_key(tenant, topic_name);
  return rgwrados::topic::read(dpp, y, *svc()->sysobj, svc()->cache,
                               zone, key, topic, *ctl()->meta.topic_cache,
                               nullptr, objv_tracker);
}

int RadosStore::write_topic_v2(const rgw_pubsub_topic& topic, bool exclusive,
                               RGWObjVersionTracker& objv_tracker,
                               optional_yield y,
                               const DoutPrefixProvider* dpp)
{
  librados::Rados& rados = *getRados()->get_rados_handle();
  const RGWZoneParams& zone = svc()->zone->get_zone_params();
  return rgwrados::topic::write(dpp, y, *svc()->sysobj, svc()->mdlog, rados,
                                zone, topic, objv_tracker, {}, exclusive);
}

int RadosStore::remove_topic_v2(const std::string& topic_name,
                                const std::string& tenant,
                                RGWObjVersionTracker& objv_tracker,
                                optional_yield y,
                                const DoutPrefixProvider* dpp)
{
  librados::Rados& rados = *getRados()->get_rados_handle();
  const RGWZoneParams& zone = svc()->zone->get_zone_params();
  return rgwrados::topic::remove(dpp, y, *svc()->sysobj, svc()->mdlog,
                                 rados, zone, tenant, topic_name, objv_tracker);
}

int RadosStore::list_account_topics(const DoutPrefixProvider* dpp,
                                    optional_yield y,
                                    std::string_view account_id,
                                    std::string_view marker,
                                    uint32_t max_items,
                                    TopicList& listing)
{
  librados::Rados& rados = *getRados()->get_rados_handle();
  const RGWZoneParams& zone = svc()->zone->get_zone_params();
  const rgw_raw_obj& obj = rgwrados::account::get_topics_obj(zone, account_id);
  return rgwrados::topics::list(dpp, y, rados, obj, marker, max_items,
                                listing.topics, listing.next_marker);
}

int RadosStore::add_persistent_topic(const DoutPrefixProvider* dpp,
                                     optional_yield y,
                                     const std::string& topic_queue)
{
  return rgw::notify::add_persistent_topic(
      dpp, getRados()->get_notif_pool_ctx(), topic_queue, y);
}

int RadosStore::remove_persistent_topic(const DoutPrefixProvider* dpp,
                                        optional_yield y,
                                        const std::string& topic_queue)
{
  return rgw::notify::remove_persistent_topic(
      dpp, getRados()->get_notif_pool_ctx(), topic_queue, y);
}

int RadosStore::remove_bucket_mapping_from_topics(
    const rgw_pubsub_bucket_topics& bucket_topics,
    const std::string& bucket_key,
    optional_yield y,
    const DoutPrefixProvider* dpp) {
  // remove the bucket name from  the topic-bucket omap for each topic
  // subscribed.
  std::unordered_set<std::string> topics_mapping_to_remove;
  int ret = 0;
  for (const auto& [_, topic_filter] : bucket_topics.topics) {
    if (!topics_mapping_to_remove.insert(topic_filter.topic.name).second) {
      continue;  // already removed.
    }
    int op_ret = update_bucket_topic_mapping(topic_filter.topic, bucket_key,
                                             /*add_mapping=*/false, y, dpp);
    if (op_ret < 0) {
      ret = op_ret;
    }
  }
  return ret;
}

int RadosStore::update_bucket_topic_mapping(const rgw_pubsub_topic& topic,
                                            const std::string& bucket_key,
                                            bool add_mapping,
                                            optional_yield y,
                                            const DoutPrefixProvider* dpp) {
  librados::Rados& rados = *getRados()->get_rados_handle();
  const RGWZoneParams& zone = svc()->zone->get_zone_params();
  const std::string key = get_topic_metadata_key(topic);
  int ret = 0;
  if (add_mapping) {
    ret = rgwrados::topic::link_bucket(dpp, y, rados, zone, key, bucket_key);
  } else {
    ret = rgwrados::topic::unlink_bucket(dpp, y, rados, zone, key, bucket_key);
  }
  if (ret < 0) {
    ldpp_dout(dpp, 1) << "ERROR: failed to " << (add_mapping ? "add" : "remove")
                      << " topic bucket mapping for bucket: " << bucket_key
                      << " and topic: " << topic.name << " with ret:" << ret << dendl;
    return ret;
  }
  ldpp_dout(dpp, 20) << "Successfully " << (add_mapping ? "added" : "removed")
                     << " topic bucket mapping for bucket: " << bucket_key
                     << " and topic: " << topic.name << dendl;
  return ret;
}

int RadosStore::get_bucket_topic_mapping(const rgw_pubsub_topic& topic,
                                         std::set<std::string>& bucket_keys,
                                         optional_yield y,
                                         const DoutPrefixProvider* dpp)
{
  librados::Rados& rados = *getRados()->get_rados_handle();
  const RGWZoneParams& zone = svc()->zone->get_zone_params();
  const std::string key = get_topic_metadata_key(topic);
  constexpr int max_chunk = 1024;
  std::string marker;

  do {
    int ret = rgwrados::topic::list_buckets(dpp, y, rados, zone, key, marker,
                                            max_chunk, bucket_keys, marker);
    if (ret < 0) {
      ldpp_dout(dpp, 1)
          << "ERROR: failed to read bucket topic mapping object for topic: "
          << topic.name << ", ret= " << ret << dendl;
      return ret;
    }
  } while (!marker.empty());

  return 0;
}

int RadosStore::delete_raw_obj(const DoutPrefixProvider *dpp, const rgw_raw_obj& obj, optional_yield y)
{
  return rados->delete_raw_obj(dpp, obj, y);
}

void RadosStore::get_raw_obj(const rgw_placement_rule& placement_rule, const rgw_obj& obj, rgw_raw_obj* raw_obj)
{
    rados->obj_to_raw(placement_rule, obj, raw_obj);
}

int RadosStore::get_raw_chunk_size(const DoutPrefixProvider* dpp, const rgw_raw_obj& obj, uint64_t* chunk_size)
{
  return rados->get_max_chunk_size(obj.pool, chunk_size, dpp);
}

int RadosStore::init_neorados(const DoutPrefixProvider* dpp) {
  if (!neorados) try {
      neorados = neorados::RADOS::make_with_cct(dpp->get_cct(), io_context,
						ceph::async::use_blocked);
    } catch (const boost::system::system_error& e) {
      ldpp_dout(dpp, 0) << "ERROR: creating neorados handle failed: "
			<< e.what() << dendl;
      return ceph::from_error_code(e.code());
    }
  return 0;
}

int RadosStore::initialize(CephContext *cct, const DoutPrefixProvider *dpp)
{
  std::unique_ptr<ZoneGroup> zg =
    std::make_unique<RadosZoneGroup>(this, svc()->zone->get_zonegroup());
  zone = make_unique<RadosZone>(this, std::move(zg));

  return init_neorados(dpp);
}

int RadosStore::log_usage(const DoutPrefixProvider *dpp, map<rgw_user_bucket, RGWUsageBatch>& usage_info, optional_yield y)
{
    return rados->log_usage(dpp, usage_info, y);
}

int RadosStore::log_op(const DoutPrefixProvider *dpp, std::string& oid, bufferlist& bl)
{
  rgw_raw_obj obj(svc()->zone->get_zone_params().log_pool, oid);

  int ret = rados->append_async(dpp, obj, bl.length(), bl);
  if (ret == -ENOENT) {
    ret = rados->create_pool(dpp, svc()->zone->get_zone_params().log_pool);
    if (ret < 0)
      return ret;
    // retry
    ret = rados->append_async(dpp, obj, bl.length(), bl);
  }

  return ret;
}

int RadosStore::register_to_service_map(const DoutPrefixProvider *dpp, const std::string& daemon_type,
					   const map<std::string, std::string>& meta)
{
  return rados->register_to_service_map(dpp, daemon_type, meta);
}

void RadosStore::get_quota(RGWQuota& quota)
{
    quota.bucket_quota = svc()->quota->get_bucket_quota();
    quota.user_quota = svc()->quota->get_user_quota();
}

void RadosStore::get_ratelimit(RGWRateLimitInfo& bucket_ratelimit, RGWRateLimitInfo& user_ratelimit, RGWRateLimitInfo& anon_ratelimit)
{
  bucket_ratelimit = svc()->zone->get_current_period().get_config().bucket_ratelimit;
  user_ratelimit = svc()->zone->get_current_period().get_config().user_ratelimit;
  anon_ratelimit = svc()->zone->get_current_period().get_config().anon_ratelimit;
}

int RadosStore::set_buckets_enabled(const DoutPrefixProvider* dpp, vector<rgw_bucket>& buckets, bool enabled, optional_yield y)
{
    return rados->set_buckets_enabled(buckets, enabled, dpp, y);
}

int RadosStore::get_sync_policy_handler(const DoutPrefixProvider* dpp,
					   std::optional<rgw_zone_id> zone,
					   std::optional<rgw_bucket> bucket,
					   RGWBucketSyncPolicyHandlerRef* phandler,
					   optional_yield y)
{
  return ctl()->bucket->get_sync_policy_handler(zone, bucket, phandler, y, dpp);
}

RGWDataSyncStatusManager* RadosStore::get_data_sync_manager(const rgw_zone_id& source_zone)
{
  return rados->get_data_sync_manager(source_zone);
}

int RadosStore::read_all_usage(const DoutPrefixProvider *dpp, uint64_t start_epoch, uint64_t end_epoch,
				  uint32_t max_entries, bool* is_truncated,
				  RGWUsageIter& usage_iter,
				  map<rgw_user_bucket, rgw_usage_log_entry>& usage)
{
  rgw_user uid;
  std::string bucket_name;

  return rados->read_usage(dpp, uid, bucket_name, start_epoch, end_epoch, max_entries,
			   is_truncated, usage_iter, usage);
}

int RadosStore::trim_all_usage(const DoutPrefixProvider *dpp, uint64_t start_epoch, uint64_t end_epoch, optional_yield y)
{
  rgw_user uid;
  std::string bucket_name;

  return rados->trim_usage(dpp, uid, bucket_name, start_epoch, end_epoch, y);
}

int RadosStore::get_config_key_val(std::string name, bufferlist* bl)
{
  return svc()->config_key->get(name, true, bl);
}

int RadosStore::meta_list_keys_init(const DoutPrefixProvider *dpp, const std::string& section, const std::string& marker, void** phandle)
{
  return ctl()->meta.mgr->list_keys_init(dpp, section, marker, phandle);
}

int RadosStore::meta_list_keys_next(const DoutPrefixProvider *dpp, void* handle, int max, list<std::string>& keys, bool* truncated)
{
  return ctl()->meta.mgr->list_keys_next(dpp, handle, max, keys, truncated);
}

void RadosStore::meta_list_keys_complete(void* handle)
{
  ctl()->meta.mgr->list_keys_complete(handle);
}

std::string RadosStore::meta_get_marker(void* handle)
{
  return ctl()->meta.mgr->get_marker(handle);
}

int RadosStore::meta_remove(const DoutPrefixProvider* dpp, std::string& metadata_key, optional_yield y)
{
  return ctl()->meta.mgr->remove(metadata_key, y, dpp);
}

void RadosStore::finalize(void)
{
  if (rados)
    rados->finalize();
}

void RadosStore::register_admin_apis(RGWRESTMgr* mgr)
{
  mgr->register_resource("user", new RGWRESTMgr_User);
  mgr->register_resource("bucket", new RGWRESTMgr_Bucket);
  /*Registering resource for /admin/metadata */
  mgr->register_resource("metadata", new RGWRESTMgr_Metadata);
  mgr->register_resource("log", new RGWRESTMgr_Log);
  /* XXX These may become global when cbodley is done with his zone work */
  mgr->register_resource("config", new RGWRESTMgr_Config);
  mgr->register_resource("realm", new RGWRESTMgr_Realm);
  mgr->register_resource("ratelimit", new RGWRESTMgr_Ratelimit);
}

std::unique_ptr<LuaManager> RadosStore::get_lua_manager(const std::string& luarocks_path)
{
  return std::make_unique<RadosLuaManager>(this, luarocks_path);
}

std::unique_ptr<RGWRole> RadosStore::get_role(std::string name,
					      std::string tenant,
					      rgw_account_id account_id,
					      std::string path,
					      std::string trust_policy,
					      std::string description,
					      std::string max_session_duration_str,
                std::multimap<std::string,std::string> tags)
{
  return std::make_unique<RadosRole>(this, name, tenant, std::move(account_id), path, trust_policy, std::move(description), max_session_duration_str, tags);
}

std::unique_ptr<RGWRole> RadosStore::get_role(std::string id)
{
  return std::make_unique<RadosRole>(this, id);
}

std::unique_ptr<RGWRole> RadosStore::get_role(const RGWRoleInfo& info)
{
  return std::make_unique<RadosRole>(this, info);
}

int RadosStore::list_roles(const DoutPrefixProvider *dpp,
			   optional_yield y,
			   const std::string& tenant,
			   const std::string& path_prefix,
			   const std::string& marker,
			   uint32_t max_items,
			   RoleList& listing)
{
  const RGWZoneParams& zone = svc()->zone->get_zone_params();
  return rgwrados::role::list_tenant(dpp, y, *svc()->sysobj, zone,
                                     tenant, marker, max_items, path_prefix,
                                     listing.roles, listing.next_marker);
}

static constexpr std::string_view oidc_url_oid_prefix = "oidc_url.";

static std::string oidc_provider_oid(std::string_view account,
                                     std::string_view prefix,
                                     std::string_view url)
{
  return string_cat_reserve(account, prefix, url);
}

int RadosStore::store_oidc_provider(const DoutPrefixProvider *dpp,
                                    optional_yield y,
                                    const RGWOIDCProviderInfo& info,
                                    bool exclusive)
{
  auto sysobj = svc()->sysobj;
  std::string oid = oidc_provider_oid(info.tenant, oidc_url_oid_prefix,
                                      url_remove_prefix(info.provider_url));

  // TODO: add support for oidc metadata sync
  bufferlist bl;
  using ceph::encode;
  encode(info, bl);
  return rgw_put_system_obj(dpp, sysobj, svc()->zone->get_zone_params().oidc_pool, oid, bl, exclusive, nullptr, real_time(), y);
}

int RadosStore::load_oidc_provider(const DoutPrefixProvider *dpp,
                                   optional_yield y,
                                   std::string_view account,
                                   std::string_view url,
                                   RGWOIDCProviderInfo& info)
{
  auto sysobj = svc()->sysobj;
  auto& pool = svc()->zone->get_zone_params().oidc_pool;
  std::string oid = oidc_provider_oid(account, oidc_url_oid_prefix, url);
  bufferlist bl;

  int ret = rgw_get_system_obj(sysobj, pool, oid, bl, nullptr, nullptr, y, dpp);
  if (ret < 0) {
    return ret;
  }

  try {
    using ceph::decode;
    auto iter = bl.cbegin();
    decode(info, iter);
  } catch (buffer::error& err) {
    ldpp_dout(dpp, 0) << "ERROR: failed to decode oidc provider info from pool: " << pool.name <<
                  ": " << url << dendl;
    return -EIO;
  }

  return 0;
}

int RadosStore::delete_oidc_provider(const DoutPrefixProvider *dpp,
                                     optional_yield y,
                                     std::string_view account,
                                     std::string_view url)
{
  auto& pool = svc()->zone->get_zone_params().oidc_pool;
  std::string oid = oidc_provider_oid(account, oidc_url_oid_prefix, url);
  int ret = rgw_delete_system_obj(dpp, svc()->sysobj, pool, oid, nullptr, y);
  if (ret < 0) {
    ldpp_dout(dpp, 0) << "ERROR: deleting oidc url from pool: " << pool.name << ": "
                  << url << ": " << cpp_strerror(-ret) << dendl;
  }

  return ret;
}

int RadosStore::get_oidc_providers(const DoutPrefixProvider* dpp,
				   optional_yield y,
				   std::string_view tenant,
				   vector<RGWOIDCProviderInfo>& providers)
{
  std::string prefix = string_cat_reserve(tenant, oidc_url_oid_prefix);
  auto pool = svc()->zone->get_zone_params().oidc_pool;

  //Get the filtered objects
  list<std::string> result;
  bool is_truncated;
  RGWListRawObjsCtx ctx;
  do {
    list<std::string> oids;
    int r = rados->list_raw_objects(dpp, pool, prefix, 1000, ctx, oids, &is_truncated);
    if (r == -ENOENT) {
      return 0;
    }
    if (r < 0) {
      ldpp_dout(dpp, 0) << "ERROR: listing filtered objects failed: OIDC pool: "
                  << pool.name << ": " << prefix << ": " << cpp_strerror(-r) << dendl;
      return r;
    }
    for (const auto& iter : oids) {
      bufferlist bl;
      r = rgw_get_system_obj(svc()->sysobj, pool, iter, bl, nullptr, nullptr, y, dpp);
      if (r < 0) {
        return r;
      }

      RGWOIDCProviderInfo info;
      try {
        using ceph::decode;
        auto iter = bl.cbegin();
        decode(info, iter);
      } catch (buffer::error& err) {
        ldpp_dout(dpp, 0) << "ERROR: failed to decode oidc provider info from pool: "
	  << pool.name << ": " << iter << dendl;
        return -EIO;
      }

      providers.push_back(std::move(info));
    }
  } while (is_truncated);

  return 0;
}

std::unique_ptr<Writer> RadosStore::get_append_writer(const DoutPrefixProvider *dpp,
				  optional_yield y,
				  rgw::sal::Object* obj,
				  const ACLOwner& owner,
				  const rgw_placement_rule *ptail_placement_rule,
				  const std::string& unique_tag,
				  uint64_t position,
				  uint64_t *cur_accounted_size)
{
  RGWBucketInfo& bucket_info = obj->get_bucket()->get_info();
  RGWObjectCtx& obj_ctx = static_cast<RadosObject*>(obj)->get_ctx();
  auto aio = rgw::make_throttle(ctx()->_conf->rgw_put_obj_min_window_size, y);
  return std::make_unique<RadosAppendWriter>(dpp, y,
				 bucket_info, obj_ctx, obj->get_obj(),
				 this, std::move(aio), owner,
				 ptail_placement_rule,
				 unique_tag, position,
				 cur_accounted_size, obj->get_trace());
}

std::unique_ptr<Writer> RadosStore::get_atomic_writer(const DoutPrefixProvider *dpp,
				  optional_yield y,
				  rgw::sal::Object* obj,
				  const ACLOwner& owner,
				  const rgw_placement_rule *ptail_placement_rule,
				  uint64_t olh_epoch,
				  const std::string& unique_tag)
{
  RGWBucketInfo& bucket_info = obj->get_bucket()->get_info();
  RGWObjectCtx& obj_ctx = static_cast<RadosObject*>(obj)->get_ctx();
  auto aio = rgw::make_throttle(ctx()->_conf->rgw_put_obj_min_window_size, y);
  return std::make_unique<RadosAtomicWriter>(dpp, y,
				 bucket_info, obj_ctx, obj->get_obj(),
				 this, std::move(aio), owner,
				 ptail_placement_rule,
				 olh_epoch, unique_tag, obj->get_trace());
}

const std::string& RadosStore::get_compression_type(const rgw_placement_rule& rule)
{
      return svc()->zone->get_zone_params().get_compression_type(rule);
}

bool RadosStore::valid_placement(const rgw_placement_rule& rule)
{
  return svc()->zone->get_zone_params().valid_placement(rule);
}

int RadosStore::get_obj_head_ioctx(const DoutPrefixProvider *dpp, const RGWBucketInfo& bucket_info, const rgw_obj& obj, librados::IoCtx* ioctx)
{
  return rados->get_obj_head_ioctx(dpp, bucket_info, obj, ioctx);
}

RadosObject::~RadosObject()
{
  if (rados_ctx_owned)
    delete rados_ctx;
}

bool RadosObject::is_sync_completed(const DoutPrefixProvider* dpp,
   const ceph::real_time& obj_mtime)
{
  const auto& bucket_info = get_bucket()->get_info();
  if (bucket_info.is_indexless()) {
    ldpp_dout(dpp, 0) << "ERROR: Trying to check object replication status for object in an indexless bucket. obj=" << get_key() << dendl;
    return false;
  }

  const auto& log_layout = bucket_info.layout.logs.front();
  const uint32_t shard_count = num_shards(log_to_index_layout(log_layout));

  std::string marker;
  bool truncated;
  list<rgw_bi_log_entry> entries;

  const int shard_id = RGWSI_BucketIndex_RADOS::bucket_shard_index(get_key(), shard_count);

  int ret = store->svc()->bilog_rados->log_list(dpp, bucket_info, log_layout, shard_id,
    marker, 1, entries, &truncated);

  if (ret < 0) {
    ldpp_dout(dpp, 0) << "ERROR: Failed to retrieve bilog info for obj=" << get_key() << dendl;
    return false;
  }

  if (entries.empty()) {
    return true;
  }

  const rgw_bi_log_entry& earliest_marker = entries.front();
  return earliest_marker.timestamp > obj_mtime;
} /* is_sync_completed */

int RadosObject::list_parts(const DoutPrefixProvider* dpp, CephContext* cct,
			   int max_parts, int marker, int* next_marker,
			   bool* truncated, list_parts_each_t each_func,
			   optional_yield y)
{
  int ret{0};

  /* require an object with a manifest, so call to get_obj_state() must precede this */
  if (! manifest) {
    return -EINVAL;
  }

  RGWObjManifest::obj_iterator end = manifest->obj_end(dpp);
  if (end.get_cur_part_id() == 0) { // not multipart
    ldpp_dout(dpp, 20) << __func__ << " object does not have a multipart manifest"
		       << dendl;
    return 0;
  }

  auto end_part_id = end.get_cur_part_id();
  auto parts_count = (end_part_id == 1) ? 1 : end_part_id - 1;
  if (marker > (parts_count - 1)) {
    return 0;
  }

  RGWObjManifest::obj_iterator part_iter = manifest->obj_begin(dpp);

  if (marker != 0) {
    ldpp_dout_fmt(dpp, 20,
		  "{} seeking to part #{} in the object manifest",
		  __func__, marker);

    part_iter  = manifest->obj_find_part(dpp, marker + 1);

    if (part_iter == end) {
      ldpp_dout_fmt(dpp, 5,
		    "{} failed to find part #{} in the object manifest",
		    __func__, marker + 1);
      return 0;
    }
  }

  RGWObjectCtx& obj_ctx = get_ctx();
  RGWBucketInfo& bucket_info = get_bucket()->get_info();

  Object::Part obj_part{};
  for (; part_iter != manifest->obj_end(dpp); ++part_iter) {

    /* we're only interested in the first object in each logical part */
    auto cur_part_id = part_iter.get_cur_part_id();
    if (cur_part_id == obj_part.part_number) {
      continue;
    }

    if (max_parts < 1) {
      *truncated = true;
      break;
    }

    /* get_part_obj_state alters the passed manifest** to point to a part
     * manifest, which we don't want to leak out here */
    RGWObjManifest* obj_m = manifest;
    RGWObjState* astate;
    bool part_prefetch = false;
    ret = RGWRados::get_part_obj_state(dpp, y, store->getRados(), bucket_info, &obj_ctx,
				       obj_m, cur_part_id, &parts_count,
				       part_prefetch, &astate, &obj_m);

    if (ret < 0) {
      ldpp_dout_fmt(dpp, 4,
		    "{} get_part_obj_state() failed ret={}",
		    __func__, ret);
      break;
    }

    obj_part.part_number = part_iter.get_cur_part_id();
    obj_part.part_size = astate->accounted_size;

    if (auto iter = astate->attrset.find(RGW_ATTR_CKSUM);
	iter != astate->attrset.end()) {
          try {
	    rgw::cksum::Cksum part_cksum;
	    auto ck_iter = iter->second.cbegin();
	    part_cksum.decode(ck_iter);
	    obj_part.cksum = std::move(part_cksum);
	  } catch (buffer::error& err) {
	    ldpp_dout_fmt(dpp, 4,
			  "WARN: {} could not decode stored cksum, "
			  "caught buffer::error",
			  __func__);
	  }
    }

    each_func(obj_part);
    *next_marker = ++marker;
    --max_parts;
  } /* each part */
  
  return ret;
} /* RadosObject::list_parts */

int RadosObject::load_obj_state(const DoutPrefixProvider* dpp, optional_yield y, bool follow_olh)
{
  RGWObjState *pstate{nullptr};

  int ret = store->getRados()->get_obj_state(dpp, rados_ctx, bucket->get_info(), get_obj(), &pstate, &manifest, follow_olh, y);
  if (ret < 0) {
    return ret;
  }

  /* Don't overwrite obj, atomic, or prefetch */
  rgw_obj obj = get_obj();
  bool is_atomic = state.is_atomic;
  bool prefetch_data = state.prefetch_data;

  state = *pstate;

  state.obj = obj;
  state.is_atomic = is_atomic;
  state.prefetch_data = prefetch_data;
  return ret;
}

int RadosObject::read_attrs(const DoutPrefixProvider* dpp, RGWRados::Object::Read &read_op, optional_yield y, rgw_obj* target_obj)
{
  read_op.params.attrs = &state.attrset;
  read_op.params.target_obj = target_obj;
  read_op.params.obj_size = &state.size;
  read_op.params.lastmod = &state.mtime;
  read_op.params.objv_tracker = &state.objv_tracker;

  return read_op.prepare(y, dpp);
}

int RadosObject::set_obj_attrs(const DoutPrefixProvider* dpp, Attrs* setattrs, Attrs* delattrs, optional_yield y, uint32_t flags)
{
  Attrs empty;
  const bool log_op = flags & rgw::sal::FLAG_LOG_OP;
  // make a tiny adjustment to the existing mtime so that fetch_remote_obj()
  // won't return ERR_NOT_MODIFIED when syncing the modified object
  const auto mtime = log_op ? state.mtime + std::chrono::nanoseconds(1) : state.mtime;
  return store->getRados()->set_attrs(dpp, rados_ctx,
			bucket->get_info(),
			get_obj(),
			setattrs ? *setattrs : empty,
			delattrs ? delattrs : nullptr,
			y, log_op, mtime);
}

int RadosObject::get_obj_attrs(optional_yield y, const DoutPrefixProvider* dpp, rgw_obj* target_obj)
{
  RGWRados::Object op_target(store->getRados(), bucket->get_info(), *rados_ctx, get_obj());
  RGWRados::Object::Read read_op(&op_target);

  return read_attrs(dpp, read_op, y, target_obj);
}

int RadosObject::modify_obj_attrs(const char* attr_name, bufferlist& attr_val, optional_yield y, const DoutPrefixProvider* dpp)
{
  rgw_obj target = get_obj();
  rgw_obj save = get_obj();
  int r = get_obj_attrs(y, dpp, &target);
  if (r < 0) {
    return r;
  }

  /* Temporarily set target */
  state.obj = target;
  set_atomic();
  state.attrset[attr_name] = attr_val;
  r = set_obj_attrs(dpp, &state.attrset, nullptr, y, rgw::sal::FLAG_LOG_OP);
  /* Restore target */
  state.obj = save;

  return r;
}

int RadosObject::delete_obj_attrs(const DoutPrefixProvider* dpp, const char* attr_name, optional_yield y)
{
  Attrs rmattr;
  bufferlist bl;

  set_atomic();
  rmattr[attr_name] = bl;
  return set_obj_attrs(dpp, nullptr, &rmattr, y, rgw::sal::FLAG_LOG_OP);
}

bool RadosObject::is_expired() {
  auto iter = state.attrset.find(RGW_ATTR_DELETE_AT);
  if (iter == state.attrset.end()) {
    return false;
  }
  utime_t delete_at;
  try {
    auto bufit = iter->second.cbegin();
    decode(delete_at, bufit);
  } catch (buffer::error& err) {
    ldout(store->ctx(), 0) << "ERROR: " << __func__ << ": failed to decode " RGW_ATTR_DELETE_AT " attr" << dendl;
    return false;
  }

  return delete_at <= ceph_clock_now() && !delete_at.is_zero();
}

void RadosObject::gen_rand_obj_instance_name()
{
  store->getRados()->gen_rand_obj_instance_name(&state.obj.key);
}

void RadosObject::raw_obj_to_obj(const rgw_raw_obj& raw_obj)
{
  rgw_obj tobj = get_obj();
  RGWSI_Tier_RADOS::raw_obj_to_obj(get_bucket()->get_key(), raw_obj, &tobj);
  set_key(tobj.key);
}

void RadosObject::get_raw_obj(rgw_raw_obj* raw_obj)
{
  store->getRados()->obj_to_raw((bucket->get_info()).placement_rule, get_obj(), raw_obj);
}

int RadosObject::get_torrent_info(const DoutPrefixProvider* dpp,
                                  optional_yield y, bufferlist& bl)
{
  // try to read torrent info from attr
  int ret = StoreObject::get_torrent_info(dpp, y, bl);
  if (ret >= 0) {
    return ret;
  }

  // try falling back to old torrent info stored in omap
  rgw_raw_obj raw_obj;
  get_raw_obj(&raw_obj);

  rgw_rados_ref ref;
  ret = store->getRados()->get_raw_obj_ref(dpp, raw_obj, &ref);
  if (ret < 0) {
    return ret;
  }

  const std::set<std::string> keys = {"rgw.torrent"};
  std::map<std::string, bufferlist> result;

  librados::ObjectReadOperation op;
  op.omap_get_vals_by_keys(keys, &result, nullptr);

  ret = rgw_rados_operate(dpp, ref.ioctx, ref.obj.oid, &op, nullptr, y);
  if (ret < 0) {
    return ret;
  }
  if (result.empty()) { // omap key not found
    return -ENOENT;
  }
  bl = std::move(result.begin()->second);
  return 0;
}

int RadosObject::omap_get_vals_by_keys(const DoutPrefixProvider *dpp, const std::string& oid,
					  const std::set<std::string>& keys,
					  Attrs* vals)
{
  int ret;
  rgw_raw_obj head_obj;
  librados::IoCtx cur_ioctx;
  rgw_obj obj = get_obj();

  store->getRados()->obj_to_raw(bucket->get_placement_rule(), obj, &head_obj);
  ret = store->get_obj_head_ioctx(dpp, bucket->get_info(), obj, &cur_ioctx);
  if (ret < 0) {
    return ret;
  }

  return cur_ioctx.omap_get_vals_by_keys(oid, keys, vals);
}

int RadosObject::omap_set_val_by_key(const DoutPrefixProvider *dpp, const std::string& key, bufferlist& val,
					bool must_exist, optional_yield y)
{
  rgw_raw_obj raw_meta_obj;
  rgw_obj obj = get_obj();

  store->getRados()->obj_to_raw(bucket->get_placement_rule(), obj, &raw_meta_obj);

  auto sysobj = store->svc()->sysobj->get_obj(raw_meta_obj);

  return sysobj.omap().set_must_exist(must_exist).set(dpp, key, val, y);
}

int RadosObject::chown(User& new_user, const DoutPrefixProvider* dpp, optional_yield y)
{
  int r = get_obj_attrs(y, dpp);
  if (r < 0) {
    ldpp_dout(dpp, 0) << "ERROR: failed to read object attrs " << get_name() << cpp_strerror(-r) << dendl;
    return r;
  }

  const auto& aiter = get_attrs().find(RGW_ATTR_ACL);
  if (aiter == get_attrs().end()) {
    ldpp_dout(dpp, 0) << "ERROR: no acls found for object " << get_name() << dendl;
    return -EINVAL;
  }

  bufferlist& bl = aiter->second;
  RGWAccessControlPolicy policy;
  ACLOwner owner;
  auto bliter = bl.cbegin();
  try {
    policy.decode(bliter);
    owner = policy.get_owner();
  } catch (buffer::error& err) {
    ldpp_dout(dpp, 0) << "ERROR: decode policy failed" << err.what()
      << dendl;
    return -EIO;
  }

  //Get the ACL from the policy
  RGWAccessControlList& acl = policy.get_acl();

  //Remove grant that is set to old owner
  acl.remove_canon_user_grant(owner.id);

  //Create a grant and add grant
  ACLGrant grant;
  grant.set_canon(new_user.get_id(), new_user.get_display_name(), RGW_PERM_FULL_CONTROL);
  acl.add_grant(grant);

  //Update the ACL owner to the new user
  owner.id = new_user.get_id();
  owner.display_name = new_user.get_display_name();
  policy.set_owner(owner);

  bl.clear();
  encode(policy, bl);

  set_atomic();
  map<string, bufferlist> attrs;
  attrs[RGW_ATTR_ACL] = bl;
  r = set_obj_attrs(dpp, &attrs, nullptr, y, rgw::sal::FLAG_LOG_OP);
  if (r < 0) {
    ldpp_dout(dpp, 0) << "ERROR: modify attr failed " << cpp_strerror(-r) << dendl;
    return r;
  }

  return 0;
}

std::unique_ptr<MPSerializer> RadosObject::get_serializer(const DoutPrefixProvider *dpp, const std::string& lock_name)
{
  return std::make_unique<MPRadosSerializer>(dpp, store, this, lock_name);
}

int RadosObject::transition(Bucket* bucket,
			    const rgw_placement_rule& placement_rule,
			    const real_time& mtime,
			    uint64_t olh_epoch,
			    const DoutPrefixProvider* dpp,
			    optional_yield y,
                            uint32_t flags)
{
  return store->getRados()->transition_obj(*rados_ctx, bucket->get_info(), get_obj(), placement_rule,
                                           mtime, olh_epoch, dpp, y, flags & FLAG_LOG_OP);
}

int RadosObject::restore_obj_from_cloud(Bucket* bucket,
                                  rgw::sal::PlacementTier* tier,
                                  rgw_placement_rule& placement_rule,
                            	  rgw_bucket_dir_entry& o,
                          	  CephContext* cct,
                                  RGWObjTier& tier_config,
                                  real_time& mtime,
                                  uint64_t olh_epoch,
                                  std::optional<uint64_t> days,
                                  const DoutPrefixProvider* dpp, 
                                  optional_yield y,
                                  uint32_t flags)
{
  /* init */
  rgw::sal::RadosPlacementTier* rtier = static_cast<rgw::sal::RadosPlacementTier*>(tier);
  string id = "cloudid";
  string endpoint = rtier->get_rt().t.s3.endpoint;
  RGWAccessKey key = rtier->get_rt().t.s3.key;
  string region = rtier->get_rt().t.s3.region;
  HostStyle host_style = rtier->get_rt().t.s3.host_style;
  string bucket_name = rtier->get_rt().t.s3.target_path;
  const rgw::sal::ZoneGroup& zonegroup = store->get_zone()->get_zonegroup();
  int ret = 0;
  string src_storage_class = o.meta.storage_class; // or take src_placement also as input

  // fetch mtime of the object
  std::unique_ptr<rgw::sal::Object::ReadOp> read_op(get_read_op());
  read_op->params.lastmod = &mtime;

  ret = read_op->prepare(y, dpp);
  if (ret < 0) {
    ldpp_dout(dpp, 0) << "Restoring object(" << o.key << "): read_op failed ret=" << ret << dendl;
    return ret;
  }

  if (bucket_name.empty()) {
    bucket_name = "rgwx-" + zonegroup.get_name() + "-" + tier->get_storage_class() +
                    "-cloud-bucket";
    boost::algorithm::to_lower(bucket_name);
  }
  /* Create RGW REST connection */
  S3RESTConn conn(cct, id, { endpoint }, key, zonegroup.get_id(), region, host_style);

  // save source cloudtier storage class
  RGWLCCloudTierCtx tier_ctx(cct, dpp, o, store, bucket->get_info(),
           this, conn, bucket_name,
           rtier->get_rt().t.s3.target_storage_class);
  tier_ctx.acl_mappings = rtier->get_rt().t.s3.acl_mappings;
  tier_ctx.multipart_min_part_size = rtier->get_rt().t.s3.multipart_min_part_size;
  tier_ctx.multipart_sync_threshold = rtier->get_rt().t.s3.multipart_sync_threshold;
  tier_ctx.storage_class = tier->get_storage_class();

  ldpp_dout(dpp, 20) << "Restoring object(" << o.key << ") from the cloud endpoint(" << endpoint << ")" << dendl;

  if (days && days == 0) {
    ldpp_dout(dpp, 0) << "Days = 0 not valid; Not restoring object (" << o.key << ") from the cloud endpoint(" << endpoint << ")" << dendl;
    return 0;
  }

  // Note: For non-versioned objects, below should have already been set by the callers-
  // o.current should be false; this(obj)->instance should have version-id.

  // set restore_status as RESTORE_ALREADY_IN_PROGRESS
  ret = set_cloud_restore_status(dpp, y, RGWRestoreStatus::RestoreAlreadyInProgress);
  if (ret < 0) {
    ldpp_dout(dpp, 0) << " Setting cloud restore status to RESTORE_ALREADY_IN_PROGRESS for the object(" << o.key << ") from the cloud endpoint(" << endpoint << ") failed, ret=" << ret << dendl;
    return ret;
  }

  /* Restore object from the cloud endpoint.
   * All restore related status and attrs are set as part of object download to
   * avoid any races */
  ret = store->getRados()->restore_obj_from_cloud(tier_ctx, *rados_ctx,
                                bucket->get_info(), get_obj(), placement_rule,
                                tier_config,
                                mtime, olh_epoch, days, dpp, y, flags & FLAG_LOG_OP);

  if (ret < 0) { //failed to restore
    ldpp_dout(dpp, 0) << "Restoring object(" << o.key << ") from the cloud endpoint(" << endpoint << ") failed, ret=" << ret << dendl;
    auto reset_ret = set_cloud_restore_status(dpp, y, RGWRestoreStatus::RestoreFailed);

    rgw_placement_rule target_placement;
    target_placement.inherit_from(tier_ctx.bucket_info.placement_rule);
    target_placement.storage_class = tier->get_storage_class();

    /* Reset HEAD object as CloudTiered */
    reset_ret = write_cloud_tier(dpp, y, tier_ctx.o.versioned_epoch,
			   tier, tier_ctx.is_multipart_upload,
			   target_placement, tier_ctx.obj);

    if (reset_ret < 0) {
      ldpp_dout(dpp, 0) << " Reset to cloud_tier of object(" << o.key << ") from the cloud endpoint(" << endpoint << ") failed, ret=" << reset_ret << dendl;
    }
    return ret;
  }

  ldpp_dout(dpp, 20) << "Sucessfully restored object(" << o.key << ") from the cloud endpoint(" << endpoint << ")" << dendl;

  return ret;
}

int RadosObject::transition_to_cloud(Bucket* bucket,
			   rgw::sal::PlacementTier* tier,
			   rgw_bucket_dir_entry& o,
			   std::set<std::string>& cloud_targets,
			   CephContext* cct,
			   bool update_object,
			   const DoutPrefixProvider* dpp,
			   optional_yield y)
{
  /* init */
  rgw::sal::RadosPlacementTier* rtier = static_cast<rgw::sal::RadosPlacementTier*>(tier);
  string id = "cloudid";
  string endpoint = rtier->get_rt().t.s3.endpoint;
  RGWAccessKey key = rtier->get_rt().t.s3.key;
  string region = rtier->get_rt().t.s3.region;
  HostStyle host_style = rtier->get_rt().t.s3.host_style;
  string bucket_name = rtier->get_rt().t.s3.target_path;
  const rgw::sal::ZoneGroup& zonegroup = store->get_zone()->get_zonegroup();

  if (bucket_name.empty()) {
    bucket_name = "rgwx-" + zonegroup.get_name() + "-" + tier->get_storage_class() +
                    "-cloud-bucket";
    boost::algorithm::to_lower(bucket_name);
  }

  /* Create RGW REST connection */
  S3RESTConn conn(cct, id, { endpoint }, key, zonegroup.get_id(), region, host_style);

  RGWLCCloudTierCtx tier_ctx(cct, dpp, o, store, bucket->get_info(),
			     this, conn, bucket_name,
			     rtier->get_rt().t.s3.target_storage_class);
  tier_ctx.acl_mappings = rtier->get_rt().t.s3.acl_mappings;
  tier_ctx.multipart_min_part_size = rtier->get_rt().t.s3.multipart_min_part_size;
  tier_ctx.multipart_sync_threshold = rtier->get_rt().t.s3.multipart_sync_threshold;
  tier_ctx.storage_class = tier->get_storage_class();

  ldpp_dout(dpp, 0) << "Transitioning object(" << o.key << ") to the cloud endpoint(" << endpoint << ")" << dendl;

  /* Transition object to cloud end point */
  int ret = rgw_cloud_tier_transfer_object(tier_ctx, cloud_targets);

  if (ret < 0) {
    ldpp_dout(dpp, 0) << "ERROR: failed to transfer object(" << o.key << ") to the cloud endpoint(" << endpoint << ") ret=" << ret << dendl;
    return ret;
  }

  if (update_object) {
    real_time read_mtime;

    std::unique_ptr<rgw::sal::Object::ReadOp> read_op(get_read_op());
    read_op->params.lastmod = &read_mtime;

    ret = read_op->prepare(y, dpp);
    if (ret < 0) {
      ldpp_dout(dpp, 0) << "ERROR: Updating tier object(" << o.key << ") failed ret=" << ret << dendl;
      return ret;
    }

    if (read_mtime != tier_ctx.o.meta.mtime) {
      /* raced */
      ldpp_dout(dpp, 0) << "ERROR: Updating tier object(" << o.key << ") failed ret=" << -ECANCELED << dendl;
      return -ECANCELED;
    }

    rgw_placement_rule target_placement;
    target_placement.inherit_from(tier_ctx.bucket_info.placement_rule);
    target_placement.storage_class = tier->get_storage_class();

    ret = write_cloud_tier(dpp, y, tier_ctx.o.versioned_epoch,
			   tier, tier_ctx.is_multipart_upload,
			   target_placement, tier_ctx.obj);

  }

  return ret;
}

int RadosObject::set_cloud_restore_status(const DoutPrefixProvider* dpp,
				  optional_yield y,
			          rgw::sal::RGWRestoreStatus restore_status)
{
  int ret = 0;
  set_atomic();
 
  bufferlist bl;
  using ceph::encode;
  encode(restore_status, bl);

  ret = modify_obj_attrs(RGW_ATTR_RESTORE_STATUS, bl, y, dpp);

  return ret;
}

/*
 * If the object is restored temporarily and is expired, delete the data and
 * reset the HEAD object as cloud-transitioned.
 */
int RadosObject::handle_obj_expiry(const DoutPrefixProvider* dpp, optional_yield y) {
  int ret = 0;
  real_time read_mtime;
  std::unique_ptr<rgw::sal::Object::ReadOp> read_op(get_read_op());
  read_op->params.lastmod = &read_mtime;
  ldpp_dout(dpp, 20) << "Entering handle_obj_expiry Obj:" << get_key() << dendl;

  ret = read_op->prepare(y, dpp);
  if (ret < 0) {
    ldpp_dout(dpp, -1) << "handle_obj_expiry Obj:" << get_key() << 
	    ", read_op failed ret=" << ret << dendl;
    return ret;
  }

  set_atomic();
  map<string, bufferlist> attrs = get_attrs();
  RGWRados::Object op_target(store->getRados(), bucket->get_info(), *rados_ctx, get_obj());
  RGWRados::Object::Write obj_op(&op_target);
	Object* obj = (Object*)this;

  bufferlist bl;
  auto attr_iter = attrs.find(RGW_ATTR_RESTORE_TYPE);
  if (attr_iter != attrs.end()) {
    using ceph::decode;
    rgw::sal::RGWRestoreType restore_type;
    decode(restore_type, attr_iter->second);
    if (restore_type == rgw::sal::RGWRestoreType::Temporary) {
      ldpp_dout(dpp, 10) << "Expiring temporary restored Obj:" << get_key() << dendl;

      attr_iter = attrs.find(RGW_ATTR_MANIFEST);
      if (attr_iter != attrs.end()) {
        RGWObjManifest m;
        try {
          using ceph::decode;
          decode(m, attr_iter->second);
          obj_op.meta.modify_tail = true;
          obj_op.meta.flags = PUT_OBJ_CREATE;
          obj_op.meta.category = RGWObjCategory::CloudTiered;
          obj_op.meta.delete_at = real_time();
          bufferlist blo;
          obj_op.meta.data = &blo;
          obj_op.meta.if_match = NULL;
          obj_op.meta.if_nomatch = NULL;
          obj_op.meta.user_data = NULL;
          obj_op.meta.zones_trace = NULL;
          obj_op.meta.set_mtime = read_mtime;

          RGWObjManifest *pmanifest;
          pmanifest = &m;

	        Object* head_obj = (Object*)this;
          RGWObjTier tier_config;
          m.get_tier_config(&tier_config);
	
          rgw_placement_rule target_placement(pmanifest->get_head_placement_rule(), tier_config.name);

          pmanifest->set_head(target_placement, head_obj->get_obj(), 0);
          pmanifest->set_tail_placement(target_placement, head_obj->get_obj().bucket);
          pmanifest->set_obj_size(0);
          obj_op.meta.manifest = pmanifest;

          // erase restore attrs
          attrs.erase(RGW_ATTR_RESTORE_STATUS);
          attrs.erase(RGW_ATTR_RESTORE_TYPE);
          attrs.erase(RGW_ATTR_RESTORE_TIME);
          attrs.erase(RGW_ATTR_RESTORE_EXPIRY_DATE);
          attrs.erase(RGW_ATTR_CLOUDTIER_STORAGE_CLASS);

          bufferlist bl;
          bl.append(tier_config.name);
          attrs[RGW_ATTR_STORAGE_CLASS] = bl;

          const req_context rctx{dpp, y, nullptr};
          return obj_op.write_meta(0, 0, attrs, rctx, head_obj->get_trace());
        } catch (const buffer::end_of_buffer&) {
          // ignore empty manifest; it's not cloud-tiered
        } catch (const std::exception& e) {
        }
      }
      return 0;
    }
  }
  // object is not restored/temporary; go for regular deletion
  // ensure object is not overwritten and is really expired
  if (is_expired()) {
    ldpp_dout(dpp, 10) << "Deleting expired obj:" << get_key() << dendl;

    ret = obj->delete_object(dpp, null_yield, rgw::sal::FLAG_LOG_OP, nullptr, nullptr);
  }

  return ret;
}
int RadosObject::write_cloud_tier(const DoutPrefixProvider* dpp,
				  optional_yield y,
				  uint64_t olh_epoch,
				  PlacementTier* tier,
				  bool is_multipart_upload,
				  rgw_placement_rule& target_placement,
				  Object* head_obj)
{
  rgw::sal::RadosPlacementTier* rtier = static_cast<rgw::sal::RadosPlacementTier*>(tier);
  map<string, bufferlist> attrs = get_attrs();
  rgw_obj_key& obj_key = get_key();
  // bi expects empty instance for the entries created when bucket versioning
  // is not enabled or suspended.
  if (obj_key.instance == "null") {
      obj_key.instance.clear();
  }

  RGWRados::Object op_target(store->getRados(), bucket->get_info(), *rados_ctx, get_obj());
  RGWRados::Object::Write obj_op(&op_target);

  set_atomic();
  obj_op.meta.modify_tail = true;
  obj_op.meta.flags = PUT_OBJ_CREATE;
  obj_op.meta.category = RGWObjCategory::CloudTiered;
  obj_op.meta.delete_at = real_time();
  bufferlist blo;
  obj_op.meta.data = &blo;
  obj_op.meta.if_match = NULL;
  obj_op.meta.if_nomatch = NULL;
  obj_op.meta.user_data = NULL;
  obj_op.meta.zones_trace = NULL;
  obj_op.meta.olh_epoch = olh_epoch;

  RGWObjManifest *pmanifest;
  RGWObjManifest manifest;

  pmanifest = &manifest;
  RGWObjTier tier_config;
  tier_config.name = tier->get_storage_class();
  tier_config.tier_placement = rtier->get_rt();
  tier_config.is_multipart_upload = is_multipart_upload;

  pmanifest->set_tier_type("cloud-s3");
  pmanifest->set_tier_config(tier_config);

  /* check if its necessary */
  pmanifest->set_head(target_placement, head_obj->get_obj(), 0);
  pmanifest->set_tail_placement(target_placement, head_obj->get_obj().bucket);
  pmanifest->set_obj_size(0);
  obj_op.meta.manifest = pmanifest;

  /* update storage class */
  bufferlist bl;
  bl.append(tier->get_storage_class());
  attrs[RGW_ATTR_STORAGE_CLASS] = bl;

  attrs.erase(RGW_ATTR_ID_TAG);
  attrs.erase(RGW_ATTR_TAIL_TAG);

  // erase restore attrs
  attrs.erase(RGW_ATTR_RESTORE_STATUS);
  attrs.erase(RGW_ATTR_RESTORE_TYPE);
  attrs.erase(RGW_ATTR_RESTORE_TIME);
  attrs.erase(RGW_ATTR_RESTORE_EXPIRY_DATE);
  attrs.erase(RGW_ATTR_CLOUDTIER_STORAGE_CLASS);

  const req_context rctx{dpp, y, nullptr};
  return obj_op.write_meta(0, 0, attrs, rctx, head_obj->get_trace());
}

int RadosObject::get_max_chunk_size(const DoutPrefixProvider* dpp, rgw_placement_rule placement_rule, uint64_t* max_chunk_size, uint64_t* alignment)
{
  return store->getRados()->get_max_chunk_size(placement_rule, get_obj(), max_chunk_size, dpp, alignment);
}

void RadosObject::get_max_aligned_size(uint64_t size, uint64_t alignment,
				     uint64_t* max_size)
{
  store->getRados()->get_max_aligned_size(size, alignment, max_size);
}

bool RadosObject::placement_rules_match(rgw_placement_rule& r1, rgw_placement_rule& r2)
{
  rgw_obj obj;
  rgw_pool p1, p2;

  obj = get_obj();

  if (r1 == r2)
    return true;

  if (!store->getRados()->get_obj_data_pool(r1, obj, &p1)) {
    return false;
  }
  if (!store->getRados()->get_obj_data_pool(r2, obj, &p2)) {
    return false;
  }

  return p1 == p2;
}

int RadosObject::dump_obj_layout(const DoutPrefixProvider *dpp, optional_yield y, Formatter* f)
{
  int ret;
  RGWObjManifest *amanifest{nullptr};
  rgw_raw_obj head_obj;

  RGWRados::Object op_target(store->getRados(), bucket->get_info(), *rados_ctx, get_obj());
  RGWRados::Object::Read parent_op(&op_target);
  uint64_t obj_size;

  parent_op.params.obj_size = &obj_size;
  parent_op.params.attrs = &get_attrs();

  ret = parent_op.prepare(y, dpp);
  if (ret < 0) {
    return ret;
  }

  head_obj = parent_op.state.head_obj;

  ret = op_target.get_manifest(dpp, &amanifest, y);
  if (ret < 0) {
    return ret;
  }

  ::encode_json("head", head_obj, f);
  ::encode_json("manifest", *amanifest, f);
  f->open_array_section("data_location");
  for (auto miter = amanifest->obj_begin(dpp); miter != amanifest->obj_end(dpp); ++miter) {
    f->open_object_section("obj");
    rgw_raw_obj raw_loc = miter.get_location().get_raw_obj(store->getRados());
    uint64_t ofs = miter.get_ofs();
    uint64_t left = amanifest->get_obj_size() - ofs;
    ::encode_json("ofs", miter.get_ofs(), f);
    ::encode_json("loc", raw_loc, f);
    ::encode_json("loc_ofs", miter.location_ofs(), f);
    uint64_t loc_size = miter.get_stripe_size();
    if (loc_size > left) {
      loc_size = left;
    }
    ::encode_json("loc_size", loc_size, f);
    f->close_section();
  }
  f->close_section();

  return 0;
}

std::unique_ptr<Object::ReadOp> RadosObject::get_read_op()
{
  return std::make_unique<RadosObject::RadosReadOp>(this, rados_ctx);
}

RadosObject::RadosReadOp::RadosReadOp(RadosObject *_source, RGWObjectCtx *_octx) :
	source(_source),
	octx(_octx),
	op_target(_source->store->getRados(),
		  _source->get_bucket()->get_info(),
		  *static_cast<RGWObjectCtx *>(octx),
		  _source->get_obj()),
	parent_op(&op_target)
{ }

int RadosObject::RadosReadOp::prepare(optional_yield y, const DoutPrefixProvider* dpp)
{
  uint64_t obj_size;

  parent_op.conds.mod_ptr = params.mod_ptr;
  parent_op.conds.unmod_ptr = params.unmod_ptr;
  parent_op.conds.high_precision_time = params.high_precision_time;
  parent_op.conds.mod_zone_id = params.mod_zone_id;
  parent_op.conds.mod_pg_ver = params.mod_pg_ver;
  parent_op.conds.if_match = params.if_match;
  parent_op.conds.if_nomatch = params.if_nomatch;
  parent_op.params.lastmod = params.lastmod;
  parent_op.params.target_obj = params.target_obj;
  parent_op.params.part_num = params.part_num;
  parent_op.params.obj_size = &obj_size;
  parent_op.params.attrs = &source->get_attrs();

  int ret = parent_op.prepare(y, dpp);
  if (ret < 0)
    return ret;

  source->set_instance(parent_op.state.obj.key.instance);
  source->set_obj_size(obj_size);
  params.parts_count = parent_op.params.parts_count;

  return ret;
}

int RadosObject::RadosReadOp::read(int64_t ofs, int64_t end, bufferlist& bl, optional_yield y, const DoutPrefixProvider* dpp)
{
  return parent_op.read(ofs, end, bl, y, dpp);
}

int RadosObject::RadosReadOp::get_attr(const DoutPrefixProvider* dpp, const char* name, bufferlist& dest, optional_yield y)
{
  return parent_op.get_attr(dpp, name, dest, y);
}

std::unique_ptr<Object::DeleteOp> RadosObject::get_delete_op()
{
  return std::make_unique<RadosObject::RadosDeleteOp>(this);
}

RadosObject::RadosDeleteOp::RadosDeleteOp(RadosObject *_source) :
	source(_source),
	op_target(_source->store->getRados(),
		  _source->get_bucket()->get_info(),
		  _source->get_ctx(),
		  _source->get_obj()),
	parent_op(&op_target)
{ }

int RadosObject::RadosDeleteOp::delete_obj(const DoutPrefixProvider* dpp, optional_yield y, uint32_t flags)
{
  parent_op.params.bucket_owner = params.bucket_owner;
  parent_op.params.versioning_status = params.versioning_status;
  parent_op.params.obj_owner = params.obj_owner;
  parent_op.params.olh_epoch = params.olh_epoch;
  parent_op.params.marker_version_id = params.marker_version_id;
  parent_op.params.bilog_flags = params.bilog_flags;
  parent_op.params.remove_objs = params.remove_objs;
  parent_op.params.expiration_time = params.expiration_time;
  parent_op.params.unmod_since = params.unmod_since;
  parent_op.params.mtime = params.mtime;
  parent_op.params.high_precision_time = params.high_precision_time;
  parent_op.params.zones_trace = params.zones_trace;
  parent_op.params.abortmp = params.abortmp;
  parent_op.params.parts_accounted_size = params.parts_accounted_size;
  parent_op.params.null_verid = params.null_verid;
  if (params.objv_tracker) {
      parent_op.params.check_objv = params.objv_tracker->version_for_check();
  }

  int ret = parent_op.delete_obj(y, dpp, flags & FLAG_LOG_OP);
  if (ret < 0)
    return ret;

  result.delete_marker = parent_op.result.delete_marker;
  result.version_id = parent_op.result.version_id;

  return ret;
}

int RadosObject::delete_object(const DoutPrefixProvider* dpp,
			       optional_yield y,
			       uint32_t flags,
			       std::list<rgw_obj_index_key>* remove_objs,
			       RGWObjVersionTracker* objv)
{
  RGWRados::Object del_target(store->getRados(), bucket->get_info(), *rados_ctx, get_obj());
  RGWRados::Object::Delete del_op(&del_target);

  del_op.params.bucket_owner = bucket->get_info().owner;
  del_op.params.versioning_status = (flags & FLAG_PREVENT_VERSIONING)
                                    ? 0 : bucket->get_info().versioning_status();
  del_op.params.remove_objs = remove_objs;
  if (objv) {
      del_op.params.check_objv = objv->version_for_check();
  }

  return del_op.delete_obj(y, dpp, flags & FLAG_LOG_OP);
}

int RadosObject::copy_object(const ACLOwner& owner,
				const rgw_user& remote_user,
				req_info* info,
				const rgw_zone_id& source_zone,
				rgw::sal::Object* dest_object,
				rgw::sal::Bucket* dest_bucket,
				rgw::sal::Bucket* src_bucket,
				const rgw_placement_rule& dest_placement,
				ceph::real_time* src_mtime,
				ceph::real_time* mtime,
				const ceph::real_time* mod_ptr,
				const ceph::real_time* unmod_ptr,
				bool high_precision_time,
				const char* if_match,
				const char* if_nomatch,
				AttrsMod attrs_mod,
				bool copy_if_newer,
				Attrs& attrs,
				RGWObjCategory category,
				uint64_t olh_epoch,
				boost::optional<ceph::real_time> delete_at,
				std::string* version_id,
				std::string* tag,
				std::string* etag,
				void (*progress_cb)(off_t, void *),
				void* progress_data,
				const DoutPrefixProvider* dpp,
				optional_yield y)
{
  return store->getRados()->copy_obj(*rados_ctx,
				     *static_cast<RadosObject*>(dest_object)->rados_ctx,
				     owner,
				     remote_user,
				     info,
				     source_zone,
				     dest_object->get_obj(),
				     get_obj(),
				     dest_bucket->get_info(),
				     src_bucket->get_info(),
				     dest_placement,
				     src_mtime,
				     mtime,
				     mod_ptr,
				     unmod_ptr,
				     high_precision_time,
				     if_match,
				     if_nomatch,
				     static_cast<RGWRados::AttrsMod>(attrs_mod),
				     copy_if_newer,
				     attrs,
				     category,
				     olh_epoch,
				     (delete_at ? *delete_at : real_time()),
				     version_id,
				     tag,
				     etag,
				     progress_cb,
				     progress_data,
				     dpp,
				     y,
                                     dest_object->get_trace());
}

int RadosObject::RadosReadOp::iterate(const DoutPrefixProvider* dpp, int64_t ofs, int64_t end, RGWGetDataCB* cb, optional_yield y)
{
  return parent_op.iterate(dpp, ofs, end, cb, y);
}

int RadosObject::swift_versioning_restore(const ACLOwner& owner, const rgw_user& remote_user, bool& restored,
					  const DoutPrefixProvider* dpp, optional_yield y)
{
  rgw_obj obj = get_obj();
  return store->getRados()->swift_versioning_restore(*rados_ctx,
						     owner, remote_user,
						     bucket->get_info(),
						     obj,
						     restored,
						     dpp, y);
}

int RadosObject::swift_versioning_copy(const ACLOwner& owner, const rgw_user& remote_user,
                                       const DoutPrefixProvider* dpp, optional_yield y)
{
  return store->getRados()->swift_versioning_copy(*rados_ctx,
                                        owner, remote_user,
                                        bucket->get_info(),
                                        get_obj(),
                                        dpp,
                                        y);
}

int RadosMultipartUpload::cleanup_orphaned_parts(const DoutPrefixProvider *dpp,
                                                 CephContext *cct, optional_yield y,
                                                 const rgw_obj& obj,
                                                 list<rgw_obj_index_key>& remove_objs,
                                                 prefix_map_t& processed_prefixes)
{
  bool truncated;
  int ret;
  int max_parts = 1000;
  int marker = 0;
  cls_rgw_obj_chain chain;

  do {
    ret = list_parts(dpp, cct, max_parts, marker, &marker, &truncated, y);

    if (ret < 0) {
      ldpp_dout(dpp, 20) << __func__ << ": RadosMultipartUpload::list_parts returned " << ret << dendl;
      return (ret == -ENOENT) ? -ERR_NO_SUCH_UPLOAD : ret;
    }

    for (auto part_it = parts.begin(); part_it != parts.end(); ++part_it) {
      RadosMultipartPart* part = dynamic_cast<RadosMultipartPart*>(part_it->second.get());

      auto& part_prefixes = processed_prefixes[part->info.num];

      if (!part->info.manifest.empty()) {
        auto manifest_prefix = part->info.manifest.get_prefix();
        if (not manifest_prefix.empty() && part_prefixes.find(manifest_prefix) == part_prefixes.end()) {
          store->getRados()->update_gc_chain(dpp, obj, part->info.manifest, &chain);

          RGWObjManifest::obj_iterator oiter = part->info.manifest.obj_begin(dpp);
          if (oiter != part->info.manifest.obj_end(dpp)) {
            rgw_raw_obj raw_head = oiter.get_location().get_raw_obj(store->getRados());

            rgw_obj head_obj;
            RGWSI_Tier_RADOS::raw_obj_to_obj(bucket->get_key(), raw_head, &head_obj);

            rgw_obj_index_key remove_key;
            head_obj.key.get_index_key(&remove_key);
            remove_objs.push_back(remove_key);
          }
        }
      }
      cleanup_part_history(dpp, y, part, remove_objs, part_prefixes);
    }
  } while (truncated);

  if (store->getRados()->get_gc() == nullptr) {
    //Delete objects inline if gc hasn't been initialised (in case when bypass gc is specified)
    store->getRados()->delete_objs_inline(dpp, chain, mp_obj.get_upload_id(), y);
  } else {
    /* use upload id as tag and do it synchronously */
    auto [ret, leftover_chain] = store->getRados()->send_chain_to_gc(chain, mp_obj.get_upload_id(), y);
    if (ret < 0 && leftover_chain) {
      ldpp_dout(dpp, 5) << __func__ << ": gc->send_chain() returned " << ret << dendl;
      if (ret == -ENOENT) {
        return -ERR_NO_SUCH_UPLOAD;
      }
      //Delete objects inline if send chain to gc fails
      store->getRados()->delete_objs_inline(dpp, *leftover_chain, mp_obj.get_upload_id(), y);
    }
  }
  return 0;
}

int RadosMultipartUpload::cleanup_part_history(const DoutPrefixProvider* dpp,
                                               optional_yield y,
                                               RadosMultipartPart *part,
                                               list<rgw_obj_index_key>& remove_objs,
                                               boost::container::flat_set<std::string>& processed_prefixes)
{
  cls_rgw_obj_chain chain;
  for (auto& ppfx : part->get_past_prefixes()) {
    auto [it, inserted] = processed_prefixes.emplace(ppfx);
    if (!inserted) {
      continue; // duplicate
    }

    rgw_obj past_obj;
    past_obj.init_ns(bucket->get_key(), ppfx + "." + std::to_string(part->info.num), mp_ns);
    rgw_obj_index_key past_key;
    past_obj.key.get_index_key(&past_key);
    // Remove past upload part objects from index, too.
    remove_objs.push_back(past_key);

    RGWObjManifest manifest = part->get_manifest();
    manifest.set_prefix(ppfx);
    RGWObjManifest::obj_iterator miter = manifest.obj_begin(dpp);
    for (; miter != manifest.obj_end(dpp); ++miter) {
      rgw_raw_obj raw_part_obj = miter.get_location().get_raw_obj(store->getRados());
      cls_rgw_obj_key part_key(raw_part_obj.oid);
      chain.push_obj(raw_part_obj.pool.to_str(), part_key, raw_part_obj.loc);
    }
  }
  if (store->getRados()->get_gc() == nullptr) {
    // Delete objects inline if gc hasn't been initialised (in case when bypass gc is specified)
    store->getRados()->delete_objs_inline(dpp, chain, mp_obj.get_upload_id(), y);
  } else {
    // use upload id as tag and do it synchronously
    auto [ret, leftover_chain] = store->getRados()->send_chain_to_gc(chain, mp_obj.get_upload_id(), y);
    if (ret < 0 && leftover_chain) {
      ldpp_dout(dpp, 5) << __func__ << ": gc->send_chain() returned " << ret << dendl;
      if (ret == -ENOENT) {
        return -ERR_NO_SUCH_UPLOAD;
      }
      // Delete objects inline if send chain to gc fails
      store->getRados()->delete_objs_inline(dpp, *leftover_chain, mp_obj.get_upload_id(), y);
    }
  }
  return 0;
}


int RadosMultipartUpload::abort(const DoutPrefixProvider *dpp, CephContext *cct, optional_yield y)
{
  std::unique_ptr<rgw::sal::Object> meta_obj = get_meta_obj();
  meta_obj->set_in_extra_data(true);
  meta_obj->set_hash_source(mp_obj.get_key());
  cls_rgw_obj_chain chain;
  list<rgw_obj_index_key> remove_objs;
  bool truncated;
  int marker = 0;
  int ret;
  uint64_t parts_accounted_size = 0;

  prefix_map_t processed_prefixes;

  static constexpr auto MAX_DELETE_RETRIES = 15u;
  for (auto i = 0u; i < MAX_DELETE_RETRIES; i++) {
    ret = meta_obj->get_obj_attrs(y, dpp);
    if (ret < 0) {
      ldpp_dout(dpp, 0) << __func__ << ": ERROR: failed to get obj attrs, obj=" << meta_obj
                        << " ret=" << ret << dendl;
      return (ret == -ENOENT) ? -ERR_NO_SUCH_UPLOAD : ret;
    }

    RGWObjVersionTracker objv_tracker = meta_obj->get_version_tracker();

    do {
      ret = list_parts(dpp, cct, 1000, marker, &marker, &truncated, y);
      if (ret < 0) {
        ldpp_dout(dpp, 20) << __func__ << ": RadosMultipartUpload::list_parts returned " << ret << dendl;
        return (ret == -ENOENT) ? -ERR_NO_SUCH_UPLOAD : ret;
      }

      for (auto part_it = parts.begin(); part_it != parts.end(); ++part_it) {
        RadosMultipartPart* obj_part = dynamic_cast<RadosMultipartPart*>(part_it->second.get());

        if (obj_part->info.manifest.empty()) {
          std::unique_ptr<rgw::sal::Object> obj = bucket->get_object(
            rgw_obj_key(obj_part->oid, std::string(), RGW_OBJ_NS_MULTIPART));
          obj->set_hash_source(mp_obj.get_key());
          ret = obj->delete_object(dpp, y, 0, nullptr, nullptr);
          if (ret < 0 && ret != -ENOENT)
            return ret;
        } else {
          auto manifest_prefix = obj_part->info.manifest.get_prefix();
          auto [it, inserted] = processed_prefixes.emplace(obj_part->info.num, boost::container::flat_set<std::string>{});
          if (not manifest_prefix.empty()) {
            if (it->second.find(manifest_prefix) != it->second.end()) {
              continue;
            }
            it->second.emplace(manifest_prefix);
          }

          auto target = meta_obj->get_obj();
          store->getRados()->update_gc_chain(dpp, target, obj_part->info.manifest, &chain);
          RGWObjManifest::obj_iterator oiter = obj_part->info.manifest.obj_begin(dpp);
          if (oiter != obj_part->info.manifest.obj_end(dpp)) {
            std::unique_ptr<rgw::sal::Object> head = bucket->get_object(rgw_obj_key());
            rgw_raw_obj raw_head = oiter.get_location().get_raw_obj(store->getRados());
            dynamic_cast<rgw::sal::RadosObject*>(head.get())->raw_obj_to_obj(raw_head);

            rgw_obj_index_key key;
            head->get_key().get_index_key(&key);
            remove_objs.push_back(key);

            cleanup_part_history(dpp, null_yield, obj_part, remove_objs, it->second);
          }
        }
        parts_accounted_size += obj_part->info.accounted_size;
      }
    } while (truncated);

    if (store->getRados()->get_gc() == nullptr) {
      //Delete objects inline if gc hasn't been initialised (in case when bypass gc is specified)
      store->getRados()->delete_objs_inline(dpp, chain, mp_obj.get_upload_id(), y);
    } else {
      /* use upload id as tag and do it synchronously */
      auto [ret, leftover_chain] = store->getRados()->send_chain_to_gc(chain, mp_obj.get_upload_id(), y);
      if (ret < 0 && leftover_chain) {
        ldpp_dout(dpp, 5) << __func__ << ": gc->send_chain() returned " << ret << dendl;
        if (ret == -ENOENT) {
          return -ERR_NO_SUCH_UPLOAD;
        }
        //Delete objects inline if send chain to gc fails
        store->getRados()->delete_objs_inline(dpp, *leftover_chain, mp_obj.get_upload_id(), y);
      }
    }

    std::unique_ptr<rgw::sal::Object::DeleteOp> del_op = meta_obj->get_delete_op();
    del_op->params.bucket_owner = bucket->get_info().owner;
    del_op->params.versioning_status = 0;
    if (!remove_objs.empty()) {
      del_op->params.remove_objs = &remove_objs;
    }

    del_op->params.abortmp = true;
    del_op->params.parts_accounted_size = parts_accounted_size;
    del_op->params.objv_tracker = &objv_tracker;

    // and also remove the metadata obj
    ret = del_op->delete_obj(dpp, y, 0);
    if (ret != -ECANCELED) {
      if (ret < 0) {
        ldpp_dout(dpp, 20) << __func__ << ": del_op.delete_obj returned " << ret << dendl;
      }
      break;
    }
    ldpp_dout(dpp, 20) << "deleting meta_obj is cancelled due to mismatch cls_version: " << objv_tracker << dendl;
    chain.objs.clear();
    marker = 0;
  }

  return (ret == -ENOENT) ? -ERR_NO_SUCH_UPLOAD : ret;
}

std::unique_ptr<rgw::sal::Object> RadosMultipartUpload::get_meta_obj()
{
  return bucket->get_object(rgw_obj_key(get_meta(), string(), mp_ns));
}

int RadosMultipartUpload::init(const DoutPrefixProvider *dpp, optional_yield y, ACLOwner& owner, rgw_placement_rule& dest_placement, rgw::sal::Attrs& attrs)
{
  int ret;
  std::string oid = mp_obj.get_key();
  RGWObjectCtx obj_ctx(store);
  const req_context rctx{dpp, y, nullptr};

  do {
    char buf[33];
    string tmp_obj_name;
    std::unique_ptr<rgw::sal::Object> obj;
    gen_rand_alphanumeric(store->ctx(), buf, sizeof(buf) - 1);
    std::string upload_id = MULTIPART_UPLOAD_ID_PREFIX; /* v2 upload id */
    upload_id.append(buf);

    mp_obj.init(oid, upload_id);
    tmp_obj_name = mp_obj.get_meta();

    obj = bucket->get_object(rgw_obj_key(tmp_obj_name, string(), mp_ns));
    // the meta object will be indexed with 0 size, we c
    obj->set_in_extra_data(true);
    obj->set_hash_source(oid);


    const RGWBucketInfo& bucket_info = obj->get_bucket()->get_info();

    RGWRados::Object op_target(store->getRados(), bucket_info,
			       obj_ctx, obj->get_obj());
    RGWRados::Object::Write obj_op(&op_target);

    op_target.set_versioning_disabled(true); /* no versioning for multipart meta */
    obj_op.meta.owner = owner;
    obj_op.meta.bucket_owner = bucket_info.owner;
    obj_op.meta.category = RGWObjCategory::MultiMeta;
    obj_op.meta.flags = PUT_OBJ_CREATE_EXCL;
    obj_op.meta.mtime = &mtime;

    multipart_upload_info upload_info;
    upload_info.dest_placement = dest_placement;
    upload_info.cksum_type = cksum_type;

    if (obj_legal_hold) {
      upload_info.obj_legal_hold_exist = true;
      upload_info.obj_legal_hold = (*obj_legal_hold);
    }
    if (obj_retention) {
      upload_info.obj_retention_exist = true;
      upload_info.obj_retention = (*obj_retention);
    }

    bufferlist bl;
    encode(upload_info, bl);
    obj_op.meta.data = &bl;

    ret = obj_op.write_meta(bl.length(), 0, attrs, rctx, get_trace(), false);
  } while (ret == -EEXIST);

  return ret;
}

int RadosMultipartUpload::list_parts(const DoutPrefixProvider *dpp, CephContext *cct,
				     int num_parts, int marker,
				     int *next_marker, bool *truncated, optional_yield y,
				     bool assume_unsorted)
{
  map<string, bufferlist> parts_map;
  map<string, bufferlist>::iterator iter;

  rgw_obj_key key(get_meta(), std::string(), RGW_OBJ_NS_MULTIPART);
  rgw_obj obj(bucket->get_key(), key);
  obj.in_extra_data = true;

  rgw_raw_obj raw_obj;
  store->getRados()->obj_to_raw(bucket->get_placement_rule(), obj, &raw_obj);
  auto sysobj = store->svc()->sysobj->get_obj(raw_obj);

  bool sorted_omap = is_v2_upload_id(get_upload_id()) && !assume_unsorted;

  parts.clear();

  int ret;
  if (sorted_omap) {
    string p;
    p = "part.";
    char buf[32];

    snprintf(buf, sizeof(buf), "%08d", marker);
    p.append(buf);

    ret = sysobj.omap().get_vals(dpp, p, num_parts + 1, &parts_map,
                                 nullptr, y);
  } else {
    ret = sysobj.omap().get_all(dpp, &parts_map, y);
  }
  if (ret < 0) {
    return ret;
  }

  int i;
  int last_num = 0;

  uint32_t expected_next = marker + 1;

  for (i = 0, iter = parts_map.begin();
       (i < num_parts || !sorted_omap) && iter != parts_map.end();
       ++iter, ++i) {
    bufferlist& bl = iter->second;
    auto bli = bl.cbegin();
    std::unique_ptr<RadosMultipartPart> part = std::make_unique<RadosMultipartPart>();
    try {
      decode(part->info, bli);
    } catch (buffer::error& err) {
      ldpp_dout(dpp, 0) << "ERROR: could not part info, caught buffer::error" <<
	dendl;
      return -EIO;
    }
    if (sorted_omap) {
      if (part->info.num != expected_next) {
        /* ouch, we expected a specific part num here, but we got a
         * different one. Either a part is missing, or it could be a
         * case of mixed rgw versions working on the same upload,
         * where one gateway doesn't support correctly sorted omap
         * keys for multipart upload just assume data is unsorted.
         */
        return list_parts(dpp, cct, num_parts, marker, next_marker, truncated, y, true);
      }
      expected_next++;
    }
    if (sorted_omap ||
      (int)part->info.num > marker) {
      last_num = part->info.num;
      parts[part->info.num] = std::move(part);
    }
  }

  if (sorted_omap) {
    if (truncated) {
      *truncated = (iter != parts_map.end());
    }
  } else {
    /* rebuild a map with only num_parts entries */
    std::map<uint32_t, std::unique_ptr<MultipartPart>> new_parts;
    std::map<uint32_t, std::unique_ptr<MultipartPart>>::iterator piter;
    for (i = 0, piter = parts.begin();
	 i < num_parts && piter != parts.end();
	 ++i, ++piter) {
      last_num = piter->first;
      new_parts[piter->first] = std::move(piter->second);
    }

    if (truncated) {
      *truncated = (piter != parts.end());
    }

    parts.swap(new_parts);
  }

  if (next_marker) {
    *next_marker = last_num;
  }

  return 0;
}

int RadosMultipartUpload::complete(const DoutPrefixProvider *dpp,
				   optional_yield y, CephContext* cct,
				   map<int, string>& part_etags,
				   list<rgw_obj_index_key>& remove_objs,
				   uint64_t& accounted_size, bool& compressed,
				   RGWCompressionInfo& cs_info, off_t& ofs,
				   std::string& tag, ACLOwner& owner,
				   uint64_t olh_epoch,
				   rgw::sal::Object* target_obj,
				   prefix_map_t& processed_prefixes)
{
  char final_etag[CEPH_CRYPTO_MD5_DIGESTSIZE];
  char final_etag_str[CEPH_CRYPTO_MD5_DIGESTSIZE * 2 + 16];
  std::string etag;
  bufferlist etag_bl;
  MD5 hash;
  // Allow use of MD5 digest in FIPS mode for non-cryptographic purposes
  hash.SetFlags(EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
  bool truncated;
  int ret;

  int total_parts = 0;
  int handled_parts = 0;
  int max_parts = 1000;
  int marker = 0;
  uint64_t min_part_size = cct->_conf->rgw_multipart_min_part_size;
  auto etags_iter = part_etags.begin();
  rgw::sal::Attrs& attrs = target_obj->get_attrs();

  do {
    ret = list_parts(dpp, cct, max_parts, marker, &marker, &truncated, y);
    if (ret == -ENOENT) {
      ret = -ERR_NO_SUCH_UPLOAD;
    }
    if (ret < 0)
      return ret;

    total_parts += parts.size();
    if (!truncated && total_parts != (int)part_etags.size()) {
      ldpp_dout(dpp, 0) << "NOTICE: total parts mismatch: have: " << total_parts
		       << " expected: " << part_etags.size() << dendl;
      ret = -ERR_INVALID_PART;
      return ret;
    }

    for (auto obj_iter = parts.begin(); etags_iter != part_etags.end() && obj_iter != parts.end(); ++etags_iter, ++obj_iter, ++handled_parts) {
      RadosMultipartPart* part = dynamic_cast<rgw::sal::RadosMultipartPart*>(obj_iter->second.get());
      uint64_t part_size = part->get_size();
      if (handled_parts < (int)part_etags.size() - 1 &&
          part_size < min_part_size) {
        ret = -ERR_TOO_SMALL;
        return ret;
      }

      char petag[CEPH_CRYPTO_MD5_DIGESTSIZE];
      if (etags_iter->first != (int)obj_iter->first) {
        ldpp_dout(dpp, 0) << "NOTICE: parts num mismatch: next requested: "
			 << etags_iter->first << " next uploaded: "
			 << obj_iter->first << dendl;
        ret = -ERR_INVALID_PART;
        return ret;
      }
      string part_etag = rgw_string_unquote(etags_iter->second);
      if (part_etag.compare(part->get_etag()) != 0) {
        ldpp_dout(dpp, 0) << "NOTICE: etag mismatch: part: " << etags_iter->first
			 << " etag: " << etags_iter->second << dendl;
        ret = -ERR_INVALID_PART;
        return ret;
      }

      hex_to_buf(part->get_etag().c_str(), petag,
		CEPH_CRYPTO_MD5_DIGESTSIZE);
      hash.Update((const unsigned char *)petag, sizeof(petag));

      RGWUploadPartInfo& obj_part = part->info;

      /* update manifest for part */
      string oid = mp_obj.get_part(part->info.num);
      rgw_obj src_obj;
      src_obj.init_ns(bucket->get_key(), oid, mp_ns);

      auto [it, inserted] = processed_prefixes.emplace(part->info.num, boost::container::flat_set<std::string>{});

      if (obj_part.manifest.empty()) {
        ldpp_dout(dpp, 0) << "ERROR: empty manifest for object part: obj="
			 << src_obj << dendl;
        ret = -ERR_INVALID_PART;
        return ret;
      } else {
        manifest.append(dpp, obj_part.manifest, store->svc()->zone->get_zonegroup(), store->svc()->zone->get_zone_params());
        auto manifest_prefix = part->info.manifest.get_prefix();
        if (not manifest_prefix.empty()) {
          // It has an explicit prefix. Override the default one.
          src_obj.init_ns(bucket->get_key(), manifest_prefix + "." + std::to_string(part->info.num), mp_ns);
	  it->second.emplace(manifest_prefix);
        }
      }

      bool part_compressed = (obj_part.cs_info.compression_type != "none");
      if ((handled_parts > 0) &&
          ((part_compressed != compressed) ||
           (cs_info.compression_type != obj_part.cs_info.compression_type) ||
           (cs_info.compressor_message.has_value() &&
           (cs_info.compressor_message != obj_part.cs_info.compressor_message)))) {
          ldpp_dout(dpp, 0) << "ERROR: compression type or compressor message was changed during multipart upload ("
                            << cs_info.compression_type << ">>" << obj_part.cs_info.compression_type << "), "
                            << cs_info.compressor_message << ">>" << obj_part.cs_info.compressor_message << ") "
                            << dendl;
          ret = -ERR_INVALID_PART;
          return ret;
      }

      if (part_compressed) {
        int64_t new_ofs; // offset in compression data for new part
        if (cs_info.blocks.size() > 0)
          new_ofs = cs_info.blocks.back().new_ofs + cs_info.blocks.back().len;
        else
          new_ofs = 0;
        for (const auto& block : obj_part.cs_info.blocks) {
          compression_block cb;
          cb.old_ofs = block.old_ofs + cs_info.orig_size;
          cb.new_ofs = new_ofs;
          cb.len = block.len;
          cs_info.blocks.push_back(cb);
          new_ofs = cb.new_ofs + cb.len;
        }
        if (!compressed) {
          cs_info.compression_type = obj_part.cs_info.compression_type;
          if (obj_part.cs_info.compressor_message.has_value())
            cs_info.compressor_message = obj_part.cs_info.compressor_message;
        }
        cs_info.orig_size += obj_part.cs_info.orig_size;
        compressed = true;
      }

      rgw_obj_index_key remove_key;
      src_obj.key.get_index_key(&remove_key);

      remove_objs.push_back(remove_key);

      cleanup_part_history(dpp, y, part, remove_objs, it->second);

      ofs += obj_part.size;
      accounted_size += obj_part.accounted_size;
    }
  } while (truncated);
  hash.Final((unsigned char *)final_etag);

  buf_to_hex((unsigned char *)final_etag, sizeof(final_etag), final_etag_str);
  snprintf(&final_etag_str[CEPH_CRYPTO_MD5_DIGESTSIZE * 2],
	   sizeof(final_etag_str) - CEPH_CRYPTO_MD5_DIGESTSIZE * 2,
           "-%lld", (long long)part_etags.size());
  etag = final_etag_str;
  ldpp_dout(dpp, 10) << "calculated etag: " << etag << dendl;

  etag_bl.append(etag);

  attrs[RGW_ATTR_ETAG] = etag_bl;

  rgw_placement_rule* ru;
  ru = &placement;
  rgw::sal::Attrs mpu_attrs; // don't overwrite the target object attrs we are updating
  ret = RadosMultipartUpload::get_info(dpp, y, &ru, &mpu_attrs);

  if (upload_information.obj_retention_exist) {
    bufferlist obj_retention_bl;
    upload_information.obj_retention.encode(obj_retention_bl);
    attrs[RGW_ATTR_OBJECT_RETENTION] = std::move(obj_retention_bl);
  }
  if (upload_information.obj_legal_hold_exist) {
    bufferlist obj_legal_hold_bl;
    upload_information.obj_legal_hold.encode(obj_legal_hold_bl);
    attrs[RGW_ATTR_OBJECT_LEGAL_HOLD] = std::move(obj_legal_hold_bl);
  }

  if (compressed) {
    // write compression attribute to full object
    bufferlist tmp;
    encode(cs_info, tmp);
    attrs[RGW_ATTR_COMPRESSION] = tmp;
  }

  target_obj->set_atomic();

  const RGWBucketInfo& bucket_info = target_obj->get_bucket()->get_info();
  RGWRados::Object op_target(store->getRados(), bucket_info,
			     dynamic_cast<RadosObject*>(target_obj)->get_ctx(),
			     target_obj->get_obj());
  RGWRados::Object::Write obj_op(&op_target);

  obj_op.meta.manifest = &manifest;
  obj_op.meta.remove_objs = &remove_objs;

  obj_op.meta.ptag = &tag; /* use req_id as operation tag */
  obj_op.meta.owner = owner;
  obj_op.meta.bucket_owner = bucket_info.owner;
  obj_op.meta.flags = PUT_OBJ_CREATE;
  obj_op.meta.modify_tail = true;
  obj_op.meta.completeMultipart = true;
  obj_op.meta.olh_epoch = olh_epoch;

  const req_context rctx{dpp, y, nullptr};
  ret = obj_op.write_meta(ofs, accounted_size, attrs, rctx, get_trace());
  if (ret < 0)
    return ret;

  return ret;
}

int RadosMultipartUpload::get_info(const DoutPrefixProvider *dpp, optional_yield y, rgw_placement_rule** rule, rgw::sal::Attrs* attrs)
{
  if (!rule && !attrs) {
    return 0;
  }

  /* Handle caching */
  if (rule) {
    if (!placement.empty()) {
      *rule = &placement;
      if (!attrs) {
	/* Don't need attrs, done */
	return 0;
      }
    } else {
      *rule = nullptr;
    }
  }

  if (attrs) {
    if (!cached_attrs.empty()) {
      *attrs = cached_attrs;
      if (!rule || *rule != nullptr)
        return 0;
    }
  }

  /* We need either attributes or placement, so we need a read */
  std::unique_ptr<rgw::sal::Object> meta_obj;
  meta_obj = get_meta_obj();
  meta_obj->set_in_extra_data(true);

  multipart_upload_info upload_info;
  bufferlist headbl;

  /* Read the obj head which contains the multipart_upload_info */
  std::unique_ptr<rgw::sal::Object::ReadOp> read_op = meta_obj->get_read_op();
  meta_obj->set_prefetch_data();

  int ret = read_op->prepare(y, dpp);
  if (ret < 0) {
    if (ret == -ENOENT) {
      return -ERR_NO_SUCH_UPLOAD;
    }
    return ret;
  }

  /* Cache attrs filled in by prepare */
  cached_attrs = meta_obj->get_attrs();

  extract_span_context(meta_obj->get_attrs(), trace_ctx);

  if (attrs) {
    *attrs = cached_attrs;
    if (!rule || *rule != nullptr) {
      /* placement was cached; don't actually read */
      return 0;
    }
  }

  /* Now read the placement from the head */
  ret = read_op->read(0, store->ctx()->_conf->rgw_max_chunk_size, headbl, y, dpp);
  if (ret < 0) {
    if (ret == -ENOENT) {
      return -ERR_NO_SUCH_UPLOAD;
    }
    return ret;
  }

  if (headbl.length() <= 0) {
    return -ERR_NO_SUCH_UPLOAD;
  }

  /* Decode multipart_upload_info */
  auto hiter = headbl.cbegin();
  try {
    decode(upload_info, hiter);
  } catch (buffer::error& err) {
    ldpp_dout(dpp, 0) << "ERROR: failed to decode multipart upload info" << dendl;
    return -EIO;
  }
  cksum_type = upload_info.cksum_type;
  placement = upload_info.dest_placement;
  upload_information = upload_info;
  *rule = &placement;

  return 0;
}

std::unique_ptr<Writer> RadosMultipartUpload::get_writer(
				  const DoutPrefixProvider *dpp,
				  optional_yield y,
				  rgw::sal::Object* obj,
				  const ACLOwner& owner,
				  const rgw_placement_rule *ptail_placement_rule,
				  uint64_t part_num,
				  const std::string& part_num_str)
{
  RGWBucketInfo& bucket_info = obj->get_bucket()->get_info();
  RGWObjectCtx& obj_ctx = static_cast<RadosObject*>(obj)->get_ctx();
  auto aio = rgw::make_throttle(store->ctx()->_conf->rgw_put_obj_min_window_size, y);
  return std::make_unique<RadosMultipartWriter>(dpp, y, get_upload_id(),
				 bucket_info, obj_ctx,
				 obj->get_obj(), store, std::move(aio), owner,
				 ptail_placement_rule, part_num, part_num_str, obj->get_trace());
}

MPRadosSerializer::MPRadosSerializer(const DoutPrefixProvider *dpp, RadosStore* store, RadosObject* obj, const std::string& lock_name) :
  lock(lock_name)
{
  rgw_pool meta_pool;
  rgw_raw_obj raw_obj;

  obj->get_raw_obj(&raw_obj);
  oid = raw_obj.oid;
  store->getRados()->get_obj_data_pool(obj->get_bucket()->get_placement_rule(),
				       obj->get_obj(), &meta_pool);
  store->getRados()->open_pool_ctx(dpp, meta_pool, ioctx, true, true);
}

int MPRadosSerializer::try_lock(const DoutPrefixProvider *dpp, utime_t dur, optional_yield y)
{
  op.assert_exists();
  lock.set_duration(dur);
  lock.lock_exclusive(&op);
  int ret = rgw_rados_operate(dpp, ioctx, oid, &op, y);
  if (! ret) {
    locked = true;
  }
  return ret;
}

LCRadosSerializer::LCRadosSerializer(RadosStore* store, const std::string& _oid, const std::string& lock_name, const std::string& cookie) :
  StoreLCSerializer(_oid),
  lock(lock_name)
{
  ioctx = &store->getRados()->lc_pool_ctx;
  lock.set_cookie(cookie);
}

int LCRadosSerializer::try_lock(const DoutPrefixProvider *dpp, utime_t dur, optional_yield y)
{
  lock.set_duration(dur);
  return lock.lock_exclusive(ioctx, oid);
}

int RadosLifecycle::get_entry(const DoutPrefixProvider* dpp, optional_yield y,
                              const std::string& oid, const std::string& marker,
			      LCEntry& entry)
{
  librados::ObjectReadOperation op;
  bufferlist bl;
  cls_rgw_lc_get_entry(op, marker, bl);

  auto& ioctx = *store->getRados()->get_lc_pool_ctx();
  int ret = rgw_rados_operate(dpp, ioctx, oid, &op, nullptr, y);
  if (ret < 0) {
    return ret;
  }

  cls_rgw_lc_entry cls_entry;
  ret = cls_rgw_lc_get_entry_decode(bl, cls_entry);
  if (ret < 0) {
    return ret;
  }

  entry.bucket = std::move(cls_entry.bucket);
  entry.start_time = cls_entry.start_time;
  entry.status = cls_entry.status;
  return 0;
}

int RadosLifecycle::get_next_entry(const DoutPrefixProvider* dpp, optional_yield y,
                                   const std::string& oid, const std::string& marker,
				   LCEntry& entry)
{
  librados::ObjectReadOperation op;
  bufferlist bl;
  cls_rgw_lc_get_next_entry(op, marker, bl);

  auto& ioctx = *store->getRados()->get_lc_pool_ctx();
  int ret = rgw_rados_operate(dpp, ioctx, oid, &op, nullptr, y);
  if (ret < 0) {
    return ret;
  }

  cls_rgw_lc_entry cls_entry;
  ret = cls_rgw_lc_get_next_entry_decode(bl, cls_entry);
  if (ret < 0) {
    return ret;
  }

  entry.bucket = std::move(cls_entry.bucket);
  entry.start_time = cls_entry.start_time;
  entry.status = cls_entry.status;
  return 0;
}

int RadosLifecycle::set_entry(const DoutPrefixProvider* dpp, optional_yield y,
                              const std::string& oid, const LCEntry& entry)
{
  cls_rgw_lc_entry cls_entry;

  cls_entry.bucket = entry.bucket;
  cls_entry.start_time = entry.start_time;
  cls_entry.status = entry.status;

  librados::ObjectWriteOperation op;
  cls_rgw_lc_set_entry(op, cls_entry);

  auto& ioctx = *store->getRados()->get_lc_pool_ctx();
  return rgw_rados_operate(dpp, ioctx, oid, &op, y);
}

int RadosLifecycle::list_entries(const DoutPrefixProvider* dpp, optional_yield y,
                                 const std::string& oid, const std::string& marker,
                                 uint32_t max_entries, std::vector<LCEntry>& entries)
{
  entries.clear();

  librados::ObjectReadOperation op;
  bufferlist bl;
  cls_rgw_lc_list(op, marker, max_entries, bl);

  auto& ioctx = *store->getRados()->get_lc_pool_ctx();
  int ret = rgw_rados_operate(dpp, ioctx, oid, &op, nullptr, y);
  if (ret < 0) {
    return ret;
  }

  vector<cls_rgw_lc_entry> cls_entries;
  ret = cls_rgw_lc_list_decode(bl, cls_entries);
  if (ret < 0) {
    return ret;
  }

  for (auto& entry : cls_entries) {
    entries.push_back(LCEntry{entry.bucket, entry.start_time, entry.status});
  }

  return ret;
}

int RadosLifecycle::rm_entry(const DoutPrefixProvider* dpp, optional_yield y,
                             const std::string& oid, const LCEntry& entry)
{
  cls_rgw_lc_entry cls_entry;
  cls_entry.bucket = entry.bucket;
  cls_entry.start_time = entry.start_time;
  cls_entry.status = entry.status;

  librados::ObjectWriteOperation op;
  cls_rgw_lc_rm_entry(op, cls_entry);

  auto& ioctx = *store->getRados()->get_lc_pool_ctx();
  return rgw_rados_operate(dpp, ioctx, oid, &op, y);
}

int RadosLifecycle::get_head(const DoutPrefixProvider* dpp, optional_yield y,
                             const std::string& oid, LCHead& head)
{
  librados::ObjectReadOperation op;
  bufferlist bl;
  cls_rgw_lc_get_head(op, bl);

  auto& ioctx = *store->getRados()->get_lc_pool_ctx();
  int ret = rgw_rados_operate(dpp, ioctx, oid, &op, nullptr, y);
  if (ret < 0) {
    return ret;
  }

  cls_rgw_lc_obj_head cls_head;
  ret = cls_rgw_lc_get_head_decode(bl, cls_head);
  if (ret < 0) {
    return ret;
  }

  head.start_date = cls_head.start_date;
  head.shard_rollover_date = cls_head.shard_rollover_date;
  head.marker = std::move(cls_head.marker);
  return 0;
}

int RadosLifecycle::put_head(const DoutPrefixProvider* dpp, optional_yield y,
                             const std::string& oid, const LCHead& head)
{
  cls_rgw_lc_obj_head cls_head;

  cls_head.marker = head.marker;
  cls_head.start_date = head.start_date;
  cls_head.shard_rollover_date = head.shard_rollover_date;

  librados::ObjectWriteOperation op;
  cls_rgw_lc_put_head(op, cls_head);

  auto& ioctx = *store->getRados()->get_lc_pool_ctx();
  return rgw_rados_operate(dpp, ioctx, oid, &op, y);
}

std::unique_ptr<LCSerializer> RadosLifecycle::get_serializer(const std::string& lock_name,
							     const std::string& oid,
							     const std::string& cookie)
{
  return std::make_unique<LCRadosSerializer>(store, oid, lock_name, cookie);
}

int RadosNotification::publish_reserve(const DoutPrefixProvider *dpp, RGWObjTags* obj_tags)
{
  return rgw::notify::publish_reserve(dpp, *store->svc()->site, event_types, res, obj_tags);
}

int RadosNotification::publish_commit(const DoutPrefixProvider* dpp, uint64_t size,
				     const ceph::real_time& mtime, const std::string& etag, const std::string& version)
{
  return rgw::notify::publish_commit(obj, size, mtime, etag, version, res, dpp);
}

int RadosAtomicWriter::prepare(optional_yield y)
{
  return processor.prepare(y);
}

int RadosAtomicWriter::process(bufferlist&& data, uint64_t offset)
{
  return processor.process(std::move(data), offset);
}

int RadosAtomicWriter::complete(size_t accounted_size, const std::string& etag,
                       ceph::real_time *mtime, ceph::real_time set_mtime,
                       std::map<std::string, bufferlist>& attrs,
		       const std::optional<rgw::cksum::Cksum>& cksum,
                       ceph::real_time delete_at,
                       const char *if_match, const char *if_nomatch,
                       const std::string *user_data,
                       rgw_zone_set *zones_trace, bool *canceled,
                       const req_context& rctx,
                       uint32_t flags)
{
  return processor.complete(accounted_size, etag, mtime, set_mtime, attrs,
			    cksum, delete_at, if_match, if_nomatch,
			    user_data, zones_trace, canceled, rctx, flags);
}

int RadosAppendWriter::prepare(optional_yield y)
{
  return processor.prepare(y);
}

int RadosAppendWriter::process(bufferlist&& data, uint64_t offset)
{
  return processor.process(std::move(data), offset);
}

int RadosAppendWriter::complete(size_t accounted_size, const std::string& etag,
                       ceph::real_time *mtime, ceph::real_time set_mtime,
                       std::map<std::string, bufferlist>& attrs,
		       const std::optional<rgw::cksum::Cksum>& cksum,
                       ceph::real_time delete_at,
                       const char *if_match, const char *if_nomatch,
                       const std::string *user_data,
                       rgw_zone_set *zones_trace, bool *canceled,
                       const req_context& rctx,
                       uint32_t flags)
{
  return processor.complete(accounted_size, etag, mtime, set_mtime, attrs,
			    cksum, delete_at, if_match, if_nomatch,
			    user_data, zones_trace, canceled, rctx, flags);
}

int RadosMultipartWriter::prepare(optional_yield y)
{
  return processor.prepare(y);
}

int RadosMultipartWriter::process(bufferlist&& data, uint64_t offset)
{
  return processor.process(std::move(data), offset);
}

int RadosMultipartWriter::complete(
		       size_t accounted_size,
		       const std::string& etag,
                       ceph::real_time *mtime, ceph::real_time set_mtime,
                       std::map<std::string, bufferlist>& attrs,
		       const std::optional<rgw::cksum::Cksum>& cksum,
                       ceph::real_time delete_at,
                       const char *if_match, const char *if_nomatch,
                       const std::string *user_data,
                       rgw_zone_set *zones_trace, bool *canceled,
                       const req_context& rctx,
                       uint32_t flags)
{
  return processor.complete(accounted_size, etag, mtime, set_mtime, attrs,
			    cksum, delete_at, if_match, if_nomatch,
			    user_data, zones_trace, canceled, rctx, flags);
}

bool RadosZoneGroup::placement_target_exists(std::string& target) const
{
  return !!group.placement_targets.count(target);
}

void RadosZoneGroup::get_placement_target_names(std::set<std::string>& names) const
{
  for (const auto& target : group.placement_targets) {
    names.emplace(target.second.name);
  }
}

int RadosZoneGroup::get_placement_tier(const rgw_placement_rule& rule,
				       std::unique_ptr<PlacementTier>* tier)
{
  std::map<std::string, RGWZoneGroupPlacementTarget>::const_iterator titer;
  titer = group.placement_targets.find(rule.name);
  if (titer == group.placement_targets.end()) {
    return -ENOENT;
  }

  const auto& target_rule = titer->second;
  std::map<std::string, RGWZoneGroupPlacementTier>::const_iterator ttier;
  ttier = target_rule.tier_targets.find(rule.storage_class);
  if (ttier == target_rule.tier_targets.end()) {
    // not found
    return -ENOENT;
  }

  PlacementTier* t;
  t = new RadosPlacementTier(store, ttier->second);
  if (!t)
    return -ENOMEM;

  tier->reset(t);
  return 0;
}

int RadosZoneGroup::get_zone_by_id(const std::string& id, std::unique_ptr<Zone>* zone)
{
  RGWZone* rz = store->svc()->zone->find_zone(id);
  if (!rz)
    return -ENOENT;

  Zone* z = new RadosZone(store, clone(), *rz);
  zone->reset(z);
  return 0;
}

int RadosZoneGroup::get_zone_by_name(const std::string& name, std::unique_ptr<Zone>* zone)
{
  rgw_zone_id id;
  int ret = store->svc()->zone->find_zone_id_by_name(name, &id);
  if (ret < 0)
    return ret;

  RGWZone* rz = store->svc()->zone->find_zone(id.id);
  if (!rz)
    return -ENOENT;

  Zone* z = new RadosZone(store, clone(), *rz);
  zone->reset(z);
  return 0;
}

int RadosZoneGroup::list_zones(std::list<std::string>& zone_ids)
{
  for (const auto& entry : group.zones)
    {
      zone_ids.push_back(entry.second.id);
    }
  return 0;
}

std::unique_ptr<Zone> RadosZone::clone()
{
  if (local_zone)
    return std::make_unique<RadosZone>(store, group->clone());

  return std::make_unique<RadosZone>(store, group->clone(), rgw_zone);
}

const std::string& RadosZone::get_id()
{
  if (local_zone)
    return store->svc()->zone->zone_id().id;

  return rgw_zone.id;
}

const std::string& RadosZone::get_name() const
{
  if (local_zone)
    return store->svc()->zone->zone_name();

  return rgw_zone.name;
}

bool RadosZone::is_writeable()
{
  if (local_zone)
    return store->svc()->zone->zone_is_writeable();

  return !rgw_zone.read_only;
}

bool RadosZone::get_redirect_endpoint(std::string* endpoint)
{
  if (local_zone)
    return store->svc()->zone->get_redirect_zone_endpoint(endpoint);

  endpoint = &rgw_zone.redirect_zone;
  return true;
}

const std::string& RadosZone::get_current_period_id()
{
  return store->svc()->zone->get_current_period_id();
}

const RGWAccessKey& RadosZone::get_system_key()
{
  return store->svc()->zone->get_zone_params().system_key;
}

const std::string& RadosZone::get_realm_name()
{
  return store->svc()->zone->get_realm().get_name();
}

const std::string& RadosZone::get_realm_id()
{
  return store->svc()->zone->get_realm().get_id();
}

const std::string_view RadosZone::get_tier_type()
{
  if (local_zone)
    return store->svc()->zone->get_zone().tier_type;

  return rgw_zone.tier_type;
}

RGWBucketSyncPolicyHandlerRef RadosZone::get_sync_policy_handler()
{
  return store->svc()->zone->get_sync_policy_handler(get_id());
}

RadosLuaManager::RadosLuaManager(RadosStore* _s, const std::string& _luarocks_path) :
  StoreLuaManager(_luarocks_path),
  store(_s),
  pool((store->svc() && store->svc()->zone) ? store->svc()->zone->get_zone_params().log_pool : rgw_pool()),
  ioctx(*store->getRados()->get_lc_pool_ctx()),
  packages_watcher(this)
{ }

int RadosLuaManager::get_script(const DoutPrefixProvider* dpp, optional_yield y, const std::string& key, std::string& script)
{
  if (pool.empty()) {
    ldpp_dout(dpp, 10) << "WARNING: missing pool when reading Lua script " << dendl;
    return 0;
  }
  bufferlist bl;

  int r = rgw_get_system_obj(store->svc()->sysobj, pool, key, bl, nullptr, nullptr, y, dpp);
  if (r < 0) {
    return r;
  }

  auto iter = bl.cbegin();
  try {
    ceph::decode(script, iter);
  } catch (buffer::error& err) {
    return -EIO;
  }

  return 0;
}

int RadosLuaManager::put_script(const DoutPrefixProvider* dpp, optional_yield y, const std::string& key, const std::string& script)
{
  if (pool.empty()) {
    ldpp_dout(dpp, 10) << "WARNING: missing pool when writing Lua script " << dendl;
    return 0;
  }
  bufferlist bl;
  ceph::encode(script, bl);

  int r = rgw_put_system_obj(dpp, store->svc()->sysobj, pool, key, bl, false, nullptr, real_time(), y);
  if (r < 0) {
    return r;
  }

  return 0;
}

int RadosLuaManager::del_script(const DoutPrefixProvider* dpp, optional_yield y, const std::string& key)
{
  if (pool.empty()) {
    ldpp_dout(dpp, 10) << "WARNING: missing pool when deleting Lua script " << dendl;
    return 0;
  }
  int r = rgw_delete_system_obj(dpp, store->svc()->sysobj, pool, key, nullptr, y);
  if (r < 0 && r != -ENOENT) {
    return r;
  }

  return 0;
}

const std::string PACKAGE_LIST_OBJECT_NAME = "lua_package_allowlist";

int RadosLuaManager::add_package(const DoutPrefixProvider *dpp, optional_yield y, const std::string& package_name)
{
  if (!ioctx.is_valid()) {
    ldpp_dout(dpp, 10) << "WARNING: missing pool when adding Lua package" << dendl;
    return 0;
  }
  // add package to list
  const bufferlist empty_bl;
  std::map<std::string, bufferlist> new_package{{package_name, empty_bl}};
  librados::ObjectWriteOperation op;
  op.omap_set(new_package);
  return rgw_rados_operate(dpp, ioctx,
      PACKAGE_LIST_OBJECT_NAME, &op, y);
}

int RadosLuaManager::remove_package(const DoutPrefixProvider *dpp, optional_yield y, const std::string& package_name)
{
  if (!ioctx.is_valid()) {
    ldpp_dout(dpp, 10) << "WARNING: missing pool when removing Lua package" << dendl;
    return -ENOENT;
  }
  librados::ObjectWriteOperation op;
  size_t pos = package_name.find(" ");
  if (pos != package_name.npos) {
    // remove specific version of the the package
    op.omap_rm_keys(std::set<std::string>({package_name}));
    auto ret = rgw_rados_operate(dpp, ioctx,
        PACKAGE_LIST_OBJECT_NAME, &op, y);
    if (ret < 0) {
        return ret;
    }
    return 0;
  }
  // otherwise, remove any existing versions of the package
  rgw::lua::packages_t packages;
  auto ret = list_packages(dpp, y, packages);
  if (ret < 0 && ret != -ENOENT) {
    return ret;
  }
  for(const auto& package : packages) {
    const std::string package_no_version = package.substr(0, package.find(" "));
    if (package_no_version.compare(package_name) == 0) {
        op.omap_rm_keys(std::set<std::string>({package}));
        ret = rgw_rados_operate(dpp, ioctx,
            PACKAGE_LIST_OBJECT_NAME, &op, y);
        if (ret < 0) {
            return ret;
        }
    }
  }
  return 0;
}

int RadosLuaManager::list_packages(const DoutPrefixProvider *dpp, optional_yield y, rgw::lua::packages_t& packages)
{
  if (!ioctx.is_valid()) {
    ldpp_dout(dpp, 10) << "WARNING: missing pool when listing Lua packages" << dendl;
    return -ENOENT;
  }
  constexpr auto max_chunk = 1024U;
  std::string start_after;
  bool more = true;
  int rval;
  while (more) {
    librados::ObjectReadOperation op;
    rgw::lua::packages_t packages_chunk;
    op.omap_get_keys2(start_after, max_chunk, &packages_chunk, &more, &rval);
    const auto ret = rgw_rados_operate(dpp, ioctx,
      PACKAGE_LIST_OBJECT_NAME, &op, nullptr, y);

    if (ret < 0) {
      return ret;
    }

    packages.merge(packages_chunk);
  }

  return 0;
}

int RadosLuaManager::watch_reload(const DoutPrefixProvider* dpp)
{
  if (!ioctx.is_valid()) {
    ldpp_dout(dpp, 10) << "WARNING: missing pool when watching reloads of Lua packages" << dendl;
    return -ENOENT;
  }
  // create the object to watch (object may already exist)
  librados::ObjectWriteOperation op;
  op.create(false);
  auto r = rgw_rados_operate(dpp, ioctx,
      PACKAGE_LIST_OBJECT_NAME, &op, null_yield);
  if (r < 0) {
    ldpp_dout(dpp, 1) << "ERROR: failed to watch " << PACKAGE_LIST_OBJECT_NAME
        << ". cannot create object. error: " << cpp_strerror(r) << dendl;
    return r;
  }
  r = ioctx.watch2(PACKAGE_LIST_OBJECT_NAME, &watch_handle, &packages_watcher);
  if (r < 0) {
    ldpp_dout(dpp, 1) << "ERROR: failed to watch " << PACKAGE_LIST_OBJECT_NAME
        << ". error: " << cpp_strerror(r) << dendl;
    return r;
  }
  ldpp_dout(dpp, 20) << "Started watching for reloads of  " << PACKAGE_LIST_OBJECT_NAME
    << " with handle: " << watch_handle << dendl;

  return 0;
}

int RadosLuaManager::unwatch_reload(const DoutPrefixProvider* dpp)
{
  if (watch_handle == 0) {
    // nothing to unwatch
    return 0;
  }

  if (!ioctx.is_valid()) {
    ldpp_dout(dpp, 10) << "WARNING: missing pool when unwatching reloads of Lua packages" << dendl;
    return -ENOENT;
  }
  const auto r = ioctx.unwatch2(watch_handle);
  if (r < 0) {
    ldpp_dout(dpp, 1) << "ERROR: failed to unwatch " << PACKAGE_LIST_OBJECT_NAME
        << ". error: " << cpp_strerror(r) << dendl;
    return r;
  }
  ldpp_dout(dpp, 20) << "Stopped watching for reloads of " << PACKAGE_LIST_OBJECT_NAME
    << " with handle: " << watch_handle << dendl;

  return 0;
}

void RadosLuaManager::ack_reload(const DoutPrefixProvider* dpp, uint64_t notify_id, uint64_t cookie, int reload_status) {
  if (!ioctx.is_valid()) {
    ldpp_dout(dpp, 10) << "WARNING: missing pool when acking reload of Lua packages" << dendl;
    return;
  }
  bufferlist reply;
  ceph::encode(reload_status, reply);
  ioctx.notify_ack(PACKAGE_LIST_OBJECT_NAME, notify_id, cookie, reply);
}

void RadosLuaManager::handle_reload_notify(const DoutPrefixProvider* dpp, optional_yield y, uint64_t notify_id, uint64_t cookie) {
  if (cookie != watch_handle) {
    return;
  }

#ifdef WITH_RADOSGW_LUA_PACKAGES
  rgw::lua::packages_t failed_packages;
  std::string install_dir;
  auto r = rgw::lua::install_packages(dpp, store,
      y, store->ctx()->_conf.get_val<std::string>("rgw_luarocks_location"),
      failed_packages, install_dir);
  if (r < 0) {
    ldpp_dout(dpp, 1) << "WARNING: failed to install Lua packages from allowlist. error code: " << r
            << dendl;
  }
  set_luarocks_path(install_dir);
  for (const auto &p : failed_packages) {
    ldpp_dout(dpp, 5) << "WARNING: failed to install Lua package: " << p
            << " from allowlist" << dendl;
  }
#else
  const int r = 0;
#endif
  ack_reload(dpp, notify_id, cookie, r);
}

int RadosLuaManager::reload_packages(const DoutPrefixProvider *dpp, optional_yield y)
{
  if (!ioctx.is_valid()) {
    ldpp_dout(dpp, 10) << "WARNING: missing pool trying to notify reload of Lua packages" << dendl;
    return -ENOENT;
  }
  bufferlist empty_bl;
  bufferlist reply_bl;
  const uint64_t timeout_ms = 0;
  auto r = rgw_rados_notify(dpp,
      ioctx,
      PACKAGE_LIST_OBJECT_NAME,
      empty_bl, timeout_ms, &reply_bl, y);
  if (r < 0) {
    ldpp_dout(dpp, 1) << "ERROR: failed to notify reload on " << PACKAGE_LIST_OBJECT_NAME
        << ". error: " << cpp_strerror(r) << dendl;
    return r;
  }

  std::vector<librados::notify_ack_t> acks;
  std::vector<librados::notify_timeout_t> timeouts;
  ioctx.decode_notify_response(reply_bl, &acks, &timeouts);
  if (timeouts.size() > 0) {
    ldpp_dout(dpp, 1) << "ERROR: failed to notify reload on " << PACKAGE_LIST_OBJECT_NAME
      << ". error: timeout" << dendl;
    return -EAGAIN;
  }
  for (auto& ack : acks) {
    try {
      auto iter = ack.payload_bl.cbegin();
      ceph::decode(r, iter);
    } catch (buffer::error& err) {
      ldpp_dout(dpp, 1) << "ERROR: couldn't decode Lua packages reload status. error: " <<
        err.what() << dendl;
      return -EINVAL;
    }
    if (r < 0) {
      return r;
    }
  }

  return 0;
}

void RadosLuaManager::PackagesWatcher::handle_notify(uint64_t notify_id, uint64_t cookie, uint64_t notifier_id, bufferlist &bl)
{
  parent->handle_reload_notify(this, null_yield, notify_id, cookie);
}

void RadosLuaManager::PackagesWatcher::handle_error(uint64_t cookie, int err)
{
  if (parent->watch_handle != cookie) {
    return;
  }
  ldpp_dout(this, 5) << "WARNING: restarting reload watch handler. error: " << err << dendl;

  parent->unwatch_reload(this);
  parent->watch_reload(this);
}

CephContext* RadosLuaManager::PackagesWatcher::get_cct() const {
  return parent->store->ctx();
}

unsigned RadosLuaManager::PackagesWatcher::get_subsys() const {
  return dout_subsys;
}

std::ostream& RadosLuaManager::PackagesWatcher::gen_prefix(std::ostream& out) const {
  return out << "rgw lua package reloader: ";
}

int RadosRole::store_info(const DoutPrefixProvider *dpp, bool exclusive, optional_yield y)
{
  librados::Rados& rados = *store->getRados()->get_rados_handle();
  RGWServices* svc = store->svc();
  const RGWZoneParams& zone = svc->zone->get_zone_params();
  return rgwrados::role::write(dpp, y, rados, *svc->sysobj, svc->mdlog,
                               zone, info, info.objv_tracker,
                               ceph::real_time{}, exclusive);
}

int RadosRole::load_by_name(const DoutPrefixProvider *dpp, optional_yield y)
{
  RGWServices* svc = store->svc();
  const RGWZoneParams& zone = svc->zone->get_zone_params();
  return rgwrados::role::read_by_name(dpp, y, *svc->sysobj, zone,
                                      info.tenant, info.account_id,
                                      info.name, info, &info.mtime,
                                      &info.objv_tracker);
}

int RadosRole::load_by_id(const DoutPrefixProvider *dpp, optional_yield y)
{
  RGWServices* svc = store->svc();
  const RGWZoneParams& zone = svc->zone->get_zone_params();
  return rgwrados::role::read_by_id(dpp, y, *svc->sysobj, zone, info.id,
                                    info, &info.mtime, &info.objv_tracker);
}

int RadosRole::delete_obj(const DoutPrefixProvider *dpp, optional_yield y)
{
  librados::Rados& rados = *store->getRados()->get_rados_handle();
  RGWServices* svc = store->svc();
  const RGWZoneParams& zone = svc->zone->get_zone_params();
  return rgwrados::role::remove(dpp, y, rados, *svc->sysobj, svc->mdlog, zone,
                                info.tenant, info.account_id, info.name);
}

} // namespace rgw::sal

extern "C" {

void* newRadosStore(void* io_context)
{
  rgw::sal::RadosStore* store = new rgw::sal::RadosStore(
    *static_cast<boost::asio::io_context*>(io_context));
  if (store) {
    RGWRados* rados = new RGWRados();

    if (!rados) {
      delete store; store = nullptr;
    } else {
      store->setRados(rados);
      rados->set_store(store);
    }
  }

  return store;
}

}