blob: c04c7ccba9e3f140ed5243a6ccdcc58d6afcaa15 [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>
Mingming Cao617ba132006-10-11 01:20:53 -070013#include <linux/ext4_fs.h>
Mingming Caodab291a2006-10-11 01:21:01 -070014#include <linux/ext4_jbd2.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070015#include <linux/time.h>
16#include <linux/compat.h>
17#include <linux/smp_lock.h>
18#include <asm/uaccess.h>
19
Mingming Cao617ba132006-10-11 01:20:53 -070020int ext4_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070021 unsigned long arg)
22{
Mingming Cao617ba132006-10-11 01:20:53 -070023 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070024 unsigned int flags;
25 unsigned short rsv_window_size;
26
Mingming Cao617ba132006-10-11 01:20:53 -070027 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;
36 int err;
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
41 if (IS_RDONLY(inode))
42 return -EROFS;
43
Satyam Sharma3bd858a2007-07-17 15:00:08 +053044 if (!is_owner_or_cap(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -070045 return -EACCES;
46
47 if (get_user(flags, (int __user *) arg))
48 return -EFAULT;
49
50 if (!S_ISDIR(inode->i_mode))
Mingming Cao617ba132006-10-11 01:20:53 -070051 flags &= ~EXT4_DIRSYNC_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070052
53 mutex_lock(&inode->i_mutex);
54 oldflags = ei->i_flags;
55
56 /* The JOURNAL_DATA flag is modifiable only by root */
Mingming Cao617ba132006-10-11 01:20:53 -070057 jflag = flags & EXT4_JOURNAL_DATA_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070058
59 /*
60 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
61 * the relevant capability.
62 *
63 * This test looks nicer. Thanks to Pauline Middelink
64 */
Mingming Cao617ba132006-10-11 01:20:53 -070065 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -070066 if (!capable(CAP_LINUX_IMMUTABLE)) {
67 mutex_unlock(&inode->i_mutex);
68 return -EPERM;
69 }
70 }
71
72 /*
73 * The JOURNAL_DATA flag can only be changed by
74 * the relevant capability.
75 */
Mingming Cao617ba132006-10-11 01:20:53 -070076 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -070077 if (!capable(CAP_SYS_RESOURCE)) {
78 mutex_unlock(&inode->i_mutex);
79 return -EPERM;
80 }
81 }
82
83
Mingming Cao617ba132006-10-11 01:20:53 -070084 handle = ext4_journal_start(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070085 if (IS_ERR(handle)) {
86 mutex_unlock(&inode->i_mutex);
87 return PTR_ERR(handle);
88 }
89 if (IS_SYNC(inode))
90 handle->h_sync = 1;
Mingming Cao617ba132006-10-11 01:20:53 -070091 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070092 if (err)
93 goto flags_err;
94
Mingming Cao617ba132006-10-11 01:20:53 -070095 flags = flags & EXT4_FL_USER_MODIFIABLE;
96 flags |= oldflags & ~EXT4_FL_USER_MODIFIABLE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070097 ei->i_flags = flags;
98
Mingming Cao617ba132006-10-11 01:20:53 -070099 ext4_set_inode_flags(inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -0400100 inode->i_ctime = ext4_current_time(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700101
Mingming Cao617ba132006-10-11 01:20:53 -0700102 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700103flags_err:
Mingming Cao617ba132006-10-11 01:20:53 -0700104 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700105 if (err) {
106 mutex_unlock(&inode->i_mutex);
107 return err;
108 }
109
Mingming Cao617ba132006-10-11 01:20:53 -0700110 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
111 err = ext4_change_inode_journal_flag(inode, jflag);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700112 mutex_unlock(&inode->i_mutex);
113 return err;
114 }
Mingming Cao617ba132006-10-11 01:20:53 -0700115 case EXT4_IOC_GETVERSION:
116 case EXT4_IOC_GETVERSION_OLD:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700117 return put_user(inode->i_generation, (int __user *) arg);
Mingming Cao617ba132006-10-11 01:20:53 -0700118 case EXT4_IOC_SETVERSION:
119 case EXT4_IOC_SETVERSION_OLD: {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700120 handle_t *handle;
Mingming Cao617ba132006-10-11 01:20:53 -0700121 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700122 __u32 generation;
123 int err;
124
Satyam Sharma3bd858a2007-07-17 15:00:08 +0530125 if (!is_owner_or_cap(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700126 return -EPERM;
127 if (IS_RDONLY(inode))
128 return -EROFS;
129 if (get_user(generation, (int __user *) arg))
130 return -EFAULT;
131
Mingming Cao617ba132006-10-11 01:20:53 -0700132 handle = ext4_journal_start(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700133 if (IS_ERR(handle))
134 return PTR_ERR(handle);
Mingming Cao617ba132006-10-11 01:20:53 -0700135 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700136 if (err == 0) {
Kalpak Shahef7f3832007-07-18 09:15:20 -0400137 inode->i_ctime = ext4_current_time(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700138 inode->i_generation = generation;
Mingming Cao617ba132006-10-11 01:20:53 -0700139 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700140 }
Mingming Cao617ba132006-10-11 01:20:53 -0700141 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700142 return err;
143 }
Jose R. Santose23291b2007-07-18 08:57:06 -0400144#ifdef CONFIG_JBD2_DEBUG
Mingming Cao617ba132006-10-11 01:20:53 -0700145 case EXT4_IOC_WAIT_FOR_READONLY:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700146 /*
147 * This is racy - by the time we're woken up and running,
148 * the superblock could be released. And the module could
149 * have been unloaded. So sue me.
150 *
151 * Returns 1 if it slept, else zero.
152 */
153 {
154 struct super_block *sb = inode->i_sb;
155 DECLARE_WAITQUEUE(wait, current);
156 int ret = 0;
157
158 set_current_state(TASK_INTERRUPTIBLE);
Mingming Cao617ba132006-10-11 01:20:53 -0700159 add_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
160 if (timer_pending(&EXT4_SB(sb)->turn_ro_timer)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700161 schedule();
162 ret = 1;
163 }
Mingming Cao617ba132006-10-11 01:20:53 -0700164 remove_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700165 return ret;
166 }
167#endif
Mingming Cao617ba132006-10-11 01:20:53 -0700168 case EXT4_IOC_GETRSVSZ:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700169 if (test_opt(inode->i_sb, RESERVATION)
170 && S_ISREG(inode->i_mode)
171 && ei->i_block_alloc_info) {
172 rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
173 return put_user(rsv_window_size, (int __user *)arg);
174 }
175 return -ENOTTY;
Mingming Cao617ba132006-10-11 01:20:53 -0700176 case EXT4_IOC_SETRSVSZ: {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700177
178 if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
179 return -ENOTTY;
180
181 if (IS_RDONLY(inode))
182 return -EROFS;
183
Satyam Sharma3bd858a2007-07-17 15:00:08 +0530184 if (!is_owner_or_cap(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700185 return -EACCES;
186
187 if (get_user(rsv_window_size, (int __user *)arg))
188 return -EFAULT;
189
Mingming Cao617ba132006-10-11 01:20:53 -0700190 if (rsv_window_size > EXT4_MAX_RESERVE_BLOCKS)
191 rsv_window_size = EXT4_MAX_RESERVE_BLOCKS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700192
193 /*
194 * need to allocate reservation structure for this inode
195 * before set the window size
196 */
197 mutex_lock(&ei->truncate_mutex);
198 if (!ei->i_block_alloc_info)
Mingming Cao617ba132006-10-11 01:20:53 -0700199 ext4_init_block_alloc_info(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700200
201 if (ei->i_block_alloc_info){
Mingming Cao617ba132006-10-11 01:20:53 -0700202 struct ext4_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700203 rsv->rsv_goal_size = rsv_window_size;
204 }
205 mutex_unlock(&ei->truncate_mutex);
206 return 0;
207 }
Mingming Cao617ba132006-10-11 01:20:53 -0700208 case EXT4_IOC_GROUP_EXTEND: {
209 ext4_fsblk_t n_blocks_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700210 struct super_block *sb = inode->i_sb;
211 int err;
212
213 if (!capable(CAP_SYS_RESOURCE))
214 return -EPERM;
215
216 if (IS_RDONLY(inode))
217 return -EROFS;
218
219 if (get_user(n_blocks_count, (__u32 __user *)arg))
220 return -EFAULT;
221
Mingming Cao617ba132006-10-11 01:20:53 -0700222 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
Mingming Caodab291a2006-10-11 01:21:01 -0700223 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
224 jbd2_journal_flush(EXT4_SB(sb)->s_journal);
225 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700226
227 return err;
228 }
Mingming Cao617ba132006-10-11 01:20:53 -0700229 case EXT4_IOC_GROUP_ADD: {
230 struct ext4_new_group_data input;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700231 struct super_block *sb = inode->i_sb;
232 int err;
233
234 if (!capable(CAP_SYS_RESOURCE))
235 return -EPERM;
236
237 if (IS_RDONLY(inode))
238 return -EROFS;
239
Mingming Cao617ba132006-10-11 01:20:53 -0700240 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700241 sizeof(input)))
242 return -EFAULT;
243
Mingming Cao617ba132006-10-11 01:20:53 -0700244 err = ext4_group_add(sb, &input);
Mingming Caodab291a2006-10-11 01:21:01 -0700245 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
246 jbd2_journal_flush(EXT4_SB(sb)->s_journal);
247 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700248
249 return err;
250 }
251
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700252 default:
253 return -ENOTTY;
254 }
255}
256
257#ifdef CONFIG_COMPAT
Mingming Cao617ba132006-10-11 01:20:53 -0700258long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700259{
Josef "Jeff" Sipek9d549892006-12-08 02:36:38 -0800260 struct inode *inode = file->f_path.dentry->d_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700261 int ret;
262
263 /* These are just misnamed, they actually get/put from/to user an int */
264 switch (cmd) {
Mingming Cao617ba132006-10-11 01:20:53 -0700265 case EXT4_IOC32_GETFLAGS:
266 cmd = EXT4_IOC_GETFLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700267 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700268 case EXT4_IOC32_SETFLAGS:
269 cmd = EXT4_IOC_SETFLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700270 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700271 case EXT4_IOC32_GETVERSION:
272 cmd = EXT4_IOC_GETVERSION;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700273 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700274 case EXT4_IOC32_SETVERSION:
275 cmd = EXT4_IOC_SETVERSION;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700276 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700277 case EXT4_IOC32_GROUP_EXTEND:
278 cmd = EXT4_IOC_GROUP_EXTEND;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700279 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700280 case EXT4_IOC32_GETVERSION_OLD:
281 cmd = EXT4_IOC_GETVERSION_OLD;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700282 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700283 case EXT4_IOC32_SETVERSION_OLD:
284 cmd = EXT4_IOC_SETVERSION_OLD;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700285 break;
Jose R. Santose23291b2007-07-18 08:57:06 -0400286#ifdef CONFIG_JBD2_DEBUG
Mingming Cao617ba132006-10-11 01:20:53 -0700287 case EXT4_IOC32_WAIT_FOR_READONLY:
288 cmd = EXT4_IOC_WAIT_FOR_READONLY;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700289 break;
290#endif
Mingming Cao617ba132006-10-11 01:20:53 -0700291 case EXT4_IOC32_GETRSVSZ:
292 cmd = EXT4_IOC_GETRSVSZ;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700293 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700294 case EXT4_IOC32_SETRSVSZ:
295 cmd = EXT4_IOC_SETRSVSZ;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700296 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700297 case EXT4_IOC_GROUP_ADD:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700298 break;
299 default:
300 return -ENOIOCTLCMD;
301 }
302 lock_kernel();
Mingming Cao617ba132006-10-11 01:20:53 -0700303 ret = ext4_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700304 unlock_kernel();
305 return ret;
306}
307#endif