mtp-mount 0.4.0

Mount MTP devices as local filesystems via FUSE
Documentation
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
use std::collections::HashMap;
use std::ffi::OsStr;
use std::io;
use std::io::{Seek, SeekFrom};
use std::path::PathBuf;
use std::pin::Pin;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use bytes::Bytes;
use fuser::{
    BsdFileFlags, Errno, FileAttr, FileHandle, FileType, Filesystem, FopenFlags, Generation,
    INodeNo, KernelConfig, LockOwner, MountOption, OpenFlags, RenameFlags, ReplyAttr, ReplyCreate,
    ReplyData, ReplyDirectory, ReplyEmpty, ReplyEntry, ReplyOpen, ReplyStatfs, ReplyWrite, Request,
    TimeOrNow, WriteFlags,
};
use log::{debug, error, info, warn};
use mtp_rs::mtp::{DeviceEvent, MtpDevice};
use mtp_rs::{NewObjectInfo, ObjectHandle, Storage};

use crate::buffer::WriteBuffer;
use crate::device::{is_link_lost, DeviceOpener, UnplugSwitch};
use crate::inode::{ChildInfo, InodeEntry, InodeKind, InodeTable, FUSE_ROOT_INODE};
use crate::reconnect::ReconnectPolicy;
use crate::shutdown::Shutdown;
use crate::sparse_cache::SparseCache;

const TTL: Duration = Duration::from_secs(1);

/// How much of a spool file an upload holds in memory at a time.
const UPLOAD_CHUNK: usize = 65536;

/// How many times an operation is retried across reconnects before it gives up.
/// One reconnect is the cable glitch we're here for; a second is a device that
/// keeps dropping mid-operation, and past that the retry is not the answer.
const MAX_ATTEMPTS: u32 = 3;

/// How many times an operation re-resolves its handles and tries again after
/// the device says a handle is stale.
///
/// One, which is what `mtp-rs` prescribes: the first `StaleHandle` means the
/// device re-keyed the object and a fresh listing has the new token, so the
/// retry works. A second one for the same operation means the re-resolved token
/// died too, which isn't a re-key any more; looping on it would hammer the
/// device instead of telling the caller.
///
/// This budget is deliberately separate from [`MAX_ATTEMPTS`]: the two failures
/// have nothing in common. A stale handle costs one listing against a healthy
/// session, a dead link costs a reopen and a backoff wait, so spending one
/// shouldn't shorten the other, and a stale handle must never fall through into
/// the reconnect path.
const MAX_STALE_RETRIES: u32 = 1;

type MtpResult<T> = Result<T, mtp_rs::Error>;

/// Everything the mount needs beyond the device itself.
pub struct MtpFsConfig {
    pub read_only: bool,
    /// Disk-backed directory for write buffers and read caches (see [`crate::spool`]).
    /// Must exist and be writable; resolve it with [`crate::spool::spool_dir_from_env`]
    /// and [`crate::spool::prepare_spool_dir`].
    pub spool_dir: PathBuf,
    /// How long to wait for a device that went away.
    pub reconnect: ReconnectPolicy,
    /// The pretend cable, shared with the [`DeviceOpener`] (tests only).
    pub unplug: UnplugSwitch,
}

fn mtp_datetime_to_system_time(dt: &mtp_rs::DateTime) -> SystemTime {
    fn days_from_civil(y: i64, m: i64, d: i64) -> i64 {
        let y = if m <= 2 { y - 1 } else { y };
        let era = if y >= 0 { y } else { y - 399 } / 400;
        let yoe = (y - era * 400) as u64;
        let m_adj = if m > 2 { m - 3 } else { m + 9 } as u64;
        let doy = (153 * m_adj + 2) / 5 + d as u64 - 1;
        let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
        era * 146097 + doe as i64 - 719468
    }

    let days = days_from_civil(dt.year as i64, dt.month as i64, dt.day as i64);
    let secs = days * 86400 + dt.hour as i64 * 3600 + dt.minute as i64 * 60 + dt.second as i64;
    if secs >= 0 {
        UNIX_EPOCH + Duration::from_secs(secs as u64)
    } else {
        UNIX_EPOCH
    }
}

fn inode_to_file_attr(entry: &InodeEntry) -> FileAttr {
    let uid = unsafe { libc::getuid() };
    let gid = unsafe { libc::getgid() };
    FileAttr {
        ino: INodeNo(entry.inode),
        size: entry.size,
        blocks: entry.size.div_ceil(512),
        atime: entry.atime,
        mtime: entry.mtime,
        ctime: entry.mtime,
        crtime: entry.mtime,
        kind: if entry.is_dir() {
            FileType::Directory
        } else {
            FileType::RegularFile
        },
        perm: if entry.is_dir() { 0o755 } else { 0o644 },
        nlink: if entry.is_dir() { 2 } else { 1 },
        uid,
        gid,
        rdev: 0,
        blksize: 4096,
        flags: 0,
    }
}

/// The name a storage shows up under in the mount.
fn storage_name(storage: &Storage) -> String {
    if storage.info().description.is_empty() {
        format!("Storage_{}", storage.id().0)
    } else {
        storage.info().description.clone()
    }
}

/// The temporary name a safe flush uploads under before renaming into place.
fn temp_upload_name(name: &str) -> String {
    format!(".~tmp~{name}")
}

/// Wraps a local I/O failure as an MTP error so spool problems flow through the
/// same result type as device problems.
fn io_error(e: io::Error) -> mtp_rs::Error {
    mtp_rs::Error::Io {
        message: e.to_string(),
    }
}

/// Helper to create an `Unpin` stream from a `Vec<u8>`.
fn bytes_stream(
    data: Vec<u8>,
) -> futures::stream::Iter<std::vec::IntoIter<Result<Bytes, io::Error>>> {
    let chunks = if data.is_empty() {
        vec![Ok(Bytes::new())]
    } else {
        vec![Ok(Bytes::from(data))]
    };
    futures::stream::iter(chunks)
}

/// Streams a spool file in [`UPLOAD_CHUNK`]-sized pieces, reading each one only
/// when the consumer asks for it. That's what keeps an upload's memory flat: a
/// 4 GB file costs one chunk of RAM, not 4 GB.
///
/// The read is a blocking `std::fs` read, which is fine because the only caller
/// polls this from `Handle::block_on` on a FUSE callback thread that has nothing
/// else to do. Don't spawn this stream onto the runtime: it would park a worker.
fn file_stream(
    file: std::fs::File,
) -> Pin<Box<dyn futures::Stream<Item = Result<Bytes, io::Error>> + Send>> {
    use std::io::Read as _;
    // The `Option` is the terminator. Handing the file back only after a
    // successful read means EOF and errors both end the stream; a version that
    // kept the file after an error would re-emit that same error forever.
    Box::pin(futures::stream::unfold(Some(file), |state| async move {
        let mut file = state?;
        let mut buf = vec![0u8; UPLOAD_CHUNK];
        match file.read(&mut buf) {
            Ok(0) => None,
            Ok(n) => {
                buf.truncate(n);
                Some((Ok(Bytes::from(buf)), Some(file)))
            }
            Err(e) => Some((Err(e), None)),
        }
    }))
}

