borisb / rpms / btrfs-progs

Forked from rpms/btrfs-progs 2 years ago
Clone
dbfe2d
From e274c8de61c02f4a65812371bc6f1c2a6ac4d30c Mon Sep 17 00:00:00 2001
dbfe2d
From: Boris Burkov <boris@bur.io>
dbfe2d
Date: Fri, 21 Aug 2020 00:40:01 -0700
dbfe2d
Subject: [PATCH] btrfs-progs: receive: dynamically allocate sctx->read_buf
dbfe2d
dbfe2d
In send stream v2, write commands can now be an arbitrary size. For that
dbfe2d
reason, we can no longer allocate a fixed array in sctx for read_cmd.
dbfe2d
Instead, read_cmd dynamically allocates sctx->read_buf. To avoid
dbfe2d
needless reallocations, we reuse read_buf between read_cmd calls by also
dbfe2d
keeping track of the size of the allocated buffer in sctx->read_buf_sz.
dbfe2d
dbfe2d
We do the first allocation of the old default size at the start of
dbfe2d
processing the stream, and we only reallocate if we encounter a command
dbfe2d
that needs a larger buffer.
dbfe2d
dbfe2d
Signed-off-by: Boris Burkov <boris@bur.io>
dbfe2d
---
dbfe2d
 common/send-stream.c   | 56 ++++++++++++++++++++++++++++--------------
dbfe2d
 kernel-shared/send.h   |  6 ++++-
dbfe2d
 libbtrfs/send-stream.c |  2 +-
dbfe2d
 3 files changed, 43 insertions(+), 21 deletions(-)
dbfe2d
dbfe2d
diff --git a/common/send-stream.c b/common/send-stream.c
dbfe2d
index 7d182238..421cd1bb 100644
dbfe2d
--- a/common/send-stream.c
dbfe2d
+++ b/common/send-stream.c
dbfe2d
@@ -35,11 +35,11 @@ struct btrfs_send_attribute {
dbfe2d
 };
dbfe2d
 
dbfe2d
 struct btrfs_send_stream {
dbfe2d
-	char read_buf[BTRFS_SEND_BUF_SIZE];
dbfe2d
+	char *read_buf;
dbfe2d
+	size_t read_buf_sz;
dbfe2d
 	int fd;
dbfe2d
 
dbfe2d
 	int cmd;
dbfe2d
-	struct btrfs_cmd_header *cmd_hdr;
dbfe2d
 	struct btrfs_send_attribute cmd_attrs[BTRFS_SEND_A_MAX + 1];
dbfe2d
 	u32 version;
dbfe2d
 
dbfe2d
@@ -111,11 +111,12 @@ static int read_cmd(struct btrfs_send_stream *sctx)
dbfe2d
 	u32 pos;
dbfe2d
 	u32 crc;
dbfe2d
 	u32 crc2;
dbfe2d
+	struct btrfs_cmd_header *cmd_hdr;
dbfe2d
+	size_t buf_len;
dbfe2d
 
dbfe2d
 	memset(sctx->cmd_attrs, 0, sizeof(sctx->cmd_attrs));
dbfe2d
 
dbfe2d
-	ASSERT(sizeof(*sctx->cmd_hdr) <= sizeof(sctx->read_buf));
dbfe2d
-	ret = read_buf(sctx, sctx->read_buf, sizeof(*sctx->cmd_hdr));
dbfe2d
+	ret = read_buf(sctx, sctx->read_buf, sizeof(*cmd_hdr));
dbfe2d
 	if (ret < 0)
dbfe2d
 		goto out;
dbfe2d
 	if (ret) {
dbfe2d
@@ -124,18 +125,25 @@ static int read_cmd(struct btrfs_send_stream *sctx)
dbfe2d
 		goto out;
dbfe2d
 	}
dbfe2d
 
dbfe2d
-	sctx->cmd_hdr = (struct btrfs_cmd_header *)sctx->read_buf;
dbfe2d
-	cmd = le16_to_cpu(sctx->cmd_hdr->cmd);
dbfe2d
-	cmd_len = le32_to_cpu(sctx->cmd_hdr->len);
dbfe2d
+	cmd_hdr = (struct btrfs_cmd_header *)sctx->read_buf;
dbfe2d
+	cmd_len = le32_to_cpu(cmd_hdr->len);
dbfe2d
+	cmd = le16_to_cpu(cmd_hdr->cmd);
dbfe2d
+	buf_len = sizeof(*cmd_hdr) + cmd_len;
dbfe2d
+	if (sctx->read_buf_sz < buf_len) {
dbfe2d
+		void *new_read_buf;
dbfe2d
 
dbfe2d
-	if (cmd_len + sizeof(*sctx->cmd_hdr) >= sizeof(sctx->read_buf)) {
dbfe2d
-		ret = -EINVAL;
dbfe2d
-		error("command length %u too big for buffer %zu",
dbfe2d
-				cmd_len, sizeof(sctx->read_buf));
dbfe2d
-		goto out;
dbfe2d
+		new_read_buf = realloc(sctx->read_buf, buf_len);
dbfe2d
+		if (!new_read_buf) {
dbfe2d
+			ret = -ENOMEM;
dbfe2d
+			error("failed to reallocate read buffer for cmd");
dbfe2d
+			goto out;
dbfe2d
+		}
dbfe2d
+		sctx->read_buf = new_read_buf;
dbfe2d
+		sctx->read_buf_sz = buf_len;
dbfe2d
+		/* We need to reset cmd_hdr after realloc of sctx->read_buf */
dbfe2d
+		cmd_hdr = (struct btrfs_cmd_header *)sctx->read_buf;
dbfe2d
 	}
dbfe2d
-
dbfe2d
-	data = sctx->read_buf + sizeof(*sctx->cmd_hdr);
dbfe2d
+	data = sctx->read_buf + sizeof(*cmd_hdr);
dbfe2d
 	ret = read_buf(sctx, data, cmd_len);
dbfe2d
 	if (ret < 0)
dbfe2d
 		goto out;
dbfe2d
@@ -145,11 +153,12 @@ static int read_cmd(struct btrfs_send_stream *sctx)
dbfe2d
 		goto out;
dbfe2d
 	}
dbfe2d
 
dbfe2d
-	crc = le32_to_cpu(sctx->cmd_hdr->crc);
dbfe2d
-	sctx->cmd_hdr->crc = 0;
dbfe2d
+	crc = le32_to_cpu(cmd_hdr->crc);
dbfe2d
+	/* in send, crc is computed with header crc = 0, replicate that */
dbfe2d
+	cmd_hdr->crc = 0;
dbfe2d
 
dbfe2d
 	crc2 = crc32c(0, (unsigned char*)sctx->read_buf,
dbfe2d
-			sizeof(*sctx->cmd_hdr) + cmd_len);
dbfe2d
+			sizeof(*cmd_hdr) + cmd_len);
dbfe2d
 
