blob: 3e14060b398e38bfca85495d02e5cae4862c3a11 [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;
26 unsigned short rsv_window_size;
27
Theodore Ts'oaf5bc922008-09-08 22:25:24 -040028 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070029
30 switch (cmd) {
Mingming Cao617ba132006-10-11 01:20:53 -070031 case EXT4_IOC_GETFLAGS:
Jan Karaff9ddf72007-07-18 09:24:20 -040032 ext4_get_inode_flags(ei);
Mingming Cao617ba132006-10-11 01:20:53 -070033 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070034 return put_user(flags, (int __user *) arg);
Mingming Cao617ba132006-10-11 01:20:53 -070035 case EXT4_IOC_SETFLAGS: {
Dave Kleikampac27a0e2006-10-11 01:20:50 -070036 handle_t *handle = NULL;
Aneesh Kumar K.V4db46fc2008-10-08 23:34:06 -040037 int err, migrate = 0;
Mingming Cao617ba132006-10-11 01:20:53 -070038 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070039 unsigned int oldflags;
40 unsigned int jflag;
41
Satyam Sharma3bd858a2007-07-17 15:00:08 +053042 if (!is_owner_or_cap(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -070043 return -EACCES;
44
45 if (get_user(flags, (int __user *) arg))
46 return -EFAULT;
47
Dave Hansen42a74f22008-02-15 14:37:46 -080048 err = mnt_want_write(filp->f_path.mnt);
49 if (err)
50 return err;
51
Dave Kleikampac27a0e2006-10-11 01:20:50 -070052 if (!S_ISDIR(inode->i_mode))
Mingming Cao617ba132006-10-11 01:20:53 -070053 flags &= ~EXT4_DIRSYNC_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070054
Dave Hansen42a74f22008-02-15 14:37:46 -080055 err = -EPERM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070056 mutex_lock(&inode->i_mutex);
Jan Karae47776a2007-11-14 16:58:56 -080057 /* Is it quota file? Do not allow user to mess with it */
Dave Hansen42a74f22008-02-15 14:37:46 -080058 if (IS_NOQUOTA(inode))
59 goto flags_out;
60
Dave Kleikampac27a0e2006-10-11 01:20:50 -070061 oldflags = ei->i_flags;
62
63 /* The JOURNAL_DATA flag is modifiable only by root */
Mingming Cao617ba132006-10-11 01:20:53 -070064 jflag = flags & EXT4_JOURNAL_DATA_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070065
66 /*
67 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
68 * the relevant capability.
69 *
70 * This test looks nicer. Thanks to Pauline Middelink
71 */
Mingming Cao617ba132006-10-11 01:20:53 -070072 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
Dave Hansen42a74f22008-02-15 14:37:46 -080073 if (!capable(CAP_LINUX_IMMUTABLE))
74 goto flags_out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070075 }
76
77 /*
78 * The JOURNAL_DATA flag can only be changed by
79 * the relevant capability.
80 */
Mingming Cao617ba132006-10-11 01:20:53 -070081 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
Dave Hansen42a74f22008-02-15 14:37:46 -080082 if (!capable(CAP_SYS_RESOURCE))
83 goto flags_out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070084 }
Aneesh Kumar K.V4db46fc2008-10-08 23:34:06 -040085 if (oldflags & EXT4_EXTENTS_FL) {
86 /* We don't support clearning extent flags */
87 if (!(flags & EXT4_EXTENTS_FL)) {
88 err = -EOPNOTSUPP;
89 goto flags_out;
90 }
91 } else if (flags & EXT4_EXTENTS_FL) {
92 /* migrate the file */
93 migrate = 1;
94 flags &= ~EXT4_EXTENTS_FL;
95 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -070096
Mingming Cao617ba132006-10-11 01:20:53 -070097 handle = ext4_journal_start(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070098 if (IS_ERR(handle)) {
Dave Hansen42a74f22008-02-15 14:37:46 -080099 err = PTR_ERR(handle);
100 goto flags_out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700101 }
102 if (IS_SYNC(inode))
103 handle->h_sync = 1;
Mingming Cao617ba132006-10-11 01:20:53 -0700104 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700105 if (err)
106 goto flags_err;
107
Mingming Cao617ba132006-10-11 01:20:53 -0700108 flags = flags & EXT4_FL_USER_MODIFIABLE;
109 flags |= oldflags & ~EXT4_FL_USER_MODIFIABLE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700110 ei->i_flags = flags;
111
Mingming Cao617ba132006-10-11 01:20:53 -0700112 ext4_set_inode_flags(inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -0400113 inode->i_ctime = ext4_current_time(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700114
Mingming Cao617ba132006-10-11 01:20:53 -0700115 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700116flags_err:
Mingming Cao617ba132006-10-11 01:20:53 -0700117 ext4_journal_stop(handle);
Dave Hansen42a74f22008-02-15 14:37:46 -0800118 if (err)
119 goto flags_out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700120
Mingming Cao617ba132006-10-11 01:20:53 -0700121 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
122 err = ext4_change_inode_journal_flag(inode, jflag);
Aneesh Kumar K.V4db46fc2008-10-08 23:34:06 -0400123 if (err)
124 goto flags_out;
125 if (migrate)
126 err = ext4_ext_migrate(inode);
Dave Hansen42a74f22008-02-15 14:37:46 -0800127flags_out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700128 mutex_unlock(&inode->i_mutex);
Dave Hansen42a74f22008-02-15 14:37:46 -0800129 mnt_drop_write(filp->f_path.mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700130 return err;
131 }
Mingming Cao617ba132006-10-11 01:20:53 -0700132 case EXT4_IOC_GETVERSION:
133 case EXT4_IOC_GETVERSION_OLD:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700134 return put_user(inode->i_generation, (int __user *) arg);
Mingming Cao617ba132006-10-11 01:20:53 -0700135 case EXT4_IOC_SETVERSION:
136 case EXT4_IOC_SETVERSION_OLD: {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700137 handle_t *handle;
Mingming Cao617ba132006-10-11 01:20:53 -0700138 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700139 __u32 generation;
140 int err;
141
Satyam Sharma3bd858a2007-07-17 15:00:08 +0530142 if (!is_owner_or_cap(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700143 return -EPERM;
Dave Hansen42a74f22008-02-15 14:37:46 -0800144
145 err = mnt_want_write(filp->f_path.mnt);
146 if (err)
147 return err;
148 if (get_user(generation, (int __user *) arg)) {
149 err = -EFAULT;
150 goto setversion_out;
151 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700152
Mingming Cao617ba132006-10-11 01:20:53 -0700153 handle = ext4_journal_start(inode, 1);
Dave Hansen42a74f22008-02-15 14:37:46 -0800154 if (IS_ERR(handle)) {
155 err = PTR_ERR(handle);
156 goto setversion_out;
157 }
Mingming Cao617ba132006-10-11 01:20:53 -0700158 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700159 if (err == 0) {
Kalpak Shahef7f3832007-07-18 09:15:20 -0400160 inode->i_ctime = ext4_current_time(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700161 inode->i_generation = generation;
Mingming Cao617ba132006-10-11 01:20:53 -0700162 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700163 }
Mingming Cao617ba132006-10-11 01:20:53 -0700164 ext4_journal_stop(handle);
Dave Hansen42a74f22008-02-15 14:37:46 -0800165setversion_out:
166 mnt_drop_write(filp->f_path.mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700167 return err;
168 }
Jose R. Santose23291b2007-07-18 08:57:06 -0400169#ifdef CONFIG_JBD2_DEBUG
Mingming Cao617ba132006-10-11 01:20:53 -0700170 case EXT4_IOC_WAIT_FOR_READONLY:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700171 /*
172 * This is racy - by the time we're woken up and running,
173 * the superblock could be released. And the module could
174 * have been unloaded. So sue me.
175 *
176 * Returns 1 if it slept, else zero.
177 */
178 {
179 struct super_block *sb = inode->i_sb;
180 DECLARE_WAITQUEUE(wait, current);
181 int ret = 0;
182
183 set_current_state(TASK_INTERRUPTIBLE);
Mingming Cao617ba132006-10-11 01:20:53 -0700184 add_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
185 if (timer_pending(&EXT4_SB(sb)->turn_ro_timer)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700186 schedule();
187 ret = 1;
188 }
Mingming Cao617ba132006-10-11 01:20:53 -0700189 remove_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700190 return ret;
191 }
192#endif
Mingming Cao617ba132006-10-11 01:20:53 -0700193 case EXT4_IOC_GETRSVSZ:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700194 if (test_opt(inode->i_sb, RESERVATION)
195 && S_ISREG(inode->i_mode)
196 && ei->i_block_alloc_info) {
197 rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
198 return put_user(rsv_window_size, (int __user *)arg);
199 }
200 return -ENOTTY;
Mingming Cao617ba132006-10-11 01:20:53 -0700201 case EXT4_IOC_SETRSVSZ: {
Dave Hansen42a74f22008-02-15 14:37:46 -0800202 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700203
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400204 if (!test_opt(inode->i_sb, RESERVATION) || !S_ISREG(inode->i_mode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700205 return -ENOTTY;
206
Satyam Sharma3bd858a2007-07-17 15:00:08 +0530207 if (!is_owner_or_cap(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700208 return -EACCES;
209
210 if (get_user(rsv_window_size, (int __user *)arg))
211 return -EFAULT;
212
Dave Hansen42a74f22008-02-15 14:37:46 -0800213 err = mnt_want_write(filp->f_path.mnt);
214 if (err)
215 return err;
216
Mingming Cao617ba132006-10-11 01:20:53 -0700217 if (rsv_window_size > EXT4_MAX_RESERVE_BLOCKS)
218 rsv_window_size = EXT4_MAX_RESERVE_BLOCKS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700219
220 /*
221 * need to allocate reservation structure for this inode
222 * before set the window size
223 */
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500224 down_write(&ei->i_data_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700225 if (!ei->i_block_alloc_info)
Mingming Cao617ba132006-10-11 01:20:53 -0700226 ext4_init_block_alloc_info(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700227
228 if (ei->i_block_alloc_info){
Mingming Cao617ba132006-10-11 01:20:53 -0700229 struct ext4_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700230 rsv->rsv_goal_size = rsv_window_size;
231 }
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500232 up_write(&ei->i_data_sem);
Dave Hansen42a74f22008-02-15 14:37:46 -0800233 mnt_drop_write(filp->f_path.mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700234 return 0;
235 }
Mingming Cao617ba132006-10-11 01:20:53 -0700236 case EXT4_IOC_GROUP_EXTEND: {
237 ext4_fsblk_t n_blocks_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700238 struct super_block *sb = inode->i_sb;
239 int err;
240
241 if (!capable(CAP_SYS_RESOURCE))
242 return -EPERM;
243
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700244 if (get_user(n_blocks_count, (__u32 __user *)arg))
245 return -EFAULT;
246
Dave Hansen42a74f22008-02-15 14:37:46 -0800247 err = mnt_want_write(filp->f_path.mnt);
248 if (err)
249 return err;
250
Mingming Cao617ba132006-10-11 01:20:53 -0700251 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
Mingming Caodab291a2006-10-11 01:21:01 -0700252 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
253 jbd2_journal_flush(EXT4_SB(sb)->s_journal);
254 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Dave Hansen42a74f22008-02-15 14:37:46 -0800255 mnt_drop_write(filp->f_path.mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700256
257 return err;
258 }
Mingming Cao617ba132006-10-11 01:20:53 -0700259 case EXT4_IOC_GROUP_ADD: {
260 struct ext4_new_group_data input;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700261 struct super_block *sb = inode->i_sb;
262 int err;
263
264 if (!capable(CAP_SYS_RESOURCE))
265 return -EPERM;
266
Mingming Cao617ba132006-10-11 01:20:53 -0700267 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700268 sizeof(input)))
269 return -EFAULT;
270
Dave Hansen42a74f22008-02-15 14:37:46 -0800271 err = mnt_want_write(filp->f_path.mnt);
272 if (err)
273 return err;
274
Mingming Cao617ba132006-10-11 01:20:53 -0700275 err = ext4_group_add(sb, &input);
Mingming Caodab291a2006-10-11 01:21:01 -0700276 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
277 jbd2_journal_flush(EXT4_SB(sb)->s_journal);
278 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Dave Hansen42a74f22008-02-15 14:37:46 -0800279 mnt_drop_write(filp->f_path.mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700280
281 return err;
282 }
283
Aneesh Kumar K.Vc14c6fd2008-01-28 23:58:26 -0500284 case EXT4_IOC_MIGRATE:
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400285 {
286 int err;
287 if (!is_owner_or_cap(inode))
288 return -EACCES;
289
290 err = mnt_want_write(filp->f_path.mnt);
291 if (err)
292 return err;
293 /*
294 * inode_mutex prevent write and truncate on the file.
295 * Read still goes through. We take i_data_sem in
296 * ext4_ext_swap_inode_data before we switch the
297 * inode format to prevent read.
298 */
299 mutex_lock(&(inode->i_mutex));
300 err = ext4_ext_migrate(inode);
301 mutex_unlock(&(inode->i_mutex));
302 mnt_drop_write(filp->f_path.mnt);
303 return err;
304 }
Aneesh Kumar K.Vc14c6fd2008-01-28 23:58:26 -0500305
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700306 default:
307 return -ENOTTY;
308 }
309}
310
311#ifdef CONFIG_COMPAT
Mingming Cao617ba132006-10-11 01:20:53 -0700312long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700313{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700314 /* These are just misnamed, they actually get/put from/to user an int */
315 switch (cmd) {
Mingming Cao617ba132006-10-11 01:20:53 -0700316 case EXT4_IOC32_GETFLAGS:
317 cmd = EXT4_IOC_GETFLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700318 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700319 case EXT4_IOC32_SETFLAGS:
320 cmd = EXT4_IOC_SETFLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700321 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700322 case EXT4_IOC32_GETVERSION:
323 cmd = EXT4_IOC_GETVERSION;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700324 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700325 case EXT4_IOC32_SETVERSION:
326 cmd = EXT4_IOC_SETVERSION;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700327 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700328 case EXT4_IOC32_GROUP_EXTEND:
329 cmd = EXT4_IOC_GROUP_EXTEND;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700330 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700331 case EXT4_IOC32_GETVERSION_OLD:
332 cmd = EXT4_IOC_GETVERSION_OLD;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700333 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700334 case EXT4_IOC32_SETVERSION_OLD:
335 cmd = EXT4_IOC_SETVERSION_OLD;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700336 break;
Jose R. Santose23291b2007-07-18 08:57:06 -0400337#ifdef CONFIG_JBD2_DEBUG
Mingming Cao617ba132006-10-11 01:20:53 -0700338 case EXT4_IOC32_WAIT_FOR_READONLY:
339 cmd = EXT4_IOC_WAIT_FOR_READONLY;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700340 break;
341#endif
Mingming Cao617ba132006-10-11 01:20:53 -0700342 case EXT4_IOC32_GETRSVSZ:
343 cmd = EXT4_IOC_GETRSVSZ;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700344 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700345 case EXT4_IOC32_SETRSVSZ:
346 cmd = EXT4_IOC_SETRSVSZ;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700347 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700348 case EXT4_IOC_GROUP_ADD:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700349 break;
350 default:
351 return -ENOIOCTLCMD;
352 }
Andi Kleen5cdd7b22008-04-29 22:03:54 -0400353 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700354}
355#endif