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
//! Implementation of FUSE operations for the OverlayFS.
//!
//! This module translates high-level filesystem requests (lookup, read, write, etc.)
//! into layer-aware operations, managing the coordination between the upper
//! (read-write) and lower (read-only) layers.
use crate::files::OverlayFiles;
use crate::inode::{InodeMode, InodeStore};
use crate::layers::{LayerManager, WH_PREFIX};
use fuser::{
AccessFlags, BsdFileFlags, CopyFileRangeFlags, Errno, FileAttr, FileHandle, FileType,
Filesystem, FopenFlags, Generation, INodeNo, LockOwner, OpenFlags, RenameFlags, ReplyAttr,
ReplyCreate, ReplyData, ReplyDirectory, ReplyDirectoryPlus, ReplyEmpty, ReplyEntry, ReplyLseek,
ReplyOpen, ReplyStatfs, ReplyWrite, ReplyXattr, Request, TimeOrNow, WriteFlags,
};
use std::collections::HashSet;
use std::ffi::{CString, OsStr};
use std::fs::{self, OpenOptions};
use std::io::{Read, Seek, SeekFrom, Write};
use std::os::unix::ffi::OsStrExt;
use std::os::unix::fs::{FileTypeExt, MetadataExt, PermissionsExt};
use std::path::Path;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use zerocopy::IntoBytes;
/// TTL for upper-layer entries: always revalidate since the upper layer is mutable.
const TTL: Duration = Duration::ZERO;
/// Provides the core logic for FUSE callbacks, managing inodes and layer resolution.
pub struct OverlayOps {
/// Storage and mapping for virtual and persistent inodes.
pub inodes: InodeStore,
/// Manager responsible for layer resolution and Copy-on-Write (CoW) logic.
pub layers: LayerManager,
}
impl OverlayOps {
/// Creates a new `OverlayOps` instance.
///
/// # Arguments
/// * `backend` - Configuration containing the paths for lower, upper, and work directories.
/// * `mode` - The strategy for inode generation (Virtual or Persistent).
///
/// # Returns
/// * A new instance of `OverlayOps`.
pub fn new(backend: OverlayFiles, mode: InodeMode) -> Self {
Self {
inodes: InodeStore::new(mode),
layers: LayerManager::new(backend),
}
}
/// Builds a FUSE `FileAttr` from an inode and host filesystem metadata.
///
/// # Arguments
/// * `ino` - The inode number to assign to the attributes.
/// * `meta` - Metadata from the underlying host filesystem.
///
/// # Returns
/// * A `FileAttr` structure compatible with the FUSE protocol.
fn make_attr(&self, ino: INodeNo, meta: &fs::Metadata) -> FileAttr {
let ft = meta.file_type();
let kind = if ft.is_dir() {
FileType::Directory
} else if ft.is_symlink() {
FileType::Symlink
} else if ft.is_fifo() {
FileType::NamedPipe
} else if ft.is_socket() {
FileType::Socket
} else if ft.is_char_device() {
FileType::CharDevice
} else if ft.is_block_device() {
FileType::BlockDevice
} else {
FileType::RegularFile
};
FileAttr {
ino,
size: meta.len(),
blocks: meta.blocks(),
atime: meta.accessed().unwrap_or(SystemTime::now()),
mtime: meta.modified().unwrap_or(SystemTime::now()),
ctime: if meta.ctime() >= 0 {
UNIX_EPOCH + Duration::from_secs(meta.ctime() as u64)
} else {
UNIX_EPOCH
},
crtime: SystemTime::now(),
kind,
perm: meta.mode() as u16,
nlink: meta.nlink() as u32,
uid: meta.uid(),
gid: meta.gid(),
rdev: meta.rdev() as u32,
blksize: meta.blksize() as u32,
flags: 0,
}
}
/// Maps a `DirEntry`'s file type to a FUSE `FileType`, covering all Unix types.
///
/// This helper is used during directory listing (`readdir`) to ensure that the
/// entry type reported to the FUSE client matches the actual type on the host
/// filesystem (e.g., distinguishing between regular files, directories, and sockets).
///
/// # Arguments
/// * `entry` - A reference to the standard library's `fs::DirEntry` found during directory iteration.
///
/// # Returns
/// * The corresponding `fuser::FileType` (Directory, Symlink, NamedPipe, Socket,
/// CharDevice, BlockDevice, or RegularFile as a fallback).
fn entry_file_type(entry: &fs::DirEntry) -> FileType {
match entry.file_type() {
Ok(ft) if ft.is_dir() => FileType::Directory,
Ok(ft) if ft.is_symlink() => FileType::Symlink,
Ok(ft) if ft.is_fifo() => FileType::NamedPipe,
Ok(ft) if ft.is_socket() => FileType::Socket,
Ok(ft) if ft.is_char_device() => FileType::CharDevice,
Ok(ft) if ft.is_block_device() => FileType::BlockDevice,
_ => FileType::RegularFile,
}
}
/// Recursively copies a directory tree from source to destination.
///
/// Used during renames of directories that only exist in the lower layer.
///
/// # Arguments
/// * `src` - Path to the source directory.
/// * `dst` - Path to the destination directory.
///
/// # Returns
/// * `Ok(())` on success, or an `std::io::Result` on failure.
fn copy_dir_all(src: &Path, dst: &Path) -> std::io::Result<()> {
fs::create_dir_all(dst)?;
let src_meta = fs::symlink_metadata(src)?;
fs::set_permissions(dst, src_meta.permissions())?;
Self::copy_ownership_and_times(src, dst, &src_meta);
for entry in fs::read_dir(src)? {
let entry = entry?;
let src_path = entry.path();
let dst_path = dst.join(entry.file_name());
let ft = entry.file_type()?;
let entry_meta = fs::symlink_metadata(&src_path)?;
if ft.is_dir() {
Self::copy_dir_all(&src_path, &dst_path)?;
} else if ft.is_symlink() {
let target = fs::read_link(&src_path)?;
std::os::unix::fs::symlink(&target, &dst_path)?;
Self::copy_ownership_and_times(&src_path, &dst_path, &entry_meta);
} else {
fs::copy(&src_path, &dst_path)?;
fs::set_permissions(&dst_path, entry_meta.permissions())?;
Self::copy_ownership_and_times(&src_path, &dst_path, &entry_meta);
}
}
Ok(())
}
/// Copies uid, gid, and atime/mtime from `src` metadata onto `dst` without following symlinks.
/// Applies uid, gid, atime, and mtime from `meta` onto `dst` without following symlinks.
///
/// Use `lchown(2)` and `utimensat(2)` with `AT_SYMLINK_NOFOLLOW` so that symlinks
/// themselves are updated rather than their targets.
///
/// # Arguments
/// * `src` - Unused; kept for call-site symmetry with `fs::copy`. The caller has
/// already got the metadata via `fs::symlink_metadata(src)`.
/// * `dst` - The destination path whose ownership and timestamps will be updated.
/// * `meta` - Metadata from the source entry, providing uid, gid, atime, and mtime.
fn copy_ownership_and_times(src: &Path, dst: &Path, meta: &fs::Metadata) {
let _ = src;
let Ok(dst_c) = CString::new(dst.as_os_str().as_bytes()) else {
return;
};
unsafe {
libc::lchown(dst_c.as_ptr(), meta.uid(), meta.gid());
let times = [
libc::timespec {
tv_sec: meta.atime() as _, // FIX libc::time_m 32bit
tv_nsec: meta.atime_nsec() as libc::c_long,
},
libc::timespec {
tv_sec: meta.mtime() as _, // FIX libc::time_m 32bit
tv_nsec: meta.mtime_nsec() as libc::c_long,
},
];
libc::utimensat(
libc::AT_FDCWD,
dst_c.as_ptr(),
times.as_ptr(),
libc::AT_SYMLINK_NOFOLLOW,
);
}
}
/// Opens `path` with `O_WRONLY` and returns the raw fd.
///
/// This is a low-level helper that wraps the `libc::open` system call to get
/// a write-only file descriptor. It is particularly useful for operations
/// like `fallocate` or `copy_file_range` that require raw descriptors.
///
/// # Safety
/// Caller is responsible for closing the fd with `libc::close` to prevent resource leaks.
///
/// # Arguments
/// * `path` - The filesystem path to the file to be opened.
///
/// # Returns
/// * `Ok(libc::c_int)` containing the raw file descriptor on success.
/// * `Err` if the path contains null bytes or the system call fails.
fn open_wronly_fd(path: &Path) -> std::io::Result<libc::c_int> {
let path_c = CString::new(path.as_os_str().as_bytes())
.map_err(|_| std::io::Error::from_raw_os_error(libc::EINVAL))?;
let fd = unsafe { libc::open(path_c.as_ptr(), libc::O_WRONLY) };
if fd < 0 {
Err(std::io::Error::last_os_error())
} else {
Ok(fd)
}
}
/// Opens `path` with `O_RDONLY` and returns the raw fd.
///
/// This helper wraps the `libc::open` system call to provide a read-only
/// file descriptor. It is used when a raw descriptor is needed for system
/// calls that do not require write access, such as the source in `copy_file_range`.
///
/// # Safety
/// Caller is responsible for closing the fd with `libc::close` to avoid leaking
/// file descriptors.
///
/// # Arguments
/// * `path` - The filesystem path to the file to be opened.
///
/// # Returns
/// * `Ok(libc::c_int)` containing the raw file descriptor on success.
/// * `Err` if the path conversion fails or the `open` call returns an error.
fn open_rdonly_fd(path: &Path) -> std::io::Result<libc::c_int> {
let path_c = CString::new(path.as_os_str().as_bytes())
.map_err(|_| std::io::Error::from_raw_os_error(libc::EINVAL))?;
let fd = unsafe { libc::open(path_c.as_ptr(), libc::O_RDONLY) };
if fd < 0 {
Err(std::io::Error::last_os_error())
} else {
Ok(fd)
}
}
/// Checks whether an upper-layer directory is marked as opaque.
///
/// An opaque directory completely replaces its lower-layer counterpart: none of
/// the lower entries should be visible through the merge. The Linux kernel overlayfs
/// sets `trusted.overlay.opaque = y` on such directories; unprivileged tools like
/// bwrap and proot use the `user.overlay.opaque` namespace instead.
///
/// # Arguments
/// * `upper_dir` - Absolute path to the directory in the upper layer to inspect.
///
/// # Returns
/// * `true` if either xattr is present and set to `"y"`, `false` otherwise
/// (including when the path contains null bytes or the xattr syscall fails).
fn is_opaque_dir(upper_dir: &std::path::PathBuf) -> bool {
let Ok(path_c) = CString::new(upper_dir.as_os_str().as_bytes()) else {
return false;
};
for name in [
b"trusted.overlay.opaque\0" as &[u8],
b"user.overlay.opaque\0",
] {
let name_ptr = name.as_ptr() as *const libc::c_char;
let mut val = [0u8; 2];
let len = unsafe {
libc::lgetxattr(
path_c.as_ptr(),
name_ptr,
val.as_mut_ptr() as *mut libc::c_void,
val.len(),
)
};
if len == 1 && val[0] == b'y' {
return true;
}
}
false
}
}
impl Filesystem for OverlayOps {
/// Looks up a directory entry by name and returns its attributes.
///
/// This method resolves whether a file exists in the upper or lower layer,
/// while checking for whiteouts that might hide lower-layer entries.
///
/// # Arguments
/// * `_req` - The FUSE request context (PID, UID, GID of the caller).
/// * `parent` - The Inode number of the parent directory.
/// * `name` - The filename to look up within the parent directory.
/// * `reply` - The callback to send the lookup result (entry attributes) or an error.
///
/// # Returns
/// * Calls `reply.entry` with metadata if found.
/// * Call `reply.error` with `ENOENT` if the file is hidden or non-existent.
fn lookup(&self, _req: &Request, parent: INodeNo, name: &OsStr, reply: ReplyEntry) {
let rel = self.inodes.child_path(parent, name);
if self.layers.is_hidden(&rel) {
return reply.error(Errno::from_i32(libc::ENOENT));
}
let Some((full, _)) = self.layers.resolve(&rel) else {
return reply.error(Errno::from_i32(libc::ENOENT));
};
match fs::symlink_metadata(&full) {
Ok(meta) => {
let ino = self.inodes.get_ino(&rel);
reply.entry(&TTL, &self.make_attr(ino, &meta), Generation(0));
}
Err(_) => reply.error(Errno::from_i32(libc::ENOENT)),
}
}
/// Notifies the filesystem that the kernel no longer tracks an inode.
///
/// # Arguments
/// * `_req` - The FUSE request context.
/// * `ino` - The raw inode number being forgotten.
/// * `_nlookup` - The number of lookups the kernel is forgetting.
///
/// # Returns
/// * This function does not return a value, as the FUSE protocol does not
/// expect a reply for the `forget` operation.
fn forget(&self, _req: &Request, ino: INodeNo, nlookup: u64) {
self.inodes.forget_ino(ino, nlookup);
}
/// Gets file attributes (stat).
///
/// # Arguments
/// * `_req` - The FUSE request context.
/// * `ino` - The Inode number of the target file/directory.
/// * `_fh` - An optional file handle if the file is already open.
/// * `reply` - The callback to send the `FileAttr` structure.
///
/// # Returns
/// * Calls `reply.attr` with the resolved metadata.
/// * Call `reply.error` if the inode or path is invalid.
fn getattr(&self, _req: &Request, ino: INodeNo, _fh: Option<FileHandle>, reply: ReplyAttr) {
let Some(path) = self.inodes.get_path(ino) else {
return reply.error(Errno::from_i32(libc::ENOENT));
};
if self.layers.is_hidden(&path) {
return reply.error(Errno::from_i32(libc::ENOENT));
}
let Some((full, _)) = self.layers.resolve(&path) else {
return reply.error(Errno::from_i32(libc::ENOENT));
};
match fs::symlink_metadata(&full) {
Ok(meta) => reply.attr(&TTL, &self.make_attr(ino, &meta)),
Err(_) => reply.error(Errno::from_i32(libc::ENOENT)),
}
}
/// Sets file attributes (chmod, chown, truncate, utimes).
///
/// This operation triggers a Copy-on-Write (CoW) if the file resides in the lower layer,
/// as metadata changes must be persisted in the upper layer.
///
/// # Arguments
/// * `_req` - The FUSE request context.
/// * `ino` - Inode number of the target.
/// * `mode` - Optional new file permissions.
/// * `uid` - Optional new user ID.
/// * `gid` - Optional new group ID.
/// * `size` - Optional new size (for truncation).
/// * `atime` - Optional new access time.
/// * `mtime` - Optional new modification time.
/// * `ctime` - Optional new change time.
/// * `_fh` - Optional file handle.
/// * `_crtime` - Optional creation time (macOS).
/// * `_chgtime` - Optional attribute change time.
/// * `_bkuptime` - Optional backup time.
/// * `_flags` - Optional BSD file flags.
/// * `reply` - Callback to return the updated attributes.
///
/// # Returns
/// * Calls `reply.attr` after applying changes in the upper layer.
/// * Call `reply.error` on permission or I/O failures.
fn setattr(
&self,
_req: &Request,
ino: INodeNo,
mode: Option<u32>,
uid: Option<u32>,
gid: Option<u32>,
size: Option<u64>,
atime: Option<TimeOrNow>,
mtime: Option<TimeOrNow>,
_ctime: Option<SystemTime>,
_fh: Option<FileHandle>,
_crtime: Option<SystemTime>,
_chgtime: Option<SystemTime>,
_bkuptime: Option<SystemTime>,
_flags: Option<BsdFileFlags>,
reply: ReplyAttr,
) {
let Some(path) = self.inodes.get_path(ino) else {
return reply.error(Errno::from_i32(libc::ENOENT));
};
let upper = match self.layers.copy_on_write(&path) {
Ok(p) => p,
Err(e) => return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO))),
};
let is_symlink = fs::symlink_metadata(&upper)
.map(|m| m.file_type().is_symlink())
.unwrap_or(false);
if let Some(new_size) = size {
if is_symlink {
return reply.error(Errno::from_i32(libc::EINVAL));
}
if let Err(e) = OpenOptions::new()
.write(true)
.open(&upper)
.and_then(|f| f.set_len(new_size))
{
return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO)));
}
}
if let Some(m) = mode {
if !is_symlink {
if let Err(e) = fs::set_permissions(&upper, fs::Permissions::from_mode(m)) {
return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO)));
}
}
}
if uid.is_some() || gid.is_some() {
let Ok(path_c) = CString::new(upper.as_os_str().as_bytes()) else {
return reply.error(Errno::from_i32(libc::EINVAL));
};
let u = uid.map(|v| v as libc::uid_t).unwrap_or(u32::MAX);
let g = gid.map(|v| v as libc::gid_t).unwrap_or(u32::MAX);
if unsafe { libc::lchown(path_c.as_ptr(), u, g) } != 0 {
return reply.error(Errno::from_i32(
std::io::Error::last_os_error()
.raw_os_error()
.unwrap_or(libc::EIO),
));
}
}
if atime.is_some() || mtime.is_some() {
let Ok(path_c) = CString::new(upper.as_os_str().as_bytes()) else {
return reply.error(Errno::from_i32(libc::EINVAL));
};
let to_ts = |t: Option<TimeOrNow>| -> libc::timespec {
match t {
Some(TimeOrNow::SpecificTime(st)) => {
let d = st
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap_or_default();
libc::timespec {
tv_sec: d.as_secs() as _, // FIX libc::time_m 32bit
tv_nsec: d.subsec_nanos() as libc::c_long,
}
}
_ => libc::timespec {
tv_sec: 0,
tv_nsec: libc::UTIME_NOW,
},
}
};
let times = [to_ts(atime), to_ts(mtime)];
if unsafe {
libc::utimensat(
libc::AT_FDCWD,
path_c.as_ptr(),
times.as_ptr(),
libc::AT_SYMLINK_NOFOLLOW,
)
} != 0
{
return reply.error(Errno::from_i32(
std::io::Error::last_os_error()
.raw_os_error()
.unwrap_or(libc::EIO),
));
}
}
self.getattr(_req, ino, None, reply);
}
/// Reads the target of a symbolic link.
///
/// # Arguments
/// * `_req` - The FUSE request context.
/// * `ino` - Inode number of the symlink.
/// * `reply` - Callback to return the link destination as a byte array.
///
/// # Returns
/// * `reply.data` with the path string.
/// * `reply.error` if the inode does not point to a symlink.
fn readlink(&self, _req: &Request, ino: INodeNo, reply: ReplyData) {
let Some(path) = self.inodes.get_path(ino) else {
return reply.error(Errno::from_i32(libc::ENOENT));
};
let Some((full, _)) = self.layers.resolve(&path) else {
return reply.error(Errno::from_i32(libc::ENOENT));
};
match fs::read_link(&full) {
Ok(target) => reply.data(target.as_os_str().as_encoded_bytes()),
Err(e) => reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO))),
}
}
/// Creates a file node (regular file, device special file, or pipe).
///
/// This method ensures the node is created in the upper layer. If a whiteout exists
/// for this name (masking a lower file), it is removed to make the new node visible.
///
/// # Arguments
/// * `_req` - The FUSE request context containing caller's UID, GID, and PID.
/// * `parent` - The Inode number of the parent directory where the node will be created.
/// * `name` - The name of the new node to be created.
/// * `mode` - The file type and permissions for the new node.
/// * `umask` - The process umask to be applied to the mode.
/// * `rdev` - The device ID (major/minor) if creating a character or block device.
/// * `reply` - The callback to return the new entry's attributes or an error.
///
/// # Returns
/// * Calls `reply.entry` with the new file's metadata on success.
/// * Call `reply.error` with the corresponding `libc` error code on failure.
fn mknod(
&self,
_req: &Request,
parent: INodeNo,
name: &OsStr,
mode: u32,
umask: u32,
rdev: u32,
reply: ReplyEntry,
) {
let rel = self.inodes.child_path(parent, name);
let upper_path = self.layers.backend.upper.join(&rel);
if let Some(p) = upper_path.parent() {
if let Err(e) = fs::create_dir_all(p) {
return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO)));
}
}
let Ok(path_c) = CString::new(upper_path.as_os_str().as_bytes()) else {
return reply.error(Errno::from_i32(libc::EINVAL));
};
let ret = unsafe {
libc::mknod(
path_c.as_ptr(),
(mode & !umask) as libc::mode_t,
rdev as libc::dev_t,
)
};
if ret != 0 {
return reply.error(Errno::from_i32(
std::io::Error::last_os_error()
.raw_os_error()
.unwrap_or(libc::EIO),
));
}
self.layers.clear_whiteout(&rel);
match fs::symlink_metadata(&upper_path) {
Ok(meta) => {
let ino = self.inodes.get_ino(&rel);
reply.entry(&TTL, &self.make_attr(ino, &meta), Generation(0));
}
Err(e) => reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO))),
}
}
/// Creates a new directory in the upper layer.
///
/// # Arguments
/// * `_req` - The FUSE request context.
/// * `parent` - Inode number of the parent directory.
/// * `name` - The name of the new directory.
/// * `mode` - The permissions for the new directory.
/// * `umask` - The process umask to apply to the permissions.
/// * `reply` - The callback to return the new directory's attributes.
///
/// # Returns
/// * Calls `reply.entry` with the directory metadata if created successfully.
/// * Call `reply.error` with `EEXIST` if the directory already exists (POSIX).
/// * Call `reply.error` if directory creation fails or whiteout cannot be cleared.
fn mkdir(
&self,
_req: &Request,
parent: INodeNo,
name: &OsStr,
mode: u32,
umask: u32,
reply: ReplyEntry,
) {
let rel = self.inodes.child_path(parent, name);
let upper_path = self.layers.backend.upper.join(&rel);
// Ensure ancestor directories exist in the upper layer.
if let Some(p) = upper_path.parent() {
if let Err(e) = fs::create_dir_all(p) {
return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO)));
}
}
// Use create_dir (not create_dir_all) so that trying to mkdir an
// already-existing directory returns EEXIST, as POSIX requires.
if let Err(e) = fs::create_dir(&upper_path) {
return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO)));
}
let _ = fs::set_permissions(
&upper_path,
fs::Permissions::from_mode(mode & !umask & 0o7777),
);
self.layers.clear_whiteout(&rel);
// If a directory with the same name exists in the lower layer, mark the new
// upper directory as opaque (trusted.overlay.opaque = y). Without this, a
// rmdir + mkdir cycle would cause the old lower entries to reappear through
// the merge, because there is no individual whiteout for each child.
// Bwrap and proot rely on this behavior when they recreate dirs like /tmp.
let lower_dir = self.layers.backend.lower.join(&rel);
if fs::symlink_metadata(&lower_dir)
.map(|m| m.is_dir())
.unwrap_or(false)
{
if let Ok(path_c) = CString::new(upper_path.as_os_str().as_bytes()) {
for attr_name in [
b"trusted.overlay.opaque\0" as &[u8],
b"user.overlay.opaque\0",
] {
let name_ptr = attr_name.as_ptr() as *const libc::c_char;
unsafe {
libc::lsetxattr(
path_c.as_ptr(),
name_ptr,
b"y".as_ptr() as *const libc::c_void,
1,
0,
);
}
}
}
}
match fs::metadata(&upper_path) {
Ok(meta) => {
let ino = self.inodes.get_ino(&rel);
reply.entry(&TTL, &self.make_attr(ino, &meta), Generation(0));
}
Err(e) => reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO))),
}
}
/// Removes a file or a symbolic link.
///
/// If the file only exists in the lower layer, this function creates a whiteout
/// in the upper layer to hide it. If it exists in the upper layer, it is deleted.
///
/// # Arguments
/// * `_req` - The FUSE request context.
/// * `parent` - Inode number of the parent directory.
/// * `name` - Name of the file/link to be removed.
/// * `reply` - The callback to confirm completion or return an error.
///
/// # Returns
/// * Calls `reply.ok` if the file was successfully masked or deleted.
/// * Call `reply.error` with `EISDIR` if the path is a directory (POSIX).
/// * Call `reply.error` on I/O failures.
fn unlink(&self, _req: &Request, parent: INodeNo, name: &OsStr, reply: ReplyEmpty) {
let rel = self.inodes.child_path(parent, name);
let upper_path = self.layers.backend.upper.join(&rel);
// POSIX: unlink on a directory must return EISDIR.
// Check via resolve so we catch the case where only the lower has it.
if let Some((full, _)) = self.layers.resolve(&rel) {
if let Ok(meta) = fs::symlink_metadata(&full) {
if meta.is_dir() {
return reply.error(Errno::from_i32(libc::EISDIR));
}
}
}
if fs::symlink_metadata(&upper_path).is_ok() {
if let Err(e) = fs::remove_file(&upper_path) {
return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO)));
}
}
if fs::symlink_metadata(self.layers.backend.lower.join(&rel)).is_ok() {
if let Err(e) = self.layers.create_whiteout(&rel) {
return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO)));
}
}
self.inodes.remove_ino(&rel);
reply.ok()
}
/// Removes a directory.
///
/// A directory can only be removed if it is empty (no visible entries after whiteouts).
///
/// # Arguments
/// * `_req` - The FUSE request context.
/// * `parent` - Inode number of the parent directory.
/// * `name` - Name of the directory to be removed.
/// * `reply` - The callback to confirm completion or return an error.
///
/// # Returns
/// * Calls `reply.ok` on successful removal.
/// * Call `reply.error` with `ENOTEMPTY` if the directory is not empty.
fn rmdir(&self, _req: &Request, parent: INodeNo, name: &OsStr, reply: ReplyEmpty) {
let rel = self.inodes.child_path(parent, name);
let upper_path = self.layers.backend.upper.join(&rel);
let lower_path = self.layers.backend.lower.join(&rel);
// Collect names masked by whiteouts in upper.
let upper_whiteouts: HashSet<String> = fs::read_dir(&upper_path)
.map(|rd| {
rd.flatten()
.filter_map(|e| {
let n = e.file_name().to_string_lossy().to_string();
n.starts_with(WH_PREFIX)
.then(|| n.replacen(WH_PREFIX, "", 1))
})
.collect()
})
.unwrap_or_default();
let upper_has_real = fs::symlink_metadata(&upper_path).is_ok()
&& fs::read_dir(&upper_path)
.map(|rd| {
rd.flatten()
.any(|e| !e.file_name().to_string_lossy().starts_with(WH_PREFIX))
})
.unwrap_or(false);
let lower_has_visible = fs::symlink_metadata(&lower_path).is_ok()
&& fs::read_dir(&lower_path)
.map(|rd| {
rd.flatten().any(|e| {
!upper_whiteouts.contains(&e.file_name().to_string_lossy().to_string())
})
})
.unwrap_or(false);
if upper_has_real || lower_has_visible {
return reply.error(Errno::from_i32(libc::ENOTEMPTY));
}
if fs::symlink_metadata(&upper_path).is_ok() {
if let Err(e) = fs::remove_dir_all(&upper_path) {
return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO)));
}
}
if fs::symlink_metadata(&lower_path).is_ok() {
let _ = self.layers.create_whiteout(&rel);
}
self.inodes.remove_subtree(&rel);
reply.ok()
}
/// Creates a symbolic link in the upper layer.
///
/// # Arguments
/// * `_req` - The FUSE request context.
/// * `parent` - Inode number of the parent directory.
/// * `link_name` - The name of the symlink to be created.
/// * `target` - The path the symlink should point to.
/// * `reply` - The callback to return the new link's metadata.
///
/// # Returns
/// * Calls `reply.entry` with metadata on success.
/// * Calls `reply.error` on system call failures.
fn symlink(
&self,
_req: &Request,
parent: INodeNo,
link_name: &OsStr,
target: &Path,
reply: ReplyEntry,
) {
let rel = self.inodes.child_path(parent, link_name);
let upper_path = self.layers.backend.upper.join(&rel);
if let Some(p) = upper_path.parent() {
if let Err(e) = fs::create_dir_all(p) {
return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO)));
}
}
match std::os::unix::fs::symlink(target, &upper_path) {
Ok(_) => {
self.layers.clear_whiteout(&rel);
match fs::symlink_metadata(&upper_path) {
Ok(meta) => {
let ino = self.inodes.get_ino(&rel);
reply.entry(&TTL, &self.make_attr(ino, &meta), Generation(0));
}
Err(e) => reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO))),
}
}
Err(e) => reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO))),
}
}
/// Renames or moves an entry.
///
/// If the entry is in the lower layer, it performs a Copy-on-Write of the entire
/// entry (including subdirectories) before moving it within the upper layer.
///
/// # Arguments
/// * `_req` - The FUSE request context.
/// * `parent` - Inode of the current parent directory.
/// * `name` - Current name of the entry.
/// * `newparent` - Inode of the target parent directory.
/// * `newname` - New name of the entry.
/// * `flags` - Rename flags (e.g., NOREPLACE, EXCHANGE).
/// * `reply` - The callback to confirm status.
///
/// # Returns
/// * Calls `reply.ok` if the move was successful.
/// * Call `reply.error` if the source doesn't exist or target is invalid.
fn rename(
&self,
_req: &Request,
parent: INodeNo,
name: &OsStr,
newparent: INodeNo,
newname: &OsStr,
flags: RenameFlags,
reply: ReplyEmpty,
) {
let old_rel = self.inodes.child_path(parent, name);
let new_rel = self.inodes.child_path(newparent, newname);
let old_lower = self.layers.backend.lower.join(&old_rel);
let old_upper = self.layers.backend.upper.join(&old_rel);
let new_upper = self.layers.backend.upper.join(&new_rel);
if flags.contains(RenameFlags::RENAME_NOREPLACE) {
if self.layers.resolve(&new_rel).is_some() {
return reply.error(Errno::from_i32(libc::EEXIST));
}
}
if flags.contains(RenameFlags::RENAME_EXCHANGE) {
return reply.error(Errno::from_i32(libc::ENOTSUP));
}
if fs::symlink_metadata(&old_upper).is_err() {
if old_lower.is_dir() {
if let Err(e) = Self::copy_dir_all(&old_lower, &old_upper) {
return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO)));
}
} else if let Err(e) = self.layers.copy_on_write(&old_rel) {
return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO)));
}
}
if let Some(p) = new_upper.parent() {
if let Err(e) = fs::create_dir_all(p) {
return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO)));
}
}
if let Err(e) = fs::rename(&old_upper, &new_upper) {
return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO)));
}
if fs::symlink_metadata(&old_lower).is_ok() {
let _ = self.layers.create_whiteout(&old_rel);
}
self.layers.clear_whiteout(&new_rel);
self.inodes.remove_subtree(&new_rel);
self.inodes.remove_subtree(&old_rel);
reply.ok()
}
/// Creates a hard link to an existing file.
///
/// Triggers a Copy-on-Write for the source file if it is currently in the lower layer.
///
/// # Arguments
/// * `_req` - The FUSE request context.
/// * `ino` - The Inode number of the existing source file.
/// * `newparent` - The Inode of the target parent directory.
/// * `newname` - The name of the new hard link.
/// * `reply` - The callback to return the new entry attributes.
///
/// # Returns
/// * Calls `reply.entry` with attributes on success.
/// * Call `reply.error` on link creation failures.
fn link(
&self,
_req: &Request,
ino: INodeNo,
newparent: INodeNo,
newname: &OsStr,
reply: ReplyEntry,
) {
let Some(src_rel) = self.inodes.get_path(ino) else {
return reply.error(Errno::from_i32(libc::ENOENT));
};
let src_upper = match self.layers.copy_on_write(&src_rel) {
Ok(p) => p,
Err(e) => return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO))),
};
let dst_rel = self.inodes.child_path(newparent, newname);
let dst_upper = self.layers.backend.upper.join(&dst_rel);
if let Some(p) = dst_upper.parent() {
if let Err(e) = fs::create_dir_all(p) {
return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO)));
}
}
if let Err(e) = fs::hard_link(&src_upper, &dst_upper) {
return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO)));
}
self.layers.clear_whiteout(&dst_rel);
match fs::symlink_metadata(&dst_upper) {
Ok(meta) => {
let new_ino = self.inodes.get_ino(&dst_rel);
reply.entry(&TTL, &self.make_attr(new_ino, &meta), Generation(0));
}
Err(e) => reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO))),
}
}
/// Opens a file for reading or writing.
///
/// File handles (fh) are not tracked (stateless), but `O_TRUNC` is honored
/// explicitly: if the caller opens for writing with truncation, we perform a
/// Copy-on-Write to the upper layer and then truncate the file to zero.
/// Without this, a shell redirect (`>file`) would leave the old content
/// beyond the newly written range, silently corrupting the file.
///
/// # Arguments
/// * `_req` - The FUSE request context.
/// * `ino` - The Inode number of the file.
/// * `flags` - Access flags from the caller (O_RDONLY / O_WRONLY / O_RDWR / O_TRUNC …).
/// * `reply` - The callback to return the file handle.
///
/// # Returns
/// * Calls `reply.opened` with a default file handle (0).
fn open(&self, _req: &Request, ino: INodeNo, flags: OpenFlags, reply: ReplyOpen) {
let raw: i32 = flags.0;
let access = raw & libc::O_ACCMODE;
let wants_write = access == libc::O_WRONLY || access == libc::O_RDWR;
let wants_trunc = raw & libc::O_TRUNC != 0;
if wants_write {
let Some(path) = self.inodes.get_path(ino) else {
return reply.error(Errno::from_i32(libc::ENOENT));
};
match self.layers.copy_on_write(&path) {
Ok(upper) => {
if wants_trunc {
if let Err(e) = OpenOptions::new().write(true).truncate(true).open(&upper) {
return reply
.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO)));
}
}
}
Err(e) => {
return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO)));
}
}
}
reply.opened(FileHandle(0), FopenFlags::empty());
}
/// Reads data from a file at a specific offset.
///
/// # Arguments
/// * `_req` - The FUSE request context.
/// * `ino` - The Inode number of the file.
/// * `_fh` - The file handle.
/// * `offset` - The position to start reading from.
/// * `size` - The number of bytes to read.
/// * `_flags` - Opening flags.
/// * `_lock` - Optional lock owner information.
/// * `reply` - The callback containing the read data.
///
/// # Returns
/// * Calls `reply.data` with the buffer content.
/// * Call `reply.error` on read or seek errors.
fn read(
&self,
_req: &Request,
ino: INodeNo,
_fh: FileHandle,
offset: u64,
size: u32,
_flags: OpenFlags,
_lock: Option<LockOwner>,
reply: ReplyData,
) {
let Some(path) = self.inodes.get_path(ino) else {
return reply.error(Errno::from_i32(libc::ENOENT));
};
let Some((full, _)) = self.layers.resolve(&path) else {
return reply.error(Errno::from_i32(libc::ENOENT));
};
let file = match fs::File::open(&full) {
Ok(f) => f,
Err(e) => return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO))),
};
let file_len = match file.metadata() {
Ok(m) => m.len() as usize,
Err(e) => return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO))),
};
let offset_usize = offset as usize;
if file_len == 0 || offset_usize >= file_len {
return reply.data(&[]);
}
const MMAP_THRESHOLD: usize = 128 * 1024;
if file_len < MMAP_THRESHOLD {
let mut file = file;
let mut buf = vec![0u8; size as usize];
if let Err(e) = file.seek(SeekFrom::Start(offset)) {
return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO)));
}
let n = file.read(&mut buf).unwrap_or(0);
return reply.data(buf[..n].as_bytes());
}
match unsafe { memmap2::MmapOptions::new().map(&file) } {
Ok(mmap) => {
let end = (offset_usize + size as usize).min(file_len);
let slice: &[u8] = &mmap[offset_usize..end];
reply.data(slice.as_bytes());
}
Err(_) => {
let mut file = file;
let mut buf = vec![0u8; size as usize];
if let Err(e) = file.seek(SeekFrom::Start(offset)) {
return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO)));
}
let n = file.read(&mut buf).unwrap_or(0);
reply.data(buf[..n].as_bytes());
}
}
}
/// Writes data to a file.
///
/// Forces a Copy-on-Write to the upper layer if the file is not already there.
///
/// # Arguments
/// * `_req` - The FUSE request context.
/// * `ino` - The Inode number of the file.
/// * `_fh` - The file handle.
/// * `offset` - The position to start writing at.
/// * `data` - The byte buffer containing data to write.
/// * `_write_flags` - Specific write behavior flags.
/// * `_flags` - Opening flags.
/// * `_lock` - Optional lock owner information.
/// * `reply` - The callback to return the number of bytes written.
///
/// # Returns
/// * Calls `reply.written` with the byte count on success.
/// * Call `reply.error` on writing or CoW failures.
///
/// # Note on `truncate(false)`
/// The `OpenOptions` **must** include `.truncate(false)`. Without it,
/// opening an already-existing upper-layer file with `O_WRONLY | O_CREAT`
/// would silently truncate the file to zero before writing, corrupting any
/// content outside the range `[offset, offset + data.len()]` — a classic
/// TOCTOU-style data-loss bug.
fn write(
&self,
_req: &Request,
ino: INodeNo,
_fh: FileHandle,
offset: u64,
data: &[u8],
_write_flags: WriteFlags,
_flags: OpenFlags,
_lock: Option<LockOwner>,
reply: ReplyWrite,
) {
let Some(path) = self.inodes.get_path(ino) else {
return reply.error(Errno::from_i32(libc::ENOENT));
};
let upper = match self.layers.copy_on_write(&path) {
Ok(p) => p,
Err(e) => return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO))),
};
match OpenOptions::new().write(true).truncate(false).open(&upper) {
Ok(mut file) => {
if let Err(e) = file.seek(SeekFrom::Start(offset)) {
return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO)));
}
match file.write(data) {
Ok(n) => reply.written(n as u32),
Err(e) => reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO))),
}
}
Err(e) => reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO))),
}
}
/// Handles the closing of a file descriptor.
///
/// Called on every `close(2)` of a file descriptor. Unlike `fsync`, which the
/// application calls explicitly, `flush` is a best-effort opportunity to report
/// any pending writing errors before the close completes.
///
/// For files that were written (i.e., exist in the upper layer) we open them
/// with write access so that `sync_all` uses a valid writable fd — calling
/// `fdatasync` on an `O_RDONLY` fd returns `EBADF` on Linux ≥ 5.15.
/// Files that are only in the lower layer (read-only) need no flushing.
///
/// # Arguments
/// * `_req` - The FUSE request context.
/// * `ino` - The Inode number of the file being flushed.
/// * `_fh` - The file handle associated with the open file.
/// * `_lock_owner` - The ID of the lock owner, if any locks are held.
/// * `reply` - The callback to confirm the flush or return an error.
///
/// # Returns
/// * Calls `reply.ok` if the file was successfully synced or is not in the upper layer.
/// * Calls `reply.error(EIO)` if `sync_all` fails on an upper-layer file.
fn flush(
&self,
_req: &Request,
ino: INodeNo,
_fh: FileHandle,
_lock_owner: LockOwner,
reply: ReplyEmpty,
) {
let Some(path) = self.inodes.get_path(ino) else {
return reply.ok();
};
let upper = self.layers.backend.upper.join(&path);
// Only flush if the file actually landed in the upper layer.
let Ok(meta) = fs::symlink_metadata(&upper) else {
return reply.ok();
};
// Symlinks and directories cannot be meaningfully synced this way.
if !meta.is_file() {
return reply.ok();
}
// Must open with write access; sync on a read-only fd is EBADF.
match OpenOptions::new().write(true).open(&upper) {
Ok(f) => {
if f.sync_all().is_ok() {
reply.ok()
} else {
reply.error(Errno::from_i32(libc::EIO))
}
}
Err(_) => reply.ok(),
}
}
/// Synchronizes dirty file data in memory with storage.
///
/// Only upper-layer files are synced; lower-layer files are read-only and
/// need no flushing from our side.
///
/// The file must be opened with write access: `fdatasync(2)` and `fsync(2)`
/// return `EBADF` on a read-only file descriptor on Linux ≥ 5.15.
///
/// # Arguments
/// * `_req` - The FUSE request context.
/// * `ino` - The Inode number of the file.
/// * `_fh` - The file handles.
/// * `datasync` - If `true`, only synchronizes data (not metadata).
/// * `reply` - The callback to confirm sync completion.
///
/// # Returns
/// * Calls `reply.ok` if the sync succeeded or the file is not in the upper layer.
/// * Call `reply.error` with the errno from `fdatasync`/`fsync` on failure.
fn fsync(
&self,
_req: &Request,
ino: INodeNo,
_fh: FileHandle,
datasync: bool,
reply: ReplyEmpty,
) {
let Some(path) = self.inodes.get_path(ino) else {
return reply.error(Errno::from_i32(libc::ENOENT));
};
let upper = self.layers.backend.upper.join(&path);
// File not in upper → nothing to sync on our side.
let Ok(meta) = fs::symlink_metadata(&upper) else {
return reply.ok();
};
if !meta.is_file() {
return reply.ok();
}
// Must open with write access; fdatasync/fsync on O_RDONLY is EBADF.
match OpenOptions::new().write(true).open(&upper) {
Ok(f) => {
use std::os::unix::io::AsRawFd;
let ret = if datasync {
unsafe { libc::fdatasync(f.as_raw_fd()) }
} else {
unsafe { libc::fsync(f.as_raw_fd()) }
};
if ret == 0 {
reply.ok()
} else {
reply.error(Errno::from_i32(
std::io::Error::last_os_error()
.raw_os_error()
.unwrap_or(libc::EIO),
))
}
}
Err(e) => reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO))),
}
}
/// Opens a directory.
///
/// Since this implementation is stateless regarding handles, it returns a
/// default file handle. Actual entry reading is handled by `readdir`.
///
/// # Arguments
/// * `_req` - The FUSE request context.
/// * `_ino` - The Inode number of the directory to be opened.
/// * `_flags` - The flags for opening the directory (e.g., O_RDONLY).
/// * `reply` - The callback to return the directory handle.
///
/// # Returns
/// * Calls `reply.opened` with a default directory handle (0).
fn opendir(&self, _req: &Request, _ino: INodeNo, _flags: OpenFlags, reply: ReplyOpen) {
reply.opened(FileHandle(0), FopenFlags::empty());
}
/// Lists directory entries, merging upper and lower layers and handling whiteouts.
///
/// This is a critical operation for OverlayFS: it must show files from both
/// layers, but hide files in the lower layer that have a corresponding whiteout
/// in the upper layer.
///
/// # Arguments
/// * `_req` - The FUSE request context.
/// * `ino` - The Inode number of the directory to list.
/// * `_fh` - The directory handle (unused in this stateless implementation).
/// * `offset` - The index in the stream to resume listing from.
/// * `reply` - The callback to add entries to the directory stream.
///
/// # Returns
/// * Calls `reply.add` for each valid entry (calculating new offsets).
/// * Call `reply.ok` once all entries (or the buffer limit) are processed.
fn readdir(
&self,
_req: &Request,
ino: INodeNo,
_fh: FileHandle,
offset: u64,
mut reply: ReplyDirectory,
) {
let rel_path = self.inodes.get_path(ino).unwrap_or_default();
let parent_ino = if rel_path.as_os_str().is_empty() {
INodeNo(1)
} else {
let parent_path = rel_path.parent().unwrap_or(Path::new(""));
if parent_path.as_os_str().is_empty() {
INodeNo(1)
} else {
self.inodes.get_ino(parent_path)
}
};
let mut entries: Vec<(INodeNo, FileType, String)> = vec![
(ino, FileType::Directory, ".".to_string()),
(parent_ino, FileType::Directory, "..".to_string()),
];
let mut seen: HashSet<String> = HashSet::new();
let mut whiteouts: HashSet<String> = HashSet::new();
let upper_dir = self.layers.backend.upper.join(&rel_path);
let lower_dir = self.layers.backend.lower.join(&rel_path);
if let Ok(list) = fs::read_dir(&upper_dir) {
let entries_raw: Vec<_> = list.flatten().collect();
for entry in &entries_raw {
let name = entry.file_name().to_string_lossy().to_string();
if name.starts_with(WH_PREFIX) {
whiteouts.insert(name.replacen(WH_PREFIX, "", 1));
}
}
for entry in entries_raw {
let name = entry.file_name().to_string_lossy().to_string();
if name.starts_with(WH_PREFIX) || whiteouts.contains(&name) {
continue;
}
seen.insert(name.clone());
entries.push((
self.inodes.peek_ino(&rel_path.join(&name)),
Self::entry_file_type(&entry),
name,
));
}
}
let upper_is_opaque = Self::is_opaque_dir(&upper_dir);
if !upper_is_opaque {
if let Ok(list) = fs::read_dir(&lower_dir) {
for entry in list.flatten() {
let name = entry.file_name().to_string_lossy().to_string();
if !seen.contains(&name) && !whiteouts.contains(&name) {
entries.push((
self.inodes.peek_ino(&rel_path.join(&name)),
Self::entry_file_type(&entry),
name,
));
}
}
}
}
for (i, (ino, ft, name)) in entries.into_iter().enumerate().skip(offset as usize) {
if reply.add(ino, (i + 1) as u64, ft, name) {
break;
}
}
reply.ok()
}
/// Lists directory entries with pre-fetched attributes, merging upper and lower layers.
///
/// This is the optimized variant of `readdir`: each entry is returned together with
/// its full `FileAttr`, allowing the kernel to skip individual `lookup` calls for
/// every item. The merge logic (whiteout handling, opaque directories) is identical
/// to `readdir`.
///
/// # Arguments
/// * `_req` - The FUSE request context (UID, GID, PID of the caller).
/// * `ino` - The Inode number of the directory to list.
/// * `_fh` - The directory handle (unused — this implementation is stateless).
/// * `offset` - Index in the entry stream to resume from (0 = start from beginning).
/// * `reply` - The callback used to push entries; returns `true` when the buffer is full.
///
/// # Returns
/// * Calls `reply.add` for each valid entry until the buffer is full or the list is exhausted.
/// * Call `reply.ok` once all entries have been processed.
fn readdirplus(
&self,
_req: &Request,
ino: INodeNo,
_fh: FileHandle,
offset: u64,
mut reply: ReplyDirectoryPlus,
) {
let rel_path = self.inodes.get_path(ino).unwrap_or_default();
let parent_ino = if rel_path.as_os_str().is_empty() {
INodeNo(1)
} else {
let parent_path = rel_path.parent().unwrap_or(Path::new(""));
if parent_path.as_os_str().is_empty() {
INodeNo(1)
} else {
self.inodes.get_ino(parent_path)
}
};
struct PlusEntry {
ino: INodeNo,
name: String,
rel: std::path::PathBuf,
is_upper: bool,
}
let mut entries: Vec<PlusEntry> = Vec::new();
entries.push(PlusEntry {
ino,
name: ".".into(),
rel: rel_path.clone(),
is_upper: true,
});
let parent_rel = rel_path.parent().unwrap_or(Path::new("")).to_path_buf();
entries.push(PlusEntry {
ino: parent_ino,
name: "..".into(),
rel: parent_rel,
is_upper: true,
});
let upper_dir = self.layers.backend.upper.join(&rel_path);
let lower_dir = self.layers.backend.lower.join(&rel_path);
let mut seen: HashSet<String> = HashSet::new();
let mut whiteouts: HashSet<String> = HashSet::new();
if let Ok(list) = fs::read_dir(&upper_dir) {
let entries_raw: Vec<_> = list.flatten().collect();
for entry in &entries_raw {
let name = entry.file_name().to_string_lossy().to_string();
if name.starts_with(WH_PREFIX) {
whiteouts.insert(name.replacen(WH_PREFIX, "", 1));
}
}
for entry in entries_raw {
let name = entry.file_name().to_string_lossy().to_string();
if name.starts_with(WH_PREFIX) || whiteouts.contains(&name) {
continue;
}
seen.insert(name.clone());
entries.push(PlusEntry {
ino: self.inodes.peek_ino(&rel_path.join(&name)),
name,
rel: rel_path.join(entry.file_name()),
is_upper: true,
});
}
}
let upper_is_opaque = Self::is_opaque_dir(&upper_dir);
if !upper_is_opaque {
if let Ok(list) = fs::read_dir(&lower_dir) {
for entry in list.flatten() {
let name = entry.file_name().to_string_lossy().to_string();
if !seen.contains(&name) && !whiteouts.contains(&name) {
entries.push(PlusEntry {
ino: self.inodes.peek_ino(&rel_path.join(&name)),
name,
rel: rel_path.join(entry.file_name()),
is_upper: false,
});
}
}
}
}
for (i, e) in entries.into_iter().enumerate().skip(offset as usize) {
let phys = if e.is_upper {
self.layers.backend.upper.join(&e.rel)
} else {
self.layers.backend.lower.join(&e.rel)
};
let Ok(meta) = fs::symlink_metadata(&phys) else {
continue;
};
let attr = self.make_attr(e.ino, &meta);
if reply.add(e.ino, (i + 1) as u64, &e.name, &TTL, &attr, Generation(0)) {
break;
}
}
reply.ok();
}
/// Returns filesystem statistics by querying both layers.
///
/// Total blocks/inodes are taken from the lower layer (read-only content),
/// while free/available space comes from the upper layer (where writes land).
/// Block-size fields (`bsize`, `frsize`, `namelen`) are also taken from the
/// upper layer so that arithmetic such as `bavail * bsize` is self-consistent
/// for the device that actually accepts new data.
///
/// # Arguments
/// * `_req` - The FUSE request context.
/// * `_ino` - The Inode number (unused).
/// * `reply` - The callback returning block size, free blocks, etc.
fn statfs(&self, _req: &Request, _ino: INodeNo, reply: ReplyStatfs) {
let lower_c = CString::new(self.layers.backend.lower.as_os_str().as_bytes());
let upper_c = CString::new(self.layers.backend.upper.as_os_str().as_bytes());
let (Ok(lower_c), Ok(upper_c)) = (lower_c, upper_c) else {
return reply.statfs(0, 0, 0, 0, 0, 512, 255, 0);
};
let mut lower_stat: libc::statvfs = unsafe { std::mem::zeroed() };
let mut upper_stat: libc::statvfs = unsafe { std::mem::zeroed() };
if unsafe { libc::statvfs(lower_c.as_ptr(), &mut lower_stat) } != 0
|| unsafe { libc::statvfs(upper_c.as_ptr(), &mut upper_stat) } != 0
{
return reply.statfs(0, 0, 0, 0, 0, 512, 255, 0);
}
reply.statfs(
lower_stat.f_blocks,
upper_stat.f_bfree,
upper_stat.f_bavail,
lower_stat.f_files,
upper_stat.f_ffree,
upper_stat.f_bsize as u32,
upper_stat.f_namemax as u32,
upper_stat.f_frsize as u32,
);
}
/// Sets an extended attribute (xattr) on a file.
///
/// Promotes the target to the upper layer before applying the attribute.
///
/// # Arguments
/// * `_req` - The FUSE request context.
/// * `ino` - The Inode number of the file.
/// * `name` - The name of the extended attribute.
/// * `value` - The byte buffer containing the attribute data.
/// * `_flags` - Setxattr flags (e.g., CREATE, REPLACE).
/// * `_position` - Attribute offset (macOS only).
/// * `reply` - The callback to confirm completion.
///
/// # Returns
/// * Calls `reply.ok` on success.
/// * Call `reply.error` on CoW or libc errors.
fn setxattr(
&self,
_req: &Request,
ino: INodeNo,
name: &OsStr,
value: &[u8],
_flags: i32,
_position: u32,
reply: ReplyEmpty,
) {
let Some(path) = self.inodes.get_path(ino) else {
return reply.error(Errno::from_i32(libc::ENOENT));
};
let upper = match self.layers.copy_on_write(&path) {
Ok(p) => p,
Err(e) => return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO))),
};
let (Ok(path_c), Ok(name_c)) = (
CString::new(upper.as_os_str().as_bytes()),
CString::new(name.as_bytes()),
) else {
return reply.error(Errno::from_i32(libc::EINVAL));
};
let ret = unsafe {
libc::lsetxattr(
path_c.as_ptr(),
name_c.as_ptr(),
value.as_ptr() as *const libc::c_void,
value.len(),
0,
)
};
if ret != 0 {
reply.error(Errno::from_i32(
std::io::Error::last_os_error()
.raw_os_error()
.unwrap_or(libc::EIO),
));
} else {
reply.ok();
}
}
/// Gets an extended attribute value.
///
/// # Arguments
/// * `_req` - The FUSE request context.
/// * `ino` - The Inode number of the file.
/// * `name` - The name of the attribute to fetch.
/// * `size` - Size of the destination buffer.
/// * `reply` - The callback returning the data or required size.
///
/// # Returns
/// * Calls `reply.data` or `reply.size` on success.
fn getxattr(&self, _req: &Request, ino: INodeNo, name: &OsStr, size: u32, reply: ReplyXattr) {
let Some(path) = self.inodes.get_path(ino) else {
return reply.error(Errno::from_i32(libc::ENOENT));
};
let Some((full, _)) = self.layers.resolve(&path) else {
return reply.error(Errno::from_i32(libc::ENOENT));
};
let (Ok(path_c), Ok(name_c)) = (
CString::new(full.as_os_str().as_bytes()),
CString::new(name.as_bytes()),
) else {
return reply.error(Errno::from_i32(libc::EINVAL));
};
if size == 0 {
let len = unsafe {
libc::lgetxattr(path_c.as_ptr(), name_c.as_ptr(), std::ptr::null_mut(), 0)
};
if len < 0 {
reply.error(Errno::from_i32(
std::io::Error::last_os_error()
.raw_os_error()
.unwrap_or(libc::EIO),
));
} else {
reply.size(len as u32);
}
} else {
let mut buf = vec![0u8; size as usize];
let len = unsafe {
libc::lgetxattr(
path_c.as_ptr(),
name_c.as_ptr(),
buf.as_mut_ptr() as *mut libc::c_void,
size as libc::size_t,
)
};
if len < 0 {
reply.error(Errno::from_i32(
std::io::Error::last_os_error()
.raw_os_error()
.unwrap_or(libc::EIO),
));
} else {
reply.data(&buf[..len as usize]);
}
}
}
/// Lists all extended attribute names for a file or directory.
///
/// Merges xattr names from both the upper and lower layers, deduplicating
/// names that appear in both. Formats the result as a null-separated byte
/// sequence as expected by the FUSE kernel driver. When `size == 0`,
/// returns only the required buffer length without writing any data.
///
/// # Arguments
/// * `_req` - The FUSE request context (UID, GID, PID of the caller).
/// * `ino` - The Inode number of the target file or directory.
/// * `size` - The size of the buffer provided by the caller. If 0, the function
/// must return the required buffer size to hold all names.
/// * `reply` - The callback used to send the attribute list or the required size.
///
/// # Returns
/// * Calls `reply.size` if the input `size` was 0, providing the total byte count.
/// * Calls `reply.data` with the null-separated list of attribute names.
/// * Call `reply.error` with `ERANGE` if the provided buffer is too small,
/// or `ENOENT` if the path cannot be resolved.
fn listxattr(&self, _req: &Request, ino: INodeNo, size: u32, reply: ReplyXattr) {
let Some(path) = self.inodes.get_path(ino) else {
return reply.error(Errno::from_i32(libc::ENOENT));
};
/// Internal helper: Retrieves a raw null-separated list of xattr names from
/// the physical filesystem using the `llistxattr` system call.
///
/// # Arguments
/// * `p` - The physical path to the file in a specific layer.
fn raw_list(p: &Path) -> Vec<u8> {
let Ok(path_c) = CString::new(p.as_os_str().as_bytes()) else {
return Vec::new();
};
let len = unsafe { libc::llistxattr(path_c.as_ptr(), std::ptr::null_mut(), 0) };
if len <= 0 {
return Vec::new();
}
let mut buf = vec![0u8; len as usize];
let n = unsafe {
libc::llistxattr(
path_c.as_ptr(),
buf.as_mut_ptr() as *mut libc::c_char,
len as libc::size_t,
)
};
if n >= 0 {
buf.truncate(n as usize);
buf
} else {
Vec::new()
}
}
/// Internal helper: Parses the raw byte buffer into a vector of strings.
///
/// # Arguments
/// * `raw` - The null-separated byte slice from the kernel.
fn parse_names(raw: &[u8]) -> Vec<String> {
raw.split(|&b| b == 0)
.filter(|s| !s.is_empty())
.map(|s| String::from_utf8_lossy(s).into_owned())
.collect()
}
let Some((full, is_upper)) = self.layers.resolve(&path) else {
return reply.error(Errno::from_i32(libc::ENOENT));
};
let mut names: Vec<String> = parse_names(&raw_list(&full));
if !is_upper {
let upper_path = self.layers.backend.upper.join(&path);
if fs::symlink_metadata(&upper_path).is_ok() {
for n in parse_names(&raw_list(&upper_path)) {
if !names.contains(&n) {
names.push(n);
}
}
}
}
let mut out: Vec<u8> = Vec::new();
for n in &names {
out.extend_from_slice(n.as_bytes());
out.push(0);
}
if size == 0 {
reply.size(out.len() as u32);
} else if (size as usize) < out.len() {
reply.error(Errno::from_i32(libc::ERANGE));
} else {
reply.data(&out);
}
}
/// Removes an extended attribute from a file or directory.
///
/// To maintain the integrity of the lower layer, this method triggers a
/// Copy-on-Write (CoW). The attribute is then removed from the newly
/// created file in the upper layer.
///
/// # Arguments
/// * `_req` - The FUSE request context.
/// * `ino` - The Inode number of the target.
/// * `name` - The name of the extended attribute to be removed.
/// * `reply` - The callback to confirm the successful removal.
///
/// # Returns
/// * Calls `reply.ok` if the attribute was successfully removed from the upper layer.
/// * Calls `reply.error` with `ENOENT` if the file does not exist, or
/// the corresponding `libc` error if the removal fails.
fn removexattr(&self, _req: &Request, ino: INodeNo, name: &OsStr, reply: ReplyEmpty) {
let Some(path) = self.inodes.get_path(ino) else {
return reply.error(Errno::from_i32(libc::ENOENT));
};
let upper = match self.layers.copy_on_write(&path) {
Ok(p) => p,
Err(e) => return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO))),
};
let (Ok(path_c), Ok(name_c)) = (
CString::new(upper.as_os_str().as_bytes()),
CString::new(name.as_bytes()),
) else {
return reply.error(Errno::from_i32(libc::EINVAL));
};
let ret = unsafe { libc::lremovexattr(path_c.as_ptr(), name_c.as_ptr()) };
if ret != 0 {
reply.error(Errno::from_i32(
std::io::Error::last_os_error()
.raw_os_error()
.unwrap_or(libc::EIO),
));
} else {
reply.ok();
}
}
/// Checks if a file exists and if the caller has the requested permissions.
///
/// # Implementation note
/// `DefaultPermissions` is enabled at mount time, which means the FUSE kernel
/// driver already performs standard DAC checks **before** dispatching `access`
/// to us. Our role here is therefore limited to:
///
/// 1. Confirming the path actually exists (not hidden by a whiteout).
/// 2. Handling `F_OK` (existence-only checks).
/// 3. Applying the correct POSIX semantics for `uid 0` (root bypasses all
/// permission bits except the executed bit on regular files when `X_OK`
/// is requested — and even then only if *no* executed bit is set at all).
///
/// For non-root callers, the kernel has already validated `R_OK`/`W_OK`/`X_OK`
/// via `DefaultPermissions`, so we return `ok()` for them unconditionally
/// (the kernel would not have forwarded the call if access were denied).
///
/// # Arguments
/// * `req` - The FUSE request context (UID, GID, PID of the caller).
/// * `ino` - The Inode number of the file to check.
/// * `mask` - The bitmask of permissions to check (R_OK, W_OK, X_OK, F_OK=0).
/// * `reply` - The callback to return the result.
///
/// # Returns
/// * Calls `reply.ok` if access is permitted (or if the caller is not root and
/// `DefaultPermissions` handles the check at the kernel level).
/// * Call `reply.error(ENOENT)` if the inode is hidden or unresolvable.
/// * Calls `reply.error(EACCES)` if root requests execute access on a file with
/// no executed bit set for any principal (owner, group, or other).
fn access(&self, req: &Request, ino: INodeNo, mask: AccessFlags, reply: ReplyEmpty) {
let Some(path) = self.inodes.get_path(ino) else {
return reply.error(Errno::from_i32(libc::ENOENT));
};
if self.layers.is_hidden(&path) {
return reply.error(Errno::from_i32(libc::ENOENT));
}
let Some((full, _)) = self.layers.resolve(&path) else {
return reply.error(Errno::from_i32(libc::ENOENT));
};
if mask.bits() == 0 {
return reply.ok();
}
if req.uid() != 0 {
return reply.ok();
}
if mask.bits() & libc::X_OK != 0 {
let meta = match fs::symlink_metadata(&full) {
Ok(m) => m,
Err(_) => return reply.error(Errno::from_i32(libc::ENOENT)),
};
if !meta.file_type().is_dir() && (meta.mode() & 0o111) == 0 {
return reply.error(Errno::from_i32(libc::EACCES));
}
}
reply.ok()
}
/// Creates and opens a new regular file atomically.
///
/// The file is always created in the upper layer, ensuring visibility over
/// any potential lower-layer content.
///
/// # Arguments
/// * `_req` - The FUSE request context.
/// * `parent` - The Inode number of the parent directory.
/// * `name` - The name of the new file.
/// * `mode` - The file permissions.
/// * `umask` - The process umask.
/// * `flags` - Opening flags for the new file.
/// * `reply` - The callback returning the file metadata and handle.
///
/// # Returns
/// * Calls `reply.created` on success.
/// * Call `reply.error` on creation failures.
fn create(
&self,
_req: &Request,
parent: INodeNo,
name: &OsStr,
mode: u32,
umask: u32,
_flags: i32,
reply: ReplyCreate,
) {
let rel = self.inodes.child_path(parent, name);
let upper_path = self.layers.backend.upper.join(&rel);
if let Some(p) = upper_path.parent() {
if let Err(e) = fs::create_dir_all(p) {
return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO)));
}
}
match fs::File::create(&upper_path) {
Ok(_) => {
let _ = fs::set_permissions(
&upper_path,
fs::Permissions::from_mode(mode & !umask & 0o7777),
);
self.layers.clear_whiteout(&rel);
match fs::metadata(&upper_path) {
Ok(meta) => {
let ino = self.inodes.get_ino(&rel);
reply.created(
&TTL,
&self.make_attr(ino, &meta),
Generation(0),
FileHandle(0),
FopenFlags::empty(),
);
}
Err(e) => reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO))),
}
}
Err(e) => reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO))),
}
}
/// Manipulates the allocated disk space for a file.
///
/// This method allows pre-allocating or deallocating space for a file (e.g., hole punching).
/// Since it modifies the file's allocation, a Copy-on-Write (CoW) operation is
/// triggered to ensure the change happens in the upper layer.
///
/// # Arguments
/// * `_req` - The FUSE request context (containing caller's UID, GID, and PID).
/// * `ino` - The Inode number of the file to modify.
/// * `_fh` - The file handle (unused in this stateless implementation).
/// * `offset` - The starting byte offset for the allocation change.
/// * `length` - The number of bytes to allocate or deallocate.
/// * `mode` - The specific operation to perform (e.g., `FALLOC_FL_PUNCH_HOLE`).
/// * `reply` - The callback to confirm the operation's success or return an error.
///
/// # Returns
/// * Calls `reply.ok` if the allocation was successful in the upper layer.
/// * Call `reply.error` with the corresponding `libc` error code on failure.
fn fallocate(
&self,
_req: &Request,
ino: INodeNo,
_fh: FileHandle,
offset: u64,
length: u64,
mode: i32,
reply: ReplyEmpty,
) {
let Some(path) = self.inodes.get_path(ino) else {
return reply.error(Errno::from_i32(libc::ENOENT));
};
let upper = match self.layers.copy_on_write(&path) {
Ok(p) => p,
Err(e) => return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO))),
};
let fd = match Self::open_wronly_fd(&upper) {
Ok(fd) => fd,
Err(e) => return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO))),
};
let ret =
unsafe { libc::fallocate(fd, mode, offset as libc::off_t, length as libc::off_t) };
unsafe { libc::close(fd) };
if ret != 0 {
reply.error(Errno::from_i32(
std::io::Error::last_os_error()
.raw_os_error()
.unwrap_or(libc::EIO),
));
} else {
reply.ok();
}
}
/// Computes a new file offset without performing any I/O.
///
/// Opens the resolved file read-only to get a real file descriptor, then delegates
/// to `libc::lseek` so that the host kernel handles sparse-file
/// semantics ('SEEK_DATA' / 'SEEK_HOLE') correctly. The fd is closed immediately after.
///
/// # Arguments
/// * `_req` - The FUSE request context.
/// * `ino` - The Inode number of the target file.
/// * `_fh` - The file handle (unused — this implementation is stateless).
/// * `offset` - The byte offset to seek from (interpretation depends on `whence`).
/// * `whence` - Seek mode: `SEEK_SET`, `SEEK_CUR`, `SEEK_END`, `SEEK_DATA`, or `SEEK_HOLE`.
/// * `reply` - The callback returning the resulting absolute offset.
///
/// # Returns
/// * Calls `reply.offset` with the new position on success.
/// * Call `reply.error` with the errno from `lseek(2)` on failure (e.g. `ENXIO` for
/// `SEEK_DATA` past end-of-file, `EINVAL` for an unsupported `whence`).
fn lseek(
&self,
_req: &Request,
ino: INodeNo,
_fh: FileHandle,
offset: i64,
whence: i32,
reply: ReplyLseek,
) {
let Some(path) = self.inodes.get_path(ino) else {
return reply.error(Errno::from_i32(libc::ENOENT));
};
let Some((full, _)) = self.layers.resolve(&path) else {
return reply.error(Errno::from_i32(libc::ENOENT));
};
match whence {
libc::SEEK_SET => {
if offset < 0 {
return reply.error(Errno::from_i32(libc::EINVAL));
}
return reply.offset(offset);
}
libc::SEEK_END => {
let size = match fs::symlink_metadata(&full) {
Ok(m) => m.len() as i64,
Err(e) => {
return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO)));
}
};
let result = size.checked_add(offset).unwrap_or(-1);
if result < 0 {
return reply.error(Errno::from_i32(libc::EINVAL));
}
return reply.offset(result);
}
libc::SEEK_CUR => {
return reply.error(Errno::from_i32(libc::EINVAL));
}
_ => {}
}
let Ok(path_c) = CString::new(full.as_os_str().as_bytes()) else {
return reply.error(Errno::from_i32(libc::EINVAL));
};
let fd = unsafe { libc::open(path_c.as_ptr(), libc::O_RDONLY) };
if fd < 0 {
return reply.error(Errno::from_i32(
std::io::Error::last_os_error()
.raw_os_error()
.unwrap_or(libc::EIO),
));
}
let result = unsafe { libc::lseek(fd, offset as libc::off_t, whence) };
unsafe { libc::close(fd) };
if result < 0 {
reply.error(Errno::from_i32(
std::io::Error::last_os_error()
.raw_os_error()
.unwrap_or(libc::EIO),
));
} else {
reply.offset(result);
}
}
/// Offloads data copying between two files to the kernel.
///
/// In an OverlayFS, if the destination file is in the lower layer, it must
/// be promoted to the upper layer via Copy-on-Write before the copy starts.
///
/// # Arguments
/// * `_req` - The FUSE request context.
/// * `ino_in` - The Inode number of the source file.
/// * `_fh_in` - The file handle of the source file.
/// * `offset_in` - The starting offset in the source file.
/// * `ino_out` - The Inode number of the destination file.
/// * `_fh_out` - The file handle of the destination file.
/// * `offset_out` - The starting offset in the destination file.
/// * `len` - The total number of bytes to copy.
/// * `_flags` - Copy flags (unused).
/// * `reply` - The callback returning the actual number of bytes copied.
///
/// # Returns
/// * Calls `reply.written` with the number of bytes copied on success.
/// * Call `reply.error` on I/O or CoW failures.
fn copy_file_range(
&self,
_req: &Request,
ino_in: INodeNo,
_fh_in: FileHandle,
offset_in: u64,
ino_out: INodeNo,
_fh_out: FileHandle,
offset_out: u64,
len: u64,
_flags: CopyFileRangeFlags,
reply: ReplyWrite,
) {
let (Some(path_in), Some(path_out)) =
(self.inodes.get_path(ino_in), self.inodes.get_path(ino_out))
else {
return reply.error(Errno::from_i32(libc::ENOENT));
};
let Some((src_full, _)) = self.layers.resolve(&path_in) else {
return reply.error(Errno::from_i32(libc::ENOENT));
};
let dst_upper = match self.layers.copy_on_write(&path_out) {
Ok(p) => p,
Err(e) => return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO))),
};
let fd_in = match Self::open_rdonly_fd(&src_full) {
Ok(fd) => fd,
Err(e) => return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO))),
};
let fd_out = match Self::open_wronly_fd(&dst_upper) {
Ok(fd) => fd,
Err(e) => {
unsafe { libc::close(fd_in) };
return reply.error(Errno::from_i32(e.raw_os_error().unwrap_or(libc::EIO)));
}
};
let mut off_in = offset_in as libc::off64_t;
let mut off_out = offset_out as libc::off64_t;
let copied = unsafe {
libc::copy_file_range(
fd_in,
&mut off_in,
fd_out,
&mut off_out,
len as libc::size_t,
0,
)
};
unsafe {
libc::close(fd_in);
libc::close(fd_out);
}
if copied < 0 {
reply.error(Errno::from_i32(
std::io::Error::last_os_error()
.raw_os_error()
.unwrap_or(libc::EIO),
));
} else {
reply.written(copied as u32);
}
}
}