Selaa lähdekoodia

Speed up WebDAV transfers and parallel connections

- send up to four TCP segments per cumulatively acknowledged burst (500 ms RTO)
- zero-window SYN-ACK with a 1 KB early-request buffer and 8 listener slots;
  open the window on accept and repeat the update when the final ACK arrives
- feed new SYNs/data to the listener from the active connection's poll loops
- batch PROPFIND XML into 8 KB chunks and keep the per-request close
- cache the first 64 entries of the last FAT directory
- enlarge the r8168 RX ring to 64 descriptors
Johannes Zellner 2 viikkoa sitten
vanhempi
sitoutus
db4098193e
6 muutettua tiedostoa jossa 398 lisäystä ja 55 poistoa
  1. 30 9
      AGENTS.md
  2. 1 1
      r8168.c
  3. 185 29
      tcp.c
  4. 2 0
      tcp.h
  5. 78 11
      ucmd_websrv.c
  6. 102 5
      vfs.c

+ 30 - 9
AGENTS.md

@@ -234,7 +234,10 @@ below the run area), so adding code no longer moves any address.
   operation sets `sys_fs_busy`, which the scheduler honours (no process switch
   operation sets `sys_fs_busy`, which the scheduler honours (no process switch
   during it): the FAT driver has static state and the floppy still needs its
   during it): the FAT driver has static state and the floppy still needs its
   IRQ.  So expect a brief system stall (not ticks — the PIT keeps running)
   IRQ.  So expect a brief system stall (not ticks — the PIT keeps running)
-  while a command reads or writes a disk.
+  while a command reads or writes a disk.  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;
+  any write/mkdir/rmdir/unlink invalidates it.
 - **`/share`**: `share.c/h` — writable, page-backed in-memory filesystem for
 - **`/share`**: `share.c/h` — writable, page-backed in-memory filesystem for
   network uploads (the netbooted board may have no writable disk).  Entries
   network uploads (the netbooted board may have no writable disk).  Entries
   are full relative paths (like the pack ramfs) plus explicit directory
   are full relative paths (like the pack ramfs) plus explicit directory
@@ -255,7 +258,8 @@ below the run area), so adding code no longer moves any address.
 - **Serial**: `serial.c`, `serial_ring_buf.c/h`, `bin_recv.c/h`, `serial_recv.c`,
 - **Serial**: `serial.c`, `serial_ring_buf.c/h`, `bin_recv.c/h`, `serial_recv.c`,
   `serial_loop.c`, `send_serial.c` — 16550 UART, IRQ-driven ring buffer, XMODEM.
   `serial_loop.c`, `send_serial.c` — 16550 UART, IRQ-driven ring buffer, XMODEM.
 - **Network**: `netdev.c/h` — NIC driver registry (`struct netdev`, first probe
 - **Network**: `netdev.c/h` — NIC driver registry (`struct netdev`, first probe
-  wins; drivers: `r8168.c` RTL8111/8168 (PCIe, polled, C+ mode) and `ne2000.c`
+  wins; drivers: `r8168.c` RTL8111/8168 (PCIe, polled, C+ mode, 64-entry RX
+  ring) and `ne2000.c`
   RTL8029 (QEMU/ISA)). `net.c` — ARP + ICMP echo, IPv4/UDP send + parse helpers,
   RTL8029 (QEMU/ISA)). `net.c` — ARP + ICMP echo, IPv4/UDP send + parse helpers,
   shared ARP resolution (`net_arp_resolve`) and routing (`net_next_hop`), frame
   shared ARP resolution (`net_arp_resolve`) and routing (`net_next_hop`), frame
   decode, `net_checksum()`/`net_checksum_tcp()`. `dhcp.c` — minimal DHCP client
   decode, `net_checksum()`/`net_checksum_tcp()`. `dhcp.c` — minimal DHCP client
