GNU Linux-libre 4.19.286-gnu1
[releases.git] / fs / ubifs / ioctl.c
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>
28 #include <linux/mount.h>
29 #include "ubifs.h"
30
31 /* Need to be kept consistent with checked flags in ioctl2ubifs() */
32 #define UBIFS_SUPPORTED_IOCTL_FLAGS \
33         (FS_COMPR_FL | FS_SYNC_FL | FS_APPEND_FL | \
34          FS_IMMUTABLE_FL | FS_DIRSYNC_FL)
35
36 /**
37  * ubifs_set_inode_flags - set VFS inode flags.
38  * @inode: VFS inode to set flags for
39  *
40  * This function propagates flags from UBIFS inode object to VFS inode object.
41  */
42 void ubifs_set_inode_flags(struct inode *inode)
43 {
44         unsigned int flags = ubifs_inode(inode)->flags;
45
46         inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_DIRSYNC |
47                             S_ENCRYPTED);
48         if (flags & UBIFS_SYNC_FL)
49                 inode->i_flags |= S_SYNC;
50         if (flags & UBIFS_APPEND_FL)
51                 inode->i_flags |= S_APPEND;
52         if (flags & UBIFS_IMMUTABLE_FL)
53                 inode->i_flags |= S_IMMUTABLE;
54         if (flags & UBIFS_DIRSYNC_FL)
55                 inode->i_flags |= S_DIRSYNC;
56         if (flags & UBIFS_CRYPT_FL)
57                 inode->i_flags |= S_ENCRYPTED;
58 }
59
60 /*
61  * ioctl2ubifs - convert ioctl inode flags to UBIFS inode flags.
62  * @ioctl_flags: flags to convert
63  *
64  * This function converts ioctl flags (@FS_COMPR_FL, etc) to UBIFS inode flags
65  * (@UBIFS_COMPR_FL, etc).
66  */
67 static int ioctl2ubifs(int ioctl_flags)
68 {
69         int ubifs_flags = 0;
70
71         if (ioctl_flags & FS_COMPR_FL)
72                 ubifs_flags |= UBIFS_COMPR_FL;
73         if (ioctl_flags & FS_SYNC_FL)
74                 ubifs_flags |= UBIFS_SYNC_FL;
75         if (ioctl_flags & FS_APPEND_FL)
76                 ubifs_flags |= UBIFS_APPEND_FL;
77         if (ioctl_flags & FS_IMMUTABLE_FL)
78                 ubifs_flags |= UBIFS_IMMUTABLE_FL;
79         if (ioctl_flags & FS_DIRSYNC_FL)
80                 ubifs_flags |= UBIFS_DIRSYNC_FL;
81
82         return ubifs_flags;
83 }
84
85 /*
86  * ubifs2ioctl - convert UBIFS inode flags to ioctl inode flags.
87  * @ubifs_flags: flags to convert
88  *
89  * This function converts UBIFS inode flags (@UBIFS_COMPR_FL, etc) to ioctl
90  * flags (@FS_COMPR_FL, etc).
91  */
92 static int ubifs2ioctl(int ubifs_flags)
93 {
94         int ioctl_flags = 0;
95
96         if (ubifs_flags & UBIFS_COMPR_FL)
97                 ioctl_flags |= FS_COMPR_FL;
98         if (ubifs_flags & UBIFS_SYNC_FL)
99                 ioctl_flags |= FS_SYNC_FL;
100         if (ubifs_flags & UBIFS_APPEND_FL)
101                 ioctl_flags |= FS_APPEND_FL;
102         if (ubifs_flags & UBIFS_IMMUTABLE_FL)
103                 ioctl_flags |= FS_IMMUTABLE_FL;
104         if (ubifs_flags & UBIFS_DIRSYNC_FL)
105                 ioctl_flags |= FS_DIRSYNC_FL;
106
107         return ioctl_flags;
108 }
109
110 static int setflags(struct inode *inode, int flags)
111 {
112         int oldflags, err, release;
113         struct ubifs_inode *ui = ubifs_inode(inode);
114         struct ubifs_info *c = inode->i_sb->s_fs_info;
115         struct ubifs_budget_req req = { .dirtied_ino = 1,
116                         .dirtied_ino_d = ALIGN(ui->data_len, 8) };
117
118         err = ubifs_budget_space(c, &req);
119         if (err)
120                 return err;
121
122         /*
123          * The IMMUTABLE and APPEND_ONLY flags can only be changed by
124          * the relevant capability.
125          */
126         mutex_lock(&ui->ui_mutex);
127         oldflags = ubifs2ioctl(ui->flags);
128         if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
129                 if (!capable(CAP_LINUX_IMMUTABLE)) {
130                         err = -EPERM;
131                         goto out_unlock;
132                 }
133         }
134
135         ui->flags &= ~ioctl2ubifs(UBIFS_SUPPORTED_IOCTL_FLAGS);
136         ui->flags |= ioctl2ubifs(flags);
137         ubifs_set_inode_flags(inode);
138         inode->i_ctime = current_time(inode);
139         release = ui->dirty;
140         mark_inode_dirty_sync(inode);
141         mutex_unlock(&ui->ui_mutex);
142
143         if (release)
144                 ubifs_release_budget(c, &req);
145         if (IS_SYNC(inode))
146                 err = write_inode_now(inode, 1);
147         return err;
148
149 out_unlock:
150         ubifs_err(c, "can't modify inode %lu attributes", inode->i_ino);
151         mutex_unlock(&ui->ui_mutex);
152         ubifs_release_budget(c, &req);
153         return err;
154 }
155
156 long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
157 {
158         int flags, err;
159         struct inode *inode = file_inode(file);
160
161         switch (cmd) {
162         case FS_IOC_GETFLAGS:
163                 flags = ubifs2ioctl(ubifs_inode(inode)->flags);
164
165                 dbg_gen("get flags: %#x, i_flags %#x", flags, inode->i_flags);
166                 return put_user(flags, (int __user *) arg);
167
168         case FS_IOC_SETFLAGS: {
169                 if (IS_RDONLY(inode))
170                         return -EROFS;
171
172                 if (!inode_owner_or_capable(inode))
173                         return -EACCES;
174
175                 if (get_user(flags, (int __user *) arg))
176                         return -EFAULT;
177
178                 if (flags & ~UBIFS_SUPPORTED_IOCTL_FLAGS)
179                         return -EOPNOTSUPP;
180
181                 if (!S_ISDIR(inode->i_mode))
182                         flags &= ~FS_DIRSYNC_FL;
183
184                 /*
185                  * Make sure the file-system is read-write and make sure it
186                  * will not become read-only while we are changing the flags.
187                  */
188                 err = mnt_want_write_file(file);
189                 if (err)
190                         return err;
191                 dbg_gen("set flags: %#x, i_flags %#x", flags, inode->i_flags);
192                 err = setflags(inode, flags);
193                 mnt_drop_write_file(file);
194                 return err;
195         }
196         case FS_IOC_SET_ENCRYPTION_POLICY: {
197 #ifdef CONFIG_UBIFS_FS_ENCRYPTION
198                 struct ubifs_info *c = inode->i_sb->s_fs_info;
199
200                 err = ubifs_enable_encryption(c);
201                 if (err)
202                         return err;
203
204                 return fscrypt_ioctl_set_policy(file, (const void __user *)arg);
205 #else
206                 return -EOPNOTSUPP;
207 #endif
208         }
209         case FS_IOC_GET_ENCRYPTION_POLICY: {
210 #ifdef CONFIG_UBIFS_FS_ENCRYPTION
211                 return fscrypt_ioctl_get_policy(file, (void __user *)arg);
212 #else
213                 return -EOPNOTSUPP;
214 #endif
215         }
216
217         default:
218                 return -ENOTTY;
219         }
220 }
221
222 #ifdef CONFIG_COMPAT
223 long ubifs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
224 {
225         switch (cmd) {
226         case FS_IOC32_GETFLAGS:
227                 cmd = FS_IOC_GETFLAGS;
228                 break;
229         case FS_IOC32_SETFLAGS:
230                 cmd = FS_IOC_SETFLAGS;
231                 break;
232         case FS_IOC_SET_ENCRYPTION_POLICY:
233         case FS_IOC_GET_ENCRYPTION_POLICY:
234                 break;
235         default:
236                 return -ENOIOCTLCMD;
237         }
238         return ubifs_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
239 }
240 #endif