Skip to content

usb: USB mass storage S1 (BOT/SCSI, block nodes, /dev/uevent v2, mount/2) - #170

Merged
ccclosed merged 11 commits into
devfrom
feat/usb-ms-s1
Nov 22, 2026
Merged

ccclosed merged 11 commits into
devfrom
feat/usb-ms-s1

Conversation

@ccclosed

Copy link
Copy Markdown
Owner

USB mass storage S1 (kernel)

Base dev, branch feat/usb-ms-s1. Spec: docs/usb-mass-storage-spec.md (fbec8f8)
and docs/usb-storage-files-ux.md (5198b01). Version intentionally not bumped.

Stages

  • S1a aa6ab747 — xHCI MSC enumeration + BOT/SCSI. usb_desc::parse_msc_interfaces
    (class 8/subclass 6/protocol 0x50), GET_MAX_LUN (STALL → LUN 0), CBW 31 B,
    data stage, CSW 13 B with a 30 s wait, STALL → ClearFeature+Reset Endpoint,
    full Reset Recovery (Bulk-Only Reset + Reset Endpoint + Set TR Dequeue Pointer)
    and one retry; three failures mark the channel dead. SCSI: TUR, INQUIRY,
    READ CAPACITY(10), READ/WRITE(10), REQUEST SENSE, SYNCHRONIZE CACHE(10);
    4K logical blocks and >2 TiB are explicit rejects (no truncation).
    Synchronous BOT runs on the calling thread under CONTROLLER; probe/registration
    run in a separate usb-storage thread after poll() releases the lock.
  • S1b — usbN/usbNpM via drivers::partition (GPT/MBR), monotonic
    non-reused names, /proc/partitions (major 240, 16*N+M), read-only
    /dev/block/<name> (S_IFBLK; raw write not offered), GPT unique GUID and
    ext2 s_uuid/s_volume_name helpers.
  • S1c — /dev/uevent v2: exactly 144 B (usb_uevent_abi host-tests every
    offset/version), ring 256, live seq++, seq=0 snapshots on open and
    PUEV_IOC_SNAPSHOT (0x5501); exclusive reader (EBUSY); read >= 144
    returns one record, < 144 is EINVAL; poll(POLLIN) iff non-empty.
    ADD/REMOVE/CHANGE/ERROR/MOUNTED/UNMOUNTED carry mount[16] and uuid[16].
  • S1d — mount(2) nr 165 / umount2(2) nr 166 (ABI 168→170; SUPPORTED_SYSCALLS
    and the independent EXPECTED list). Root-only, ext2 only, source usbN/usbNpM,
    target exactly "/" + source and ≤15 bytes (else ENAMETOOLONG + bad_mount).
    Only pagh volumes mount (mount writes group descriptors/WAL reservation; foreign
    ext2 stays visible, EINVAL). Busy scan of all fd tables → EBUSY; clean unmount
    flushes with SYNCHRONIZE CACHE(10); surprise removal detaches with EIO,
    unclean=1, safe_to_remove=0; MNT_FORCE only on dead devices; replay failure
    is fail-closed. ROOT_KEEPING_COMPONENTS learns the usb prefix so /usb0p1
    is not rewritten to /mnt/usb0p1; the boot /mnt flow is unchanged and the
    kernel never auto-mounts (userspace compositor policy).

Evidence

  • QEMU usb-storage acceptance: 46/46 checks (/tmp/serial-accept2.log):
    uevent snapshot ADD usb0p1 (fs=1, uuid), mount usb0p1 /usb0p1 rc=0 + MOUNTED,
    write/read hello, statfs, busy-fd umount EBUSY (-16), clean umount +
    UNMOUNTED (flags=144), /proc/partitions + /dev/block/usb0, QMP device_del
    → REMOVE, device_add → usb1 (monotonic) with the file still readable,
    open-fd read after surprise unplug → EIO (-5), ERROR+REMOVE events,
    dirty umount EIO (-5) + UNMOUNTED flags=224 (unclean/detached/surprise),
    second surprise + MNT_FORCE → 0 + flags=240 (safe/unclean/detached/surprise),
    re-add as usb3 → mount does WAL replay, committed file readable,
    bad target /mnt/usb3p1 → -22, final clean umount.
  • In-QEMU selftest SELFTEST SUMMARY: 123 routines, 0 failed checks, 1 skipped
    (the skip is NVMe because this QEMU run has no NVMe device); new routine
    usb::BOT/SCSI/uevent layouts and /usb root rule passes.
  • Gates 6/6: cargo fmt --all -- --check; tools/build.py build;
    tools/build.py build --release; tools/host_tests.py (872 passed, 0 failed);
    tools/check_safety.py; tools/check_agents_md.py.

