ioctl.c (21cb47be6fb9ece7e6ee63f6780986faa384a77c) ioctl.c (14f3db5542e62bcf6fe088a09760ac52d55306c5)
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * linux/fs/ext4/ioctl.c
4 *
5 * Copyright (C) 1993, 1994, 1995
6 * Remy Card (card@masi.ibp.fr)
7 * Laboratoire MASI - Institut Blaise Pascal
8 * Universite Pierre et Marie Curie (Paris VI)

--- 93 unchanged lines hidden (view full) ---

102}
103
104/**
105 * Swap the information from the given @inode and the inode
106 * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
107 * important fields of the inodes.
108 *
109 * @sb: the super block of the filesystem
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * linux/fs/ext4/ioctl.c
4 *
5 * Copyright (C) 1993, 1994, 1995
6 * Remy Card (card@masi.ibp.fr)
7 * Laboratoire MASI - Institut Blaise Pascal
8 * Universite Pierre et Marie Curie (Paris VI)

--- 93 unchanged lines hidden (view full) ---

102}
103
104/**
105 * Swap the information from the given @inode and the inode
106 * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
107 * important fields of the inodes.
108 *
109 * @sb: the super block of the filesystem
110 * @mnt_userns: user namespace of the mount the inode was found from
110 * @inode: the inode to swap with EXT4_BOOT_LOADER_INO
111 *
112 */
113static long swap_inode_boot_loader(struct super_block *sb,
111 * @inode: the inode to swap with EXT4_BOOT_LOADER_INO
112 *
113 */
114static long swap_inode_boot_loader(struct super_block *sb,
115 struct user_namespace *mnt_userns,
114 struct inode *inode)
115{
116 handle_t *handle;
117 int err;
118 struct inode *inode_bl;
119 struct ext4_inode_info *ei_bl;
120 qsize_t size, size_bl, diff;
121 blkcnt_t blocks;

--- 12 unchanged lines hidden (view full) ---

134 IS_SWAPFILE(inode) || IS_ENCRYPTED(inode) ||
135 (EXT4_I(inode)->i_flags & EXT4_JOURNAL_DATA_FL) ||
136 ext4_has_inline_data(inode)) {
137 err = -EINVAL;
138 goto journal_err_out;
139 }
140
141 if (IS_RDONLY(inode) || IS_APPEND(inode) || IS_IMMUTABLE(inode) ||
116 struct inode *inode)
117{
118 handle_t *handle;
119 int err;
120 struct inode *inode_bl;
121 struct ext4_inode_info *ei_bl;
122 qsize_t size, size_bl, diff;
123 blkcnt_t blocks;

--- 12 unchanged lines hidden (view full) ---

136 IS_SWAPFILE(inode) || IS_ENCRYPTED(inode) ||
137 (EXT4_I(inode)->i_flags & EXT4_JOURNAL_DATA_FL) ||
138 ext4_has_inline_data(inode)) {
139 err = -EINVAL;
140 goto journal_err_out;
141 }
142
143 if (IS_RDONLY(inode) || IS_APPEND(inode) || IS_IMMUTABLE(inode) ||
142 !inode_owner_or_capable(&init_user_ns, inode) ||
144 !inode_owner_or_capable(mnt_userns, inode) ||
143 !capable(CAP_SYS_ADMIN)) {
144 err = -EPERM;
145 goto journal_err_out;
146 }
147
148 down_write(&EXT4_I(inode)->i_mmap_sem);
149 err = filemap_write_and_wait(inode->i_mapping);
150 if (err)

--- 659 unchanged lines hidden (view full) ---

810 return error;
811}
812
813static long __ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
814{
815 struct inode *inode = file_inode(filp);
816 struct super_block *sb = inode->i_sb;
817 struct ext4_inode_info *ei = EXT4_I(inode);
145 !capable(CAP_SYS_ADMIN)) {
146 err = -EPERM;
147 goto journal_err_out;
148 }
149
150 down_write(&EXT4_I(inode)->i_mmap_sem);
151 err = filemap_write_and_wait(inode->i_mapping);
152 if (err)

--- 659 unchanged lines hidden (view full) ---

