linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Anand jain <Anand.Jain@oracle.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH] Btrfs-progs: Add get/set label ioctl
Date: Wed, 29 Aug 2012 16:46:01 +0800	[thread overview]
Message-ID: <1346229961-635-2-git-send-email-Anand.Jain@oracle.com> (raw)
In-Reply-To: <1346229961-635-1-git-send-email-Anand.Jain@oracle.com>

From: Anand Jain <anand.jain@oracle.com>

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 btrfslabel.c |   90 ++++++++++++++++++++++++++++++++++++++--------------------
 ioctl.h      |    2 +
 utils.h      |    1 +
 3 files changed, 62 insertions(+), 31 deletions(-)

diff --git a/btrfslabel.c b/btrfslabel.c
index bf73802..3676db0 100644
--- a/btrfslabel.c
+++ b/btrfslabel.c
@@ -51,6 +51,10 @@ static void change_label_unmounted(char *dev, char *nLabel)
        struct btrfs_root *root;
        struct btrfs_trans_handle *trans;
 
+       if(check_mounted(dev)) {
+         fprintf(stderr, "ERROR: dev is mounted, use mount point\n");
+         return;
+       }
        /* Open the super_block at the default location
         * and as read-write.
         */
@@ -67,10 +71,57 @@ static void change_label_unmounted(char *dev, char *nLabel)
        close_ctree(root);
 }
 
+static void set_fs_label(char *mnt, char *label)
+{
+	int fd, e=0;
+	char fslabel[BTRFS_LABEL_SIZE+1];
+
+	memset(fslabel, 0, BTRFS_LABEL_SIZE+1);
+	strncpy(fslabel,label,BTRFS_LABEL_SIZE);
+
+	fd = open(mnt, O_RDONLY| O_NOATIME);
+	if (fd < 0) {
+		fprintf(stderr, "ERROR: Open %s failed\n", mnt);
+		return;
+	}
+
+	if(ioctl(fd, BTRFS_IOC_SET_LABEL, fslabel) < 0) {
+		e = errno;
+		fprintf(stderr, "ERROR: get label failed, %s\n",
+			strerror(e));
+	}
+	close(fd);
+}
+
+static void get_fs_label(char *path)
+{
+	int fd, e=0;
+	char label[BTRFS_LABEL_SIZE+1];
+
+	fd = open(path, O_RDONLY| O_NOATIME);
+	if (fd < 0) {
+		fprintf(stderr, "ERROR: Open %s failed\n", path);
+		return;
+	}
+
+	if(ioctl(fd, BTRFS_IOC_GET_LABEL, label) < 0) {
+		e = errno;
+		fprintf(stderr, "ERROR: get label failed, %s\n",
+			strerror(e));
+		return;
+	}
+	printf("%s\n",label);
+	close(fd);
+}
+
 static void get_label_unmounted(char *dev)
 {
        struct btrfs_root *root;
 
+       if(check_mounted(dev)) {
+         fprintf(stderr, "ERROR: dev is mounted, use mount point\n");
+         return;
+       }
        /* Open the super_block at the default location
         * and as read-only.
         */
@@ -84,41 +135,18 @@ static void get_label_unmounted(char *dev)
 
 int get_label(char *btrfs_dev)
 {
-
-	int ret;
-	ret = check_mounted(btrfs_dev);
-	if (ret < 0)
-	{
-	       fprintf(stderr, "FATAL: error checking %s mount status\n", btrfs_dev);
-	       return -1;
-	}
-
-	if(ret != 0)
-	{
-	       fprintf(stderr, "FATAL: the filesystem has to be unmounted\n");
-	       return -2;
-	}
-	get_label_unmounted(btrfs_dev);
+	if(is_existing_blk_or_reg_file(btrfs_dev))
+		get_label_unmounted(btrfs_dev);
+	else
+		get_fs_label(btrfs_dev);
 	return 0;
 }
 
-
 int set_label(char *btrfs_dev, char *nLabel)
 {
-
-	int ret;
-	ret = check_mounted(btrfs_dev);
-	if (ret < 0)
-	{
-	       fprintf(stderr, "FATAL: error checking %s mount status\n", btrfs_dev);
-	       return -1;
-	}
-
-	if(ret != 0)
-	{
-	       fprintf(stderr, "FATAL: the filesystem has to be unmounted\n");
-	       return -2;
-	}
-	change_label_unmounted(btrfs_dev, nLabel);
+	if(is_existing_blk_or_reg_file(btrfs_dev))
+		change_label_unmounted(btrfs_dev, nLabel);
+	else
+		set_fs_label(btrfs_dev, nLabel);
 	return 0;
 }
diff --git a/ioctl.h b/ioctl.h
index d6f3d07..7e1dcda 100644
--- a/ioctl.h
+++ b/ioctl.h
@@ -370,4 +370,6 @@ struct btrfs_ioctl_clone_range_args {
 				struct btrfs_ioctl_received_subvol_args)
 #define BTRFS_IOC_SEND _IOW(BTRFS_IOCTL_MAGIC, 38, struct btrfs_ioctl_send_args)
 
+#define BTRFS_IOC_GET_LABEL _IOR(BTRFS_IOCTL_MAGIC, 53, __u64)
+#define BTRFS_IOC_SET_LABEL _IOW(BTRFS_IOCTL_MAGIC, 54, __u64)
 #endif
diff --git a/utils.h b/utils.h
index c147c12..ba088fe 100644
--- a/utils.h
+++ b/utils.h
@@ -48,4 +48,5 @@ int check_label(char *input);
 int get_mountpt(char *dev, char *mntpt, size_t size);
 
 int btrfs_scan_block_devices(int run_ioctl);
+int is_existing_blk_or_reg_file(const char* filename);
 #endif
-- 
1.7.1


  reply	other threads:[~2012-08-29  8:44 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-29  8:46 [PATCH] Btrfs: Add get/set label ioctl Anand jain
2012-08-29  8:46 ` Anand jain [this message]
2012-08-30 18:27   ` [PATCH] Btrfs-progs: " Goffredo Baroncelli
2012-09-14  6:03     ` [PATCH 1/1] Btrfs-progs: Update btrfs man page to indicate label for a mounted fs can be changed Anand jain
2012-08-29  9:00 ` [PATCH] Btrfs: Add get/set label ioctl Jie Liu
2012-08-30  1:54   ` Anand Jain
2012-08-30  5:44     ` Anand Jain
2012-08-30  5:55       ` Jie Liu
2012-08-30  6:04         ` Jie Liu
2012-08-30  6:28         ` Anand Jain
2012-08-30  7:22           ` Jie Liu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1346229961-635-2-git-send-email-Anand.Jain@oracle.com \
    --to=anand.jain@oracle.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).