Skip to contentSkip to conversation

Add user-space support for read-only snapshot creation.

PATCH New Andreas Philipp 1 participant 6 messages Apr 26, 2011 Btrfs file system

lore
Share & tools
lore
#0 lore raw

v2 cover Add user-space support for read-only snapshot creation.

Changelog v2:
- Fixed uninitialized variable (args).
- Corrected order of patches to make everything compile.
- Fixed some style errors.

There is kernel side support for the creation of read-only snapshots for some time now, but I did not find any patches for the userspace btrfs utilites. Thus I created this patchset which is tested and working with kernel version 2.6.39-rc2.

Andreas Philipp (5):
Added support for an additional ioctl.
Add support for read-only subvolumes.
Support the new parameters in do_clone(int argc, char** argv). Test the additional ioctl.
Updated manpage for btrfs subvolume snapshot.

 btrfs.c        |    6 +++---
 btrfs_cmds.c   |   48 ++++++++++++++++++++++++++++++++++++++++--------
 ioctl-test.c   |    1 +
 ioctl.h        |   14 ++++++++++++++
 man/btrfs.8.in |   11 ++++++-----
 5 files changed, 64 insertions(+), 16 deletions(-)
Signature · 1 line
1.7.4.1
#1 lore raw

PATCH v2 1/5 Added support for an additional ioctl.

Added BTRFS_IOC_SNAP_CREATE_V2 and struct btrfs_ioctl_vol_args_v2 as defined in fs/btrfs/ioctl.h in the kernel sources.

---
 ioctl.h |   14 ++++++++++++++
 1 files changed, 14 insertions(+), 0 deletions(-)
ioctl.h +14 -0
index 776d7a9..358f814 100644
@@ -30,6 +30,17 @@ struct btrfs_ioctl_vol_args {
 	char name[BTRFS_PATH_NAME_MAX + 1];
 };
 
+#define BTRFS_SUBVOL_RDONLY		(1ULL << 1)
+#define BTRFS_SUBVOL_NAME_MAX 4039
+
+struct btrfs_ioctl_vol_args_v2 {
+	__s64 fd;
+	__u64 transid;
+	__u64 flags;
+	__u64 unused[4];
+	char name[BTRFS_SUBVOL_NAME_MAX + 1];
+};
+
 struct btrfs_ioctl_search_key {
 	/* which root are we searching.  0 is the tree of tree roots */
 	__u64 tree_id;
@@ -132,6 +143,7 @@ struct btrfs_ioctl_space_args {
 	struct btrfs_ioctl_space_info spaces[0];
 };
 
+/* BTRFS_IOC_SNAP_CREATE is no longer used by the btrfs command */
 #define BTRFS_IOC_SNAP_CREATE _IOW(BTRFS_IOCTL_MAGIC, 1, \
 				   struct btrfs_ioctl_vol_args)
 #define BTRFS_IOC_DEFRAG _IOW(BTRFS_IOCTL_MAGIC, 2, \
@@ -169,4 +181,6 @@ struct btrfs_ioctl_space_args {
 #define BTRFS_IOC_DEFAULT_SUBVOL _IOW(BTRFS_IOCTL_MAGIC, 19, u64)
 #define BTRFS_IOC_SPACE_INFO _IOWR(BTRFS_IOCTL_MAGIC, 20, \
 				    struct btrfs_ioctl_space_args)
+#define BTRFS_IOC_SNAP_CREATE_V2 _IOW(BTRFS_IOCTL_MAGIC, 23, \
+				   struct btrfs_ioctl_vol_args_v2)
 #endif
Signature · 1 line
1.7.4.1
#2 lore raw

PATCH v2 2/5 Add support for read-only subvolumes.

In reply to Andreas Philipp

Use BTRFS_IOC_CREATE_SNAP_V2 instead of BTRFS_IOC_CREATE_SNAP and add an option for the creation of a readonly snapshot.

---
 btrfs_cmds.c |   48 ++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 40 insertions(+), 8 deletions(-)
btrfs_cmds.c +40 -8
index 8031c58..9367bac 100644
@@ -43,7 +43,7 @@
 
 #ifdef __CHECKER__
 #define BLKGETSIZE64 0
-#define BTRFS_IOC_SNAP_CREATE 0
+#define BTRFS_IOC_SNAP_CREATE_V2 0
 #define BTRFS_VOL_NAME_MAX 255
 struct btrfs_ioctl_vol_args { char name[BTRFS_VOL_NAME_MAX]; };
 static inline int ioctl(int fd, int define, void *arg) { return 0; }
