blob: 6eee25591b8159bc96d35a16f94f94c0855a35b9 [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;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070041 unsigned int oldflags;
42 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
Mingming Cao617ba132006-10-11 01:20:53 -0700118 flags = flags & EXT4_FL_USER_MODIFIABLE;
119 flags |= oldflags & ~EXT4_FL_USER_MODIFIABLE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700120 ei->i_flags = flags;
121
Mingming Cao617ba132006-10-11 01:20:53 -0700122 ext4_set_inode_flags(inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -0400123 inode->i_ctime = ext4_current_time(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700124
Mingming Cao617ba132006-10-11 01:20:53 -0700125 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700126flags_err:
Mingming Cao617ba132006-10-11 01:20:53 -0700127 ext4_journal_stop(handle);
Dave Hansen42a74f22008-02-15 14:37:46 -0800128 if (err)
129 goto flags_out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700130
Mingming Cao617ba132006-10-11 01:20:53 -0700131 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
132 err = ext4_change_inode_journal_flag(inode, jflag);
Aneesh Kumar K.V4db46fc2008-10-08 23:34:06 -0400133 if (err)
134 goto flags_out;
135 if (migrate)
136 err = ext4_ext_migrate(inode);
Dave Hansen42a74f22008-02-15 14:37:46 -0800137flags_out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700138 mutex_unlock(&inode->i_mutex);
Al Viro2a79f172011-12-09 08:06:57 -0500139 mnt_drop_write_file(filp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700140 return err;
141 }
Mingming Cao617ba132006-10-11 01:20:53 -0700142 case EXT4_IOC_GETVERSION:
143 case EXT4_IOC_GETVERSION_OLD:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700144 return put_user(inode->i_generation, (int __user *) arg);
Mingming Cao617ba132006-10-11 01:20:53 -0700145 case EXT4_IOC_SETVERSION:
146 case EXT4_IOC_SETVERSION_OLD: {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700147 handle_t *handle;
Mingming Cao617ba132006-10-11 01:20:53 -0700148 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700149 __u32 generation;
150 int err;
151
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700152 if (!inode_owner_or_capable(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700153 return -EPERM;
Dave Hansen42a74f22008-02-15 14:37:46 -0800154
Al Viroa561be72011-11-23 11:57:51 -0500155 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800156 if (err)
157 return err;
158 if (get_user(generation, (int __user *) arg)) {
159 err = -EFAULT;
160 goto setversion_out;
161 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700162
Djalal Harouni6c2155b2012-01-03 02:31:52 +0100163 mutex_lock(&inode->i_mutex);
Mingming Cao617ba132006-10-11 01:20:53 -0700164 handle = ext4_journal_start(inode, 1);
Dave Hansen42a74f22008-02-15 14:37:46 -0800165 if (IS_ERR(handle)) {
166 err = PTR_ERR(handle);
Djalal Harouni6c2155b2012-01-03 02:31:52 +0100167 goto unlock_out;
Dave Hansen42a74f22008-02-15 14:37:46 -0800168 }
Mingming Cao617ba132006-10-11 01:20:53 -0700169 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700170 if (err == 0) {
Kalpak Shahef7f3832007-07-18 09:15:20 -0400171 inode->i_ctime = ext4_current_time(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700172 inode->i_generation = generation;
Mingming Cao617ba132006-10-11 01:20:53 -0700173 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700174 }
Mingming Cao617ba132006-10-11 01:20:53 -0700175 ext4_journal_stop(handle);
Djalal Harouni6c2155b2012-01-03 02:31:52 +0100176
177unlock_out:
178 mutex_unlock(&inode->i_mutex);
Dave Hansen42a74f22008-02-15 14:37:46 -0800179setversion_out:
Al Viro2a79f172011-12-09 08:06:57 -0500180 mnt_drop_write_file(filp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700181 return err;
182 }
Mingming Cao617ba132006-10-11 01:20:53 -0700183 case EXT4_IOC_GROUP_EXTEND: {
184 ext4_fsblk_t n_blocks_count;
Peng Taoac046f12009-07-13 09:30:17 -0400185 int err, err2=0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700186
Yongqiang Yang8f82f842011-07-26 21:35:44 -0400187 err = ext4_resize_begin(sb);
188 if (err)
189 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700190
Djalal Harouni014a1772012-01-04 17:09:52 -0500191 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
192 err = -EFAULT;
193 goto group_extend_out;
194 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700195
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400196 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
197 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
198 ext4_msg(sb, KERN_ERR,
199 "Online resizing not supported with bigalloc");
Djalal Harouni014a1772012-01-04 17:09:52 -0500200 err = -EOPNOTSUPP;
201 goto group_extend_out;
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400202 }
203
Al Viroa561be72011-11-23 11:57:51 -0500204 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800205 if (err)
Djalal Harouni014a1772012-01-04 17:09:52 -0500206 goto group_extend_out;
Dave Hansen42a74f22008-02-15 14:37:46 -0800207
Mingming Cao617ba132006-10-11 01:20:53 -0700208 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
Peng Taoac046f12009-07-13 09:30:17 -0400209 if (EXT4_SB(sb)->s_journal) {
210 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
211 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
212 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
213 }
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -0400214 if (err == 0)
215 err = err2;
Al Viro2a79f172011-12-09 08:06:57 -0500216 mnt_drop_write_file(filp);
Djalal Harouni014a1772012-01-04 17:09:52 -0500217group_extend_out:
Yongqiang Yang8f82f842011-07-26 21:35:44 -0400218 ext4_resize_end(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700219 return err;
220 }
Akira Fujita748de672009-06-17 19:24:03 -0400221
222 case EXT4_IOC_MOVE_EXT: {
223 struct move_extent me;
224 struct file *donor_filp;
225 int err;
226
Akira Fujita4a585792009-12-06 23:38:31 -0500227 if (!(filp->f_mode & FMODE_READ) ||
228 !(filp->f_mode & FMODE_WRITE))
229 return -EBADF;
230
Akira Fujita748de672009-06-17 19:24:03 -0400231 if (copy_from_user(&me,
232 (struct move_extent __user *)arg, sizeof(me)))
233 return -EFAULT;
Akira Fujita4a585792009-12-06 23:38:31 -0500234 me.moved_len = 0;
Akira Fujita748de672009-06-17 19:24:03 -0400235
236 donor_filp = fget(me.donor_fd);
237 if (!donor_filp)
238 return -EBADF;
239
Akira Fujita4a585792009-12-06 23:38:31 -0500240 if (!(donor_filp->f_mode & FMODE_WRITE)) {
241 err = -EBADF;
242 goto mext_out;
Akira Fujita748de672009-06-17 19:24:03 -0400243 }
244
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400245 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
246 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
247 ext4_msg(sb, KERN_ERR,
248 "Online defrag not supported with bigalloc");
249 return -EOPNOTSUPP;
250 }
251
Al Viroa561be72011-11-23 11:57:51 -0500252 err = mnt_want_write_file(filp);
Akira Fujita4a585792009-12-06 23:38:31 -0500253 if (err)
254 goto mext_out;
255
Akira Fujita748de672009-06-17 19:24:03 -0400256 err = ext4_move_extents(filp, donor_filp, me.orig_start,
257 me.donor_start, me.len, &me.moved_len);
Al Viro2a79f172011-12-09 08:06:57 -0500258 mnt_drop_write_file(filp);
Akira Fujita4a585792009-12-06 23:38:31 -0500259 mnt_drop_write(filp->f_path.mnt);
Akira Fujita748de672009-06-17 19:24:03 -0400260
Theodore Ts'o60e66792010-05-17 07:00:00 -0400261 if (copy_to_user((struct move_extent __user *)arg,
Akira Fujitac437b272010-03-04 00:39:24 -0500262 &me, sizeof(me)))
Akira Fujita4a585792009-12-06 23:38:31 -0500263 err = -EFAULT;
264mext_out:
265 fput(donor_filp);
Akira Fujita748de672009-06-17 19:24:03 -0400266 return err;
267 }
268
Mingming Cao617ba132006-10-11 01:20:53 -0700269 case EXT4_IOC_GROUP_ADD: {
270 struct ext4_new_group_data input;
Peng Taoac046f12009-07-13 09:30:17 -0400271 int err, err2=0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700272
Yongqiang Yang8f82f842011-07-26 21:35:44 -0400273 err = ext4_resize_begin(sb);
274 if (err)
275 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700276
Mingming Cao617ba132006-10-11 01:20:53 -0700277 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
Djalal Harouni014a1772012-01-04 17:09:52 -0500278 sizeof(input))) {
279 err = -EFAULT;
280 goto group_add_out;
281 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700282
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400283 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
284 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
285 ext4_msg(sb, KERN_ERR,
286 "Online resizing not supported with bigalloc");
Djalal Harouni014a1772012-01-04 17:09:52 -0500287 err = -EOPNOTSUPP;
288 goto group_add_out;
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400289 }
290
Al Viroa561be72011-11-23 11:57:51 -0500291 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800292 if (err)
Djalal Harouni014a1772012-01-04 17:09:52 -0500293 goto group_add_out;
Dave Hansen42a74f22008-02-15 14:37:46 -0800294
Mingming Cao617ba132006-10-11 01:20:53 -0700295 err = ext4_group_add(sb, &input);
Peng Taoac046f12009-07-13 09:30:17 -0400296 if (EXT4_SB(sb)->s_journal) {
297 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
298 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
299 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
300 }
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -0400301 if (err == 0)
302 err = err2;
Al Viro2a79f172011-12-09 08:06:57 -0500303 mnt_drop_write_file(filp);
Djalal Harouni014a1772012-01-04 17:09:52 -0500304group_add_out:
Yongqiang Yang8f82f842011-07-26 21:35:44 -0400305 ext4_resize_end(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700306 return err;
307 }
308
Aneesh Kumar K.Vc14c6fd2008-01-28 23:58:26 -0500309 case EXT4_IOC_MIGRATE:
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400310 {
311 int err;
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700312 if (!inode_owner_or_capable(inode))
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400313 return -EACCES;
314
Al Viroa561be72011-11-23 11:57:51 -0500315 err = mnt_want_write_file(filp);
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400316 if (err)
317 return err;
318 /*
319 * inode_mutex prevent write and truncate on the file.
320 * Read still goes through. We take i_data_sem in
321 * ext4_ext_swap_inode_data before we switch the
322 * inode format to prevent read.
323 */
324 mutex_lock(&(inode->i_mutex));
325 err = ext4_ext_migrate(inode);
326 mutex_unlock(&(inode->i_mutex));
Al Viro2a79f172011-12-09 08:06:57 -0500327 mnt_drop_write_file(filp);
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400328 return err;
329 }
Aneesh Kumar K.Vc14c6fd2008-01-28 23:58:26 -0500330
Theodore Ts'occd25062009-02-26 01:04:07 -0500331 case EXT4_IOC_ALLOC_DA_BLKS:
332 {
333 int err;
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700334 if (!inode_owner_or_capable(inode))
Theodore Ts'occd25062009-02-26 01:04:07 -0500335 return -EACCES;
336
Al Viroa561be72011-11-23 11:57:51 -0500337 err = mnt_want_write_file(filp);
Theodore Ts'occd25062009-02-26 01:04:07 -0500338 if (err)
339 return err;
340 err = ext4_alloc_da_blocks(inode);
Al Viro2a79f172011-12-09 08:06:57 -0500341 mnt_drop_write_file(filp);
Theodore Ts'occd25062009-02-26 01:04:07 -0500342 return err;
343 }
344
Yongqiang Yang19c52462012-01-04 17:09:44 -0500345 case EXT4_IOC_RESIZE_FS: {
346 ext4_fsblk_t n_blocks_count;
347 struct super_block *sb = inode->i_sb;
348 int err = 0, err2 = 0;
349
350 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
351 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
352 ext4_msg(sb, KERN_ERR,
353 "Online resizing not (yet) supported with bigalloc");
354 return -EOPNOTSUPP;
355 }
356
357 if (EXT4_HAS_INCOMPAT_FEATURE(sb,
358 EXT4_FEATURE_INCOMPAT_META_BG)) {
359 ext4_msg(sb, KERN_ERR,
360 "Online resizing not (yet) supported with meta_bg");
361 return -EOPNOTSUPP;
362 }
363
364 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
365 sizeof(__u64))) {
366 return -EFAULT;
367 }
368
369 if (n_blocks_count > MAX_32_NUM &&
370 !EXT4_HAS_INCOMPAT_FEATURE(sb,
371 EXT4_FEATURE_INCOMPAT_64BIT)) {
372 ext4_msg(sb, KERN_ERR,
373 "File system only supports 32-bit block numbers");
374 return -EOPNOTSUPP;
375 }
376
377 err = ext4_resize_begin(sb);
378 if (err)
379 return err;
380
381 err = mnt_want_write(filp->f_path.mnt);
382 if (err)
383 goto resizefs_out;
384
385 err = ext4_resize_fs(sb, n_blocks_count);
386 if (EXT4_SB(sb)->s_journal) {
387 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
388 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
389 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
390 }
391 if (err == 0)
392 err = err2;
393 mnt_drop_write(filp->f_path.mnt);
394resizefs_out:
395 ext4_resize_end(sb);
396 return err;
397 }
398
Lukas Czernere681c042010-11-19 21:47:07 -0500399 case FITRIM:
400 {
Lukas Czerner41431792011-02-23 12:42:32 -0500401 struct request_queue *q = bdev_get_queue(sb->s_bdev);
Lukas Czernere681c042010-11-19 21:47:07 -0500402 struct fstrim_range range;
403 int ret = 0;
404
405 if (!capable(CAP_SYS_ADMIN))
406 return -EPERM;
407
Lukas Czerner41431792011-02-23 12:42:32 -0500408 if (!blk_queue_discard(q))
409 return -EOPNOTSUPP;
410
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400411 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
412 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
413 ext4_msg(sb, KERN_ERR,
414 "FITRIM not supported with bigalloc");
415 return -EOPNOTSUPP;
416 }
417
H Hartley Sweetene6705f72011-10-18 10:59:51 -0400418 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
Lukas Czernere681c042010-11-19 21:47:07 -0500419 sizeof(range)))
420 return -EFAULT;
421
Lukas Czerner5c2ed622011-02-23 17:49:51 -0500422 range.minlen = max((unsigned int)range.minlen,
423 q->limits.discard_granularity);
Lukas Czernere681c042010-11-19 21:47:07 -0500424 ret = ext4_trim_fs(sb, &range);
425 if (ret < 0)
426 return ret;
427
H Hartley Sweetene6705f72011-10-18 10:59:51 -0400428 if (copy_to_user((struct fstrim_range __user *)arg, &range,
Lukas Czernere681c042010-11-19 21:47:07 -0500429 sizeof(range)))
430 return -EFAULT;
431
432 return 0;
433 }
434
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700435 default:
436 return -ENOTTY;
437 }
438}
439
440#ifdef CONFIG_COMPAT
Mingming Cao617ba132006-10-11 01:20:53 -0700441long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700442{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700443 /* These are just misnamed, they actually get/put from/to user an int */
444 switch (cmd) {
Mingming Cao617ba132006-10-11 01:20:53 -0700445 case EXT4_IOC32_GETFLAGS:
446 cmd = EXT4_IOC_GETFLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700447 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700448 case EXT4_IOC32_SETFLAGS:
449 cmd = EXT4_IOC_SETFLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700450 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700451 case EXT4_IOC32_GETVERSION:
452 cmd = EXT4_IOC_GETVERSION;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700453 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700454 case EXT4_IOC32_SETVERSION:
455 cmd = EXT4_IOC_SETVERSION;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700456 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700457 case EXT4_IOC32_GROUP_EXTEND:
458 cmd = EXT4_IOC_GROUP_EXTEND;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700459 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700460 case EXT4_IOC32_GETVERSION_OLD:
461 cmd = EXT4_IOC_GETVERSION_OLD;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700462 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700463 case EXT4_IOC32_SETVERSION_OLD:
464 cmd = EXT4_IOC_SETVERSION_OLD;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700465 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700466 case EXT4_IOC32_GETRSVSZ:
467 cmd = EXT4_IOC_GETRSVSZ;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700468 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700469 case EXT4_IOC32_SETRSVSZ:
470 cmd = EXT4_IOC_SETRSVSZ;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700471 break;
Ben Hutchings4d92dc02010-05-17 06:00:00 -0400472 case EXT4_IOC32_GROUP_ADD: {
473 struct compat_ext4_new_group_input __user *uinput;
474 struct ext4_new_group_input input;
475 mm_segment_t old_fs;
476 int err;
477
478 uinput = compat_ptr(arg);
479 err = get_user(input.group, &uinput->group);
480 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
481 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
482 err |= get_user(input.inode_table, &uinput->inode_table);
483 err |= get_user(input.blocks_count, &uinput->blocks_count);
484 err |= get_user(input.reserved_blocks,
485 &uinput->reserved_blocks);
486 if (err)
487 return -EFAULT;
488 old_fs = get_fs();
489 set_fs(KERNEL_DS);
490 err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
491 (unsigned long) &input);
492 set_fs(old_fs);
493 return err;
494 }
Christian Borntraegerb684b2e2010-05-15 00:00:00 -0400495 case EXT4_IOC_MOVE_EXT:
Tao Maa56e69c2011-03-20 23:16:58 -0400496 case FITRIM:
Yongqiang Yang19c52462012-01-04 17:09:44 -0500497 case EXT4_IOC_RESIZE_FS:
Christian Borntraegerb684b2e2010-05-15 00:00:00 -0400498 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700499 default:
500 return -ENOIOCTLCMD;
501 }
Andi Kleen5cdd7b22008-04-29 22:03:54 -0400502 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700503}
504#endif