Honest limits (named, not hidden)

  • Only 512-byte logical blocks; 4K → error=unsupported_block; LBA > 32 bits and

    2 TiB → error=capacity_overflow (READ(16)/WRITE(16) builders exist but are
    not enabled).

  • LUN 0 only; GET_MAX_LUN > 0 is logged and skipped.
  • No hubs, no UAS, no EHCI/UHCI/OHCI; one xHCI controller; no real hardware run yet.
  • A new open on a stale surprise mount returns ENOENT because dentry lookup
    itself does I/O and link_walk maps lookup errors to not-found; an already-open
    descriptor gets EIO. This nuance is documented rather than papered over.
  • Auto-mount, eject UX and Files UI are S2–S4 (userspace) and not in this PR.

QEMU hotplug note: with -drive, device_del drops the block backend and a
later QMP device_add ... drive= fails; acceptance uses -blockdev/node-name.

agent-4 and others added 11 commits November 21, 2026 16:05
Add the transport half of USB mass storage: xHCI enumerates Bulk-Only
Transport interfaces (class 8/subclass 6/protocol 0x50), GET_MAX_LUN,
and a synchronous BOT command path (CBW 31 B, data stage, CSW 13 B,
STALL/Reset Recovery, 30 s CSW wait), plus the SCSI commands Phase 1
needs (TUR, INQUIRY, READ CAPACITY(10), READ/WRITE(10), REQUEST SENSE,
SYNCHRONIZE CACHE(10)). 4K logical blocks and >2 TiB capacities are
refused explicitly with a reason, never truncated.

A separate usb-storage thread runs probe and partition registration after
poll() releases CONTROLLER: spinlocks are not reentrant, so BOT commands
cannot run inside the port poll. Devices get monotonic usbN names that
are never reused; hot-unplug is detected from PORTSC.CCS and marks the
channel dead before the slot is released, so open descriptors get EIO
instead of talking to a vanished endpoint.

Pure wire/SCSI layouts live in usb_msc_proto.rs/usb_scsi.rs and are
covered by host tests; QEMU usb-storage with a kernel-formatted GPT image
enumerates usb0, reads INQUIRY/capacity, and shows usb0p1 as ext2.
Complete the kernel half of USB mass storage S1. S1b/S1c/S1d share one
commit because procfs, VFS and the syscall layer are the same plumbing:
splitting them would leave non-building intermediate trees.

S1b: partitions (GPT/MBR) are reused from drivers::partition as usbNpM;
usbN is monotonic and never reused; /proc/partitions renders DeviceManager
(major 240, 16*N+M) and read-only /dev/block/<name> gives diagnostics
without a raw write path that could bypass the WAL. GPT unique GUIDs and
ext2 s_uuid/s_volume_name feed the event uuid/label fields.

S1c: /dev/uevent v2 is a fixed 144-byte record (usb_uevent_abi, exact
offset/version host tests) with a 256-entry ring, seq monotonic live events
and seq=0 snapshots on open and PUEV_IOC_SNAPSHOT (0x5501). Open is
exclusive (second reader EBUSY); read(count>=144) returns exactly one
record, count<144 is EINVAL; poll returns POLLIN iff the ring is non-empty.
ADD/REMOVE/CHANGE/ERROR/MOUNTED/UNMOUNTED carry mount[16] and uuid[16].

