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