Linux 5.6.13
[linux/fpc-iii.git] / fs / ubifs / ioctl.c
blobd49fc04f2d7d4628a2da57b5eca997fe0c87e81b
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * This file is part of UBIFS.
5 * Copyright (C) 2006-2008 Nokia Corporation.
6 * Copyright (C) 2006, 2007 University of Szeged, Hungary
8 * Authors: Zoltan Sogor
9 * Artem Bityutskiy (Битюцкий Артём)
10 * Adrian Hunter
13 /* This file implements EXT2-compatible extended attribute ioctl() calls */
15 #include <linux/compat.h>
16 #include <linux/mount.h>
17 #include "ubifs.h"
19 /* Need to be kept consistent with checked flags in ioctl2ubifs() */
20 #define UBIFS_SETTABLE_IOCTL_FLAGS \
21 (FS_COMPR_FL | FS_SYNC_FL | FS_APPEND_FL | \
22 FS_IMMUTABLE_FL | FS_DIRSYNC_FL)
24 /* Need to be kept consistent with checked flags in ubifs2ioctl() */
25 #define UBIFS_GETTABLE_IOCTL_FLAGS \
26 (UBIFS_SETTABLE_IOCTL_FLAGS | FS_ENCRYPT_FL)
28 /**
29 * ubifs_set_inode_flags - set VFS inode flags.
30 * @inode: VFS inode to set flags for
32 * This function propagates flags from UBIFS inode object to VFS inode object.
34 void ubifs_set_inode_flags(struct inode *inode)
36 unsigned int flags = ubifs_inode(inode)->flags;
38 inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_DIRSYNC |
39 S_ENCRYPTED);
40 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;
48 if (flags & UBIFS_CRYPT_FL)
49 inode->i_flags |= S_ENCRYPTED;
53 * ioctl2ubifs - convert ioctl inode flags to UBIFS inode flags.
54 * @ioctl_flags: flags to convert
56 * This function converts ioctl flags (@FS_COMPR_FL, etc) to UBIFS inode flags
57 * (@UBIFS_COMPR_FL, etc).
59 static int ioctl2ubifs(int ioctl_flags)
61 int ubifs_flags = 0;
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;
74 return ubifs_flags;
78 * ubifs2ioctl - convert UBIFS inode flags to ioctl inode flags.
79 * @ubifs_flags: flags to convert
81 * This function converts UBIFS inode flags (@UBIFS_COMPR_FL, etc) to ioctl
82 * flags (@FS_COMPR_FL, etc).
84 static int ubifs2ioctl(int ubifs_flags)
86 int ioctl_flags = 0;
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;
98 if (ubifs_flags & UBIFS_CRYPT_FL)
99 ioctl_flags |= FS_ENCRYPT_FL;
101 return ioctl_flags;
104 static int setflags(struct inode *inode, int flags)
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 };
112 err = ubifs_budget_space(c, &req);
113 if (err)
114 return err;
116 mutex_lock(&ui->ui_mutex);
117 oldflags = ubifs2ioctl(ui->flags);
118 err = vfs_ioc_setflags_prepare(inode, oldflags, flags);
119 if (err)
120 goto out_unlock;
122 ui->flags &= ~ioctl2ubifs(UBIFS_SETTABLE_IOCTL_FLAGS);
123 ui->flags |= ioctl2ubifs(flags);
124 ubifs_set_inode_flags(inode);
125 inode->i_ctime = current_time(inode);
126 release = ui->dirty;
127 mark_inode_dirty_sync(inode);
128 mutex_unlock(&ui->ui_mutex);
130 if (release)
131 ubifs_release_budget(c, &req);
132 if (IS_SYNC(inode))
133 err = write_inode_now(inode, 1);
134 return err;
136 out_unlock:
137 ubifs_err(c, "can't modify inode %lu attributes", inode->i_ino);
138 mutex_unlock(&ui->ui_mutex);
139 ubifs_release_budget(c, &req);
140 return err;
143 long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
145 int flags, err;
146 struct inode *inode = file_inode(file);
148 switch (cmd) {
149 case FS_IOC_GETFLAGS:
150 flags = ubifs2ioctl(ubifs_inode(inode)->flags);
152 dbg_gen("get flags: %#x, i_flags %#x", flags, inode->i_flags);
153 return put_user(flags, (int __user *) arg);
155 case FS_IOC_SETFLAGS: {
156 if (IS_RDONLY(inode))
157 return -EROFS;
159 if (!inode_owner_or_capable(inode))
160 return -EACCES;
162 if (get_user(flags, (int __user *) arg))
163 return -EFAULT;
165 if (flags & ~UBIFS_GETTABLE_IOCTL_FLAGS)
166 return -EOPNOTSUPP;
167 flags &= UBIFS_SETTABLE_IOCTL_FLAGS;
169 if (!S_ISDIR(inode->i_mode))
170 flags &= ~FS_DIRSYNC_FL;
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.
176 err = mnt_want_write_file(file);
177 if (err)
178 return err;
179 dbg_gen("set flags: %#x, i_flags %#x", flags, inode->i_flags);
180 err = setflags(inode, flags);
181 mnt_drop_write_file(file);
182 return err;
184 case FS_IOC_SET_ENCRYPTION_POLICY: {
185 struct ubifs_info *c = inode->i_sb->s_fs_info;
187 err = ubifs_enable_encryption(c);
188 if (err)
189 return err;
191 return fscrypt_ioctl_set_policy(file, (const void __user *)arg);
193 case FS_IOC_GET_ENCRYPTION_POLICY:
194 return fscrypt_ioctl_get_policy(file, (void __user *)arg);
196 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
197 return fscrypt_ioctl_get_policy_ex(file, (void __user *)arg);
199 case FS_IOC_ADD_ENCRYPTION_KEY:
200 return fscrypt_ioctl_add_key(file, (void __user *)arg);
202 case FS_IOC_REMOVE_ENCRYPTION_KEY:
203 return fscrypt_ioctl_remove_key(file, (void __user *)arg);
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);
211 default:
212 return -ENOTTY;
216 #ifdef CONFIG_COMPAT
217 long ubifs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
219 switch (cmd) {
220 case FS_IOC32_GETFLAGS:
221 cmd = FS_IOC_GETFLAGS;
222 break;
223 case FS_IOC32_SETFLAGS:
224 cmd = FS_IOC_SETFLAGS;
225 break;
226 case FS_IOC_SET_ENCRYPTION_POLICY:
227 case FS_IOC_GET_ENCRYPTION_POLICY:
228 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
229 case FS_IOC_ADD_ENCRYPTION_KEY:
230 case FS_IOC_REMOVE_ENCRYPTION_KEY:
231 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
232 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
233 break;
234 default:
235 return -ENOIOCTLCMD;
237 return ubifs_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
239 #endif