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 v4] Btrfs: ability to add label to snapshot and subvol
Date: Mon, 25 Feb 2013 13:31:45 +0800	[thread overview]
Message-ID: <1361770305-1206-2-git-send-email-anand.jain@oracle.com> (raw)
In-Reply-To: <1361770305-1206-1-git-send-email-anand.jain@oracle.com>

Generally snapshots are machine generated, so at any point in time
if a sysadmin looks at a list of snapshots there should be some
info about the snapshots to indicate purpose of it being created.
With this patch and along with the corresponding btrfs-progs patch
a 32byte info can be added to the snapshots/subvol.

Further, ioctl is preferred over the attribute to write the label
since btrfs-progs is not only the application which might be
interacting with the subvol to write the label, for example
btrfs-gui might as well write the label, so that needs an
additional efforts to maintain a consistent keyword across the
applications is difficult in the long run.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 fs/btrfs/ctree.h           | 12 +++++++++-
 fs/btrfs/ioctl.c           | 58 ++++++++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/transaction.c     |  1 +
 include/uapi/linux/btrfs.h |  2 ++
 4 files changed, 72 insertions(+), 1 deletion(-)

diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 1679051..6b527bc 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -392,6 +392,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,
@@ -769,7 +770,8 @@ struct btrfs_root_item {
 	struct btrfs_timespec otime;
 	struct btrfs_timespec stime;
 	struct btrfs_timespec rtime;
-	__le64 reserved[8]; /* for future */
+	char label[BTRFS_SUBVOL_LABEL_SIZE];
+	__le64 reserved[4]; /* for future */
 } __attribute__ ((__packed__));
 
 /*
@@ -2546,6 +2548,14 @@ static inline bool btrfs_root_readonly(struct btrfs_root *root)
 {
 	return (root->root_item.flags & cpu_to_le64(BTRFS_ROOT_SUBVOL_RDONLY)) != 0;
 }
+static inline char * btrfs_root_label(struct btrfs_root_item *root_item)
+{
+	return (root_item->label);
+}
+static inline void btrfs_root_set_label(struct btrfs_root_item *root_item, char *val)
+{
+	memcpy(root_item->label, val, BTRFS_SUBVOL_LABEL_SIZE);
+}
 
 /* struct btrfs_root_backup */
 BTRFS_SETGET_STACK_FUNCS(backup_tree_root, struct btrfs_root_backup,
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 2bbbed5..211b319 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -3982,6 +3982,60 @@ out_unlock:
 	return ret;
 }
 
+static int btrfs_ioctl_subvol_getlabel(struct btrfs_root *root,
+						void __user *arg)
+{
+	char *label;
+
+	spin_lock(&root->root_item_lock);
+	label = btrfs_root_label(&root->root_item);
+	if (copy_to_user(arg, label, BTRFS_SUBVOL_LABEL_SIZE)) {
+		spin_unlock(&root->root_item_lock);
+		return -EFAULT;
+	}
+	spin_unlock(&root->root_item_lock);
+	return 0;
+}
+
+static int btrfs_ioctl_subvol_setlabel(struct file *file,
+						void __user *arg)
+{
+	char label[BTRFS_SUBVOL_LABEL_SIZE+1];
+	struct btrfs_trans_handle *trans;
+	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
+	struct inode *inode = file->f_path.dentry->d_inode;
+	int ret;
+
+	if (btrfs_root_readonly(root))
+		return -EROFS;
+
+	if (!inode_owner_or_capable(inode))
+		return -EACCES;
+
+	ret = mnt_want_write_file(file);
+	if (ret)
+		return ret;
+
+	if (copy_from_user(label, arg, BTRFS_SUBVOL_LABEL_SIZE)) {
+		ret = -EFAULT;
+		goto out;
+	}
+
+	trans = btrfs_join_transaction(root);
+	if (IS_ERR(trans)) {
+		ret = PTR_ERR(trans);
+		goto out;
+	}
+	spin_lock(&root->root_item_lock);
+	btrfs_root_set_label(&root->root_item, label);
+	spin_unlock(&root->root_item_lock);
+	btrfs_end_transaction(trans, root);
+
+out:
+	mnt_drop_write_file(file);
+	return ret;
+}
+
 long btrfs_ioctl(struct file *file, unsigned int
 		cmd, unsigned long arg)
 {
@@ -4086,6 +4140,10 @@ long btrfs_ioctl(struct file *file, unsigned int
 		return btrfs_ioctl_get_fslabel(file, argp);
 	case BTRFS_IOC_SET_FSLABEL:
 		return btrfs_ioctl_set_fslabel(file, argp);
+	case BTRFS_IOC_SUBVOL_GETLABEL:
+		return btrfs_ioctl_subvol_getlabel(root, argp);
+	case BTRFS_IOC_SUBVOL_SETLABEL:
+		return btrfs_ioctl_subvol_setlabel(file, argp);
 	}
 
 	return -ENOTTY;
diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
index 955204c..955097e 100644
--- a/fs/btrfs/transaction.c
+++ b/fs/btrfs/transaction.c
@@ -1181,6 +1181,7 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
 	memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
 	btrfs_set_root_stransid(new_root_item, 0);
 	btrfs_set_root_rtransid(new_root_item, 0);
+	memset(&new_root_item->label, 0, BTRFS_SUBVOL_LABEL_SIZE);
 
 	old = btrfs_lock_root_node(root);
 	ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
diff --git a/include/uapi/linux/btrfs.h b/include/uapi/linux/btrfs.h
index fa3a5f9..38d0f56 100644
--- a/include/uapi/linux/btrfs.h
+++ b/include/uapi/linux/btrfs.h
@@ -510,5 +510,7 @@ struct btrfs_ioctl_send_args {
 				      struct btrfs_ioctl_get_dev_stats)
 #define BTRFS_IOC_DEV_REPLACE _IOWR(BTRFS_IOCTL_MAGIC, 53, \
 				    struct btrfs_ioctl_dev_replace_args)
