aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/ioctl.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs/ioctl.c')
-rw-r--r--fs/btrfs/ioctl.c199
1 files changed, 150 insertions, 49 deletions
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index b6985d33eede..a506a22b522a 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -147,6 +147,9 @@ static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
147 unsigned int flags, oldflags; 147 unsigned int flags, oldflags;
148 int ret; 148 int ret;
149 149
150 if (btrfs_root_readonly(root))
151 return -EROFS;
152
150 if (copy_from_user(&flags, arg, sizeof(flags))) 153 if (copy_from_user(&flags, arg, sizeof(flags)))
151 return -EFAULT; 154 return -EFAULT;
152 155
@@ -360,7 +363,8 @@ fail:
360} 363}
361 364
362static int create_snapshot(struct btrfs_root *root, struct dentry *dentry, 365static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
363 char *name, int namelen, u64 *async_transid) 366 char *name, int namelen, u64 *async_transid,
367 bool readonly)
364{ 368{
365 struct inode *inode; 369 struct inode *inode;
366 struct dentry *parent; 370 struct dentry *parent;
@@ -378,6 +382,7 @@ static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
378 btrfs_init_block_rsv(&pending_snapshot->block_rsv); 382 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
379 pending_snapshot->dentry = dentry; 383 pending_snapshot->dentry = dentry;
380 pending_snapshot->root = root; 384 pending_snapshot->root = root;
385 pending_snapshot->readonly = readonly;
381 386
382 trans = btrfs_start_transaction(root->fs_info->extent_root, 5); 387 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
383 if (IS_ERR(trans)) { 388 if (IS_ERR(trans)) {
@@ -509,7 +514,7 @@ static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
509static noinline int btrfs_mksubvol(struct path *parent, 514static noinline int btrfs_mksubvol(struct path *parent,
510 char *name, int namelen, 515 char *name, int namelen,
511 struct btrfs_root *snap_src, 516 struct btrfs_root *snap_src,
512 u64 *async_transid) 517 u64 *async_transid, bool readonly)
513{ 518{
514 struct inode *dir = parent->dentry->d_inode; 519 struct inode *dir = parent->dentry->d_inode;
515 struct dentry *dentry; 520 struct dentry *dentry;
@@ -541,7 +546,7 @@ static noinline int btrfs_mksubvol(struct path *parent,
541 546
542 if (snap_src) { 547 if (snap_src) {
543 error = create_snapshot(snap_src, dentry, 548 error = create_snapshot(snap_src, dentry,
544 name, namelen, async_transid); 549 name, namelen, async_transid, readonly);
545 } else { 550 } else {
546 error = create_subvol(BTRFS_I(dir)->root, dentry, 551 error = create_subvol(BTRFS_I(dir)->root, dentry,
547 name, namelen, async_transid); 552 name, namelen, async_transid);
@@ -918,7 +923,8 @@ static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
918 char *name, 923 char *name,
919 unsigned long fd, 924 unsigned long fd,
920 int subvol, 925 int subvol,
921 u64 *transid) 926 u64 *transid,
927 bool readonly)
922{ 928{
923 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; 929 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
924 struct file *src_file; 930 struct file *src_file;
@@ -936,7 +942,7 @@ static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
936 942
937 if (subvol) { 943 if (subvol) {
938 ret = btrfs_mksubvol(&file->f_path, name, namelen, 944 ret = btrfs_mksubvol(&file->f_path, name, namelen,
939 NULL, transid); 945 NULL, transid, readonly);
940 } else { 946 } else {
941 struct inode *src_inode; 947 struct inode *src_inode;
942 src_file = fget(fd); 948 src_file = fget(fd);
@@ -955,7 +961,7 @@ static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
955 } 961 }
956 ret = btrfs_mksubvol(&file->f_path, name, namelen, 962 ret = btrfs_mksubvol(&file->f_path, name, namelen,
957 BTRFS_I(src_inode)->root, 963 BTRFS_I(src_inode)->root,
958 transid); 964 transid, readonly);
959 fput(src_file); 965 fput(src_file);
960 } 966 }
961out: 967out:
@@ -963,58 +969,139 @@ out:
963} 969}
964 970
965static noinline int btrfs_ioctl_snap_create(struct file *file, 971static noinline int btrfs_ioctl_snap_create(struct file *file,
966 void __user *arg, int subvol, 972 void __user *arg, int subvol)
967 int v2)
968{ 973{
969 struct btrfs_ioctl_vol_args *vol_args = NULL; 974 struct btrfs_ioctl_vol_args *vol_args;
970 struct btrfs_ioctl_vol_args_v2 *vol_args_v2 = NULL;
971 char *name;
972 u64 fd;
973 int ret; 975 int ret;
974 976
975 if (v2) { 977 vol_args = memdup_user(arg, sizeof(*vol_args));
976 u64 transid = 0; 978 if (IS_ERR(vol_args))
977 u64 *ptr = NULL; 979 return PTR_ERR(vol_args);
978 980 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
979 vol_args_v2 = memdup_user(arg, sizeof(*vol_args_v2));
980 if (IS_ERR(vol_args_v2))
981 return PTR_ERR(vol_args_v2);
982 981
983 if (vol_args_v2->flags & ~BTRFS_SUBVOL_CREATE_ASYNC) { 982 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
984 ret = -EINVAL; 983 vol_args->fd, subvol,
985 goto out; 984 NULL, false);
986 }
987 985
988 name = vol_args_v2->name; 986 kfree(vol_args);
989 fd = vol_args_v2->fd; 987 return ret;
990 vol_args_v2->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; 988}
991 989
992 if (vol_args_v2->flags & BTRFS_SUBVOL_CREATE_ASYNC) 990static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
993 ptr = &transid; 991 void __user *arg, int subvol)
992{
993 struct btrfs_ioctl_vol_args_v2 *vol_args;
994 int ret;
995 u64 transid = 0;
996 u64 *ptr = NULL;
997 bool readonly = false;
994 998
995 ret = btrfs_ioctl_snap_create_transid(file, name, fd, 999 vol_args = memdup_user(arg, sizeof(*vol_args));
996 subvol, ptr); 1000 if (IS_ERR(vol_args))
1001 return PTR_ERR(vol_args);
1002 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
997 1003
998 if (ret == 0 && ptr && 1004 if (vol_args->flags &
999 copy_to_user(arg + 1005 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1000 offsetof(struct btrfs_ioctl_vol_args_v2, 1006 ret = -EOPNOTSUPP;
1001 transid), ptr, sizeof(*ptr))) 1007 goto out;
1002 ret = -EFAULT;
1003 } else {
1004 vol_args = memdup_user(arg, sizeof(*vol_args));
1005 if (IS_ERR(vol_args))
1006 return PTR_ERR(vol_args);
1007 name = vol_args->name;
1008 fd = vol_args->fd;
1009 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1010
1011 ret = btrfs_ioctl_snap_create_transid(file, name, fd,
1012 subvol, NULL);
1013 } 1008 }
1009
1010 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1011 ptr = &transid;
1012 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1013 readonly = true;
1014
1015 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1016 vol_args->fd, subvol,
1017 ptr, readonly);
1018
1019 if (ret == 0 && ptr &&
1020 copy_to_user(arg +
1021 offsetof(struct btrfs_ioctl_vol_args_v2,
1022 transid), ptr, sizeof(*ptr)))
1023 ret = -EFAULT;
1014out: 1024out:
1015 kfree(vol_args); 1025 kfree(vol_args);
1016 kfree(vol_args_v2); 1026 return ret;
1027}
1028
1029static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1030 void __user *arg)
1031{
1032 struct inode *inode = fdentry(file)->d_inode;
1033 struct btrfs_root *root = BTRFS_I(inode)->root;
1034 int ret = 0;
1035 u64 flags = 0;
1036
1037 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1038 return -EINVAL;
1039
1040 down_read(&root->fs_info->subvol_sem);
1041 if (btrfs_root_readonly(root))
1042 flags |= BTRFS_SUBVOL_RDONLY;
1043 up_read(&root->fs_info->subvol_sem);
1044
1045 if (copy_to_user(arg, &flags, sizeof(flags)))
1046 ret = -EFAULT;
1047
1048 return ret;
1049}
1050
1051static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1052 void __user *arg)
1053{
1054 struct inode *inode = fdentry(file)->d_inode;
1055 struct btrfs_root *root = BTRFS_I(inode)->root;
1056 struct btrfs_trans_handle *trans;
1057 u64 root_flags;
1058 u64 flags;
1059 int ret = 0;
1060
1061 if (root->fs_info->sb->s_flags & MS_RDONLY)
1062 return -EROFS;
1063
1064 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1065 return -EINVAL;
1066
1067 if (copy_from_user(&flags, arg, sizeof(flags)))
1068 return -EFAULT;
1069
1070 if (flags & ~BTRFS_SUBVOL_CREATE_ASYNC)
1071 return -EINVAL;
1072
1073 if (flags & ~BTRFS_SUBVOL_RDONLY)
1074 return -EOPNOTSUPP;
1075
1076 down_write(&root->fs_info->subvol_sem);
1077
1078 /* nothing to do */
1079 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1080 goto out;
1081
1082 root_flags = btrfs_root_flags(&root->root_item);
1083 if (flags & BTRFS_SUBVOL_RDONLY)
1084 btrfs_set_root_flags(&root->root_item,
1085 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1086 else
1087 btrfs_set_root_flags(&root->root_item,
1088 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1089
1090 trans = btrfs_start_transaction(root, 1);
1091 if (IS_ERR(trans)) {
1092 ret = PTR_ERR(trans);
1093 goto out_reset;
1094 }
1017 1095
1096 ret = btrfs_update_root(trans, root,
1097 &root->root_key, &root->root_item);
1098
1099 btrfs_commit_transaction(trans, root);
1100out_reset:
1101 if (ret)
1102 btrfs_set_root_flags(&root->root_item, root_flags);
1103out:
1104 up_write(&root->fs_info->subvol_sem);
1018 return ret; 1105 return ret;
1019} 1106}
1020 1107
@@ -1526,6 +1613,9 @@ static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
1526 struct btrfs_ioctl_defrag_range_args *range; 1613 struct btrfs_ioctl_defrag_range_args *range;
1527 int ret; 1614 int ret;
1528 1615
1616 if (btrfs_root_readonly(root))
1617 return -EROFS;
1618
1529 ret = mnt_want_write(file->f_path.mnt); 1619 ret = mnt_want_write(file->f_path.mnt);
1530 if (ret) 1620 if (ret)
1531 return ret; 1621 return ret;
@@ -1654,6 +1744,9 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1654 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND)) 1744 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
1655 return -EINVAL; 1745 return -EINVAL;
1656 1746
1747 if (btrfs_root_readonly(root))
1748 return -EROFS;
1749
1657 ret = mnt_want_write(file->f_path.mnt); 1750 ret = mnt_want_write(file->f_path.mnt);
1658 if (ret) 1751 if (ret)
1659 return ret; 1752 return ret;
@@ -1975,6 +2068,10 @@ static long btrfs_ioctl_trans_start(struct file *file)
1975 if (file->private_data) 2068 if (file->private_data)
1976 goto out; 2069 goto out;
1977 2070
2071 ret = -EROFS;
2072 if (btrfs_root_readonly(root))
2073 goto out;
2074
1978 ret = mnt_want_write(file->f_path.mnt); 2075 ret = mnt_want_write(file->f_path.mnt);
1979 if (ret) 2076 if (ret)
1980 goto out; 2077 goto out;
@@ -2274,13 +2371,17 @@ long btrfs_ioctl(struct file *file, unsigned int
2274 case FS_IOC_GETVERSION: 2371 case FS_IOC_GETVERSION:
2275 return btrfs_ioctl_getversion(file, argp); 2372 return btrfs_ioctl_getversion(file, argp);
2276 case BTRFS_IOC_SNAP_CREATE: 2373 case BTRFS_IOC_SNAP_CREATE:
2277 return btrfs_ioctl_snap_create(file, argp, 0, 0); 2374 return btrfs_ioctl_snap_create(file, argp, 0);
2278 case BTRFS_IOC_SNAP_CREATE_V2: 2375 case BTRFS_IOC_SNAP_CREATE_V2:
2279 return btrfs_ioctl_snap_create(file, argp, 0, 1); 2376 return btrfs_ioctl_snap_create_v2(file, argp, 0);
2280 case BTRFS_IOC_SUBVOL_CREATE: 2377 case BTRFS_IOC_SUBVOL_CREATE:
2281 return btrfs_ioctl_snap_create(file, argp, 1, 0); 2378 return btrfs_ioctl_snap_create(file, argp, 1);
2282 case BTRFS_IOC_SNAP_DESTROY: 2379 case BTRFS_IOC_SNAP_DESTROY:
2283 return btrfs_ioctl_snap_destroy(file, argp); 2380 return btrfs_ioctl_snap_destroy(file, argp);
2381 case BTRFS_IOC_SUBVOL_GETFLAGS:
2382 return btrfs_ioctl_subvol_getflags(file, argp);
2383 case BTRFS_IOC_SUBVOL_SETFLAGS:
2384 return btrfs_ioctl_subvol_setflags(file, argp);
2284 case BTRFS_IOC_DEFAULT_SUBVOL: 2385 case BTRFS_IOC_DEFAULT_SUBVOL:
2285 return btrfs_ioctl_default_subvol(file, argp); 2386 return btrfs_ioctl_default_subvol(file, argp);
2286 case BTRFS_IOC_DEFRAG: 2387 case BTRFS_IOC_DEFRAG: