Linux 4.14.215
[linux/fpc-iii.git] / fs / ubifs / ioctl.c
blob1f6d16105990fe70a87034c9ccb189438b7a9dcb
1 /*
2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 * Copyright (C) 2006, 2007 University of Szeged, Hungary
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.
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.
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
20 * Authors: Zoltan Sogor
21 * Artem Bityutskiy (Битюцкий Артём)
22 * Adrian Hunter
25 /* This file implements EXT2-compatible extended attribute ioctl() calls */
27 #include <linux/compat.h>
28 #include <linux/mount.h>
29 #include "ubifs.h"
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)
36 /**
37 * ubifs_set_inode_flags - set VFS inode flags.
38 * @inode: VFS inode to set flags for
40 * This function propagates flags from UBIFS inode object to VFS inode object.
42 void ubifs_set_inode_flags(struct inode *inode)
44 unsigned int flags = ubifs_inode(inode)->flags;
46 inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_DIRSYNC);
47 if (flags & UBIFS_SYNC_FL)
48 inode->i_flags |= S_SYNC;
49 if (flags & UBIFS_APPEND_FL)
50 inode->i_flags |= S_APPEND;
51 if (flags & UBIFS_IMMUTABLE_FL)
52 inode->i_flags |= S_IMMUTABLE;
53 if (flags & UBIFS_DIRSYNC_FL)
54 inode->i_flags |= S_DIRSYNC;
58 * ioctl2ubifs - convert ioctl inode flags to UBIFS inode flags.
59 * @ioctl_flags: flags to convert
61 * This function converts ioctl flags (@FS_COMPR_FL, etc) to UBIFS inode flags
62 * (@UBIFS_COMPR_FL, etc).
64 static int ioctl2ubifs(int ioctl_flags)
66 int ubifs_flags = 0;
68 if (ioctl_flags & FS_COMPR_FL)
69 ubifs_flags |= UBIFS_COMPR_FL;
70 if (ioctl_flags & FS_SYNC_FL)
71 ubifs_flags |= UBIFS_SYNC_FL;
72 if (ioctl_flags & FS_APPEND_FL)
73 ubifs_flags |= UBIFS_APPEND_FL;
74 if (ioctl_flags & FS_IMMUTABLE_FL)
75 ubifs_flags |= UBIFS_IMMUTABLE_FL;
76 if (ioctl_flags & FS_DIRSYNC_FL)
77 ubifs_flags |= UBIFS_DIRSYNC_FL;
79 return ubifs_flags;
83 * ubifs2ioctl - convert UBIFS inode flags to ioctl inode flags.
84 * @ubifs_flags: flags to convert
86 * This function converts UBIFS inode flags (@UBIFS_COMPR_FL, etc) to ioctl
87 * flags (@FS_COMPR_FL, etc).
89 static int ubifs2ioctl(int ubifs_flags)
91 int ioctl_flags = 0;
93 if (ubifs_flags & UBIFS_COMPR_FL)
94 ioctl_flags |= FS_COMPR_FL;
95 if (ubifs_flags & UBIFS_SYNC_FL)
96 ioctl_flags |= FS_SYNC_FL;
97 if (ubifs_flags & UBIFS_APPEND_FL)
98 ioctl_flags |= FS_APPEND_FL;
99 if (ubifs_flags & UBIFS_IMMUTABLE_FL)
100 ioctl_flags |= FS_IMMUTABLE_FL;
101 if (ubifs_flags & UBIFS_DIRSYNC_FL)
102 ioctl_flags |= FS_DIRSYNC_FL;
104 return ioctl_flags;
107 static int setflags(struct inode *inode, int flags)
109 int oldflags, err, release;
110 struct ubifs_inode *ui = ubifs_inode(inode);
111 struct ubifs_info *c = inode->i_sb->s_fs_info;
112 struct ubifs_budget_req req = { .dirtied_ino = 1,
113 .dirtied_ino_d = ui->data_len };
115 err = ubifs_budget_space(c, &req);
116 if (err)
117 return err;
120 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
121 * the relevant capability.
123 mutex_lock(&ui->ui_mutex);
124 oldflags = ubifs2ioctl(ui->flags);
125 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
126 if (!capable(CAP_LINUX_IMMUTABLE)) {
127 err = -EPERM;
128 goto out_unlock;
132 ui->flags &= ~ioctl2ubifs(UBIFS_SUPPORTED_IOCTL_FLAGS);
133 ui->flags |= ioctl2ubifs(flags);
134 ubifs_set_inode_flags(inode);
135 inode->i_ctime = current_time(inode);
136 release = ui->dirty;
137 mark_inode_dirty_sync(inode);
138 mutex_unlock(&ui->ui_mutex);
140 if (release)
141 ubifs_release_budget(c, &req);
142 if (IS_SYNC(inode))
143 err = write_inode_now(inode, 1);
144 return err;
146 out_unlock:
147 ubifs_err(c, "can't modify inode %lu attributes", inode->i_ino);
148 mutex_unlock(&ui->ui_mutex);
149 ubifs_release_budget(c, &req);
150 return err;
153 long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
155 int flags, err;
156 struct inode *inode = file_inode(file);
158 switch (cmd) {
159 case FS_IOC_GETFLAGS:
160 flags = ubifs2ioctl(ubifs_inode(inode)->flags);
162 dbg_gen("get flags: %#x, i_flags %#x", flags, inode->i_flags);
163 return put_user(flags, (int __user *) arg);
165 case FS_IOC_SETFLAGS: {
166 if (IS_RDONLY(inode))
167 return -EROFS;
169 if (!inode_owner_or_capable(inode))
170 return -EACCES;
172 if (get_user(flags, (int __user *) arg))
173 return -EFAULT;
175 if (flags & ~UBIFS_SUPPORTED_IOCTL_FLAGS)
176 return -EOPNOTSUPP;
178 if (!S_ISDIR(inode->i_mode))
179 flags &= ~FS_DIRSYNC_FL;
182 * Make sure the file-system is read-write and make sure it
183 * will not become read-only while we are changing the flags.
185 err = mnt_want_write_file(file);
186 if (err)
187 return err;
188 dbg_gen("set flags: %#x, i_flags %#x", flags, inode->i_flags);
189 err = setflags(inode, flags);
190 mnt_drop_write_file(file);
191 return err;
193 case FS_IOC_SET_ENCRYPTION_POLICY: {
194 #ifdef CONFIG_UBIFS_FS_ENCRYPTION
195 struct ubifs_info *c = inode->i_sb->s_fs_info;
197 err = ubifs_enable_encryption(c);
198 if (err)
199 return err;
201 return fscrypt_ioctl_set_policy(file, (const void __user *)arg);
202 #else
203 return -EOPNOTSUPP;
204 #endif
206 case FS_IOC_GET_ENCRYPTION_POLICY: {
207 #ifdef CONFIG_UBIFS_FS_ENCRYPTION
208 return fscrypt_ioctl_get_policy(file, (void __user *)arg);
209 #else
210 return -EOPNOTSUPP;
211 #endif
214 default:
215 return -ENOTTY;
219 #ifdef CONFIG_COMPAT
220 long ubifs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
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;
229 case FS_IOC_SET_ENCRYPTION_POLICY:
230 case FS_IOC_GET_ENCRYPTION_POLICY:
231 break;
232 default:
233 return -ENOIOCTLCMD;
235 return ubifs_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
237 #endif