1 // SPDX-License-Identifier: GPL-2.0-only
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 (Битюцкий Артём)
13 /* This file implements EXT2-compatible extended attribute ioctl() calls */
15 #include <linux/compat.h>
16 #include <linux/mount.h>
17 #include <linux/fileattr.h>
20 /* Need to be kept consistent with checked flags in ioctl2ubifs() */
21 #define UBIFS_SETTABLE_IOCTL_FLAGS \
22 (FS_COMPR_FL | FS_SYNC_FL | FS_APPEND_FL | \
23 FS_IMMUTABLE_FL | FS_DIRSYNC_FL)
25 /* Need to be kept consistent with checked flags in ubifs2ioctl() */
26 #define UBIFS_GETTABLE_IOCTL_FLAGS \
27 (UBIFS_SETTABLE_IOCTL_FLAGS | FS_ENCRYPT_FL)
30 * ubifs_set_inode_flags - set VFS inode flags.
31 * @inode: VFS inode to set flags for
33 * This function propagates flags from UBIFS inode object to VFS inode object.
35 void ubifs_set_inode_flags(struct inode *inode)
37 unsigned int flags = ubifs_inode(inode)->flags;
39 inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_DIRSYNC |
41 if (flags & UBIFS_SYNC_FL)
42 inode->i_flags |= S_SYNC;
43 if (flags & UBIFS_APPEND_FL)
44 inode->i_flags |= S_APPEND;
45 if (flags & UBIFS_IMMUTABLE_FL)
46 inode->i_flags |= S_IMMUTABLE;
47 if (flags & UBIFS_DIRSYNC_FL)
48 inode->i_flags |= S_DIRSYNC;
49 if (flags & UBIFS_CRYPT_FL)
50 inode->i_flags |= S_ENCRYPTED;
54 * ioctl2ubifs - convert ioctl inode flags to UBIFS inode flags.
55 * @ioctl_flags: flags to convert
57 * This function converts ioctl flags (@FS_COMPR_FL, etc) to UBIFS inode flags
58 * (@UBIFS_COMPR_FL, etc).
60 static int ioctl2ubifs(int ioctl_flags)
64 if (ioctl_flags & FS_COMPR_FL)
65 ubifs_flags |= UBIFS_COMPR_FL;
66 if (ioctl_flags & FS_SYNC_FL)
67 ubifs_flags |= UBIFS_SYNC_FL;
68 if (ioctl_flags & FS_APPEND_FL)
69 ubifs_flags |= UBIFS_APPEND_FL;
70 if (ioctl_flags & FS_IMMUTABLE_FL)
71 ubifs_flags |= UBIFS_IMMUTABLE_FL;
72 if (ioctl_flags & FS_DIRSYNC_FL)
73 ubifs_flags |= UBIFS_DIRSYNC_FL;
79 * ubifs2ioctl - convert UBIFS inode flags to ioctl inode flags.
80 * @ubifs_flags: flags to convert
82 * This function converts UBIFS inode flags (@UBIFS_COMPR_FL, etc) to ioctl
83 * flags (@FS_COMPR_FL, etc).
85 static int ubifs2ioctl(int ubifs_flags)
89 if (ubifs_flags & UBIFS_COMPR_FL)
90 ioctl_flags |= FS_COMPR_FL;
91 if (ubifs_flags & UBIFS_SYNC_FL)
92 ioctl_flags |= FS_SYNC_FL;
93 if (ubifs_flags & UBIFS_APPEND_FL)
94 ioctl_flags |= FS_APPEND_FL;
95 if (ubifs_flags & UBIFS_IMMUTABLE_FL)
96 ioctl_flags |= FS_IMMUTABLE_FL;
97 if (ubifs_flags & UBIFS_DIRSYNC_FL)
98 ioctl_flags |= FS_DIRSYNC_FL;
99 if (ubifs_flags & UBIFS_CRYPT_FL)
100 ioctl_flags |= FS_ENCRYPT_FL;
105 static int setflags(struct inode *inode, int flags)
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 };
113 err = ubifs_budget_space(c, &req);
117 mutex_lock(&ui->ui_mutex);
118 ui->flags &= ~ioctl2ubifs(UBIFS_SETTABLE_IOCTL_FLAGS);
119 ui->flags |= ioctl2ubifs(flags);
120 ubifs_set_inode_flags(inode);
121 inode->i_ctime = current_time(inode);
123 mark_inode_dirty_sync(inode);
124 mutex_unlock(&ui->ui_mutex);
127 ubifs_release_budget(c, &req);
129 err = write_inode_now(inode, 1);
133 int ubifs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
135 struct inode *inode = d_inode(dentry);
136 int flags = ubifs2ioctl(ubifs_inode(inode)->flags);
138 if (d_is_special(dentry))
141 dbg_gen("get flags: %#x, i_flags %#x", flags, inode->i_flags);
142 fileattr_fill_flags(fa, flags);
147 int ubifs_fileattr_set(struct user_namespace *mnt_userns,
148 struct dentry *dentry, struct fileattr *fa)
150 struct inode *inode = d_inode(dentry);
151 int flags = fa->flags;
153 if (d_is_special(dentry))
156 if (fileattr_has_fsx(fa))
159 if (flags & ~UBIFS_GETTABLE_IOCTL_FLAGS)
162 flags &= UBIFS_SETTABLE_IOCTL_FLAGS;
164 if (!S_ISDIR(inode->i_mode))
165 flags &= ~FS_DIRSYNC_FL;
167 dbg_gen("set flags: %#x, i_flags %#x", flags, inode->i_flags);
168 return setflags(inode, flags);
171 long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
174 struct inode *inode = file_inode(file);
177 case FS_IOC_SET_ENCRYPTION_POLICY: {
178 struct ubifs_info *c = inode->i_sb->s_fs_info;
180 err = ubifs_enable_encryption(c);
184 return fscrypt_ioctl_set_policy(file, (const void __user *)arg);
186 case FS_IOC_GET_ENCRYPTION_POLICY:
187 return fscrypt_ioctl_get_policy(file, (void __user *)arg);
189 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
190 return fscrypt_ioctl_get_policy_ex(file, (void __user *)arg);
192 case FS_IOC_ADD_ENCRYPTION_KEY:
193 return fscrypt_ioctl_add_key(file, (void __user *)arg);
195 case FS_IOC_REMOVE_ENCRYPTION_KEY:
196 return fscrypt_ioctl_remove_key(file, (void __user *)arg);
198 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
199 return fscrypt_ioctl_remove_key_all_users(file,
201 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
202 return fscrypt_ioctl_get_key_status(file, (void __user *)arg);
204 case FS_IOC_GET_ENCRYPTION_NONCE:
205 return fscrypt_ioctl_get_nonce(file, (void __user *)arg);
213 long ubifs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
216 case FS_IOC32_GETFLAGS:
217 cmd = FS_IOC_GETFLAGS;
219 case FS_IOC32_SETFLAGS:
220 cmd = FS_IOC_SETFLAGS;
222 case FS_IOC_SET_ENCRYPTION_POLICY:
223 case FS_IOC_GET_ENCRYPTION_POLICY:
224 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
225 case FS_IOC_ADD_ENCRYPTION_KEY:
226 case FS_IOC_REMOVE_ENCRYPTION_KEY:
227 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
228 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
229 case FS_IOC_GET_ENCRYPTION_NONCE:
234 return ubifs_ioctl(file, cmd, (unsigned long)compat_ptr(arg));