]>
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> | |
42a74f20 | 15 | #include <linux/mount.h> |
748de673 | 16 | #include <linux/file.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 | ||
2dc6b0d4 | 51 | flags = ext4_mask_flags(inode->i_mode, flags); |
ac27a0ec | 52 | |
42a74f20 | 53 | err = -EPERM; |
ac27a0ec | 54 | mutex_lock(&inode->i_mutex); |
e47776a0 | 55 | /* Is it quota file? Do not allow user to mess with it */ |
42a74f20 DH |
56 | if (IS_NOQUOTA(inode)) |
57 | goto flags_out; | |
58 | ||
ac27a0ec DK |
59 | oldflags = ei->i_flags; |
60 | ||
61 | /* The JOURNAL_DATA flag is modifiable only by root */ | |
617ba13b | 62 | jflag = flags & EXT4_JOURNAL_DATA_FL; |
ac27a0ec DK |
63 | |
64 | /* | |
65 | * The IMMUTABLE and APPEND_ONLY flags can only be changed by | |
66 | * the relevant capability. | |
67 | * | |
68 | * This test looks nicer. Thanks to Pauline Middelink | |
69 | */ | |
617ba13b | 70 | if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) { |
42a74f20 DH |
71 | if (!capable(CAP_LINUX_IMMUTABLE)) |
72 | goto flags_out; | |
ac27a0ec DK |
73 | } |
74 | ||
75 | /* | |
76 | * The JOURNAL_DATA flag can only be changed by | |
77 | * the relevant capability. | |
78 | */ | |
617ba13b | 79 | if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) { |
42a74f20 DH |
80 | if (!capable(CAP_SYS_RESOURCE)) |
81 | goto flags_out; | |
ac27a0ec | 82 | } |
4db46fc2 AK |
83 | if (oldflags & EXT4_EXTENTS_FL) { |
84 | /* We don't support clearning extent flags */ | |
85 | if (!(flags & EXT4_EXTENTS_FL)) { | |
86 | err = -EOPNOTSUPP; | |
87 | goto flags_out; | |
88 | } | |
89 | } else if (flags & EXT4_EXTENTS_FL) { | |
90 | /* migrate the file */ | |
91 | migrate = 1; | |
92 | flags &= ~EXT4_EXTENTS_FL; | |
93 | } | |
ac27a0ec | 94 | |
617ba13b | 95 | handle = ext4_journal_start(inode, 1); |
ac27a0ec | 96 | if (IS_ERR(handle)) { |
42a74f20 DH |
97 | err = PTR_ERR(handle); |
98 | goto flags_out; | |
ac27a0ec DK |
99 | } |
100 | if (IS_SYNC(inode)) | |
0390131b | 101 | ext4_handle_sync(handle); |
617ba13b | 102 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
ac27a0ec DK |
103 | if (err) |
104 | goto flags_err; | |
105 | ||
617ba13b MC |
106 | flags = flags & EXT4_FL_USER_MODIFIABLE; |
107 | flags |= oldflags & ~EXT4_FL_USER_MODIFIABLE; | |
ac27a0ec DK |
108 | ei->i_flags = flags; |
109 | ||
617ba13b | 110 | ext4_set_inode_flags(inode); |
ef7f3835 | 111 | inode->i_ctime = ext4_current_time(inode); |
ac27a0ec | 112 | |
617ba13b | 113 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); |
ac27a0ec | 114 | flags_err: |
617ba13b | 115 | ext4_journal_stop(handle); |
42a74f20 DH |
116 | if (err) |
117 | goto flags_out; | |
ac27a0ec | 118 | |
617ba13b MC |
119 | if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) |
120 | err = ext4_change_inode_journal_flag(inode, jflag); | |
4db46fc2 AK |
121 | if (err) |
122 | goto flags_out; | |
123 | if (migrate) | |
124 | err = ext4_ext_migrate(inode); | |
42a74f20 | 125 | flags_out: |
ac27a0ec | 126 | mutex_unlock(&inode->i_mutex); |
42a74f20 | 127 | mnt_drop_write(filp->f_path.mnt); |
ac27a0ec DK |
128 | return err; |
129 | } | |
617ba13b MC |
130 | case EXT4_IOC_GETVERSION: |
131 | case EXT4_IOC_GETVERSION_OLD: | |
ac27a0ec | 132 | return put_user(inode->i_generation, (int __user *) arg); |
617ba13b MC |
133 | case EXT4_IOC_SETVERSION: |
134 | case EXT4_IOC_SETVERSION_OLD: { | |
ac27a0ec | 135 | handle_t *handle; |
617ba13b | 136 | struct ext4_iloc iloc; |
ac27a0ec DK |
137 | __u32 generation; |
138 | int err; | |
139 | ||
3bd858ab | 140 | if (!is_owner_or_cap(inode)) |
ac27a0ec | 141 | return -EPERM; |
42a74f20 DH |
142 | |
143 | err = mnt_want_write(filp->f_path.mnt); | |
144 | if (err) | |
145 | return err; | |
146 | if (get_user(generation, (int __user *) arg)) { | |
147 | err = -EFAULT; | |
148 | goto setversion_out; | |
149 | } | |
ac27a0ec | 150 | |
617ba13b | 151 | handle = ext4_journal_start(inode, 1); |
42a74f20 DH |
152 | if (IS_ERR(handle)) { |
153 | err = PTR_ERR(handle); | |
154 | goto setversion_out; | |
155 | } | |
617ba13b | 156 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
ac27a0ec | 157 | if (err == 0) { |
ef7f3835 | 158 | inode->i_ctime = ext4_current_time(inode); |
ac27a0ec | 159 | inode->i_generation = generation; |
617ba13b | 160 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); |
ac27a0ec | 161 | } |
617ba13b | 162 | ext4_journal_stop(handle); |
42a74f20 DH |
163 | setversion_out: |
164 | mnt_drop_write(filp->f_path.mnt); | |
ac27a0ec DK |
165 | return err; |
166 | } | |
e23291b9 | 167 | #ifdef CONFIG_JBD2_DEBUG |
617ba13b | 168 | case EXT4_IOC_WAIT_FOR_READONLY: |
ac27a0ec DK |
169 | /* |
170 | * This is racy - by the time we're woken up and running, | |
171 | * the superblock could be released. And the module could | |
172 | * have been unloaded. So sue me. | |
173 | * | |
174 | * Returns 1 if it slept, else zero. | |
175 | */ | |
176 | { | |
177 | struct super_block *sb = inode->i_sb; | |
178 | DECLARE_WAITQUEUE(wait, current); | |
179 | int ret = 0; | |
180 | ||
181 | set_current_state(TASK_INTERRUPTIBLE); | |
617ba13b MC |
182 | add_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait); |
183 | if (timer_pending(&EXT4_SB(sb)->turn_ro_timer)) { | |
ac27a0ec DK |
184 | schedule(); |
185 | ret = 1; | |
186 | } | |
617ba13b | 187 | remove_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait); |
ac27a0ec DK |
188 | return ret; |
189 | } | |
190 | #endif | |
617ba13b MC |
191 | case EXT4_IOC_GROUP_EXTEND: { |
192 | ext4_fsblk_t n_blocks_count; | |
ac27a0ec | 193 | struct super_block *sb = inode->i_sb; |
ac046f1d | 194 | int err, err2=0; |
ac27a0ec DK |
195 | |
196 | if (!capable(CAP_SYS_RESOURCE)) | |
197 | return -EPERM; | |
198 | ||
ac27a0ec DK |
199 | if (get_user(n_blocks_count, (__u32 __user *)arg)) |
200 | return -EFAULT; | |
201 | ||
42a74f20 DH |
202 | err = mnt_want_write(filp->f_path.mnt); |
203 | if (err) | |
204 | return err; | |
205 | ||
617ba13b | 206 | err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count); |
ac046f1d PT |
207 | if (EXT4_SB(sb)->s_journal) { |
208 | jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); | |
209 | err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal); | |
210 | jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); | |
211 | } | |
7ffe1ea8 HK |
212 | if (err == 0) |
213 | err = err2; | |
42a74f20 | 214 | mnt_drop_write(filp->f_path.mnt); |
ac27a0ec DK |
215 | |
216 | return err; | |
217 | } | |
748de673 AF |
218 | |
219 | case EXT4_IOC_MOVE_EXT: { | |
220 | struct move_extent me; | |
221 | struct file *donor_filp; | |
222 | int err; | |
223 | ||
4a58579b AF |
224 | if (!(filp->f_mode & FMODE_READ) || |
225 | !(filp->f_mode & FMODE_WRITE)) | |
226 | return -EBADF; | |
227 | ||
748de673 AF |
228 | if (copy_from_user(&me, |
229 | (struct move_extent __user *)arg, sizeof(me))) | |
230 | return -EFAULT; | |
4a58579b | 231 | me.moved_len = 0; |
748de673 AF |
232 | |
233 | donor_filp = fget(me.donor_fd); | |
234 | if (!donor_filp) | |
235 | return -EBADF; | |
236 | ||
4a58579b AF |
237 | if (!(donor_filp->f_mode & FMODE_WRITE)) { |
238 | err = -EBADF; | |
239 | goto mext_out; | |
748de673 AF |
240 | } |
241 | ||
4a58579b AF |
242 | err = mnt_want_write(filp->f_path.mnt); |
243 | if (err) | |
244 | goto mext_out; | |
245 | ||
748de673 AF |
246 | err = ext4_move_extents(filp, donor_filp, me.orig_start, |
247 | me.donor_start, me.len, &me.moved_len); | |
4a58579b AF |
248 | mnt_drop_write(filp->f_path.mnt); |
249 | if (me.moved_len > 0) | |
250 | file_remove_suid(donor_filp); | |
748de673 | 251 | |
8d666913 | 252 | if (copy_to_user((struct move_extent *)arg, &me, sizeof(me))) |
4a58579b AF |
253 | err = -EFAULT; |
254 | mext_out: | |
255 | fput(donor_filp); | |
748de673 AF |
256 | return err; |
257 | } | |
258 | ||
617ba13b MC |
259 | case EXT4_IOC_GROUP_ADD: { |
260 | struct ext4_new_group_data input; | |
ac27a0ec | 261 | struct super_block *sb = inode->i_sb; |
ac046f1d | 262 | int err, err2=0; |
ac27a0ec DK |
263 | |
264 | if (!capable(CAP_SYS_RESOURCE)) | |
265 | return -EPERM; | |
266 | ||
617ba13b | 267 | if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg, |
ac27a0ec DK |
268 | sizeof(input))) |
269 | return -EFAULT; | |
270 | ||
42a74f20 DH |
271 | err = mnt_want_write(filp->f_path.mnt); |
272 | if (err) | |
273 | return err; | |
274 | ||
617ba13b | 275 | err = ext4_group_add(sb, &input); |
ac046f1d PT |
276 | if (EXT4_SB(sb)->s_journal) { |
277 | jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); | |
278 | err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal); | |
279 | jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); | |
280 | } | |
7ffe1ea8 HK |
281 | if (err == 0) |
282 | err = err2; | |
42a74f20 | 283 | mnt_drop_write(filp->f_path.mnt); |
ac27a0ec DK |
284 | |
285 | return err; | |
286 | } | |
287 | ||
c14c6fd5 | 288 | case EXT4_IOC_MIGRATE: |
2a43a878 AK |
289 | { |
290 | int err; | |
291 | if (!is_owner_or_cap(inode)) | |
292 | return -EACCES; | |
293 | ||
294 | err = mnt_want_write(filp->f_path.mnt); | |
295 | if (err) | |
296 | return err; | |
297 | /* | |
298 | * inode_mutex prevent write and truncate on the file. | |
299 | * Read still goes through. We take i_data_sem in | |
300 | * ext4_ext_swap_inode_data before we switch the | |
301 | * inode format to prevent read. | |
302 | */ | |
303 | mutex_lock(&(inode->i_mutex)); | |
304 | err = ext4_ext_migrate(inode); | |
305 | mutex_unlock(&(inode->i_mutex)); | |
306 | mnt_drop_write(filp->f_path.mnt); | |
307 | return err; | |
308 | } | |
c14c6fd5 | 309 | |
ccd2506b TT |
310 | case EXT4_IOC_ALLOC_DA_BLKS: |
311 | { | |
312 | int err; | |
313 | if (!is_owner_or_cap(inode)) | |
314 | return -EACCES; | |
315 | ||
316 | err = mnt_want_write(filp->f_path.mnt); | |
317 | if (err) | |
318 | return err; | |
319 | err = ext4_alloc_da_blocks(inode); | |
320 | mnt_drop_write(filp->f_path.mnt); | |
321 | return err; | |
322 | } | |
323 | ||
ac27a0ec DK |
324 | default: |
325 | return -ENOTTY; | |
326 | } | |
327 | } | |
328 | ||
329 | #ifdef CONFIG_COMPAT | |
617ba13b | 330 | long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
ac27a0ec | 331 | { |
ac27a0ec DK |
332 | /* These are just misnamed, they actually get/put from/to user an int */ |
333 | switch (cmd) { | |
617ba13b MC |
334 | case EXT4_IOC32_GETFLAGS: |
335 | cmd = EXT4_IOC_GETFLAGS; | |
ac27a0ec | 336 | break; |
617ba13b MC |
337 | case EXT4_IOC32_SETFLAGS: |
338 | cmd = EXT4_IOC_SETFLAGS; | |
ac27a0ec | 339 | break; |
617ba13b MC |
340 | case EXT4_IOC32_GETVERSION: |
341 | cmd = EXT4_IOC_GETVERSION; | |
ac27a0ec | 342 | break; |
617ba13b MC |
343 | case EXT4_IOC32_SETVERSION: |
344 | cmd = EXT4_IOC_SETVERSION; | |
ac27a0ec | 345 | break; |
617ba13b MC |
346 | case EXT4_IOC32_GROUP_EXTEND: |
347 | cmd = EXT4_IOC_GROUP_EXTEND; | |
ac27a0ec | 348 | break; |
617ba13b MC |
349 | case EXT4_IOC32_GETVERSION_OLD: |
350 | cmd = EXT4_IOC_GETVERSION_OLD; | |
ac27a0ec | 351 | break; |
617ba13b MC |
352 | case EXT4_IOC32_SETVERSION_OLD: |
353 | cmd = EXT4_IOC_SETVERSION_OLD; | |
ac27a0ec | 354 | break; |
e23291b9 | 355 | #ifdef CONFIG_JBD2_DEBUG |
617ba13b MC |
356 | case EXT4_IOC32_WAIT_FOR_READONLY: |
357 | cmd = EXT4_IOC_WAIT_FOR_READONLY; | |
ac27a0ec DK |
358 | break; |
359 | #endif | |
617ba13b MC |
360 | case EXT4_IOC32_GETRSVSZ: |
361 | cmd = EXT4_IOC_GETRSVSZ; | |
ac27a0ec | 362 | break; |
617ba13b MC |
363 | case EXT4_IOC32_SETRSVSZ: |
364 | cmd = EXT4_IOC_SETRSVSZ; | |
ac27a0ec | 365 | break; |
617ba13b | 366 | case EXT4_IOC_GROUP_ADD: |
ac27a0ec DK |
367 | break; |
368 | default: | |
369 | return -ENOIOCTLCMD; | |
370 | } | |
5cdd7b2d | 371 | return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); |
ac27a0ec DK |
372 | } |
373 | #endif |