/// Mutable state protected by `RefCell` so fuser's `&self` callbacks can mutate it.
struct Inner {
    storages: Vec<Storage>,
    /// Disk-backed directory for write buffers and read caches (see [`crate::spool`]).
    spool_dir: PathBuf,
    inodes: InodeTable,
    write_buf: WriteBuffer,
    read_cache: HashMap<u64, SparseCache>,
    dirs_loaded: HashMap<u64, bool>,
    fh_to_inode: HashMap<u64, u64>,
}

/// FUSE filesystem backed by an MTP device.
pub struct MtpFs {
    rt: tokio::runtime::Handle,
    device: Mutex<MtpDevice>,
    /// How to reopen the same device after it goes away.
    opener: Arc<dyn DeviceOpener>,
    policy: ReconnectPolicy,
    unplug: UnplugSwitch,
    /// Raised when the device is gone for good, so whoever owns the mount takes
    /// it down instead of leaving something that answers every call with EIO.
    shutdown: Arc<Shutdown>,
    /// Bumped on every reconnect so the event loop from the previous session
    /// notices it's been superseded and exits.
    event_epoch: Arc<AtomicU64>,
    inner: Arc<Mutex<Inner>>,
    next_fh: AtomicU64,
    read_only: bool,
    /// Counter incremented on every MTP partial-read fetch. Used by integration
    /// tests to verify that the sparse cache prevents redundant fetches.
    fetch_counter: Arc<AtomicU64>,
}

impl MtpFs {
    /// Builds a filesystem over an already-open device.
    ///
    /// `opener` must resolve to that same device; it's what the mount uses to
    /// come back after a disconnect.
    pub fn new(
        device: MtpDevice,
        opener: Arc<dyn DeviceOpener>,
        rt: tokio::runtime::Handle,
        config: MtpFsConfig,
    ) -> Self {
        let MtpFsConfig {
            read_only,
            spool_dir,
            reconnect,
            unplug,
        } = config;
        Self {
            rt,
            device: Mutex::new(device),
            opener,
            policy: reconnect,
            unplug,
            shutdown: Arc::new(Shutdown::default()),
            event_epoch: Arc::new(AtomicU64::new(0)),
            inner: Arc::new(Mutex::new(Inner {
                storages: Vec::new(),
                spool_dir: spool_dir.clone(),
                inodes: InodeTable::new(),
                write_buf: WriteBuffer::new(spool_dir),
                read_cache: HashMap::new(),
                dirs_loaded: HashMap::new(),
                fh_to_inode: HashMap::new(),
            })),
            next_fh: AtomicU64::new(1),
            read_only,
            fetch_counter: Arc::new(AtomicU64::new(0)),
        }
    }

    /// The signal that asks for this mount to be taken down.
    ///
    /// Whoever mounted the filesystem owns the unmount handle, so it has to
    /// watch this and unmount when a reason shows up. See [`crate::shutdown`].
    pub fn shutdown(&self) -> Arc<Shutdown> {
        Arc::clone(&self.shutdown)
    }

    /// Returns a shared handle to the MTP fetch counter.
    ///
    /// The counter increments each time a partial-read operation is issued to
    /// the device. Primarily used by integration tests to verify cache behavior.
    #[allow(dead_code)] // used by integration tests via lib.rs, not by the bin
    pub fn fetch_counter(&self) -> Arc<AtomicU64> {
        Arc::clone(&self.fetch_counter)
    }

    fn alloc_fh(&self) -> u64 {
        self.next_fh.fetch_add(1, Ordering::Relaxed)
    }

    /// Find the storage index that owns a given inode by walking up the tree.
    fn find_storage_index(inner: &Inner, inode: u64) -> Option<usize> {
        let mut current = inode;
        loop {
            let entry = inner.inodes.get(current)?;
            if let InodeKind::Storage { storage_id } = &entry.kind {
                return inner
                    .storages
                    .iter()
                    .position(|s: &Storage| s.id() == *storage_id);
            }
            if current == entry.parent {
                return None;
            }
            current = entry.parent;
        }
    }

    /// Runs an MTP operation, riding out the two ways its handles can die under
    /// it: the device went away, or the device re-keyed the object.
    ///
    /// `attempt` is re-run from scratch after either recovery, so it must
    /// resolve its own handles (through [`Self::file_handle`] and friends) rather
    /// than close over handles from the previous try.
    ///
    /// The two paths are siblings, not variations. A dead link needs a reopen
    /// and a wait; a stale handle needs neither, because the session is fine and
    /// only the token is dead, so it re-resolves by path and retries straight
    /// away. Reopening a healthy device would be a real regression: on Android
    /// a reopen is expensive and can wedge the device.
    fn with_recovery<T>(
        &self,
        inner: &mut Inner,
        mut attempt: impl FnMut(&Self, &mut Inner) -> MtpResult<T>,
    ) -> MtpResult<T> {
        for _ in 0..MAX_ATTEMPTS {
            // Stale-handle retries happen against the current session, on their
            // own budget, and never fall through to the reconnect below.
            let mut stale_retries = MAX_STALE_RETRIES;
            loop {
                if self.unplug.is_unplugged() {
                    break;
                }
                match attempt(self, inner) {
                    Ok(value) => return Ok(value),
                    Err(e) if e.is_stale_handle() => {
                        if stale_retries == 0 {
                            error!("Operation still hit a stale object handle after re-resolving");
                            return Err(e);
                        }
                        stale_retries -= 1;
                        debug!("Operation hit a stale object handle, re-resolving by path");
                        Self::invalidate_handles(inner);
                    }
                    Err(e) if !is_link_lost(&e) => return Err(e),
                    Err(e) => {
                        debug!("Operation hit a dead session: {e}");
                        break;
                    }
                }
            }
            self.reconnect(inner)?;
        }
        Err(mtp_rs::Error::Disconnected)
    }

    /// Marks every cached object handle stale so the next use re-resolves it by
    /// path, and drops the cached listings that produced them.
    ///
    /// Whole-table rather than just the inode that failed, for two reasons. A
    /// device that re-keys re-keys in batches (Android's MediaProvider does it
    /// across a whole media rescan), so the neighbours are suspect too. And the
    /// generation counter makes marking free: re-resolution is lazy, so the only
    /// inodes that pay for a listing are the ones something actually touches
    /// again. This is the same mechanism a reconnect uses, minus the reopen.
    fn invalidate_handles(inner: &mut Inner) {
        inner.inodes.bump_generation();
        inner.dirs_loaded.clear();
        inner.dirs_loaded.insert(FUSE_ROOT_INODE, true);
    }

    /// Waits for the device to come back and rebuilds the session on top of the
    /// existing inode tree. Gives up (and unmounts) when the window runs out.
    fn reconnect(&self, inner: &mut Inner) -> MtpResult<()> {
        let who = self.opener.describe();

        if self.policy.is_disabled() {
            self.give_up(&format!(
                "{who} disconnected and reconnect is off (--reconnect-timeout 0)"
            ));
            return Err(mtp_rs::Error::Disconnected);
        }

        let secs = self.policy.timeout().as_secs();
        info!("{who} disconnected, waiting up to {secs}s for it to come back...");
        eprintln!("mtp-mount: {who} disconnected, waiting up to {secs}s for it to come back...");

        for delay in self.policy.schedule() {
            std::thread::sleep(delay);
            if self.unplug.is_unplugged() {
                continue;
            }
            let device = match self.opener.open(&self.rt) {
                Ok(device) => device,
                Err(e) => {
                    debug!("Reconnect attempt failed: {e}");
                    continue;
                }
            };
            match self.adopt(inner, device) {
                Ok(()) => {
                    eprintln!("mtp-mount: {who} is back, carrying on.");
                    return Ok(());
                }
                Err(e) => {
                    warn!("Reopened {who} but couldn't resume the mount: {e}");
                    continue;
                }
            }
        }

        self.give_up(&format!("{who} didn't come back within {secs}s"));
        Err(mtp_rs::Error::Disconnected)
    }

