blob: e7f894bdb4202359974088ba5c1f14585a43fc03 [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);
Jan Karae47776a2007-11-14 16:58:56 -080054 /* Is it quota file? Do not allow user to mess with it */
55 if (IS_NOQUOTA(inode)) {
56 mutex_unlock(&inode->i_mutex);
57 return -EPERM;
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 Kleikampac27a0e2006-10-11 01:20:50 -070071 if (!capable(CAP_LINUX_IMMUTABLE)) {
72 mutex_unlock(&inode->i_mutex);
73 return -EPERM;
74 }
75 }
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 Kleikampac27a0e2006-10-11 01:20:50 -070082 if (!capable(CAP_SYS_RESOURCE)) {
83 mutex_unlock(&inode->i_mutex);
84 return -EPERM;
85 }
86 }
87
88
Mingming Cao617ba132006-10-11 01:20:53 -070089 handle = ext4_journal_start(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070090 if (IS_ERR(handle)) {
91 mutex_unlock(&inode->i_mutex);
92 return PTR_ERR(handle);
93 }
94 if (IS_SYNC(inode))
95 handle->h_sync = 1;
Mingming Cao617ba132006-10-11 01:20:53 -070096 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070097 if (err)
98 goto flags_err;
99
Mingming Cao617ba132006-10-11 01:20:53 -0700100 flags = flags & EXT4_FL_USER_MODIFIABLE;
101 flags |= oldflags & ~EXT4_FL_USER_MODIFIABLE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700102 ei->i_flags = flags;
103
Mingming Cao617ba132006-10-11 01:20:53 -0700104 ext4_set_inode_flags(inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -0400105 inode->i_ctime = ext4_current_time(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700106
Mingming Cao617ba132006-10-11 01:20:53 -0700107 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700108flags_err:
Mingming Cao617ba132006-10-11 01:20:53 -0700109 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700110 if (err) {
111 mutex_unlock(&inode->i_mutex);
112 return err;
113 }
114
Mingming Cao617ba132006-10-11 01:20:53 -0700115 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
116 err = ext4_change_inode_journal_flag(inode, jflag);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700117 mutex_unlock(&inode->i_mutex);
118 return err;
119 }
Mingming Cao617ba132006-10-11 01:20:53 -0700120 case EXT4_IOC_GETVERSION:
121 case EXT4_IOC_GETVERSION_OLD:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700122 return put_user(inode->i_generation, (int __user *) arg);
Mingming Cao617ba132006-10-11 01:20:53 -0700123 case EXT4_IOC_SETVERSION:
124 case EXT4_IOC_SETVERSION_OLD: {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700125 handle_t *handle;
Mingming Cao617ba132006-10-11 01:20:53 -0700126 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700127 __u32 generation;
128 int err;
129
Satyam Sharma3bd858a2007-07-17 15:00:08 +0530130 if (!is_owner_or_cap(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700131 return -EPERM;
132 if (IS_RDONLY(inode))
133 return -EROFS;
134 if (get_user(generation, (int __user *) arg))
135 return -EFAULT;
136
Mingming Cao617ba132006-10-11 01:20:53 -0700137 handle = ext4_journal_start(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700138 if (IS_ERR(handle))
139 return PTR_ERR(handle);
Mingming Cao617ba132006-10-11 01:20:53 -0700140 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700141 if (err == 0) {
Kalpak Shahef7f3832007-07-18 09:15:20 -0400142 inode->i_ctime = ext4_current_time(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700143 inode->i_generation = generation;
Mingming Cao617ba132006-10-11 01:20:53 -0700144 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700145 }
Mingming Cao617ba132006-10-11 01:20:53 -0700146 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700147 return err;
148 }
Jose R. Santose23291b2007-07-18 08:57:06 -0400149#ifdef CONFIG_JBD2_DEBUG
Mingming Cao617ba132006-10-11 01:20:53 -0700150 case EXT4_IOC_WAIT_FOR_READONLY:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700151 /*
152 * This is racy - by the time we're woken up and running,
153 * the superblock could be released. And the module could
154 * have been unloaded. So sue me.
155 *
156 * Returns 1 if it slept, else zero.
157 */
158 {
159 struct super_block *sb = inode->i_sb;
160 DECLARE_WAITQUEUE(wait, current);
161 int ret = 0;
162
163 set_current_state(TASK_INTERRUPTIBLE);
Mingming Cao617ba132006-10-11 01:20:53 -0700164 add_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
165 if (timer_pending(&EXT4_SB(sb)->turn_ro_timer)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700166 schedule();
167 ret = 1;
168 }
Mingming Cao617ba132006-10-11 01:20:53 -0700169 remove_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700170 return ret;
171 }
172#endif
Mingming Cao617ba132006-10-11 01:20:53 -0700173 case EXT4_IOC_GETRSVSZ:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700174 if (test_opt(inode->i_sb, RESERVATION)
175 && S_ISREG(inode->i_mode)
176 && ei->i_block_alloc_info) {
177 rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
178 return put_user(rsv_window_size, (int __user *)arg);
179 }
180 return -ENOTTY;
Mingming Cao617ba132006-10-11 01:20:53 -0700181 case EXT4_IOC_SETRSVSZ: {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700182
183 if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
184 return -ENOTTY;
185
186 if (IS_RDONLY(inode))
187 return -EROFS;
188
Satyam Sharma3bd858a2007-07-17 15:00:08 +0530189 if (!is_owner_or_cap(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700190 return -EACCES;
191
192 if (get_user(rsv_window_size, (int __user *)arg))
193 return -EFAULT;
194
Mingming Cao617ba132006-10-11 01:20:53 -0700195 if (rsv_window_size > EXT4_MAX_RESERVE_BLOCKS)
196 rsv_window_size = EXT4_MAX_RESERVE_BLOCKS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700197
198 /*
199 * need to allocate reservation structure for this inode
200 * before set the window size
201 */
202 mutex_lock(&ei->truncate_mutex);
203 if (!ei->i_block_alloc_info)
Mingming Cao617ba132006-10-11 01:20:53 -0700204 ext4_init_block_alloc_info(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700205
206 if (ei->i_block_alloc_info){
Mingming Cao617ba132006-10-11 01:20:53 -0700207 struct ext4_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700208 rsv->rsv_goal_size = rsv_window_size;
209 }
210 mutex_unlock(&ei->truncate_mutex);
211 return 0;
212 }
Mingming Cao617ba132006-10-11 01:20:53 -0700213 case EXT4_IOC_GROUP_EXTEND: {
214 ext4_fsblk_t n_blocks_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700215 struct super_block *sb = inode->i_sb;
216 int err;
217
218 if (!capable(CAP_SYS_RESOURCE))
219 return -EPERM;
220
221 if (IS_RDONLY(inode))
222 return -EROFS;
223
224 if (get_user(n_blocks_count, (__u32 __user *)arg))
225 return -EFAULT;
226
Mingming Cao617ba132006-10-11 01:20:53 -0700227 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
Mingming Caodab291a2006-10-11 01:21:01 -0700228 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
229 jbd2_journal_flush(EXT4_SB(sb)->s_journal);
230 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700231
232 return err;
233 }
Mingming Cao617ba132006-10-11 01:20:53 -0700234 case EXT4_IOC_GROUP_ADD: {
235 struct ext4_new_group_data input;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700236 struct super_block *sb = inode->i_sb;
237 int err;
238
239 if (!capable(CAP_SYS_RESOURCE))
240 return -EPERM;
241
242 if (IS_RDONLY(inode))
243 return -EROFS;
244
Mingming Cao617ba132006-10-11 01:20:53 -0700245 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700246 sizeof(input)))
247 return -EFAULT;
248
Mingming Cao617ba132006-10-11 01:20:53 -0700249 err = ext4_group_add(sb, &input);
Mingming Caodab291a2006-10-11 01:21:01 -0700250 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
251 jbd2_journal_flush(EXT4_SB(sb)->s_journal);
252 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700253
254 return err;
255 }
256
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700257 default:
258 return -ENOTTY;
259 }
260}
261
262#ifdef CONFIG_COMPAT
Mingming Cao617ba132006-10-11 01:20:53 -0700263long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700264{
Josef "Jeff" Sipek9d549892006-12-08 02:36:38 -0800265 struct inode *inode = file->f_path.dentry->d_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700266 int ret;
267
268 /* These are just misnamed, they actually get/put from/to user an int */
269 switch (cmd) {
Mingming Cao617ba132006-10-11 01:20:53 -0700270 case EXT4_IOC32_GETFLAGS:
271 cmd = EXT4_IOC_GETFLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700272 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700273 case EXT4_IOC32_SETFLAGS:
274 cmd = EXT4_IOC_SETFLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700275 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700276 case EXT4_IOC32_GETVERSION:
277 cmd = EXT4_IOC_GETVERSION;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700278 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700279 case EXT4_IOC32_SETVERSION:
280 cmd = EXT4_IOC_SETVERSION;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700281 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700282 case EXT4_IOC32_GROUP_EXTEND:
283 cmd = EXT4_IOC_GROUP_EXTEND;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700284 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700285 case EXT4_IOC32_GETVERSION_OLD:
286 cmd = EXT4_IOC_GETVERSION_OLD;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700287 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700288 case EXT4_IOC32_SETVERSION_OLD:
289 cmd = EXT4_IOC_SETVERSION_OLD;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700290 break;
Jose R. Santose23291b2007-07-18 08:57:06 -0400291#ifdef CONFIG_JBD2_DEBUG
Mingming Cao617ba132006-10-11 01:20:53 -0700292 case EXT4_IOC32_WAIT_FOR_READONLY:
293 cmd = EXT4_IOC_WAIT_FOR_READONLY;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700294 break;
295#endif
Mingming Cao617ba132006-10-11 01:20:53 -0700296 case EXT4_IOC32_GETRSVSZ:
297 cmd = EXT4_IOC_GETRSVSZ;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700298 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700299 case EXT4_IOC32_SETRSVSZ:
300 cmd = EXT4_IOC_SETRSVSZ;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700301 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700302 case EXT4_IOC_GROUP_ADD:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700303 break;
304 default:
305 return -ENOIOCTLCMD;
306 }
307 lock_kernel();
Mingming Cao617ba132006-10-11 01:20:53 -0700308 ret = ext4_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700309 unlock_kernel();
310 return ret;
311}
312#endif