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 | |
Yongqiang Yang | 19c5246 | 2012-01-04 17:09:44 -0500 | [diff] [blame] | 21 | #define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1) |
| 22 | |
Andi Kleen | 5cdd7b2 | 2008-04-29 22:03:54 -0400 | [diff] [blame] | 23 | long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 24 | { |
Andi Kleen | 5cdd7b2 | 2008-04-29 22:03:54 -0400 | [diff] [blame] | 25 | struct inode *inode = filp->f_dentry->d_inode; |
Theodore Ts'o | bab08ab | 2011-09-09 18:36:51 -0400 | [diff] [blame] | 26 | struct super_block *sb = inode->i_sb; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 27 | struct ext4_inode_info *ei = EXT4_I(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 28 | unsigned int flags; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 29 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 30 | ext4_debug("cmd = %u, arg = %lu\n", cmd, arg); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 31 | |
| 32 | switch (cmd) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 33 | case EXT4_IOC_GETFLAGS: |
Jan Kara | ff9ddf7 | 2007-07-18 09:24:20 -0400 | [diff] [blame] | 34 | ext4_get_inode_flags(ei); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 35 | flags = ei->i_flags & EXT4_FL_USER_VISIBLE; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 36 | return put_user(flags, (int __user *) arg); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 37 | case EXT4_IOC_SETFLAGS: { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 38 | handle_t *handle = NULL; |
Aneesh Kumar K.V | 4db46fc | 2008-10-08 23:34:06 -0400 | [diff] [blame] | 39 | int err, migrate = 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 40 | struct ext4_iloc iloc; |
Theodore Ts'o | 7990696 | 2012-05-31 23:46:01 -0400 | [diff] [blame] | 41 | unsigned int oldflags, mask, i; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 42 | unsigned int jflag; |
| 43 | |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 44 | if (!inode_owner_or_capable(inode)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 45 | return -EACCES; |
| 46 | |
| 47 | if (get_user(flags, (int __user *) arg)) |
| 48 | return -EFAULT; |
| 49 | |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 50 | err = mnt_want_write_file(filp); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 51 | if (err) |
| 52 | return err; |
| 53 | |
Duane Griffin | 2dc6b0d | 2009-02-15 18:09:20 -0500 | [diff] [blame] | 54 | flags = ext4_mask_flags(inode->i_mode, flags); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 55 | |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 56 | err = -EPERM; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 57 | mutex_lock(&inode->i_mutex); |
Jan Kara | e47776a | 2007-11-14 16:58:56 -0800 | [diff] [blame] | 58 | /* Is it quota file? Do not allow user to mess with it */ |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 59 | if (IS_NOQUOTA(inode)) |
| 60 | goto flags_out; |
| 61 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 62 | oldflags = ei->i_flags; |
| 63 | |
| 64 | /* The JOURNAL_DATA flag is modifiable only by root */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 65 | jflag = flags & EXT4_JOURNAL_DATA_FL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 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 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 73 | if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) { |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 74 | if (!capable(CAP_LINUX_IMMUTABLE)) |
| 75 | goto flags_out; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /* |
| 79 | * The JOURNAL_DATA flag can only be changed by |
| 80 | * the relevant capability. |
| 81 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 82 | if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) { |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 83 | if (!capable(CAP_SYS_RESOURCE)) |
| 84 | goto flags_out; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 85 | } |
Aneesh Kumar K.V | 4db46fc | 2008-10-08 23:34:06 -0400 | [diff] [blame] | 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 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 97 | |
Jiaying Zhang | c8d46e4 | 2010-02-24 09:52:53 -0500 | [diff] [blame] | 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 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 107 | handle = ext4_journal_start(inode, 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 108 | if (IS_ERR(handle)) { |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 109 | err = PTR_ERR(handle); |
| 110 | goto flags_out; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 111 | } |
| 112 | if (IS_SYNC(inode)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 113 | ext4_handle_sync(handle); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 114 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 115 | if (err) |
| 116 | goto flags_err; |
| 117 | |
Theodore Ts'o | 7990696 | 2012-05-31 23:46:01 -0400 | [diff] [blame] | 118 | for (i = 0, mask = 1; i < 32; i++, mask <<= 1) { |
| 119 | if (!(mask & EXT4_FL_USER_MODIFIABLE)) |
| 120 | continue; |
| 121 | if (mask & flags) |
| 122 | ext4_set_inode_flag(inode, i); |
| 123 | else |
| 124 | ext4_clear_inode_flag(inode, i); |
| 125 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 126 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 127 | ext4_set_inode_flags(inode); |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 128 | inode->i_ctime = ext4_current_time(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 129 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 130 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 131 | flags_err: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 132 | ext4_journal_stop(handle); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 133 | if (err) |
| 134 | goto flags_out; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 135 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 136 | if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) |
| 137 | err = ext4_change_inode_journal_flag(inode, jflag); |
Aneesh Kumar K.V | 4db46fc | 2008-10-08 23:34:06 -0400 | [diff] [blame] | 138 | if (err) |
| 139 | goto flags_out; |
| 140 | if (migrate) |
| 141 | err = ext4_ext_migrate(inode); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 142 | flags_out: |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 143 | mutex_unlock(&inode->i_mutex); |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 144 | mnt_drop_write_file(filp); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 145 | return err; |
| 146 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 147 | case EXT4_IOC_GETVERSION: |
| 148 | case EXT4_IOC_GETVERSION_OLD: |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 149 | return put_user(inode->i_generation, (int __user *) arg); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 150 | case EXT4_IOC_SETVERSION: |
| 151 | case EXT4_IOC_SETVERSION_OLD: { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 152 | handle_t *handle; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 153 | struct ext4_iloc iloc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 154 | __u32 generation; |
| 155 | int err; |
| 156 | |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 157 | if (!inode_owner_or_capable(inode)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 158 | return -EPERM; |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 159 | |
Darrick J. Wong | 814525f | 2012-04-29 18:31:10 -0400 | [diff] [blame] | 160 | if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, |
| 161 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) { |
| 162 | ext4_warning(sb, "Setting inode version is not " |
| 163 | "supported with metadata_csum enabled."); |
| 164 | return -ENOTTY; |
| 165 | } |
| 166 | |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 167 | err = mnt_want_write_file(filp); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 168 | if (err) |
| 169 | return err; |
| 170 | if (get_user(generation, (int __user *) arg)) { |
| 171 | err = -EFAULT; |
| 172 | goto setversion_out; |
| 173 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 174 | |
Djalal Harouni | 6c2155b | 2012-01-03 02:31:52 +0100 | [diff] [blame] | 175 | mutex_lock(&inode->i_mutex); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 176 | handle = ext4_journal_start(inode, 1); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 177 | if (IS_ERR(handle)) { |
| 178 | err = PTR_ERR(handle); |
Djalal Harouni | 6c2155b | 2012-01-03 02:31:52 +0100 | [diff] [blame] | 179 | goto unlock_out; |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 180 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 181 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 182 | if (err == 0) { |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 183 | inode->i_ctime = ext4_current_time(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 184 | inode->i_generation = generation; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 185 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 186 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 187 | ext4_journal_stop(handle); |
Djalal Harouni | 6c2155b | 2012-01-03 02:31:52 +0100 | [diff] [blame] | 188 | |
| 189 | unlock_out: |
| 190 | mutex_unlock(&inode->i_mutex); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 191 | setversion_out: |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 192 | mnt_drop_write_file(filp); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 193 | return err; |
| 194 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 195 | case EXT4_IOC_GROUP_EXTEND: { |
| 196 | ext4_fsblk_t n_blocks_count; |
Peng Tao | ac046f1 | 2009-07-13 09:30:17 -0400 | [diff] [blame] | 197 | int err, err2=0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 198 | |
Yongqiang Yang | 8f82f84 | 2011-07-26 21:35:44 -0400 | [diff] [blame] | 199 | err = ext4_resize_begin(sb); |
| 200 | if (err) |
| 201 | return err; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 202 | |
Djalal Harouni | 014a177 | 2012-01-04 17:09:52 -0500 | [diff] [blame] | 203 | if (get_user(n_blocks_count, (__u32 __user *)arg)) { |
| 204 | err = -EFAULT; |
| 205 | goto group_extend_out; |
| 206 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 207 | |
Theodore Ts'o | bab08ab | 2011-09-09 18:36:51 -0400 | [diff] [blame] | 208 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, |
| 209 | EXT4_FEATURE_RO_COMPAT_BIGALLOC)) { |
| 210 | ext4_msg(sb, KERN_ERR, |
| 211 | "Online resizing not supported with bigalloc"); |
Djalal Harouni | 014a177 | 2012-01-04 17:09:52 -0500 | [diff] [blame] | 212 | err = -EOPNOTSUPP; |
| 213 | goto group_extend_out; |
Theodore Ts'o | bab08ab | 2011-09-09 18:36:51 -0400 | [diff] [blame] | 214 | } |
| 215 | |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 216 | err = mnt_want_write_file(filp); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 217 | if (err) |
Djalal Harouni | 014a177 | 2012-01-04 17:09:52 -0500 | [diff] [blame] | 218 | goto group_extend_out; |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 219 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 220 | 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] | 221 | if (EXT4_SB(sb)->s_journal) { |
| 222 | jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); |
| 223 | err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal); |
| 224 | jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); |
| 225 | } |
Hidehiro Kawai | 7ffe1ea | 2008-10-10 20:29:21 -0400 | [diff] [blame] | 226 | if (err == 0) |
| 227 | err = err2; |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 228 | mnt_drop_write_file(filp); |
Djalal Harouni | 014a177 | 2012-01-04 17:09:52 -0500 | [diff] [blame] | 229 | group_extend_out: |
Yongqiang Yang | 8f82f84 | 2011-07-26 21:35:44 -0400 | [diff] [blame] | 230 | ext4_resize_end(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 231 | return err; |
| 232 | } |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 233 | |
| 234 | case EXT4_IOC_MOVE_EXT: { |
| 235 | struct move_extent me; |
| 236 | struct file *donor_filp; |
| 237 | int err; |
| 238 | |
Akira Fujita | 4a58579 | 2009-12-06 23:38:31 -0500 | [diff] [blame] | 239 | if (!(filp->f_mode & FMODE_READ) || |
| 240 | !(filp->f_mode & FMODE_WRITE)) |
| 241 | return -EBADF; |
| 242 | |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 243 | if (copy_from_user(&me, |
| 244 | (struct move_extent __user *)arg, sizeof(me))) |
| 245 | return -EFAULT; |
Akira Fujita | 4a58579 | 2009-12-06 23:38:31 -0500 | [diff] [blame] | 246 | me.moved_len = 0; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 247 | |
| 248 | donor_filp = fget(me.donor_fd); |
| 249 | if (!donor_filp) |
| 250 | return -EBADF; |
| 251 | |
Akira Fujita | 4a58579 | 2009-12-06 23:38:31 -0500 | [diff] [blame] | 252 | if (!(donor_filp->f_mode & FMODE_WRITE)) { |
| 253 | err = -EBADF; |
| 254 | goto mext_out; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 255 | } |
| 256 | |
Theodore Ts'o | bab08ab | 2011-09-09 18:36:51 -0400 | [diff] [blame] | 257 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, |
| 258 | EXT4_FEATURE_RO_COMPAT_BIGALLOC)) { |
| 259 | ext4_msg(sb, KERN_ERR, |
| 260 | "Online defrag not supported with bigalloc"); |
| 261 | return -EOPNOTSUPP; |
| 262 | } |
| 263 | |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 264 | err = mnt_want_write_file(filp); |
Akira Fujita | 4a58579 | 2009-12-06 23:38:31 -0500 | [diff] [blame] | 265 | if (err) |
| 266 | goto mext_out; |
| 267 | |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 268 | err = ext4_move_extents(filp, donor_filp, me.orig_start, |
| 269 | me.donor_start, me.len, &me.moved_len); |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 270 | mnt_drop_write_file(filp); |
Akira Fujita | 4a58579 | 2009-12-06 23:38:31 -0500 | [diff] [blame] | 271 | mnt_drop_write(filp->f_path.mnt); |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 272 | |
Theodore Ts'o | 60e6679 | 2010-05-17 07:00:00 -0400 | [diff] [blame] | 273 | if (copy_to_user((struct move_extent __user *)arg, |
Akira Fujita | c437b27 | 2010-03-04 00:39:24 -0500 | [diff] [blame] | 274 | &me, sizeof(me))) |
Akira Fujita | 4a58579 | 2009-12-06 23:38:31 -0500 | [diff] [blame] | 275 | err = -EFAULT; |
| 276 | mext_out: |
| 277 | fput(donor_filp); |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 278 | return err; |
| 279 | } |
| 280 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 281 | case EXT4_IOC_GROUP_ADD: { |
| 282 | struct ext4_new_group_data input; |
Peng Tao | ac046f1 | 2009-07-13 09:30:17 -0400 | [diff] [blame] | 283 | int err, err2=0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 284 | |
Yongqiang Yang | 8f82f84 | 2011-07-26 21:35:44 -0400 | [diff] [blame] | 285 | err = ext4_resize_begin(sb); |
| 286 | if (err) |
| 287 | return err; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 288 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 289 | if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg, |
Djalal Harouni | 014a177 | 2012-01-04 17:09:52 -0500 | [diff] [blame] | 290 | sizeof(input))) { |
| 291 | err = -EFAULT; |
| 292 | goto group_add_out; |
| 293 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 294 | |
Theodore Ts'o | bab08ab | 2011-09-09 18:36:51 -0400 | [diff] [blame] | 295 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, |
| 296 | EXT4_FEATURE_RO_COMPAT_BIGALLOC)) { |
| 297 | ext4_msg(sb, KERN_ERR, |
| 298 | "Online resizing not supported with bigalloc"); |
Djalal Harouni | 014a177 | 2012-01-04 17:09:52 -0500 | [diff] [blame] | 299 | err = -EOPNOTSUPP; |
| 300 | goto group_add_out; |
Theodore Ts'o | bab08ab | 2011-09-09 18:36:51 -0400 | [diff] [blame] | 301 | } |
| 302 | |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 303 | err = mnt_want_write_file(filp); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 304 | if (err) |
Djalal Harouni | 014a177 | 2012-01-04 17:09:52 -0500 | [diff] [blame] | 305 | goto group_add_out; |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 306 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 307 | err = ext4_group_add(sb, &input); |
Peng Tao | ac046f1 | 2009-07-13 09:30:17 -0400 | [diff] [blame] | 308 | if (EXT4_SB(sb)->s_journal) { |
| 309 | jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); |
| 310 | err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal); |
| 311 | jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); |
| 312 | } |
Hidehiro Kawai | 7ffe1ea | 2008-10-10 20:29:21 -0400 | [diff] [blame] | 313 | if (err == 0) |
| 314 | err = err2; |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 315 | mnt_drop_write_file(filp); |
Djalal Harouni | 014a177 | 2012-01-04 17:09:52 -0500 | [diff] [blame] | 316 | group_add_out: |
Yongqiang Yang | 8f82f84 | 2011-07-26 21:35:44 -0400 | [diff] [blame] | 317 | ext4_resize_end(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 318 | return err; |
| 319 | } |
| 320 | |
Aneesh Kumar K.V | c14c6fd | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 321 | case EXT4_IOC_MIGRATE: |
Aneesh Kumar K.V | 2a43a87 | 2008-09-13 12:52:26 -0400 | [diff] [blame] | 322 | { |
| 323 | int err; |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 324 | if (!inode_owner_or_capable(inode)) |
Aneesh Kumar K.V | 2a43a87 | 2008-09-13 12:52:26 -0400 | [diff] [blame] | 325 | return -EACCES; |
| 326 | |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 327 | err = mnt_want_write_file(filp); |
Aneesh Kumar K.V | 2a43a87 | 2008-09-13 12:52:26 -0400 | [diff] [blame] | 328 | if (err) |
| 329 | return err; |
| 330 | /* |
| 331 | * inode_mutex prevent write and truncate on the file. |
| 332 | * Read still goes through. We take i_data_sem in |
| 333 | * ext4_ext_swap_inode_data before we switch the |
| 334 | * inode format to prevent read. |
| 335 | */ |
| 336 | mutex_lock(&(inode->i_mutex)); |
| 337 | err = ext4_ext_migrate(inode); |
| 338 | mutex_unlock(&(inode->i_mutex)); |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 339 | mnt_drop_write_file(filp); |
Aneesh Kumar K.V | 2a43a87 | 2008-09-13 12:52:26 -0400 | [diff] [blame] | 340 | return err; |
| 341 | } |
Aneesh Kumar K.V | c14c6fd | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 342 | |
Theodore Ts'o | ccd2506 | 2009-02-26 01:04:07 -0500 | [diff] [blame] | 343 | case EXT4_IOC_ALLOC_DA_BLKS: |
| 344 | { |
| 345 | int err; |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 346 | if (!inode_owner_or_capable(inode)) |
Theodore Ts'o | ccd2506 | 2009-02-26 01:04:07 -0500 | [diff] [blame] | 347 | return -EACCES; |
| 348 | |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 349 | err = mnt_want_write_file(filp); |
Theodore Ts'o | ccd2506 | 2009-02-26 01:04:07 -0500 | [diff] [blame] | 350 | if (err) |
| 351 | return err; |
| 352 | err = ext4_alloc_da_blocks(inode); |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 353 | mnt_drop_write_file(filp); |
Theodore Ts'o | ccd2506 | 2009-02-26 01:04:07 -0500 | [diff] [blame] | 354 | return err; |
| 355 | } |
| 356 | |
Yongqiang Yang | 19c5246 | 2012-01-04 17:09:44 -0500 | [diff] [blame] | 357 | case EXT4_IOC_RESIZE_FS: { |
| 358 | ext4_fsblk_t n_blocks_count; |
| 359 | struct super_block *sb = inode->i_sb; |
| 360 | int err = 0, err2 = 0; |
| 361 | |
| 362 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, |
| 363 | EXT4_FEATURE_RO_COMPAT_BIGALLOC)) { |
| 364 | ext4_msg(sb, KERN_ERR, |
| 365 | "Online resizing not (yet) supported with bigalloc"); |
| 366 | return -EOPNOTSUPP; |
| 367 | } |
| 368 | |
| 369 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, |
| 370 | EXT4_FEATURE_INCOMPAT_META_BG)) { |
| 371 | ext4_msg(sb, KERN_ERR, |
| 372 | "Online resizing not (yet) supported with meta_bg"); |
| 373 | return -EOPNOTSUPP; |
| 374 | } |
| 375 | |
| 376 | if (copy_from_user(&n_blocks_count, (__u64 __user *)arg, |
| 377 | sizeof(__u64))) { |
| 378 | return -EFAULT; |
| 379 | } |
| 380 | |
| 381 | if (n_blocks_count > MAX_32_NUM && |
| 382 | !EXT4_HAS_INCOMPAT_FEATURE(sb, |
| 383 | EXT4_FEATURE_INCOMPAT_64BIT)) { |
| 384 | ext4_msg(sb, KERN_ERR, |
| 385 | "File system only supports 32-bit block numbers"); |
| 386 | return -EOPNOTSUPP; |
| 387 | } |
| 388 | |
| 389 | err = ext4_resize_begin(sb); |
| 390 | if (err) |
| 391 | return err; |
| 392 | |
Al Viro | 8cae6f7 | 2012-07-19 11:19:07 +0400 | [diff] [blame^] | 393 | err = mnt_want_write_file(filp); |
Yongqiang Yang | 19c5246 | 2012-01-04 17:09:44 -0500 | [diff] [blame] | 394 | if (err) |
| 395 | goto resizefs_out; |
| 396 | |
| 397 | err = ext4_resize_fs(sb, n_blocks_count); |
| 398 | if (EXT4_SB(sb)->s_journal) { |
| 399 | jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); |
| 400 | err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal); |
| 401 | jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); |
| 402 | } |
| 403 | if (err == 0) |
| 404 | err = err2; |
Al Viro | 8cae6f7 | 2012-07-19 11:19:07 +0400 | [diff] [blame^] | 405 | mnt_drop_write_file(filp); |
Yongqiang Yang | 19c5246 | 2012-01-04 17:09:44 -0500 | [diff] [blame] | 406 | resizefs_out: |
| 407 | ext4_resize_end(sb); |
| 408 | return err; |
| 409 | } |
| 410 | |
Lukas Czerner | e681c04 | 2010-11-19 21:47:07 -0500 | [diff] [blame] | 411 | case FITRIM: |
| 412 | { |
Lukas Czerner | 4143179 | 2011-02-23 12:42:32 -0500 | [diff] [blame] | 413 | struct request_queue *q = bdev_get_queue(sb->s_bdev); |
Lukas Czerner | e681c04 | 2010-11-19 21:47:07 -0500 | [diff] [blame] | 414 | struct fstrim_range range; |
| 415 | int ret = 0; |
| 416 | |
| 417 | if (!capable(CAP_SYS_ADMIN)) |
| 418 | return -EPERM; |
| 419 | |
Lukas Czerner | 4143179 | 2011-02-23 12:42:32 -0500 | [diff] [blame] | 420 | if (!blk_queue_discard(q)) |
| 421 | return -EOPNOTSUPP; |
| 422 | |
Theodore Ts'o | bab08ab | 2011-09-09 18:36:51 -0400 | [diff] [blame] | 423 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, |
| 424 | EXT4_FEATURE_RO_COMPAT_BIGALLOC)) { |
| 425 | ext4_msg(sb, KERN_ERR, |
| 426 | "FITRIM not supported with bigalloc"); |
| 427 | return -EOPNOTSUPP; |
| 428 | } |
| 429 | |
H Hartley Sweeten | e6705f7 | 2011-10-18 10:59:51 -0400 | [diff] [blame] | 430 | if (copy_from_user(&range, (struct fstrim_range __user *)arg, |
Lukas Czerner | e681c04 | 2010-11-19 21:47:07 -0500 | [diff] [blame] | 431 | sizeof(range))) |
| 432 | return -EFAULT; |
| 433 | |
Lukas Czerner | 5c2ed62 | 2011-02-23 17:49:51 -0500 | [diff] [blame] | 434 | range.minlen = max((unsigned int)range.minlen, |
| 435 | q->limits.discard_granularity); |
Lukas Czerner | e681c04 | 2010-11-19 21:47:07 -0500 | [diff] [blame] | 436 | ret = ext4_trim_fs(sb, &range); |
| 437 | if (ret < 0) |
| 438 | return ret; |
| 439 | |
H Hartley Sweeten | e6705f7 | 2011-10-18 10:59:51 -0400 | [diff] [blame] | 440 | if (copy_to_user((struct fstrim_range __user *)arg, &range, |
Lukas Czerner | e681c04 | 2010-11-19 21:47:07 -0500 | [diff] [blame] | 441 | sizeof(range))) |
| 442 | return -EFAULT; |
| 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 447 | default: |
| 448 | return -ENOTTY; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | #ifdef CONFIG_COMPAT |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 453 | 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] | 454 | { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 455 | /* These are just misnamed, they actually get/put from/to user an int */ |
| 456 | switch (cmd) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 457 | case EXT4_IOC32_GETFLAGS: |
| 458 | cmd = EXT4_IOC_GETFLAGS; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 459 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 460 | case EXT4_IOC32_SETFLAGS: |
| 461 | cmd = EXT4_IOC_SETFLAGS; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 462 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 463 | case EXT4_IOC32_GETVERSION: |
| 464 | cmd = EXT4_IOC_GETVERSION; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 465 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 466 | case EXT4_IOC32_SETVERSION: |
| 467 | cmd = EXT4_IOC_SETVERSION; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 468 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 469 | case EXT4_IOC32_GROUP_EXTEND: |
| 470 | cmd = EXT4_IOC_GROUP_EXTEND; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 471 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 472 | case EXT4_IOC32_GETVERSION_OLD: |
| 473 | cmd = EXT4_IOC_GETVERSION_OLD; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 474 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 475 | case EXT4_IOC32_SETVERSION_OLD: |
| 476 | cmd = EXT4_IOC_SETVERSION_OLD; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 477 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 478 | case EXT4_IOC32_GETRSVSZ: |
| 479 | cmd = EXT4_IOC_GETRSVSZ; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 480 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 481 | case EXT4_IOC32_SETRSVSZ: |
| 482 | cmd = EXT4_IOC_SETRSVSZ; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 483 | break; |
Ben Hutchings | 4d92dc0 | 2010-05-17 06:00:00 -0400 | [diff] [blame] | 484 | case EXT4_IOC32_GROUP_ADD: { |
| 485 | struct compat_ext4_new_group_input __user *uinput; |
| 486 | struct ext4_new_group_input input; |
| 487 | mm_segment_t old_fs; |
| 488 | int err; |
| 489 | |
| 490 | uinput = compat_ptr(arg); |
| 491 | err = get_user(input.group, &uinput->group); |
| 492 | err |= get_user(input.block_bitmap, &uinput->block_bitmap); |
| 493 | err |= get_user(input.inode_bitmap, &uinput->inode_bitmap); |
| 494 | err |= get_user(input.inode_table, &uinput->inode_table); |
| 495 | err |= get_user(input.blocks_count, &uinput->blocks_count); |
| 496 | err |= get_user(input.reserved_blocks, |
| 497 | &uinput->reserved_blocks); |
| 498 | if (err) |
| 499 | return -EFAULT; |
| 500 | old_fs = get_fs(); |
| 501 | set_fs(KERNEL_DS); |
| 502 | err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD, |
| 503 | (unsigned long) &input); |
| 504 | set_fs(old_fs); |
| 505 | return err; |
| 506 | } |
Christian Borntraeger | b684b2e | 2010-05-15 00:00:00 -0400 | [diff] [blame] | 507 | case EXT4_IOC_MOVE_EXT: |
Tao Ma | a56e69c | 2011-03-20 23:16:58 -0400 | [diff] [blame] | 508 | case FITRIM: |
Yongqiang Yang | 19c5246 | 2012-01-04 17:09:44 -0500 | [diff] [blame] | 509 | case EXT4_IOC_RESIZE_FS: |
Christian Borntraeger | b684b2e | 2010-05-15 00:00:00 -0400 | [diff] [blame] | 510 | break; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 511 | default: |
| 512 | return -ENOIOCTLCMD; |
| 513 | } |
Andi Kleen | 5cdd7b2 | 2008-04-29 22:03:54 -0400 | [diff] [blame] | 514 | return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 515 | } |
| 516 | #endif |