@@ -310,13 +310,38 @@ int do_subvol_list(int argc, char **argv)
 int do_clone(int argc, char **argv)
 {
 	char	*subvol, *dst;
-	int	res, fd, fddst, len;
+	int	res, fd, fddst, len, optind = 0, readonly = 0;
 	char	*newname;
 	char	*dstdir;
+	struct btrfs_ioctl_vol_args_v2	args;
 
-	subvol = argv[1];
-	dst = argv[2];
-	struct btrfs_ioctl_vol_args	args;
+	memset(&args, 0, sizeof(args));
+
+	while (1) {
+		int c = getopt(argc, argv, "r");
+
+		if (c < 0)
+			break;
+		switch (c) {
+		case 'r':
+			optind++;
+			readonly = 1;
+			break;
+		default:
+			fprintf(stderr,
+				"Invalid arguments for subvolume snapshot\n");
+			free(argv);
+			return 1;
+		}
+	}
+	if (argc - optind < 2) {
+		fprintf(stderr, "Invalid arguments for subvolume snapshot\n");
+		free(argv);
+		return 1;
+	}
+
+	subvol = argv[optind+1];
+	dst = argv[optind+2];
 
 	res = test_issubvolume(subvol);
 	if(res<0){
@@ -372,11 +397,18 @@ int do_clone(int argc, char **argv)
 		return 12;
 	}
 
-	printf("Create a snapshot of '%s' in '%s/%s'\n",
-	       subvol, dstdir, newname);
+	if (readonly) {
+		args.flags |= BTRFS_SUBVOL_RDONLY;
+		printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
+		       subvol, dstdir, newname);
+	} else {
+		printf("Create a snapshot of '%s' in '%s/%s'\n",
+		       subvol, dstdir, newname);
+	}
+
 	args.fd = fd;
 	strcpy(args.name, newname);
-	res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE, &args);
+	res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
 
 	close(fd);
 	close(fddst);
Signature · 1 line
1.7.4.1
#3 lore raw

PATCH v2 3/5 Support the new parameters in do_clone(int argc, char** argv).

In reply to Andreas Philipp

Now 'btrfs subvolume snapshot' takes not two but only at least two parameters. Additionally, the help message is updated accordingly.

---
 btrfs.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
btrfs.c +3 -3
index 46314cf..f70d64b 100644
@@ -44,9 +44,9 @@ static struct Command commands[] = {
 	/*
 		avoid short commands different for the case only
 	*/
-	{ do_clone, 2,
-	  "subvolume snapshot", "<source> [<dest>/]<name>\n"
-		"Create a writable snapshot of the subvolume <source> with\n"
+	{ do_clone, -2,
+	  "subvolume snapshot", "[-r] <source> [<dest>/]<name>\n"
+		"Create a writable/readonly snapshot of the subvolume <source> with\n"
 		"the name <name> in the <dest> directory."
 	},
 	{ do_delete_subvolume, 1,
Signature · 1 line
1.7.4.1
#4 lore raw

PATCH v2 4/5 Test the additional ioctl.

In reply to Andreas Philipp
---
 ioctl-test.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
ioctl-test.c +1 -0
index 7cf3bc2..1c27d61 100644
@@ -22,6 +22,7 @@ unsigned long ioctls[] = {
 	BTRFS_IOC_INO_LOOKUP,
 	BTRFS_IOC_DEFAULT_SUBVOL,
 	BTRFS_IOC_SPACE_INFO,
+	BTRFS_IOC_SNAP_CREATE_V2,
 	0 };
 
 int main(int ac, char **av)
Signature · 1 line
1.7.4.1
#5 lore raw

PATCH v2 5/5 Updated manpage for btrfs subvolume snapshot.

In reply to Andreas Philipp
---
 man/btrfs.8.in |   11 ++++++-----
 1 files changed, 6 insertions(+), 5 deletions(-)
man/btrfs.8.in +6 -5
index 26ef982..b59bc6f 100644
@@ -5,7 +5,7 @@
 .SH NAME
 btrfs \- control a btrfs filesystem
 .SH SYNOPSIS
-\fBbtrfs\fP \fBsubvolume snapshot\fP\fI <source> [<dest>/]<name>\fP
+\fBbtrfs\fP \fBsubvolume snapshot\fP\fI [-r] <source> [<dest>/]<name>\fP
 .PP
 \fBbtrfs\fP \fBsubvolume delete\fP\fI <subvolume>\fP
 .PP
@@ -70,10 +70,11 @@ command.
 .SH COMMANDS
 .TP
 
-\fBsubvolume snapshot\fR\fI <source> [<dest>/]<name>\fR
-Create a writable snapshot of the subvolume \fI<source>\fR with the name
-\fI<name>\fR in the \fI<dest>\fR directory. If \fI<source>\fR is not a
-subvolume, \fBbtrfs\fR returns an error.
+\fBsubvolume snapshot\fR\fI [-r] <source> [<dest>/]<name>\fR
+Create a writable/readonly snapshot of the subvolume \fI<source>\fR with the
+name \fI<name>\fR in the \fI<dest>\fR directory. If \fI<source>\fR is not a
+subvolume, \fBbtrfs\fR returns an error. If \fI-r\fR is given, the snapshot
+will be readonly.
 .TP
 
 \fBsubvolume delete\fR\fI <subvolume>\fR
Signature · 1 line
1.7.4.1