blob: 3df9be2c684c381e5f740ee5931887c83c9ef8d0 [file] [log] [blame]
Thomas Gleixner2b27bdc2019-05-29 16:57:50 -07001// SPDX-License-Identifier: GPL-2.0-only
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002/*
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 Bityutskiy1e517642008-07-14 19:08:37 +03008 * 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 Bityutskiy1e517642008-07-14 19:08:37 +030016#include <linux/mount.h>
17#include "ubifs.h"
18
Hou Tao2fe8b2d2019-02-09 16:54:20 +080019/* Need to be kept consistent with checked flags in ioctl2ubifs() */
Eric Biggers219b0e22019-12-09 14:23:25 -080020#define UBIFS_SETTABLE_IOCTL_FLAGS \
Hou Tao2fe8b2d2019-02-09 16:54:20 +080021 (FS_COMPR_FL | FS_SYNC_FL | FS_APPEND_FL | \
22 FS_IMMUTABLE_FL | FS_DIRSYNC_FL)
23
Eric Biggers219b0e22019-12-09 14:23:25 -080024/* Need to be kept consistent with checked flags in ubifs2ioctl() */
25#define UBIFS_GETTABLE_IOCTL_FLAGS \
26 (UBIFS_SETTABLE_IOCTL_FLAGS | FS_ENCRYPT_FL)
27
Artem Bityutskiy1e517642008-07-14 19:08:37 +030028/**
29 * ubifs_set_inode_flags - set VFS inode flags.
30 * @inode: VFS inode to set flags for
31 *
32 * This function propagates flags from UBIFS inode object to VFS inode object.
33 */
34void ubifs_set_inode_flags(struct inode *inode)
35{
36 unsigned int flags = ubifs_inode(inode)->flags;
37
Eric Biggers2ee6a572017-10-09 12:15:35 -070038 inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_DIRSYNC |
39 S_ENCRYPTED);
Artem Bityutskiy1e517642008-07-14 19:08:37 +030040 if (flags & UBIFS_SYNC_FL)
41 inode->i_flags |= S_SYNC;
42 if (flags & UBIFS_APPEND_FL)
43 inode->i_flags |= S_APPEND;
44 if (flags & UBIFS_IMMUTABLE_FL)
45 inode->i_flags |= S_IMMUTABLE;
46 if (flags & UBIFS_DIRSYNC_FL)
47 inode->i_flags |= S_DIRSYNC;
Eric Biggers2ee6a572017-10-09 12:15:35 -070048 if (flags & UBIFS_CRYPT_FL)
49 inode->i_flags |= S_ENCRYPTED;
Artem Bityutskiy1e517642008-07-14 19:08:37 +030050}
51
52/*
53 * ioctl2ubifs - convert ioctl inode flags to UBIFS inode flags.
54 * @ioctl_flags: flags to convert
55 *
Rock Lee798868c2017-04-13 23:16:06 -070056 * This function converts ioctl flags (@FS_COMPR_FL, etc) to UBIFS inode flags
Artem Bityutskiy1e517642008-07-14 19:08:37 +030057 * (@UBIFS_COMPR_FL, etc).
58 */
59static int ioctl2ubifs(int ioctl_flags)
60{
61 int ubifs_flags = 0;
62
63 if (ioctl_flags & FS_COMPR_FL)
64 ubifs_flags |= UBIFS_COMPR_FL;
65 if (ioctl_flags & FS_SYNC_FL)
66 ubifs_flags |= UBIFS_SYNC_FL;
67 if (ioctl_flags & FS_APPEND_FL)
68 ubifs_flags |= UBIFS_APPEND_FL;
69 if (ioctl_flags & FS_IMMUTABLE_FL)
70 ubifs_flags |= UBIFS_IMMUTABLE_FL;
71 if (ioctl_flags & FS_DIRSYNC_FL)
72 ubifs_flags |= UBIFS_DIRSYNC_FL;
73
74 return ubifs_flags;
75}
76
77/*
78 * ubifs2ioctl - convert UBIFS inode flags to ioctl inode flags.
79 * @ubifs_flags: flags to convert
80 *
Rock Lee798868c2017-04-13 23:16:06 -070081 * This function converts UBIFS inode flags (@UBIFS_COMPR_FL, etc) to ioctl
82 * flags (@FS_COMPR_FL, etc).
Artem Bityutskiy1e517642008-07-14 19:08:37 +030083 */
84static int ubifs2ioctl(int ubifs_flags)
85{
86 int ioctl_flags = 0;
87
88 if (ubifs_flags & UBIFS_COMPR_FL)
89 ioctl_flags |= FS_COMPR_FL;
90 if (ubifs_flags & UBIFS_SYNC_FL)
91 ioctl_flags |= FS_SYNC_FL;
92 if (ubifs_flags & UBIFS_APPEND_FL)
93 ioctl_flags |= FS_APPEND_FL;
94 if (ubifs_flags & UBIFS_IMMUTABLE_FL)
95 ioctl_flags |= FS_IMMUTABLE_FL;
96 if (ubifs_flags & UBIFS_DIRSYNC_FL)
97 ioctl_flags |= FS_DIRSYNC_FL;
Eric Biggers219b0e22019-12-09 14:23:25 -080098 if (ubifs_flags & UBIFS_CRYPT_FL)
99 ioctl_flags |= FS_ENCRYPT_FL;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300100
101 return ioctl_flags;
102}
103
104static int setflags(struct inode *inode, int flags)
105{
106 int oldflags, err, release;
107 struct ubifs_inode *ui = ubifs_inode(inode);
108 struct ubifs_info *c = inode->i_sb->s_fs_info;
109 struct ubifs_budget_req req = { .dirtied_ino = 1,
110 .dirtied_ino_d = ui->data_len };
111
112 err = ubifs_budget_space(c, &req);
113 if (err)
114 return err;
115
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300116 mutex_lock(&ui->ui_mutex);
117 oldflags = ubifs2ioctl(ui->flags);
Darrick J. Wong5aca2842019-07-01 08:25:34 -0700118 err = vfs_ioc_setflags_prepare(inode, oldflags, flags);
119 if (err)
120 goto out_unlock;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300121
Eric Biggers219b0e22019-12-09 14:23:25 -0800122 ui->flags &= ~ioctl2ubifs(UBIFS_SETTABLE_IOCTL_FLAGS);
Eric Biggers2b570672019-12-09 14:23:24 -0800123 ui->flags |= ioctl2ubifs(flags);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300124 ubifs_set_inode_flags(inode);
Deepa Dinamani607a11a2017-05-08 15:59:25 -0700125 inode->i_ctime = current_time(inode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300126 release = ui->dirty;
127 mark_inode_dirty_sync(inode);
128 mutex_unlock(&ui->ui_mutex);
129
130 if (release)
131 ubifs_release_budget(c, &req);
132 if (IS_SYNC(inode))
133 err = write_inode_now(inode, 1);
134 return err;
135
136out_unlock:
Sheng Yong235c3622015-03-20 10:39:42 +0000137 ubifs_err(c, "can't modify inode %lu attributes", inode->i_ino);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300138 mutex_unlock(&ui->ui_mutex);
139 ubifs_release_budget(c, &req);
140 return err;
141}
142
143long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
144{
145 int flags, err;
Al Viro496ad9a2013-01-23 17:07:38 -0500146 struct inode *inode = file_inode(file);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300147
148 switch (cmd) {
149 case FS_IOC_GETFLAGS:
150 flags = ubifs2ioctl(ubifs_inode(inode)->flags);
151
Artem Bityutskiya9f2fc02008-12-23 14:39:14 +0200152 dbg_gen("get flags: %#x, i_flags %#x", flags, inode->i_flags);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300153 return put_user(flags, (int __user *) arg);
154
155 case FS_IOC_SETFLAGS: {
156 if (IS_RDONLY(inode))
157 return -EROFS;
158
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700159 if (!inode_owner_or_capable(inode))
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300160 return -EACCES;
161
162 if (get_user(flags, (int __user *) arg))
163 return -EFAULT;
164
Eric Biggers219b0e22019-12-09 14:23:25 -0800165 if (flags & ~UBIFS_GETTABLE_IOCTL_FLAGS)
Hou Tao2fe8b2d2019-02-09 16:54:20 +0800166 return -EOPNOTSUPP;
Eric Biggers219b0e22019-12-09 14:23:25 -0800167 flags &= UBIFS_SETTABLE_IOCTL_FLAGS;
Hou Tao2fe8b2d2019-02-09 16:54:20 +0800168
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300169 if (!S_ISDIR(inode->i_mode))
170 flags &= ~FS_DIRSYNC_FL;
171
172 /*
173 * Make sure the file-system is read-write and make sure it
174 * will not become read-only while we are changing the flags.
175 */
Al Viroa561be72011-11-23 11:57:51 -0500176 err = mnt_want_write_file(file);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300177 if (err)
178 return err;
Artem Bityutskiya9f2fc02008-12-23 14:39:14 +0200179 dbg_gen("set flags: %#x, i_flags %#x", flags, inode->i_flags);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300180 err = setflags(inode, flags);
Al Viro2a79f172011-12-09 08:06:57 -0500181 mnt_drop_write_file(file);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300182 return err;
183 }
Richard Weinbergerd475a502016-10-20 16:47:56 +0200184 case FS_IOC_SET_ENCRYPTION_POLICY: {
Richard Weinbergere0219862016-10-19 23:24:47 +0200185 struct ubifs_info *c = inode->i_sb->s_fs_info;
Richard Weinbergerd475a502016-10-20 16:47:56 +0200186
Richard Weinbergere0219862016-10-19 23:24:47 +0200187 err = ubifs_enable_encryption(c);
188 if (err)
189 return err;
190
Richard Weinbergerec9160d2016-12-13 00:27:59 +0100191 return fscrypt_ioctl_set_policy(file, (const void __user *)arg);
Richard Weinbergerd475a502016-10-20 16:47:56 +0200192 }
Eric Biggerscf394962019-03-15 13:55:03 -0700193 case FS_IOC_GET_ENCRYPTION_POLICY:
Richard Weinbergerec9160d2016-12-13 00:27:59 +0100194 return fscrypt_ioctl_get_policy(file, (void __user *)arg);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300195
Eric Biggers62de2592019-08-04 19:35:49 -0700196 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
197 return fscrypt_ioctl_get_policy_ex(file, (void __user *)arg);
198
199 case FS_IOC_ADD_ENCRYPTION_KEY:
200 return fscrypt_ioctl_add_key(file, (void __user *)arg);
201
202 case FS_IOC_REMOVE_ENCRYPTION_KEY:
203 return fscrypt_ioctl_remove_key(file, (void __user *)arg);
204
205 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
206 return fscrypt_ioctl_remove_key_all_users(file,
207 (void __user *)arg);
208 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
209 return fscrypt_ioctl_get_key_status(file, (void __user *)arg);
210
Eric Biggers861261f2020-03-14 13:50:52 -0700211 case FS_IOC_GET_ENCRYPTION_NONCE:
212 return fscrypt_ioctl_get_nonce(file, (void __user *)arg);
213
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300214 default:
215 return -ENOTTY;
216 }
217}
218
219#ifdef CONFIG_COMPAT
220long ubifs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
221{
222 switch (cmd) {
223 case FS_IOC32_GETFLAGS:
224 cmd = FS_IOC_GETFLAGS;
225 break;
226 case FS_IOC32_SETFLAGS:
227 cmd = FS_IOC_SETFLAGS;
228 break;
Eric Biggersa75467d2016-12-19 11:12:48 -0800229 case FS_IOC_SET_ENCRYPTION_POLICY:
230 case FS_IOC_GET_ENCRYPTION_POLICY:
Eric Biggers62de2592019-08-04 19:35:49 -0700231 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
232 case FS_IOC_ADD_ENCRYPTION_KEY:
233 case FS_IOC_REMOVE_ENCRYPTION_KEY:
234 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
235 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
Eric Biggers861261f2020-03-14 13:50:52 -0700236 case FS_IOC_GET_ENCRYPTION_NONCE:
Eric Biggersa75467d2016-12-19 11:12:48 -0800237 break;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300238 default:
239 return -ENOIOCTLCMD;
240 }
241 return ubifs_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
242}
243#endif