dbfe2d
 	if (crc != crc2) {
dbfe2d
 		ret = -EINVAL;
dbfe2d
@@ -537,19 +546,28 @@ int btrfs_read_and_process_send_stream(int fd,
dbfe2d
 		goto out;
dbfe2d
 	}
dbfe2d
 
dbfe2d
+	sctx.read_buf = malloc(BTRFS_SEND_BUF_SIZE_V1);
dbfe2d
+	if (!sctx.read_buf) {
dbfe2d
+		ret = -ENOMEM;
dbfe2d
+		error("unable to allocate send stream read buffer");
dbfe2d
+		goto out;
dbfe2d
+	}
dbfe2d
+	sctx.read_buf_sz = BTRFS_SEND_BUF_SIZE_V1;
dbfe2d
+
dbfe2d
 	while (1) {
dbfe2d
 		ret = read_and_process_cmd(&sctx);
dbfe2d
 		if (ret < 0) {
dbfe2d
 			last_err = ret;
dbfe2d
 			errors++;
dbfe2d
 			if (max_errors > 0 && errors >= max_errors)
dbfe2d
-				goto out;
dbfe2d
+				break;
dbfe2d
 		} else if (ret > 0) {
dbfe2d
 			if (!honor_end_cmd)
dbfe2d
 				ret = 0;
dbfe2d
-			goto out;
dbfe2d
+			break;
dbfe2d
 		}
dbfe2d
 	}
dbfe2d
+	free(sctx.read_buf);
dbfe2d
 
dbfe2d
 out:
dbfe2d
 	if (last_err && !ret)
dbfe2d
diff --git a/kernel-shared/send.h b/kernel-shared/send.h
dbfe2d
index e73f09df..e986b6c8 100644
dbfe2d
--- a/kernel-shared/send.h
dbfe2d
+++ b/kernel-shared/send.h
dbfe2d
@@ -33,7 +33,11 @@ extern "C" {
dbfe2d
 #define BTRFS_SEND_STREAM_MAGIC "btrfs-stream"
dbfe2d
 #define BTRFS_SEND_STREAM_VERSION 1
dbfe2d
 
dbfe2d
-#define BTRFS_SEND_BUF_SIZE  (64 * 1024)
dbfe2d
+/*
dbfe2d
+ * In send stream v1, no command is larger than 64k. In send stream v2, no limit
dbfe2d
+ * should be assumed.
dbfe2d
+ */
dbfe2d
+#define BTRFS_SEND_BUF_SIZE_V1 (64 * 1024)
dbfe2d
 #define BTRFS_SEND_READ_SIZE (1024 * 48)
dbfe2d
 
dbfe2d
 enum btrfs_tlv_type {
dbfe2d
diff --git a/libbtrfs/send-stream.c b/libbtrfs/send-stream.c
dbfe2d
index 2b21d846..39cbb3ed 100644
dbfe2d
--- a/libbtrfs/send-stream.c
dbfe2d
+++ b/libbtrfs/send-stream.c
dbfe2d
@@ -22,7 +22,7 @@
dbfe2d
 #include "crypto/crc32c.h"
dbfe2d
 
dbfe2d
 struct btrfs_send_stream {
dbfe2d
-	char read_buf[BTRFS_SEND_BUF_SIZE];
dbfe2d
+	char read_buf[BTRFS_SEND_BUF_SIZE_V1];
dbfe2d
 	int fd;
dbfe2d
 
dbfe2d
 	int cmd;
dbfe2d
-- 
dbfe2d
2.35.1
dbfe2d