Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2 | * linux/fs/ext4/ioctl.c |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 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> |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 11 | #include <linux/jbd2.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 12 | #include <linux/capability.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 13 | #include <linux/time.h> |
| 14 | #include <linux/compat.h> |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 15 | #include <linux/mount.h> |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 16 | #include <linux/file.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 17 | #include <asm/uaccess.h> |
Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 18 | #include "ext4_jbd2.h" |
| 19 | #include "ext4.h" |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 20 | |
Andi Kleen | 5cdd7b2 | 2008-04-29 22:03:54 -0400 | [diff] [blame] | 21 | long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 22 | { |
Andi Kleen | 5cdd7b2 | 2008-04-29 22:03:54 -0400 | [diff] [blame] | 23 | struct inode *inode = filp->f_dentry->d_inode; |
Theodore Ts'o | bab08ab | 2011-09-09 18:36:51 -0400 | [diff] [blame] | 24 | struct super_block *sb = inode->i_sb; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 25 | struct ext4_inode_info *ei = EXT4_I(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 26 | unsigned int flags; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 27 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 28 | ext4_debug("cmd = %u, arg = %lu\n", cmd, arg); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 29 | |
| 30 | switch (cmd) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 31 | case EXT4_IOC_GETFLAGS: |
Jan Kara | ff9ddf7 | 2007-07-18 09:24:20 -0400 | [diff] [blame] | 32 | ext4_get_inode_flags(ei); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 33 | flags = ei->i_flags & EXT4_FL_USER_VISIBLE; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 34 | return put_user(flags, (int __user *) arg); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 35 | case EXT4_IOC_SETFLAGS: { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 36 | handle_t *handle = NULL; |
Aneesh Kumar K.V | 4db46fc | 2008-10-08 23:34:06 -0400 | [diff] [blame] | 37 | int err, migrate = 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 38 | struct ext4_iloc iloc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 39 | unsigned int oldflags; |
| 40 | unsigned int jflag; |
| 41 | |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 42 | if (!inode_owner_or_capable(inode)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 43 | return -EACCES; |
| 44 | |
| 45 | if (get_user(flags, (int __user *) arg)) |
| 46 | return -EFAULT; |
| 47 | |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 48 | err = mnt_want_write(filp->f_path.mnt); |
| 49 | if (err) |
| 50 | return err; |
| 51 | |
Duane Griffin | 2dc6b0d | 2009-02-15 18:09:20 -0500 | [diff] [blame] | 52 | flags = ext4_mask_flags(inode->i_mode, flags); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 53 | |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 54 | err = -EPERM; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 55 | mutex_lock(&inode->i_mutex); |
Jan Kara | e47776a | 2007-11-14 16:58:56 -0800 | [diff] [blame] | 56 | /* Is it quota file? Do not allow user to mess with it */ |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 57 | if (IS_NOQUOTA(inode)) |
| 58 | goto flags_out; |
| 59 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 60 | oldflags = ei->i_flags; |
| 61 | |
| 62 | /* The JOURNAL_DATA flag is modifiable only by root */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 63 | jflag = flags & EXT4_JOURNAL_DATA_FL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 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 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 71 | if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) { |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 72 | if (!capable(CAP_LINUX_IMMUTABLE)) |
| 73 | goto flags_out; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /* |
| 77 | * The JOURNAL_DATA flag can only be changed by |
| 78 | * the relevant capability. |
| 79 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 80 | if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) { |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 81 | if (!capable(CAP_SYS_RESOURCE)) |
| 82 | goto flags_out; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 83 | } |
Aneesh Kumar K.V | 4db46fc | 2008-10-08 23:34:06 -0400 | [diff] [blame] | 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 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 95 | |
Jiaying Zhang | c8d46e4 | 2010-02-24 09:52:53 -0500 | [diff] [blame] | 96 | if (flags & EXT4_EOFBLOCKS_FL) { |
| 97 | /* we don't support adding EOFBLOCKS flag */ |
| 98 | if (!(oldflags & EXT4_EOFBLOCKS_FL)) { |
| 99 | err = -EOPNOTSUPP; |
| 100 | goto flags_out; |
| 101 | } |
| 102 | } else if (oldflags & EXT4_EOFBLOCKS_FL) |
| 103 | ext4_truncate(inode); |
| 104 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 105 | handle = ext4_journal_start(inode, 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 106 | if (IS_ERR(handle)) { |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 107 | err = PTR_ERR(handle); |
| 108 | goto flags_out; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 109 | } |
| 110 | if (IS_SYNC(inode)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 111 | ext4_handle_sync(handle); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 112 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 113 | if (err) |
| 114 | goto flags_err; |
| 115 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 116 | flags = flags & EXT4_FL_USER_MODIFIABLE; |
| 117 | flags |= oldflags & ~EXT4_FL_USER_MODIFIABLE; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 118 | ei->i_flags = flags; |
| 119 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 120 | ext4_set_inode_flags(inode); |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 121 | inode->i_ctime = ext4_current_time(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 122 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 123 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 124 | flags_err: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 125 | ext4_journal_stop(handle); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 126 | if (err) |
| 127 | goto flags_out; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 128 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 129 | if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) |
| 130 | err = ext4_change_inode_journal_flag(inode, jflag); |
Aneesh Kumar K.V | 4db46fc | 2008-10-08 23:34:06 -0400 | [diff] [blame] | 131 | if (err) |
| 132 | goto flags_out; |
| 133 | if (migrate) |
| 134 | err = ext4_ext_migrate(inode); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 135 | flags_out: |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 136 | mutex_unlock(&inode->i_mutex); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 137 | mnt_drop_write(filp->f_path.mnt); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 138 | return err; |
| 139 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 140 | case EXT4_IOC_GETVERSION: |
| 141 | case EXT4_IOC_GETVERSION_OLD: |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 142 | return put_user(inode->i_generation, (int __user *) arg); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 143 | case EXT4_IOC_SETVERSION: |
| 144 | case EXT4_IOC_SETVERSION_OLD: { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 145 | handle_t *handle; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 146 | struct ext4_iloc iloc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 147 | __u32 generation; |
| 148 | int err; |
| 149 | |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 150 | if (!inode_owner_or_capable(inode)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 151 | return -EPERM; |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 152 | |
| 153 | err = mnt_want_write(filp->f_path.mnt); |
| 154 | if (err) |
| 155 | return err; |
| 156 | if (get_user(generation, (int __user *) arg)) { |
| 157 | err = -EFAULT; |
| 158 | goto setversion_out; |
| 159 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 160 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 161 | handle = ext4_journal_start(inode, 1); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 162 | if (IS_ERR(handle)) { |
| 163 | err = PTR_ERR(handle); |
| 164 | goto setversion_out; |
| 165 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 166 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 167 | if (err == 0) { |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 168 | inode->i_ctime = ext4_current_time(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 169 | inode->i_generation = generation; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 170 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 171 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 172 | ext4_journal_stop(handle); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 173 | setversion_out: |
| 174 | mnt_drop_write(filp->f_path.mnt); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 175 | return err; |
| 176 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 177 | case EXT4_IOC_GROUP_EXTEND: { |
| 178 | ext4_fsblk_t n_blocks_count; |
Peng Tao | ac046f1 | 2009-07-13 09:30:17 -0400 | [diff] [blame] | 179 | int err, err2=0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 180 | |
Yongqiang Yang | 8f82f84 | 2011-07-26 21:35:44 -0400 | [diff] [blame] | 181 | err = ext4_resize_begin(sb); |
| 182 | if (err) |
| 183 | return err; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 184 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 185 | if (get_user(n_blocks_count, (__u32 __user *)arg)) |
| 186 | return -EFAULT; |
| 187 | |
Theodore Ts'o | bab08ab | 2011-09-09 18:36:51 -0400 | [diff] [blame] | 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"); |
| 192 | return -EOPNOTSUPP; |
| 193 | } |
| 194 | |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 195 | err = mnt_want_write(filp->f_path.mnt); |
| 196 | if (err) |
| 197 | return err; |
| 198 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 199 | err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count); |
Peng Tao | ac046f1 | 2009-07-13 09:30:17 -0400 | [diff] [blame] | 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); |
| 204 | } |
Hidehiro Kawai | 7ffe1ea | 2008-10-10 20:29:21 -0400 | [diff] [blame] | 205 | if (err == 0) |
| 206 | err = err2; |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 207 | mnt_drop_write(filp->f_path.mnt); |
Yongqiang Yang | 8f82f84 | 2011-07-26 21:35:44 -0400 | [diff] [blame] | 208 | ext4_resize_end(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 209 | |
| 210 | return err; |
| 211 | } |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 212 | |
| 213 | case EXT4_IOC_MOVE_EXT: { |
| 214 | struct move_extent me; |
| 215 | struct file *donor_filp; |
| 216 | int err; |
| 217 | |
Akira Fujita | 4a58579 | 2009-12-06 23:38:31 -0500 | [diff] [blame] | 218 | if (!(filp->f_mode & FMODE_READ) || |
| 219 | !(filp->f_mode & FMODE_WRITE)) |
| 220 | return -EBADF; |
| 221 | |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 222 | if (copy_from_user(&me, |
| 223 | (struct move_extent __user *)arg, sizeof(me))) |
| 224 | return -EFAULT; |
Akira Fujita | 4a58579 | 2009-12-06 23:38:31 -0500 | [diff] [blame] | 225 | me.moved_len = 0; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 226 | |
| 227 | donor_filp = fget(me.donor_fd); |
| 228 | if (!donor_filp) |
| 229 | return -EBADF; |
| 230 | |
Akira Fujita | 4a58579 | 2009-12-06 23:38:31 -0500 | [diff] [blame] | 231 | if (!(donor_filp->f_mode & FMODE_WRITE)) { |
| 232 | err = -EBADF; |
| 233 | goto mext_out; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 234 | } |
| 235 | |
Theodore Ts'o | bab08ab | 2011-09-09 18:36:51 -0400 | [diff] [blame] | 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"); |
| 240 | return -EOPNOTSUPP; |
| 241 | } |
| 242 | |
Akira Fujita | 4a58579 | 2009-12-06 23:38:31 -0500 | [diff] [blame] | 243 | err = mnt_want_write(filp->f_path.mnt); |
| 244 | if (err) |
| 245 | goto mext_out; |
| 246 | |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 247 | err = ext4_move_extents(filp, donor_filp, me.orig_start, |
| 248 | me.donor_start, me.len, &me.moved_len); |
Akira Fujita | 4a58579 | 2009-12-06 23:38:31 -0500 | [diff] [blame] | 249 | mnt_drop_write(filp->f_path.mnt); |
| 250 | if (me.moved_len > 0) |
| 251 | file_remove_suid(donor_filp); |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 252 | |
Theodore Ts'o | 60e6679 | 2010-05-17 07:00:00 -0400 | [diff] [blame] | 253 | if (copy_to_user((struct move_extent __user *)arg, |
Akira Fujita | c437b27 | 2010-03-04 00:39:24 -0500 | [diff] [blame] | 254 | &me, sizeof(me))) |
Akira Fujita | 4a58579 | 2009-12-06 23:38:31 -0500 | [diff] [blame] | 255 | err = -EFAULT; |
| 256 | mext_out: |
| 257 | fput(donor_filp); |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 258 | return err; |
| 259 | } |
| 260 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 261 | case EXT4_IOC_GROUP_ADD: { |
| 262 | struct ext4_new_group_data input; |
Peng Tao | ac046f1 | 2009-07-13 09:30:17 -0400 | [diff] [blame] | 263 | int err, err2=0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 264 | |
Yongqiang Yang | 8f82f84 | 2011-07-26 21:35:44 -0400 | [diff] [blame] | 265 | err = ext4_resize_begin(sb); |
| 266 | if (err) |
| 267 | return err; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 268 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 269 | if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 270 | sizeof(input))) |
| 271 | return -EFAULT; |
| 272 | |
Theodore Ts'o | bab08ab | 2011-09-09 18:36:51 -0400 | [diff] [blame] | 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 | |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 280 | err = mnt_want_write(filp->f_path.mnt); |
| 281 | if (err) |
| 282 | return err; |
| 283 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 284 | err = ext4_group_add(sb, &input); |
Peng Tao | ac046f1 | 2009-07-13 09:30:17 -0400 | [diff] [blame] | 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 | } |
Hidehiro Kawai | 7ffe1ea | 2008-10-10 20:29:21 -0400 | [diff] [blame] | 290 | if (err == 0) |
| 291 | err = err2; |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 292 | mnt_drop_write(filp->f_path.mnt); |
Yongqiang Yang | 8f82f84 | 2011-07-26 21:35:44 -0400 | [diff] [blame] | 293 | ext4_resize_end(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 294 | |
| 295 | return err; |
| 296 | } |
| 297 | |
Aneesh Kumar K.V | c14c6fd | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 298 | case EXT4_IOC_MIGRATE: |
Aneesh Kumar K.V | 2a43a87 | 2008-09-13 12:52:26 -0400 | [diff] [blame] | 299 | { |
| 300 | int err; |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 301 | if (!inode_owner_or_capable(inode)) |
Aneesh Kumar K.V | 2a43a87 | 2008-09-13 12:52:26 -0400 | [diff] [blame] | 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 | } |
Aneesh Kumar K.V | c14c6fd | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 319 | |
Theodore Ts'o | ccd2506 | 2009-02-26 01:04:07 -0500 | [diff] [blame] | 320 | case EXT4_IOC_ALLOC_DA_BLKS: |
| 321 | { |
| 322 | int err; |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 323 | if (!inode_owner_or_capable(inode)) |
Theodore Ts'o | ccd2506 | 2009-02-26 01:04:07 -0500 | [diff] [blame] | 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 | |
Lukas Czerner | e681c04 | 2010-11-19 21:47:07 -0500 | [diff] [blame] | 334 | case FITRIM: |
| 335 | { |
Lukas Czerner | 4143179 | 2011-02-23 12:42:32 -0500 | [diff] [blame] | 336 | struct request_queue *q = bdev_get_queue(sb->s_bdev); |
Lukas Czerner | e681c04 | 2010-11-19 21:47:07 -0500 | [diff] [blame] | 337 | struct fstrim_range range; |
| 338 | int ret = 0; |
| 339 | |
| 340 | if (!capable(CAP_SYS_ADMIN)) |
| 341 | return -EPERM; |
| 342 | |
Lukas Czerner | 4143179 | 2011-02-23 12:42:32 -0500 | [diff] [blame] | 343 | if (!blk_queue_discard(q)) |
| 344 | return -EOPNOTSUPP; |
| 345 | |
Theodore Ts'o | bab08ab | 2011-09-09 18:36:51 -0400 | [diff] [blame] | 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"); |
| 350 | return -EOPNOTSUPP; |
| 351 | } |
| 352 | |
H Hartley Sweeten | e6705f7 | 2011-10-18 10:59:51 -0400 | [diff] [blame] | 353 | if (copy_from_user(&range, (struct fstrim_range __user *)arg, |
Lukas Czerner | e681c04 | 2010-11-19 21:47:07 -0500 | [diff] [blame] | 354 | sizeof(range))) |
| 355 | return -EFAULT; |
| 356 | |
Lukas Czerner | 5c2ed62 | 2011-02-23 17:49:51 -0500 | [diff] [blame] | 357 | range.minlen = max((unsigned int)range.minlen, |
| 358 | q->limits.discard_granularity); |
Lukas Czerner | e681c04 | 2010-11-19 21:47:07 -0500 | [diff] [blame] | 359 | ret = ext4_trim_fs(sb, &range); |
| 360 | if (ret < 0) |
| 361 | return ret; |
| 362 | |
H Hartley Sweeten | e6705f7 | 2011-10-18 10:59:51 -0400 | [diff] [blame] | 363 | if (copy_to_user((struct fstrim_range __user *)arg, &range, |
Lukas Czerner | e681c04 | 2010-11-19 21:47:07 -0500 | [diff] [blame] | 364 | sizeof(range))) |
| 365 | return -EFAULT; |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 370 | default: |
| 371 | return -ENOTTY; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | #ifdef CONFIG_COMPAT |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 376 | long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 377 | { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 378 | /* These are just misnamed, they actually get/put from/to user an int */ |
| 379 | switch (cmd) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 380 | case EXT4_IOC32_GETFLAGS: |
| 381 | cmd = EXT4_IOC_GETFLAGS; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 382 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 383 | case EXT4_IOC32_SETFLAGS: |
| 384 | cmd = EXT4_IOC_SETFLAGS; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 385 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 386 | case EXT4_IOC32_GETVERSION: |
| 387 | cmd = EXT4_IOC_GETVERSION; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 388 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 389 | case EXT4_IOC32_SETVERSION: |
| 390 | cmd = EXT4_IOC_SETVERSION; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 391 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 392 | case EXT4_IOC32_GROUP_EXTEND: |
| 393 | cmd = EXT4_IOC_GROUP_EXTEND; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 394 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 395 | case EXT4_IOC32_GETVERSION_OLD: |
| 396 | cmd = EXT4_IOC_GETVERSION_OLD; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 397 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 398 | case EXT4_IOC32_SETVERSION_OLD: |
| 399 | cmd = EXT4_IOC_SETVERSION_OLD; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 400 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 401 | case EXT4_IOC32_GETRSVSZ: |
| 402 | cmd = EXT4_IOC_GETRSVSZ; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 403 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 404 | case EXT4_IOC32_SETRSVSZ: |
| 405 | cmd = EXT4_IOC_SETRSVSZ; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 406 | break; |
Ben Hutchings | 4d92dc0 | 2010-05-17 06:00:00 -0400 | [diff] [blame] | 407 | case EXT4_IOC32_GROUP_ADD: { |
| 408 | struct compat_ext4_new_group_input __user *uinput; |
| 409 | struct ext4_new_group_input input; |
| 410 | mm_segment_t old_fs; |
| 411 | int err; |
| 412 | |
| 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); |
| 421 | if (err) |
| 422 | return -EFAULT; |
| 423 | old_fs = get_fs(); |
| 424 | set_fs(KERNEL_DS); |
| 425 | err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD, |
| 426 | (unsigned long) &input); |
| 427 | set_fs(old_fs); |
| 428 | return err; |
| 429 | } |
Christian Borntraeger | b684b2e | 2010-05-15 00:00:00 -0400 | [diff] [blame] | 430 | case EXT4_IOC_MOVE_EXT: |
Tao Ma | a56e69c | 2011-03-20 23:16:58 -0400 | [diff] [blame] | 431 | case FITRIM: |
Christian Borntraeger | b684b2e | 2010-05-15 00:00:00 -0400 | [diff] [blame] | 432 | break; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 433 | default: |
| 434 | return -ENOIOCTLCMD; |
| 435 | } |
Andi Kleen | 5cdd7b2 | 2008-04-29 22:03:54 -0400 | [diff] [blame] | 436 | return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 437 | } |
| 438 | #endif |