Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of UBIFS. |
| 3 | * |
| 4 | * Copyright (C) 2006-2008 Nokia Corporation. |
| 5 | * Copyright (C) 2006, 2007 University of Szeged, Hungary |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License version 2 as published by |
| 9 | * the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 14 | * more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 18 | * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | * |
| 20 | * Authors: Zoltan Sogor |
| 21 | * Artem Bityutskiy (Битюцкий Артём) |
| 22 | * Adrian Hunter |
| 23 | */ |
| 24 | |
| 25 | /* This file implements EXT2-compatible extended attribute ioctl() calls */ |
| 26 | |
| 27 | #include <linux/compat.h> |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 28 | #include <linux/mount.h> |
| 29 | #include "ubifs.h" |
| 30 | |
Hou Tao | 2fe8b2d | 2019-02-09 16:54:20 +0800 | [diff] [blame] | 31 | /* Need to be kept consistent with checked flags in ioctl2ubifs() */ |
| 32 | #define UBIFS_SUPPORTED_IOCTL_FLAGS \ |
| 33 | (FS_COMPR_FL | FS_SYNC_FL | FS_APPEND_FL | \ |
| 34 | FS_IMMUTABLE_FL | FS_DIRSYNC_FL) |
| 35 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 36 | /** |
| 37 | * ubifs_set_inode_flags - set VFS inode flags. |
| 38 | * @inode: VFS inode to set flags for |
| 39 | * |
| 40 | * This function propagates flags from UBIFS inode object to VFS inode object. |
| 41 | */ |
| 42 | void ubifs_set_inode_flags(struct inode *inode) |
| 43 | { |
| 44 | unsigned int flags = ubifs_inode(inode)->flags; |
| 45 | |
Eric Biggers | 2ee6a57 | 2017-10-09 12:15:35 -0700 | [diff] [blame] | 46 | inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_DIRSYNC | |
| 47 | S_ENCRYPTED); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 48 | if (flags & UBIFS_SYNC_FL) |
| 49 | inode->i_flags |= S_SYNC; |
| 50 | if (flags & UBIFS_APPEND_FL) |
| 51 | inode->i_flags |= S_APPEND; |
| 52 | if (flags & UBIFS_IMMUTABLE_FL) |
| 53 | inode->i_flags |= S_IMMUTABLE; |
| 54 | if (flags & UBIFS_DIRSYNC_FL) |
| 55 | inode->i_flags |= S_DIRSYNC; |
Eric Biggers | 2ee6a57 | 2017-10-09 12:15:35 -0700 | [diff] [blame] | 56 | if (flags & UBIFS_CRYPT_FL) |
| 57 | inode->i_flags |= S_ENCRYPTED; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /* |
| 61 | * ioctl2ubifs - convert ioctl inode flags to UBIFS inode flags. |
| 62 | * @ioctl_flags: flags to convert |
| 63 | * |
Rock Lee | 798868c | 2017-04-13 23:16:06 -0700 | [diff] [blame] | 64 | * This function converts ioctl flags (@FS_COMPR_FL, etc) to UBIFS inode flags |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 65 | * (@UBIFS_COMPR_FL, etc). |
| 66 | */ |
| 67 | static int ioctl2ubifs(int ioctl_flags) |
| 68 | { |
| 69 | int ubifs_flags = 0; |
| 70 | |
| 71 | if (ioctl_flags & FS_COMPR_FL) |
| 72 | ubifs_flags |= UBIFS_COMPR_FL; |
| 73 | if (ioctl_flags & FS_SYNC_FL) |
| 74 | ubifs_flags |= UBIFS_SYNC_FL; |
| 75 | if (ioctl_flags & FS_APPEND_FL) |
| 76 | ubifs_flags |= UBIFS_APPEND_FL; |
| 77 | if (ioctl_flags & FS_IMMUTABLE_FL) |
| 78 | ubifs_flags |= UBIFS_IMMUTABLE_FL; |
| 79 | if (ioctl_flags & FS_DIRSYNC_FL) |
| 80 | ubifs_flags |= UBIFS_DIRSYNC_FL; |
| 81 | |
| 82 | return ubifs_flags; |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * ubifs2ioctl - convert UBIFS inode flags to ioctl inode flags. |
| 87 | * @ubifs_flags: flags to convert |
| 88 | * |
Rock Lee | 798868c | 2017-04-13 23:16:06 -0700 | [diff] [blame] | 89 | * This function converts UBIFS inode flags (@UBIFS_COMPR_FL, etc) to ioctl |
| 90 | * flags (@FS_COMPR_FL, etc). |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 91 | */ |
| 92 | static int ubifs2ioctl(int ubifs_flags) |
| 93 | { |
| 94 | int ioctl_flags = 0; |
| 95 | |
| 96 | if (ubifs_flags & UBIFS_COMPR_FL) |
| 97 | ioctl_flags |= FS_COMPR_FL; |
| 98 | if (ubifs_flags & UBIFS_SYNC_FL) |
| 99 | ioctl_flags |= FS_SYNC_FL; |
| 100 | if (ubifs_flags & UBIFS_APPEND_FL) |
| 101 | ioctl_flags |= FS_APPEND_FL; |
| 102 | if (ubifs_flags & UBIFS_IMMUTABLE_FL) |
| 103 | ioctl_flags |= FS_IMMUTABLE_FL; |
| 104 | if (ubifs_flags & UBIFS_DIRSYNC_FL) |
| 105 | ioctl_flags |= FS_DIRSYNC_FL; |
| 106 | |
| 107 | return ioctl_flags; |
| 108 | } |
| 109 | |
| 110 | static int setflags(struct inode *inode, int flags) |
| 111 | { |
| 112 | int oldflags, err, release; |
| 113 | struct ubifs_inode *ui = ubifs_inode(inode); |
| 114 | struct ubifs_info *c = inode->i_sb->s_fs_info; |
| 115 | struct ubifs_budget_req req = { .dirtied_ino = 1, |
| 116 | .dirtied_ino_d = ui->data_len }; |
| 117 | |
| 118 | err = ubifs_budget_space(c, &req); |
| 119 | if (err) |
| 120 | return err; |
| 121 | |
| 122 | /* |
| 123 | * The IMMUTABLE and APPEND_ONLY flags can only be changed by |
| 124 | * the relevant capability. |
| 125 | */ |
| 126 | mutex_lock(&ui->ui_mutex); |
| 127 | oldflags = ubifs2ioctl(ui->flags); |
| 128 | if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { |
| 129 | if (!capable(CAP_LINUX_IMMUTABLE)) { |
| 130 | err = -EPERM; |
| 131 | goto out_unlock; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | ui->flags = ioctl2ubifs(flags); |
| 136 | ubifs_set_inode_flags(inode); |
Deepa Dinamani | 607a11a | 2017-05-08 15:59:25 -0700 | [diff] [blame] | 137 | inode->i_ctime = current_time(inode); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 138 | release = ui->dirty; |
| 139 | mark_inode_dirty_sync(inode); |
| 140 | mutex_unlock(&ui->ui_mutex); |
| 141 | |
| 142 | if (release) |
| 143 | ubifs_release_budget(c, &req); |
| 144 | if (IS_SYNC(inode)) |
| 145 | err = write_inode_now(inode, 1); |
| 146 | return err; |
| 147 | |
| 148 | out_unlock: |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 149 | ubifs_err(c, "can't modify inode %lu attributes", inode->i_ino); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 150 | mutex_unlock(&ui->ui_mutex); |
| 151 | ubifs_release_budget(c, &req); |
| 152 | return err; |
| 153 | } |
| 154 | |
| 155 | long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 156 | { |
| 157 | int flags, err; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 158 | struct inode *inode = file_inode(file); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 159 | |
| 160 | switch (cmd) { |
| 161 | case FS_IOC_GETFLAGS: |
| 162 | flags = ubifs2ioctl(ubifs_inode(inode)->flags); |
| 163 | |
Artem Bityutskiy | a9f2fc0 | 2008-12-23 14:39:14 +0200 | [diff] [blame] | 164 | dbg_gen("get flags: %#x, i_flags %#x", flags, inode->i_flags); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 165 | return put_user(flags, (int __user *) arg); |
| 166 | |
| 167 | case FS_IOC_SETFLAGS: { |
| 168 | if (IS_RDONLY(inode)) |
| 169 | return -EROFS; |
| 170 | |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 171 | if (!inode_owner_or_capable(inode)) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 172 | return -EACCES; |
| 173 | |
| 174 | if (get_user(flags, (int __user *) arg)) |
| 175 | return -EFAULT; |
| 176 | |
Hou Tao | 2fe8b2d | 2019-02-09 16:54:20 +0800 | [diff] [blame] | 177 | if (flags & ~UBIFS_SUPPORTED_IOCTL_FLAGS) |
| 178 | return -EOPNOTSUPP; |
| 179 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 180 | if (!S_ISDIR(inode->i_mode)) |
| 181 | flags &= ~FS_DIRSYNC_FL; |
| 182 | |
| 183 | /* |
| 184 | * Make sure the file-system is read-write and make sure it |
| 185 | * will not become read-only while we are changing the flags. |
| 186 | */ |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 187 | err = mnt_want_write_file(file); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 188 | if (err) |
| 189 | return err; |
Artem Bityutskiy | a9f2fc0 | 2008-12-23 14:39:14 +0200 | [diff] [blame] | 190 | dbg_gen("set flags: %#x, i_flags %#x", flags, inode->i_flags); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 191 | err = setflags(inode, flags); |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 192 | mnt_drop_write_file(file); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 193 | return err; |
| 194 | } |
Richard Weinberger | d475a50 | 2016-10-20 16:47:56 +0200 | [diff] [blame] | 195 | case FS_IOC_SET_ENCRYPTION_POLICY: { |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 196 | #ifdef CONFIG_FS_ENCRYPTION |
Richard Weinberger | e021986 | 2016-10-19 23:24:47 +0200 | [diff] [blame] | 197 | struct ubifs_info *c = inode->i_sb->s_fs_info; |
Richard Weinberger | d475a50 | 2016-10-20 16:47:56 +0200 | [diff] [blame] | 198 | |
Richard Weinberger | e021986 | 2016-10-19 23:24:47 +0200 | [diff] [blame] | 199 | err = ubifs_enable_encryption(c); |
| 200 | if (err) |
| 201 | return err; |
| 202 | |
Richard Weinberger | ec9160d | 2016-12-13 00:27:59 +0100 | [diff] [blame] | 203 | return fscrypt_ioctl_set_policy(file, (const void __user *)arg); |
Richard Weinberger | d475a50 | 2016-10-20 16:47:56 +0200 | [diff] [blame] | 204 | #else |
| 205 | return -EOPNOTSUPP; |
| 206 | #endif |
| 207 | } |
| 208 | case FS_IOC_GET_ENCRYPTION_POLICY: { |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 209 | #ifdef CONFIG_FS_ENCRYPTION |
Richard Weinberger | ec9160d | 2016-12-13 00:27:59 +0100 | [diff] [blame] | 210 | return fscrypt_ioctl_get_policy(file, (void __user *)arg); |
Richard Weinberger | d475a50 | 2016-10-20 16:47:56 +0200 | [diff] [blame] | 211 | #else |
| 212 | return -EOPNOTSUPP; |
| 213 | #endif |
| 214 | } |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 215 | |
| 216 | default: |
| 217 | return -ENOTTY; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | #ifdef CONFIG_COMPAT |
| 222 | long ubifs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 223 | { |
| 224 | switch (cmd) { |
| 225 | case FS_IOC32_GETFLAGS: |
| 226 | cmd = FS_IOC_GETFLAGS; |
| 227 | break; |
| 228 | case FS_IOC32_SETFLAGS: |
| 229 | cmd = FS_IOC_SETFLAGS; |
| 230 | break; |
Eric Biggers | a75467d | 2016-12-19 11:12:48 -0800 | [diff] [blame] | 231 | case FS_IOC_SET_ENCRYPTION_POLICY: |
| 232 | case FS_IOC_GET_ENCRYPTION_POLICY: |
| 233 | break; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 234 | default: |
| 235 | return -ENOIOCTLCMD; |
| 236 | } |
| 237 | return ubifs_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); |
| 238 | } |
| 239 | #endif |