    /// Takes over a freshly opened device: re-maps storage IDs, marks every
    /// cached object handle stale, and starts a new event loop.
    ///
    /// Inode numbers, names, the tree shape, open file handles, read caches, and
    /// write spools all survive untouched. Only the session-scoped tokens change.
    fn adopt(&self, inner: &mut Inner, device: MtpDevice) -> MtpResult<()> {
        let storages = self.rt.block_on(device.storages())?;

        // Storage IDs are session-scoped too. Match the new storages to the
        // storage inodes by name, falling back to position when a device
        // reports no description (the old name embeds the old ID).
        let storage_inodes = inner.inodes.children(FUSE_ROOT_INODE);
        for (position, storage_ino) in storage_inodes.iter().enumerate() {
            let name = match inner.inodes.get(*storage_ino) {
                Some(entry) => entry.name.clone(),
                None => continue,
            };
            let matched = storages
                .iter()
                .find(|s| storage_name(s) == name)
                .or_else(|| storages.get(position));
            match matched {
                Some(storage) => inner.inodes.set_storage_id(*storage_ino, storage.id()),
                None => warn!("Storage '{name}' is missing after the reconnect"),
            }
        }

        inner.storages = storages;
        *self.device.lock().unwrap() = device.clone();
        inner.inodes.bump_generation();

        // Names and sizes are re-read on the next access; the handles behind
        // them are re-resolved lazily by path.
        inner.dirs_loaded.clear();
        inner.dirs_loaded.insert(FUSE_ROOT_INODE, true);

        let epoch = self.event_epoch.fetch_add(1, Ordering::SeqCst) + 1;
        self.spawn_event_loop(device, epoch);
        Ok(())
    }

    /// Says why the mount is going away and asks for it to be taken down. The
    /// operation that triggered this still returns an error to its caller.
    fn give_up(&self, reason: &str) {
        error!("{reason}; unmounting");
        eprintln!("mtp-mount: {reason}. Unmounting.");
        self.shutdown.request(reason);
    }

    /// Re-resolves an inode's MTP handle by path if it came from an older
    /// session, walking down from the storage root and refreshing each ancestor
    /// on the way. Inode numbers never change here.
    fn ensure_fresh(&self, inner: &mut Inner, inode: u64) -> MtpResult<()> {
        if inner.inodes.is_fresh(inode) {
            return Ok(());
        }

        // Collect the chain from the storage root down to this inode.
        let mut chain = Vec::new();
        let mut current = inode;
        loop {
            let entry = inner.inodes.get(current).ok_or(mtp_rs::Error::NotFound)?;
            match entry.kind {
                InodeKind::Root | InodeKind::Storage { .. } => break,
                _ => {
                    chain.push(current);
                    if current == entry.parent {
                        return Err(mtp_rs::Error::NotFound);
                    }
                    current = entry.parent;
                }
            }
        }
        chain.reverse();

        let storage_idx = Self::find_storage_index(inner, inode).ok_or(mtp_rs::Error::NotFound)?;

        let mut parent_handle: Option<ObjectHandle> = None;
        for node in chain {
            let entry = inner.inodes.get(node).ok_or(mtp_rs::Error::NotFound)?;
            let name = entry.name.clone();
            if inner.inodes.is_fresh(node) {
                parent_handle = match inner.inodes.get(node).map(|e| &e.kind) {
                    Some(InodeKind::Directory { handle } | InodeKind::File { handle }) => {
                        Some(*handle)
                    }
                    _ => return Err(mtp_rs::Error::NotFound),
                };
                continue;
            }

            let objects = self
                .rt
                .block_on(inner.storages[storage_idx].list_objects(parent_handle))?;
            let found = objects
                .into_iter()
                .find(|obj| obj.filename == name)
                .ok_or(mtp_rs::Error::NotFound)?;
            inner.inodes.set_handle(node, found.handle);
            parent_handle = Some(found.handle);
        }

        Ok(())
    }

    /// The current handle of a file inode.
    fn file_handle(&self, inner: &mut Inner, inode: u64) -> MtpResult<ObjectHandle> {
        self.ensure_fresh(inner, inode)?;
        match inner.inodes.get(inode).map(|e| &e.kind) {
            Some(InodeKind::File { handle }) => Ok(*handle),
            _ => Err(mtp_rs::Error::NotFound),
        }
    }

    /// The current handle of a file or directory inode.
    fn object_handle(&self, inner: &mut Inner, inode: u64) -> MtpResult<ObjectHandle> {
        self.ensure_fresh(inner, inode)?;
        match inner.inodes.get(inode).map(|e| &e.kind) {
            Some(InodeKind::File { handle } | InodeKind::Directory { handle }) => Ok(*handle),
            _ => Err(mtp_rs::Error::NotFound),
        }
    }

    /// The MTP parent handle to use for operations inside a directory inode
    /// (`None` means the storage root).
    fn parent_handle(&self, inner: &mut Inner, inode: u64) -> MtpResult<Option<ObjectHandle>> {
        self.ensure_fresh(inner, inode)?;
        match inner.inodes.get(inode).map(|e| &e.kind) {
            Some(InodeKind::Storage { .. }) => Ok(None),
            Some(InodeKind::Directory { handle }) => Ok(Some(*handle)),
            _ => Err(mtp_rs::Error::NotFound),
        }
    }

    /// Load children of a directory from MTP into the inode table.
    fn load_dir(&self, inner: &mut Inner, parent_inode: u64) {
        if inner.dirs_loaded.get(&parent_inode) == Some(&true) {
            return;
        }

        if parent_inode == FUSE_ROOT_INODE {
            inner.dirs_loaded.insert(parent_inode, true);
            return;
        }

        match self.with_recovery(inner, |fs, inner| fs.list_into_table(inner, parent_inode)) {
            Ok(()) => {
                inner.dirs_loaded.insert(parent_inode, true);
            }
            Err(e) => error!("Failed to list MTP objects: {e}"),
        }
    }

    /// One listing pass: ask the device, then reconcile the inode table.
    fn list_into_table(&self, inner: &mut Inner, parent_inode: u64) -> MtpResult<()> {
        let mtp_parent = self.parent_handle(inner, parent_inode)?;
        let storage_idx =
            Self::find_storage_index(inner, parent_inode).ok_or(mtp_rs::Error::NotFound)?;

        let objects = self
            .rt
            .block_on(inner.storages[storage_idx].list_objects(mtp_parent))?;

        let children: Vec<ChildInfo> = objects
            .into_iter()
            .map(|obj| ChildInfo {
                handle: obj.handle,
                is_dir: obj.is_folder(),
                size: obj.size,
                mtime: obj
                    .modified
                    .as_ref()
                    .map(mtp_datetime_to_system_time)
                    .unwrap_or(UNIX_EPOCH),
                name: obj.filename,
            })
            .collect();

        inner.inodes.sync_children(parent_inode, &children);
        Ok(())
    }

