blob: dc99b4776d58f052806b322b1bd91f8c7765ff83 [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
Dave Kleikampac27a0e2006-10-11 01:20:50 -070051 if (!S_ISDIR(inode->i_mode))
Mingming Cao617ba132006-10-11 01:20:53 -070052 flags &= ~EXT4_DIRSYNC_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070053
Dave Hansen42a74f22008-02-15 14:37:46 -080054 err = -EPERM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070055 mutex_lock(&inode->i_mutex);
Jan Karae47776a2007-11-14 16:58:56 -080056 /* Is it quota file? Do not allow user to mess with it */
Dave Hansen42a74f22008-02-15 14:37:46 -080057 if (IS_NOQUOTA(inode))
58 goto flags_out;
59
Dave Kleikampac27a0e2006-10-11 01:20:50 -070060 oldflags = ei->i_flags;
61
62 /* The JOURNAL_DATA flag is modifiable only by root */
Mingming Cao617ba132006-10-11 01:20:53 -070063 jflag = flags & EXT4_JOURNAL_DATA_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070064
65 /*
66 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
67 * the relevant capability.
68 *
69 * This test looks nicer. Thanks to Pauline Middelink
70 */
Mingming Cao617ba132006-10-11 01:20:53 -070071 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
Dave Hansen42a74f22008-02-15 14:37:46 -080072 if (!capable(CAP_LINUX_IMMUTABLE))
73 goto flags_out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070074 }
75
76 /*
77 * The JOURNAL_DATA flag can only be changed by
78 * the relevant capability.
79 */
Mingming Cao617ba132006-10-11 01:20:53 -070080 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
Dave Hansen42a74f22008-02-15 14:37:46 -080081 if (!capable(CAP_SYS_RESOURCE))
82 goto flags_out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070083 }
Aneesh Kumar K.V4db46fc2008-10-08 23:34:06 -040084 if (oldflags & EXT4_EXTENTS_FL) {
85 /* We don't support clearning extent flags */
86 if (!(flags & EXT4_EXTENTS_FL)) {
87 err = -EOPNOTSUPP;
88 goto flags_out;
89 }
90 } else if (flags & EXT4_EXTENTS_FL) {
91 /* migrate the file */
92 migrate = 1;
93 flags &= ~EXT4_EXTENTS_FL;
94 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -070095
Mingming Cao617ba132006-10-11 01:20:53 -070096 handle = ext4_journal_start(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070097 if (IS_ERR(handle)) {
Dave Hansen42a74f22008-02-15 14:37:46 -080098 err = PTR_ERR(handle);
99 goto flags_out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700100 }
101 if (IS_SYNC(inode))
102 handle->h_sync = 1;
Mingming Cao617ba132006-10-11 01:20:53 -0700103 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700104 if (err)
105 goto flags_err;
106
Mingming Cao617ba132006-10-11 01:20:53 -0700107 flags = flags & EXT4_FL_USER_MODIFIABLE;
108 flags |= oldflags & ~EXT4_FL_USER_MODIFIABLE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700109 ei->i_flags = flags;
110
Mingming Cao617ba132006-10-11 01:20:53 -0700111 ext4_set_inode_flags(inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -0400112 inode->i_ctime = ext4_current_time(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700113
Mingming Cao617ba132006-10-11 01:20:53 -0700114 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700115flags_err:
Mingming Cao617ba132006-10-11 01:20:53 -0700116 ext4_journal_stop(handle);
Dave Hansen42a74f22008-02-15 14:37:46 -0800117 if (err)
118 goto flags_out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700119
Mingming Cao617ba132006-10-11 01:20:53 -0700120 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
121 err = ext4_change_inode_journal_flag(inode, jflag);
Aneesh Kumar K.V4db46fc2008-10-08 23:34:06 -0400122 if (err)
123 goto flags_out;
124 if (migrate)
125 err = ext4_ext_migrate(inode);
Dave Hansen42a74f22008-02-15 14:37:46 -0800126flags_out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700127 mutex_unlock(&inode->i_mutex);
Dave Hansen42a74f22008-02-15 14:37:46 -0800128 mnt_drop_write(filp->f_path.mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700129 return err;
130 }
Mingming Cao617ba132006-10-11 01:20:53 -0700131 case EXT4_IOC_GETVERSION:
132 case EXT4_IOC_GETVERSION_OLD:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700133 return put_user(inode->i_generation, (int __user *) arg);
Mingming Cao617ba132006-10-11 01:20:53 -0700134 case EXT4_IOC_SETVERSION:
135 case EXT4_IOC_SETVERSION_OLD: {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700136 handle_t *handle;
Mingming Cao617ba132006-10-11 01:20:53 -0700137 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700138 __u32 generation;
139 int err;
140
Satyam Sharma3bd858a2007-07-17 15:00:08 +0530141 if (!is_owner_or_cap(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700142 return -EPERM;
Dave Hansen42a74f22008-02-15 14:37:46 -0800143
144 err = mnt_want_write(filp->f_path.mnt);
145 if (err)
146 return err;
147 if (get_user(generation, (int __user *) arg)) {
148 err = -EFAULT;
149 goto setversion_out;
150 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700151
Mingming Cao617ba132006-10-11 01:20:53 -0700152 handle = ext4_journal_start(inode, 1);
Dave Hansen42a74f22008-02-15 14:37:46 -0800153 if (IS_ERR(handle)) {
154 err = PTR_ERR(handle);
155 goto setversion_out;
156 }
Mingming Cao617ba132006-10-11 01:20:53 -0700157 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700158 if (err == 0) {
Kalpak Shahef7f3832007-07-18 09:15:20 -0400159 inode->i_ctime = ext4_current_time(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700160 inode->i_generation = generation;
Mingming Cao617ba132006-10-11 01:20:53 -0700161 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700162 }
Mingming Cao617ba132006-10-11 01:20:53 -0700163 ext4_journal_stop(handle);
Dave Hansen42a74f22008-02-15 14:37:46 -0800164setversion_out:
165 mnt_drop_write(filp->f_path.mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700166 return err;
167 }
Jose R. Santose23291b2007-07-18 08:57:06 -0400168#ifdef CONFIG_JBD2_DEBUG
Mingming Cao617ba132006-10-11 01:20:53 -0700169 case EXT4_IOC_WAIT_FOR_READONLY:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700170 /*
171 * This is racy - by the time we're woken up and running,
172 * the superblock could be released. And the module could
173 * have been unloaded. So sue me.
174 *
175 * Returns 1 if it slept, else zero.
176 */
177 {
178 struct super_block *sb = inode->i_sb;
179 DECLARE_WAITQUEUE(wait, current);
180 int ret = 0;
181
182 set_current_state(TASK_INTERRUPTIBLE);
Mingming Cao617ba132006-10-11 01:20:53 -0700183 add_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
184 if (timer_pending(&EXT4_SB(sb)->turn_ro_timer)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700185 schedule();
186 ret = 1;
187 }
Mingming Cao617ba132006-10-11 01:20:53 -0700188 remove_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700189 return ret;
190 }
191#endif
Mingming Cao617ba132006-10-11 01:20:53 -0700192 case EXT4_IOC_GROUP_EXTEND: {
193 ext4_fsblk_t n_blocks_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700194 struct super_block *sb = inode->i_sb;
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -0400195 int err, err2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700196
197 if (!capable(CAP_SYS_RESOURCE))
198 return -EPERM;
199
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700200 if (get_user(n_blocks_count, (__u32 __user *)arg))
201 return -EFAULT;
202
Dave Hansen42a74f22008-02-15 14:37:46 -0800203 err = mnt_want_write(filp->f_path.mnt);
204 if (err)
205 return err;
206
Mingming Cao617ba132006-10-11 01:20:53 -0700207 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
Mingming Caodab291a2006-10-11 01:21:01 -0700208 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -0400209 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
Mingming Caodab291a2006-10-11 01:21:01 -0700210 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -0400211 if (err == 0)
212 err = err2;
Dave Hansen42a74f22008-02-15 14:37:46 -0800213 mnt_drop_write(filp->f_path.mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700214
215 return err;
216 }
Mingming Cao617ba132006-10-11 01:20:53 -0700217 case EXT4_IOC_GROUP_ADD: {
218 struct ext4_new_group_data input;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700219 struct super_block *sb = inode->i_sb;
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -0400220 int err, err2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700221
222 if (!capable(CAP_SYS_RESOURCE))
223 return -EPERM;
224
Mingming Cao617ba132006-10-11 01:20:53 -0700225 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700226 sizeof(input)))
227 return -EFAULT;
228
Dave Hansen42a74f22008-02-15 14:37:46 -0800229 err = mnt_want_write(filp->f_path.mnt);
230 if (err)
231 return err;
232
Mingming Cao617ba132006-10-11 01:20:53 -0700233 err = ext4_group_add(sb, &input);
Mingming Caodab291a2006-10-11 01:21:01 -0700234 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -0400235 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
Mingming Caodab291a2006-10-11 01:21:01 -0700236 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -0400237 if (err == 0)
238 err = err2;
Dave Hansen42a74f22008-02-15 14:37:46 -0800239 mnt_drop_write(filp->f_path.mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700240
241 return err;
242 }
243
Aneesh Kumar K.Vc14c6fd2008-01-28 23:58:26 -0500244 case EXT4_IOC_MIGRATE:
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400245 {
246 int err;
247 if (!is_owner_or_cap(inode))
248 return -EACCES;
249
250 err = mnt_want_write(filp->f_path.mnt);
251 if (err)
252 return err;
253 /*
254 * inode_mutex prevent write and truncate on the file.
255 * Read still goes through. We take i_data_sem in
256 * ext4_ext_swap_inode_data before we switch the
257 * inode format to prevent read.
258 */
259 mutex_lock(&(inode->i_mutex));
260 err = ext4_ext_migrate(inode);
261 mutex_unlock(&(inode->i_mutex));
262 mnt_drop_write(filp->f_path.mnt);
263 return err;
264 }
Aneesh Kumar K.Vc14c6fd2008-01-28 23:58:26 -0500265
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700266 default:
267 return -ENOTTY;
268 }
269}
270
271#ifdef CONFIG_COMPAT
Mingming Cao617ba132006-10-11 01:20:53 -0700272long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700273{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700274 /* These are just misnamed, they actually get/put from/to user an int */
275 switch (cmd) {
Mingming Cao617ba132006-10-11 01:20:53 -0700276 case EXT4_IOC32_GETFLAGS:
277 cmd = EXT4_IOC_GETFLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700278 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700279 case EXT4_IOC32_SETFLAGS:
280 cmd = EXT4_IOC_SETFLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700281 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700282 case EXT4_IOC32_GETVERSION:
283 cmd = EXT4_IOC_GETVERSION;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700284 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700285 case EXT4_IOC32_SETVERSION:
286 cmd = EXT4_IOC_SETVERSION;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700287 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700288 case EXT4_IOC32_GROUP_EXTEND:
289 cmd = EXT4_IOC_GROUP_EXTEND;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700290 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700291 case EXT4_IOC32_GETVERSION_OLD:
292 cmd = EXT4_IOC_GETVERSION_OLD;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700293 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700294 case EXT4_IOC32_SETVERSION_OLD:
295 cmd = EXT4_IOC_SETVERSION_OLD;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700296 break;
Jose R. Santose23291b2007-07-18 08:57:06 -0400297#ifdef CONFIG_JBD2_DEBUG
Mingming Cao617ba132006-10-11 01:20:53 -0700298 case EXT4_IOC32_WAIT_FOR_READONLY:
299 cmd = EXT4_IOC_WAIT_FOR_READONLY;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700300 break;
301#endif
Mingming Cao617ba132006-10-11 01:20:53 -0700302 case EXT4_IOC32_GETRSVSZ:
303 cmd = EXT4_IOC_GETRSVSZ;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700304 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700305 case EXT4_IOC32_SETRSVSZ:
306 cmd = EXT4_IOC_SETRSVSZ;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700307 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700308 case EXT4_IOC_GROUP_ADD:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700309 break;
310 default:
311 return -ENOIOCTLCMD;
312 }
Andi Kleen5cdd7b22008-04-29 22:03:54 -0400313 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700314}
315#endif