S1d: mount(2) nr 165 and umount2(2) nr 166 (ABI 168->170, SUPPORTED +
independent EXPECTED). Root-only, ext2 only, source usbN/usbNpM, target
exactly "/"+source and <=15 bytes (longer -> ENAMETOOLONG + bad_mount).
Only a pagh volume mounts: mount_fs writes group descriptors and reserves
the WAL, so foreign ext2 stays visible but EINVAL. umount2 does a busy scan
of all fd tables, flushes with SYNCHRONIZE CACHE(10), and on surprise
removal detaches with EIO + unclean, leaving safe_to_remove=0; MNT_FORCE is
accepted only on a dead device. WAL replay happens in mount_fs and fails
closed. ROOT_KEEPING_COMPONENTS gains the usb-prefix rule so /usb0p1 is not
rewritten to /mnt/usb0p1; the boot /mnt flow is unchanged and the kernel
never auto-mounts.

Hot-unplug marks the channel dead before the slot is released, unregisters
the block names, keeps a stale record until umount2 so UNMOUNTED can be
emitted, and reuses the same storage thread for REMOVE. In-QEMU selftest
covers the pure BOT/SCSI/uevent layouts and the /usb path rule.

tools/usb_probe.c is the freestanding ring-3 probe for acceptance. QEMU
usb-storage: 46/46 checks (mount/write/read/statfs, busy EBUSY, clean and
surprise unmount, EBUSY/EBUSY, monotonic usb1/usb2/usb3 across QMP
device_del/device_add, open-fd EIO after unplug, MNT_FORCE, WAL replay
after surprise, bad target EINVAL). Host tests 872/0; six gates green.
A failed SYNCHRONIZE CACHE during umount2 keeps the mount in place (that
was already true), but the event only carried error=flush_failed.  The
volume's durability is unproven until the next flush/replay, so mark it
unclean and keep safe_to_remove=0; otherwise a UI reads a stale clean
state after a failed eject.
Sync with dev=f76444c5 (#169 terminal full shell, tinygl PR3, VT routing).

Conflict: src/test.rs appended routines at the same place. Kept both — the
#169 terminal-session registry-command routine and the USB S1 BOT/SCSI/
uevent layout routine — so the merged suite runs 124 routines.

Everything else auto-merged. USB S1 files (xhci/usb_msc/usb_scsi/usb_msc_proto/
usb_storage/usb_uevent*/vfs block nodes/mount syscalls) are untouched by
#169; #169's VT/session/shell routing changes are preserved verbatim.
Verified after merge: gates 6/6, QEMU usb-storage 46/46, release-kernel
selftest 124 routines / 0 failed / 1 skipped (NVMe absent).
Sync with dev=e7f2bdc3 (#169 VT/session routing, #173 tinygl PR5,
#174 vtfb1 respawn, docs).

Conflicts, both at appended/summary text:
- AGENTS.md: kept the USB S1 description for this branch (S1 is present
  here) and stated explicitly that it reaches dev only after this PR;
  #169/#173/#174 prose is kept verbatim.
- src/test.rs: kept BOTH the new #174
  "desktop-b::/dev/vtfb1 respawn after session exit (device path)" and the
  USB "usb::BOT/SCSI/uevent layouts and /usb root rule" routines.

All USB S1 files (xhci/usb_msc/usb_scsi/usb_msc_proto/usb_storage/
usb_uevent*/vfs block nodes/mount syscalls) are untouched by dev; the
dev-side terminal/tinygl/respawn changes are preserved.

Post-merge evidence: gates 6/6 (host 872/0), QEMU usb-storage acceptance
46/46, release-kernel selftest 125 routines / 0 failed / 1 skipped
(NVMe absent).
Sync with dev=c1e80933 (#147/#175/#176/#178: console grid from real
framebuffer, scrollback/selection, rubberband fullscreen, os-identity,
docs).

Conflict: src/test.rs only — both sides appended routines/modules at the
same two places. Resolution keeps BOTH:
- dev qbug2 "console::text grid matches the framebuffer resolution"
  (console_grid_tests) and the USB "usb::BOT/SCSI/uevent layouts and
  /usb root rule" (usb_storage_prop_tests);
- both modules are present and closed; all_tests registers 126 routines.

All USB S1 files are untouched by these dev changes, and the dev
framebuffer/console/scrollback/selection/os-identity work is preserved
verbatim. AGENTS.md auto-merged and keeps the USB S1 gap text.

Post-merge evidence: gates 6/6 (host 885/0), QEMU usb-storage acceptance
46/46, release-kernel selftest 126 routines / 0 failed / 1 skipped
(NVMe absent), in one flocked run.
Sync with dev=c1853ad0 (#177 i8042/ppkg responsive + frame-stats,
#174 vtfb1 respawn, terminal docs). Clean auto-merge: no textual or
semantic conflicts; the only shared files (drivers/mod.rs listing the new
i8042 driver, fs/ext2 with the volume-identity helper) merged without
overlap.

USB S1 files (xhci/usb_msc/usb_scsi/usb_msc_proto/usb_storage/
usb_uevent*/vfs block nodes/mount syscalls) are untouched by these dev
changes; the dev i8042/ppkg/frame-stats/respawn work is preserved.

Post-merge evidence: gates 6/6 (host 885/0), QEMU usb-storage acceptance
46/46, release-kernel selftest 126 routines / 0 failed / 1 skipped
(NVMe absent), one flocked run.
Second sync in this round: dev moved from c1853ad to 7b3e5a7 with
#172 terminal PTY job control on top of #177 i8042/ppkg, #174 respawn and
the terminal docs. This merge closes the real DIRTY state: dev changed
io_sys (per-session tty ioctl) and test.rs while both files also carry
USB S1 hooks.

Conflicts:
- src/arch/x86_64/linux/io_sys.rs: kept the USB
  `/dev/uevent` ioctl arm and merged the dev per-session tty path —
  final order is fbdev, vtconsole, vtfb1, uevent, vtinput1, then
  Console/Stdin -> tty_ioctl(session::current(), ...).
- src/test.rs: kept BOTH the dev per-session-tty selftest routine
  ("linux-tty::per-session foreground groups + winsize + SIGWINCH") and
  the USB "usb::BOT/SCSI/uevent layouts and /usb root rule" routine.

USB S1 files are otherwise untouched; all dev i8042/ppkg/frame-stats/
PTY-job-control/respawn changes are preserved.

Post-merge evidence: gates 6/6 (host 893/0), QEMU usb-storage acceptance
46/46, release-kernel selftest 127 routines / 0 failed / 1 skipped
(NVMe absent), one flocked run.
Sync with dev=f1453f55 (#167 multi-session terminal lifecycle on top of
#172 PTY job control): /dev/vtfbN, /dev/vtinputN and /dev/vtmouseN per
session instead of the single-session vtfb1/vtinput1 pair.

Conflicts:
- src/arch/x86_64/linux/io_sys.rs (4 stops): merged the dev per-session
  accessors with the USB hooks.
  * read: `/dev/uevent` record path stays before the dev
    `vtconsole || vtfb_session().is_some()` snapshot path.
  * close: dev `closing_session` (vtfb/vtinput/vtmouse) and USB
    `closing_uevent` both release their reader/session.
  * stat mode: dev session accessors plus `is_uevent()` stay S_IFCHR.
  * ioctl: fbdev -> vtconsole -> vtfb_session -> uevent -> vtinput_session
    (with the dev VTINPUT_SET_FOCUS/tty_ioctl split) -> Console/Stdin.
- src/vfs/mod.rs: dev terminal_devices loop (vtfb/vtinput/vtmouse per
  MAX_SESSIONS) is kept, and `/dev/block` + `/dev/uevent` are chained into
  the same DevDirectory children list.
- src/arch/x86_64/linux/errno.rs auto-merged with a duplicate `EBUSY=16`
  (USB needed it, #167 added its own); collapsed to one variant with both
  documented meanings.
- src/test.rs auto-merged; all three routines remain (console grid,
  linux-tty per-session, USB layout).

The dead `vtinput1_ioctl` wrapper superseded by the dev inline
session-aware branch is removed.

Post-merge evidence: gates 6/6 (host 903/0), QEMU usb-storage acceptance
46/46, release-kernel selftest 129 routines / 0 failed / 1 skipped
(NVMe absent), one flocked run.
@ccclosed
ccclosed merged commit 6de2a2c into dev Nov 22, 2026
8 checks passed
@ccclosed
ccclosed deleted the feat/usb-ms-s1 branch November 22, 2026 11:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant