blob: 91e75f7a9e732c4ba6cdf909cfd3607f32dbd800 [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>
15#include <linux/smp_lock.h>
Dave Hansen42a74f22008-02-15 14:37:46 -080016#include <linux/mount.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
Andi Kleen5cdd7b22008-04-29 22:03:54 -040021long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070022{
Andi Kleen5cdd7b22008-04-29 22:03:54 -040023 struct inode *inode = filp->f_dentry->d_inode;
Mingming Cao617ba132006-10-11 01:20:53 -070024 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070025 unsigned int flags;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070026
Theodore Ts'oaf5bc922008-09-08 22:25:24 -040027 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070028
29 switch (cmd) {
Mingming Cao617ba132006-10-11 01:20:53 -070030 case EXT4_IOC_GETFLAGS:
Jan Karaff9ddf72007-07-18 09:24:20 -040031 ext4_get_inode_flags(ei);
Mingming Cao617ba132006-10-11 01:20:53 -070032 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070033 return put_user(flags, (int __user *) arg);
Mingming Cao617ba132006-10-11 01:20:53 -070034 case EXT4_IOC_SETFLAGS: {
Dave Kleikampac27a0e2006-10-11 01:20:50 -070035 handle_t *handle = NULL;
Aneesh Kumar K.V4db46fc2008-10-08 23:34:06 -040036 int err, migrate = 0;
Mingming Cao617ba132006-10-11 01:20:53 -070037 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070038 unsigned int oldflags;
39 unsigned int jflag;
40
Satyam Sharma3bd858a2007-07-17 15:00:08 +053041 if (!is_owner_or_cap(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -070042 return -EACCES;
43
44 if (get_user(flags, (int __user *) arg))
45 return -EFAULT;
46
Dave Hansen42a74f22008-02-15 14:37:46 -080047 err = mnt_want_write(filp->f_path.mnt);
48 if (err)
49 return err;
50
Duane Griffin2dc6b0d2009-02-15 18:09:20 -050051 flags = ext4_mask_flags(inode->i_mode, flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070052
Dave Hansen42a74f22008-02-15 14:37:46 -080053 err = -EPERM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070054 mutex_lock(&inode->i_mutex);
Jan Karae47776a2007-11-14 16:58:56 -080055 /* Is it quota file? Do not allow user to mess with it */
Dave Hansen42a74f22008-02-15 14:37:46 -080056 if (IS_NOQUOTA(inode))
57 goto flags_out;
58
Dave Kleikampac27a0e2006-10-11 01:20:50 -070059 oldflags = ei->i_flags;
60
61 /* The JOURNAL_DATA flag is modifiable only by root */
Mingming Cao617ba132006-10-11 01:20:53 -070062 jflag = flags & EXT4_JOURNAL_DATA_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070063
64 /*
65 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
66 * the relevant capability.
67 *
68 * This test looks nicer. Thanks to Pauline Middelink
69 */
Mingming Cao617ba132006-10-11 01:20:53 -070070 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
Dave Hansen42a74f22008-02-15 14:37:46 -080071 if (!capable(CAP_LINUX_IMMUTABLE))
72 goto flags_out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070073 }
74
75 /*
76 * The JOURNAL_DATA flag can only be changed by
77 * the relevant capability.
78 */
Mingming Cao617ba132006-10-11 01:20:53 -070079 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
Dave Hansen42a74f22008-02-15 14:37:46 -080080 if (!capable(CAP_SYS_RESOURCE))
81 goto flags_out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070082 }
Aneesh Kumar K.V4db46fc2008-10-08 23:34:06 -040083 if (oldflags & EXT4_EXTENTS_FL) {
84 /* We don't support clearning extent flags */
85 if (!(flags & EXT4_EXTENTS_FL)) {
86 err = -EOPNOTSUPP;
87 goto flags_out;
88 }
89 } else if (flags & EXT4_EXTENTS_FL) {
90 /* migrate the file */
91 migrate = 1;
92 flags &= ~EXT4_EXTENTS_FL;
93 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -070094
Mingming Cao617ba132006-10-11 01:20:53 -070095 handle = ext4_journal_start(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070096 if (IS_ERR(handle)) {
Dave Hansen42a74f22008-02-15 14:37:46 -080097 err = PTR_ERR(handle);
98 goto flags_out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070099 }
100 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500101 ext4_handle_sync(handle);
Mingming Cao617ba132006-10-11 01:20:53 -0700102 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700103 if (err)
104 goto flags_err;
105
Mingming Cao617ba132006-10-11 01:20:53 -0700106 flags = flags & EXT4_FL_USER_MODIFIABLE;
107 flags |= oldflags & ~EXT4_FL_USER_MODIFIABLE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700108 ei->i_flags = flags;
109
Mingming Cao617ba132006-10-11 01:20:53 -0700110 ext4_set_inode_flags(inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -0400111 inode->i_ctime = ext4_current_time(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700112
Mingming Cao617ba132006-10-11 01:20:53 -0700113 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700114flags_err:
Mingming Cao617ba132006-10-11 01:20:53 -0700115 ext4_journal_stop(handle);
Dave Hansen42a74f22008-02-15 14:37:46 -0800116 if (err)
117 goto flags_out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700118
Mingming Cao617ba132006-10-11 01:20:53 -0700119 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
120 err = ext4_change_inode_journal_flag(inode, jflag);
Aneesh Kumar K.V4db46fc2008-10-08 23:34:06 -0400121 if (err)
122 goto flags_out;
123 if (migrate)
124 err = ext4_ext_migrate(inode);
Dave Hansen42a74f22008-02-15 14:37:46 -0800125flags_out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700126 mutex_unlock(&inode->i_mutex);
Dave Hansen42a74f22008-02-15 14:37:46 -0800127 mnt_drop_write(filp->f_path.mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700128 return err;
129 }
Mingming Cao617ba132006-10-11 01:20:53 -0700130 case EXT4_IOC_GETVERSION:
131 case EXT4_IOC_GETVERSION_OLD:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700132 return put_user(inode->i_generation, (int __user *) arg);
Mingming Cao617ba132006-10-11 01:20:53 -0700133 case EXT4_IOC_SETVERSION:
134 case EXT4_IOC_SETVERSION_OLD: {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700135 handle_t *handle;
Mingming Cao617ba132006-10-11 01:20:53 -0700136 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700137 __u32 generation;
138 int err;
139
Satyam Sharma3bd858a2007-07-17 15:00:08 +0530140 if (!is_owner_or_cap(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700141 return -EPERM;
Dave Hansen42a74f22008-02-15 14:37:46 -0800142
143 err = mnt_want_write(filp->f_path.mnt);
144 if (err)
145 return err;
146 if (get_user(generation, (int __user *) arg)) {
147 err = -EFAULT;
148 goto setversion_out;
149 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700150
Mingming Cao617ba132006-10-11 01:20:53 -0700151 handle = ext4_journal_start(inode, 1);
Dave Hansen42a74f22008-02-15 14:37:46 -0800152 if (IS_ERR(handle)) {
153 err = PTR_ERR(handle);
154 goto setversion_out;
155 }
Mingming Cao617ba132006-10-11 01:20:53 -0700156 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700157 if (err == 0) {
Kalpak Shahef7f3832007-07-18 09:15:20 -0400158 inode->i_ctime = ext4_current_time(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700159 inode->i_generation = generation;
Mingming Cao617ba132006-10-11 01:20:53 -0700160 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700161 }
Mingming Cao617ba132006-10-11 01:20:53 -0700162 ext4_journal_stop(handle);
Dave Hansen42a74f22008-02-15 14:37:46 -0800163setversion_out:
164 mnt_drop_write(filp->f_path.mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700165 return err;
166 }
Jose R. Santose23291b2007-07-18 08:57:06 -0400167#ifdef CONFIG_JBD2_DEBUG
Mingming Cao617ba132006-10-11 01:20:53 -0700168 case EXT4_IOC_WAIT_FOR_READONLY:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700169 /*
170 * This is racy - by the time we're woken up and running,
171 * the superblock could be released. And the module could
172 * have been unloaded. So sue me.
173 *
174 * Returns 1 if it slept, else zero.
175 */
176 {
177 struct super_block *sb = inode->i_sb;
178 DECLARE_WAITQUEUE(wait, current);
179 int ret = 0;
180
181 set_current_state(TASK_INTERRUPTIBLE);
Mingming Cao617ba132006-10-11 01:20:53 -0700182 add_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
183 if (timer_pending(&EXT4_SB(sb)->turn_ro_timer)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700184 schedule();
185 ret = 1;
186 }
Mingming Cao617ba132006-10-11 01:20:53 -0700187 remove_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700188 return ret;
189 }
190#endif
Mingming Cao617ba132006-10-11 01:20:53 -0700191 case EXT4_IOC_GROUP_EXTEND: {
192 ext4_fsblk_t n_blocks_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700193 struct super_block *sb = inode->i_sb;
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -0400194 int err, err2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700195
196 if (!capable(CAP_SYS_RESOURCE))
197 return -EPERM;
198
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700199 if (get_user(n_blocks_count, (__u32 __user *)arg))
200 return -EFAULT;
201
Dave Hansen42a74f22008-02-15 14:37:46 -0800202 err = mnt_want_write(filp->f_path.mnt);
203 if (err)
204 return err;
205
Mingming Cao617ba132006-10-11 01:20:53 -0700206 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
Mingming Caodab291a2006-10-11 01:21:01 -0700207 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -0400208 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
Mingming Caodab291a2006-10-11 01:21:01 -0700209 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -0400210 if (err == 0)
211 err = err2;
Dave Hansen42a74f22008-02-15 14:37:46 -0800212 mnt_drop_write(filp->f_path.mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700213
214 return err;
215 }
Mingming Cao617ba132006-10-11 01:20:53 -0700216 case EXT4_IOC_GROUP_ADD: {
217 struct ext4_new_group_data input;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700218 struct super_block *sb = inode->i_sb;
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -0400219 int err, err2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700220
221 if (!capable(CAP_SYS_RESOURCE))
222 return -EPERM;
223
Mingming Cao617ba132006-10-11 01:20:53 -0700224 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700225 sizeof(input)))
226 return -EFAULT;
227
Dave Hansen42a74f22008-02-15 14:37:46 -0800228 err = mnt_want_write(filp->f_path.mnt);
229 if (err)
230 return err;
231
Mingming Cao617ba132006-10-11 01:20:53 -0700232 err = ext4_group_add(sb, &input);
Mingming Caodab291a2006-10-11 01:21:01 -0700233 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -0400234 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
Mingming Caodab291a2006-10-11 01:21:01 -0700235 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -0400236 if (err == 0)
237 err = err2;
Dave Hansen42a74f22008-02-15 14:37:46 -0800238 mnt_drop_write(filp->f_path.mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700239
240 return err;
241 }
242
Aneesh Kumar K.Vc14c6fd2008-01-28 23:58:26 -0500243 case EXT4_IOC_MIGRATE:
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400244 {
245 int err;
246 if (!is_owner_or_cap(inode))
247 return -EACCES;
248
249 err = mnt_want_write(filp->f_path.mnt);
250 if (err)
251 return err;
252 /*
253 * inode_mutex prevent write and truncate on the file.
254 * Read still goes through. We take i_data_sem in
255 * ext4_ext_swap_inode_data before we switch the
256 * inode format to prevent read.
257 */
258 mutex_lock(&(inode->i_mutex));
259 err = ext4_ext_migrate(inode);
260 mutex_unlock(&(inode->i_mutex));
261 mnt_drop_write(filp->f_path.mnt);
262 return err;
263 }
Aneesh Kumar K.Vc14c6fd2008-01-28 23:58:26 -0500264
Theodore Ts'occd25062009-02-26 01:04:07 -0500265 case EXT4_IOC_ALLOC_DA_BLKS:
266 {
267 int err;
268 if (!is_owner_or_cap(inode))
269 return -EACCES;
270
271 err = mnt_want_write(filp->f_path.mnt);
272 if (err)
273 return err;
274 err = ext4_alloc_da_blocks(inode);
275 mnt_drop_write(filp->f_path.mnt);
276 return err;
277 }
278
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700279 default:
280 return -ENOTTY;
281 }
282}
283
284#ifdef CONFIG_COMPAT
Mingming Cao617ba132006-10-11 01:20:53 -0700285long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700286{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700287 /* These are just misnamed, they actually get/put from/to user an int */
288 switch (cmd) {
Mingming Cao617ba132006-10-11 01:20:53 -0700289 case EXT4_IOC32_GETFLAGS:
290 cmd = EXT4_IOC_GETFLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700291 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700292 case EXT4_IOC32_SETFLAGS:
293 cmd = EXT4_IOC_SETFLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700294 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700295 case EXT4_IOC32_GETVERSION:
296 cmd = EXT4_IOC_GETVERSION;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700297 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700298 case EXT4_IOC32_SETVERSION:
299 cmd = EXT4_IOC_SETVERSION;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700300 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700301 case EXT4_IOC32_GROUP_EXTEND:
302 cmd = EXT4_IOC_GROUP_EXTEND;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700303 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700304 case EXT4_IOC32_GETVERSION_OLD:
305 cmd = EXT4_IOC_GETVERSION_OLD;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700306 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700307 case EXT4_IOC32_SETVERSION_OLD:
308 cmd = EXT4_IOC_SETVERSION_OLD;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700309 break;
Jose R. Santose23291b2007-07-18 08:57:06 -0400310#ifdef CONFIG_JBD2_DEBUG
Mingming Cao617ba132006-10-11 01:20:53 -0700311 case EXT4_IOC32_WAIT_FOR_READONLY:
312 cmd = EXT4_IOC_WAIT_FOR_READONLY;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700313 break;
314#endif
Mingming Cao617ba132006-10-11 01:20:53 -0700315 case EXT4_IOC32_GETRSVSZ:
316 cmd = EXT4_IOC_GETRSVSZ;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700317 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700318 case EXT4_IOC32_SETRSVSZ:
319 cmd = EXT4_IOC_SETRSVSZ;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700320 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700321 case EXT4_IOC_GROUP_ADD:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700322 break;
323 default:
324 return -ENOIOCTLCMD;
325 }
Andi Kleen5cdd7b22008-04-29 22:03:54 -0400326 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700327}
328#endif