    /// Flush a dirty write buffer to MTP.
    ///
    /// When the device supports rename, uses a safe upload-then-delete-then-rename
    /// sequence to avoid data loss if the upload fails. Falls back to
    /// delete-then-upload on devices without rename support.
    ///
    /// The spooled bytes live in an unlinked temp file that a disconnect can't
    /// touch, so an upload interrupted by a cable glitch is retried from the
    /// start once the device is back. Only the upload is retried: once it lands,
    /// the data is on the device and a second attempt would just duplicate it.
    fn flush_to_mtp(&self, inner: &mut Inner, fh: u64) -> MtpResult<()> {
        let buf = match inner.write_buf.close(fh) {
            Some(b) => b,
            None => return Ok(()),
        };

        if !buf.is_dirty() {
            return Ok(());
        }

        let inode = buf.inode;
        let mut file = buf.into_file();
        let file_len = file.seek(SeekFrom::End(0)).map_err(io_error)?;

        let entry = match inner.inodes.get(inode) {
            Some(e) => e.clone(),
            None => {
                error!("Flush: inode {inode} not found");
                return Err(mtp_rs::Error::NotFound);
            }
        };

        let mut attempts = 0u32;
        self.with_recovery(inner, |fs, inner| {
            let mut attempt = file.try_clone().map_err(io_error)?;
            attempt.seek(SeekFrom::Start(0)).map_err(io_error)?;
            attempts += 1;
            fs.flush_once(inner, inode, &entry, file_len, attempt, attempts > 1)
        })
    }

    /// One flush attempt against the current session.
    ///
    /// `is_retry` means an earlier attempt died mid-upload, so a half-written
    /// temp object may be sitting in the target directory; it's cleared out
    /// before uploading again.
    #[allow(clippy::too_many_arguments)]
    fn flush_once(
        &self,
        inner: &mut Inner,
        inode: u64,
        entry: &InodeEntry,
        size: u64,
        file: std::fs::File,
        is_retry: bool,
    ) -> MtpResult<()> {
        let handle = self.file_handle(inner, inode)?;
        let storage_idx = Self::find_storage_index(inner, inode).ok_or(mtp_rs::Error::NotFound)?;
        let parent_handle = match self.parent_handle(inner, entry.parent) {
            Ok(handle) => handle,
            Err(e) => {
                error!("Flush: no parent directory for inode {inode}: {e}");
                return Err(e);
            }
        };

        let supports_rename = self.device.lock().unwrap().supports_rename();

        if is_retry && supports_rename {
            self.purge_leftover(
                inner,
                storage_idx,
                parent_handle,
                &temp_upload_name(&entry.name),
            );
        }

        if supports_rename {
            self.flush_safe(
                inner,
                inode,
                handle,
                storage_idx,
                parent_handle,
                entry,
                size,
                file,
            )
        } else {
            warn!(
                "Flush: device does not support rename, using delete-then-upload \
                 (data loss possible if upload fails)"
            );
            self.flush_unsafe(
                inner,
                inode,
                handle,
                storage_idx,
                parent_handle,
                entry,
                size,
                file,
            )
        }
    }

    /// Safe flush: upload with temp name, delete old, rename new.
    ///
    /// Only the upload returns an error to the caller: after it lands, the bytes
    /// are safe on the device and a retry would upload them twice, so the later
    /// steps report what happened and return `Ok`.
    #[allow(clippy::too_many_arguments)]
    fn flush_safe(
        &self,
        inner: &mut Inner,
        inode: u64,
        old_handle: ObjectHandle,
        storage_idx: usize,
        parent_handle: Option<ObjectHandle>,
        entry: &InodeEntry,
        size: u64,
        file: std::fs::File,
    ) -> MtpResult<()> {
        let storage = &inner.storages[storage_idx];
        let temp_name = temp_upload_name(&entry.name);

        // Step 1: Upload new data with a temp name.
        let info = NewObjectInfo::file(&temp_name, size);
        let stream = file_stream(file);
        let new_handle = match self
            .rt
            .block_on(storage.upload(parent_handle, info, stream))
        {
            Ok(h) => h,
            Err(e) => {
                error!("Flush: upload failed (original file untouched): {e}");
                return Err(e.into());
            }
        };

        // Step 2: Delete old object.
        if let Err(e) = self.rt.block_on(storage.delete(old_handle)) {
            error!("Flush: failed to delete old object (new data saved as '{temp_name}'): {e}");
            if let Some(e) = inner.inodes.get_mut(inode) {
                e.kind = InodeKind::File { handle: new_handle };
                e.name = temp_name;
                e.size = size;
                e.mtime = SystemTime::now();
            }
            return Ok(());
        }

        // Step 3: Rename temp to original name.
        if let Err(e) = self.rt.block_on(storage.rename(new_handle, &entry.name)) {
            warn!(
                "Flush: rename from '{temp_name}' to '{}' failed: {e}",
                entry.name
            );
            if let Some(e) = inner.inodes.get_mut(inode) {
                e.kind = InodeKind::File { handle: new_handle };
                e.name = temp_name;
                e.size = size;
                e.mtime = SystemTime::now();
            }
            return Ok(());
        }

        if let Some(e) = inner.inodes.get_mut(inode) {
            e.kind = InodeKind::File { handle: new_handle };
            e.size = size;
            e.mtime = SystemTime::now();
        }
        Ok(())
    }

    /// Unsafe flush: delete old object, then upload. Data is lost if upload fails.
    #[allow(clippy::too_many_arguments)]
    fn flush_unsafe(
        &self,
        inner: &mut Inner,
        inode: u64,
        old_handle: ObjectHandle,
        storage_idx: usize,
        parent_handle: Option<ObjectHandle>,
        entry: &InodeEntry,
        size: u64,
        file: std::fs::File,
    ) -> MtpResult<()> {
        let storage = &inner.storages[storage_idx];

        if let Err(e) = self.rt.block_on(storage.delete(old_handle)) {
            error!("Flush: failed to delete old object: {e}");
            return Err(e);
        }

        let info = NewObjectInfo::file(&entry.name, size);
        let stream = file_stream(file);

        match self
            .rt
            .block_on(storage.upload(parent_handle, info, stream))
        {
            Ok(new_handle) => {
                if let Some(e) = inner.inodes.get_mut(inode) {
                    e.kind = InodeKind::File { handle: new_handle };
                    e.size = size;
                    e.mtime = SystemTime::now();
                }
                Ok(())
            }
            Err(e) => {
                error!("Flush: upload failed after delete (data lost): {e}");
                Err(e.into())
            }
        }
    }

    /// Best-effort removal of a leftover object by name, used to clear a
    /// half-uploaded temp file before retrying a flush.
    fn purge_leftover(
        &self,
        inner: &mut Inner,
        storage_idx: usize,
        parent_handle: Option<ObjectHandle>,
        name: &str,
    ) {
        let storage = &inner.storages[storage_idx];
        let objects = match self.rt.block_on(storage.list_objects(parent_handle)) {
            Ok(objects) => objects,
            Err(e) => {
                debug!("Flush retry: couldn't list the target directory: {e}");
                return;
            }
        };
        for obj in objects.into_iter().filter(|o| o.filename == name) {
            if let Err(e) = self.rt.block_on(storage.delete(obj.handle)) {
                warn!("Flush retry: couldn't remove leftover '{name}': {e}");
            }
        }
    }