812 return error;
813}
814
815static long __ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
816{
817 struct inode *inode = file_inode(filp);
818 struct super_block *sb = inode->i_sb;
819 struct ext4_inode_info *ei = EXT4_I(inode);
820 struct user_namespace *mnt_userns = file_mnt_user_ns(filp);
818 unsigned int flags;
819
820 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
821
822 switch (cmd) {
823 case FS_IOC_GETFSMAP:
824 return ext4_ioc_getfsmap(sb, (void __user *)arg);
825 case FS_IOC_GETFLAGS:
826 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
827 if (S_ISREG(inode->i_mode))
828 flags &= ~EXT4_PROJINHERIT_FL;
829 return put_user(flags, (int __user *) arg);
830 case FS_IOC_SETFLAGS: {
831 int err;
832
821 unsigned int flags;
822
823 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
824
825 switch (cmd) {
826 case FS_IOC_GETFSMAP:
827 return ext4_ioc_getfsmap(sb, (void __user *)arg);
828 case FS_IOC_GETFLAGS:
829 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
830 if (S_ISREG(inode->i_mode))
831 flags &= ~EXT4_PROJINHERIT_FL;
832 return put_user(flags, (int __user *) arg);
833 case FS_IOC_SETFLAGS: {
834 int err;
835
833 if (!inode_owner_or_capable(&init_user_ns, inode))
836 if (!inode_owner_or_capable(mnt_userns, inode))
834 return -EACCES;
835
836 if (get_user(flags, (int __user *) arg))
837 return -EFAULT;
838
839 if (flags & ~EXT4_FL_USER_VISIBLE)
840 return -EOPNOTSUPP;
841 /*

--- 25 unchanged lines hidden (view full) ---

867 return put_user(inode->i_generation, (int __user *) arg);
868 case EXT4_IOC_SETVERSION:
869 case EXT4_IOC_SETVERSION_OLD: {
870 handle_t *handle;
871 struct ext4_iloc iloc;
872 __u32 generation;
873 int err;
874
837 return -EACCES;
838
839 if (get_user(flags, (int __user *) arg))
840 return -EFAULT;
841
842 if (flags & ~EXT4_FL_USER_VISIBLE)
843 return -EOPNOTSUPP;
844 /*

--- 25 unchanged lines hidden (view full) ---

870 return put_user(inode->i_generation, (int __user *) arg);
871 case EXT4_IOC_SETVERSION:
872 case EXT4_IOC_SETVERSION_OLD: {
873 handle_t *handle;
874 struct ext4_iloc iloc;
875 __u32 generation;
876 int err;
877
875 if (!inode_owner_or_capable(&init_user_ns, inode))
878 if (!inode_owner_or_capable(mnt_userns, inode))
876 return -EPERM;
877
878 if (ext4_has_metadata_csum(inode->i_sb)) {
879 ext4_warning(sb, "Setting inode version is not "
880 "supported with metadata_csum enabled.");
881 return -ENOTTY;
882 }
883

--- 122 unchanged lines hidden (view full) ---

1006 return -EFAULT;
1007
1008 return ext4_ioctl_group_add(filp, &input);
1009 }
1010
1011 case EXT4_IOC_MIGRATE:
1012 {
1013 int err;
879 return -EPERM;
880
881 if (ext4_has_metadata_csum(inode->i_sb)) {
882 ext4_warning(sb, "Setting inode version is not "
883 "supported with metadata_csum enabled.");
884 return -ENOTTY;
885 }
886

--- 122 unchanged lines hidden (view full) ---

1009 return -EFAULT;
1010
1011 return ext4_ioctl_group_add(filp, &input);
1012 }
1013
1014 case EXT4_IOC_MIGRATE:
1015 {
1016 int err;
1014 if (!inode_owner_or_capable(&init_user_ns, inode))
1017 if (!inode_owner_or_capable(mnt_userns, inode))
1015 return -EACCES;
1016
1017 err = mnt_want_write_file(filp);
1018 if (err)
1019 return err;
1020 /*
1021 * inode_mutex prevent write and truncate on the file.
1022 * Read still goes through. We take i_data_sem in

--- 5 unchanged lines hidden (view full) ---

1028 inode_unlock((inode));
1029 mnt_drop_write_file(filp);
1030 return err;
1031 }
1032
1033 case EXT4_IOC_ALLOC_DA_BLKS:
1034 {
1035 int err;
1018 return -EACCES;
1019
1020 err = mnt_want_write_file(filp);
1021 if (err)
1022 return err;
1023 /*
1024 * inode_mutex prevent write and truncate on the file.
1025 * Read still goes through. We take i_data_sem in

--- 5 unchanged lines hidden (view full) ---

1031 inode_unlock((inode));
1032 mnt_drop_write_file(filp);
1033 return err;
1034 }
1035
1036 case EXT4_IOC_ALLOC_DA_BLKS:
1037 {
1038 int err;
1036 if (!inode_owner_or_capable(&init_user_ns, inode))
1039 if (!inode_owner_or_capable(mnt_userns, inode))
1037 return -EACCES;
1038
1039 err = mnt_want_write_file(filp);
1040 if (err)
1041 return err;
1042 err = ext4_alloc_da_blocks(inode);
1043 mnt_drop_write_file(filp);
1044 return err;
1045 }
1046
1047 case EXT4_IOC_SWAP_BOOT:
1048 {
1049 int err;
1050 if (!(filp->f_mode & FMODE_WRITE))
1051 return -EBADF;
1052 err = mnt_want_write_file(filp);
1053 if (err)
1054 return err;
1040 return -EACCES;
1041
1042 err = mnt_want_write_file(filp);
1043 if (err)
1044 return err;
1045 err = ext4_alloc_da_blocks(inode);
1046 mnt_drop_write_file(filp);
1047 return err;
1048 }
1049
1050 case EXT4_IOC_SWAP_BOOT:
1051 {
1052 int err;
1053 if (!(filp->f_mode & FMODE_WRITE))
1054 return -EBADF;
1055 err = mnt_want_write_file(filp);
1056 if (err)
1057 return err;
1055 err = swap_inode_boot_loader(sb, inode);
1058 err = swap_inode_boot_loader(sb, mnt_userns, inode);
1056 mnt_drop_write_file(filp);
1057 return err;
1058 }
1059
1060 case EXT4_IOC_RESIZE_FS: {
1061 ext4_fsblk_t n_blocks_count;
1062 int err = 0, err2 = 0;
1063 ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;

--- 149 unchanged lines hidden (view full) ---

1213
1214 case FS_IOC_GET_ENCRYPTION_NONCE:
1215 if (!ext4_has_feature_encrypt(sb))
1216 return -EOPNOTSUPP;
1217 return fscrypt_ioctl_get_nonce(filp, (void __user *)arg);
1218
1219 case EXT4_IOC_CLEAR_ES_CACHE:
1220 {
1059 mnt_drop_write_file(filp);
1060 return err;
1061 }
1062
1063 case EXT4_IOC_RESIZE_FS: {
1064 ext4_fsblk_t n_blocks_count;
1065 int err = 0, err2 = 0;
1066 ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;

--- 149 unchanged lines hidden (view full) ---

1216
1217 case FS_IOC_GET_ENCRYPTION_NONCE:
1218 if (!ext4_has_feature_encrypt(sb))
1219 return -EOPNOTSUPP;
1220 return fscrypt_ioctl_get_nonce(filp, (void __user *)arg);
1221
1222 case EXT4_IOC_CLEAR_ES_CACHE:
1223 {
1221 if (!inode_owner_or_capable(&init_user_ns, inode))
1224 if (!inode_owner_or_capable(mnt_userns, inode))
1222 return -EACCES;
1223 ext4_clear_inode_es(inode);
1224 return 0;
1225 }
1226
1227 case EXT4_IOC_GETSTATE:
1228 {
1229 __u32 state = 0;

--- 29 unchanged lines hidden (view full) ---

1259 struct fsxattr fa, old_fa;
1260 int err;
1261
1262 if (copy_from_user(&fa, (struct fsxattr __user *)arg,
1263 sizeof(fa)))
1264 return -EFAULT;
1265
1266 /* Make sure caller has proper permission */
1225 return -EACCES;
1226 ext4_clear_inode_es(inode);
1227 return 0;
1228 }
1229
1230 case EXT4_IOC_GETSTATE:
1231 {
1232 __u32 state = 0;

--- 29 unchanged lines hidden (view full) ---

1262 struct fsxattr fa, old_fa;
1263 int err;
1264
1265 if (copy_from_user(&fa, (struct fsxattr __user *)arg,
1266 sizeof(fa)))
1267 return -EFAULT;
1268
1269 /* Make sure caller has proper permission */
1267 if (!inode_owner_or_capable(&init_user_ns, inode))
1270 if (!inode_owner_or_capable(mnt_userns, inode))
1268 return -EACCES;
1269
1270 if (fa.fsx_xflags & ~EXT4_SUPPORTED_FS_XFLAGS)
1271 return -EOPNOTSUPP;
1272
1273 flags = ext4_xflags_to_iflags(fa.fsx_xflags);
1274 if (ext4_mask_flags(inode->i_mode, flags) != flags)
1275 return -EOPNOTSUPP;

--- 131 unchanged lines hidden ---
1271 return -EACCES;
1272
1273 if (fa.fsx_xflags & ~EXT4_SUPPORTED_FS_XFLAGS)
1274 return -EOPNOTSUPP;
1275
1276 flags = ext4_xflags_to_iflags(fa.fsx_xflags);
1277 if (ext4_mask_flags(inode->i_mode, flags) != flags)
1278 return -EOPNOTSUPP;

--- 131 unchanged lines hidden ---