[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

[Nbd] [PATCH] Consume unwanted data on a write that fails



When a write fails, we should consume the unwanted data sent to use. Whilst
currently the server exits (under certain conditions), it does so messily
(attempting to read some incoming data as commands). Instead, throw away
the data for easier debugging.

Available from git.alex.org.uk as usual
-- 
Alex Bligh


Signed-off-by: Alex Bligh <alex@...872...>
---
 nbd-server.c |   20 ++++++++++++++++++++
 1 files changed, 20 insertions(+), 0 deletions(-)

diff --git a/nbd-server.c b/nbd-server.c
index a9098a5..695deef 100644
--- a/nbd-server.c
+++ b/nbd-server.c
@@ -360,6 +360,24 @@ static inline void readit(int f, void *buf, size_t len) {
 }
 
 /**
+ * Consume data from an FD that we don't want
+ *
+ * @param f a file descriptor
+ * @param buf a buffer
+ * @param len the number of bytes to consume
+ * @param bufsiz the size of the buffer
+ **/
+static inline void consume(int f, void * buf, size_t len, size_t bufsiz) {
+	size_t curlen;
+	while (len>0) {
+		curlen = (len>bufsiz)?bufsiz:len;
+		readit(f, buf, curlen);
+		len -= curlen;
+	}
+}
+
+
+/**
  * Write data from a buffer into a filedescriptor
  *
  * @param f a file descriptor
@@ -1547,12 +1565,14 @@ int mainloop(CLIENT *client) {
 				    (client->server->flags & F_AUTOREADONLY)) {
 					DEBUG("[WRITE to READONLY!]");
 					ERROR(client, reply, EPERM);
+					consume(client->net, buf, len-currlen, BUFSIZE);
 					continue;
 				}
 				if (expwrite(request.from, buf, len, client,
 					     request.type & NBD_CMD_FLAG_FUA)) {
 					DEBUG("Write failed: %m" );
 					ERROR(client, reply, errno);
+					consume(client->net, buf, len-currlen, BUFSIZE);
 					continue;
 				}
 				len -= currlen;
-- 
1.7.4.1




Reply to: