Blame SOURCES/1106-btrfs-progs-receive-process-encoded_write-commands.patch

dbfe2d
From 3523dd761db148a1214269f6d3dd357231dcbcff Mon Sep 17 00:00:00 2001
dbfe2d
From: Boris Burkov <borisb@fb.com>
dbfe2d
Date: Fri, 21 Aug 2020 00:40:05 -0700
dbfe2d
Subject: [PATCH] btrfs-progs: receive: process encoded_write commands
dbfe2d
dbfe2d
Add a new btrfs_send_op and support for both dumping and proper receive
dbfe2d
processing which does actual encoded writes.
dbfe2d
dbfe2d
Encoded writes are only allowed on a file descriptor opened with an
dbfe2d
extra flag that allows encoded writes, so we also add support for this
dbfe2d
flag when opening or reusing a file for writing.
dbfe2d
dbfe2d
Signed-off-by: Boris Burkov <boris@bur.io>
dbfe2d
---
dbfe2d
 cmds/receive-dump.c  |  16 +++++-
dbfe2d
 cmds/receive.c       |  48 ++++++++++++++++
dbfe2d
 common/send-stream.c |  29 ++++++++++
dbfe2d
 common/send-stream.h |   4 ++
dbfe2d
 ioctl.h              | 132 +++++++++++++++++++++++++++++++++++++++++++
dbfe2d
 5 files changed, 228 insertions(+), 1 deletion(-)
dbfe2d
dbfe2d
diff --git a/cmds/receive-dump.c b/cmds/receive-dump.c
dbfe2d
index 00ad4fd1..83701b62 100644
dbfe2d
--- a/cmds/receive-dump.c
dbfe2d
+++ b/cmds/receive-dump.c
dbfe2d
@@ -318,6 +318,19 @@ static int print_update_extent(const char *path, u64 offset, u64 len,
dbfe2d
 			  offset, len);
dbfe2d
 }
dbfe2d
 
