]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/ext4/ioctl.c
ext4: let ext4_group_add() use common code
[mirror_ubuntu-zesty-kernel.git] / fs / ext4 / ioctl.c
CommitLineData
ac27a0ec 1/*
617ba13b 2 * linux/fs/ext4/ioctl.c
ac27a0ec
DK
3 *
4 * Copyright (C) 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
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
19c5246d
YY
21#define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1)
22
5cdd7b2d 23long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
ac27a0ec 24{
5cdd7b2d 25 struct inode *inode = filp->f_dentry->d_inode;
bab08ab9 26 struct super_block *sb = inode->i_sb;
617ba13b 27 struct ext4_inode_info *ei = EXT4_I(inode);
ac27a0ec 28 unsigned int flags;
ac27a0ec 29
af5bc92d 30 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
ac27a0ec
DK
31
32 switch (cmd) {
617ba13b 33 case EXT4_IOC_GETFLAGS:
ff9ddf7e 34 ext4_get_inode_flags(ei);
617ba13b 35 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
ac27a0ec 36 return put_user(flags, (int __user *) arg);
617ba13b 37 case EXT4_IOC_SETFLAGS: {
ac27a0ec 38 handle_t *handle = NULL;
4db46fc2 39 int err, migrate = 0;
617ba13b 40 struct ext4_iloc iloc;
ac27a0ec
DK
41 unsigned int oldflags;
42 unsigned int jflag;
43
2e149670 44 if (!inode_owner_or_capable(inode))
ac27a0ec
DK
45 return -EACCES;
46
47 if (get_user(flags, (int __user *) arg))
48 return -EFAULT;
49
42a74f20
DH
50 err = mnt_want_write(filp->f_path.mnt);
51 if (err)
52 return err;
53
2dc6b0d4 54 flags = ext4_mask_flags(inode->i_mode, flags);
ac27a0ec 55
42a74f20 56 err = -EPERM;
ac27a0ec 57 mutex_lock(&inode->i_mutex);
e47776a0 58 /* Is it quota file? Do not allow user to mess with it */
42a74f20
DH
59 if (IS_NOQUOTA(inode))
60 goto flags_out;
61
ac27a0ec
DK
62 oldflags = ei->i_flags;
63
64 /* The JOURNAL_DATA flag is modifiable only by root */
617ba13b 65 jflag = flags & EXT4_JOURNAL_DATA_FL;
ac27a0ec
DK
66
67 /*
68 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
69 * the relevant capability.
70 *
71 * This test looks nicer. Thanks to Pauline Middelink
72 */
617ba13b 73 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
42a74f20
DH
74 if (!capable(CAP_LINUX_IMMUTABLE))
75 goto flags_out;
ac27a0ec
DK
76 }
77
78 /*
79 * The JOURNAL_DATA flag can only be changed by
80 * the relevant capability.
81 */
617ba13b 82 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
42a74f20
DH
83 if (!capable(CAP_SYS_RESOURCE))
84 goto flags_out;
ac27a0ec 85 }
4db46fc2
AK
86 if (oldflags & EXT4_EXTENTS_FL) {
87 /* We don't support clearning extent flags */
88 if (!(flags & EXT4_EXTENTS_FL)) {
89 err = -EOPNOTSUPP;
90 goto flags_out;
91 }
92 } else if (flags & EXT4_EXTENTS_FL) {
93 /* migrate the file */
94 migrate = 1;
95 flags &= ~EXT4_EXTENTS_FL;
96 }
ac27a0ec 97
c8d46e41
JZ
98 if (flags & EXT4_EOFBLOCKS_FL) {
99 /* we don't support adding EOFBLOCKS flag */
100 if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
101 err = -EOPNOTSUPP;
102 goto flags_out;
103 }
104 } else if (oldflags & EXT4_EOFBLOCKS_FL)
105 ext4_truncate(inode);
106
617ba13b 107 handle = ext4_journal_start(inode, 1);
ac27a0ec 108 if (IS_ERR(handle)) {
42a74f20
DH
109 err = PTR_ERR(handle);
110 goto flags_out;
ac27a0ec
DK
111 }
112 if (IS_SYNC(inode))
0390131b 113 ext4_handle_sync(handle);
617ba13b 114 err = ext4_reserve_inode_write(handle, inode, &iloc);
ac27a0ec
DK
115 if (err)
116 goto flags_err;
117
617ba13b
MC
118 flags = flags & EXT4_FL_USER_MODIFIABLE;
119 flags |= oldflags & ~EXT4_FL_USER_MODIFIABLE;
ac27a0ec
DK
120 ei->i_flags = flags;
121
617ba13b 122 ext4_set_inode_flags(inode);
ef7f3835 123 inode->i_ctime = ext4_current_time(inode);
ac27a0ec 124
617ba13b 125 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
ac27a0ec 126flags_err:
617ba13b 127 ext4_journal_stop(handle);
42a74f20
DH
128 if (err)
129 goto flags_out;
ac27a0ec 130
617ba13b
MC
131 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
132 err = ext4_change_inode_journal_flag(inode, jflag);
4db46fc2
AK
133 if (err)
134 goto flags_out;
135 if (migrate)
136 err = ext4_ext_migrate(inode);
42a74f20 137flags_out:
ac27a0ec 138 mutex_unlock(&inode->i_mutex);
42a74f20 139 mnt_drop_write(filp->f_path.mnt);
ac27a0ec
DK
140 return err;
141 }
617ba13b
MC
142 case EXT4_IOC_GETVERSION:
143 case EXT4_IOC_GETVERSION_OLD:
ac27a0ec 144 return put_user(inode->i_generation, (int __user *) arg);
617ba13b
MC
145 case EXT4_IOC_SETVERSION:
146 case EXT4_IOC_SETVERSION_OLD: {
ac27a0ec 147 handle_t *handle;
617ba13b 148 struct ext4_iloc iloc;
ac27a0ec
DK
149 __u32 generation;
150 int err;
151
2e149670 152 if (!inode_owner_or_capable(inode))
ac27a0ec 153 return -EPERM;
42a74f20
DH
154
155 err = mnt_want_write(filp->f_path.mnt);
156 if (err)
157 return err;
158 if (get_user(generation, (int __user *) arg)) {
159 err = -EFAULT;
160 goto setversion_out;
161 }
ac27a0ec 162
617ba13b 163 handle = ext4_journal_start(inode, 1);
42a74f20
DH
164 if (IS_ERR(handle)) {
165 err = PTR_ERR(handle);
166 goto setversion_out;
167 }
617ba13b 168 err = ext4_reserve_inode_write(handle, inode, &iloc);
ac27a0ec 169 if (err == 0) {
ef7f3835 170 inode->i_ctime = ext4_current_time(inode);
ac27a0ec 171 inode->i_generation = generation;
617ba13b 172 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
ac27a0ec 173 }
617ba13b 174 ext4_journal_stop(handle);
42a74f20
DH
175setversion_out:
176 mnt_drop_write(filp->f_path.mnt);
ac27a0ec
DK
177 return err;
178 }
617ba13b
MC
179 case EXT4_IOC_GROUP_EXTEND: {
180 ext4_fsblk_t n_blocks_count;
ac046f1d 181 int err, err2=0;
ac27a0ec 182
8f82f840
YY
183 err = ext4_resize_begin(sb);
184 if (err)
185 return err;
ac27a0ec 186
ac27a0ec
DK
187 if (get_user(n_blocks_count, (__u32 __user *)arg))
188 return -EFAULT;
189
bab08ab9
TT
190 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
191 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
192 ext4_msg(sb, KERN_ERR,
193 "Online resizing not supported with bigalloc");
194 return -EOPNOTSUPP;
195 }
196
42a74f20
DH
197 err = mnt_want_write(filp->f_path.mnt);
198 if (err)
199 return err;
200
617ba13b 201 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
ac046f1d
PT
202 if (EXT4_SB(sb)->s_journal) {
203 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
204 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
205 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
206 }
7ffe1ea8
HK
207 if (err == 0)
208 err = err2;
42a74f20 209 mnt_drop_write(filp->f_path.mnt);
8f82f840 210 ext4_resize_end(sb);
ac27a0ec
DK
211
212 return err;
213 }
748de673
AF
214
215 case EXT4_IOC_MOVE_EXT: {
216 struct move_extent me;
217 struct file *donor_filp;
218 int err;
219
4a58579b
AF
220 if (!(filp->f_mode & FMODE_READ) ||
221 !(filp->f_mode & FMODE_WRITE))
222 return -EBADF;
223
748de673
AF
224 if (copy_from_user(&me,
225 (struct move_extent __user *)arg, sizeof(me)))
226 return -EFAULT;
4a58579b 227 me.moved_len = 0;
748de673
AF
228
229 donor_filp = fget(me.donor_fd);
230 if (!donor_filp)
231 return -EBADF;
232
4a58579b
AF
233 if (!(donor_filp->f_mode & FMODE_WRITE)) {
234 err = -EBADF;
235 goto mext_out;
748de673
AF
236 }
237
bab08ab9
TT
238 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
239 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
240 ext4_msg(sb, KERN_ERR,
241 "Online defrag not supported with bigalloc");
242 return -EOPNOTSUPP;
243 }
244
4a58579b
AF
245 err = mnt_want_write(filp->f_path.mnt);
246 if (err)
247 goto mext_out;
248
748de673
AF
249 err = ext4_move_extents(filp, donor_filp, me.orig_start,
250 me.donor_start, me.len, &me.moved_len);
4a58579b 251 mnt_drop_write(filp->f_path.mnt);
748de673 252
60e6679e 253 if (copy_to_user((struct move_extent __user *)arg,
c437b273 254 &me, sizeof(me)))
4a58579b
AF
255 err = -EFAULT;
256mext_out:
257 fput(donor_filp);
748de673
AF
258 return err;
259 }
260
617ba13b
MC
261 case EXT4_IOC_GROUP_ADD: {
262 struct ext4_new_group_data input;
ac046f1d 263 int err, err2=0;
ac27a0ec 264
8f82f840
YY
265 err = ext4_resize_begin(sb);
266 if (err)
267 return err;
ac27a0ec 268
617ba13b 269 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
ac27a0ec
DK
270 sizeof(input)))
271 return -EFAULT;
272
bab08ab9
TT
273 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
274 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
275 ext4_msg(sb, KERN_ERR,
276 "Online resizing not supported with bigalloc");
277 return -EOPNOTSUPP;
278 }
279
42a74f20
DH
280 err = mnt_want_write(filp->f_path.mnt);
281 if (err)
282 return err;
283
617ba13b 284 err = ext4_group_add(sb, &input);
ac046f1d
PT
285 if (EXT4_SB(sb)->s_journal) {
286 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
287 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
288 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
289 }
7ffe1ea8
HK
290 if (err == 0)
291 err = err2;
42a74f20 292 mnt_drop_write(filp->f_path.mnt);
8f82f840 293 ext4_resize_end(sb);
ac27a0ec
DK
294
295 return err;
296 }
297
c14c6fd5 298 case EXT4_IOC_MIGRATE:
2a43a878
AK
299 {
300 int err;
2e149670 301 if (!inode_owner_or_capable(inode))
2a43a878
AK
302 return -EACCES;
303
304 err = mnt_want_write(filp->f_path.mnt);
305 if (err)
306 return err;
307 /*
308 * inode_mutex prevent write and truncate on the file.
309 * Read still goes through. We take i_data_sem in
310 * ext4_ext_swap_inode_data before we switch the
311 * inode format to prevent read.
312 */
313 mutex_lock(&(inode->i_mutex));
314 err = ext4_ext_migrate(inode);
315 mutex_unlock(&(inode->i_mutex));
316 mnt_drop_write(filp->f_path.mnt);
317 return err;
318 }
c14c6fd5 319
ccd2506b
TT
320 case EXT4_IOC_ALLOC_DA_BLKS:
321 {
322 int err;
2e149670 323 if (!inode_owner_or_capable(inode))
ccd2506b
TT
324 return -EACCES;
325
326 err = mnt_want_write(filp->f_path.mnt);
327 if (err)
328 return err;
329 err = ext4_alloc_da_blocks(inode);
330 mnt_drop_write(filp->f_path.mnt);
331 return err;
332 }
333
19c5246d
YY
334 case EXT4_IOC_RESIZE_FS: {
335 ext4_fsblk_t n_blocks_count;
336 struct super_block *sb = inode->i_sb;
337 int err = 0, err2 = 0;
338
339 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
340 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
341 ext4_msg(sb, KERN_ERR,
342 "Online resizing not (yet) supported with bigalloc");
343 return -EOPNOTSUPP;
344 }
345
346 if (EXT4_HAS_INCOMPAT_FEATURE(sb,
347 EXT4_FEATURE_INCOMPAT_META_BG)) {
348 ext4_msg(sb, KERN_ERR,
349 "Online resizing not (yet) supported with meta_bg");
350 return -EOPNOTSUPP;
351 }
352
353 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
354 sizeof(__u64))) {
355 return -EFAULT;
356 }
357
358 if (n_blocks_count > MAX_32_NUM &&
359 !EXT4_HAS_INCOMPAT_FEATURE(sb,
360 EXT4_FEATURE_INCOMPAT_64BIT)) {
361 ext4_msg(sb, KERN_ERR,
362 "File system only supports 32-bit block numbers");
363 return -EOPNOTSUPP;
364 }
365
366 err = ext4_resize_begin(sb);
367 if (err)
368 return err;
369
370 err = mnt_want_write(filp->f_path.mnt);
371 if (err)
372 goto resizefs_out;
373
374 err = ext4_resize_fs(sb, n_blocks_count);
375 if (EXT4_SB(sb)->s_journal) {
376 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
377 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
378 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
379 }
380 if (err == 0)
381 err = err2;
382 mnt_drop_write(filp->f_path.mnt);
383resizefs_out:
384 ext4_resize_end(sb);
385 return err;
386 }
387
e681c047
LC
388 case FITRIM:
389 {
41431792 390 struct request_queue *q = bdev_get_queue(sb->s_bdev);
e681c047
LC
391 struct fstrim_range range;
392 int ret = 0;
393
394 if (!capable(CAP_SYS_ADMIN))
395 return -EPERM;
396
41431792
LC
397 if (!blk_queue_discard(q))
398 return -EOPNOTSUPP;
399
bab08ab9
TT
400 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
401 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
402 ext4_msg(sb, KERN_ERR,
403 "FITRIM not supported with bigalloc");
404 return -EOPNOTSUPP;
405 }
406
e6705f7c 407 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
e681c047
LC
408 sizeof(range)))
409 return -EFAULT;
410
5c2ed62f
LC
411 range.minlen = max((unsigned int)range.minlen,
412 q->limits.discard_granularity);
e681c047
LC
413 ret = ext4_trim_fs(sb, &range);
414 if (ret < 0)
415 return ret;
416
e6705f7c 417 if (copy_to_user((struct fstrim_range __user *)arg, &range,
e681c047
LC
418 sizeof(range)))
419 return -EFAULT;
420
421 return 0;
422 }
423
ac27a0ec
DK
424 default:
425 return -ENOTTY;
426 }
427}
428
429#ifdef CONFIG_COMPAT
617ba13b 430long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
ac27a0ec 431{
ac27a0ec
DK
432 /* These are just misnamed, they actually get/put from/to user an int */
433 switch (cmd) {
617ba13b
MC
434 case EXT4_IOC32_GETFLAGS:
435 cmd = EXT4_IOC_GETFLAGS;
ac27a0ec 436 break;
617ba13b
MC
437 case EXT4_IOC32_SETFLAGS:
438 cmd = EXT4_IOC_SETFLAGS;
ac27a0ec 439 break;
617ba13b
MC
440 case EXT4_IOC32_GETVERSION:
441 cmd = EXT4_IOC_GETVERSION;
ac27a0ec 442 break;
617ba13b
MC
443 case EXT4_IOC32_SETVERSION:
444 cmd = EXT4_IOC_SETVERSION;
ac27a0ec 445 break;
617ba13b
MC
446 case EXT4_IOC32_GROUP_EXTEND:
447 cmd = EXT4_IOC_GROUP_EXTEND;
ac27a0ec 448 break;
617ba13b
MC
449 case EXT4_IOC32_GETVERSION_OLD:
450 cmd = EXT4_IOC_GETVERSION_OLD;
ac27a0ec 451 break;
617ba13b
MC
452 case EXT4_IOC32_SETVERSION_OLD:
453 cmd = EXT4_IOC_SETVERSION_OLD;
ac27a0ec 454 break;
617ba13b
MC
455 case EXT4_IOC32_GETRSVSZ:
456 cmd = EXT4_IOC_GETRSVSZ;
ac27a0ec 457 break;
617ba13b
MC
458 case EXT4_IOC32_SETRSVSZ:
459 cmd = EXT4_IOC_SETRSVSZ;
ac27a0ec 460 break;
4d92dc0f
BH
461 case EXT4_IOC32_GROUP_ADD: {
462 struct compat_ext4_new_group_input __user *uinput;
463 struct ext4_new_group_input input;
464 mm_segment_t old_fs;
465 int err;
466
467 uinput = compat_ptr(arg);
468 err = get_user(input.group, &uinput->group);
469 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
470 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
471 err |= get_user(input.inode_table, &uinput->inode_table);
472 err |= get_user(input.blocks_count, &uinput->blocks_count);
473 err |= get_user(input.reserved_blocks,
474 &uinput->reserved_blocks);
475 if (err)
476 return -EFAULT;
477 old_fs = get_fs();
478 set_fs(KERNEL_DS);
479 err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
480 (unsigned long) &input);
481 set_fs(old_fs);
482 return err;
483 }
b684b2ee 484 case EXT4_IOC_MOVE_EXT:
a56e69c2 485 case FITRIM:
19c5246d 486 case EXT4_IOC_RESIZE_FS:
b684b2ee 487 break;
ac27a0ec
DK
488 default:
489 return -ENOIOCTLCMD;
490 }
5cdd7b2d 491 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
ac27a0ec
DK
492}
493#endif