2 * linux/fs/ext4/ioctl.c
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)
11 #include <linux/jbd2.h>
12 #include <linux/capability.h>
13 #include <linux/time.h>
14 #include <linux/compat.h>
15 #include <linux/mount.h>
16 #include <linux/file.h>
17 #include <asm/uaccess.h>
18 #include "ext4_jbd2.h"
21 #define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1)
23 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
25 struct inode *inode = filp->f_dentry->d_inode;
26 struct super_block *sb = inode->i_sb;
27 struct ext4_inode_info *ei = EXT4_I(inode);
30 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
33 case EXT4_IOC_GETFLAGS:
34 ext4_get_inode_flags(ei);
35 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
36 return put_user(flags, (int __user *) arg);
37 case EXT4_IOC_SETFLAGS: {
38 handle_t *handle = NULL;
40 struct ext4_iloc iloc;
41 unsigned int oldflags;
44 if (!inode_owner_or_capable(inode))
47 if (get_user(flags, (int __user *) arg))
50 err = mnt_want_write(filp->f_path.mnt);
54 flags = ext4_mask_flags(inode->i_mode, flags);
57 mutex_lock(&inode->i_mutex);
58 /* Is it quota file? Do not allow user to mess with it */
59 if (IS_NOQUOTA(inode))
62 oldflags = ei->i_flags;
64 /* The JOURNAL_DATA flag is modifiable only by root */
65 jflag = flags & EXT4_JOURNAL_DATA_FL;
68 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
69 * the relevant capability.
71 * This test looks nicer. Thanks to Pauline Middelink
73 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
74 if (!capable(CAP_LINUX_IMMUTABLE))
79 * The JOURNAL_DATA flag can only be changed by
80 * the relevant capability.
82 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
83 if (!capable(CAP_SYS_RESOURCE))
86 if (oldflags & EXT4_EXTENTS_FL) {
87 /* We don't support clearning extent flags */
88 if (!(flags & EXT4_EXTENTS_FL)) {
92 } else if (flags & EXT4_EXTENTS_FL) {
93 /* migrate the file */
95 flags &= ~EXT4_EXTENTS_FL;
98 if (flags & EXT4_EOFBLOCKS_FL) {
99 /* we don't support adding EOFBLOCKS flag */
100 if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
104 } else if (oldflags & EXT4_EOFBLOCKS_FL)
105 ext4_truncate(inode);
107 handle = ext4_journal_start(inode, 1);
108 if (IS_ERR(handle)) {
109 err = PTR_ERR(handle);
113 ext4_handle_sync(handle);
114 err = ext4_reserve_inode_write(handle, inode, &iloc);
118 flags = flags & EXT4_FL_USER_MODIFIABLE;
119 flags |= oldflags & ~EXT4_FL_USER_MODIFIABLE;
122 ext4_set_inode_flags(inode);
123 inode->i_ctime = ext4_current_time(inode);
125 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
127 ext4_journal_stop(handle);
131 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
132 err = ext4_change_inode_journal_flag(inode, jflag);
136 err = ext4_ext_migrate(inode);
138 mutex_unlock(&inode->i_mutex);
139 mnt_drop_write(filp->f_path.mnt);
142 case EXT4_IOC_GETVERSION:
143 case EXT4_IOC_GETVERSION_OLD:
144 return put_user(inode->i_generation, (int __user *) arg);
145 case EXT4_IOC_SETVERSION:
146 case EXT4_IOC_SETVERSION_OLD: {
148 struct ext4_iloc iloc;
152 if (!inode_owner_or_capable(inode))
155 err = mnt_want_write(filp->f_path.mnt);
158 if (get_user(generation, (int __user *) arg)) {
163 handle = ext4_journal_start(inode, 1);
164 if (IS_ERR(handle)) {
165 err = PTR_ERR(handle);
168 err = ext4_reserve_inode_write(handle, inode, &iloc);
170 inode->i_ctime = ext4_current_time(inode);
171 inode->i_generation = generation;
172 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
174 ext4_journal_stop(handle);
176 mnt_drop_write(filp->f_path.mnt);
179 case EXT4_IOC_GROUP_EXTEND: {
180 ext4_fsblk_t n_blocks_count;
183 err = ext4_resize_begin(sb);
187 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
189 goto group_extend_out;
192 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
193 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
194 ext4_msg(sb, KERN_ERR,
195 "Online resizing not supported with bigalloc");
197 goto group_extend_out;
200 err = mnt_want_write(filp->f_path.mnt);
202 goto group_extend_out;
204 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
205 if (EXT4_SB(sb)->s_journal) {
206 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
207 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
208 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
213 mnt_drop_write(filp->f_path.mnt);
219 case EXT4_IOC_MOVE_EXT: {
220 struct move_extent me;
221 struct file *donor_filp;
224 if (!(filp->f_mode & FMODE_READ) ||
225 !(filp->f_mode & FMODE_WRITE))
228 if (copy_from_user(&me,
229 (struct move_extent __user *)arg, sizeof(me)))
233 donor_filp = fget(me.donor_fd);
237 if (!(donor_filp->f_mode & FMODE_WRITE)) {
242 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
243 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
244 ext4_msg(sb, KERN_ERR,
245 "Online defrag not supported with bigalloc");
249 err = mnt_want_write(filp->f_path.mnt);
253 err = ext4_move_extents(filp, donor_filp, me.orig_start,
254 me.donor_start, me.len, &me.moved_len);
255 mnt_drop_write(filp->f_path.mnt);
257 if (copy_to_user((struct move_extent __user *)arg,
265 case EXT4_IOC_GROUP_ADD: {
266 struct ext4_new_group_data input;
269 err = ext4_resize_begin(sb);
273 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
279 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
280 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
281 ext4_msg(sb, KERN_ERR,
282 "Online resizing not supported with bigalloc");
287 err = mnt_want_write(filp->f_path.mnt);
291 err = ext4_group_add(sb, &input);
292 if (EXT4_SB(sb)->s_journal) {
293 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
294 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
295 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
300 mnt_drop_write(filp->f_path.mnt);
306 case EXT4_IOC_MIGRATE:
309 if (!inode_owner_or_capable(inode))
312 err = mnt_want_write(filp->f_path.mnt);
316 * inode_mutex prevent write and truncate on the file.
317 * Read still goes through. We take i_data_sem in
318 * ext4_ext_swap_inode_data before we switch the
319 * inode format to prevent read.
321 mutex_lock(&(inode->i_mutex));
322 err = ext4_ext_migrate(inode);
323 mutex_unlock(&(inode->i_mutex));
324 mnt_drop_write(filp->f_path.mnt);
328 case EXT4_IOC_ALLOC_DA_BLKS:
331 if (!inode_owner_or_capable(inode))
334 err = mnt_want_write(filp->f_path.mnt);
337 err = ext4_alloc_da_blocks(inode);
338 mnt_drop_write(filp->f_path.mnt);
342 case EXT4_IOC_RESIZE_FS: {
343 ext4_fsblk_t n_blocks_count;
344 struct super_block *sb = inode->i_sb;
345 int err = 0, err2 = 0;
347 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
348 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
349 ext4_msg(sb, KERN_ERR,
350 "Online resizing not (yet) supported with bigalloc");
354 if (EXT4_HAS_INCOMPAT_FEATURE(sb,
355 EXT4_FEATURE_INCOMPAT_META_BG)) {
356 ext4_msg(sb, KERN_ERR,
357 "Online resizing not (yet) supported with meta_bg");
361 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
366 if (n_blocks_count > MAX_32_NUM &&
367 !EXT4_HAS_INCOMPAT_FEATURE(sb,
368 EXT4_FEATURE_INCOMPAT_64BIT)) {
369 ext4_msg(sb, KERN_ERR,
370 "File system only supports 32-bit block numbers");
374 err = ext4_resize_begin(sb);
378 err = mnt_want_write(filp->f_path.mnt);
382 err = ext4_resize_fs(sb, n_blocks_count);
383 if (EXT4_SB(sb)->s_journal) {
384 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
385 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
386 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
390 mnt_drop_write(filp->f_path.mnt);
398 struct request_queue *q = bdev_get_queue(sb->s_bdev);
399 struct fstrim_range range;
402 if (!capable(CAP_SYS_ADMIN))
405 if (!blk_queue_discard(q))
408 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
409 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
410 ext4_msg(sb, KERN_ERR,
411 "FITRIM not supported with bigalloc");
415 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
419 range.minlen = max((unsigned int)range.minlen,
420 q->limits.discard_granularity);
421 ret = ext4_trim_fs(sb, &range);
425 if (copy_to_user((struct fstrim_range __user *)arg, &range,
438 long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
440 /* These are just misnamed, they actually get/put from/to user an int */
442 case EXT4_IOC32_GETFLAGS:
443 cmd = EXT4_IOC_GETFLAGS;
445 case EXT4_IOC32_SETFLAGS:
446 cmd = EXT4_IOC_SETFLAGS;
448 case EXT4_IOC32_GETVERSION:
449 cmd = EXT4_IOC_GETVERSION;
451 case EXT4_IOC32_SETVERSION:
452 cmd = EXT4_IOC_SETVERSION;
454 case EXT4_IOC32_GROUP_EXTEND:
455 cmd = EXT4_IOC_GROUP_EXTEND;
457 case EXT4_IOC32_GETVERSION_OLD:
458 cmd = EXT4_IOC_GETVERSION_OLD;
460 case EXT4_IOC32_SETVERSION_OLD:
461 cmd = EXT4_IOC_SETVERSION_OLD;
463 case EXT4_IOC32_GETRSVSZ:
464 cmd = EXT4_IOC_GETRSVSZ;
466 case EXT4_IOC32_SETRSVSZ:
467 cmd = EXT4_IOC_SETRSVSZ;
469 case EXT4_IOC32_GROUP_ADD: {
470 struct compat_ext4_new_group_input __user *uinput;
471 struct ext4_new_group_input input;
475 uinput = compat_ptr(arg);
476 err = get_user(input.group, &uinput->group);
477 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
478 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
479 err |= get_user(input.inode_table, &uinput->inode_table);
480 err |= get_user(input.blocks_count, &uinput->blocks_count);
481 err |= get_user(input.reserved_blocks,
482 &uinput->reserved_blocks);
487 err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
488 (unsigned long) &input);
492 case EXT4_IOC_MOVE_EXT:
494 case EXT4_IOC_RESIZE_FS:
499 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));