    pub fn mount_options(&self) -> Vec<MountOption> {
        let mut opts = vec![
            MountOption::FSName("mtp-mount".to_string()),
            MountOption::Subtype("mtp".to_string()),
            MountOption::DefaultPermissions,
            MountOption::NoDev,
            MountOption::NoSuid,
        ];
        if self.read_only {
            opts.push(MountOption::RO);
        } else {
            opts.push(MountOption::RW);
        }
        opts
    }

    /// Starts the event monitor for the current session.
    fn spawn_event_loop(&self, device: MtpDevice, epoch: u64) {
        let inner = Arc::clone(&self.inner);
        let current_epoch = Arc::clone(&self.event_epoch);
        self.rt.spawn(async move {
            Self::event_loop(device, inner, current_epoch, epoch).await;
        });
    }

    /// Background event loop that polls the device for MTP events and invalidates
    /// cached directory listings when objects change on the device side.
    ///
    /// Exits when its session is gone: either the device stopped answering, or a
    /// reconnect moved the mount to a newer session (`epoch`), which starts its
    /// own loop. A disconnect here doesn't tear the mount down; the next FUSE
    /// operation is what drives the reconnect.
    async fn event_loop(
        device: MtpDevice,
        inner: Arc<Mutex<Inner>>,
        current_epoch: Arc<AtomicU64>,
        epoch: u64,
    ) {
        loop {
            if current_epoch.load(Ordering::SeqCst) != epoch {
                debug!("Event loop: superseded by a reconnect");
                return;
            }
            match tokio::time::timeout(Duration::from_millis(200), device.next_event()).await {
                Ok(Ok(event)) => {
                    Self::handle_event(&inner, &event);
                }
                Ok(Err(mtp_rs::Error::Timeout)) => continue,
                Ok(Err(e)) if is_link_lost(&e) => {
                    debug!("Event loop: device disconnected");
                    break;
                }
                Ok(Err(e)) => {
                    warn!("Event loop error: {e}");
                    break;
                }
                Err(_) => continue, // tokio timeout elapsed, loop again
            }
        }
    }

    /// Process a single device event by invalidating the relevant cache entries.
    fn handle_event(inner: &Mutex<Inner>, event: &DeviceEvent) {
        match event {
            DeviceEvent::ObjectAdded { handle } => {
                debug!("Event: object added {:?}", handle);
                let mut inner = inner.lock().unwrap();
                // The new object might be in any directory. If we can find its parent
                // in the inode table (the parent dir was already cached), invalidate
                // just that directory. Otherwise, invalidate all directories.
                if let Some(parent_ino) = inner.inodes.find_parent_by_handle(*handle) {
                    inner.dirs_loaded.remove(&parent_ino);
                } else {
                    Self::invalidate_all_dirs(&mut inner);
                }
            }
            DeviceEvent::ObjectRemoved { handle } => {
                debug!("Event: object removed {:?}", handle);
                let mut inner = inner.lock().unwrap();
                if let Some(parent_ino) = inner.inodes.find_parent_by_handle(*handle) {
                    inner.dirs_loaded.remove(&parent_ino);
                } else {
                    Self::invalidate_all_dirs(&mut inner);
                }
            }
            DeviceEvent::ObjectInfoChanged { handle } => {
                debug!("Event: object info changed {:?}", handle);
                let mut inner = inner.lock().unwrap();
                // Invalidate the parent directory and clear any read cache for this file.
                if let Some(parent_ino) = inner.inodes.find_parent_by_handle(*handle) {
                    inner.dirs_loaded.remove(&parent_ino);
                }
                // Clear read cache entries for file handles pointing to this object.
                let fhs_to_clear: Vec<u64> = inner
                    .fh_to_inode
                    .iter()
                    .filter_map(|(&fh, &ino)| {
                        inner.inodes.get(ino).and_then(|e| match &e.kind {
                            InodeKind::File { handle: h } if *h == *handle => Some(fh),
                            _ => None,
                        })
                    })
                    .collect();
                for fh in fhs_to_clear {
                    inner.read_cache.remove(&fh);
                }
            }
            DeviceEvent::StoreAdded { .. }
            | DeviceEvent::StoreRemoved { .. }
            | DeviceEvent::StorageInfoChanged { .. } => {
                debug!("Event: storage change {:?}", event);
                // Storage-level changes: invalidate everything.
                let mut inner = inner.lock().unwrap();
                Self::invalidate_all_dirs(&mut inner);
            }
            _ => {
                debug!("Event: unhandled {:?}", event);
            }
        }
    }

    /// Mark all cached directories as stale so they're re-fetched on next access.
    fn invalidate_all_dirs(inner: &mut Inner) {
        inner.dirs_loaded.retain(|&k, _| k == FUSE_ROOT_INODE);
    }
}

impl Filesystem for MtpFs {
    fn init(&mut self, _req: &Request, _config: &mut KernelConfig) -> io::Result<()> {
        let storages = self
            .rt
            .block_on(self.device.lock().unwrap().storages())
            .map_err(|e: mtp_rs::Error| io::Error::other(e.to_string()))?;

        let mut inner = self.inner.lock().unwrap();
        for storage in &storages {
            inner
                .inodes
                .add_storage(storage.id(), storage_name(storage));
        }
        inner.dirs_loaded.insert(FUSE_ROOT_INODE, true);
        inner.storages = storages;
        drop(inner);

        // Spawn a background task that monitors device events and invalidates
        // cached directory listings when objects are added, removed, or changed.
        let event_device = self.device.lock().unwrap().clone();
        self.spawn_event_loop(event_device, self.event_epoch.load(Ordering::SeqCst));

        debug!(
            "MtpFs initialized with {} storages + event monitor",
            self.inner.lock().unwrap().storages.len()
        );
        Ok(())
    }

    fn lookup(&self, _req: &Request, parent: INodeNo, name: &OsStr, reply: ReplyEntry) {
        let parent_ino = parent.0;
        let name_str = match name.to_str() {
            Some(s) => s,
            None => {
                reply.error(Errno::ENOENT);
                return;
            }
        };

        let mut inner = self.inner.lock().unwrap();
        self.load_dir(&mut inner, parent_ino);

        match inner.inodes.lookup(parent_ino, name_str) {
            Some(ino) => {
                let entry = inner.inodes.get(ino).unwrap();
                let attr = inode_to_file_attr(entry);
                reply.entry(&TTL, &attr, Generation(0));
            }
            None => {
                reply.error(Errno::ENOENT);
            }
        }
    }

    fn getattr(&self, _req: &Request, ino: INodeNo, _fh: Option<FileHandle>, reply: ReplyAttr) {
        let inner = self.inner.lock().unwrap();
        match inner.inodes.get(ino.0) {
            Some(entry) => {
                let mut attr = inode_to_file_attr(entry);
                for (&fh, &inode) in &inner.fh_to_inode {
                    if inode == ino.0 {
                        if let Some(size) = inner.write_buf.size(fh) {
                            attr.size = size;
                            attr.blocks = size.div_ceil(512);
                        }
                        break;
                    }
                }
                reply.attr(&TTL, &attr);
            }
            None => {
                reply.error(Errno::ENOENT);
            }
        }
    }