@@ -408,8 +412,14 @@ below the run area), so adding code no longer moves any address.
   net users, so being preempted mid-wait is safe.
   net users, so being preempted mid-wait is safe.
 
 
   The server side is the WebDAV command: `tcp.c` has a small listener (one
   The server side is the WebDAV command: `tcp.c` has a small listener (one
-  active connection + a SYN backlog, SYN-ACK/RST, and a fast FIN that keeps
-  the tuple briefly so a late FIN/ACK is absorbed).  `SYS_TCP_LISTEN/ACCEPT/
+  active connection + 7 queued ones, SYN-ACK/RST, and a fast FIN that keeps
+  the tuple briefly so a late FIN/ACK is absorbed).  The SYN-ACK advertises a
+  zero window; a queued connection may hold 1 KB of early request (usually
+  the HTTP headers), `tcp_srv_accept()` opens the window, and the update is
+  repeated when the final ACK arrives, so parallel gvfs clients queue
+  without RTO stalls.  New SYNs/data are also handed to `tcp_srv_input()`
+  from the active connection's `tcp_recv`/`tcp_srv_send` poll loops, not just
+  between requests.  `SYS_TCP_LISTEN/ACCEPT/
   SRV_RECV/SRV_SEND/SRV_CLOSE` are called by `/ramfs/bin/websrv`, which serves
   SRV_RECV/SRV_SEND/SRV_CLOSE` are called by `/ramfs/bin/websrv`, which serves
   HTTP/1.1 + WebDAV from the VFS (`/` lists mounts, `/ramfs` is read-only,
   HTTP/1.1 + WebDAV from the VFS (`/` lists mounts, `/ramfs` is read-only,
   FAT mounts accept only names that round-trip to 8.3, `/share` is the
   FAT mounts accept only names that round-trip to 8.3, `/share` is the
@@ -421,6 +431,11 @@ below the run area), so adding code no longer moves any address.
   a window update whenever the application drains the queue (otherwise a full
   a window update whenever the application drains the queue (otherwise a full
   receive queue deadlocks until the peer's zero-window probe).  No auth: it is
   receive queue deadlocks until the peer's zero-window probe).  No auth: it is
   a trusted-LAN service, and `/share` is writable by anyone who can reach it.
   a trusted-LAN service, and `/share` is writable by anyone who can reach it.
+  Responses go out in up to four-segment bursts with a cumulative ACK
+  (`TCP_RETRY_MS` 500 ms) rather than one segment per ACK, and PROPFIND XML
+  is batched into 8 KB chunks instead of one chunk per entry.  Keep-alive is
+  deliberately not used: with one active connection an idle persistent
+  connection would block the parallel connections Nautilus opens.
 
 
 ## Shell commands
 ## Shell commands
 
 
@@ -852,11 +867,12 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
 17. `files` recognises images by file extension only; a magic-based fallback
 17. `files` recognises images by file extension only; a magic-based fallback
     would also open extensionless files in the viewer.
     would also open extensionless files in the viewer.
 
 
-18. WebDAV limits: one request at a time (a 2-deep SYN backlog only holds
-    handshakes), files are buffered whole (4 MB cap; a larger PUT gets 413),
-    directory `MOVE` is 501, `PROPPATCH` is answered 200 without persisting,
-    and there is no authentication.  TCP is stop-and-wait, so throughput is
-    roughly 0.5-4 MB/s depending on the link.
+18. WebDAV limits: one request at a time (one active connection plus 7
+    queued; the queue holds each connection's first 1 KB with a zero window),
+    files are buffered whole (4 MB cap; a larger PUT gets 413), directory
+    `MOVE` is 501, `PROPPATCH` is answered 200 without persisting, and there
+    is no authentication.  Sends use a four-segment window, and keep-alive is
+    off, so parallel gvfs clients serialize but do not wedge.
 19. FAT mounts are writable over WebDAV only for names that
 19. FAT mounts are writable over WebDAV only for names that
     `fat_name_to_8_3()` can represent exactly; anything longer is refused
     `fat_name_to_8_3()` can represent exactly; anything longer is refused
     with 409 instead of silently truncating (two long names could otherwise
     with 409 instead of silently truncating (two long names could otherwise
@@ -867,3 +883,8 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     command first.
     command first.
 21. `/share` is RAM only: a reboot/kexec starts empty, and the 16 MB cap is
 21. `/share` is RAM only: a reboot/kexec starts empty, and the 16 MB cap is
     charged against the 128 MB identity-mapped window.
     charged against the 128 MB identity-mapped window.
+
+22. The TCP backlog's early-request buffers cost 8 KB of kernel BSS
+    (8 slots x 1 KB), and the FAT listing cache another 5 KB; `_end` is
+    within roughly 20 KB of the `0x7F000` wall.  Adding more kernel buffers
+    or commands should be paired with reclaiming space elsewhere.

+ 1 - 1
r8168.c

@@ -107,7 +107,7 @@
 
 
 /* Rings. */
 /* Rings. */
 #define R8168_NTX        16
 #define R8168_NTX        16
-#define R8168_NRX        16
+#define R8168_NRX        64
 #define R8168_BUF_SIZE   2048
 #define R8168_BUF_SIZE   2048
 #define R8168_RX_ARM     (R8168_BUF_SIZE - 1)
 #define R8168_RX_ARM     (R8168_BUF_SIZE - 1)
 
 

+ 185 - 29
tcp.c

@@ -15,9 +15,13 @@
 
 
 #define TCP_MSS_OURS  1460
 #define TCP_MSS_OURS  1460
 #define TCP_WINDOW    4096
 #define TCP_WINDOW    4096
-#define TCP_RETRY_MS  1000
+#define TCP_RETRY_MS  500
 #define TCP_RETRIES   3
 #define TCP_RETRIES   3
 
 
+/* Listening port for the server side (0 = not listening).  Declared here
+ * because the shared receive loop feeds non-matching frames to the listener. */
+static unsigned int srv_port;
+
 /* Frame buffers are file scope: a 4 KB process stack cannot hold them.  The
 /* Frame buffers are file scope: a 4 KB process stack cannot hold them.  The
  * receive side uses the shared frame scratch (netdev_rx_scratch()). */
  * receive side uses the shared frame scratch (netdev_rx_scratch()). */
 static unsigned char tx_frame[14 + 20 + 20 + TCP_MSS_OURS];
 static unsigned char tx_frame[14 + 20 + 20 + TCP_MSS_OURS];
@@ -84,10 +88,10 @@ static void wr32(unsigned char *p, unsigned int v)
 
 
 /* Build and transmit one segment.  `seq` is the sequence of the first byte
 /* Build and transmit one segment.  `seq` is the sequence of the first byte
  * (for SYN/FIN that is the flag's own sequence).  Returns 0 / -1 (tx). */
  * (for SYN/FIN that is the flag's own sequence).  Returns 0 / -1 (tx). */
-static int tcp_send_segment(struct tcp_conn *c, unsigned char flags,
-			    unsigned int seq, unsigned int ack,
-			    const unsigned char *payload, unsigned int len,
-			    int with_mss)
+static int tcp_send_segment_wnd(struct tcp_conn *c, unsigned char flags,
+				unsigned int seq, unsigned int ack,
+				const unsigned char *payload, unsigned int len,
+				int with_mss, unsigned int wnd)
 {
 {
 	const struct netdev *nd = netdev_active();
 	const struct netdev *nd = netdev_active();
 	unsigned char my_mac[6], my_ip[4];
 	unsigned char my_mac[6], my_ip[4];
@@ -137,10 +141,7 @@ static int tcp_send_segment(struct tcp_conn *c, unsigned char flags,
 	wr32(tcp + 8, ack);
 	wr32(tcp + 8, ack);
 	tcp[12] = (unsigned char)((5 + optlen / 4) << 4);
 	tcp[12] = (unsigned char)((5 + optlen / 4) << 4);
 	tcp[13] = flags;
 	tcp[13] = flags;
-	/* Advertise the space actually left in the receive queue (the SYN
-	 * uses the conservative constant, before the queue is reset). */
-	wr16(tcp + 14, with_mss ? TCP_WINDOW :
-	     (rxq_space() > 0xFFFFu ? 0xFFFFu : rxq_space()));
+	wr16(tcp + 14, wnd > 0xFFFFu ? 0xFFFFu : wnd);
 	wr16(tcp + 16, 0);                /* checksum */
 	wr16(tcp + 16, 0);                /* checksum */
 	wr16(tcp + 18, 0);                /* urgent pointer */
 	wr16(tcp + 18, 0);                /* urgent pointer */
 	for (i = 0; i < len; i++)
 	for (i = 0; i < len; i++)
@@ -152,6 +153,17 @@ static int tcp_send_segment(struct tcp_conn *c, unsigned char flags,
 	return nd->tx(tx_frame, 14 + total);
 	return nd->tx(tx_frame, 14 + total);
 }
 }
 
 
+static int tcp_send_segment(struct tcp_conn *c, unsigned char flags,
+			    unsigned int seq, unsigned int ack,
+			    const unsigned char *payload, unsigned int len,
+			    int with_mss)
+{
+	unsigned int wnd = with_mss ? TCP_WINDOW : rxq_space();
+
+	return tcp_send_segment_wnd(c, flags, seq, ack, payload, len,
+				    with_mss, wnd);
+}
+
 /* One received segment. */
 /* One received segment. */
 struct tcp_seg {
 struct tcp_seg {
 	unsigned int  seq, ack;
 	unsigned int  seq, ack;
@@ -250,6 +262,8 @@ static int tcp_rx_data(struct tcp_conn *c, const struct tcp_seg *s)
 {
 {
 	if (s->flags & TCP_RST)
 	if (s->flags & TCP_RST)
 		return -2;
 		return -2;
+	if (s->tcp)
+		c->peer_wnd = rd16(s->tcp + 14);
 
 
 	if (s->len) {
 	if (s->len) {
 		if (s->seq == c->rcv_nxt && s->len <= rxq_space()) {
 		if (s->seq == c->rcv_nxt && s->len <= rxq_space()) {
@@ -291,7 +305,9 @@ int tcp_connect(const unsigned char *dst_mac, const unsigned char *dst_ip,
 	c->snd_una = c->snd_nxt = (get_ticks_ms() << 16) ^ 0x5EED0000u;
 	c->snd_una = c->snd_nxt = (get_ticks_ms() << 16) ^ 0x5EED0000u;
 	c->rcv_nxt = 0;
 	c->rcv_nxt = 0;
 	c->peer_mss = 536;
 	c->peer_mss = 536;
+	c->peer_wnd = 4096;
 	c->peer_fin = 0;
 	c->peer_fin = 0;
+	c->need_wnd = 0;
 	rxq_head = rxq_tail = 0;
 	rxq_head = rxq_tail = 0;
 
 
 	for (tries = 0; tries < TCP_RETRIES; tries++) {
 	for (tries = 0; tries < TCP_RETRIES; tries++) {
@@ -318,6 +334,7 @@ int tcp_connect(const unsigned char *dst_mac, const unsigned char *dst_ip,
 				c->rcv_nxt = s.seq + 1;
 				c->rcv_nxt = s.seq + 1;
 				c->snd_nxt++;
 				c->snd_nxt++;
 				c->snd_una = c->snd_nxt;
 				c->snd_una = c->snd_nxt;
+				c->peer_wnd = rd16(s.tcp + 14);
 				if (tcp_parse_mss(s.tcp, s.doff, &mss) &&
 				if (tcp_parse_mss(s.tcp, s.doff, &mss) &&
 				    mss >= 64 && mss <= TCP_MSS_OURS)
 				    mss >= 64 && mss <= TCP_MSS_OURS)
 					c->peer_mss = mss;
 					c->peer_mss = mss;
@@ -410,8 +427,13 @@ int tcp_recv(struct tcp_conn *c, unsigned char *out, unsigned int maxlen,
 			if (len < 0)
 			if (len < 0)
 				continue;
 				continue;
 			if (!tcp_parse(rxbuf, (unsigned int)len, c->dst_ip,
 			if (!tcp_parse(rxbuf, (unsigned int)len, c->dst_ip,
-				       c->dport, c->sport, &s))
+				       c->dport, c->sport, &s)) {
+				/* A new connection (or its first data) while
+				 * this one is being served. */
+				if (srv_port)
+					tcp_srv_input(rxbuf, (unsigned int)len);
 				continue;
 				continue;
+			}
 		}
 		}
 
 
 		/* Track our send window (nothing pending here, but keeps
 		/* Track our send window (nothing pending here, but keeps
@@ -419,6 +441,11 @@ int tcp_recv(struct tcp_conn *c, unsigned char *out, unsigned int maxlen,
 		if ((unsigned)(s.ack - c->snd_una) <=
 		if ((unsigned)(s.ack - c->snd_una) <=
 		    (unsigned)(c->snd_nxt - c->snd_una))
 		    (unsigned)(c->snd_nxt - c->snd_una))
 			c->snd_una = s.ack;
 			c->snd_una = s.ack;
+		if (c->need_wnd && (s.flags & TCP_ACK)) {
+			tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
+					 0, 0, 0);
+			c->need_wnd = 0;
+		}
 
 
 		if (tcp_rx_data(c, &s) == -2)
 		if (tcp_rx_data(c, &s) == -2)
 			return -2;
 			return -2;
@@ -476,7 +503,7 @@ void tcp_close(struct tcp_conn *c)
  * is absorbed instead of answered with RST (libsoup treats a reset after a
  * is absorbed instead of answered with RST (libsoup treats a reset after a
  * complete response as an error). */
  * complete response as an error). */
 
 
-#define TCP_SRV_SLOTS      3          /* 1 active + 2 backlog/closing */
+#define TCP_SRV_SLOTS      8          /* 1 active + 7 backlog/closing */
 #define TCP_SRV_CLOSE_MS   2000
 #define TCP_SRV_CLOSE_MS   2000
 #define TCP_SRV_BACKLOG_MS 30000
 #define TCP_SRV_BACKLOG_MS 30000
 
 
@@ -487,15 +514,18 @@ enum {
 	SRV_CLOSING
 	SRV_CLOSING
 };
 };
 
 
+#define SRV_PRE_MAX 1024
+
 struct tcp_srv {
 struct tcp_srv {
 	struct tcp_conn c;
 	struct tcp_conn c;
 	int used;
 	int used;
 	int state;
 	int state;
 	unsigned int last_ms;
 	unsigned int last_ms;
+	unsigned char pre[SRV_PRE_MAX];   /* first request, held until accept */
+	unsigned int pre_len;
 };
 };
 
 
 static struct tcp_srv srv[TCP_SRV_SLOTS];
 static struct tcp_srv srv[TCP_SRV_SLOTS];
-static unsigned int srv_port;
 static int srv_active = -1;
 static int srv_active = -1;
 
 
 struct tcp_rx {
 struct tcp_rx {
@@ -576,6 +606,7 @@ static void srv_clear(int i)
 {
 {
 	srv[i].used = 0;
 	srv[i].used = 0;
 	srv[i].state = SRV_FREE;
 	srv[i].state = SRV_FREE;
+	srv[i].pre_len = 0;
 }
 }
 
 
 static void srv_expire(void)
 static void srv_expire(void)
@@ -605,8 +636,8 @@ static unsigned int srv_isn(void)
 
 
 static void srv_resend_synack(struct tcp_srv *e)
 static void srv_resend_synack(struct tcp_srv *e)
 {
 {
-	tcp_send_segment(&e->c, TCP_SYN | TCP_ACK, e->c.snd_una - 1,
-			 e->c.rcv_nxt, 0, 0, 1);
+	tcp_send_segment_wnd(&e->c, TCP_SYN | TCP_ACK, e->c.snd_una - 1,
+			     e->c.rcv_nxt, 0, 0, 1, 0);
 }
 }
 
 
 /* Build a connection from a SYN and answer SYN-ACK. */
 /* Build a connection from a SYN and answer SYN-ACK. */
@@ -649,16 +680,21 @@ static int srv_new(const struct tcp_rx *s)
 	e->c.snd_una = e->c.snd_nxt = srv_isn();
 	e->c.snd_una = e->c.snd_nxt = srv_isn();
 	e->c.rcv_nxt = s->seq + 1;
 	e->c.rcv_nxt = s->seq + 1;
 	e->c.peer_fin = 0;
 	e->c.peer_fin = 0;
+	e->c.need_wnd = 0;
 	e->c.peer_mss = 536;
 	e->c.peer_mss = 536;
+	e->c.peer_wnd = rd16(s->tcp + 14);
+	if (!e->c.peer_wnd)
+		e->c.peer_wnd = 4096;
 	if (tcp_parse_mss(s->tcp, s->doff, &mss) &&
 	if (tcp_parse_mss(s->tcp, s->doff, &mss) &&
 	    mss >= 64 && mss <= TCP_MSS_OURS)
 	    mss >= 64 && mss <= TCP_MSS_OURS)
 		e->c.peer_mss = mss;
 		e->c.peer_mss = mss;
 	e->used = 1;
 	e->used = 1;
 	e->state = SRV_BACKLOG;
 	e->state = SRV_BACKLOG;
 	e->last_ms = get_ticks_ms();
 	e->last_ms = get_ticks_ms();
+	e->pre_len = 0;
 
 
-	tcp_send_segment(&e->c, TCP_SYN | TCP_ACK, e->c.snd_nxt, e->c.rcv_nxt,
-			 0, 0, 1);
+	tcp_send_segment_wnd(&e->c, TCP_SYN | TCP_ACK, e->c.snd_nxt,
+			     e->c.rcv_nxt, 0, 0, 1, 0);
 	/* The SYN consumed one sequence number. */
 	/* The SYN consumed one sequence number. */
 	e->c.snd_nxt++;
 	e->c.snd_nxt++;
 	e->c.snd_una = e->c.snd_nxt;
 	e->c.snd_una = e->c.snd_nxt;
@@ -731,6 +767,11 @@ int tcp_srv_input(const unsigned char *f, unsigned int len)
 	if (srv_active >= 0 && srv_match(&srv[srv_active].c, &s)) {
 	if (srv_active >= 0 && srv_match(&srv[srv_active].c, &s)) {
 		struct tcp_conn *c = &srv[srv_active].c;
 		struct tcp_conn *c = &srv[srv_active].c;
 
 
+		if (c->need_wnd && (s.flags & TCP_ACK)) {
+			tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
+					 0, 0, 0);
+			c->need_wnd = 0;
+		}
 		if (s.flags & TCP_RST) {
 		if (s.flags & TCP_RST) {
 			srv_clear(srv_active);
 			srv_clear(srv_active);
 			srv_active = -1;
 			srv_active = -1;
@@ -767,10 +808,34 @@ int tcp_srv_input(const unsigned char *f, unsigned int len)
 			srv_resend_synack(&srv[i]);
 			srv_resend_synack(&srv[i]);
 			return 1;
 			return 1;
 		}
 		}
-		/* Early data on a not-yet-accepted connection.  Do not ACK
-		 * it: the client retransmits after tcp_srv_accept(). */
-		if (s.flags & TCP_ACK)
-			srv[i].last_ms = get_ticks_ms();
+		if ((unsigned)(s.ack - srv[i].c.snd_una) <=
+		    (unsigned)(srv[i].c.snd_nxt - srv[i].c.snd_una))
+			srv[i].c.snd_una = s.ack;
+
+		/* Hold the first request (small HTTP headers) until the
+		 * command accepts this connection.  ACK it so the client does
+		 * not have to wait for an RTO; if it does not fit, leave it
+		 * unacknowledged and the client retransmits after accept. */
+		if (s.len && s.seq == srv[i].c.rcv_nxt &&
+		    s.len <= SRV_PRE_MAX - srv[i].pre_len) {
+			unsigned int k;
+
+			for (k = 0; k < s.len; k++)
+				srv[i].pre[srv[i].pre_len + k] = s.data[k];
+			srv[i].pre_len += s.len;
+			srv[i].c.rcv_nxt += s.len;
+			if ((s.flags & TCP_FIN) &&
+			    s.seq + s.len == srv[i].c.rcv_nxt) {
+				srv[i].c.peer_fin = 1;
+				srv[i].c.rcv_nxt++;
+			}
+			/* Keep the queued connection's window closed so it
+			 * does not send a body we have nowhere to put. */
+			tcp_send_segment_wnd(&srv[i].c, TCP_ACK,
+					     srv[i].c.snd_nxt, srv[i].c.rcv_nxt,
+					     0, 0, 0, 0);
+		}
+		srv[i].last_ms = get_ticks_ms();
 		return 1;
 		return 1;
 	}
 	}
 
 
@@ -805,26 +870,117 @@ struct tcp_conn *tcp_srv_accept(void)
 	srv[best].state = SRV_ACTIVE;
 	srv[best].state = SRV_ACTIVE;
 	srv_active = best;
 	srv_active = best;
 	rxq_head = rxq_tail = 0;
 	rxq_head = rxq_tail = 0;
+	if (srv[best].pre_len) {
+		rxq_put(srv[best].pre, srv[best].pre_len);
+		srv[best].pre_len = 0;
+	}
+	/* The SYN-ACK advertised a zero window while this connection waited
+	 * for the command; open it now with the real queue space. */
+	tcp_send_segment(&srv[best].c, TCP_ACK, srv[best].c.snd_nxt,
+			 srv[best].c.rcv_nxt, 0, 0, 0);
+	/* If this update raced ahead of the client's final ACK it may have
+	 * been discarded; repeat it when that ACK reaches us. */
+	srv[best].c.need_wnd = 1;
 	return &srv[best].c;
 	return &srv[best].c;
 }
 }
 
 
+/* Send up to four segments before waiting for the cumulative ACK.  Linux
+ * delays the ACK of a lone small segment by up to 40 ms; a burst makes the
+ * peer acknowledge every second segment immediately, which is the difference
+ * between ~35 KB/s and a few MB/s on a LAN.  The peer's advertised window
+ * caps the burst. */
 int tcp_srv_send(struct tcp_conn *c, const void *data, unsigned int len)
 int tcp_srv_send(struct tcp_conn *c, const void *data, unsigned int len)
 {
 {
+	const struct netdev *nd = netdev_active();
+	unsigned char *rxbuf = netdev_rx_scratch();
 	const unsigned char *p = data;
 	const unsigned char *p = data;
+	unsigned int mss;
 
 
-	if (!c)
+	if (!c || !nd)
 		return -3;
 		return -3;
-	if (c->peer_mss < 64)
-		c->peer_mss = 64;
+	mss = c->peer_mss;
+	if (mss < 64)
+		mss = 64;
 
 
 	while (len) {
 	while (len) {
-		unsigned int n = len > c->peer_mss ? c->peer_mss : len;
-		int r = tcp_send(c, p, n);
+		struct {
+			unsigned int seq;
+			unsigned int len;
+			const unsigned char *data;
+		} seg[4];
+		unsigned int nseg = 0, off = 0, total = 0, cap, wnd;
+		unsigned int tries, i;
+
+		wnd = c->peer_wnd ? c->peer_wnd : 4096;
+		cap = mss * 4;
+		if (cap > wnd)
+			cap = wnd;
+		if (cap < mss)
+			cap = mss;
+
+		while (off < len && nseg < 4 && total < cap) {
+			unsigned int n = len - off;
+
+			if (n > mss)
+				n = mss;
+			if (total + n > cap)
+				n = cap - total;
+			if (!n)
+				break;
+			seg[nseg].seq = c->snd_nxt;
+			seg[nseg].len = n;
+			seg[nseg].data = p + off;
+			if (tcp_send_segment(c, TCP_PSH | TCP_ACK, c->snd_nxt,
+					     c->rcv_nxt, p + off, n, 0) != 0)
+				return -3;
+			c->snd_nxt += n;
+			off += n;
+			total += n;
+			nseg++;
+		}
 
 
-		if (r != 0)
-			return r;
-		p += n;
-		len -= n;
+		for (tries = 0; ; tries++) {
+			unsigned int deadline = get_ticks_ms() + TCP_RETRY_MS;
+
+			while ((int)(get_ticks_ms() - deadline) < 0) {
+				struct tcp_seg s;
+				int rlen = nd->rx(rxbuf, NETDEV_FRAME_MAX);
+
+				if (rlen < 0)
+					continue;
+				if (!tcp_parse(rxbuf, (unsigned int)rlen,
+					       c->dst_ip, c->dport, c->sport, &s)) {
+					if (srv_port)
+						tcp_srv_input(rxbuf,
+							      (unsigned int)rlen);
+					continue;
+				}
+				if (s.flags & TCP_RST)
+					return -2;
+				if ((unsigned)(s.ack - c->snd_una) <=
+				    (unsigned)(c->snd_nxt - c->snd_una))
+					c->snd_una = s.ack;
+				c->peer_wnd = rd16(s.tcp + 14);
+				if ((int)(s.ack - c->snd_nxt) >= 0)
+					goto burst_done;
+				tcp_rx_data(c, &s);
+			}
+			if (tries >= TCP_RETRIES)
+				return -1;
+			/* Retransmit the still-unacknowledged part of this
+			 * burst (duplicates are harmless). */
+			for (i = 0; i < nseg; i++) {
+				if ((int)(seg[i].seq + seg[i].len -
+					  c->snd_una) <= 0)
+					continue;
+				tcp_send_segment(c, TCP_PSH | TCP_ACK,
+						 seg[i].seq, c->rcv_nxt,
+						 seg[i].data, seg[i].len, 0);
+			}
+		}
+burst_done:
+		p += off;
+		len -= off;
 	}
 	}
 	return 0;
 	return 0;
 }
 }

+ 2 - 0
tcp.h

@@ -15,7 +15,9 @@ struct tcp_conn {
 	unsigned int  snd_nxt;    /* next sequence number to send */
 	unsigned int  snd_nxt;    /* next sequence number to send */
 	unsigned int  rcv_nxt;    /* next sequence number expected from peer */
 	unsigned int  rcv_nxt;    /* next sequence number expected from peer */
 	unsigned int  peer_mss;   /* from the SYN-ACK option; 536 when absent */
 	unsigned int  peer_mss;   /* from the SYN-ACK option; 536 when absent */
+	unsigned int  peer_wnd;   /* peer's advertised receive window (raw) */
 	int           peer_fin;   /* peer FIN consumed (EOF) */
 	int           peer_fin;   /* peer FIN consumed (EOF) */
+	int           need_wnd;   /* re-send the window update after the final ACK */
 };
 };
 
 
 /* Open a connection (SYN -> SYN-ACK -> ACK).  Returns:
 /* Open a connection (SYN -> SYN-ACK -> ACK).  Returns:

+ 78 - 11
ucmd_websrv.c

@@ -23,7 +23,7 @@
 #define BODY_FALLBACK (1024u * 1024u)
 #define BODY_FALLBACK (1024u * 1024u)
 #define PATH_MAX      192u
 #define PATH_MAX      192u
 #define XML_MAX       3072u
 #define XML_MAX       3072u
-#define CHUNK_MAX     3072u
+#define CHUNK_MAX     8224u
 #define SOCK_MS       1000u
 #define SOCK_MS       1000u
 #define HDR_TIMEOUT_MS 10000u
 #define HDR_TIMEOUT_MS 10000u
 #define REQ_TIMEOUT_MS 60000u
 #define REQ_TIMEOUT_MS 60000u
@@ -39,6 +39,8 @@ static unsigned int resp_len;
 static char xml[XML_MAX];
 static char xml[XML_MAX];
 static unsigned int xml_len;
 static unsigned int xml_len;
 static char cbuf[CHUNK_MAX];
 static char cbuf[CHUNK_MAX];
+static char batch[8192];
+static unsigned int batch_len;
 static struct sys_mount mounts[MAX_MOUNTS];
 static struct sys_mount mounts[MAX_MOUNTS];
 static unsigned int mount_count;
 static unsigned int mount_count;
 static unsigned int req_start;
 static unsigned int req_start;
@@ -58,6 +60,7 @@ struct req {
 	int  expect_continue;
 	int  expect_continue;
 	int  overwrite;
 	int  overwrite;
 	int  have_dest;
 	int  have_dest;
+	int  keep_alive;
 };
 };
 
 
 static struct req rq;
 static struct req rq;
@@ -220,7 +223,9 @@ static int send_headers(int code, const char *reason, const char *ctype,
 	ra(" ");
 	ra(" ");
 	ra(reason);
 	ra(reason);
 	ra("\r\nServer: Nebs OS/0.2.0\r\n");
 	ra("\r\nServer: Nebs OS/0.2.0\r\n");
-	ra("Connection: close\r\nCache-Control: no-store\r\n");
+	ra((rq.keep_alive && code < 400) ? "Connection: keep-alive\r\n"
+					 : "Connection: close\r\n");
+	ra("Cache-Control: no-store\r\n");
 	if (extra_hdrs) {
 	if (extra_hdrs) {
 		ra(extra_hdrs);
 		ra(extra_hdrs);
 		ra("\r\n");
 		ra("\r\n");
@@ -316,6 +321,16 @@ static const char multistatus_open[] =
 	"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
 	"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
 	"<D:multistatus xmlns:D=\"DAV:\">";
 	"<D:multistatus xmlns:D=\"DAV:\">";
 
 
+static int flush_batch(void)
+{
+	if (!batch_len)
+		return 0;
+	if (chunk_send(batch, batch_len) != 0)
+		return -1;
+	batch_len = 0;
+	return 0;
+}
+
 static int send_multistatus_begin(void)
 static int send_multistatus_begin(void)
 {
 {
 	resp_len = 0;
 	resp_len = 0;
@@ -323,18 +338,30 @@ static int send_multistatus_begin(void)
 	ra("Server: Nebs OS/0.2.0\r\n");
 	ra("Server: Nebs OS/0.2.0\r\n");
 	ra("Content-Type: text/xml; charset=\"utf-8\"\r\n");
 	ra("Content-Type: text/xml; charset=\"utf-8\"\r\n");
 	ra("Transfer-Encoding: chunked\r\n");
 	ra("Transfer-Encoding: chunked\r\n");
-	ra("Connection: close\r\nCache-Control: no-store\r\n\r\n");
+	ra(rq.keep_alive ? "Connection: keep-alive\r\n"
+			 : "Connection: close\r\n");
+	ra("Cache-Control: no-store\r\n\r\n");
 	status = 207;
 	status = 207;
 	if (u_tcp_srv_send(resp, resp_len) != 0)
 	if (u_tcp_srv_send(resp, resp_len) != 0)
 		return -1;
 		return -1;
-	return chunk_send(multistatus_open, s_len(multistatus_open));
+	batch_len = 0;
+	if (s_len(multistatus_open) > sizeof(batch))
+		return -1;
+	mem_copy(batch, multistatus_open, s_len(multistatus_open));
+	batch_len = s_len(multistatus_open);
+	return 0;
 }
 }
 
 
 static int end_multistatus(void)
 static int end_multistatus(void)
 {
 {
 	static const char close_tag[] = "</D:multistatus>";
 	static const char close_tag[] = "</D:multistatus>";
+	unsigned int n = s_len(close_tag);
 
 
-	if (chunk_send(close_tag, s_len(close_tag)) != 0)
+	if (batch_len + n > sizeof(batch) && flush_batch() != 0)
+		return -1;
+	mem_copy(batch + batch_len, close_tag, n);
+	batch_len += n;
+	if (flush_batch() != 0)
 		return -1;
 		return -1;
 	return chunk_send("", 0);
 	return chunk_send("", 0);
 }
 }
@@ -602,6 +629,7 @@ static int parse_request(void)
 		return 414;
 		return 414;
 	s_copy(rq.target, sp1, sizeof(rq.target));
 	s_copy(rq.target, sp1, sizeof(rq.target));
 	rq.http11 = s_eq(sp2, "HTTP/1.1");
 	rq.http11 = s_eq(sp2, "HTTP/1.1");
+	rq.keep_alive = rq.http11 ? 1 : 0;
 
 
 	if (s_eq(sp1, "*")) {
 	if (s_eq(sp1, "*")) {
 		rq.path[0] = '\0';
 		rq.path[0] = '\0';
@@ -669,6 +697,11 @@ static int parse_request(void)
 		} else if (s_ieq(line, "Expect")) {
 		} else if (s_ieq(line, "Expect")) {
 			if (s_contains_ci(value, "100-continue"))
 			if (s_contains_ci(value, "100-continue"))
 				rq.expect_continue = 1;
 				rq.expect_continue = 1;
+		} else if (s_ieq(line, "Connection")) {
+			if (s_contains_ci(value, "close"))
+				rq.keep_alive = 0;
+			else if (s_contains_ci(value, "keep-alive"))
+				rq.keep_alive = 1;
 		}
 		}
 		line = end;
 		line = end;
 	}
 	}
@@ -693,6 +726,7 @@ static int read_request(void)
 	rq.expect_continue = 0;
 	rq.expect_continue = 0;
 	rq.overwrite = 1;
 	rq.overwrite = 1;
 	rq.have_dest = 0;
 	rq.have_dest = 0;
+	rq.keep_alive = 0;
 
 
 	for (;;) {
 	for (;;) {
 		int end = find_hdr_end();
 		int end = find_hdr_end();
@@ -705,6 +739,10 @@ static int read_request(void)
 				if (r != 0)
 				if (r != 0)
 					return r;
 					return r;
 			}
 			}
+			/* One active TCP connection: gvfs/Nautilus open
+			 * parallel ones, and an idle keep-alive connection
+			 * would block their SYNs.  Close after each response. */
+			rq.keep_alive = 0;
 			rb_init((unsigned int)end + 4);
 			rb_init((unsigned int)end + 4);
 			return 0;
 			return 0;
 		}
 		}
@@ -1093,7 +1131,13 @@ static int emit_prop(const char *p, int is_dir, unsigned int size,
 	xa("<D:getlastmodified>Thu, 01 Jan 2026 00:00:00 GMT</D:getlastmodified>");
 	xa("<D:getlastmodified>Thu, 01 Jan 2026 00:00:00 GMT</D:getlastmodified>");
 	xa("</D:prop><D:status>HTTP/1.1 200 OK</D:status></D:propstat>");
 	xa("</D:prop><D:status>HTTP/1.1 200 OK</D:status></D:propstat>");
 	xa("</D:response>");
 	xa("</D:response>");
-	return chunk_send(xml, xml_len);
+	if (xml_len > sizeof(batch))
+		return -1;
+	if (batch_len + xml_len > sizeof(batch) && flush_batch() != 0)
+		return -1;
+	mem_copy(batch + batch_len, xml, xml_len);
+	batch_len += xml_len;
+	return 0;
 }
 }
 
 
 static int list_child(unsigned int index, const char *dir, char *out,
 static int list_child(unsigned int index, const char *dir, char *out,
@@ -1127,7 +1171,9 @@ static void do_options2(void)
 	ra("DAV: 1, 2\r\nMS-Author-Via: DAV\r\n");
 	ra("DAV: 1, 2\r\nMS-Author-Via: DAV\r\n");
 	ra("Allow: OPTIONS, GET, HEAD, PUT, DELETE, MKCOL, PROPFIND, "
 	ra("Allow: OPTIONS, GET, HEAD, PUT, DELETE, MKCOL, PROPFIND, "
 	   "PROPPATCH, MOVE, LOCK, UNLOCK\r\n");
 	   "PROPPATCH, MOVE, LOCK, UNLOCK\r\n");
-	ra("Connection: close\r\nCache-Control: no-store\r\n");
+	ra(rq.keep_alive ? "Connection: keep-alive\r\n"
+			 : "Connection: close\r\n");
+	ra("Cache-Control: no-store\r\n");
 	ra("Content-Length: 0\r\n\r\n");
 	ra("Content-Length: 0\r\n\r\n");
 	status = 200;
 	status = 200;
 	u_tcp_srv_send(resp, resp_len);
 	u_tcp_srv_send(resp, resp_len);
@@ -1503,24 +1549,36 @@ static void do_lock(void)
 
 
 /* ---- request dispatch ---- */
 /* ---- request dispatch ---- */
 
 
-static void serve(void)
+/* Serve one request.  Returns 1 when the connection should stay open for the
+ * next request (HTTP keep-alive), 0 to close. */
+static int serve(void)
 {
 {
 	int r = read_request();
 	int r = read_request();
 
 
 	status = 0;
 	status = 0;
 	extra_hdrs = 0;
 	extra_hdrs = 0;
 	if (r == -2)
 	if (r == -2)
-		return;
+		return 0;
 	if (r != 0) {
 	if (r != 0) {
 		send_error(r);
 		send_error(r);
 		u_puts("websrv: bad request (");
 		u_puts("websrv: bad request (");
 		u_putdec((unsigned int)r);
 		u_putdec((unsigned int)r);
 		u_puts(")\n");
 		u_puts(")\n");
-		return;
+		return 0;
 	}
 	}
 
 
 	load_mounts();
 	load_mounts();
 
 
+	/* Methods with no body of their own: consume one if a client sent it,
+	 * otherwise it would be parsed as the next request. */
+	if ((s_eq(rq.method, "GET") || s_eq(rq.method, "HEAD") ||
+	     s_eq(rq.method, "OPTIONS") || s_eq(rq.method, "DELETE") ||
+	     s_eq(rq.method, "MKCOL") || s_eq(rq.method, "MOVE")) &&
+	    (rq.have_len || rq.chunked)) {
+		if (drain_body() != 0)
+			return 0;
+	}
+
 	if (s_eq(rq.method, "OPTIONS"))
 	if (s_eq(rq.method, "OPTIONS"))
 		do_options2();
 		do_options2();
 	else if (s_eq(rq.method, "GET"))
 	else if (s_eq(rq.method, "GET"))
@@ -1558,6 +1616,14 @@ static void serve(void)
 		u_putdec((unsigned int)status);
 		u_putdec((unsigned int)status);
 		u_putc('\n');
 		u_putc('\n');
 	}
 	}
+
+	if (!status || status >= 400)
+		return 0;
+	/* Unconsumed bytes (HTTP pipelining, or a body we did not read) would
+	 * be lost on the next read: close rather than desynchronise. */
+	if (rlen > rpos)
+		return 0;
+	return rq.keep_alive ? 1 : 0;
 }
 }
 
 
 static unsigned int parse_port(const char *arg)
 static unsigned int parse_port(const char *arg)
@@ -1638,7 +1704,8 @@ void _start(const char *arg)
 				u_putdec(pport);
 				u_putdec(pport);
 				u_putc('\n');
 				u_putc('\n');
 			}
 			}
-			serve();
+			while (serve())
+				;
 			u_tcp_srv_close();
 			u_tcp_srv_close();
 			continue;
 			continue;
 		}
 		}

+ 102 - 5
vfs.c

@@ -87,6 +87,8 @@ static void copy_bounded(char *dst, const char *src, unsigned int size)
 
 
 /* ---- FAT implementation ---- */
 /* ---- FAT implementation ---- */
 
 
+static void fat_cache_clear(void);
+
 /* One load buffer for the shell: only one command is loaded at a time and the
 /* One load buffer for the shell: only one command is loaded at a time and the
  * ELF loader has copied the segments out before we return. */
  * ELF loader has copied the segments out before we return. */
 static unsigned char fat_load_buf[VFS_FILE_MAX];
 static unsigned char fat_load_buf[VFS_FILE_MAX];
@@ -150,6 +152,7 @@ static void fat_op_release_file(struct vfs_mount *m, struct vfs_file *f)
 static int fat_op_write_file(struct vfs_mount *m, const char *path,
 static int fat_op_write_file(struct vfs_mount *m, const char *path,
 			     const void *buf, unsigned int len)
 			     const void *buf, unsigned int len)
 {
 {
+	fat_cache_clear();
 	unsigned int dir;
 	unsigned int dir;
 	char name[12];
 	char name[12];
 	int r = fat_resolve_path(&m->fs, path, &dir, name);
 	int r = fat_resolve_path(&m->fs, path, &dir, name);
@@ -168,6 +171,7 @@ static int fat_op_write_file(struct vfs_mount *m, const char *path,
 
 
 static int fat_op_unlink(struct vfs_mount *m, const char *path)
 static int fat_op_unlink(struct vfs_mount *m, const char *path)
 {
 {
+	fat_cache_clear();
 	unsigned int dir;
 	unsigned int dir;
 	char name[12];
 	char name[12];
 	int r = fat_resolve_path(&m->fs, path, &dir, name);
 	int r = fat_resolve_path(&m->fs, path, &dir, name);
@@ -186,6 +190,7 @@ static int fat_op_unlink(struct vfs_mount *m, const char *path)
 
 
 static int fat_op_mkdir(struct vfs_mount *m, const char *path)
 static int fat_op_mkdir(struct vfs_mount *m, const char *path)
 {
 {
+	fat_cache_clear();
 	unsigned int dir;
 	unsigned int dir;
 	char name[12];
 	char name[12];
 	int r = fat_resolve_path(&m->fs, path, &dir, name);
 	int r = fat_resolve_path(&m->fs, path, &dir, name);
@@ -204,6 +209,7 @@ static int fat_op_mkdir(struct vfs_mount *m, const char *path)
 
 
 static int fat_op_rmdir(struct vfs_mount *m, const char *path)
 static int fat_op_rmdir(struct vfs_mount *m, const char *path)
 {
 {
+	fat_cache_clear();
 	unsigned int dir;
 	unsigned int dir;
 	char name[12];
 	char name[12];
 	int r = fat_resolve_path(&m->fs, path, &dir, name);
 	int r = fat_resolve_path(&m->fs, path, &dir, name);
@@ -220,8 +226,52 @@ static int fat_op_rmdir(struct vfs_mount *m, const char *path)
 	return 0;
 	return 0;
 }
 }
 
 
-/* Index-based listing: fat_list_dir walks from the start each call and we
- * count past `index` entries.  Directories are small. */
+/* Index-based listing used to re-walk the FAT directory for every index, and
+ * the WebDAV server asks for 0..N-1 in one PROPFIND.  Cache the first 64
+ * entries of the last directory so a listing is one directory walk; a write
+ * invalidates it.  Larger directories fall back to the old walk past 64. */
+#define FAT_LIST_CACHE 64
+
+static struct vfs_dirent fat_cache[FAT_LIST_CACHE];
+static unsigned int fat_cache_count;
+static int fat_cache_valid;
+static int fat_cache_truncated;
+static struct vfs_mount *fat_cache_mount;
+static char fat_cache_path[VFS_PATH_MAX];
+
+static void fat_cache_clear(void)
+{
+	fat_cache_valid = 0;
+}
+
+static int fat_path_eq(const char *a, const char *b)
+{
+	unsigned int i = 0;
+
+	while (a[i] && b[i] && a[i] == b[i])
+		i++;
+	return a[i] == '\0' && b[i] == '\0';
+}
+
+static void fat_cache_cb(const char *name, int is_dir, unsigned int size)
+{
+	struct vfs_dirent *d;
+	unsigned int i;
+
+	if (fat_cache_count >= FAT_LIST_CACHE) {
+		fat_cache_truncated = 1;
+		return;
+	}
+	d = &fat_cache[fat_cache_count++];
+	for (i = 0; i < 11; i++)
+		d->name[i] = name[i] ? name[i] : ' ';
+	d->name[11] = '\0';
+	d->size = size;
+	d->is_dir = (unsigned int)is_dir;
+	d->raw_83 = 1;
+}
+
+/* Old behaviour: one directory walk, counting to `index`. */
 static unsigned int fat_list_index;
 static unsigned int fat_list_index;
 static struct vfs_dirent *fat_list_out;
 static struct vfs_dirent *fat_list_out;
 static int fat_list_done;
 static int fat_list_done;
@@ -245,8 +295,8 @@ static void fat_list_cb(const char *name, int is_dir, unsigned int size)
 	fat_list_seen++;
 	fat_list_seen++;
 }
 }
 
 
-static int fat_op_list_dir(struct vfs_mount *m, const char *path,
-			   unsigned int index, struct vfs_dirent *out)
+static int fat_list_walk(struct vfs_mount *m, const char *path,
+			 unsigned int index, struct vfs_dirent *out)
 {
 {
 	unsigned int dir;
 	unsigned int dir;
 	int r = fat_resolve_dir(&m->fs, path, &dir);
 	int r = fat_resolve_dir(&m->fs, path, &dir);
@@ -254,7 +304,7 @@ static int fat_op_list_dir(struct vfs_mount *m, const char *path,
 	if (r == -2)
 	if (r == -2)
 		return VFS_EIO;
 		return VFS_EIO;
 	if (r != 0)
 	if (r != 0)
-		return VFS_ENOENT;   /* not found (or not a directory) */
+		return VFS_ENOENT;
 
 
 	fat_list_index = index;
 	fat_list_index = index;
 	fat_list_seen = 0;
 	fat_list_seen = 0;
@@ -266,6 +316,50 @@ static int fat_op_list_dir(struct vfs_mount *m, const char *path,
 	return fat_list_done ? 1 : 0;
 	return fat_list_done ? 1 : 0;
 }
 }
 
 
+static int fat_op_list_dir(struct vfs_mount *m, const char *path,
+			   unsigned int index, struct vfs_dirent *out)
+{
+	int same = fat_cache_valid && fat_cache_mount == m &&
+		   fat_path_eq(fat_cache_path, path);
+
+	if (same) {
+		if (index < fat_cache_count) {
+			*out = fat_cache[index];
+			return 1;
+		}
+		if (!fat_cache_truncated)
+			return 0;
+		/* Beyond the cached prefix: use the old walk. */
+		return fat_list_walk(m, path, index, out);
+	}
+
+	{
+		unsigned int dir;
+		int r = fat_resolve_dir(&m->fs, path, &dir);
+
+		if (r == -2)
+			return VFS_EIO;
+		if (r != 0)
+			return VFS_ENOENT;   /* not found / not a directory */
+
+		fat_cache_count = 0;
+		fat_cache_truncated = 0;
+		fat_cache_mount = m;
+		copy_bounded(fat_cache_path, path, sizeof(fat_cache_path));
+		fat_cache_valid = 0;
+		r = fat_list_dir(&m->fs, dir, fat_cache_cb);
+		if (r == -2)
+			return VFS_EIO;
+		fat_cache_valid = 1;
+	}
+
+	if (index < fat_cache_count) {
+		*out = fat_cache[index];
+		return 1;
+	}
+	return 0;
+}
+
 static const struct fs_ops fat_ops = {
 static const struct fs_ops fat_ops = {
 	.raw_83 = 1,
 	.raw_83 = 1,
 	.read_file = fat_op_read_file,
 	.read_file = fat_op_read_file,
@@ -444,6 +538,8 @@ static void mount_one(blk_dev_t *dev, unsigned int part_lba, const char *name)
 	fat_fs_t fs;
 	fat_fs_t fs;
 	vfs_mount_t *m;
 	vfs_mount_t *m;
 
 
+	fat_cache_clear();
+
 	if (fat_mount(dev, part_lba, &fs) != 0)
 	if (fat_mount(dev, part_lba, &fs) != 0)
 		return;
 		return;
 	m = mount_slot(name);
 	m = mount_slot(name);
@@ -605,6 +701,7 @@ void vfs_remount_device(blk_dev_t *d)
 
 
 	if (!d)
 	if (!d)
 		return;
 		return;
+	fat_cache_clear();
 	unmount_device(d->name);
 	unmount_device(d->name);
 	mount_device(d, buf);
 	mount_device(d, buf);
 	if (!device_is_mounted(d->name)) {
 	if (!device_is_mounted(d->name)) {