]>
Commit | Line | Data |
---|---|---|
ac27a0ec | 1 | /* |
617ba13b | 2 | * linux/fs/ext4/ioctl.c |
ac27a0ec DK |
3 | * |
4 | * Copyright (C) 1993, 1994, 1995 | |
5 | * Remy Card ([email protected]) | |
6 | * Laboratoire MASI - Institut Blaise Pascal | |
7 | * Universite Pierre et Marie Curie (Paris VI) | |
8 | */ | |
9 | ||
10 | #include <linux/fs.h> | |
dab291af | 11 | #include <linux/jbd2.h> |
ac27a0ec | 12 | #include <linux/capability.h> |
ac27a0ec DK |
13 | #include <linux/time.h> |
14 | #include <linux/compat.h> | |
15 | #include <linux/smp_lock.h> | |
42a74f20 | 16 | #include <linux/mount.h> |
ac27a0ec | 17 | #include <asm/uaccess.h> |
3dcf5451 CH |
18 | #include "ext4_jbd2.h" |
19 | #include "ext4.h" | |
ac27a0ec | 20 | |
5cdd7b2d | 21 | long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
ac27a0ec | 22 | { |
5cdd7b2d | 23 | struct inode *inode = filp->f_dentry->d_inode; |
617ba13b | 24 | struct ext4_inode_info *ei = EXT4_I(inode); |
ac27a0ec | 25 | unsigned int flags; |
ac27a0ec | 26 | |
af5bc92d | 27 | ext4_debug("cmd = %u, arg = %lu\n", cmd, arg); |
ac27a0ec DK |
28 | |
29 | switch (cmd) { | |
617ba13b | 30 | case EXT4_IOC_GETFLAGS: |
ff9ddf7e | 31 | ext4_get_inode_flags(ei); |
617ba13b | 32 | flags = ei->i_flags & EXT4_FL_USER_VISIBLE; |
ac27a0ec | 33 | return put_user(flags, (int __user *) arg); |
617ba13b | 34 | case EXT4_IOC_SETFLAGS: { |
ac27a0ec | 35 | handle_t *handle = NULL; |
4db46fc2 | 36 | int err, migrate = 0; |
617ba13b | 37 | struct ext4_iloc iloc; |
ac27a0ec DK |
38 | unsigned int oldflags; |
39 | unsigned int jflag; | |
40 | ||
3bd858ab | 41 | if (!is_owner_or_cap(inode)) |
ac27a0ec DK |
42 | return -EACCES; |
43 | ||
44 | if (get_user(flags, (int __user *) arg)) | |
45 | return -EFAULT; | |
46 | ||
42a74f20 DH |
47 | err = mnt_want_write(filp->f_path.mnt); |
48 | if (err) | |
49 | return err; | |
50 | ||
ac27a0ec | 51 | if (!S_ISDIR(inode->i_mode)) |
617ba13b | 52 | flags &= ~EXT4_DIRSYNC_FL; |
ac27a0ec | 53 | |
42a74f20 | 54 | err = -EPERM; |
ac27a0ec | 55 | mutex_lock(&inode->i_mutex); |
e47776a0 | 56 | /* Is it quota file? Do not allow user to mess with it */ |
42a74f20 DH |
57 | if (IS_NOQUOTA(inode)) |
58 | goto flags_out; | |
59 | ||
ac27a0ec DK |
60 | oldflags = ei->i_flags; |
61 | ||
62 | /* The JOURNAL_DATA flag is modifiable only by root */ | |
617ba13b | 63 | jflag = flags & EXT4_JOURNAL_DATA_FL; |
ac27a0ec DK |
64 | |
65 | /* | |
66 | * The IMMUTABLE and APPEND_ONLY flags can only be changed by | |
67 | * the relevant capability. | |
68 | * | |
69 | * This test looks nicer. Thanks to Pauline Middelink | |
70 | */ | |
617ba13b | 71 | if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) { |
42a74f20 DH |
72 | if (!capable(CAP_LINUX_IMMUTABLE)) |
73 | goto flags_out; | |
ac27a0ec DK |
74 | } |
75 | ||
76 | /* | |
77 | * The JOURNAL_DATA flag can only be changed by | |
78 | * the relevant capability. | |
79 | */ | |
617ba13b | 80 | if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) { |
42a74f20 DH |
81 | if (!capable(CAP_SYS_RESOURCE)) |
82 | goto flags_out; | |
ac27a0ec | 83 | } |
4db46fc2 AK |
84 | if (oldflags & EXT4_EXTENTS_FL) { |
85 | /* We don't support clearning extent flags */ | |
86 | if (!(flags & EXT4_EXTENTS_FL)) { | |
87 | err = -EOPNOTSUPP; | |
88 | goto flags_out; | |
89 | } | |
90 | } else if (flags & EXT4_EXTENTS_FL) { | |
91 | /* migrate the file */ | |
92 | migrate = 1; | |
93 | flags &= ~EXT4_EXTENTS_FL; | |
94 | } | |
ac27a0ec | 95 | |
617ba13b | 96 | handle = ext4_journal_start(inode, 1); |
ac27a0ec | 97 | if (IS_ERR(handle)) { |
42a74f20 DH |
98 | err = PTR_ERR(handle); |
99 | goto flags_out; | |
ac27a0ec DK |
100 | } |
101 | if (IS_SYNC(inode)) | |
102 | handle->h_sync = 1; | |
617ba13b | 103 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
ac27a0ec DK |
104 | if (err) |
105 | goto flags_err; | |
106 | ||
617ba13b MC |
107 | flags = flags & EXT4_FL_USER_MODIFIABLE; |
108 | flags |= oldflags & ~EXT4_FL_USER_MODIFIABLE; | |
ac27a0ec DK |
109 | ei->i_flags = flags; |
110 | ||
617ba13b | 111 | ext4_set_inode_flags(inode); |
ef7f3835 | 112 | inode->i_ctime = ext4_current_time(inode); |
ac27a0ec | 113 | |
617ba13b | 114 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); |
ac27a0ec | 115 | flags_err: |
617ba13b | 116 | ext4_journal_stop(handle); |
42a74f20 DH |
117 | if (err) |
118 | goto flags_out; | |
ac27a0ec | 119 | |
617ba13b MC |
120 | if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) |
121 | err = ext4_change_inode_journal_flag(inode, jflag); | |
4db46fc2 AK |
122 | if (err) |
123 | goto flags_out; | |
124 | if (migrate) | |
125 | err = ext4_ext_migrate(inode); | |
42a74f20 | 126 | flags_out: |
ac27a0ec | 127 | mutex_unlock(&inode->i_mutex); |
42a74f20 | 128 | mnt_drop_write(filp->f_path.mnt); |
ac27a0ec DK |
129 | return err; |
130 | } | |
617ba13b MC |
131 | case EXT4_IOC_GETVERSION: |
132 | case EXT4_IOC_GETVERSION_OLD: | |
ac27a0ec | 133 | return put_user(inode->i_generation, (int __user *) arg); |
617ba13b MC |
134 | case EXT4_IOC_SETVERSION: |
135 | case EXT4_IOC_SETVERSION_OLD: { | |
ac27a0ec | 136 | handle_t *handle; |
617ba13b | 137 | struct ext4_iloc iloc; |
ac27a0ec DK |
138 | __u32 generation; |
139 | int err; | |
140 | ||
3bd858ab | 141 | if (!is_owner_or_cap(inode)) |
ac27a0ec | 142 | return -EPERM; |
42a74f20 DH |
143 | |
144 | err = mnt_want_write(filp->f_path.mnt); | |
145 | if (err) | |
146 | return err; | |
147 | if (get_user(generation, (int __user *) arg)) { | |
148 | err = -EFAULT; | |
149 | goto setversion_out; | |
150 | } | |
ac27a0ec | 151 | |
617ba13b | 152 | handle = ext4_journal_start(inode, 1); |
42a74f20 DH |
153 | if (IS_ERR(handle)) { |
154 | err = PTR_ERR(handle); | |
155 | goto setversion_out; | |
156 | } | |
617ba13b | 157 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
ac27a0ec | 158 | if (err == 0) { |
ef7f3835 | 159 | inode->i_ctime = ext4_current_time(inode); |
ac27a0ec | 160 | inode->i_generation = generation; |
617ba13b | 161 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); |
ac27a0ec | 162 | } |
617ba13b | 163 | ext4_journal_stop(handle); |
42a74f20 DH |
164 | setversion_out: |
165 | mnt_drop_write(filp->f_path.mnt); | |
ac27a0ec DK |
166 | return err; |
167 | } | |
e23291b9 | 168 | #ifdef CONFIG_JBD2_DEBUG |
617ba13b | 169 | case EXT4_IOC_WAIT_FOR_READONLY: |
ac27a0ec DK |
170 | /* |
171 | * This is racy - by the time we're woken up and running, | |
172 | * the superblock could be released. And the module could | |
173 | * have been unloaded. So sue me. | |
174 | * | |
175 | * Returns 1 if it slept, else zero. | |
176 | */ | |
177 | { | |
178 | struct super_block *sb = inode->i_sb; | |
179 | DECLARE_WAITQUEUE(wait, current); | |
180 | int ret = 0; | |
181 | ||
182 | set_current_state(TASK_INTERRUPTIBLE); | |
617ba13b MC |
183 | add_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait); |
184 | if (timer_pending(&EXT4_SB(sb)->turn_ro_timer)) { | |
ac27a0ec DK |
185 | schedule(); |
186 | ret = 1; | |
187 | } | |
617ba13b | 188 | remove_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait); |
ac27a0ec DK |
189 | return ret; |
190 | } | |
191 | #endif | |
617ba13b MC |
192 | case EXT4_IOC_GROUP_EXTEND: { |
193 | ext4_fsblk_t n_blocks_count; | |
ac27a0ec | 194 | struct super_block *sb = inode->i_sb; |
7ffe1ea8 | 195 | int err, err2; |
ac27a0ec DK |
196 | |
197 | if (!capable(CAP_SYS_RESOURCE)) | |
198 | return -EPERM; | |
199 | ||
ac27a0ec DK |
200 | if (get_user(n_blocks_count, (__u32 __user *)arg)) |
201 | return -EFAULT; | |
202 | ||
42a74f20 DH |
203 | err = mnt_want_write(filp->f_path.mnt); |
204 | if (err) | |
205 | return err; | |
206 | ||
617ba13b | 207 | err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count); |
dab291af | 208 | jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); |
7ffe1ea8 | 209 | err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal); |
dab291af | 210 | jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); |
7ffe1ea8 HK |
211 | if (err == 0) |
212 | err = err2; | |
42a74f20 | 213 | mnt_drop_write(filp->f_path.mnt); |
ac27a0ec DK |
214 | |
215 | return err; | |
216 | } | |
617ba13b MC |
217 | case EXT4_IOC_GROUP_ADD: { |
218 | struct ext4_new_group_data input; | |
ac27a0ec | 219 | struct super_block *sb = inode->i_sb; |
7ffe1ea8 | 220 | int err, err2; |
ac27a0ec DK |
221 | |
222 | if (!capable(CAP_SYS_RESOURCE)) | |
223 | return -EPERM; | |
224 | ||
617ba13b | 225 | if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg, |
ac27a0ec DK |
226 | sizeof(input))) |
227 | return -EFAULT; | |
228 | ||
42a74f20 DH |
229 | err = mnt_want_write(filp->f_path.mnt); |
230 | if (err) | |
231 | return err; | |
232 | ||
617ba13b | 233 | err = ext4_group_add(sb, &input); |
dab291af | 234 | jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); |
7ffe1ea8 | 235 | err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal); |
dab291af | 236 | jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); |
7ffe1ea8 HK |
237 | if (err == 0) |
238 | err = err2; | |
42a74f20 | 239 | mnt_drop_write(filp->f_path.mnt); |
ac27a0ec DK |
240 | |
241 | return err; | |
242 | } | |
243 | ||
c14c6fd5 | 244 | case EXT4_IOC_MIGRATE: |
2a43a878 AK |
245 | { |
246 | int err; | |
247 | if (!is_owner_or_cap(inode)) | |
248 | return -EACCES; | |
249 | ||
250 | err = mnt_want_write(filp->f_path.mnt); | |
251 | if (err) | |
252 | return err; | |
253 | /* | |
254 | * inode_mutex prevent write and truncate on the file. | |
255 | * Read still goes through. We take i_data_sem in | |
256 | * ext4_ext_swap_inode_data before we switch the | |
257 | * inode format to prevent read. | |
258 | */ | |
259 | mutex_lock(&(inode->i_mutex)); | |
260 | err = ext4_ext_migrate(inode); | |
261 | mutex_unlock(&(inode->i_mutex)); | |
262 | mnt_drop_write(filp->f_path.mnt); | |
263 | return err; | |
264 | } | |
c14c6fd5 | 265 | |
ac27a0ec DK |
266 | default: |
267 | return -ENOTTY; | |
268 | } | |
269 | } | |
270 | ||
271 | #ifdef CONFIG_COMPAT | |
617ba13b | 272 | long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
ac27a0ec | 273 | { |
ac27a0ec DK |
274 | /* These are just misnamed, they actually get/put from/to user an int */ |
275 | switch (cmd) { | |
617ba13b MC |
276 | case EXT4_IOC32_GETFLAGS: |
277 | cmd = EXT4_IOC_GETFLAGS; | |
ac27a0ec | 278 | break; |
617ba13b MC |
279 | case EXT4_IOC32_SETFLAGS: |
280 | cmd = EXT4_IOC_SETFLAGS; | |
ac27a0ec | 281 | break; |
617ba13b MC |
282 | case EXT4_IOC32_GETVERSION: |
283 | cmd = EXT4_IOC_GETVERSION; | |
ac27a0ec | 284 | break; |
617ba13b MC |
285 | case EXT4_IOC32_SETVERSION: |
286 | cmd = EXT4_IOC_SETVERSION; | |
ac27a0ec | 287 | break; |
617ba13b MC |
288 | case EXT4_IOC32_GROUP_EXTEND: |
289 | cmd = EXT4_IOC_GROUP_EXTEND; | |
ac27a0ec | 290 | break; |
617ba13b MC |
291 | case EXT4_IOC32_GETVERSION_OLD: |
292 | cmd = EXT4_IOC_GETVERSION_OLD; | |
ac27a0ec | 293 | break; |
617ba13b MC |
294 | case EXT4_IOC32_SETVERSION_OLD: |
295 | cmd = EXT4_IOC_SETVERSION_OLD; | |
ac27a0ec | 296 | break; |
e23291b9 | 297 | #ifdef CONFIG_JBD2_DEBUG |
617ba13b MC |
298 | case EXT4_IOC32_WAIT_FOR_READONLY: |
299 | cmd = EXT4_IOC_WAIT_FOR_READONLY; | |
ac27a0ec DK |
300 | break; |
301 | #endif | |
617ba13b MC |
302 | case EXT4_IOC32_GETRSVSZ: |
303 | cmd = EXT4_IOC_GETRSVSZ; | |
ac27a0ec | 304 | break; |
617ba13b MC |
305 | case EXT4_IOC32_SETRSVSZ: |
306 | cmd = EXT4_IOC_SETRSVSZ; | |
ac27a0ec | 307 | break; |
617ba13b | 308 | case EXT4_IOC_GROUP_ADD: |
ac27a0ec DK |
309 | break; |
310 | default: | |
311 | return -ENOIOCTLCMD; | |
312 | } | |
5cdd7b2d | 313 | return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); |
ac27a0ec DK |
314 | } |
315 | #endif |