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 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
23 struct inode *inode = filp->f_dentry->d_inode;
24 struct super_block *sb = inode->i_sb;
25 struct ext4_inode_info *ei = EXT4_I(inode);
28 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
31 case EXT4_IOC_GETFLAGS:
32 ext4_get_inode_flags(ei);
33 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
34 return put_user(flags, (int __user *) arg);
35 case EXT4_IOC_SETFLAGS: {
36 handle_t *handle = NULL;
38 struct ext4_iloc iloc;
39 unsigned int oldflags;
42 if (!inode_owner_or_capable(inode))
45 if (get_user(flags, (int __user *) arg))
48 err = mnt_want_write(filp->f_path.mnt);
52 flags = ext4_mask_flags(inode->i_mode, flags);
55 mutex_lock(&inode->i_mutex);
56 /* Is it quota file? Do not allow user to mess with it */
57 if (IS_NOQUOTA(inode))
60 oldflags = ei->i_flags;
62 /* The JOURNAL_DATA flag is modifiable only by root */
63 jflag = flags & EXT4_JOURNAL_DATA_FL;
66 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
67 * the relevant capability.
69 * This test looks nicer. Thanks to Pauline Middelink
71 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
72 if (!capable(CAP_LINUX_IMMUTABLE))
77 * The JOURNAL_DATA flag can only be changed by
78 * the relevant capability.
80 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
81 if (!capable(CAP_SYS_RESOURCE))
84 if (oldflags & EXT4_EXTENTS_FL) {
85 /* We don't support clearning extent flags */
86 if (!(flags & EXT4_EXTENTS_FL)) {
90 } else if (flags & EXT4_EXTENTS_FL) {
91 /* migrate the file */
93 flags &= ~EXT4_EXTENTS_FL;
96 if (flags & EXT4_EOFBLOCKS_FL) {
97 /* we don't support adding EOFBLOCKS flag */
98 if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
102 } else if (oldflags & EXT4_EOFBLOCKS_FL)
103 ext4_truncate(inode);
105 handle = ext4_journal_start(inode, 1);
106 if (IS_ERR(handle)) {
107 err = PTR_ERR(handle);
111 ext4_handle_sync(handle);
112 err = ext4_reserve_inode_write(handle, inode, &iloc);
116 flags = flags & EXT4_FL_USER_MODIFIABLE;
117 flags |= oldflags & ~EXT4_FL_USER_MODIFIABLE;
120 ext4_set_inode_flags(inode);
121 inode->i_ctime = ext4_current_time(inode);
123 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
125 ext4_journal_stop(handle);
129 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
130 err = ext4_change_inode_journal_flag(inode, jflag);
134 err = ext4_ext_migrate(inode);
136 mutex_unlock(&inode->i_mutex);
137 mnt_drop_write(filp->f_path.mnt);
140 case EXT4_IOC_GETVERSION:
141 case EXT4_IOC_GETVERSION_OLD:
142 return put_user(inode->i_generation, (int __user *) arg);
143 case EXT4_IOC_SETVERSION:
144 case EXT4_IOC_SETVERSION_OLD: {
146 struct ext4_iloc iloc;
150 if (!inode_owner_or_capable(inode))
153 err = mnt_want_write(filp->f_path.mnt);
156 if (get_user(generation, (int __user *) arg)) {
161 handle = ext4_journal_start(inode, 1);
162 if (IS_ERR(handle)) {
163 err = PTR_ERR(handle);
166 err = ext4_reserve_inode_write(handle, inode, &iloc);
168 inode->i_ctime = ext4_current_time(inode);
169 inode->i_generation = generation;
170 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
172 ext4_journal_stop(handle);
174 mnt_drop_write(filp->f_path.mnt);
177 case EXT4_IOC_GROUP_EXTEND: {
178 ext4_fsblk_t n_blocks_count;
181 err = ext4_resize_begin(sb);
185 if (get_user(n_blocks_count, (__u32 __user *)arg))
188 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
189 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
190 ext4_msg(sb, KERN_ERR,
191 "Online resizing not supported with bigalloc");
195 err = mnt_want_write(filp->f_path.mnt);
199 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
200 if (EXT4_SB(sb)->s_journal) {
201 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
202 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
203 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
207 mnt_drop_write(filp->f_path.mnt);
213 case EXT4_IOC_MOVE_EXT: {
214 struct move_extent me;
215 struct file *donor_filp;
218 if (!(filp->f_mode & FMODE_READ) ||
219 !(filp->f_mode & FMODE_WRITE))
222 if (copy_from_user(&me,
223 (struct move_extent __user *)arg, sizeof(me)))
227 donor_filp = fget(me.donor_fd);
231 if (!(donor_filp->f_mode & FMODE_WRITE)) {
236 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
237 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
238 ext4_msg(sb, KERN_ERR,
239 "Online defrag not supported with bigalloc");
243 err = mnt_want_write(filp->f_path.mnt);
247 err = ext4_move_extents(filp, donor_filp, me.orig_start,
248 me.donor_start, me.len, &me.moved_len);
249 mnt_drop_write(filp->f_path.mnt);
250 if (me.moved_len > 0)
251 file_remove_suid(donor_filp);
253 if (copy_to_user((struct move_extent __user *)arg,
261 case EXT4_IOC_GROUP_ADD: {
262 struct ext4_new_group_data input;
265 err = ext4_resize_begin(sb);
269 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
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");
280 err = mnt_want_write(filp->f_path.mnt);
284 err = ext4_group_add(sb, &input);
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);
292 mnt_drop_write(filp->f_path.mnt);
298 case EXT4_IOC_MIGRATE:
301 if (!inode_owner_or_capable(inode))
304 err = mnt_want_write(filp->f_path.mnt);
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.
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);
320 case EXT4_IOC_ALLOC_DA_BLKS:
323 if (!inode_owner_or_capable(inode))
326 err = mnt_want_write(filp->f_path.mnt);
329 err = ext4_alloc_da_blocks(inode);
330 mnt_drop_write(filp->f_path.mnt);
336 struct request_queue *q = bdev_get_queue(sb->s_bdev);
337 struct fstrim_range range;
340 if (!capable(CAP_SYS_ADMIN))
343 if (!blk_queue_discard(q))
346 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
347 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
348 ext4_msg(sb, KERN_ERR,
349 "FITRIM not supported with bigalloc");
353 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
357 range.minlen = max((unsigned int)range.minlen,
358 q->limits.discard_granularity);
359 ret = ext4_trim_fs(sb, &range);
363 if (copy_to_user((struct fstrim_range __user *)arg, &range,
376 long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
378 /* These are just misnamed, they actually get/put from/to user an int */
380 case EXT4_IOC32_GETFLAGS:
381 cmd = EXT4_IOC_GETFLAGS;
383 case EXT4_IOC32_SETFLAGS:
384 cmd = EXT4_IOC_SETFLAGS;
386 case EXT4_IOC32_GETVERSION:
387 cmd = EXT4_IOC_GETVERSION;
389 case EXT4_IOC32_SETVERSION:
390 cmd = EXT4_IOC_SETVERSION;
392 case EXT4_IOC32_GROUP_EXTEND:
393 cmd = EXT4_IOC_GROUP_EXTEND;
395 case EXT4_IOC32_GETVERSION_OLD:
396 cmd = EXT4_IOC_GETVERSION_OLD;
398 case EXT4_IOC32_SETVERSION_OLD:
399 cmd = EXT4_IOC_SETVERSION_OLD;
401 case EXT4_IOC32_GETRSVSZ:
402 cmd = EXT4_IOC_GETRSVSZ;
404 case EXT4_IOC32_SETRSVSZ:
405 cmd = EXT4_IOC_SETRSVSZ;
407 case EXT4_IOC32_GROUP_ADD: {
408 struct compat_ext4_new_group_input __user *uinput;
409 struct ext4_new_group_input input;
413 uinput = compat_ptr(arg);
414 err = get_user(input.group, &uinput->group);
415 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
416 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
417 err |= get_user(input.inode_table, &uinput->inode_table);
418 err |= get_user(input.blocks_count, &uinput->blocks_count);
419 err |= get_user(input.reserved_blocks,
420 &uinput->reserved_blocks);
425 err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
426 (unsigned long) &input);
430 case EXT4_IOC_MOVE_EXT:
436 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));