    fn readdir(
        &self,
        _req: &Request,
        ino: INodeNo,
        _fh: FileHandle,
        offset: u64,
        mut reply: ReplyDirectory,
    ) {
        let ino_val = ino.0;

        let mut inner = self.inner.lock().unwrap();
        self.load_dir(&mut inner, ino_val);

        let parent_ino = inner
            .inodes
            .get(ino_val)
            .map(|e| e.parent)
            .unwrap_or(FUSE_ROOT_INODE);

        let mut entries: Vec<(u64, INodeNo, FileType, String)> = vec![
            (1, INodeNo(ino_val), FileType::Directory, ".".to_string()),
            (
                2,
                INodeNo(parent_ino),
                FileType::Directory,
                "..".to_string(),
            ),
        ];

        let children = inner.inodes.children(ino_val);
        for (i, child_ino) in children.iter().enumerate() {
            if let Some(child) = inner.inodes.get(*child_ino) {
                let kind = if child.is_dir() {
                    FileType::Directory
                } else {
                    FileType::RegularFile
                };
                entries.push((i as u64 + 3, INodeNo(*child_ino), kind, child.name.clone()));
            }
        }

        for (i, (off, ino, kind, name)) in entries.iter().enumerate() {
            if i as u64 >= offset && reply.add(*ino, *off, *kind, name) {
                break;
            }
        }
        reply.ok();
    }

    fn open(&self, _req: &Request, ino: INodeNo, _flags: OpenFlags, reply: ReplyOpen) {
        let mut inner = self.inner.lock().unwrap();
        match inner.inodes.get(ino.0) {
            Some(entry) if !entry.is_dir() => {
                let fh = self.alloc_fh();
                inner.fh_to_inode.insert(fh, ino.0);
                reply.opened(FileHandle(fh), FopenFlags::empty());
            }
            Some(_) => {
                reply.error(Errno::EISDIR);
            }
            None => {
                reply.error(Errno::ENOENT);
            }
        }
    }

    fn read(
        &self,
        _req: &Request,
        ino: INodeNo,
        fh: FileHandle,
        offset: u64,
        size: u32,
        _flags: OpenFlags,
        _lock_owner: Option<LockOwner>,
        reply: ReplyData,
    ) {
        let fh_val = fh.0;
        let mut inner = self.inner.lock().unwrap();

        // If there's a write buffer open for this fh, read from it.
        if inner.write_buf.is_open(fh_val) {
            match inner.write_buf.read(fh_val, offset as i64, size) {
                Ok(data) => reply.data(&data),
                Err(e) => {
                    error!("Read from write buffer failed: {e}");
                    reply.error(Errno::EIO);
                }
            }
            return;
        }

        // Resolve the MTP object and its storage.
        let entry = match inner.inodes.get(ino.0) {
            Some(e) => e.clone(),
            None => {
                reply.error(Errno::ENOENT);
                return;
            }
        };

        if entry.is_dir() {
            reply.error(Errno::EISDIR);
            return;
        }

        // Lazily create a sparse cache for this file handle.
        use std::collections::hash_map::Entry;
        let spool_dir = inner.spool_dir.clone();
        if let Entry::Vacant(slot) = inner.read_cache.entry(fh_val) {
            let cache = match SparseCache::new(entry.size, &spool_dir) {
                Ok(c) => c,
                Err(e) => {
                    error!("Failed to create sparse cache: {e}");
                    reply.error(Errno::EIO);
                    return;
                }
            };
            slot.insert(cache);
        }

        // Figure out which byte ranges still need to be fetched from MTP.
        let missing = {
            let cache = inner.read_cache.get(&fh_val).unwrap();
            cache.missing_ranges(offset, size as u64)
        };

        // Fetch missing ranges. `read_range` uses the 64-bit partial-read op to
        // support offsets beyond 4 GB. Each USB transfer is capped at 1 MB to keep
        // latency reasonable.
        // Each chunk resolves the object handle again inside `with_recovery`,
        // so a read that spans a cable glitch picks up the new session's handle
        // and carries on from the byte it stopped at.
        const CHUNK: u64 = 1024 * 1024;
        for range in missing {
            let mut cursor = range.start;
            while cursor < range.end {
                let chunk_size = (range.end - cursor).min(CHUNK) as u32;
                self.fetch_counter.fetch_add(1, Ordering::Relaxed);
                let bytes = match self.with_recovery(&mut inner, |fs, inner| {
                    let handle = fs.file_handle(inner, ino.0)?;
                    let storage_idx =
                        Self::find_storage_index(inner, ino.0).ok_or(mtp_rs::Error::NotFound)?;
                    fs.rt.block_on(
                        inner.storages[storage_idx].read_range(handle, cursor, chunk_size),
                    )
                }) {
                    Ok(b) => b,
                    Err(e) => {
                        error!("MTP read_range failed at offset {cursor}: {e}");
                        reply.error(Errno::EIO);
                        return;
                    }
                };
                let bytes_len = bytes.len() as u64;
                let cache = inner.read_cache.get_mut(&fh_val).unwrap();
                if let Err(e) = cache.write_at(cursor, &bytes) {
                    error!("Sparse cache write failed: {e}");
                    reply.error(Errno::EIO);
                    return;
                }
                // Short read from device — the object is smaller than reported;
                // stop fetching to avoid an infinite loop.
                if bytes_len == 0 {
                    break;
                }
                cursor += bytes_len;
            }
        }

        // Serve the requested slice from the cache.
        let cache = inner.read_cache.get_mut(&fh_val).unwrap();
        match cache.read_at(offset, size as u64) {
            Ok(buf) => reply.data(&buf),
            Err(e) => {
                error!("Sparse cache read failed: {e}");
                reply.error(Errno::EIO);
            }
        }
    }

    fn release(
        &self,
        _req: &Request,
        _ino: INodeNo,
        fh: FileHandle,
        _flags: OpenFlags,
        _lock_owner: Option<LockOwner>,
        _flush: bool,
        reply: ReplyEmpty,
    ) {
        let fh_val = fh.0;
        let mut inner = self.inner.lock().unwrap();

        let flushed = if inner.write_buf.is_open(fh_val) {
            self.flush_to_mtp(&mut inner, fh_val)
        } else {
            Ok(())
        };

        inner.read_cache.remove(&fh_val);
        inner.fh_to_inode.remove(&fh_val);

        // A failed flush means the bytes never reached the device, so `close()`
        // has to say so instead of pretending the write worked.
        match flushed {
            Ok(()) => reply.ok(),
            Err(e) => {
                error!("Flush on close failed: {e}");
                reply.error(Errno::EIO);
            }
        }
    }

