Sfoglia il codice sorgente

fs: skip raw reads of a failed USB slot

The boot background probe can leave a mass-storage device that never
answers its media read.  The Disks app then issued SYS_BLK_READ on it
again and waited out another full timeout before the window appeared.

Mark the MSC slot failed when a read command finally fails, and return
EIO immediately from SYS_BLK_READ for such a BLK_USB slot.  A storage
rescan clears the mark and retries the device with the full recovery
path.
Johannes Zellner 1 giorno fa
parent
commit
8373a22461
5 ha cambiato i file con 29 aggiunte e 3 eliminazioni
  1. 3 1
      AGENTS.md
  2. 1 1
      docs/filesystems.html
  3. 7 0
      kernel/sys_fs.c
  4. 13 1
      kernel/usbmsc.c
  5. 5 0
      kernel/usbmsc.h

+ 3 - 1
AGENTS.md

@@ -809,7 +809,9 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   operations wait, while the USB HCD waits can sleep/yield, so a stuck USB
   disk cannot freeze the WM.  A `BLK_USB` raw read also sets
   `usbmsc_set_probe_only(1)` for a single 5 s attempt instead of the full
-  reset/retry chain.  Directory
+  reset/retry chain; once a slot has failed, `usbmsc_slot_failed()` makes
+  later raw reads return `EIO` immediately, and a full `storage` scan
+  clears the mark.  Directory
   listings cache the first
   64 entries of the last FAT directory (`fat_cache` in `vfs.c`), so the
   WebDAV server's index 0..N-1 sequence is one directory walk instead of N;

+ 1 - 1
docs/filesystems.html

@@ -166,7 +166,7 @@
 			<li><strong>Read/write.</strong> Read, write, delete, mkdir and rmdir are implemented; FAT32 allocation uses an FSInfo-seeded free-cluster hint.</li>
 			<li><strong>8.3 names.</strong> The on-disk name is the canonical one. The shell and <code>files</code> display the space-padded form; a long name cannot be created.</li>
 			<li><strong>Directory cache.</strong> The last FAT directory's first 64 entries are cached in <code>vfs.c</code>, so WebDAV's index sequence is one walk instead of N. Any write invalidates the cache.</li>
-			<li><strong>Serialization.</strong> A file operation runs in a non-preemptible section because the FAT driver has static state and the floppy needs IRQ 6; <code>vfs_scan_busy</code>/<code>format_busy</code> make it sleep first while a mount scan or a format owns the disks. A format releases the BKL for its raw-sector loop, and only disk-backed mounts wait for it, so <code>/ramfs</code> and <code>/share</code> stay usable.  A rescan (the <code>bootscan</code> thread or the <code>storage</code> command) re-adds <code>/ramfs</code>/<code>/share</code> first and is skipped by their operations too (<code>vfs_mount_is_ram</code>); it runs preemptible, and the floppy/ATA/AHCI/SDHCI wait loops (including the SDHCI command and CMD1 power-up polls) call <code>vfs_scan_poll</code>/<code>vfs_scan_relax</code>, which sleep (dropping the BKL) once a wait passes 2 ms and only for the scan's owning process.  A USB disk read by the rescan claims the USB bus (<code>usb_bus_claim()</code>), so its host-controller waits yield too; <code>storage</code> no longer freezes the WM.  A raw-sector read (<code>SYS_BLK_READ</code>, the Disks partition view) takes a scan-owned but preemptible section: disk file operations wait, while the USB waits yield and a USB read is best-effort (one 5&nbsp;s attempt), so an unresponsive stick cannot freeze the WM.  The floppy probe is skipped for a drive whose CMOS type (register <code>0x10</code>) is 0.</li>
+			<li><strong>Serialization.</strong> A file operation runs in a non-preemptible section because the FAT driver has static state and the floppy needs IRQ 6; <code>vfs_scan_busy</code>/<code>format_busy</code> make it sleep first while a mount scan or a format owns the disks. A format releases the BKL for its raw-sector loop, and only disk-backed mounts wait for it, so <code>/ramfs</code> and <code>/share</code> stay usable.  A rescan (the <code>bootscan</code> thread or the <code>storage</code> command) re-adds <code>/ramfs</code>/<code>/share</code> first and is skipped by their operations too (<code>vfs_mount_is_ram</code>); it runs preemptible, and the floppy/ATA/AHCI/SDHCI wait loops (including the SDHCI command and CMD1 power-up polls) call <code>vfs_scan_poll</code>/<code>vfs_scan_relax</code>, which sleep (dropping the BKL) once a wait passes 2 ms and only for the scan's owning process.  A USB disk read by the rescan claims the USB bus (<code>usb_bus_claim()</code>), so its host-controller waits yield too; <code>storage</code> no longer freezes the WM.  A raw-sector read (<code>SYS_BLK_READ</code>, the Disks partition view) takes a scan-owned but preemptible section: disk file operations wait, while the USB waits yield and a USB read is best-effort (one 5&nbsp;s attempt), so an unresponsive stick cannot freeze the WM. Once a USB slot has failed its probe, later raw reads return <code>EIO</code> immediately; a <code>storage</code> rescan clears the failure and retries.  The floppy probe is skipped for a drive whose CMOS type (register <code>0x10</code>) is 0.</li>
 			<li><strong>Mount scan.</strong> <code>mount_device()</code> recognises the NebOS MBR layout (<code>mbr_is_nebos_layout()</code>: active type-0x06 P1 at LBA 63 of 1024 sectors, P2 at LBA 1088) and skips P1 without reading it, so the boot stick costs only its MBR read. A valid MBR whose partitions are all non-FAT is not probed as a superfloppy, and an unreadable partition boot sector is skipped rather than mounted as FAT. The boot scan calls <code>vfs_mount_all(1)</code>, which logs USB devices as <code>deferred</code>; <code>vfs_mount_usb()</code> appends them after the bootscan window has closed, so a card reader that NAKs its first read for seconds cannot delay the other mounts or the desktop. The background pass is best-effort: one 5 s first read without the reset/retry chain (<code>usbmsc_set_probe_only</code>). The <code>storage</code> command's rescan still does the full scan (<code>vfs_mount_all(0)</code>) with the normal 15 s first-read deadline and retries.</li>
 		</ul>
 		<p>

