irontide-session 1.0.1

BitTorrent session management: peers, torrents, and piece selection
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
//! IOCP disk I/O backend -- wraps [`PosixDiskIo`], overrides `write_block_direct()`.
//!
//! Uses a single shared IOCP completion port per session. Writes and reads are
//! submitted synchronously within `block_in_place` context -- NOT async-integrated
//! with tokio.
//!
//! The entire module is gated with `#[cfg(all(target_os = "windows", feature = "iocp"))]`
//! at the `mod` declaration site in `lib.rs`.

use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};

use bytes::Bytes;
use irontide_core::{Id20, Id32};
use irontide_storage::{FileMap, IocpConfig, IocpStorageState, TorrentStorage};
use parking_lot::{Mutex, RwLock};
use tracing::warn;
use windows_sys::Win32::Foundation::{
    CloseHandle, ERROR_IO_PENDING, GetLastError, HANDLE, INVALID_HANDLE_VALUE,
};
use windows_sys::Win32::Storage::FileSystem::{ReadFile, WriteFile};
use windows_sys::Win32::System::IO::{
    CreateIoCompletionPort, GetQueuedCompletionStatusEx, OVERLAPPED, OVERLAPPED_ENTRY,
};
use windows_sys::Win32::System::Threading::INFINITE;

use crate::disk::DiskConfig;
use crate::disk_backend::{DiskIoBackend, DiskIoStats, PosixDiskIo};

/// Per-torrent IOCP state: pre-opened HANDLEs + file map.
pub(crate) struct IocpTorrentState {
    handles: IocpStorageState,
    file_map: FileMap,
}

/// IOCP disk I/O backend.
///
/// Wraps [`PosixDiskIo`] for cache and hashing. Overrides `write_block_direct()`
/// and volatile `read_chunk()` to submit I/O via Windows overlapped IOCP.
///
/// Falls back to the inner [`PosixDiskIo`] when:
/// - Storage does not implement `filesystem_info()` (memory, mmap backends)
/// - IOCP handle opening fails at registration time
/// - An IOCP I/O submission fails at write/read time
pub(crate) struct IocpDiskIo {
    inner: PosixDiskIo,
    iocp: HANDLE,
    /// Serialize submit+reap to prevent completion interleaving between threads.
    iocp_guard: Mutex<()>,
    pub(crate) iocp_states: RwLock<HashMap<Id20, IocpTorrentState>>,
    config: IocpConfig,
    /// Cumulative bytes written via the IOCP path (lock-free stat tracking).
    iocp_write_bytes: AtomicU64,
    /// Cumulative bytes read via the IOCP path (lock-free stat tracking).
    iocp_read_bytes: AtomicU64,
}

/// Extract contiguous bytes from two slices at a given offset and length.
///
/// Returns a `Vec<u8>` containing the data from `s0` and `s1` as if they were
/// concatenated, starting at byte `pos` for `len` bytes.
fn extract_segment_data(s0: &[u8], s1: &[u8], pos: usize, len: usize) -> Vec<u8> {
    let end = pos.saturating_add(len);
    if end <= s0.len() {
        // Entirely within s0.
        s0[pos..end].to_vec()
    } else if pos >= s0.len() {
        // Entirely within s1.
        let s1_start = pos.saturating_sub(s0.len());
        let s1_end = end.saturating_sub(s0.len());
        s1[s1_start..s1_end].to_vec()
    } else {
        // Straddle: part from s0, part from s1.
        let mut buf = Vec::with_capacity(len);
        buf.extend_from_slice(&s0[pos..]);
        let s1_need = len.saturating_sub(s0.len().saturating_sub(pos));
        buf.extend_from_slice(&s1[..s1_need]);
        buf
    }
}

impl IocpDiskIo {
    /// Create a new IOCP backend wrapping [`PosixDiskIo`].
    ///
    /// # Errors
    ///
    /// Returns an I/O error if `CreateIoCompletionPort` fails to create the
    /// completion port.
    pub fn new(disk_config: &DiskConfig, iocp_config: IocpConfig) -> std::io::Result<Self> {
        // SAFETY: Creating a new IOCP with no file handle association.
        // INVALID_HANDLE_VALUE as the file handle + 0 for existing port = new port.
        let iocp = unsafe {
            CreateIoCompletionPort(
                INVALID_HANDLE_VALUE,
                std::ptr::null_mut(),
                0,
                iocp_config.concurrent_threads,
            )
        };
        if iocp.is_null() {
            return Err(std::io::Error::last_os_error());
        }

        Ok(Self {
            inner: PosixDiskIo::new(disk_config),
            iocp,
            iocp_guard: Mutex::new(()),
            iocp_states: RwLock::new(HashMap::new()),
            config: iocp_config,
            iocp_write_bytes: AtomicU64::new(0),
            iocp_read_bytes: AtomicU64::new(0),
        })
    }

    /// Associate a file HANDLE with the IOCP completion port.
    ///
    /// # Errors
    ///
    /// Returns an I/O error if `CreateIoCompletionPort` fails to associate
    /// the handle.
    fn associate_handle(&self, handle: HANDLE) -> std::io::Result<()> {
        // SAFETY: `handle` is a valid file HANDLE opened with FILE_FLAG_OVERLAPPED.
        // `self.iocp` is a valid completion port from the constructor.
        let result = unsafe { CreateIoCompletionPort(handle, self.iocp, 0, 0) };
        if result.is_null() {
            return Err(std::io::Error::last_os_error());
        }
        Ok(())
    }

    /// Submit overlapped writes via IOCP for a single block.
    ///
    /// Maps (piece, begin, s0, s1) to file segments via [`FileMap`], builds
    /// `OVERLAPPED` per segment, issues `WriteFile`, and reaps completions via
    /// `GetQueuedCompletionStatusEx`.
    ///
    /// # Errors
    ///
    /// Returns an I/O error if WriteFile submission fails (not pending), if
    /// reaping fails, or if fewer completions arrive than expected.
    fn iocp_write(
        &self,
        state: &IocpTorrentState,
        piece: u32,
        begin: u32,
        s0: &[u8],
        s1: &[u8],
    ) -> crate::Result<()> {
        let total_len = s0.len().saturating_add(s1.len());
        let total_u32 = u32::try_from(total_len).map_err(|_| {
            std::io::Error::new(
                std::io::ErrorKind::InvalidInput,
                "block length exceeds u32::MAX",
            )
        })?;
        let segments = state.file_map.chunk_segments(piece, begin, total_u32);
        let num_segments = segments.len();

        if num_segments == 0 {
            return Ok(());
        }

        // Prepare per-segment data buffers and OVERLAPPED structures.
        // Buffers must remain alive until completions are reaped.
        let mut seg_bufs: Vec<Vec<u8>> = Vec::with_capacity(num_segments);
        let mut overlappeds: Vec<OVERLAPPED> = Vec::with_capacity(num_segments);

        let mut pos: usize = 0;
        for seg in &segments {
            let seg_len = seg.len as usize;
            let buf = extract_segment_data(s0, s1, pos, seg_len);
            pos = pos.saturating_add(seg_len);

            let mut ov: OVERLAPPED = unsafe { std::mem::zeroed() };
            // Set the file offset in the OVERLAPPED union fields.
            unsafe {
                ov.Anonymous.Anonymous.Offset = (seg.file_offset & 0xFFFF_FFFF) as u32;
                ov.Anonymous.Anonymous.OffsetHigh = (seg.file_offset >> 32) as u32;
            }

            seg_bufs.push(buf);
            overlappeds.push(ov);
        }

        // Lock the IOCP guard to serialize submit+reap.
        let _guard = self.iocp_guard.lock();

        for i in 0..num_segments {
            let handle = state.handles.handle(segments[i].file_index);
            let buf = &seg_bufs[i];
            let ov = &mut overlappeds[i];
            let len_u32 = u32::try_from(buf.len()).map_err(|_| {
                std::io::Error::new(
                    std::io::ErrorKind::InvalidInput,
                    "segment length exceeds u32::MAX",
                )
            })?;

            // SAFETY: `handle` is valid and associated with IOCP. `buf` and `ov`
            // are alive for the duration of the I/O. The guard ensures no
            // concurrent submissions interleave completions.
            let ok = unsafe {
                WriteFile(
                    handle,
                    buf.as_ptr(),
                    len_u32,
                    std::ptr::null_mut(),
                    ov as *mut OVERLAPPED,
                )
            };

            if ok == 0 {
                let err = unsafe { GetLastError() };
                if err != ERROR_IO_PENDING {
                    return Err(crate::Error::Io(std::io::Error::from_raw_os_error(
                        err as i32,
                    )));
                }
            }
        }

        // Reap all completions.
        let mut entries: Vec<OVERLAPPED_ENTRY> = vec![unsafe { std::mem::zeroed() }; num_segments];
        let mut removed: u32 = 0;

        // SAFETY: `entries` has capacity for `num_segments` entries. `self.iocp`
        // is a valid completion port. INFINITE timeout blocks until all
        // completions arrive.
        let ok = unsafe {
            GetQueuedCompletionStatusEx(
                self.iocp,
                entries.as_mut_ptr(),
                u32::try_from(num_segments).unwrap_or(u32::MAX),
                &mut removed,
                INFINITE,
                0, // not alertable
            )
        };

        if ok == 0 {
            return Err(crate::Error::Io(std::io::Error::last_os_error()));
        }

        let completed = removed as usize;
        if completed < num_segments {
            return Err(crate::Error::Io(std::io::Error::other(format!(
                "iocp: expected {num_segments} write completions, got {completed}"
            ))));
        }

        // Check for per-entry errors via Internal field.
        for entry in entries.iter().take(completed) {
            // Internal == 0 means success (STATUS_SUCCESS / NTSTATUS 0).
            // Non-zero Internal values are NTSTATUS error codes.
            if entry.Internal != 0 {
                // Convert NTSTATUS to a meaningful error. The lower 16 bits
                // of many NTSTATUS codes map to Win32 error codes, but the
                // safest approach is to report the raw status.
                return Err(crate::Error::Io(std::io::Error::other(format!(
                    "iocp: write completion failed with NTSTATUS 0x{:08X}",
                    entry.Internal
                ))));
            }
        }

        Ok(())
    }

