Linux-libre 5.4.48-gnu
[librecmc/linux-libre.git] / fs / ubifs / ioctl.c
1 // SPDX-License-Identifier: GPL-2.0-only
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  *
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>
16 #include <linux/mount.h>
17 #include "ubifs.h"
18
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
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
34         inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_DIRSYNC |
35                             S_ENCRYPTED);
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;
44         if (flags & UBIFS_CRYPT_FL)
45                 inode->i_flags |= S_ENCRYPTED;
46 }
47
48 /*
49  * ioctl2ubifs - convert ioctl inode flags to UBIFS inode flags.
50  * @ioctl_flags: flags to convert
51  *
52  * This function converts ioctl flags (@FS_COMPR_FL, etc) to UBIFS inode flags
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  *
77  * This function converts UBIFS inode flags (@UBIFS_COMPR_FL, etc) to ioctl
78  * flags (@FS_COMPR_FL, etc).
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
110         mutex_lock(&ui->ui_mutex);
111         oldflags = ubifs2ioctl(ui->flags);
112         err = vfs_ioc_setflags_prepare(inode, oldflags, flags);
113         if (err)
114                 goto out_unlock;
115
116         ui->flags &= ~ioctl2ubifs(UBIFS_SUPPORTED_IOCTL_FLAGS);
117         ui->flags |= ioctl2ubifs(flags);
118         ubifs_set_inode_flags(inode);
119         inode->i_ctime = current_time(inode);
120         release = ui->dirty;
121         mark_inode_dirty_sync(inode);
122         mutex_unlock(&ui->ui_mutex);
123
124         if (release)
125                 ubifs_release_budget(c, &req);
126         if (IS_SYNC(inode))
127                 err = write_inode_now(inode, 1);
128         return err;
129
130 out_unlock:
131         ubifs_err(c, "can't modify inode %lu attributes", inode->i_ino);
132         mutex_unlock(&ui->ui_mutex);
133         ubifs_release_budget(c, &req);
134         return err;
135 }
136
137 long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
138 {
139         int flags, err;
140         struct inode *inode = file_inode(file);
141
142         switch (cmd) {
143         case FS_IOC_GETFLAGS:
144                 flags = ubifs2ioctl(ubifs_inode(inode)->flags);
145
146                 dbg_gen("get flags: %#x, i_flags %#x", flags, inode->i_flags);
147                 return put_user(flags, (int __user *) arg);
148
149         case FS_IOC_SETFLAGS: {
150                 if (IS_RDONLY(inode))
151                         return -EROFS;
152
153                 if (!inode_owner_or_capable(inode))
154                         return -EACCES;
155
156                 if (get_user(flags, (int __user *) arg))
157                         return -EFAULT;
158
159                 if (flags & ~UBIFS_SUPPORTED_IOCTL_FLAGS)
160                         return -EOPNOTSUPP;
161
162                 if (!S_ISDIR(inode->i_mode))
163                         flags &= ~FS_DIRSYNC_FL;
164
165                 /*
166                  * Make sure the file-system is read-write and make sure it
167                  * will not become read-only while we are changing the flags.
168                  */
169                 err = mnt_want_write_file(file);
170                 if (err)
171                         return err;
172                 dbg_gen("set flags: %#x, i_flags %#x", flags, inode->i_flags);
173                 err = setflags(inode, flags);
174                 mnt_drop_write_file(file);
175                 return err;
176         }
177         case FS_IOC_SET_ENCRYPTION_POLICY: {
178                 struct ubifs_info *c = inode->i_sb->s_fs_info;
179
180                 err = ubifs_enable_encryption(c);
181                 if (err)
182                         return err;
183
184                 return fscrypt_ioctl_set_policy(file, (const void __user *)arg);
185         }
186         case FS_IOC_GET_ENCRYPTION_POLICY:
187                 return fscrypt_ioctl_get_policy(file, (void __user *)arg);
188
189         case FS_IOC_GET_ENCRYPTION_POLICY_EX:
190                 return fscrypt_ioctl_get_policy_ex(file, (void __user *)arg);
191
192         case FS_IOC_ADD_ENCRYPTION_KEY:
193                 return fscrypt_ioctl_add_key(file, (void __user *)arg);
194
195         case FS_IOC_REMOVE_ENCRYPTION_KEY:
196                 return fscrypt_ioctl_remove_key(file, (void __user *)arg);
197
198         case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
199                 return fscrypt_ioctl_remove_key_all_users(file,
200                                                           (void __user *)arg);
201         case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
202                 return fscrypt_ioctl_get_key_status(file, (void __user *)arg);
203
204         default:
205                 return -ENOTTY;
206         }
207 }
208
209 #ifdef CONFIG_COMPAT
210 long ubifs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
211 {
212         switch (cmd) {
213         case FS_IOC32_GETFLAGS:
214                 cmd = FS_IOC_GETFLAGS;
215                 break;
216         case FS_IOC32_SETFLAGS:
217                 cmd = FS_IOC_SETFLAGS;
218                 break;
219         case FS_IOC_SET_ENCRYPTION_POLICY:
220         case FS_IOC_GET_ENCRYPTION_POLICY:
221         case FS_IOC_GET_ENCRYPTION_POLICY_EX:
222         case FS_IOC_ADD_ENCRYPTION_KEY:
223         case FS_IOC_REMOVE_ENCRYPTION_KEY:
224         case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
225         case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
226                 break;
227         default:
228                 return -ENOIOCTLCMD;
229         }
230         return ubifs_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
231 }
232 #endif