dbfe2d
+static int print_encoded_write(const char *path, const void *data, u64 offset,
dbfe2d
+			       u64 len, u64 unencoded_file_len,
dbfe2d
+			       u64 unencoded_len, u64 unencoded_offset,
dbfe2d
+			       u32 compression, u32 encryption, void *user)
dbfe2d
+{
dbfe2d
+	return PRINT_DUMP(user, path, "encoded_write",
dbfe2d
+			  "offset=%llu len=%llu, unencoded_file_len=%llu, "
dbfe2d
+			  "unencoded_len=%llu, unencoded_offset=%llu, "
dbfe2d
+			  "compression=%u, encryption=%u",
dbfe2d
+			  offset, len, unencoded_file_len, unencoded_len,
dbfe2d
+			  unencoded_offset, compression, encryption);
dbfe2d
+}
dbfe2d
+
dbfe2d
 struct btrfs_send_ops btrfs_print_send_ops = {
dbfe2d
 	.subvol = print_subvol,
dbfe2d
 	.snapshot = print_snapshot,
dbfe2d
@@ -339,5 +352,6 @@ struct btrfs_send_ops btrfs_print_send_ops = {
dbfe2d
 	.chmod = print_chmod,
dbfe2d
 	.chown = print_chown,
dbfe2d
 	.utimes = print_utimes,
dbfe2d
-	.update_extent = print_update_extent
dbfe2d
+	.update_extent = print_update_extent,
dbfe2d
+	.encoded_write = print_encoded_write,
dbfe2d
 };
dbfe2d
diff --git a/cmds/receive.c b/cmds/receive.c
dbfe2d
index d106e554..8226ca32 100644
dbfe2d
--- a/cmds/receive.c
dbfe2d
+++ b/cmds/receive.c
dbfe2d
@@ -29,12 +29,14 @@
dbfe2d
 #include <assert.h>
dbfe2d
 #include <getopt.h>
dbfe2d
 #include <limits.h>
dbfe2d
+#include <errno.h>
dbfe2d
 
dbfe2d
 #include <sys/stat.h>
dbfe2d
 #include <sys/types.h>
dbfe2d
 #include <sys/ioctl.h>
dbfe2d
 #include <sys/time.h>
dbfe2d
 #include <sys/types.h>
dbfe2d
+#include <sys/uio.h>
dbfe2d
 #include <sys/xattr.h>
dbfe2d
 #include <uuid/uuid.h>
dbfe2d
 
dbfe2d
@@ -49,6 +51,7 @@
dbfe2d
 #include "cmds/receive-dump.h"
dbfe2d
 #include "common/help.h"
dbfe2d
 #include "common/path-utils.h"
dbfe2d
+#include "stubs.h"
dbfe2d
 
dbfe2d
 struct btrfs_receive
dbfe2d
 {
dbfe2d
@@ -982,6 +985,50 @@ static int process_update_extent(const char *path, u64 offset, u64 len,
dbfe2d
 	return 0;
dbfe2d
 }
dbfe2d
 
dbfe2d
+static int process_encoded_write(const char *path, const void *data, u64 offset,
dbfe2d
+				 u64 len, u64 unencoded_file_len,
dbfe2d
+				 u64 unencoded_len, u64 unencoded_offset,
dbfe2d
+				 u32 compression, u32 encryption, void *user)
dbfe2d
+{
dbfe2d
+	int ret;
dbfe2d
+	struct btrfs_receive *rctx = user;
dbfe2d
+	char full_path[PATH_MAX];
dbfe2d
+	struct iovec iov = { (char *)data, len };
dbfe2d
+	struct btrfs_ioctl_encoded_io_args encoded = {
dbfe2d
+		.iov = &iov,
dbfe2d
+		.iovcnt = 1,
dbfe2d
+		.offset = offset,
dbfe2d
+		.len = unencoded_file_len,
dbfe2d
+		.unencoded_len = unencoded_len,
dbfe2d
+		.unencoded_offset = unencoded_offset,
dbfe2d
+		.compression = compression,
dbfe2d
+		.encryption = encryption,
dbfe2d
+	};
dbfe2d
+
dbfe2d
+	if (encryption) {
dbfe2d
+		error("encoded_write: encryption not supported");
dbfe2d
+		return -EOPNOTSUPP;
dbfe2d
+	}
dbfe2d
+
dbfe2d
+	ret = path_cat_out(full_path, rctx->full_subvol_path, path);
dbfe2d
+	if (ret < 0) {
dbfe2d
+		error("encoded_write: path invalid: %s", path);
dbfe2d
+		return ret;
dbfe2d
+	}
dbfe2d
+
dbfe2d
+	ret = open_inode_for_write(rctx, full_path);
dbfe2d
+	if (ret < 0)
dbfe2d
+		return ret;
dbfe2d
+
dbfe2d
+	ret = ioctl(rctx->write_fd, BTRFS_IOC_ENCODED_WRITE, &encoded);
dbfe2d
+	if (ret < 0) {
dbfe2d
+		ret = -errno;
dbfe2d
+		error("encoded_write: writing to %s failed: %m", path);
dbfe2d
+		return ret;
dbfe2d
+	}
dbfe2d
+	return 0;
dbfe2d
+}
dbfe2d
+
dbfe2d
 static struct btrfs_send_ops send_ops = {
dbfe2d
 	.subvol = process_subvol,
dbfe2d
 	.snapshot = process_snapshot,
dbfe2d
@@ -1004,6 +1051,7 @@ static struct btrfs_send_ops send_ops = {
dbfe2d
 	.chown = process_chown,
dbfe2d
 	.utimes = process_utimes,
dbfe2d
 	.update_extent = process_update_extent,
dbfe2d
+	.encoded_write = process_encoded_write,
dbfe2d
 };
dbfe2d
 
dbfe2d
 static int do_receive(struct btrfs_receive *rctx, const char *tomnt,
dbfe2d
diff --git a/common/send-stream.c b/common/send-stream.c
dbfe2d
index 81a830d9..ce7c40f5 100644
dbfe2d
--- a/common/send-stream.c
dbfe2d
+++ b/common/send-stream.c
dbfe2d
@@ -357,6 +357,8 @@ static int read_and_process_cmd(struct btrfs_send_stream *sctx)
dbfe2d
 	struct timespec mt;
dbfe2d
 	u8 uuid[BTRFS_UUID_SIZE];
dbfe2d
 	u8 clone_uuid[BTRFS_UUID_SIZE];
dbfe2d
+	u32 compression;
dbfe2d
+	u32 encryption;
dbfe2d
 	u64 tmp;
dbfe2d
 	u64 tmp2;
dbfe2d
 	u64 ctransid;
dbfe2d
@@ -366,6 +368,9 @@ static int read_and_process_cmd(struct btrfs_send_stream *sctx)
dbfe2d
 	u64 clone_offset;
dbfe2d
 	u64 offset;
dbfe2d
 	u64 ino;
dbfe2d
+	u64 unencoded_file_len;
dbfe2d
+	u64 unencoded_len;
dbfe2d
+	u64 unencoded_offset;
dbfe2d
 	int len;
dbfe2d
 	int xattr_len;
dbfe2d
 
dbfe2d
@@ -452,6 +457,30 @@ static int read_and_process_cmd(struct btrfs_send_stream *sctx)
dbfe2d
 		TLV_GET(sctx, BTRFS_SEND_A_DATA, &data, &len;;
dbfe2d
 		ret = sctx->ops->write(path, data, offset, len, sctx->user);
dbfe2d
 		break;
dbfe2d
+	case BTRFS_SEND_C_ENCODED_WRITE:
dbfe2d
+		TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
dbfe2d
+		TLV_GET_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, &offset);
dbfe2d
+		TLV_GET_U64(sctx, BTRFS_SEND_A_UNENCODED_FILE_LEN,
dbfe2d
+			    &unencoded_file_len);
dbfe2d
+		TLV_GET_U64(sctx, BTRFS_SEND_A_UNENCODED_LEN, &unencoded_len);
dbfe2d
+		TLV_GET_U64(sctx, BTRFS_SEND_A_UNENCODED_OFFSET,
dbfe2d
+			    &unencoded_offset);
dbfe2d
+		/* Compression and encryption default to none if omitted. */
dbfe2d
+		if (sctx->cmd_attrs[BTRFS_SEND_A_COMPRESSION].data)
dbfe2d
+			TLV_GET_U32(sctx, BTRFS_SEND_A_COMPRESSION, &compression);
dbfe2d
+		else
dbfe2d
+			compression = BTRFS_ENCODED_IO_COMPRESSION_NONE;
dbfe2d
+		if (sctx->cmd_attrs[BTRFS_SEND_A_ENCRYPTION].data)
dbfe2d
+			TLV_GET_U32(sctx, BTRFS_SEND_A_ENCRYPTION, &encryption);
dbfe2d
+		else
dbfe2d
+			encryption = BTRFS_ENCODED_IO_ENCRYPTION_NONE;
dbfe2d
+		TLV_GET(sctx, BTRFS_SEND_A_DATA, &data, &len;;
dbfe2d
+		ret = sctx->ops->encoded_write(path, data, offset, len,
dbfe2d
+					       unencoded_file_len,
dbfe2d
+					       unencoded_len, unencoded_offset,
dbfe2d
+					       compression, encryption,
dbfe2d
+					       sctx->user);
dbfe2d
+		break;
dbfe2d
 	case BTRFS_SEND_C_CLONE:
dbfe2d
 		TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
dbfe2d
 		TLV_GET_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, &offset);
dbfe2d
diff --git a/common/send-stream.h b/common/send-stream.h
dbfe2d
index 2de51eac..44abbc9d 100644
dbfe2d
--- a/common/send-stream.h
dbfe2d
+++ b/common/send-stream.h
dbfe2d
@@ -53,6 +53,10 @@ struct btrfs_send_ops {
dbfe2d
 		      struct timespec *mt, struct timespec *ct,
dbfe2d
 		      void *user);
dbfe2d
 	int (*update_extent)(const char *path, u64 offset, u64 len, void *user);
dbfe2d
+	int (*encoded_write)(const char *path, const void *data, u64 offset,
dbfe2d
+			     u64 len, u64 unencoded_file_len, u64 unencoded_len,
dbfe2d
+			     u64 unencoded_offset, u32 compression,
dbfe2d
+			     u32 encryption, void *user);
dbfe2d
 };
dbfe2d
 
dbfe2d
 int btrfs_read_and_process_send_stream(int fd,
dbfe2d
diff --git a/ioctl.h b/ioctl.h
dbfe2d
index 368a87b2..8adf63c2 100644
dbfe2d
--- a/ioctl.h
dbfe2d
+++ b/ioctl.h
dbfe2d
@@ -777,6 +777,134 @@ struct btrfs_ioctl_get_subvol_rootref_args {
dbfe2d
 };
dbfe2d
 BUILD_ASSERT(sizeof(struct btrfs_ioctl_get_subvol_rootref_args) == 4096);
dbfe2d
 
dbfe2d
+/*
dbfe2d
+ * Data and metadata for an encoded read or write.
dbfe2d
+ *
dbfe2d
+ * Encoded I/O bypasses any encoding automatically done by the filesystem (e.g.,
dbfe2d
+ * compression). This can be used to read the compressed contents of a file or
dbfe2d
+ * write pre-compressed data directly to a file.
dbfe2d
+ *
dbfe2d
+ * BTRFS_IOC_ENCODED_READ and BTRFS_IOC_ENCODED_WRITE are essentially
dbfe2d
+ * preadv/pwritev with additional metadata about how the data is encoded and the
dbfe2d
+ * size of the unencoded data.
dbfe2d
+ *
dbfe2d
+ * BTRFS_IOC_ENCODED_READ fills the given iovecs with the encoded data, fills
dbfe2d
+ * the metadata fields, and returns the size of the encoded data. It reads one
dbfe2d
+ * extent per call. It can also read data which is not encoded.
dbfe2d
+ *
dbfe2d
+ * BTRFS_IOC_ENCODED_WRITE uses the metadata fields, writes the encoded data
dbfe2d
+ * from the iovecs, and returns the size of the encoded data. Note that the
dbfe2d
+ * encoded data is not validated when it is written; if it is not valid (e.g.,
dbfe2d
+ * it cannot be decompressed), then a subsequent read may return an error.
dbfe2d
+ *
dbfe2d
+ * Since the filesystem page cache contains decoded data, encoded I/O bypasses
dbfe2d
+ * the page cache. Encoded I/O requires CAP_SYS_ADMIN.
dbfe2d
+ */
dbfe2d
+struct btrfs_ioctl_encoded_io_args {
dbfe2d
+	/* Input parameters for both reads and writes. */
dbfe2d
+
dbfe2d
+	/*
dbfe2d
+	 * iovecs containing encoded data.
dbfe2d
+	 *
dbfe2d
+	 * For reads, if the size of the encoded data is larger than the sum of
dbfe2d
+	 * iov[n].iov_len for 0 <= n < iovcnt, then the ioctl fails with
dbfe2d
+	 * ENOBUFS.
dbfe2d
+	 *
dbfe2d
+	 * For writes, the size of the encoded data is the sum of iov[n].iov_len
dbfe2d
+	 * for 0 <= n < iovcnt. This must be less than 128 KiB (this limit may
dbfe2d
+	 * increase in the future). This must also be less than or equal to
dbfe2d
+	 * unencoded_len.
dbfe2d
+	 */
dbfe2d
+	const struct iovec __user *iov;
dbfe2d
+	/* Number of iovecs. */
dbfe2d
+	unsigned long iovcnt;
dbfe2d
+	/*
dbfe2d
+	 * Offset in file.
dbfe2d
+	 *
dbfe2d
+	 * For writes, must be aligned to the sector size of the filesystem.
dbfe2d
+	 */
dbfe2d
+	__s64 offset;
dbfe2d
+	/* Currently must be zero. */
dbfe2d
+	__u64 flags;
dbfe2d
+
dbfe2d
+	/*
dbfe2d
+	 * For reads, the following members are output parameters that will
dbfe2d
+	 * contain the returned metadata for the encoded data.
dbfe2d
+	 * For writes, the following members must be set to the metadata for the
dbfe2d
+	 * encoded data.
dbfe2d
+	 */
dbfe2d
+
dbfe2d
+	/*
dbfe2d
+	 * Length of the data in the file.
dbfe2d
+	 *
dbfe2d
+	 * Must be less than or equal to unencoded_len - unencoded_offset. For
dbfe2d
+	 * writes, must be aligned to the sector size of the filesystem unless
dbfe2d
+	 * the data ends at or beyond the current end of the file.
dbfe2d
+	 */
dbfe2d
+	__u64 len;
dbfe2d
+	/*
dbfe2d
+	 * Length of the unencoded (i.e., decrypted and decompressed) data.
dbfe2d
+	 *
dbfe2d
+	 * For writes, must be no more than 128 KiB (this limit may increase in
dbfe2d
+	 * the future). If the unencoded data is actually longer than
dbfe2d
+	 * unencoded_len, then it is truncated; if it is shorter, then it is
dbfe2d
+	 * extended with zeroes.
dbfe2d
+	 */
dbfe2d
+	__u64 unencoded_len;
dbfe2d
+	/*
dbfe2d
+	 * Offset from the first byte of the unencoded data to the first byte of
dbfe2d
+	 * logical data in the file.
dbfe2d
+	 *
dbfe2d
+	 * Must be less than unencoded_len.
dbfe2d
+	 */
dbfe2d
+	__u64 unencoded_offset;
dbfe2d
+	/*
dbfe2d
+	 * BTRFS_ENCODED_IO_COMPRESSION_* type.
dbfe2d
+	 *
dbfe2d
+	 * For writes, must not be BTRFS_ENCODED_IO_COMPRESSION_NONE.
dbfe2d
+	 */
dbfe2d
+	__u32 compression;
dbfe2d
+	/* Currently always BTRFS_ENCODED_IO_ENCRYPTION_NONE. */
dbfe2d
+	__u32 encryption;
dbfe2d
+	/*
dbfe2d
+	 * Reserved for future expansion.
dbfe2d
+	 *
dbfe2d
+	 * For reads, always returned as zero. Users should check for non-zero
dbfe2d
+	 * bytes. If there are any, then the kernel has a newer version of this
dbfe2d
+	 * structure with additional information that the user definition is
dbfe2d
+	 * missing.
dbfe2d
+	 *
dbfe2d
+	 * For writes, must be zeroed.
dbfe2d
+	 */
dbfe2d
+	__u8 reserved[64];
dbfe2d
+};
dbfe2d
+
dbfe2d
+/* Data is not compressed. */
dbfe2d
+#define BTRFS_ENCODED_IO_COMPRESSION_NONE 0
dbfe2d
+/* Data is compressed as a single zlib stream. */
dbfe2d
+#define BTRFS_ENCODED_IO_COMPRESSION_ZLIB 1
dbfe2d
+/*
dbfe2d
+ * Data is compressed as a single zstd frame with the windowLog compression
dbfe2d
+ * parameter set to no more than 17.
dbfe2d
+ */
dbfe2d
+#define BTRFS_ENCODED_IO_COMPRESSION_ZSTD 2
dbfe2d
+/*
dbfe2d
+ * Data is compressed sector by sector (using the sector size indicated by the
dbfe2d
+ * name of the constant) with LZO1X and wrapped in the format documented in
dbfe2d
+ * fs/btrfs/lzo.c. For writes, the compression sector size must match the
dbfe2d
+ * filesystem sector size.
dbfe2d
+ */
dbfe2d
+#define BTRFS_ENCODED_IO_COMPRESSION_LZO_4K 3
dbfe2d
+#define BTRFS_ENCODED_IO_COMPRESSION_LZO_8K 4
dbfe2d
+#define BTRFS_ENCODED_IO_COMPRESSION_LZO_16K 5
dbfe2d
+#define BTRFS_ENCODED_IO_COMPRESSION_LZO_32K 6
dbfe2d
+#define BTRFS_ENCODED_IO_COMPRESSION_LZO_64K 7
dbfe2d
+#define BTRFS_ENCODED_IO_COMPRESSION_TYPES 8
dbfe2d
+
dbfe2d
+/* Data is not encrypted. */
dbfe2d
+#define BTRFS_ENCODED_IO_ENCRYPTION_NONE 0
dbfe2d
+#define BTRFS_ENCODED_IO_ENCRYPTION_TYPES 1
dbfe2d
+
dbfe2d
 /* Error codes as returned by the kernel */
dbfe2d
 enum btrfs_err_code {
dbfe2d
 	notused,
dbfe2d
@@ -951,6 +1079,10 @@ static inline char *btrfs_err_str(enum btrfs_err_code err_code)
dbfe2d
 				struct btrfs_ioctl_ino_lookup_user_args)
dbfe2d
 #define BTRFS_IOC_SNAP_DESTROY_V2 _IOW(BTRFS_IOCTL_MAGIC, 63, \
dbfe2d
 				   struct btrfs_ioctl_vol_args_v2)
dbfe2d
+#define BTRFS_IOC_ENCODED_READ _IOR(BTRFS_IOCTL_MAGIC, 64, \
dbfe2d
+				    struct btrfs_ioctl_encoded_io_args)
dbfe2d
+#define BTRFS_IOC_ENCODED_WRITE _IOW(BTRFS_IOCTL_MAGIC, 64, \
dbfe2d
+				     struct btrfs_ioctl_encoded_io_args)
dbfe2d
 
dbfe2d
 #ifdef __cplusplus
dbfe2d
 }
dbfe2d
-- 
dbfe2d
2.35.1
dbfe2d