    /// Submit overlapped reads via IOCP for a contiguous chunk.
    ///
    /// Maps (piece, begin, length) to file segments via [`FileMap`], builds
    /// `OVERLAPPED` per segment, issues `ReadFile`, and reaps completions.
    ///
    /// # Errors
    ///
    /// Returns an I/O error if ReadFile submission fails (not pending), if
    /// reaping fails, or if fewer completions arrive than expected.
    fn iocp_read(
        &self,
        state: &IocpTorrentState,
        piece: u32,
        begin: u32,
        length: u32,
    ) -> crate::Result<Vec<u8>> {
        let segments = state.file_map.chunk_segments(piece, begin, length);
        let num_segments = segments.len();

        let mut buf = vec![0u8; length as usize];

        if num_segments == 0 {
            return Ok(buf);
        }

        // Build per-segment buffer slices and OVERLAPPED structures.
        // We track (offset, len) into `buf` for each segment.
        let mut seg_ranges: Vec<(usize, usize)> = Vec::with_capacity(num_segments);
        let mut overlappeds: Vec<OVERLAPPED> = Vec::with_capacity(num_segments);

        let mut pos: usize = 0;
        for seg in &segments {
            let seg_len = seg.len as usize;
            let seg_end = pos.saturating_add(seg_len);

            let mut ov: OVERLAPPED = unsafe { std::mem::zeroed() };
            unsafe {
                ov.Anonymous.Anonymous.Offset = (seg.file_offset & 0xFFFF_FFFF) as u32;
                ov.Anonymous.Anonymous.OffsetHigh = (seg.file_offset >> 32) as u32;
            }

            seg_ranges.push((pos, seg_end));
            overlappeds.push(ov);
            pos = seg_end;
        }

        // Lock the IOCP guard to serialize submit+reap.
        let _guard = self.iocp_guard.lock();

        for i in 0..num_segments {
            let handle = state.handles.handle(segments[i].file_index);
            let (start, end) = seg_ranges[i];
            let seg_buf = &mut buf[start..end];
            let ov = &mut overlappeds[i];
            let len_u32 = u32::try_from(seg_buf.len()).map_err(|_| {
                std::io::Error::new(
                    std::io::ErrorKind::InvalidInput,
                    "segment length exceeds u32::MAX",
                )
            })?;

            // SAFETY: `handle` is valid and associated with IOCP. `seg_buf` and
            // `ov` are alive for the duration of the I/O. The guard prevents
            // concurrent interleaving.
            let ok = unsafe {
                ReadFile(
                    handle,
                    seg_buf.as_mut_ptr(),
                    len_u32,
                    std::ptr::null_mut(),
                    ov as *mut OVERLAPPED,
                )
            };

            if ok == 0 {
                let err = unsafe { GetLastError() };
                if err != ERROR_IO_PENDING {
                    return Err(crate::Error::Io(std::io::Error::from_raw_os_error(
                        err as i32,
                    )));
                }
            }
        }

        // Reap all completions.
        let mut entries: Vec<OVERLAPPED_ENTRY> = vec![unsafe { std::mem::zeroed() }; num_segments];
        let mut removed: u32 = 0;

        let ok = unsafe {
            GetQueuedCompletionStatusEx(
                self.iocp,
                entries.as_mut_ptr(),
                u32::try_from(num_segments).unwrap_or(u32::MAX),
                &mut removed,
                INFINITE,
                0,
            )
        };

        if ok == 0 {
            return Err(crate::Error::Io(std::io::Error::last_os_error()));
        }

        let completed = removed as usize;
        if completed < num_segments {
            return Err(crate::Error::Io(std::io::Error::other(format!(
                "iocp: expected {num_segments} read completions, got {completed}"
            ))));
        }

        for entry in entries.iter().take(completed) {
            if entry.Internal != 0 {
                return Err(crate::Error::Io(std::io::Error::other(format!(
                    "iocp: read completion failed with NTSTATUS 0x{:08X}",
                    entry.Internal
                ))));
            }
        }

        Ok(buf)
    }
}

