diff mbox

[2/3] Btrfs-progs: add attribute label for subvol and snapshot

Message ID 1354290482-5324-3-git-send-email-Anand.Jain@oracle.com (mailing list archive)
State New, archived
Headers show

Commit Message

Anand Jain Nov. 30, 2012, 3:48 p.m. UTC
From: Anand Jain <anand.jain@oracle.com>

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 btrfslabel.c |   38 ++++++++++++++++++++++++++++++++++++++
 btrfslabel.h |    4 +++-
 ctree.h      |    1 +
 3 files changed, 42 insertions(+), 1 deletions(-)
diff mbox

Patch

diff --git a/btrfslabel.c b/btrfslabel.c
index cb142b0..8c424e5 100644
--- a/btrfslabel.c
+++ b/btrfslabel.c
@@ -34,6 +34,8 @@ 
 #include <linux/fs.h>
 #include <linux/limits.h>
 #include <ctype.h>
+#include <attr/xattr.h>
+#include <attr/attributes.h>
 #include "kerncompat.h"
 #include "ctree.h"
 #include "utils.h"
@@ -126,3 +128,39 @@  int set_label(char *btrfs_dev, char *nLabel)
 	change_label_unmounted(btrfs_dev, nLabel);
 	return 0;
 }
+
+int get_subvol_label(char *subvol, char *labelp)
+{
+	int ret;
+	ret = getxattr(subvol, "user.label", labelp, BTRFS_SUBVOL_LABEL_SIZE);
+	if(ret < 0) {
+		labelp = "";
+		fprintf(stderr, "ERROR: get subvol label failed, %s\n",
+			strerror(errno));
+		return -errno;
+	}
+	labelp[ret] = '\0';
+	return 0;
+}
+
+int set_subvol_label(char *subvol, char *labelp)
+{
+	int e=0;
+	char label[BTRFS_SUBVOL_LABEL_SIZE+1];
+
+	if (strlen(labelp) > BTRFS_SUBVOL_LABEL_SIZE) {
+		fprintf(stderr, "ERROR: Subvol label is more than max length %d\n",
+			BTRFS_SUBVOL_LABEL_SIZE);
+		return -1;
+	}
+	memset(label, 0, BTRFS_SUBVOL_LABEL_SIZE+1);
+	strcpy(label, labelp);
+	if(setxattr(subvol, "user.label", label, BTRFS_SUBVOL_LABEL_SIZE,
+		ATTR_ROOT) == -1) {
+		e = errno;
+		fprintf(stderr, "ERROR: set subvol label failed, %s\n",
+			strerror(e));
+		return -e;
+	}
+	return 0;
+}
diff --git a/btrfslabel.h b/btrfslabel.h
index abf43ad..1abc483 100644
--- a/btrfslabel.h
+++ b/btrfslabel.h
@@ -2,4 +2,6 @@ 
 
 
 int get_label(char *btrfs_dev);
-int set_label(char *btrfs_dev, char *nLabel);
\ No newline at end of file
+int set_label(char *btrfs_dev, char *nLabel);
+int get_subvol_label(char *subvol, char *labelp);
+int set_subvol_label(char *subvol, char *labelp);
diff --git a/ctree.h b/ctree.h
index 293b24f..993dbcf 100644
--- a/ctree.h
+++ b/ctree.h
@@ -325,6 +325,7 @@  struct btrfs_header {
  */
 #define BTRFS_SYSTEM_CHUNK_ARRAY_SIZE 2048
 #define BTRFS_LABEL_SIZE 256
+#define BTRFS_SUBVOL_LABEL_SIZE 32
 
 /*
  * just in case we somehow lose the roots and are not able to mount,