    fn write(
        &self,
        _req: &Request,
        ino: INodeNo,
        fh: FileHandle,
        offset: u64,
        data: &[u8],
        _write_flags: WriteFlags,
        _flags: OpenFlags,
        _lock_owner: Option<LockOwner>,
        reply: ReplyWrite,
    ) {
        if self.read_only {
            reply.error(Errno::EROFS);
            return;
        }

        let fh_val = fh.0;
        let mut inner = self.inner.lock().unwrap();

        if !inner.write_buf.is_open(fh_val) {
            let original_size = inner.inodes.get(ino.0).map(|e| e.size).unwrap_or(0);
            if let Err(e) = inner.write_buf.open(fh_val, ino.0, original_size) {
                error!("Failed to open write buffer: {e}");
                reply.error(Errno::EIO);
                return;
            }
        }

        match inner.write_buf.write(fh_val, offset as i64, data) {
            Ok(written) => reply.written(written),
            Err(e) => {
                error!("Write failed: {e}");
                reply.error(Errno::EIO);
            }
        }
    }

    fn create(
        &self,
        _req: &Request,
        parent: INodeNo,
        name: &OsStr,
        _mode: u32,
        _umask: u32,
        _flags: i32,
        reply: ReplyCreate,
    ) {
        if self.read_only {
            reply.error(Errno::EROFS);
            return;
        }

        let name_str = match name.to_str() {
            Some(s) => s,
            None => {
                reply.error(Errno::EINVAL);
                return;
            }
        };

        let parent_ino = parent.0;
        let mut inner = self.inner.lock().unwrap();

        if !inner.inodes.get(parent_ino).is_some_and(|e| e.is_dir()) {
            reply.error(Errno::ENOTDIR);
            return;
        }

        let handle = match self.with_recovery(&mut inner, |fs, inner| {
            let mtp_parent = fs.parent_handle(inner, parent_ino)?;
            let storage_idx =
                Self::find_storage_index(inner, parent_ino).ok_or(mtp_rs::Error::NotFound)?;
            let info = NewObjectInfo::file(name_str, 0);
            let stream = bytes_stream(Vec::new());
            fs.rt
                .block_on(inner.storages[storage_idx].upload(mtp_parent, info, stream))
                .map_err(mtp_rs::Error::from)
        }) {
            Ok(h) => h,
            Err(e) => {
                error!("MTP create failed: {e}");
                reply.error(Errno::EIO);
                return;
            }
        };

        let now = SystemTime::now();
        let ino = inner
            .inodes
            .add_object(parent_ino, handle, name_str.to_string(), false, 0, now);

        let fh = self.alloc_fh();
        inner.fh_to_inode.insert(fh, ino);
        if let Err(e) = inner.write_buf.open(fh, ino, 0) {
            error!("Failed to open write buffer: {e}");
            reply.error(Errno::EIO);
            return;
        }

        let entry = inner.inodes.get(ino).unwrap();
        let attr = inode_to_file_attr(entry);
        reply.created(
            &TTL,
            &attr,
            Generation(0),
            FileHandle(fh),
            FopenFlags::empty(),
        );
    }

    fn mkdir(
        &self,
        _req: &Request,
        parent: INodeNo,
        name: &OsStr,
        _mode: u32,
        _umask: u32,
        reply: ReplyEntry,
    ) {
        if self.read_only {
            reply.error(Errno::EROFS);
            return;
        }

        let name_str = match name.to_str() {
            Some(s) => s,
            None => {
                reply.error(Errno::EINVAL);
                return;
            }
        };

        let parent_ino = parent.0;
        let mut inner = self.inner.lock().unwrap();

        if !inner.inodes.get(parent_ino).is_some_and(|e| e.is_dir()) {
            reply.error(Errno::ENOTDIR);
            return;
        }

        let handle = match self.with_recovery(&mut inner, |fs, inner| {
            let mtp_parent = fs.parent_handle(inner, parent_ino)?;
            let storage_idx =
                Self::find_storage_index(inner, parent_ino).ok_or(mtp_rs::Error::NotFound)?;
            fs.rt
                .block_on(inner.storages[storage_idx].create_folder(mtp_parent, name_str))
        }) {
            Ok(h) => h,
            Err(e) => {
                error!("MTP mkdir failed: {e}");
                reply.error(Errno::EIO);
                return;
            }
        };

        let now = SystemTime::now();
        let ino = inner
            .inodes
            .add_object(parent_ino, handle, name_str.to_string(), true, 0, now);

        let entry = inner.inodes.get(ino).unwrap();
        let attr = inode_to_file_attr(entry);
        reply.entry(&TTL, &attr, Generation(0));
    }

    fn unlink(&self, _req: &Request, parent: INodeNo, name: &OsStr, reply: ReplyEmpty) {
        if self.read_only {
            reply.error(Errno::EROFS);
            return;
        }

        let name_str = match name.to_str() {
            Some(s) => s,
            None => {
                reply.error(Errno::ENOENT);
                return;
            }
        };

        let parent_ino = parent.0;
        let mut inner = self.inner.lock().unwrap();

        let child_ino = match inner.inodes.lookup(parent_ino, name_str) {
            Some(i) => i,
            None => {
                reply.error(Errno::ENOENT);
                return;
            }
        };

        if inner.inodes.get(child_ino).is_some_and(|e| e.is_dir()) {
            reply.error(Errno::EISDIR);
            return;
        }

        if let Err(e) = self.with_recovery(&mut inner, |fs, inner| {
            let handle = fs.file_handle(inner, child_ino)?;
            let storage_idx =
                Self::find_storage_index(inner, child_ino).ok_or(mtp_rs::Error::NotFound)?;
            fs.rt.block_on(inner.storages[storage_idx].delete(handle))
        }) {
            error!("MTP delete failed: {e}");
            reply.error(Errno::EIO);
            return;
        }

        inner.inodes.remove(child_ino);
        reply.ok();
    }

    fn rmdir(&self, _req: &Request, parent: INodeNo, name: &OsStr, reply: ReplyEmpty) {
        if self.read_only {
            reply.error(Errno::EROFS);
            return;
        }

        let name_str = match name.to_str() {
            Some(s) => s,
            None => {
                reply.error(Errno::ENOENT);
                return;
            }
        };

        let parent_ino = parent.0;
        let mut inner = self.inner.lock().unwrap();

        let child_ino = match inner.inodes.lookup(parent_ino, name_str) {
            Some(i) => i,
            None => {
                reply.error(Errno::ENOENT);
                return;
            }
        };

        if !matches!(
            inner.inodes.get(child_ino).map(|e| &e.kind),
            Some(InodeKind::Directory { .. })
        ) {
            reply.error(Errno::ENOTDIR);
            return;
        }

        if let Err(e) = self.with_recovery(&mut inner, |fs, inner| {
            let handle = fs.object_handle(inner, child_ino)?;
            let storage_idx =
                Self::find_storage_index(inner, child_ino).ok_or(mtp_rs::Error::NotFound)?;
            fs.rt.block_on(inner.storages[storage_idx].delete(handle))
        }) {
            error!("MTP rmdir failed: {e}");
            reply.error(Errno::EIO);
            return;
        }

        inner.inodes.remove(child_ino);
        reply.ok();
    }