+#define BTRFS_IOC_SUBVOL_GETLABEL _IOWR(BTRFS_IOCTL_MAGIC, 55, __u64)
+#define BTRFS_IOC_SUBVOL_SETLABEL _IOW(BTRFS_IOCTL_MAGIC, 56, __u64)
 
 #endif /* _UAPI_LINUX_BTRFS_H */
-- 
1.8.1.227.g44fe835


  reply	other threads:[~2013-02-25  5:31 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-11-01 10:46 [Request for review] [RFC] Add label support for snapshots and subvols Anand jain
2012-11-01 10:46 ` [PATCH 1/2] Btrfs-progs: move open_file_or_dir() to utils.c Anand jain
2012-11-01 10:46 ` [PATCH 2/2] Btrfs-progs: add feature to label subvol and snapshot Anand jain
2012-11-01 10:46 ` [PATCH] Btrfs: add label to snapshot and subvol Anand jain
2012-11-05  7:39   ` Jan Schmidt
2012-11-16  5:15     ` Anand Jain
2012-11-01 22:16 ` [Request for review] [RFC] Add label support for snapshots and subvols cwillu
2012-11-01 22:28   ` Fajar A. Nugraha
2012-11-01 22:32     ` Hugo Mills
2012-11-01 22:49       ` Fajar A. Nugraha
2012-11-05  7:24         ` Anand Jain
2012-11-05 23:22           ` David Sterba
2012-11-01 23:04     ` Goffredo Baroncelli
2012-11-16  4:52 ` [Request for review v2] " Anand jain
2012-11-16  4:52   ` [PATCH 1/3] Btrfs-progs: move open_file_or_dir() to utils.c Anand jain
2012-11-16  4:52   ` [PATCH v2] Btrfs: add label to snapshot and subvol Anand jain
2012-11-16  6:33     ` Miao Xie
2012-11-27 18:26       ` Anand Jain
2012-11-16  4:52   ` [PATCH 2/3] Btrfs-progs: add feature to label subvol and snapshot Anand jain
2012-11-16  4:52   ` [PATCH 3/3] Btrfs-progs: cmd option to show or set the subvol label Anand jain
2012-11-27 18:29 ` [Request for review v3] [RFC] Add label support for snapshots and subvols Anand jain
2012-11-27 18:29   ` [PATCH v3] Btrfs: add label to snapshot and subvol Anand jain
2012-11-28  3:05     ` Miao Xie
2012-11-28 10:02       ` Anand Jain
2012-11-28 10:25         ` Miao Xie
2012-11-28 12:17           ` Anand Jain
2012-11-28 12:15     ` Anand jain
2012-11-27 18:29   ` [PATCH 1/3] Btrfs-progs: move open_file_or_dir() to utils.c Anand jain
2012-11-27 18:29   ` [PATCH 2/3] Btrfs-progs: add feature to label subvol and snapshot Anand jain
2012-11-27 18:29   ` [PATCH 3/3] Btrfs-progs: cmd option to show or set the subvol label Anand jain
2012-11-30 15:47 ` [PATCH 0/3] Add subvol and snapshot attribute label Anand jain
2012-11-30 15:48   ` [PATCH 1/3] Btrfs-progs: move open_file_or_dir() to utils.c Anand jain
2012-11-30 15:48   ` [PATCH 2/3] Btrfs-progs: add attribute label for subvol and snapshot Anand jain
2012-11-30 15:48   ` [PATCH 3/3] Btrfs-progs: cmd option to show or set the subvol label Anand jain
2013-02-25  5:31 ` [PATCH 0/2 v4] Btrfs-progs: Add support for " Anand Jain
2013-02-25  5:31   ` [PATCH 1/2 v4] Btrfs-progs: add feature to label subvol and snapshot Anand Jain
2013-02-25  5:31   ` [PATCH 2/2 v4] Btrfs-progs: cmd option to show or set the subvol label Anand Jain
2013-02-25  5:31 ` [PATCH v4] Btrfs: Add support for " Anand Jain
2013-02-25  5:31   ` Anand Jain [this message]
2013-02-25 20:45     ` [PATCH v4] Btrfs: ability to add label to snapshot and subvol Hugo Mills
2013-03-01 10:36       ` Anand Jain

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=1361770305-1206-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).