blob: 8ad112ae0ade2f21a953ccc03b687939b0b81310 [file] [log] [blame]
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001/*
Mingming Cao617ba132006-10-11 01:20:53 -07002 * linux/fs/ext4/ioctl.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003 *
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 Caodab291a2006-10-11 01:21:01 -070011#include <linux/jbd2.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070012#include <linux/capability.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070013#include <linux/time.h>
14#include <linux/compat.h>
Dave Hansen42a74f22008-02-15 14:37:46 -080015#include <linux/mount.h>
Akira Fujita748de672009-06-17 19:24:03 -040016#include <linux/file.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070017#include <asm/uaccess.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040018#include "ext4_jbd2.h"
19#include "ext4.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070020
Yongqiang Yang19c52462012-01-04 17:09:44 -050021#define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1)
22
Andi Kleen5cdd7b22008-04-29 22:03:54 -040023long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070024{
Andi Kleen5cdd7b22008-04-29 22:03:54 -040025 struct inode *inode = filp->f_dentry->d_inode;
Theodore Ts'obab08ab2011-09-09 18:36:51 -040026 struct super_block *sb = inode->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -070027 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070028 unsigned int flags;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070029
Theodore Ts'oaf5bc922008-09-08 22:25:24 -040030 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070031
32 switch (cmd) {
Mingming Cao617ba132006-10-11 01:20:53 -070033 case EXT4_IOC_GETFLAGS:
Jan Karaff9ddf72007-07-18 09:24:20 -040034 ext4_get_inode_flags(ei);
Mingming Cao617ba132006-10-11 01:20:53 -070035 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070036 return put_user(flags, (int __user *) arg);
Mingming Cao617ba132006-10-11 01:20:53 -070037 case EXT4_IOC_SETFLAGS: {
Dave Kleikampac27a0e2006-10-11 01:20:50 -070038 handle_t *handle = NULL;
Aneesh Kumar K.V4db46fc2008-10-08 23:34:06 -040039 int err, migrate = 0;
Mingming Cao617ba132006-10-11 01:20:53 -070040 struct ext4_iloc iloc;
Theodore Ts'o79906962012-05-31 23:46:01 -040041 unsigned int oldflags, mask, i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070042 unsigned int jflag;
43
Serge E. Hallyn2e149672011-03-23 16:43:26 -070044 if (!inode_owner_or_capable(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -070045 return -EACCES;
46
47 if (get_user(flags, (int __user *) arg))
48 return -EFAULT;
49
Al Viroa561be72011-11-23 11:57:51 -050050 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -080051 if (err)
52 return err;
53
Duane Griffin2dc6b0d2009-02-15 18:09:20 -050054 flags = ext4_mask_flags(inode->i_mode, flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070055
Dave Hansen42a74f22008-02-15 14:37:46 -080056 err = -EPERM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070057 mutex_lock(&inode->i_mutex);
Jan Karae47776a2007-11-14 16:58:56 -080058 /* Is it quota file? Do not allow user to mess with it */
Dave Hansen42a74f22008-02-15 14:37:46 -080059 if (IS_NOQUOTA(inode))
60 goto flags_out;
61
Dave Kleikampac27a0e2006-10-11 01:20:50 -070062 oldflags = ei->i_flags;
63
64 /* The JOURNAL_DATA flag is modifiable only by root */
Mingming Cao617ba132006-10-11 01:20:53 -070065 jflag = flags & EXT4_JOURNAL_DATA_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070066
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 Cao617ba132006-10-11 01:20:53 -070073 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
Dave Hansen42a74f22008-02-15 14:37:46 -080074 if (!capable(CAP_LINUX_IMMUTABLE))
75 goto flags_out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070076 }
77
78 /*
79 * The JOURNAL_DATA flag can only be changed by
80 * the relevant capability.
81 */
Mingming Cao617ba132006-10-11 01:20:53 -070082 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
Dave Hansen42a74f22008-02-15 14:37:46 -080083 if (!capable(CAP_SYS_RESOURCE))
84 goto flags_out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070085 }
Aneesh Kumar K.V4db46fc2008-10-08 23:34:06 -040086 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 Kleikampac27a0e2006-10-11 01:20:50 -070097
Jiaying Zhangc8d46e42010-02-24 09:52:53 -050098 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 Cao617ba132006-10-11 01:20:53 -0700107 handle = ext4_journal_start(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700108 if (IS_ERR(handle)) {
Dave Hansen42a74f22008-02-15 14:37:46 -0800109 err = PTR_ERR(handle);
110 goto flags_out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700111 }
112 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500113 ext4_handle_sync(handle);
Mingming Cao617ba132006-10-11 01:20:53 -0700114 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700115 if (err)
116 goto flags_err;
117
Theodore Ts'o79906962012-05-31 23:46:01 -0400118 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 Kleikampac27a0e2006-10-11 01:20:50 -0700126 ei->i_flags = flags;
127
Mingming Cao617ba132006-10-11 01:20:53 -0700128 ext4_set_inode_flags(inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -0400129 inode->i_ctime = ext4_current_time(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700130
Mingming Cao617ba132006-10-11 01:20:53 -0700131 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700132flags_err:
Mingming Cao617ba132006-10-11 01:20:53 -0700133 ext4_journal_stop(handle);
Dave Hansen42a74f22008-02-15 14:37:46 -0800134 if (err)
135 goto flags_out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700136
Mingming Cao617ba132006-10-11 01:20:53 -0700137 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
138 err = ext4_change_inode_journal_flag(inode, jflag);
Aneesh Kumar K.V4db46fc2008-10-08 23:34:06 -0400139 if (err)
140 goto flags_out;
141 if (migrate)
142 err = ext4_ext_migrate(inode);
Dave Hansen42a74f22008-02-15 14:37:46 -0800143flags_out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700144 mutex_unlock(&inode->i_mutex);
Al Viro2a79f172011-12-09 08:06:57 -0500145 mnt_drop_write_file(filp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700146 return err;
147 }
Mingming Cao617ba132006-10-11 01:20:53 -0700148 case EXT4_IOC_GETVERSION:
149 case EXT4_IOC_GETVERSION_OLD:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700150 return put_user(inode->i_generation, (int __user *) arg);
Mingming Cao617ba132006-10-11 01:20:53 -0700151 case EXT4_IOC_SETVERSION:
152 case EXT4_IOC_SETVERSION_OLD: {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700153 handle_t *handle;
Mingming Cao617ba132006-10-11 01:20:53 -0700154 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700155 __u32 generation;
156 int err;
157
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700158 if (!inode_owner_or_capable(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700159 return -EPERM;
Dave Hansen42a74f22008-02-15 14:37:46 -0800160
Darrick J. Wong814525f2012-04-29 18:31:10 -0400161 if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
162 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
163 ext4_warning(sb, "Setting inode version is not "
164 "supported with metadata_csum enabled.");
165 return -ENOTTY;
166 }
167
Al Viroa561be72011-11-23 11:57:51 -0500168 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800169 if (err)
170 return err;
171 if (get_user(generation, (int __user *) arg)) {
172 err = -EFAULT;
173 goto setversion_out;
174 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700175
Djalal Harouni6c2155b2012-01-03 02:31:52 +0100176 mutex_lock(&inode->i_mutex);
Mingming Cao617ba132006-10-11 01:20:53 -0700177 handle = ext4_journal_start(inode, 1);
Dave Hansen42a74f22008-02-15 14:37:46 -0800178 if (IS_ERR(handle)) {
179 err = PTR_ERR(handle);
Djalal Harouni6c2155b2012-01-03 02:31:52 +0100180 goto unlock_out;
Dave Hansen42a74f22008-02-15 14:37:46 -0800181 }
Mingming Cao617ba132006-10-11 01:20:53 -0700182 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700183 if (err == 0) {
Kalpak Shahef7f3832007-07-18 09:15:20 -0400184 inode->i_ctime = ext4_current_time(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700185 inode->i_generation = generation;
Mingming Cao617ba132006-10-11 01:20:53 -0700186 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700187 }
Mingming Cao617ba132006-10-11 01:20:53 -0700188 ext4_journal_stop(handle);
Djalal Harouni6c2155b2012-01-03 02:31:52 +0100189
190unlock_out:
191 mutex_unlock(&inode->i_mutex);
Dave Hansen42a74f22008-02-15 14:37:46 -0800192setversion_out:
Al Viro2a79f172011-12-09 08:06:57 -0500193 mnt_drop_write_file(filp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700194 return err;
195 }
Mingming Cao617ba132006-10-11 01:20:53 -0700196 case EXT4_IOC_GROUP_EXTEND: {
197 ext4_fsblk_t n_blocks_count;
Peng Taoac046f12009-07-13 09:30:17 -0400198 int err, err2=0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700199
Yongqiang Yang8f82f842011-07-26 21:35:44 -0400200 err = ext4_resize_begin(sb);
201 if (err)
202 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700203
Djalal Harouni014a1772012-01-04 17:09:52 -0500204 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
205 err = -EFAULT;
206 goto group_extend_out;
207 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700208
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400209 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
210 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
211 ext4_msg(sb, KERN_ERR,
212 "Online resizing not supported with bigalloc");
Djalal Harouni014a1772012-01-04 17:09:52 -0500213 err = -EOPNOTSUPP;
214 goto group_extend_out;
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400215 }
216
Al Viroa561be72011-11-23 11:57:51 -0500217 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800218 if (err)
Djalal Harouni014a1772012-01-04 17:09:52 -0500219 goto group_extend_out;
Dave Hansen42a74f22008-02-15 14:37:46 -0800220
Mingming Cao617ba132006-10-11 01:20:53 -0700221 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
Peng Taoac046f12009-07-13 09:30:17 -0400222 if (EXT4_SB(sb)->s_journal) {
223 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
224 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
225 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
226 }
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -0400227 if (err == 0)
228 err = err2;
Al Viro2a79f172011-12-09 08:06:57 -0500229 mnt_drop_write_file(filp);
Djalal Harouni014a1772012-01-04 17:09:52 -0500230group_extend_out:
Yongqiang Yang8f82f842011-07-26 21:35:44 -0400231 ext4_resize_end(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700232 return err;
233 }
Akira Fujita748de672009-06-17 19:24:03 -0400234
235 case EXT4_IOC_MOVE_EXT: {
236 struct move_extent me;
237 struct file *donor_filp;
238 int err;
239
Akira Fujita4a585792009-12-06 23:38:31 -0500240 if (!(filp->f_mode & FMODE_READ) ||
241 !(filp->f_mode & FMODE_WRITE))
242 return -EBADF;
243
Akira Fujita748de672009-06-17 19:24:03 -0400244 if (copy_from_user(&me,
245 (struct move_extent __user *)arg, sizeof(me)))
246 return -EFAULT;
Akira Fujita4a585792009-12-06 23:38:31 -0500247 me.moved_len = 0;
Akira Fujita748de672009-06-17 19:24:03 -0400248
249 donor_filp = fget(me.donor_fd);
250 if (!donor_filp)
251 return -EBADF;
252
Akira Fujita4a585792009-12-06 23:38:31 -0500253 if (!(donor_filp->f_mode & FMODE_WRITE)) {
254 err = -EBADF;
255 goto mext_out;
Akira Fujita748de672009-06-17 19:24:03 -0400256 }
257
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400258 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
259 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
260 ext4_msg(sb, KERN_ERR,
261 "Online defrag not supported with bigalloc");
262 return -EOPNOTSUPP;
263 }
264
Al Viroa561be72011-11-23 11:57:51 -0500265 err = mnt_want_write_file(filp);
Akira Fujita4a585792009-12-06 23:38:31 -0500266 if (err)
267 goto mext_out;
268
Akira Fujita748de672009-06-17 19:24:03 -0400269 err = ext4_move_extents(filp, donor_filp, me.orig_start,
270 me.donor_start, me.len, &me.moved_len);
Al Viro2a79f172011-12-09 08:06:57 -0500271 mnt_drop_write_file(filp);
Akira Fujita4a585792009-12-06 23:38:31 -0500272 mnt_drop_write(filp->f_path.mnt);
Akira Fujita748de672009-06-17 19:24:03 -0400273
Theodore Ts'o60e66792010-05-17 07:00:00 -0400274 if (copy_to_user((struct move_extent __user *)arg,
Akira Fujitac437b272010-03-04 00:39:24 -0500275 &me, sizeof(me)))
Akira Fujita4a585792009-12-06 23:38:31 -0500276 err = -EFAULT;
277mext_out:
278 fput(donor_filp);
Akira Fujita748de672009-06-17 19:24:03 -0400279 return err;
280 }
281
Mingming Cao617ba132006-10-11 01:20:53 -0700282 case EXT4_IOC_GROUP_ADD: {
283 struct ext4_new_group_data input;
Peng Taoac046f12009-07-13 09:30:17 -0400284 int err, err2=0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700285
Yongqiang Yang8f82f842011-07-26 21:35:44 -0400286 err = ext4_resize_begin(sb);
287 if (err)
288 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700289
Mingming Cao617ba132006-10-11 01:20:53 -0700290 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
Djalal Harouni014a1772012-01-04 17:09:52 -0500291 sizeof(input))) {
292 err = -EFAULT;
293 goto group_add_out;
294 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700295
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400296 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
297 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
298 ext4_msg(sb, KERN_ERR,
299 "Online resizing not supported with bigalloc");
Djalal Harouni014a1772012-01-04 17:09:52 -0500300 err = -EOPNOTSUPP;
301 goto group_add_out;
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400302 }
303
Al Viroa561be72011-11-23 11:57:51 -0500304 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800305 if (err)
Djalal Harouni014a1772012-01-04 17:09:52 -0500306 goto group_add_out;
Dave Hansen42a74f22008-02-15 14:37:46 -0800307
Mingming Cao617ba132006-10-11 01:20:53 -0700308 err = ext4_group_add(sb, &input);
Peng Taoac046f12009-07-13 09:30:17 -0400309 if (EXT4_SB(sb)->s_journal) {
310 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
311 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
312 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
313 }
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -0400314 if (err == 0)
315 err = err2;
Al Viro2a79f172011-12-09 08:06:57 -0500316 mnt_drop_write_file(filp);
Djalal Harouni014a1772012-01-04 17:09:52 -0500317group_add_out:
Yongqiang Yang8f82f842011-07-26 21:35:44 -0400318 ext4_resize_end(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700319 return err;
320 }
321
Aneesh Kumar K.Vc14c6fd2008-01-28 23:58:26 -0500322 case EXT4_IOC_MIGRATE:
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400323 {
324 int err;
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700325 if (!inode_owner_or_capable(inode))
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400326 return -EACCES;
327
Al Viroa561be72011-11-23 11:57:51 -0500328 err = mnt_want_write_file(filp);
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400329 if (err)
330 return err;
331 /*
332 * inode_mutex prevent write and truncate on the file.
333 * Read still goes through. We take i_data_sem in
334 * ext4_ext_swap_inode_data before we switch the
335 * inode format to prevent read.
336 */
337 mutex_lock(&(inode->i_mutex));
338 err = ext4_ext_migrate(inode);
339 mutex_unlock(&(inode->i_mutex));
Al Viro2a79f172011-12-09 08:06:57 -0500340 mnt_drop_write_file(filp);
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400341 return err;
342 }
Aneesh Kumar K.Vc14c6fd2008-01-28 23:58:26 -0500343
Theodore Ts'occd25062009-02-26 01:04:07 -0500344 case EXT4_IOC_ALLOC_DA_BLKS:
345 {
346 int err;
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700347 if (!inode_owner_or_capable(inode))
Theodore Ts'occd25062009-02-26 01:04:07 -0500348 return -EACCES;
349
Al Viroa561be72011-11-23 11:57:51 -0500350 err = mnt_want_write_file(filp);
Theodore Ts'occd25062009-02-26 01:04:07 -0500351 if (err)
352 return err;
353 err = ext4_alloc_da_blocks(inode);
Al Viro2a79f172011-12-09 08:06:57 -0500354 mnt_drop_write_file(filp);
Theodore Ts'occd25062009-02-26 01:04:07 -0500355 return err;
356 }
357
Yongqiang Yang19c52462012-01-04 17:09:44 -0500358 case EXT4_IOC_RESIZE_FS: {
359 ext4_fsblk_t n_blocks_count;
360 struct super_block *sb = inode->i_sb;
361 int err = 0, err2 = 0;
362
363 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
364 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
365 ext4_msg(sb, KERN_ERR,
366 "Online resizing not (yet) supported with bigalloc");
367 return -EOPNOTSUPP;
368 }
369
370 if (EXT4_HAS_INCOMPAT_FEATURE(sb,
371 EXT4_FEATURE_INCOMPAT_META_BG)) {
372 ext4_msg(sb, KERN_ERR,
373 "Online resizing not (yet) supported with meta_bg");
374 return -EOPNOTSUPP;
375 }
376
377 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
378 sizeof(__u64))) {
379 return -EFAULT;
380 }
381
382 if (n_blocks_count > MAX_32_NUM &&
383 !EXT4_HAS_INCOMPAT_FEATURE(sb,
384 EXT4_FEATURE_INCOMPAT_64BIT)) {
385 ext4_msg(sb, KERN_ERR,
386 "File system only supports 32-bit block numbers");
387 return -EOPNOTSUPP;
388 }
389
390 err = ext4_resize_begin(sb);
391 if (err)
392 return err;
393
394 err = mnt_want_write(filp->f_path.mnt);
395 if (err)
396 goto resizefs_out;
397
398 err = ext4_resize_fs(sb, n_blocks_count);
399 if (EXT4_SB(sb)->s_journal) {
400 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
401 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
402 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
403 }
404 if (err == 0)
405 err = err2;
406 mnt_drop_write(filp->f_path.mnt);
407resizefs_out:
408 ext4_resize_end(sb);
409 return err;
410 }
411
Lukas Czernere681c042010-11-19 21:47:07 -0500412 case FITRIM:
413 {
Lukas Czerner41431792011-02-23 12:42:32 -0500414 struct request_queue *q = bdev_get_queue(sb->s_bdev);
Lukas Czernere681c042010-11-19 21:47:07 -0500415 struct fstrim_range range;
416 int ret = 0;
417
418 if (!capable(CAP_SYS_ADMIN))
419 return -EPERM;
420
Lukas Czerner41431792011-02-23 12:42:32 -0500421 if (!blk_queue_discard(q))
422 return -EOPNOTSUPP;
423
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400424 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
425 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
426 ext4_msg(sb, KERN_ERR,
427 "FITRIM not supported with bigalloc");
428 return -EOPNOTSUPP;
429 }
430
H Hartley Sweetene6705f72011-10-18 10:59:51 -0400431 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
Lukas Czernere681c042010-11-19 21:47:07 -0500432 sizeof(range)))
433 return -EFAULT;
434
Lukas Czerner5c2ed622011-02-23 17:49:51 -0500435 range.minlen = max((unsigned int)range.minlen,
436 q->limits.discard_granularity);
Lukas Czernere681c042010-11-19 21:47:07 -0500437 ret = ext4_trim_fs(sb, &range);
438 if (ret < 0)
439 return ret;
440
H Hartley Sweetene6705f72011-10-18 10:59:51 -0400441 if (copy_to_user((struct fstrim_range __user *)arg, &range,
Lukas Czernere681c042010-11-19 21:47:07 -0500442 sizeof(range)))
443 return -EFAULT;
444
445 return 0;
446 }
447
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700448 default:
449 return -ENOTTY;
450 }
451}
452
453#ifdef CONFIG_COMPAT
Mingming Cao617ba132006-10-11 01:20:53 -0700454long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700455{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700456 /* These are just misnamed, they actually get/put from/to user an int */
457 switch (cmd) {
Mingming Cao617ba132006-10-11 01:20:53 -0700458 case EXT4_IOC32_GETFLAGS:
459 cmd = EXT4_IOC_GETFLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700460 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700461 case EXT4_IOC32_SETFLAGS:
462 cmd = EXT4_IOC_SETFLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700463 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700464 case EXT4_IOC32_GETVERSION:
465 cmd = EXT4_IOC_GETVERSION;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700466 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700467 case EXT4_IOC32_SETVERSION:
468 cmd = EXT4_IOC_SETVERSION;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700469 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700470 case EXT4_IOC32_GROUP_EXTEND:
471 cmd = EXT4_IOC_GROUP_EXTEND;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700472 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700473 case EXT4_IOC32_GETVERSION_OLD:
474 cmd = EXT4_IOC_GETVERSION_OLD;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700475 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700476 case EXT4_IOC32_SETVERSION_OLD:
477 cmd = EXT4_IOC_SETVERSION_OLD;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700478 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700479 case EXT4_IOC32_GETRSVSZ:
480 cmd = EXT4_IOC_GETRSVSZ;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700481 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700482 case EXT4_IOC32_SETRSVSZ:
483 cmd = EXT4_IOC_SETRSVSZ;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700484 break;
Ben Hutchings4d92dc02010-05-17 06:00:00 -0400485 case EXT4_IOC32_GROUP_ADD: {
486 struct compat_ext4_new_group_input __user *uinput;
487 struct ext4_new_group_input input;
488 mm_segment_t old_fs;
489 int err;
490
491 uinput = compat_ptr(arg);
492 err = get_user(input.group, &uinput->group);
493 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
494 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
495 err |= get_user(input.inode_table, &uinput->inode_table);
496 err |= get_user(input.blocks_count, &uinput->blocks_count);
497 err |= get_user(input.reserved_blocks,
498 &uinput->reserved_blocks);
499 if (err)
500 return -EFAULT;
501 old_fs = get_fs();
502 set_fs(KERNEL_DS);
503 err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
504 (unsigned long) &input);
505 set_fs(old_fs);
506 return err;
507 }
Christian Borntraegerb684b2e2010-05-15 00:00:00 -0400508 case EXT4_IOC_MOVE_EXT:
Tao Maa56e69c2011-03-20 23:16:58 -0400509 case FITRIM:
Yongqiang Yang19c52462012-01-04 17:09:44 -0500510 case EXT4_IOC_RESIZE_FS:
Christian Borntraegerb684b2e2010-05-15 00:00:00 -0400511 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700512 default:
513 return -ENOIOCTLCMD;
514 }
Andi Kleen5cdd7b22008-04-29 22:03:54 -0400515 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700516}
517#endif