Skip to content

Commit a8bfd4a

Browse files
pibrochJosef Bacik
authored andcommitted
Btrfs: set/change the label of a mounted file system
With this new ioctl(2) BTRFS_IOC_SET_FSLABEL, we can set/change the label of a mounted file system. Signed-off-by: Jie Liu <jeff.liu@oracle.com> Signed-off-by: Anand Jain <anand.jain@oracle.com> Reviewed-by: Miao Xie <miaox@cn.fujitsu.com> Reviewed-by: Goffredo Baroncelli <kreijack@inwind.it> Reviewed-by: David Sterba <dsterba@suse.cz> Reviewed-by: Goffredo Baroncelli <kreijack@inwind.it> Signed-off-by: Josef Bacik <jbacik@fusionio.com>
1 parent 867ab66 commit a8bfd4a

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

‎fs/btrfs/ioctl.c‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3936,6 +3936,46 @@ static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
39363936
return ret ? -EFAULT : 0;
39373937
}
39383938

3939+
static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
3940+
{
3941+
struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3942+
struct btrfs_super_block *super_block = root->fs_info->super_copy;
3943+
struct btrfs_trans_handle *trans;
3944+
char label[BTRFS_LABEL_SIZE];
3945+
int ret;
3946+
3947+
if (!capable(CAP_SYS_ADMIN))
3948+
return -EPERM;
3949+
3950+
if (copy_from_user(label, arg, sizeof(label)))
3951+
return -EFAULT;
3952+
3953+
if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
3954+
pr_err("btrfs: unable to set label with more than %d bytes\n",
3955+
BTRFS_LABEL_SIZE - 1);
3956+
return -EINVAL;
3957+
}
3958+
3959+
ret = mnt_want_write_file(file);
3960+
if (ret)
3961+
return ret;
3962+
3963+
mutex_lock(&root->fs_info->volume_mutex);
3964+
trans = btrfs_start_transaction(root, 0);
3965+
if (IS_ERR(trans)) {
3966+
ret = PTR_ERR(trans);
3967+
goto out_unlock;
3968+
}
3969+
3970+
strcpy(super_block->label, label);
3971+
ret = btrfs_end_transaction(trans, root);
3972+
3973+
out_unlock:
3974+
mutex_unlock(&root->fs_info->volume_mutex);
3975+
mnt_drop_write_file(file);
3976+
return ret;
3977+
}
3978+
39393979
long btrfs_ioctl(struct file *file, unsigned int
39403980
cmd, unsigned long arg)
39413981
{
@@ -4038,6 +4078,8 @@ long btrfs_ioctl(struct file *file, unsigned int
40384078
return btrfs_ioctl_dev_replace(root, argp);
40394079
case BTRFS_IOC_GET_FSLABEL:
40404080
return btrfs_ioctl_get_fslabel(file, argp);
4081+
case BTRFS_IOC_SET_FSLABEL:
4082+
return btrfs_ioctl_set_fslabel(file, argp);
40414083
}
40424084

40434085
return -ENOTTY;

‎include/uapi/linux/btrfs.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,8 @@ struct btrfs_ioctl_send_args {
504504
struct btrfs_ioctl_qgroup_limit_args)
505505
#define BTRFS_IOC_GET_FSLABEL _IOR(BTRFS_IOCTL_MAGIC, 49, \
506506
char[BTRFS_LABEL_SIZE])
507+
#define BTRFS_IOC_SET_FSLABEL _IOW(BTRFS_IOCTL_MAGIC, 50, \
508+
char[BTRFS_LABEL_SIZE])
507509
#define BTRFS_IOC_GET_DEV_STATS _IOWR(BTRFS_IOCTL_MAGIC, 52, \
508510
struct btrfs_ioctl_get_dev_stats)
509511
#define BTRFS_IOC_DEV_REPLACE _IOWR(BTRFS_IOCTL_MAGIC, 53, \

0 commit comments

Comments
 (0)