]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/ubifs/ioctl.c
Merge branch 'timers-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-jammy-kernel.git] / fs / ubifs / ioctl.c
CommitLineData
2b27bdcc 1// SPDX-License-Identifier: GPL-2.0-only
1e51764a
AB
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 *
1e51764a
AB
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>
1e51764a
AB
16#include <linux/mount.h>
17#include "ubifs.h"
18
2fe8b2d5
HT
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
1e51764a
AB
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 */
30void ubifs_set_inode_flags(struct inode *inode)
31{
32 unsigned int flags = ubifs_inode(inode)->flags;
33
2ee6a576
EB
34 inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_DIRSYNC |
35 S_ENCRYPTED);
1e51764a
AB
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;
2ee6a576
EB
44 if (flags & UBIFS_CRYPT_FL)
45 inode->i_flags |= S_ENCRYPTED;
1e51764a
AB
46}
47
48/*
49 * ioctl2ubifs - convert ioctl inode flags to UBIFS inode flags.
50 * @ioctl_flags: flags to convert
51 *
798868c0 52 * This function converts ioctl flags (@FS_COMPR_FL, etc) to UBIFS inode flags
1e51764a
AB
53 * (@UBIFS_COMPR_FL, etc).
54 */
55static 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 *
798868c0
RL
77 * This function converts UBIFS inode flags (@UBIFS_COMPR_FL, etc) to ioctl
78 * flags (@FS_COMPR_FL, etc).
1e51764a
AB
79 */
80static 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
98static 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 /*
111 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
112 * the relevant capability.
113 */
114 mutex_lock(&ui->ui_mutex);
115 oldflags = ubifs2ioctl(ui->flags);
116 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
117 if (!capable(CAP_LINUX_IMMUTABLE)) {
118 err = -EPERM;
119 goto out_unlock;
120 }
121 }
122
123 ui->flags = ioctl2ubifs(flags);
124 ubifs_set_inode_flags(inode);
607a11ad 125 inode->i_ctime = current_time(inode);
1e51764a
AB
126 release = ui->dirty;
127 mark_inode_dirty_sync(inode);
128 mutex_unlock(&ui->ui_mutex);
129
130 if (release)
131 ubifs_release_budget(c, &req);
132 if (IS_SYNC(inode))
133 err = write_inode_now(inode, 1);
134 return err;
135
136out_unlock:
235c362b 137 ubifs_err(c, "can't modify inode %lu attributes", inode->i_ino);
1e51764a
AB
138 mutex_unlock(&ui->ui_mutex);
139 ubifs_release_budget(c, &req);
140 return err;
141}
142
143long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
144{
145 int flags, err;
496ad9aa 146 struct inode *inode = file_inode(file);
1e51764a
AB
147
148 switch (cmd) {
149 case FS_IOC_GETFLAGS:
150 flags = ubifs2ioctl(ubifs_inode(inode)->flags);
151
a9f2fc0e 152 dbg_gen("get flags: %#x, i_flags %#x", flags, inode->i_flags);
1e51764a
AB
153 return put_user(flags, (int __user *) arg);
154
155 case FS_IOC_SETFLAGS: {
156 if (IS_RDONLY(inode))
157 return -EROFS;
158
2e149670 159 if (!inode_owner_or_capable(inode))
1e51764a
AB
160 return -EACCES;
161
162 if (get_user(flags, (int __user *) arg))
163 return -EFAULT;
164
2fe8b2d5
HT
165 if (flags & ~UBIFS_SUPPORTED_IOCTL_FLAGS)
166 return -EOPNOTSUPP;
167
1e51764a
AB
168 if (!S_ISDIR(inode->i_mode))
169 flags &= ~FS_DIRSYNC_FL;
170
171 /*
172 * Make sure the file-system is read-write and make sure it
173 * will not become read-only while we are changing the flags.
174 */
a561be71 175 err = mnt_want_write_file(file);
1e51764a
AB
176 if (err)
177 return err;
a9f2fc0e 178 dbg_gen("set flags: %#x, i_flags %#x", flags, inode->i_flags);
1e51764a 179 err = setflags(inode, flags);
2a79f17e 180 mnt_drop_write_file(file);
1e51764a
AB
181 return err;
182 }
d475a507 183 case FS_IOC_SET_ENCRYPTION_POLICY: {
e021986e 184 struct ubifs_info *c = inode->i_sb->s_fs_info;
d475a507 185
e021986e
RW
186 err = ubifs_enable_encryption(c);
187 if (err)
188 return err;
189
ec9160da 190 return fscrypt_ioctl_set_policy(file, (const void __user *)arg);
d475a507 191 }
cf394967 192 case FS_IOC_GET_ENCRYPTION_POLICY:
ec9160da 193 return fscrypt_ioctl_get_policy(file, (void __user *)arg);
1e51764a
AB
194
195 default:
196 return -ENOTTY;
197 }
198}
199
200#ifdef CONFIG_COMPAT
201long ubifs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
202{
203 switch (cmd) {
204 case FS_IOC32_GETFLAGS:
205 cmd = FS_IOC_GETFLAGS;
206 break;
207 case FS_IOC32_SETFLAGS:
208 cmd = FS_IOC_SETFLAGS;
209 break;
a75467d9
EB
210 case FS_IOC_SET_ENCRYPTION_POLICY:
211 case FS_IOC_GET_ENCRYPTION_POLICY:
212 break;
1e51764a
AB
213 default:
214 return -ENOIOCTLCMD;
215 }
216 return ubifs_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
217}
218#endif