blob: 88974814783a346ca18d9eddea1c417cccbf695b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/ext3/ioctl.c
3 *
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>
11#include <linux/jbd.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080012#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/ext3_fs.h>
14#include <linux/ext3_jbd.h>
Dave Hansen42a74f22008-02-15 14:37:46 -080015#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/time.h>
David Howells52a700c2006-08-29 19:06:23 +010017#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/uaccess.h>
19
Cyrus Massoumi039fd8c2009-04-02 16:57:12 -070020long ext3_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
Cyrus Massoumi039fd8c2009-04-02 16:57:12 -070022 struct inode *inode = filp->f_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 struct ext3_inode_info *ei = EXT3_I(inode);
24 unsigned int flags;
25 unsigned short rsv_window_size;
26
27 ext3_debug ("cmd = %u, arg = %lu\n", cmd, arg);
28
29 switch (cmd) {
30 case EXT3_IOC_GETFLAGS:
Jan Kara28be5ab2007-05-08 00:30:33 -070031 ext3_get_inode_flags(ei);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 flags = ei->i_flags & EXT3_FL_USER_VISIBLE;
33 return put_user(flags, (int __user *) arg);
34 case EXT3_IOC_SETFLAGS: {
35 handle_t *handle = NULL;
36 int err;
37 struct ext3_iloc iloc;
38 unsigned int oldflags;
39 unsigned int jflag;
40
Cyrus Massoumi039fd8c2009-04-02 16:57:12 -070041 if (!is_owner_or_cap(inode))
42 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Duane Griffin04143e22009-01-07 18:07:26 -080051 flags = ext3_mask_flags(inode->i_mode, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Al Viroa090d912006-04-26 07:32:40 +010053 mutex_lock(&inode->i_mutex);
Cyrus Massoumi039fd8c2009-04-02 16:57:12 -070054
Jan Karae47776a2007-11-14 16:58:56 -080055 /* Is it quota file? Do not allow user to mess with it */
Cyrus Massoumi039fd8c2009-04-02 16:57:12 -070056 err = -EPERM;
57 if (IS_NOQUOTA(inode))
Dave Hansen42a74f22008-02-15 14:37:46 -080058 goto flags_out;
Cyrus Massoumi039fd8c2009-04-02 16:57:12 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 oldflags = ei->i_flags;
61
62 /* The JOURNAL_DATA flag is modifiable only by root */
63 jflag = flags & EXT3_JOURNAL_DATA_FL;
64
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 */
71 if ((flags ^ oldflags) & (EXT3_APPEND_FL | EXT3_IMMUTABLE_FL)) {
Cyrus Massoumi039fd8c2009-04-02 16:57:12 -070072 if (!capable(CAP_LINUX_IMMUTABLE))
Dave Hansen42a74f22008-02-15 14:37:46 -080073 goto flags_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
75
76 /*
77 * The JOURNAL_DATA flag can only be changed by
78 * the relevant capability.
79 */
80 if ((jflag ^ oldflags) & (EXT3_JOURNAL_DATA_FL)) {
Cyrus Massoumi039fd8c2009-04-02 16:57:12 -070081 if (!capable(CAP_SYS_RESOURCE))
Dave Hansen42a74f22008-02-15 14:37:46 -080082 goto flags_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 handle = ext3_journal_start(inode, 1);
Al Viroa090d912006-04-26 07:32:40 +010086 if (IS_ERR(handle)) {
Dave Hansen42a74f22008-02-15 14:37:46 -080087 err = PTR_ERR(handle);
88 goto flags_out;
Al Viroa090d912006-04-26 07:32:40 +010089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 if (IS_SYNC(inode))
91 handle->h_sync = 1;
92 err = ext3_reserve_inode_write(handle, inode, &iloc);
93 if (err)
94 goto flags_err;
95
96 flags = flags & EXT3_FL_USER_MODIFIABLE;
97 flags |= oldflags & ~EXT3_FL_USER_MODIFIABLE;
98 ei->i_flags = flags;
99
100 ext3_set_inode_flags(inode);
101 inode->i_ctime = CURRENT_TIME_SEC;
102
103 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
104flags_err:
105 ext3_journal_stop(handle);
Cyrus Massoumi039fd8c2009-04-02 16:57:12 -0700106 if (err)
107 goto flags_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 if ((jflag ^ oldflags) & (EXT3_JOURNAL_DATA_FL))
110 err = ext3_change_inode_journal_flag(inode, jflag);
Dave Hansen42a74f22008-02-15 14:37:46 -0800111flags_out:
Cyrus Massoumi039fd8c2009-04-02 16:57:12 -0700112 mutex_unlock(&inode->i_mutex);
Dave Hansen42a74f22008-02-15 14:37:46 -0800113 mnt_drop_write(filp->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return err;
115 }
116 case EXT3_IOC_GETVERSION:
117 case EXT3_IOC_GETVERSION_OLD:
118 return put_user(inode->i_generation, (int __user *) arg);
119 case EXT3_IOC_SETVERSION:
120 case EXT3_IOC_SETVERSION_OLD: {
121 handle_t *handle;
122 struct ext3_iloc iloc;
123 __u32 generation;
124 int err;
125
Satyam Sharma3bd858a2007-07-17 15:00:08 +0530126 if (!is_owner_or_cap(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return -EPERM;
Cyrus Massoumi039fd8c2009-04-02 16:57:12 -0700128
Dave Hansen42a74f22008-02-15 14:37:46 -0800129 err = mnt_want_write(filp->f_path.mnt);
130 if (err)
131 return err;
132 if (get_user(generation, (int __user *) arg)) {
133 err = -EFAULT;
134 goto setversion_out;
135 }
Cyrus Massoumi039fd8c2009-04-02 16:57:12 -0700136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 handle = ext3_journal_start(inode, 1);
Dave Hansen42a74f22008-02-15 14:37:46 -0800138 if (IS_ERR(handle)) {
139 err = PTR_ERR(handle);
140 goto setversion_out;
141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 err = ext3_reserve_inode_write(handle, inode, &iloc);
143 if (err == 0) {
144 inode->i_ctime = CURRENT_TIME_SEC;
145 inode->i_generation = generation;
146 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
147 }
148 ext3_journal_stop(handle);
Dave Hansen42a74f22008-02-15 14:37:46 -0800149setversion_out:
150 mnt_drop_write(filp->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return err;
152 }
153#ifdef CONFIG_JBD_DEBUG
154 case EXT3_IOC_WAIT_FOR_READONLY:
155 /*
156 * This is racy - by the time we're woken up and running,
157 * the superblock could be released. And the module could
158 * have been unloaded. So sue me.
159 *
160 * Returns 1 if it slept, else zero.
161 */
162 {
163 struct super_block *sb = inode->i_sb;
164 DECLARE_WAITQUEUE(wait, current);
165 int ret = 0;
166
167 set_current_state(TASK_INTERRUPTIBLE);
168 add_wait_queue(&EXT3_SB(sb)->ro_wait_queue, &wait);
169 if (timer_pending(&EXT3_SB(sb)->turn_ro_timer)) {
170 schedule();
171 ret = 1;
172 }
173 remove_wait_queue(&EXT3_SB(sb)->ro_wait_queue, &wait);
174 return ret;
175 }
176#endif
177 case EXT3_IOC_GETRSVSZ:
178 if (test_opt(inode->i_sb, RESERVATION)
179 && S_ISREG(inode->i_mode)
180 && ei->i_block_alloc_info) {
181 rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
182 return put_user(rsv_window_size, (int __user *)arg);
183 }
184 return -ENOTTY;
185 case EXT3_IOC_SETRSVSZ: {
Dave Hansen42a74f22008-02-15 14:37:46 -0800186 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
189 return -ENOTTY;
190
Dave Hansen42a74f22008-02-15 14:37:46 -0800191 err = mnt_want_write(filp->f_path.mnt);
192 if (err)
193 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Dave Hansen42a74f22008-02-15 14:37:46 -0800195 if (!is_owner_or_cap(inode)) {
196 err = -EACCES;
197 goto setrsvsz_out;
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Dave Hansen42a74f22008-02-15 14:37:46 -0800200 if (get_user(rsv_window_size, (int __user *)arg)) {
201 err = -EFAULT;
202 goto setrsvsz_out;
203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 if (rsv_window_size > EXT3_MAX_RESERVE_BLOCKS)
206 rsv_window_size = EXT3_MAX_RESERVE_BLOCKS;
207
208 /*
209 * need to allocate reservation structure for this inode
210 * before set the window size
211 */
Arjan van de Ven97461512006-03-23 03:00:42 -0800212 mutex_lock(&ei->truncate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (!ei->i_block_alloc_info)
214 ext3_init_block_alloc_info(inode);
215
216 if (ei->i_block_alloc_info){
217 struct ext3_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
218 rsv->rsv_goal_size = rsv_window_size;
219 }
Arjan van de Ven97461512006-03-23 03:00:42 -0800220 mutex_unlock(&ei->truncate_mutex);
Dave Hansen42a74f22008-02-15 14:37:46 -0800221setrsvsz_out:
222 mnt_drop_write(filp->f_path.mnt);
223 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225 case EXT3_IOC_GROUP_EXTEND: {
Mingming Cao43d23f92006-06-25 05:48:07 -0700226 ext3_fsblk_t n_blocks_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 struct super_block *sb = inode->i_sb;
Hidehiro Kawai2d7c8202008-10-22 14:15:01 -0700228 int err, err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 if (!capable(CAP_SYS_RESOURCE))
231 return -EPERM;
232
Dave Hansen42a74f22008-02-15 14:37:46 -0800233 err = mnt_want_write(filp->f_path.mnt);
234 if (err)
235 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Dave Hansen42a74f22008-02-15 14:37:46 -0800237 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
238 err = -EFAULT;
239 goto group_extend_out;
240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 err = ext3_group_extend(sb, EXT3_SB(sb)->s_es, n_blocks_count);
242 journal_lock_updates(EXT3_SB(sb)->s_journal);
Hidehiro Kawai2d7c8202008-10-22 14:15:01 -0700243 err2 = journal_flush(EXT3_SB(sb)->s_journal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 journal_unlock_updates(EXT3_SB(sb)->s_journal);
Hidehiro Kawai2d7c8202008-10-22 14:15:01 -0700245 if (err == 0)
246 err = err2;
Dave Hansen42a74f22008-02-15 14:37:46 -0800247group_extend_out:
248 mnt_drop_write(filp->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return err;
250 }
251 case EXT3_IOC_GROUP_ADD: {
252 struct ext3_new_group_data input;
253 struct super_block *sb = inode->i_sb;
Hidehiro Kawai2d7c8202008-10-22 14:15:01 -0700254 int err, err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 if (!capable(CAP_SYS_RESOURCE))
257 return -EPERM;
258
Dave Hansen42a74f22008-02-15 14:37:46 -0800259 err = mnt_want_write(filp->f_path.mnt);
260 if (err)
261 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 if (copy_from_user(&input, (struct ext3_new_group_input __user *)arg,
Dave Hansen42a74f22008-02-15 14:37:46 -0800264 sizeof(input))) {
265 err = -EFAULT;
266 goto group_add_out;
267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 err = ext3_group_add(sb, &input);
270 journal_lock_updates(EXT3_SB(sb)->s_journal);
Hidehiro Kawai2d7c8202008-10-22 14:15:01 -0700271 err2 = journal_flush(EXT3_SB(sb)->s_journal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 journal_unlock_updates(EXT3_SB(sb)->s_journal);
Hidehiro Kawai2d7c8202008-10-22 14:15:01 -0700273 if (err == 0)
274 err = err2;
Dave Hansen42a74f22008-02-15 14:37:46 -0800275group_add_out:
276 mnt_drop_write(filp->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return err;
278 }
279
280
281 default:
282 return -ENOTTY;
283 }
284}
David Howells52a700c2006-08-29 19:06:23 +0100285
286#ifdef CONFIG_COMPAT
287long ext3_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
288{
David Howells52a700c2006-08-29 19:06:23 +0100289 /* These are just misnamed, they actually get/put from/to user an int */
290 switch (cmd) {
291 case EXT3_IOC32_GETFLAGS:
292 cmd = EXT3_IOC_GETFLAGS;
293 break;
294 case EXT3_IOC32_SETFLAGS:
295 cmd = EXT3_IOC_SETFLAGS;
296 break;
297 case EXT3_IOC32_GETVERSION:
298 cmd = EXT3_IOC_GETVERSION;
299 break;
300 case EXT3_IOC32_SETVERSION:
301 cmd = EXT3_IOC_SETVERSION;
302 break;
303 case EXT3_IOC32_GROUP_EXTEND:
304 cmd = EXT3_IOC_GROUP_EXTEND;
305 break;
306 case EXT3_IOC32_GETVERSION_OLD:
307 cmd = EXT3_IOC_GETVERSION_OLD;
308 break;
309 case EXT3_IOC32_SETVERSION_OLD:
310 cmd = EXT3_IOC_SETVERSION_OLD;
311 break;
312#ifdef CONFIG_JBD_DEBUG
313 case EXT3_IOC32_WAIT_FOR_READONLY:
314 cmd = EXT3_IOC_WAIT_FOR_READONLY;
315 break;
316#endif
317 case EXT3_IOC32_GETRSVSZ:
318 cmd = EXT3_IOC_GETRSVSZ;
319 break;
320 case EXT3_IOC32_SETRSVSZ:
321 cmd = EXT3_IOC_SETRSVSZ;
322 break;
323 case EXT3_IOC_GROUP_ADD:
324 break;
325 default:
326 return -ENOIOCTLCMD;
327 }
Cyrus Massoumi039fd8c2009-04-02 16:57:12 -0700328 return ext3_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
David Howells52a700c2006-08-29 19:06:23 +0100329}
330#endif