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 | |
| 31 | /** |
| 32 | * ubifs_set_inode_flags - set VFS inode flags. |
| 33 | * @inode: VFS inode to set flags for |
| 34 | * |
| 35 | * This function propagates flags from UBIFS inode object to VFS inode object. |
| 36 | */ |
| 37 | void ubifs_set_inode_flags(struct inode *inode) |
| 38 | { |
| 39 | unsigned int flags = ubifs_inode(inode)->flags; |
| 40 | |
Eric Biggers | 2ee6a57 | 2017-10-09 12:15:35 -0700 | [diff] [blame] | 41 | inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_DIRSYNC | |
| 42 | S_ENCRYPTED); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 43 | if (flags & UBIFS_SYNC_FL) |
| 44 | inode->i_flags |= S_SYNC; |
| 45 | if (flags & UBIFS_APPEND_FL) |
| 46 | inode->i_flags |= S_APPEND; |
| 47 | if (flags & UBIFS_IMMUTABLE_FL) |
| 48 | inode->i_flags |= S_IMMUTABLE; |
| 49 | if (flags & UBIFS_DIRSYNC_FL) |
| 50 | inode->i_flags |= S_DIRSYNC; |
Eric Biggers | 2ee6a57 | 2017-10-09 12:15:35 -0700 | [diff] [blame] | 51 | if (flags & UBIFS_CRYPT_FL) |
| 52 | inode->i_flags |= S_ENCRYPTED; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /* |
| 56 | * ioctl2ubifs - convert ioctl inode flags to UBIFS inode flags. |
| 57 | * @ioctl_flags: flags to convert |
| 58 | * |
Rock Lee | 798868c | 2017-04-13 23:16:06 -0700 | [diff] [blame] | 59 | * 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] | 60 | * (@UBIFS_COMPR_FL, etc). |
| 61 | */ |
| 62 | static int ioctl2ubifs(int ioctl_flags) |
| 63 | { |
| 64 | int ubifs_flags = 0; |
| 65 | |
| 66 | if (ioctl_flags & FS_COMPR_FL) |
| 67 | ubifs_flags |= UBIFS_COMPR_FL; |
| 68 | if (ioctl_flags & FS_SYNC_FL) |
| 69 | ubifs_flags |= UBIFS_SYNC_FL; |
| 70 | if (ioctl_flags & FS_APPEND_FL) |
| 71 | ubifs_flags |= UBIFS_APPEND_FL; |
| 72 | if (ioctl_flags & FS_IMMUTABLE_FL) |
| 73 | ubifs_flags |= UBIFS_IMMUTABLE_FL; |
| 74 | if (ioctl_flags & FS_DIRSYNC_FL) |
| 75 | ubifs_flags |= UBIFS_DIRSYNC_FL; |
| 76 | |
| 77 | return ubifs_flags; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * ubifs2ioctl - convert UBIFS inode flags to ioctl inode flags. |
| 82 | * @ubifs_flags: flags to convert |
| 83 | * |
Rock Lee | 798868c | 2017-04-13 23:16:06 -0700 | [diff] [blame] | 84 | * This function converts UBIFS inode flags (@UBIFS_COMPR_FL, etc) to ioctl |
| 85 | * flags (@FS_COMPR_FL, etc). |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 86 | */ |
| 87 | static int ubifs2ioctl(int ubifs_flags) |
| 88 | { |
| 89 | int ioctl_flags = 0; |
| 90 | |
| 91 | if (ubifs_flags & UBIFS_COMPR_FL) |
| 92 | ioctl_flags |= FS_COMPR_FL; |
| 93 | if (ubifs_flags & UBIFS_SYNC_FL) |
| 94 | ioctl_flags |= FS_SYNC_FL; |
| 95 | if (ubifs_flags & UBIFS_APPEND_FL) |
| 96 | ioctl_flags |= FS_APPEND_FL; |
| 97 | if (ubifs_flags & UBIFS_IMMUTABLE_FL) |
| 98 | ioctl_flags |= FS_IMMUTABLE_FL; |
| 99 | if (ubifs_flags & UBIFS_DIRSYNC_FL) |
| 100 | ioctl_flags |= FS_DIRSYNC_FL; |
| 101 | |
| 102 | return ioctl_flags; |
| 103 | } |
| 104 | |
| 105 | static int setflags(struct inode *inode, int flags) |
| 106 | { |
| 107 | int oldflags, err, release; |
| 108 | struct ubifs_inode *ui = ubifs_inode(inode); |
| 109 | struct ubifs_info *c = inode->i_sb->s_fs_info; |
| 110 | struct ubifs_budget_req req = { .dirtied_ino = 1, |
| 111 | .dirtied_ino_d = ui->data_len }; |
| 112 | |
| 113 | err = ubifs_budget_space(c, &req); |
| 114 | if (err) |
| 115 | return err; |
| 116 | |
| 117 | /* |
| 118 | * The IMMUTABLE and APPEND_ONLY flags can only be changed by |
| 119 | * the relevant capability. |
| 120 | */ |
| 121 | mutex_lock(&ui->ui_mutex); |
| 122 | oldflags = ubifs2ioctl(ui->flags); |
| 123 | if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { |
| 124 | if (!capable(CAP_LINUX_IMMUTABLE)) { |
| 125 | err = -EPERM; |
| 126 | goto out_unlock; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | ui->flags = ioctl2ubifs(flags); |
| 131 | ubifs_set_inode_flags(inode); |
Deepa Dinamani | 607a11a | 2017-05-08 15:59:25 -0700 | [diff] [blame] | 132 | inode->i_ctime = current_time(inode); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 133 | release = ui->dirty; |
| 134 | mark_inode_dirty_sync(inode); |
| 135 | mutex_unlock(&ui->ui_mutex); |
| 136 | |
| 137 | if (release) |
| 138 | ubifs_release_budget(c, &req); |
| 139 | if (IS_SYNC(inode)) |
| 140 | err = write_inode_now(inode, 1); |
| 141 | return err; |
| 142 | |
| 143 | out_unlock: |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 144 | ubifs_err(c, "can't modify inode %lu attributes", inode->i_ino); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 145 | mutex_unlock(&ui->ui_mutex); |
| 146 | ubifs_release_budget(c, &req); |
| 147 | return err; |
| 148 | } |
| 149 | |
| 150 | long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 151 | { |
| 152 | int flags, err; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 153 | struct inode *inode = file_inode(file); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 154 | |
| 155 | switch (cmd) { |
| 156 | case FS_IOC_GETFLAGS: |
| 157 | flags = ubifs2ioctl(ubifs_inode(inode)->flags); |
| 158 | |
Artem Bityutskiy | a9f2fc0 | 2008-12-23 14:39:14 +0200 | [diff] [blame] | 159 | dbg_gen("get flags: %#x, i_flags %#x", flags, inode->i_flags); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 160 | return put_user(flags, (int __user *) arg); |
| 161 | |
| 162 | case FS_IOC_SETFLAGS: { |
| 163 | if (IS_RDONLY(inode)) |
| 164 | return -EROFS; |
| 165 | |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 166 | if (!inode_owner_or_capable(inode)) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 167 | return -EACCES; |
| 168 | |
| 169 | if (get_user(flags, (int __user *) arg)) |
| 170 | return -EFAULT; |
| 171 | |
| 172 | if (!S_ISDIR(inode->i_mode)) |
| 173 | flags &= ~FS_DIRSYNC_FL; |
| 174 | |
| 175 | /* |
| 176 | * Make sure the file-system is read-write and make sure it |
| 177 | * will not become read-only while we are changing the flags. |
| 178 | */ |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 179 | err = mnt_want_write_file(file); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 180 | if (err) |
| 181 | return err; |
Artem Bityutskiy | a9f2fc0 | 2008-12-23 14:39:14 +0200 | [diff] [blame] | 182 | dbg_gen("set flags: %#x, i_flags %#x", flags, inode->i_flags); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 183 | err = setflags(inode, flags); |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 184 | mnt_drop_write_file(file); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 185 | return err; |
| 186 | } |
Richard Weinberger | d475a50 | 2016-10-20 16:47:56 +0200 | [diff] [blame] | 187 | case FS_IOC_SET_ENCRYPTION_POLICY: { |
| 188 | #ifdef CONFIG_UBIFS_FS_ENCRYPTION |
Richard Weinberger | e021986 | 2016-10-19 23:24:47 +0200 | [diff] [blame] | 189 | struct ubifs_info *c = inode->i_sb->s_fs_info; |
Richard Weinberger | d475a50 | 2016-10-20 16:47:56 +0200 | [diff] [blame] | 190 | |
Richard Weinberger | e021986 | 2016-10-19 23:24:47 +0200 | [diff] [blame] | 191 | err = ubifs_enable_encryption(c); |
| 192 | if (err) |
| 193 | return err; |
| 194 | |
Richard Weinberger | ec9160d | 2016-12-13 00:27:59 +0100 | [diff] [blame] | 195 | return fscrypt_ioctl_set_policy(file, (const void __user *)arg); |
Richard Weinberger | d475a50 | 2016-10-20 16:47:56 +0200 | [diff] [blame] | 196 | #else |
| 197 | return -EOPNOTSUPP; |
| 198 | #endif |
| 199 | } |
| 200 | case FS_IOC_GET_ENCRYPTION_POLICY: { |
| 201 | #ifdef CONFIG_UBIFS_FS_ENCRYPTION |
Richard Weinberger | ec9160d | 2016-12-13 00:27:59 +0100 | [diff] [blame] | 202 | return fscrypt_ioctl_get_policy(file, (void __user *)arg); |
Richard Weinberger | d475a50 | 2016-10-20 16:47:56 +0200 | [diff] [blame] | 203 | #else |
| 204 | return -EOPNOTSUPP; |
| 205 | #endif |
| 206 | } |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 207 | |
| 208 | default: |
| 209 | return -ENOTTY; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | #ifdef CONFIG_COMPAT |
| 214 | long ubifs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 215 | { |
| 216 | switch (cmd) { |
| 217 | case FS_IOC32_GETFLAGS: |
| 218 | cmd = FS_IOC_GETFLAGS; |
| 219 | break; |
| 220 | case FS_IOC32_SETFLAGS: |
| 221 | cmd = FS_IOC_SETFLAGS; |
| 222 | break; |
Eric Biggers | a75467d | 2016-12-19 11:12:48 -0800 | [diff] [blame] | 223 | case FS_IOC_SET_ENCRYPTION_POLICY: |
| 224 | case FS_IOC_GET_ENCRYPTION_POLICY: |
| 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 |