+ 7 - 0
kernel/sys_fs.c

@@ -334,6 +334,13 @@ int sys_blk_read(unsigned int index, unsigned int lba, void *buf)
 		return SYS_ENODEV;
 	}
 	d = &blk_devs[index];
+	if (d->kind == BLK_USB && usbmsc_slot_failed(d->unit)) {
+		/* The boot probe (or an earlier raw read) already failed on
+		 * this device; do not time out on it again just to show the
+		 * partition view.  `storage` clears the mark and retries. */
+		__asm__ volatile ("cli");
+		return SYS_EIO;
+	}
 	/* A raw read does not touch the FAT driver's static buffers, and a USB
 	 * disk can make its HCD wait sleep.  Publish it like a scan (disk file
 	 * operations wait for it) but keep preemption enabled, so

+ 13 - 1
kernel/usbmsc.c

@@ -43,6 +43,7 @@ struct usb_msc {
 	unsigned int config;		/* configuration value, for recovery */
 	unsigned int timeout_ms;	/* bulk-IN deadline */
 	unsigned int first_read;	/* media not read since the probe */
+	int failed;			/* last media read failed */
 	unsigned int sectors;
 	unsigned int tag;
 	unsigned int last_status, last_residue;
@@ -65,8 +66,15 @@ void usbmsc_begin_full_scan(void)
 	unsigned int i;
 
 	for (i = 0; i < USB_MSC_MAX; i++)
-		if (msc[i].present)
+		if (msc[i].present) {
 			msc[i].first_read = 1;
+			msc[i].failed = 0;
+		}
+}
+
+int usbmsc_slot_failed(unsigned int unit)
+{
+	return unit < USB_MSC_MAX && msc[unit].present && msc[unit].failed;
 }
 
 static unsigned int rd32le(const unsigned char *p)
@@ -503,6 +511,7 @@ static int device_probe(unsigned int ctrl, unsigned int addr,
 	m->config = 0;
 	m->timeout_ms = MSC_TIMEOUT_MS;
 	m->first_read = 0;
+	m->failed = 0;
 
 	if (msc_config(m) != 0)
 		return 0;
@@ -629,6 +638,7 @@ static int msc_read_claimed(unsigned int unit, unsigned int lba, void *buf,
 							  (m->sense_asc << 8) |
 							  m->sense_ascq);
 						io_putchar('\n');
+						m->failed = 1;
 						return -1;
 					}
 			} else {
@@ -638,6 +648,7 @@ static int msc_read_claimed(unsigned int unit, unsigned int lba, void *buf,
 				io_puthex((m->sense_key << 16) |
 					  (m->sense_asc << 8) | m->sense_ascq);
 				io_putchar('\n');
+				m->failed = 1;
 				return -1;
 			}
 		}
@@ -645,6 +656,7 @@ static int msc_read_claimed(unsigned int unit, unsigned int lba, void *buf,
 		p += n * 512u;
 		count -= n;
 	}
+	m->failed = 0;
 	return 0;
 }
 

+ 5 - 0
kernel/usbmsc.h

@@ -25,6 +25,11 @@ void usbmsc_set_probe_only(int on);
  * deadline again (the boot background probe consumes it once). */
 void usbmsc_begin_full_scan(void);
 
+/* 1 when the last media read on this MSC slot failed.  The raw-sector view
+ * uses it to return immediately instead of timing out on a stuck device
+ * again; a full scan clears it. */
+int usbmsc_slot_failed(unsigned int unit);
+
 /* Block I/O for BLK_USB devices (unit = usbmsc slot index). */
 int usbmsc_read(unsigned int unit, unsigned int lba, void *buf,
 		unsigned int count);