impl Drop for IocpDiskIo {
    fn drop(&mut self) {
        // SAFETY: `self.iocp` was created by `CreateIoCompletionPort` in the
        // constructor and has not been closed. Individual file HANDLEs are
        // closed by `IocpStorageState::drop`.
        unsafe {
            CloseHandle(self.iocp);
        }
    }
}

impl DiskIoBackend for IocpDiskIo {
    fn name(&self) -> &str {
        "iocp"
    }

    fn register(&self, info_hash: Id20, storage: Arc<dyn TorrentStorage>) {
        // Register with inner backend first (for reads/cache).
        self.inner.register(info_hash, Arc::clone(&storage));

        // Try to open IOCP handles via filesystem_info().
        if let Some((base_dir, file_paths, file_map)) = storage.filesystem_info() {
            match IocpStorageState::open_files(base_dir, file_paths, self.config.enable_direct_io) {
                Ok(handles) => {
                    // Associate each handle with our IOCP port.
                    for i in 0..handles.len() {
                        if let Err(e) = self.associate_handle(handles.handle(i)) {
                            warn!(
                                %info_hash,
                                file_index = i,
                                error = %e,
                                "iocp: failed to associate handle, falling back to posix"
                            );
                            return;
                        }
                    }

                    self.iocp_states.write().insert(
                        info_hash,
                        IocpTorrentState {
                            handles,
                            file_map: file_map.clone(),
                        },
                    );
                }
                Err(e) => {
                    warn!(
                        %info_hash,
                        error = %e,
                        "iocp: failed to open handles, falling back to posix"
                    );
                }
            }
        }
    }

    fn unregister(&self, info_hash: Id20) {
        // Remove IOCP state first (Drop closes handles).
        self.iocp_states.write().remove(&info_hash);
        self.inner.unregister(info_hash);
    }

    fn write_chunk(
        &self,
        info_hash: Id20,
        piece: u32,
        begin: u32,
        data: Bytes,
        flush: bool,
    ) -> crate::Result<()> {
        self.inner.write_chunk(info_hash, piece, begin, data, flush)
    }

    fn read_chunk(
        &self,
        info_hash: Id20,
        piece: u32,
        begin: u32,
        length: u32,
        volatile: bool,
    ) -> crate::Result<Bytes> {
        // Non-volatile reads go through inner (cache-aware path).
        if !volatile {
            return self
                .inner
                .read_chunk(info_hash, piece, begin, length, volatile);
        }
        // Volatile reads (won't be re-read) use IOCP directly.
        let iocp_states = self.iocp_states.read();
        if let Some(state) = iocp_states.get(&info_hash) {
            let data = self.iocp_read(state, piece, begin, length)?;
            drop(iocp_states);
            self.iocp_read_bytes
                .fetch_add(u64::from(length), Ordering::Relaxed);
            return Ok(Bytes::from(data));
        }
        drop(iocp_states);
        self.inner
            .read_chunk(info_hash, piece, begin, length, volatile)
    }

    fn read_piece(&self, info_hash: Id20, piece: u32) -> crate::Result<Vec<u8>> {
        let iocp_states = self.iocp_states.read();
        if let Some(state) = iocp_states.get(&info_hash) {
            // Check-before-flush: only flush if piece is in buffer pool cache.
            // The common path (write_block_direct) bypasses the pool entirely,
            // so flushing is usually unnecessary.
            if self.inner.cached_pieces(info_hash).contains(&piece) {
                self.inner.flush_piece(info_hash, piece)?;
            }
            let piece_size = state.file_map.piece_size(piece);
            let data = self.iocp_read(state, piece, 0, piece_size)?;
            drop(iocp_states);
            self.iocp_read_bytes
                .fetch_add(u64::from(piece_size), Ordering::Relaxed);
            return Ok(data);
        }
        drop(iocp_states);
        self.inner.read_piece(info_hash, piece)
    }

    fn hash_piece(&self, info_hash: Id20, piece: u32, expected: &Id20) -> crate::Result<bool> {
        self.inner.hash_piece(info_hash, piece, expected)
    }

    fn hash_piece_v2(&self, info_hash: Id20, piece: u32, expected: &Id32) -> crate::Result<bool> {
        self.inner.hash_piece_v2(info_hash, piece, expected)
    }

    fn hash_block(
        &self,
        info_hash: Id20,
        piece: u32,
        begin: u32,
        length: u32,
    ) -> crate::Result<Id32> {
        self.inner.hash_block(info_hash, piece, begin, length)
    }

    fn clear_piece(&self, info_hash: Id20, piece: u32) {
        self.inner.clear_piece(info_hash, piece)
    }

    fn flush_piece(&self, info_hash: Id20, piece: u32) -> crate::Result<()> {
        self.inner.flush_piece(info_hash, piece)
    }

    fn flush_all(&self) -> crate::Result<()> {
        self.inner.flush_all()
    }

    fn cached_pieces(&self, info_hash: Id20) -> Vec<u32> {
        self.inner.cached_pieces(info_hash)
    }

    fn stats(&self) -> DiskIoStats {
        let mut s = self.inner.stats();
        s.write_bytes = s
            .write_bytes
            .saturating_add(self.iocp_write_bytes.load(Ordering::Relaxed));
        s.read_bytes = s
            .read_bytes
            .saturating_add(self.iocp_read_bytes.load(Ordering::Relaxed));
        s
    }

    fn write_block_direct(
        &self,
        info_hash: Id20,
        piece: u32,
        begin: u32,
        s0: &[u8],
        s1: &[u8],
    ) -> crate::Result<()> {
        // If we have IOCP state for this torrent, use IOCP.
        let iocp_states = self.iocp_states.read();
        if let Some(state) = iocp_states.get(&info_hash) {
            let result = self.iocp_write(state, piece, begin, s0, s1);
            drop(iocp_states);

            if result.is_ok() {
                let total = (s0.len().saturating_add(s1.len())) as u64;
                self.iocp_write_bytes.fetch_add(total, Ordering::Relaxed);
                return Ok(());
            }

            // Fall through to inner on failure.
            warn!(
                %info_hash,
                piece,
                begin,
                error = %result.as_ref().unwrap_err(),
                "iocp write failed, falling back to posix"
            );
        } else {
            drop(iocp_states);
        }

        self.inner
            .write_block_direct(info_hash, piece, begin, s0, s1)
    }
}