    fn rename(
        &self,
        _req: &Request,
        parent: INodeNo,
        name: &OsStr,
        newparent: INodeNo,
        newname: &OsStr,
        _flags: RenameFlags,
        reply: ReplyEmpty,
    ) {
        if self.read_only {
            reply.error(Errno::EROFS);
            return;
        }

        let name_str = match name.to_str() {
            Some(s) => s,
            None => {
                reply.error(Errno::ENOENT);
                return;
            }
        };
        let newname_str = match newname.to_str() {
            Some(s) => s,
            None => {
                reply.error(Errno::EINVAL);
                return;
            }
        };

        let parent_ino = parent.0;
        let newparent_ino = newparent.0;
        let mut inner = self.inner.lock().unwrap();

        let child_ino = match inner.inodes.lookup(parent_ino, name_str) {
            Some(i) => i,
            None => {
                reply.error(Errno::ENOENT);
                return;
            }
        };

        if !matches!(
            inner.inodes.get(child_ino).map(|e| &e.kind),
            Some(InodeKind::File { .. } | InodeKind::Directory { .. })
        ) {
            reply.error(Errno::EINVAL);
            return;
        }
        if parent_ino != newparent_ino
            && !inner.inodes.get(newparent_ino).is_some_and(|e| e.is_dir())
        {
            reply.error(Errno::ENOTDIR);
            return;
        }

        if name_str != newname_str {
            if let Err(e) = self.with_recovery(&mut inner, |fs, inner| {
                let handle = fs.object_handle(inner, child_ino)?;
                let storage_idx =
                    Self::find_storage_index(inner, child_ino).ok_or(mtp_rs::Error::NotFound)?;
                fs.rt
                    .block_on(inner.storages[storage_idx].rename(handle, newname_str))
            }) {
                error!("MTP rename failed: {e}");
                reply.error(Errno::EIO);
                return;
            }
        }

        if parent_ino != newparent_ino {
            if let Err(e) = self.with_recovery(&mut inner, |fs, inner| {
                let handle = fs.object_handle(inner, child_ino)?;
                let storage_idx =
                    Self::find_storage_index(inner, child_ino).ok_or(mtp_rs::Error::NotFound)?;
                let new_mtp_parent = fs
                    .parent_handle(inner, newparent_ino)?
                    .unwrap_or(ObjectHandle::ROOT);
                fs.rt.block_on(inner.storages[storage_idx].move_object(
                    handle,
                    new_mtp_parent,
                    None,
                ))
            }) {
                error!("MTP move failed: {e}");
                reply.error(Errno::EIO);
                return;
            }
        }

        inner
            .inodes
            .rename(child_ino, newparent_ino, newname_str.to_string());
        reply.ok();
    }

    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,
    ) {
        if let Some(new_size) = size {
            if self.read_only {
                reply.error(Errno::EROFS);
                return;
            }

            if let Some(fh) = fh {
                let fh_val = fh.0;
                let mut inner = self.inner.lock().unwrap();

                if !inner.write_buf.is_open(fh_val) {
                    let original_size = inner.inodes.get(ino.0).map(|e| e.size).unwrap_or(0);
                    if let Err(e) = inner.write_buf.open(fh_val, ino.0, original_size) {
                        error!("Failed to open write buffer: {e}");
                        reply.error(Errno::EIO);
                        return;
                    }
                }

                if new_size == 0 {
                    inner.write_buf.close(fh_val);
                    if let Err(e) = inner.write_buf.open(fh_val, ino.0, 0) {
                        error!("Failed to open write buffer: {e}");
                        reply.error(Errno::EIO);
                        return;
                    }
                }
            }
        }

        let inner = self.inner.lock().unwrap();
        match inner.inodes.get(ino.0) {
            Some(entry) => {
                let mut attr = inode_to_file_attr(entry);
                if let Some(new_size) = size {
                    attr.size = new_size;
                    attr.blocks = new_size.div_ceil(512);
                }
                reply.attr(&TTL, &attr);
            }
            None => {
                reply.error(Errno::ENOENT);
            }
        }
    }

    fn statfs(&self, _req: &Request, _ino: INodeNo, reply: ReplyStatfs) {
        let inner = self.inner.lock().unwrap();
        let block_size: u64 = 4096;

        let mut total_bytes: u64 = 0;
        let mut free_bytes: u64 = 0;
        for storage in &inner.storages {
            total_bytes = total_bytes.saturating_add(storage.info().total_capacity);
            free_bytes = free_bytes.saturating_add(storage.info().free_space);
        }

        let blocks = total_bytes / block_size;
        let bfree = free_bytes / block_size;

        reply.statfs(blocks, bfree, bfree, 0, 0, block_size as u32, 255, 0);
    }

    fn opendir(&self, _req: &Request, ino: INodeNo, _flags: OpenFlags, reply: ReplyOpen) {
        let mut inner = self.inner.lock().unwrap();
        match inner.inodes.get(ino.0) {
            Some(entry) if entry.is_dir() => {
                let fh = self.alloc_fh();
                inner.dirs_loaded.remove(&ino.0);
                reply.opened(FileHandle(fh), FopenFlags::empty());
            }
            Some(_) => {
                reply.error(Errno::ENOTDIR);
            }
            None => {
                reply.error(Errno::ENOENT);
            }
        }
    }

    fn releasedir(
        &self,
        _req: &Request,
        _ino: INodeNo,
        _fh: FileHandle,
        _flags: OpenFlags,
        reply: ReplyEmpty,
    ) {
        reply.ok();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use futures::StreamExt as _;
    use std::io::Write as _;

    const CHUNK: usize = UPLOAD_CHUNK;

    /// A temp file holding `content`, rewound to the start.
    fn spool_file(content: &[u8]) -> std::fs::File {
        let mut file = tempfile::tempfile().unwrap();
        file.write_all(content).unwrap();
        file.seek(SeekFrom::Start(0)).unwrap();
        file
    }

    #[test]
    fn file_stream_reads_lazily() {
        let file = spool_file(&vec![0xABu8; CHUNK * 4]);
        // `try_clone` dups the fd, so the clone shares the read cursor and shows
        // how far the stream has actually read.
        let mut cursor = file.try_clone().unwrap();

        let mut stream = file_stream(file);
        let first = futures::executor::block_on(stream.next()).unwrap().unwrap();

        assert_eq!(first.len(), CHUNK);
        assert_eq!(
            cursor.stream_position().unwrap(),
            CHUNK as u64,
            "one poll must read one chunk, not the whole file"
        );
    }

    #[test]
    fn file_stream_yields_the_whole_file_then_ends() {
        // Two full chunks plus a short tail, so the partial-read path is covered.
        let content = vec![0xCDu8; CHUNK * 2 + 17];

        let chunks: Vec<_> =
            futures::executor::block_on(file_stream(spool_file(&content)).collect());

        let sizes: Vec<_> = chunks.iter().map(|c| c.as_ref().unwrap().len()).collect();
        assert_eq!(sizes, vec![CHUNK, CHUNK, 17]);
        let joined: Vec<u8> = chunks
            .into_iter()
            .flat_map(|c| c.unwrap().to_vec())
            .collect();
        assert_eq!(joined, content);
    }

    #[test]
    fn file_stream_ends_after_a_read_error() {
        // A write-only fd fails every read with EBADF, and the stream has to stop
        // there rather than re-emitting the error forever.
        let path = tempfile::NamedTempFile::new().unwrap().into_temp_path();
        let file = std::fs::OpenOptions::new().write(true).open(&path).unwrap();

        let mut stream = file_stream(file);
        assert!(futures::executor::block_on(stream.next()).unwrap().is_err());
        assert!(futures::executor::block_on(stream.next()).is_none());
    }
}