summaryrefslogtreecommitdiff
path: root/incfs/incfs.cpp
blob: 29574c1e9c40df7018c1b4673fcbf26d5379fefc (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
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "incfs"

#include "incfs.h"

#include <IncrementalProperties.sysprop.h>
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/no_destructor.h>
#include <android-base/parsebool.h>
#include <android-base/properties.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <android-base/unique_fd.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <openssl/sha.h>
#include <selinux/android.h>
#include <selinux/selinux.h>
#include <sys/inotify.h>
#include <sys/mount.h>
#include <sys/poll.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/vfs.h>
#include <sys/xattr.h>
#include <unistd.h>

#include <charconv>
#include <chrono>
#include <iterator>
#include <mutex>
#include <optional>
#include <string_view>

#include "MountRegistry.h"
#include "path.h"

using namespace std::literals;
using namespace android::incfs;
using namespace android::sysprop;
namespace ab = android::base;

struct IncFsControl final {
    IncFsFd cmd;
    IncFsFd pendingReads;
    IncFsFd logs;
    IncFsFd blocksWritten;
    constexpr IncFsControl(IncFsFd cmd, IncFsFd pendingReads, IncFsFd logs, IncFsFd blocksWritten)
          : cmd(cmd), pendingReads(pendingReads), logs(logs), blocksWritten(blocksWritten) {}
};

static MountRegistry& registry() {
    static ab::NoDestructor<MountRegistry> instance{};
    return *instance;
}

static ab::unique_fd openRaw(std::string_view file) {
    auto fd = ab::unique_fd(::open(details::c_str(file), O_RDONLY | O_CLOEXEC));
    if (fd < 0) {
        return ab::unique_fd{-errno};
    }
    return fd;
}

static ab::unique_fd openAt(int fd, std::string_view name, int flags = 0) {
    auto res = ab::unique_fd(
            ::openat(fd, details::c_str(name), O_RDONLY | O_CLOEXEC | O_NOFOLLOW | flags));
    if (res < 0) {
        return ab::unique_fd{-errno};
    }
    return res;
}

static std::string indexPath(std::string_view root, IncFsFileId fileId) {
    return path::join(root, INCFS_INDEX_NAME, toString(fileId));
}

static std::string rootForCmd(int fd) {
    auto cmdFile = path::fromFd(fd);
    if (cmdFile.empty()) {
        LOG(INFO) << __func__ << "(): name empty for " << fd;
        return {};
    }
    auto res = path::dirName(cmdFile);
    if (res.empty()) {
        LOG(INFO) << __func__ << "(): dirname empty for " << cmdFile;
        return {};
    }
    if (!path::endsWith(cmdFile, INCFS_PENDING_READS_FILENAME)) {
        LOG(INFO) << __func__ << "(): invalid file name " << cmdFile;
        return {};
    }
    if (cmdFile.data() == res.data() || cmdFile.starts_with(res)) {
        cmdFile.resize(res.size());
        return cmdFile;
    }
    return std::string(res);
}

static bool isFsAvailable() {
    static const char kProcFilesystems[] = "/proc/filesystems";
    std::string filesystems;
    if (!ab::ReadFileToString(kProcFilesystems, &filesystems)) {
        return false;
    }
    const auto result = filesystems.find("\t" INCFS_NAME "\n") != std::string::npos;
    LOG(INFO) << "isFsAvailable: " << (result ? "true" : "false");
    return result;
}

static int getFirstApiLevel() {
    uint64_t api_level = android::base::GetUintProperty<uint64_t>("ro.product.first_api_level", 0);
    LOG(INFO) << "Initial API level of the device: " << api_level;
    return api_level;
}

static std::string_view incFsPropertyValue() {
    constexpr const int R_API = 30;
    static const auto kDefaultValue{getFirstApiLevel() > R_API ? "on" : ""};
    static const ab::NoDestructor<std::string> kValue{
            IncrementalProperties::enable().value_or(kDefaultValue)};
    LOG(INFO) << "ro.incremental.enable: " << *kValue;
    return *kValue;
}

static std::pair<bool, std::string_view> parseProperty(std::string_view property) {
    auto boolVal = ab::ParseBool(property);
    if (boolVal == ab::ParseBoolResult::kTrue) {
        return {isFsAvailable(), {}};
    }
    if (boolVal == ab::ParseBoolResult::kFalse) {
        return {false, {}};
    }

    // Don't load the module at once, but instead only check if it is loadable.
    static const auto kModulePrefix = "module:"sv;
    if (property.starts_with(kModulePrefix)) {
        const auto modulePath = property.substr(kModulePrefix.size());
        return {::access(details::c_str(modulePath), R_OK | X_OK), modulePath};
    }
    return {false, {}};
}

template <class Callback>
static IncFsErrorCode forEachFileIn(std::string_view dirPath, Callback cb) {
    auto dir = path::openDir(details::c_str(dirPath));
    if (!dir) {
        return -EINVAL;
    }

    int res = 0;
    while (auto entry = (errno = 0, ::readdir(dir.get()))) {
        if (entry->d_type != DT_REG) {
            continue;
        }
        ++res;
        if (!cb(entry->d_name)) {
            break;
        }
    }
    if (errno) {
        return -errno;
    }
    return res;
}

namespace {

class IncFsInit {
public:
    IncFsInit() {
        auto [featureEnabled, moduleName] = parseProperty(incFsPropertyValue());
        featureEnabled_ = featureEnabled;
        moduleName_ = moduleName;
        loaded_ = featureEnabled_ && isFsAvailable();
    }

    constexpr ~IncFsInit() = default;

    bool enabled() const { return featureEnabled_; }
    bool enabledAndReady() const {
        if (!featureEnabled_) {
            return false;
        }
        if (moduleName_.empty()) {
            return true;
        }
        if (loaded_) {
            return true;
        }
        std::call_once(loadedFlag_, [this] {
            if (isFsAvailable()) {
                // Loaded from a different process, I suppose.
                loaded_ = true;
                LOG(INFO) << "IncFS is already available, skipped loading";
                return;
            }
            const ab::unique_fd fd(TEMP_FAILURE_RETRY(
                    ::open(details::c_str(moduleName_), O_RDONLY | O_NOFOLLOW | O_CLOEXEC)));
            if (fd < 0) {
                PLOG(ERROR) << "could not open IncFs kernel module \"" << moduleName_ << '"';
                return;
            }

            const auto rc = syscall(__NR_finit_module, fd.get(), "", 0);
            if (rc < 0) {
                PLOG(ERROR) << "finit_module for IncFs \"" << moduleName_ << "\" failed";
                return;
            }
            if (!isFsAvailable()) {
                LOG(ERROR) << "loaded IncFs kernel module \"" << moduleName_
                           << "\" but incremental-fs is still not available";
            }
            loaded_ = true;
            LOG(INFO) << "successfully loaded IncFs kernel module \"" << moduleName_ << '"';
        });
        return loaded_;
    }

private:
    bool featureEnabled_;
    std::string_view moduleName_;
    mutable std::once_flag loadedFlag_;
    mutable bool loaded_;
};

} // namespace

static IncFsInit& init() {
    static IncFsInit initer;
    return initer;
}

bool IncFs_IsEnabled() {
    return init().enabled();
}

static Features readIncFsFeatures() {
    init().enabledAndReady();

    int res = Features::none | Features::mappingFilesProgressFixed;

    static const char kSysfsFeaturesDir[] = "/sys/fs/" INCFS_NAME "/features";
    const auto dir = path::openDir(kSysfsFeaturesDir);
    if (!dir) {
        PLOG(ERROR) << "IncFs_Features: failed to open features dir, assuming v1/none.";
        return Features(res);
    }

    while (auto entry = ::readdir(dir.get())) {
        if (entry->d_type != DT_REG) {
            continue;
        }
        if (entry->d_name == "corefs"sv) {
            res |= Features::core;
        } else if (entry->d_name == "v2"sv || entry->d_name == "report_uid"sv) {
            res |= Features::v2;
        }
    }

    LOG(INFO) << "IncFs_Features: " << ((res & Features::v2) ? "v2" : "v1");

    return Features(res);
}

IncFsFeatures IncFs_Features() {
    static const auto features = IncFsFeatures(readIncFsFeatures());
    return features;
}

bool isIncFsFdImpl(int fd) {
    struct statfs fs = {};
    if (::fstatfs(fd, &fs) != 0) {
        PLOG(WARNING) << __func__ << "(): could not fstatfs fd " << fd;
        return false;
    }

    return fs.f_type == (decltype(fs.f_type))INCFS_MAGIC_NUMBER;
}

bool isIncFsPathImpl(const char* path) {
    struct statfs fs = {};
    if (::statfs(path, &fs) != 0) {
        PLOG(WARNING) << __func__ << "(): could not statfs " << path;
        return false;
    }

    return fs.f_type == (decltype(fs.f_type))INCFS_MAGIC_NUMBER;
}

static int isDir(const char* path) {
    struct stat st;
    if (::stat(path, &st) != 0) {
        return -errno;
    }
    if (!S_ISDIR(st.st_mode)) {
        return -ENOTDIR;
    }
    return 0;
}

static bool isAbsolute(const char* path) {
    return path && path[0] == '/';
}

static int isValidMountTarget(const char* path) {
    if (!isAbsolute(path)) {
        return -EINVAL;
    }
    if (isIncFsPath(path)) {
        LOG(ERROR) << "[incfs] mounting over existing incfs mount is not allowed";
        return -EINVAL;
    }
    if (const auto err = isDir(path); err != 0) {
        return err;
    }
    if (const auto err = path::isEmptyDir(path); err != 0) {
        return err;
    }
    return 0;
}

static int rmDirContent(int dirFd) {
    auto dir = path::openDir(dirFd);
    if (!dir) {
        return -errno;
    }
    while (auto entry = ::readdir(dir.get())) {
        if (entry->d_name == "."sv || entry->d_name == ".."sv) {
            continue;
        }
        if (entry->d_type == DT_DIR) {
            auto fd = openAt(dirFd, entry->d_name, O_DIRECTORY);
            if (!fd.ok()) {
                return -errno;
            }
            if (const auto err = rmDirContent(fd.get())) {
                return err;
            }
            if (::unlinkat(fd.get(), entry->d_name, AT_REMOVEDIR)) {
                return -errno;
            }
        } else {
            auto fd = openAt(dirFd, entry->d_name);
            if (!fd.ok()) {
                return -errno;
            }
            if (::unlinkat(fd.get(), entry->d_name, 0)) {
                return -errno;
            }
        }
    }
    return 0;
}

static int rmDirContent(const char* path) {
    auto fd = openAt(-1, path, O_DIRECTORY);
    if (!fd.ok()) {
        return -errno;
    }
    return rmDirContent(fd.get());
}

static std::string makeMountOptionsString(IncFsMountOptions options) {
    auto opts = ab::StringPrintf("read_timeout_ms=%u,readahead=0,rlog_pages=%u,rlog_wakeup_cnt=1,",
                                 unsigned(options.defaultReadTimeoutMs),
                                 unsigned(options.readLogBufferPages < 0
                                                  ? INCFS_DEFAULT_PAGE_READ_BUFFER_PAGES
                                                  : options.readLogBufferPages));
    if (features() & Features::v2) {
        ab::StringAppendF(&opts, "report_uid,");
        if (options.sysfsName && *options.sysfsName) {
            ab::StringAppendF(&opts, "sysfs_name=%s,", options.sysfsName);
        }
    }
    return opts;
}

static IncFsControl* makeControl(int fd) {
    auto cmd = openAt(fd, INCFS_PENDING_READS_FILENAME);
    if (!cmd.ok()) {
        return nullptr;
    }
    ab::unique_fd pendingReads(fcntl(cmd.get(), F_DUPFD_CLOEXEC, cmd.get()));
    if (!pendingReads.ok()) {
        return nullptr;
    }
    auto logs = openAt(fd, INCFS_LOG_FILENAME);
    if (!logs.ok()) {
        return nullptr;
    }
    ab::unique_fd blocksWritten;
    if (features() & Features::v2) {
        blocksWritten = openAt(fd, INCFS_BLOCKS_WRITTEN_FILENAME);
        if (!blocksWritten.ok()) {
            return nullptr;
        }
    }
    auto control =
            IncFs_CreateControl(cmd.get(), pendingReads.get(), logs.get(), blocksWritten.get());
    if (control) {
        (void)cmd.release();
        (void)pendingReads.release();
        (void)logs.release();
        (void)blocksWritten.release();
    } else {
        errno = ENOMEM;
    }
    return control;
}

static std::string makeCommandPath(std::string_view root, std::string_view item) {
    auto [itemRoot, subpath] = registry().rootAndSubpathFor(item);
    if (itemRoot != root) {
        return {};
    }
    // TODO: add "/.cmd/" if we decide to use a separate control tree.
    return path::join(itemRoot, subpath);
}

static void toString(IncFsFileId id, char* out) {
    // Make sure this function matches the one in the kernel (e.g. same case for a-f digits).
    static constexpr char kHexChar[] = "0123456789abcdef";

    for (auto item = std::begin(id.data); item != std::end(id.data); ++item, out += 2) {
        out[0] = kHexChar[(*item & 0xf0) >> 4];
        out[1] = kHexChar[(*item & 0x0f)];
    }
}

static std::string toStringImpl(IncFsFileId id) {
    std::string res(kIncFsFileIdStringLength, '\0');
    toString(id, res.data());
    return res;
}

static IncFsFileId toFileIdImpl(std::string_view str) {
    if (str.size() != kIncFsFileIdStringLength) {
        return kIncFsInvalidFileId;
    }

    IncFsFileId res;
    auto out = (char*)&res;
    for (auto it = str.begin(); it != str.end(); it += 2, ++out) {
        static const auto fromChar = [](char src) -> int {
            if (src >= '0' && src <= '9') {
                return src - '0';
            }
            if (src >= 'a' && src <= 'f') {
                return src - 'a' + 10;
            }
            return -1;
        };

        const int c[2] = {fromChar(it[0]), fromChar(it[1])};
        if (c[0] == -1 || c[1] == -1) {
            errno = EINVAL;
            return kIncFsInvalidFileId;
        }
        *out = (c[0] << 4) | c[1];
    }
    return res;
}

int IncFs_FileIdToString(IncFsFileId id, char* out) {
    if (!out) {
        return -EINVAL;
    }
    toString(id, out);
    return 0;
}

IncFsFileId IncFs_FileIdFromString(const char* in) {
    return toFileIdImpl({in, kIncFsFileIdStringLength});
}

IncFsFileId IncFs_FileIdFromMetadata(IncFsSpan metadata) {
    IncFsFileId id = {};
    if (size_t(metadata.size) <= sizeof(id)) {
        memcpy(&id, metadata.data, metadata.size);
    } else {
        uint8_t buffer[SHA_DIGEST_LENGTH];
        static_assert(sizeof(buffer) >= sizeof(id));

        SHA_CTX ctx;
        SHA1_Init(&ctx);
        SHA1_Update(&ctx, metadata.data, metadata.size);
        SHA1_Final(buffer, &ctx);
        memcpy(&id, buffer, sizeof(id));
    }
    return id;
}

static bool restoreconControlFiles(std::string_view targetDir) {
    static constexpr auto restorecon = [](const char* name) {
        if (const auto err = selinux_android_restorecon(name, SELINUX_ANDROID_RESTORECON_FORCE);
            err != 0) {
            errno = -err;
            PLOG(ERROR) << "[incfs] Failed to restorecon: " << name;
            return false;
        }
        return true;
    };
    if (!restorecon(path::join(targetDir, INCFS_PENDING_READS_FILENAME).c_str())) {
        return false;
    }
    if (!restorecon(path::join(targetDir, INCFS_LOG_FILENAME).c_str())) {
        return false;
    }
    if ((features() & Features::v2) &&
        !restorecon(path::join(targetDir, INCFS_BLOCKS_WRITTEN_FILENAME).c_str())) {
        return false;
    }
    return true;
}

IncFsControl* IncFs_Mount(const char* backingPath, const char* targetDir,
                          IncFsMountOptions options) {
    if (!init().enabledAndReady()) {
        LOG(WARNING) << "[incfs] Feature is not enabled";
        errno = ENOTSUP;
        return nullptr;
    }

    if (auto err = isValidMountTarget(targetDir); err != 0) {
        errno = -err;
        return nullptr;
    }
    if (!isAbsolute(backingPath)) {
        errno = EINVAL;
        return nullptr;
    }

    if (options.flags & createOnly) {
        if (const auto err = path::isEmptyDir(backingPath); err != 0) {
            errno = -err;
            return nullptr;
        }
    } else if (options.flags & android::incfs::truncate) {
        if (const auto err = rmDirContent(backingPath); err != 0) {
            errno = -err;
            return nullptr;
        }
    }

    const auto opts = makeMountOptionsString(options);
    if (::mount(backingPath, targetDir, INCFS_NAME, MS_NOSUID | MS_NODEV | MS_NOATIME,
                opts.c_str())) {
        PLOG(ERROR) << "[incfs] Failed to mount IncFS filesystem: " << targetDir;
        return nullptr;
    }

    // in case when the path is given in a form of a /proc/.../fd/ link, we need to update
    // it here: old fd refers to the original empty directory, not to the mount
    std::string updatedTargetDir;
    if (path::dirName(targetDir) == path::procfsFdDir) {
        updatedTargetDir = path::readlink(targetDir);
    } else {
        updatedTargetDir = targetDir;
    }

    auto rootFd = ab::unique_fd(::open(updatedTargetDir.c_str(), O_PATH | O_CLOEXEC | O_DIRECTORY));
    if (updatedTargetDir != targetDir) {
        // ensure that the new directory is still the same after reopening
        if (path::fromFd(rootFd) != updatedTargetDir) {
            errno = EINVAL;
            return nullptr;
        }
    }

    if (!restoreconControlFiles(path::procfsForFd(rootFd))) {
        (void)IncFs_Unmount(targetDir);
        return nullptr;
    }

    auto control = makeControl(rootFd);
    if (control == nullptr) {
        (void)IncFs_Unmount(targetDir);
        return nullptr;
    }
    return control;
}

IncFsControl* IncFs_Open(const char* dir) {
    auto root = registry().rootFor(dir);
    if (root.empty()) {
        errno = EINVAL;
        return nullptr;
    }
    auto rootFd = ab::unique_fd(::open(details::c_str(root), O_PATH | O_CLOEXEC | O_DIRECTORY));
    return makeControl(rootFd);
}

IncFsFd IncFs_GetControlFd(const IncFsControl* control, IncFsFdType type) {
    if (!control) {
        return -EINVAL;
    }
    switch (type) {
        case CMD:
            return control->cmd;
        case PENDING_READS:
            return control->pendingReads;
        case LOGS:
            return control->logs;
        case BLOCKS_WRITTEN:
            return control->blocksWritten;
        default:
            return -EINVAL;
    }
}

IncFsSize IncFs_ReleaseControlFds(IncFsControl* control, IncFsFd out[], IncFsSize outSize) {
    if (!control || !out) {
        return -EINVAL;
    }
    if (outSize < IncFsFdType::FDS_COUNT) {
        return -ERANGE;
    }
    out[CMD] = std::exchange(control->cmd, -1);
    out[PENDING_READS] = std::exchange(control->pendingReads, -1);
    out[LOGS] = std::exchange(control->logs, -1);
    out[BLOCKS_WRITTEN] = std::exchange(control->blocksWritten, -1);
    return IncFsFdType::FDS_COUNT;
}

IncFsControl* IncFs_CreateControl(IncFsFd cmd, IncFsFd pendingReads, IncFsFd logs,
                                  IncFsFd blocksWritten) {
    return new IncFsControl(cmd, pendingReads, logs, blocksWritten);
}

void IncFs_DeleteControl(IncFsControl* control) {
    if (control) {
        if (control->cmd >= 0) {
            close(control->cmd);
        }
        if (control->pendingReads >= 0) {
            close(control->pendingReads);
        }
        if (control->logs >= 0) {
            close(control->logs);
        }
        if (control->blocksWritten >= 0) {
            close(control->blocksWritten);
        }
        delete control;
    }
}

IncFsErrorCode IncFs_SetOptions(const IncFsControl* control, IncFsMountOptions options) {
    if (!control) {
        return -EINVAL;
    }
    auto root = rootForCmd(control->cmd);
    if (root.empty()) {
        return -EINVAL;
    }
    auto opts = makeMountOptionsString(options);
    if (::mount(nullptr, root.c_str(), nullptr, MS_REMOUNT | MS_NOSUID | MS_NODEV | MS_NOATIME,
                opts.c_str()) != 0) {
        const auto error = errno;
        PLOG(ERROR) << "[incfs] Failed to remount IncFS filesystem: " << root;
        return -error;
    }
    return 0;
}

IncFsErrorCode IncFs_Root(const IncFsControl* control, char buffer[], size_t* bufferSize) {
    if (!control) {
        return -EINVAL;
    }
    std::string result = rootForCmd(control->cmd);
    if (*bufferSize <= result.size()) {
        *bufferSize = result.size() + 1;
        return -EOVERFLOW;
    }
    result.copy(buffer, result.size());
    buffer[result.size()] = '\0';
    *bufferSize = result.size();
    return 0;
}

template <class T>
std::optional<T> read(IncFsSpan& data) {
    if (data.size < (int32_t)sizeof(T)) {
        return {};
    }
    T res;
    memcpy(&res, data.data, sizeof(res));
    data.data += sizeof(res);
    data.size -= sizeof(res);
    return res;
}

static IncFsErrorCode validateSignatureFormat(IncFsSpan signature) {
    if (signature.data == nullptr && signature.size == 0) {
        return 0; // it's fine to have unverified files too
    }
    if ((signature.data == nullptr) != (signature.size == 0)) {
        return -EINVAL;
    }

    // These structs are here purely for checking the minimum size. Maybe will use them for
    // parsing later.
    struct __attribute__((packed)) Hashing {
        int32_t size;
        int32_t algorithm;
        int8_t log2_blocksize;
        int32_t salt_size;
        int32_t raw_root_hash_size;
    };
    struct __attribute__((packed)) Signing {
        int32_t size;
        int32_t apk_digest_size;
        int32_t certificate_size;
        int32_t addl_data_size;
        int32_t public_key_size;
        int32_t algorithm;
        int32_t signature_size;
    };
    struct __attribute__((packed)) MinSignature {
        int32_t version;
        Hashing hashing_info;
        Signing signing_info;
    };

    if (signature.size < (int32_t)sizeof(MinSignature)) {
        return -ERANGE;
    }
    if (signature.size > INCFS_MAX_SIGNATURE_SIZE) {
        return -ERANGE;
    }

    auto version = read<int32_t>(signature);
    if (version.value_or(-1) != INCFS_SIGNATURE_VERSION) {
        return -EINVAL;
    }
    auto hashSize = read<int32_t>(signature);
    if (!hashSize || signature.size < *hashSize) {
        return -EINVAL;
    }
    auto hashAlgo = read<int32_t>(signature);
    if (hashAlgo.value_or(-1) != INCFS_HASH_TREE_SHA256) {
        return -EINVAL;
    }
    auto logBlockSize = read<int8_t>(signature);
    if (logBlockSize.value_or(-1) != 12 /* 2^12 == 4096 */) {
        return -EINVAL;
    }
    auto saltSize = read<int32_t>(signature);
    if (saltSize.value_or(-1) != 0) {
        return -EINVAL;
    }
    auto rootHashSize = read<int32_t>(signature);
    if (rootHashSize.value_or(-1) != INCFS_MAX_HASH_SIZE) {
        return -EINVAL;
    }
    if (signature.size < *rootHashSize) {
        return -EINVAL;
    }
    signature.data += *rootHashSize;
    signature.size -= *rootHashSize;
    auto signingSize = read<int32_t>(signature);
    // everything remaining has to be in the signing info
    if (signingSize.value_or(-1) != signature.size) {
        return -EINVAL;
    }

    // TODO: validate the signature part too.
    return 0;
}

IncFsErrorCode IncFs_MakeFile(const IncFsControl* control, const char* path, int32_t mode,
                              IncFsFileId id, IncFsNewFileParams params) {
    if (!control) {
        return -EINVAL;
    }

    auto [root, subpath] = registry().rootAndSubpathFor(path);
    if (root.empty()) {
        PLOG(WARNING) << "[incfs] makeFile failed for path " << path << ", root is empty.";
        return -EINVAL;
    }
    if (params.size < 0) {
        LOG(WARNING) << "[incfs] makeFile failed for path " << path
                     << ", size is invalid: " << params.size;
        return -ERANGE;
    }

    const auto [subdir, name] = path::splitDirBase(subpath);
    incfs_new_file_args args = {
            .size = (uint64_t)params.size,
            .mode = (uint16_t)mode,
            .directory_path = (uint64_t)subdir.data(),
            .file_name = (uint64_t)name.data(),
            .file_attr = (uint64_t)params.metadata.data,
            .file_attr_len = (uint32_t)params.metadata.size,
    };
    static_assert(sizeof(args.file_id.bytes) == sizeof(id.data));
    memcpy(args.file_id.bytes, id.data, sizeof(args.file_id.bytes));

    if (auto err = validateSignatureFormat(params.signature)) {
        return err;
    }
    args.signature_info = (uint64_t)(uintptr_t)params.signature.data;
    args.signature_size = (uint64_t)params.signature.size;

    if (::ioctl(control->cmd, INCFS_IOC_CREATE_FILE, &args)) {
        PLOG(WARNING) << "[incfs] makeFile failed for " << root << " / " << subdir << " / " << name
                      << " of " << params.size << " bytes";
        return -errno;
    }
    if (::chmod(path::join(root, subdir, name).c_str(), mode)) {
        PLOG(WARNING) << "[incfs] couldn't change file mode to 0" << std::oct << mode;
    }

    return 0;
}

IncFsErrorCode IncFs_MakeMappedFile(const IncFsControl* control, const char* path, int32_t mode,
                                    IncFsNewMappedFileParams params) {
    if (!control) {
        return -EINVAL;
    }

    auto [root, subpath] = registry().rootAndSubpathFor(path);
    if (root.empty()) {
        PLOG(WARNING) << "[incfs] makeMappedFile failed for path " << path << ", root is empty.";
        return -EINVAL;
    }
    if (params.size < 0) {
        LOG(WARNING) << "[incfs] makeMappedFile failed for path " << path
                     << ", size is invalid: " << params.size;
        return -ERANGE;
    }

    const auto [subdir, name] = path::splitDirBase(subpath);
    incfs_create_mapped_file_args args = {
            .size = (uint64_t)params.size,
            .mode = (uint16_t)mode,
            .directory_path = (uint64_t)subdir.data(),
            .file_name = (uint64_t)name.data(),
            .source_offset = (uint64_t)params.sourceOffset,
    };
    static_assert(sizeof(args.source_file_id.bytes) == sizeof(params.sourceId.data));
    memcpy(args.source_file_id.bytes, params.sourceId.data, sizeof(args.source_file_id.bytes));

    if (::ioctl(control->cmd, INCFS_IOC_CREATE_MAPPED_FILE, &args)) {
        PLOG(WARNING) << "[incfs] makeMappedFile failed for " << root << " / " << subdir << " / "
                      << name << " of " << params.size << " bytes starting at "
                      << params.sourceOffset;
        return -errno;
    }
    if (::chmod(path::join(root, subpath).c_str(), mode)) {
        PLOG(WARNING) << "[incfs] makeMappedFile error: couldn't change file mode to 0" << std::oct
                      << mode;
    }

    return 0;
}

static IncFsErrorCode makeDir(const char* commandPath, int32_t mode, bool allowExisting) {
    if (!::mkdir(commandPath, mode)) {
        if (::chmod(commandPath, mode)) {
            PLOG(WARNING) << "[incfs] couldn't change directory mode to 0" << std::oct << mode;
        }
        return 0;
    }
    // don't touch the existing dir's mode - mkdir(1) works that way.
    return (allowExisting && errno == EEXIST) ? 0 : -errno;
}

static IncFsErrorCode makeDirs(std::string_view commandPath, std::string_view path,
                               std::string_view root, int32_t mode) {
    auto commandCPath = details::c_str(commandPath);
    const auto mkdirRes = makeDir(commandCPath, mode, true);
    if (!mkdirRes) {
        return 0;
    }
    if (mkdirRes != -ENOENT) {
        LOG(ERROR) << __func__ << "(): mkdir failed for " << path << " - " << mkdirRes;
        return mkdirRes;
    }

    const auto parent = path::dirName(commandPath);
    if (!path::startsWith(parent, root)) {
        // went too far, already out of the root mount
        return -EINVAL;
    }

    if (auto parentMkdirRes = makeDirs(parent, path::dirName(path), root, mode)) {
        return parentMkdirRes;
    }
    return makeDir(commandCPath, mode, true);
}

IncFsErrorCode IncFs_MakeDir(const IncFsControl* control, const char* path, int32_t mode) {
    if (!control) {
        return -EINVAL;
    }
    const auto root = rootForCmd(control->cmd);
    if (root.empty()) {
        LOG(ERROR) << __func__ << "(): root is empty for " << path;
        return -EINVAL;
    }
    auto commandPath = makeCommandPath(root, path);
    if (commandPath.empty()) {
        LOG(ERROR) << __func__ << "(): commandPath is empty for " << path;
        return -EINVAL;
    }
    if (auto res = makeDir(commandPath.c_str(), mode, false)) {
        LOG(ERROR) << __func__ << "(): mkdir failed for " << commandPath << " - " << res;
        return res;
    }
    return 0;
}

IncFsErrorCode IncFs_MakeDirs(const IncFsControl* control, const char* path, int32_t mode) {
    if (!control) {
        return -EINVAL;
    }
    const auto root = rootForCmd(control->cmd);
    if (root.empty()) {
        LOG(ERROR) << __func__ << "(): root is empty for " << path;
        return -EINVAL;
    }
    auto commandPath = makeCommandPath(root, path);
    if (commandPath.empty()) {
        LOG(ERROR) << __func__ << "(): commandPath is empty for " << path;
        return -EINVAL;
    }
    return makeDirs(commandPath, path, root, mode);
}

static IncFsErrorCode getMetadata(const char* path, char buffer[], size_t* bufferSize) {
    const auto res = ::getxattr(path, kMetadataAttrName, buffer, *bufferSize);
    if (res < 0) {
        if (errno == ERANGE) {
            auto neededSize = ::getxattr(path, kMetadataAttrName, buffer, 0);
            if (neededSize >= 0) {
                *bufferSize = neededSize;
                return 0;
            }
        }
        return -errno;
    }
    *bufferSize = res;
    return 0;
}

IncFsErrorCode IncFs_GetMetadataById(const IncFsControl* control, IncFsFileId fileId, char buffer[],
                                     size_t* bufferSize) {
    if (!control) {
        return -EINVAL;
    }

    const auto root = rootForCmd(control->cmd);
    if (root.empty()) {
        return -EINVAL;
    }
    auto name = indexPath(root, fileId);
    return getMetadata(details::c_str(name), buffer, bufferSize);
}

IncFsErrorCode IncFs_GetMetadataByPath(const IncFsControl* control, const char* path, char buffer[],
                                       size_t* bufferSize) {
    if (!control) {
        return -EINVAL;
    }
    const auto pathRoot = registry().rootFor(path);
    const auto root = rootForCmd(control->cmd);
    if (root.empty() || root != pathRoot) {
        return -EINVAL;
    }

    return getMetadata(path, buffer, bufferSize);
}

template <class GetterFunc, class Param>
static IncFsFileId getId(GetterFunc getter, Param param) {
    char buffer[kIncFsFileIdStringLength];
    const auto res = getter(param, kIdAttrName, buffer, sizeof(buffer));
    if (res != sizeof(buffer)) {
        return kIncFsInvalidFileId;
    }
    return toFileIdImpl({buffer, std::size(buffer)});
}

IncFsFileId IncFs_GetId(const IncFsControl* control, const char* path) {
    if (!control) {
        return kIncFsInvalidFileId;
    }
    const auto pathRoot = registry().rootFor(path);
    const auto root = rootForCmd(control->cmd);
    if (root.empty() || root != pathRoot) {
        errno = EINVAL;
        return kIncFsInvalidFileId;
    }
    return getId(::getxattr, path);
}

static IncFsErrorCode getSignature(int fd, char buffer[], size_t* bufferSize) {
    incfs_get_file_sig_args args = {
            .file_signature = (uint64_t)buffer,
            .file_signature_buf_size = (uint32_t)*bufferSize,
    };

    auto res = ::ioctl(fd, INCFS_IOC_READ_FILE_SIGNATURE, &args);
    if (res < 0) {
        if (errno == E2BIG) {
            *bufferSize = INCFS_MAX_SIGNATURE_SIZE;
        }
        return -errno;
    }
    *bufferSize = args.file_signature_len_out;
    return 0;
}

IncFsErrorCode IncFs_GetSignatureById(const IncFsControl* control, IncFsFileId fileId,
                                      char buffer[], size_t* bufferSize) {
    if (!control) {
        return -EINVAL;
    }

    const auto root = rootForCmd(control->cmd);
    if (root.empty()) {
        return -EINVAL;
    }
    auto file = indexPath(root, fileId);
    auto fd = openRaw(file);
    if (fd < 0) {
        return fd.get();
    }
    return getSignature(fd, buffer, bufferSize);
}

IncFsErrorCode IncFs_GetSignatureByPath(const IncFsControl* control, const char* path,
                                        char buffer[], size_t* bufferSize) {
    if (!control) {
        return -EINVAL;
    }

    const auto pathRoot = registry().rootFor(path);
    const auto root = rootForCmd(control->cmd);
    if (root.empty() || root != pathRoot) {
        return -EINVAL;
    }
    return IncFs_UnsafeGetSignatureByPath(path, buffer, bufferSize);
}

IncFsErrorCode IncFs_UnsafeGetSignatureByPath(const char* path, char buffer[], size_t* bufferSize) {
    if (!isIncFsPath(path)) {
        return -EINVAL;
    }
    auto fd = openRaw(path);
    if (fd < 0) {
        return fd.get();
    }
    return getSignature(fd, buffer, bufferSize);
}

IncFsErrorCode IncFs_Link(const IncFsControl* control, const char* fromPath,
                          const char* wherePath) {
    if (!control) {
        return -EINVAL;
    }

    auto root = rootForCmd(control->cmd);
    if (root.empty()) {
        return -EINVAL;
    }
    auto cmdFrom = makeCommandPath(root, fromPath);
    if (cmdFrom.empty()) {
        return -EINVAL;
    }
    auto cmdWhere = makeCommandPath(root, wherePath);
    if (cmdWhere.empty()) {
        return -EINVAL;
    }
    if (::link(cmdFrom.c_str(), cmdWhere.c_str())) {
        return -errno;
    }
    return 0;
}

IncFsErrorCode IncFs_Unlink(const IncFsControl* control, const char* path) {
    if (!control) {
        return -EINVAL;
    }

    auto root = rootForCmd(control->cmd);
    if (root.empty()) {
        return -EINVAL;
    }
    auto cmdPath = makeCommandPath(root, path);
    if (cmdPath.empty()) {
        return -EINVAL;
    }
    if (::unlink(cmdPath.c_str())) {
        if (errno == EISDIR) {
            if (!::rmdir(cmdPath.c_str())) {
                return 0;
            }
        }
        return -errno;
    }
    return 0;
}

template <class RawPendingRead>
static int waitForReadsImpl(int fd, int32_t timeoutMs, RawPendingRead pendingReadsBuffer[],
                            size_t* pendingReadsBufferSize) {
    using namespace std::chrono;
    auto hrTimeout = steady_clock::duration(milliseconds(timeoutMs));

    while (hrTimeout > hrTimeout.zero() || (!pendingReadsBuffer && hrTimeout == hrTimeout.zero())) {
        const auto startTs = steady_clock::now();

        pollfd pfd = {fd, POLLIN, 0};
        const auto res = ::poll(&pfd, 1, duration_cast<milliseconds>(hrTimeout).count());
        if (res > 0) {
            break;
        }
        if (res == 0) {
            if (pendingReadsBufferSize) {
                *pendingReadsBufferSize = 0;
            }
            return -ETIMEDOUT;
        }
        const auto error = errno;
        if (error != EINTR) {
            PLOG(ERROR) << "poll() failed";
            return -error;
        }
        hrTimeout -= steady_clock::now() - startTs;
    }
    if (!pendingReadsBuffer) {
        return hrTimeout < hrTimeout.zero() ? -ETIMEDOUT : 0;
    }

    auto res =
            ::read(fd, pendingReadsBuffer, *pendingReadsBufferSize * sizeof(*pendingReadsBuffer));
    if (res < 0) {
        const auto error = errno;
        PLOG(ERROR) << "read() failed";
        return -error;
    }
    if (res == 0) {
        *pendingReadsBufferSize = 0;
        return -ETIMEDOUT;
    }
    if ((res % sizeof(*pendingReadsBuffer)) != 0) {
        PLOG(ERROR) << "read() returned half of a struct??";
        return -EFAULT;
    }
    *pendingReadsBufferSize = res / sizeof(*pendingReadsBuffer);
    return 0;
}

template <class PublicPendingRead, class RawPendingRead>
PublicPendingRead convertRead(RawPendingRead rawRead) {
    PublicPendingRead res = {
            .bootClockTsUs = rawRead.timestamp_us,
            .block = (IncFsBlockIndex)rawRead.block_index,
            .serialNo = rawRead.serial_number,
    };
    memcpy(&res.id.data, rawRead.file_id.bytes, sizeof(res.id.data));

    if constexpr (std::is_same_v<PublicPendingRead, IncFsReadInfoWithUid>) {
        if constexpr (std::is_same_v<RawPendingRead, incfs_pending_read_info2>) {
            res.uid = rawRead.uid;
        } else {
            res.uid = kIncFsNoUid;
        }
    }
    return res;
}

template <class RawPendingRead, class PublicPendingRead>
static int waitForReads(IncFsFd readFd, int32_t timeoutMs, PublicPendingRead buffer[],
                        size_t* bufferSize) {
    std::vector<RawPendingRead> pendingReads(*bufferSize);
    if (const auto res = waitForReadsImpl(readFd, timeoutMs, pendingReads.data(), bufferSize)) {
        return res;
    }
    for (size_t i = 0; i != *bufferSize; ++i) {
        buffer[i] = convertRead<PublicPendingRead>(pendingReads[i]);
    }
    return 0;
}

template <class PublicPendingRead>
static int waitForReads(IncFsFd readFd, int32_t timeoutMs, PublicPendingRead buffer[],
                        size_t* bufferSize) {
    if (features() & Features::v2) {
        return waitForReads<incfs_pending_read_info2>(readFd, timeoutMs, buffer, bufferSize);
    }
    return waitForReads<incfs_pending_read_info>(readFd, timeoutMs, buffer, bufferSize);
}

IncFsErrorCode IncFs_WaitForPendingReads(const IncFsControl* control, int32_t timeoutMs,
                                         IncFsReadInfo buffer[], size_t* bufferSize) {
    if (!control || control->pendingReads < 0) {
        return -EINVAL;
    }

    return waitForReads(control->pendingReads, timeoutMs, buffer, bufferSize);
}

IncFsErrorCode IncFs_WaitForPendingReadsWithUid(const IncFsControl* control, int32_t timeoutMs,
                                                IncFsReadInfoWithUid buffer[], size_t* bufferSize) {
    if (!control || control->pendingReads < 0) {
        return -EINVAL;
    }

    return waitForReads(control->pendingReads, timeoutMs, buffer, bufferSize);
}

IncFsErrorCode IncFs_WaitForPageReads(const IncFsControl* control, int32_t timeoutMs,
                                      IncFsReadInfo buffer[], size_t* bufferSize) {
    if (!control || control->logs < 0) {
        return -EINVAL;
    }

    return waitForReads(control->logs, timeoutMs, buffer, bufferSize);
}

IncFsErrorCode IncFs_WaitForPageReadsWithUid(const IncFsControl* control, int32_t timeoutMs,
                                             IncFsReadInfoWithUid buffer[], size_t* bufferSize) {
    if (!control || control->logs < 0) {
        return -EINVAL;
    }

    return waitForReads(control->logs, timeoutMs, buffer, bufferSize);
}

static IncFsFd openForSpecialOps(int cmd, const char* path) {
    ab::unique_fd fd(::open(path, O_RDONLY | O_CLOEXEC));
    if (fd < 0) {
        return -errno;
    }
    struct incfs_permit_fill args = {.file_descriptor = (uint32_t)fd.get()};
    auto err = ::ioctl(cmd, INCFS_IOC_PERMIT_FILL, &args);
    if (err < 0) {
        return -errno;
    }
    return fd.release();
}

IncFsFd IncFs_OpenForSpecialOpsByPath(const IncFsControl* control, const char* path) {
    if (!control) {
        return -EINVAL;
    }

    const auto pathRoot = registry().rootFor(path);
    const auto cmd = control->cmd;
    const auto root = rootForCmd(cmd);
    if (root.empty() || root != pathRoot) {
        return -EINVAL;
    }
    return openForSpecialOps(cmd, makeCommandPath(root, path).c_str());
}

IncFsFd IncFs_OpenForSpecialOpsById(const IncFsControl* control, IncFsFileId id) {
    if (!control) {
        return -EINVAL;
    }

    const auto cmd = control->cmd;
    const auto root = rootForCmd(cmd);
    if (root.empty()) {
        return -EINVAL;
    }
    auto name = indexPath(root, id);
    return openForSpecialOps(cmd, makeCommandPath(root, name).c_str());
}

static int writeBlocks(int fd, const incfs_fill_block blocks[], int blocksCount) {
    if (fd < 0 || blocksCount == 0) {
        return 0;
    }
    if (blocksCount < 0) {
        return -EINVAL;
    }

    auto ptr = blocks;
    const auto end = blocks + blocksCount;
    do {
        struct incfs_fill_blocks args = {.count = uint64_t(end - ptr),
                                         .fill_blocks = (uint64_t)(uintptr_t)ptr};
        const auto written = ::ioctl(fd, INCFS_IOC_FILL_BLOCKS, &args);
        if (written < 0) {
            if (errno == EINTR) {
                continue;
            }
            const auto error = errno;
            PLOG(WARNING) << "writing IncFS blocks failed";
            if (ptr == blocks) {
                return -error;
            }
            // something has been written, return a success here and let the
            // next call handle the error.
            break;
        }
        ptr += written;
    } while (ptr < end);
    return ptr - blocks;
}

IncFsErrorCode IncFs_WriteBlocks(const IncFsDataBlock blocks[], size_t blocksCount) {
    incfs_fill_block incfsBlocks[128];
    int writtenCount = 0;
    int incfsBlocksUsed = 0;
    int lastBlockFd = -1;
    for (size_t i = 0; i < blocksCount; ++i) {
        if (lastBlockFd != blocks[i].fileFd || incfsBlocksUsed == std::size(incfsBlocks)) {
            auto count = writeBlocks(lastBlockFd, incfsBlocks, incfsBlocksUsed);
            if (count > 0) {
                writtenCount += count;
            }
            if (count != incfsBlocksUsed) {
                return writtenCount ? writtenCount : count;
            }
            lastBlockFd = blocks[i].fileFd;
            incfsBlocksUsed = 0;
        }
        incfsBlocks[incfsBlocksUsed] = incfs_fill_block{
                .block_index = (uint32_t)blocks[i].pageIndex,
                .data_len = blocks[i].dataSize,
                .data = (uint64_t)blocks[i].data,
                .compression = (uint8_t)blocks[i].compression,
                .flags = uint8_t(blocks[i].kind == INCFS_BLOCK_KIND_HASH ? INCFS_BLOCK_FLAGS_HASH
                                                                         : 0),
        };
        ++incfsBlocksUsed;
    }
    auto count = writeBlocks(lastBlockFd, incfsBlocks, incfsBlocksUsed);
    if (count > 0) {
        writtenCount += count;
    }
    return writtenCount ? writtenCount : count;
}

IncFsErrorCode IncFs_BindMount(const char* sourceDir, const char* targetDir) {
    if (!enabled()) {
        return -ENOTSUP;
    }

    if (path::dirName(sourceDir) == path::procfsFdDir) {
        // can't find such path in the mount registry, but still can verify the filesystem
        // via the stat() call
        if (!isIncFsPathImpl(sourceDir)) {
            return -EINVAL;
        }
    } else {
        auto [sourceRoot, subpath] = registry().rootAndSubpathFor(sourceDir);
        if (sourceRoot.empty()) {
            return -EINVAL;
        }
        if (subpath.empty()) {
            LOG(WARNING) << "[incfs] Binding the root mount '" << sourceRoot << "' is not allowed";
            return -EINVAL;
        }
    }

    if (auto err = isValidMountTarget(targetDir); err != 0) {
        return err;
    }

    if (::mount(sourceDir, targetDir, nullptr, MS_BIND, nullptr)) {
        PLOG(ERROR) << "[incfs] Failed to bind mount '" << sourceDir << "' to '" << targetDir
                    << '\'';
        return -errno;
    }
    return 0;
}

IncFsErrorCode IncFs_Unmount(const char* dir) {
    if (!enabled()) {
        return -ENOTSUP;
    }
    if (!isIncFsPathImpl(dir)) {
        LOG(WARNING) << __func__ << ": umount() called on non-incfs directory '" << dir << '\'';
        return -EINVAL;
    }

    errno = 0;
    if (::umount2(dir, MNT_FORCE) == 0 || errno == EINVAL || errno == ENOENT) {
        // EINVAL - not a mount point, ENOENT - doesn't exist at all
        return -errno;
    }
    PLOG(WARNING) << __func__ << ": umount(force) failed, detaching '" << dir << '\'';
    errno = 0;
    if (!::umount2(dir, MNT_DETACH)) {
        return 0;
    }
    PLOG(WARNING) << __func__ << ": umount(detach) returned non-zero for '" << dir << '\'';
    return 0;
}

bool IncFs_IsIncFsFd(int fd) {
    return isIncFsFdImpl(fd);
}

bool IncFs_IsIncFsPath(const char* path) {
    return isIncFsPathImpl(path);
}

IncFsErrorCode IncFs_GetFilledRanges(int fd, IncFsSpan outBuffer, IncFsFilledRanges* filledRanges) {
    return IncFs_GetFilledRangesStartingFrom(fd, 0, outBuffer, filledRanges);
}

IncFsErrorCode IncFs_GetFilledRangesStartingFrom(int fd, int startBlockIndex, IncFsSpan outBuffer,
                                                 IncFsFilledRanges* filledRanges) {
    if (fd < 0) {
        return -EBADF;
    }
    if (startBlockIndex < 0) {
        return -EINVAL;
    }
    if (!outBuffer.data && outBuffer.size > 0) {
        return -EINVAL;
    }
    if (!filledRanges) {
        return -EINVAL;
    }
    // Use this to optimize the incfs call and have the same buffer for both the incfs and the
    // public structs.
    static_assert(sizeof(IncFsBlockRange) == sizeof(incfs_filled_range));

    *filledRanges = {};

    auto outStart = (IncFsBlockRange*)outBuffer.data;
    auto outEnd = outStart + outBuffer.size / sizeof(*outStart);

    auto outPtr = outStart;
    int error = 0;
    int dataBlocks;
    incfs_get_filled_blocks_args args = {};
    for (;;) {
        auto start = args.index_out ? args.index_out : startBlockIndex;
        args = incfs_get_filled_blocks_args{
                .range_buffer = (uint64_t)(uintptr_t)outPtr,
                .range_buffer_size = uint32_t((outEnd - outPtr) * sizeof(*outPtr)),
                .start_index = start,
        };
        errno = 0;
        auto res = ::ioctl(fd, INCFS_IOC_GET_FILLED_BLOCKS, &args);
        error = errno;
        if (res && error != EINTR && error != ERANGE) {
            return -error;
        }

        dataBlocks = args.data_blocks_out;
        outPtr += args.range_buffer_size_out / sizeof(incfs_filled_range);
        if (!res || error == ERANGE) {
            break;
        }
        // in case of EINTR we want to continue calling the function
    }

    if (outPtr > outEnd) {
        outPtr = outEnd;
        error = ERANGE;
    }

    filledRanges->endIndex = args.index_out;
    auto hashStartPtr = outPtr;
    if (outPtr != outStart) {
        // figure out the ranges for data block and hash blocks in the output
        for (; hashStartPtr != outStart; --hashStartPtr) {
            if ((hashStartPtr - 1)->begin < dataBlocks) {
                break;
            }
        }
        auto lastDataPtr = hashStartPtr - 1;
        // here we go, this is the first block that's before or at the hashes
        if (lastDataPtr->end <= dataBlocks) {
            ; // we're good, the boundary is between the ranges - |hashStartPtr| is correct
        } else {
            // the hard part: split the |lastDataPtr| range into the data and the hash pieces
            if (outPtr == outEnd) {
                // the buffer turned out to be too small, even though it actually wasn't
                error = ERANGE;
                if (hashStartPtr == outEnd) {
                    // this is even worse: there's no room to put even a single hash block into.
                    filledRanges->endIndex = lastDataPtr->end = dataBlocks;
                } else {
                    std::copy_backward(lastDataPtr, outPtr - 1, outPtr);
                    lastDataPtr->end = hashStartPtr->begin = dataBlocks;
                    filledRanges->endIndex = (outPtr - 1)->end;
                }
            } else {
                std::copy_backward(lastDataPtr, outPtr, outPtr + 1);
                lastDataPtr->end = hashStartPtr->begin = dataBlocks;
                ++outPtr;
            }
        }
        // now fix the indices of all hash blocks - no one should know they're simply past the
        // regular data blocks in the file!
        for (auto ptr = hashStartPtr; ptr != outPtr; ++ptr) {
            ptr->begin -= dataBlocks;
            ptr->end -= dataBlocks;
        }
    }

    filledRanges->dataRanges = outStart;
    filledRanges->dataRangesCount = hashStartPtr - outStart;
    filledRanges->hashRanges = hashStartPtr;
    filledRanges->hashRangesCount = outPtr - hashStartPtr;

    return -error;
}

static IncFsErrorCode isFullyLoadedV2(std::string_view root, IncFsFileId id) {
    if (::access(path::join(root, INCFS_INCOMPLETE_NAME, toStringImpl(id)).c_str(), F_OK)) {
        if (errno == ENOENT) {
            return 0; // no such incomplete file -> it's fully loaded.
        }
        return -errno;
    }
    return -ENODATA;
}

static IncFsErrorCode isFullyLoadedSlow(int fd) {
    char buffer[2 * sizeof(IncFsBlockRange)];
    IncFsFilledRanges ranges;
    auto res = IncFs_GetFilledRanges(fd, IncFsSpan{.data = buffer, .size = std::size(buffer)},
                                     &ranges);
    if (res == -ERANGE) {
        // need room for more than two ranges - definitely not fully loaded
        return -ENODATA;
    }
    if (res != 0) {
        return res;
    }
    // empty file
    if (ranges.endIndex == 0) {
        return 0;
    }
    // file with no hash tree
    if (ranges.dataRangesCount == 1 && ranges.hashRangesCount == 0) {
        return (ranges.dataRanges[0].begin == 0 && ranges.dataRanges[0].end == ranges.endIndex)
                ? 0
                : -ENODATA;
    }
    // file with a hash tree
    if (ranges.dataRangesCount == 1 && ranges.hashRangesCount == 1) {
        // calculate the expected data size from the size of the hash range and |endIndex|, which is
        // the total number of blocks in the file, both data and hash blocks together.
        if (ranges.hashRanges[0].begin != 0) {
            return -ENODATA;
        }
        const auto expectedDataBlocks =
                ranges.endIndex - (ranges.hashRanges[0].end - ranges.hashRanges[0].begin);
        return (ranges.dataRanges[0].begin == 0 && ranges.dataRanges[0].end == expectedDataBlocks)
                ? 0
                : -ENODATA;
    }
    return -ENODATA;
}

IncFsErrorCode IncFs_IsFullyLoaded(int fd) {
    if (features() & Features::v2) {
        const auto fdPath = path::fromFd(fd);
        if (fdPath.empty()) {
            return errno ? -errno : -EINVAL;
        }
        const auto id = getId(::fgetxattr, fd);
        if (id == kIncFsInvalidFileId) {
            return -errno;
        }
        return isFullyLoadedV2(registry().rootFor(fdPath), id);
    }
    return isFullyLoadedSlow(fd);
}
IncFsErrorCode IncFs_IsFullyLoadedByPath(const IncFsControl* control, const char* path) {
    if (!control || !path) {
        return -EINVAL;
    }
    const auto root = rootForCmd(control->cmd);
    if (root.empty()) {
        return -EINVAL;
    }
    const auto pathRoot = registry().rootFor(path);
    if (pathRoot != root) {
        return -EINVAL;
    }
    if (features() & Features::v2) {
        const auto id = getId(::getxattr, path);
        if (id == kIncFsInvalidFileId) {
            return -ENOTSUP;
        }
        return isFullyLoadedV2(root, id);
    }
    auto fd = ab::unique_fd(openForSpecialOps(control->cmd, makeCommandPath(root, path).c_str()));
    return isFullyLoadedSlow(fd.get());
}
IncFsErrorCode IncFs_IsFullyLoadedById(const IncFsControl* control, IncFsFileId fileId) {
    if (!control) {
        return -EINVAL;
    }
    const auto root = rootForCmd(control->cmd);
    if (root.empty()) {
        return -EINVAL;
    }
    if (features() & Features::v2) {
        return isFullyLoadedV2(root, fileId);
    }
    auto fd = ab::unique_fd(
            openForSpecialOps(control->cmd,
                              makeCommandPath(root, indexPath(root, fileId)).c_str()));
    return isFullyLoadedSlow(fd.get());
}

static IncFsErrorCode isEverythingLoadedV2(const IncFsControl* control) {
    const auto root = rootForCmd(control->cmd);
    if (root.empty()) {
        return -EINVAL;
    }
    auto res = forEachFileIn(path::join(root, INCFS_INCOMPLETE_NAME), [](auto) { return false; });
    return res < 0 ? res : res > 0 ? -ENODATA : 0;
}

static IncFsErrorCode isEverythingLoadedSlow(const IncFsControl* control) {
    const auto root = rootForCmd(control->cmd);
    if (root.empty()) {
        return -EINVAL;
    }
    // No special API for this version of the driver, need to recurse and check each file
    // separately. Can at least speed it up by iterating over the .index/ dir and not dealing with
    // the directory tree.
    const auto indexPath = path::join(root, INCFS_INDEX_NAME);
    const auto dir = path::openDir(indexPath.c_str());
    if (!dir) {
        return -EINVAL;
    }
    while (const auto entry = ::readdir(dir.get())) {
        if (entry->d_type != DT_REG) {
            continue;
        }
        const auto name = path::join(indexPath, entry->d_name);
        auto fd =
                ab::unique_fd(openForSpecialOps(control->cmd, makeCommandPath(root, name).c_str()));
        if (fd.get() < 0) {
            PLOG(WARNING) << __func__ << "(): can't open " << entry->d_name << " for special ops";
            return fd.release();
        }
        const auto checkFullyLoaded = IncFs_IsFullyLoaded(fd.get());
        if (checkFullyLoaded == 0 || checkFullyLoaded == -EOPNOTSUPP ||
            checkFullyLoaded == -ENOTSUP || checkFullyLoaded == -ENOENT) {
            // special kinds of files may return an error here, but it still means
            // _this_ file is OK - you simply need to check the rest. E.g. can't query
            // a mapped file, instead need to check its parent.
            continue;
        }
        return checkFullyLoaded;
    }
    return 0;
}

IncFsErrorCode IncFs_IsEverythingFullyLoaded(const IncFsControl* control) {
    if (!control) {
        return -EINVAL;
    }
    if (features() & Features::v2) {
        return isEverythingLoadedV2(control);
    }
    return isEverythingLoadedSlow(control);
}

IncFsErrorCode IncFs_SetUidReadTimeouts(const IncFsControl* control,
                                        const IncFsUidReadTimeouts timeouts[], size_t count) {
    if (!control) {
        return -EINVAL;
    }
    if (!(features() & Features::v2)) {
        return -ENOTSUP;
    }

    std::vector<incfs_per_uid_read_timeouts> argTimeouts(count);
    for (size_t i = 0; i != count; ++i) {
        argTimeouts[i] = incfs_per_uid_read_timeouts{
                .uid = (uint32_t)timeouts[i].uid,
                .min_time_us = timeouts[i].minTimeUs,
                .min_pending_time_us = timeouts[i].minPendingTimeUs,
                .max_pending_time_us = timeouts[i].maxPendingTimeUs,
        };
    }
    incfs_set_read_timeouts_args args = {.timeouts_array = (uint64_t)(uintptr_t)argTimeouts.data(),
                                         .timeouts_array_size = uint32_t(
                                                 argTimeouts.size() * sizeof(*argTimeouts.data()))};
    if (::ioctl(control->cmd, INCFS_IOC_SET_READ_TIMEOUTS, &args)) {
        PLOG(WARNING) << "[incfs] setUidReadTimeouts failed";
        return -errno;
    }
    return 0;
}

IncFsErrorCode IncFs_GetUidReadTimeouts(const IncFsControl* control,
                                        IncFsUidReadTimeouts timeouts[], size_t* bufferSize) {
    if (!control || !bufferSize) {
        return -EINVAL;
    }
    if (!(features() & Features::v2)) {
        return -ENOTSUP;
    }

    std::vector<incfs_per_uid_read_timeouts> argTimeouts(*bufferSize);
    incfs_get_read_timeouts_args args = {.timeouts_array = (uint64_t)(uintptr_t)argTimeouts.data(),
                                         .timeouts_array_size = uint32_t(
                                                 argTimeouts.size() * sizeof(*argTimeouts.data())),
                                         .timeouts_array_size_out = args.timeouts_array_size};
    if (::ioctl(control->cmd, INCFS_IOC_GET_READ_TIMEOUTS, &args)) {
        if (errno == E2BIG) {
            *bufferSize = args.timeouts_array_size_out / sizeof(*argTimeouts.data());
        }
        return -errno;
    }

    *bufferSize = args.timeouts_array_size_out / sizeof(*argTimeouts.data());
    for (size_t i = 0; i != *bufferSize; ++i) {
        timeouts[i].uid = argTimeouts[i].uid;
        timeouts[i].minTimeUs = argTimeouts[i].min_time_us;
        timeouts[i].minPendingTimeUs = argTimeouts[i].min_pending_time_us;
        timeouts[i].maxPendingTimeUs = argTimeouts[i].max_pending_time_us;
    }
    return 0;
}

// Trying to detect if this is a mapped file.
// Not the best way as it might return true for other system files.
// TODO: remove after IncFS returns ENOTSUP for such files.
static bool isMapped(int fd) {
    char buffer[kIncFsFileIdStringLength];
    const auto res = ::fgetxattr(fd, kIdAttrName, buffer, sizeof(buffer));
    return res != sizeof(buffer);
}

static IncFsErrorCode getFileBlockCount(int fd, IncFsBlockCounts* blockCount) {
    if (isMapped(fd)) {
        return -ENOTSUP;
    }

    incfs_get_block_count_args args = {};
    auto res = ::ioctl(fd, INCFS_IOC_GET_BLOCK_COUNT, &args);
    if (res < 0) {
        return -errno;
    }
    *blockCount = IncFsBlockCounts{
            .totalDataBlocks = args.total_data_blocks_out,
            .filledDataBlocks = args.filled_data_blocks_out,
            .totalHashBlocks = args.total_hash_blocks_out,
            .filledHashBlocks = args.filled_hash_blocks_out,
    };
    return 0;
}

IncFsErrorCode IncFs_GetFileBlockCountById(const IncFsControl* control, IncFsFileId id,
                                           IncFsBlockCounts* blockCount) {
    if (!control) {
        return -EINVAL;
    }
    if (!(features() & Features::v2)) {
        return -ENOTSUP;
    }
    const auto root = rootForCmd(control->cmd);
    if (root.empty()) {
        return -EINVAL;
    }
    auto name = indexPath(root, id);
    auto fd = openRaw(name);
    if (fd < 0) {
        return fd.get();
    }
    return getFileBlockCount(fd, blockCount);
}

IncFsErrorCode IncFs_GetFileBlockCountByPath(const IncFsControl* control, const char* path,
                                             IncFsBlockCounts* blockCount) {
    if (!control) {
        return -EINVAL;
    }
    if (!(features() & Features::v2)) {
        return -ENOTSUP;
    }
    const auto pathRoot = registry().rootFor(path);
    const auto root = rootForCmd(control->cmd);
    if (root.empty() || root != pathRoot) {
        return -EINVAL;
    }
    auto fd = openRaw(path);
    if (fd < 0) {
        return fd.get();
    }
    return getFileBlockCount(fd, blockCount);
}

IncFsErrorCode IncFs_ListIncompleteFiles(const IncFsControl* control, IncFsFileId ids[],
                                         size_t* bufferSize) {
    if (!control || !bufferSize) {
        return -EINVAL;
    }
    if (!(features() & Features::v2)) {
        return -ENOTSUP;
    }
    const auto root = rootForCmd(control->cmd);
    if (root.empty()) {
        return -EINVAL;
    }
    size_t index = 0;
    int error = 0;
    const auto res = forEachFileIn(path::join(root, INCFS_INCOMPLETE_NAME), [&](const char* name) {
        if (index >= *bufferSize) {
            error = -E2BIG;
        } else {
            ids[index] = IncFs_FileIdFromString(name);
        }
        ++index;
        return true;
    });
    if (res < 0) {
        return res;
    }
    *bufferSize = index;
    return error ? error : 0;
}

IncFsErrorCode IncFs_ForEachFile(const IncFsControl* control, void* context, FileCallback cb) {
    if (!control || !cb) {
        return -EINVAL;
    }
    const auto root = rootForCmd(control->cmd);
    if (root.empty()) {
        return -EINVAL;
    }
    return forEachFileIn(path::join(root, INCFS_INDEX_NAME), [&](const char* name) {
        return cb(context, control, IncFs_FileIdFromString(name));
    });
}

IncFsErrorCode IncFs_ForEachIncompleteFile(const IncFsControl* control, void* context,
                                           FileCallback cb) {
    if (!control || !cb) {
        return -EINVAL;
    }
    if (!(features() & Features::v2)) {
        return -ENOTSUP;
    }
    const auto root = rootForCmd(control->cmd);
    if (root.empty()) {
        return -EINVAL;
    }
    return forEachFileIn(path::join(root, INCFS_INCOMPLETE_NAME), [&](const char* name) {
        return cb(context, control, IncFs_FileIdFromString(name));
    });
}

IncFsErrorCode IncFs_WaitForLoadingComplete(const IncFsControl* control, int32_t timeoutMs) {
    if (!control) {
        return -EINVAL;
    }
    if (!(features() & Features::v2)) {
        return -ENOTSUP;
    }

    using namespace std::chrono;
    auto hrTimeout = steady_clock::duration(milliseconds(timeoutMs));

    const auto root = rootForCmd(control->cmd);
    if (root.empty()) {
        return -EINVAL;
    }

    ab::unique_fd fd(inotify_init1(IN_NONBLOCK | IN_CLOEXEC));
    if (!fd.ok()) {
        return -EFAULT;
    }

    // first create all the watches, and only then list existing files to prevent races
    auto dirPath = path::join(root, INCFS_INCOMPLETE_NAME);
    int watchFd = inotify_add_watch(fd.get(), dirPath.c_str(), IN_DELETE);
    if (watchFd < 0) {
        return -errno;
    }

    size_t count = 0;
    auto res = IncFs_ListIncompleteFiles(control, nullptr, &count);
    if (!res) {
        return 0;
    }
    if (res != -E2BIG) {
        return res;
    }

    while (hrTimeout > hrTimeout.zero()) {
        const auto startTs = steady_clock::now();

        pollfd pfd = {fd.get(), POLLIN, 0};
        const auto res = ::poll(&pfd, 1, duration_cast<milliseconds>(hrTimeout).count());
        if (res == 0) {
            return -ETIMEDOUT;
        }
        if (res < 0) {
            const auto error = errno;
            if (error != EINTR) {
                PLOG(ERROR) << "poll() failed";
                return -error;
            }
        } else {
            // empty the inotify fd first to not miss any new deletions,
            // then check if the directory is empty.
            char buffer[sizeof(inotify_event) + NAME_MAX + 1];
            for (;;) {
                auto err = TEMP_FAILURE_RETRY(::read(fd.get(), buffer, sizeof(buffer)));
                if (err < 0) {
                    if (errno == EAGAIN) { // no new events
                        break;
                    }
                    return -errno;
                }
            }

            size_t count = 0;
            auto res = IncFs_ListIncompleteFiles(control, nullptr, &count);
            if (!res) {
                return 0;
            }
            if (res != -E2BIG) {
                return res;
            }
        }
        hrTimeout -= steady_clock::now() - startTs;
    }

    return -ETIMEDOUT;
}

IncFsErrorCode IncFs_WaitForFsWrittenBlocksChange(const IncFsControl* control, int32_t timeoutMs,
                                                  IncFsSize* count) {
    if (!control || !count) {
        return -EINVAL;
    }
    if (!(features() & Features::v2)) {
        return -ENOTSUP;
    }

    using namespace std::chrono;
    auto hrTimeout = steady_clock::duration(milliseconds(timeoutMs));

    while (hrTimeout > hrTimeout.zero()) {
        const auto startTs = steady_clock::now();

        pollfd pfd = {control->blocksWritten, POLLIN, 0};
        const auto res = ::poll(&pfd, 1, duration_cast<milliseconds>(hrTimeout).count());
        if (res > 0) {
            break;
        }
        if (res == 0) {
            return -ETIMEDOUT;
        }
        const auto error = errno;
        if (error != EINTR) {
            PLOG(ERROR) << "poll() failed";
            return -error;
        }
        hrTimeout -= steady_clock::now() - startTs;
    }

    char str[32];
    auto size = ::read(control->blocksWritten, str, sizeof(str));
    if (size < 0) {
        const auto error = errno;
        PLOG(ERROR) << "read() failed";
        return -error;
    }
    const auto res = std::from_chars(str, str + size, *count);
    if (res.ec != std::errc{}) {
        return res.ec == std::errc::invalid_argument ? -EINVAL : -ERANGE;
    }

    return 0;
}

static IncFsErrorCode reserveSpace(const char* backingPath, IncFsSize size) {
    auto fd = ab::unique_fd(::open(backingPath, O_WRONLY | O_CLOEXEC));
    if (fd < 0) {
        return -errno;
    }
    struct stat st = {};
    if (::fstat(fd.get(), &st)) {
        return -errno;
    }
    if (size == kIncFsTrimReservedSpace) {
        if (::ftruncate(fd.get(), st.st_size)) {
            return -errno;
        }
    } else {
        // Add 1.5% of the size for the hash tree and the blockmap, and some more blocks
        // for fixed overhead.
        // hash tree is ~33 bytes / page, and blockmap is 10 bytes / page
        // no need to round to a page size as filesystems already do that.
        const auto backingSize = IncFsSize(size * 1.015) + INCFS_DATA_FILE_BLOCK_SIZE * 4;
        if (backingSize < st.st_size) {
            return -EPERM;
        }
        if (::fallocate(fd.get(), FALLOC_FL_KEEP_SIZE, 0, backingSize)) {
            return -errno;
        }
    }
    return 0;
}

IncFsErrorCode IncFs_ReserveSpaceByPath(const IncFsControl* control, const char* path,
                                        IncFsSize size) {
    if (!control || (size != kIncFsTrimReservedSpace && size < 0)) {
        return -EINVAL;
    }
    const auto [pathRoot, backingRoot, subpath] = registry().detailsFor(path);
    const auto root = rootForCmd(control->cmd);
    if (root.empty() || root != pathRoot) {
        return -EINVAL;
    }
    return reserveSpace(path::join(backingRoot, subpath).c_str(), size);
}

IncFsErrorCode IncFs_ReserveSpaceById(const IncFsControl* control, IncFsFileId id, IncFsSize size) {
    if (!control || (size != kIncFsTrimReservedSpace && size < 0)) {
        return -EINVAL;
    }
    const auto root = rootForCmd(control->cmd);
    if (root.empty()) {
        return -EINVAL;
    }
    auto path = indexPath(root, id);
    const auto [pathRoot, backingRoot, subpath] = registry().detailsFor(path);
    if (root != pathRoot) {
        return -EINVAL;
    }
    return reserveSpace(path::join(backingRoot, subpath).c_str(), size);
}

template <class IntType>
static int readIntFromFile(std::string_view rootDir, std::string_view subPath, IntType& result) {
    std::string content;
    if (!ab::ReadFileToString(path::join(rootDir, subPath), &content)) {
        PLOG(ERROR) << "IncFs_GetMetrics: failed to read file: " << rootDir << "/" << subPath;
        return -errno;
    }
    const auto res = std::from_chars(content.data(), content.data() + content.size(), result);
    if (res.ec != std::errc()) {
        return -static_cast<int>(res.ec);
    }
    return 0;
}

IncFsErrorCode IncFs_GetMetrics(const char* sysfsName, IncFsMetrics* metrics) {
    if (!sysfsName || !*sysfsName) {
        return -EINVAL;
    }

    const auto kSysfsMetricsDir =
            ab::StringPrintf("/sys/fs/%s/instances/%s", INCFS_NAME, sysfsName);

    int err;
    if (err = readIntFromFile(kSysfsMetricsDir, "reads_delayed_min", metrics->readsDelayedMin);
        err != 0) {
        return err;
    }
    if (err = readIntFromFile(kSysfsMetricsDir, "reads_delayed_min_us", metrics->readsDelayedMinUs);
        err != 0) {
        return err;
    }
    if (err = readIntFromFile(kSysfsMetricsDir, "reads_delayed_pending",
                              metrics->readsDelayedPending);
        err != 0) {
        return err;
    }
    if (err = readIntFromFile(kSysfsMetricsDir, "reads_delayed_pending_us",
                              metrics->readsDelayedPendingUs);
        err != 0) {
        return err;
    }
    if (err = readIntFromFile(kSysfsMetricsDir, "reads_failed_hash_verification",
                              metrics->readsFailedHashVerification);
        err != 0) {
        return err;
    }
    if (err = readIntFromFile(kSysfsMetricsDir, "reads_failed_other", metrics->readsFailedOther);
        err != 0) {
        return err;
    }
    if (err = readIntFromFile(kSysfsMetricsDir, "reads_failed_timed_out",
                              metrics->readsFailedTimedOut);
        err != 0) {
        return err;
    }
    return 0;
}

IncFsErrorCode IncFs_GetLastReadError(const IncFsControl* control,
                                      IncFsLastReadError* lastReadError) {
    if (!control) {
        return -EINVAL;
    }
    if (!(features() & Features::v2)) {
        return -ENOTSUP;
    }
    incfs_get_last_read_error_args args = {};
    auto res = ::ioctl(control->cmd, INCFS_IOC_GET_LAST_READ_ERROR, &args);
    if (res < 0) {
        PLOG(ERROR) << "[incfs] IncFs_GetLastReadError failed.";
        return -errno;
    }
    *lastReadError = IncFsLastReadError{
            .timestampUs = args.time_us_out,
            .block = static_cast<IncFsBlockIndex>(args.page_out),
            .errorNo = args.errno_out,
            .uid = static_cast<IncFsUid>(args.uid_out),
    };
    static_assert(sizeof(args.file_id_out.bytes) == sizeof(lastReadError->id.data));
    memcpy(lastReadError->id.data, args.file_id_out.bytes, sizeof(args.file_id_out.bytes));
    return 0;
}

MountRegistry& android::incfs::defaultMountRegistry() {
    return registry();
}