osandov / rpms / btrfs-progs

Forked from rpms/btrfs-progs 2 years ago
Clone
dbfe2d
From 6e8d60a8c9ab22c489c0707ffd854bf1db9852f4 Mon Sep 17 00:00:00 2001
dbfe2d
From: Boris Burkov <boris@bur.io>
dbfe2d
Date: Fri, 21 Aug 2020 00:40:08 -0700
dbfe2d
Subject: [PATCH] btrfs-progs: receive: process setflags ioctl commands
dbfe2d
dbfe2d
In send stream v2, send can emit a command for setting inode flags via
dbfe2d
the setflags ioctl. Pass the flags attribute through to the ioctl call
dbfe2d
in receive.
dbfe2d
dbfe2d
Reviewed-by: Nikolay Borisov <nborisov@suse.com>
dbfe2d
Signed-off-by: Boris Burkov <boris@bur.io>
dbfe2d
---
dbfe2d
 cmds/receive-dump.c  |  6 ++++++
dbfe2d
 cmds/receive.c       | 25 +++++++++++++++++++++++++
dbfe2d
 common/send-stream.c |  7 +++++++
dbfe2d
 common/send-stream.h |  1 +
dbfe2d
 4 files changed, 39 insertions(+)
dbfe2d
dbfe2d
diff --git a/cmds/receive-dump.c b/cmds/receive-dump.c
dbfe2d
index fa397bcf..df5991e1 100644
dbfe2d
--- a/cmds/receive-dump.c
dbfe2d
+++ b/cmds/receive-dump.c
dbfe2d
@@ -339,6 +339,11 @@ static int print_fallocate(const char *path, int mode, u64 offset, u64 len,
dbfe2d
 			  mode, offset, len);
dbfe2d
 }
dbfe2d
 
dbfe2d
+static int print_setflags(const char *path, int flags, void *user)
dbfe2d
+{
dbfe2d
+	return PRINT_DUMP(user, path, "setflags", "flags=%d", flags);
dbfe2d
+}
dbfe2d
+
dbfe2d
 struct btrfs_send_ops btrfs_print_send_ops = {
dbfe2d
 	.subvol = print_subvol,
dbfe2d
 	.snapshot = print_snapshot,
dbfe2d
@@ -363,4 +368,5 @@ struct btrfs_send_ops btrfs_print_send_ops = {
dbfe2d
 	.update_extent = print_update_extent,
dbfe2d
 	.encoded_write = print_encoded_write,
dbfe2d
 	.fallocate = print_fallocate,
dbfe2d
+	.setflags = print_setflags,
dbfe2d
 };
dbfe2d
diff --git a/cmds/receive.c b/cmds/receive.c
dbfe2d
index 4893d693..7f76a04f 100644
dbfe2d
--- a/cmds/receive.c
dbfe2d
+++ b/cmds/receive.c
dbfe2d
@@ -38,6 +38,7 @@
dbfe2d
 #include <sys/types.h>
dbfe2d
 #include <sys/uio.h>
dbfe2d
 #include <sys/xattr.h>
dbfe2d
+#include <linux/fs.h>
dbfe2d
 #include <uuid/uuid.h>
dbfe2d
 
dbfe2d
 #include <lzo/lzo1x.h>
dbfe2d
@@ -1284,6 +1285,29 @@ static int process_fallocate(const char *path, int mode, u64 offset, u64 len,
dbfe2d
 	return 0;
dbfe2d
 }
dbfe2d
 
dbfe2d
+static int process_setflags(const char *path, int flags, void *user)
dbfe2d
+{
dbfe2d
+	int ret;
dbfe2d
+	struct btrfs_receive *rctx = user;
dbfe2d
+	char full_path[PATH_MAX];
dbfe2d
+
dbfe2d
+	ret = path_cat_out(full_path, rctx->full_subvol_path, path);
dbfe2d
+	if (ret < 0) {
dbfe2d
+		error("setflags: path invalid: %s", path);
dbfe2d
+		return ret;
dbfe2d
+	}
dbfe2d
+	ret = open_inode_for_write(rctx, full_path);
dbfe2d
+	if (ret < 0)
dbfe2d
+		return ret;
dbfe2d
+	ret = ioctl(rctx->write_fd, FS_IOC_SETFLAGS, &flags);
dbfe2d
+	if (ret < 0) {
dbfe2d
+		ret = -errno;
dbfe2d
+		error("setflags: setflags ioctl on %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
@@ -1308,6 +1332,7 @@ static struct btrfs_send_ops send_ops = {
dbfe2d
 	.update_extent = process_update_extent,
dbfe2d
 	.encoded_write = process_encoded_write,
dbfe2d
 	.fallocate = process_fallocate,
dbfe2d
+	.setflags = process_setflags,
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 2d0aa624..21295cbb 100644
dbfe2d
--- a/common/send-stream.c
dbfe2d
+++ b/common/send-stream.c
dbfe2d
@@ -374,6 +374,7 @@ static int read_and_process_cmd(struct btrfs_send_stream *sctx)
dbfe2d
 	int len;
dbfe2d
 	int xattr_len;
dbfe2d
 	int fallocate_mode;
dbfe2d
+	int setflags_flags;
dbfe2d
 
dbfe2d
 	ret = read_cmd(sctx);
dbfe2d
 	if (ret)
dbfe2d
@@ -546,8 +547,14 @@ static int read_and_process_cmd(struct btrfs_send_stream *sctx)
dbfe2d
 		ret = sctx->ops->fallocate(path, fallocate_mode, offset, tmp,
dbfe2d
 					   sctx->user);
dbfe2d
 		break;
dbfe2d
+	case BTRFS_SEND_C_SETFLAGS:
dbfe2d
+		TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
dbfe2d
+		TLV_GET_U32(sctx, BTRFS_SEND_A_SETFLAGS_FLAGS, &setflags_flags);
dbfe2d
+		ret = sctx->ops->setflags(path, setflags_flags, sctx->user);
dbfe2d
+		break;
dbfe2d
 	}
dbfe2d
 
dbfe2d
+
dbfe2d
 tlv_get_failed:
dbfe2d
 out:
dbfe2d
 	free(path);
dbfe2d
diff --git a/common/send-stream.h b/common/send-stream.h
dbfe2d
index 61a88d3d..3189f889 100644
dbfe2d
--- a/common/send-stream.h
dbfe2d
+++ b/common/send-stream.h
dbfe2d
@@ -59,6 +59,7 @@ struct btrfs_send_ops {
dbfe2d
 			     u32 encryption, void *user);
dbfe2d
 	int (*fallocate)(const char *path, int mode, u64 offset, u64 len,
dbfe2d
 			 void *user);
dbfe2d
+	int (*setflags)(const char *path, int flags, void *user);
dbfe2d
 };
dbfe2d
 
dbfe2d
 int btrfs_read_and_process_send_stream(int fd,
dbfe2d
-- 
dbfe2d
2.35.1
dbfe2d