blob: 4af574ce4a4638c651006ff8ce73ca61dbe7a14f [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
Serge E. Hallyn2e149672011-03-23 16:43:26 -070041 if (!inode_owner_or_capable(inode))
Cyrus Massoumi039fd8c2009-04-02 16:57:12 -070042 return -EACCES;
43
44 if (get_user(flags, (int __user *) arg))
45 return -EFAULT;
46
Al Viroa561be72011-11-23 11:57:51 -050047 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -080048 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);
Al Viro2a79f172011-12-09 08:06:57 -0500113 mnt_drop_write_file(filp);
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
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700126 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return -EPERM;
Cyrus Massoumi039fd8c2009-04-02 16:57:12 -0700128
Al Viroa561be72011-11-23 11:57:51 -0500129 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800130 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
Djalal Harouni6c2155b2012-01-03 02:31:52 +0100137 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 handle = ext3_journal_start(inode, 1);
Dave Hansen42a74f22008-02-15 14:37:46 -0800139 if (IS_ERR(handle)) {
140 err = PTR_ERR(handle);
Djalal Harouni6c2155b2012-01-03 02:31:52 +0100141 goto unlock_out;
Dave Hansen42a74f22008-02-15 14:37:46 -0800142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 err = ext3_reserve_inode_write(handle, inode, &iloc);
144 if (err == 0) {
145 inode->i_ctime = CURRENT_TIME_SEC;
146 inode->i_generation = generation;
147 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
148 }
149 ext3_journal_stop(handle);
Djalal Harouni6c2155b2012-01-03 02:31:52 +0100150
151unlock_out:
152 mutex_unlock(&inode->i_mutex);
Dave Hansen42a74f22008-02-15 14:37:46 -0800153setversion_out:
Al Viro2a79f172011-12-09 08:06:57 -0500154 mnt_drop_write_file(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return err;
156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 case EXT3_IOC_GETRSVSZ:
158 if (test_opt(inode->i_sb, RESERVATION)
159 && S_ISREG(inode->i_mode)
160 && ei->i_block_alloc_info) {
161 rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
162 return put_user(rsv_window_size, (int __user *)arg);
163 }
164 return -ENOTTY;
165 case EXT3_IOC_SETRSVSZ: {
Dave Hansen42a74f22008-02-15 14:37:46 -0800166 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
169 return -ENOTTY;
170
Al Viroa561be72011-11-23 11:57:51 -0500171 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800172 if (err)
173 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700175 if (!inode_owner_or_capable(inode)) {
Dave Hansen42a74f22008-02-15 14:37:46 -0800176 err = -EACCES;
177 goto setrsvsz_out;
178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Dave Hansen42a74f22008-02-15 14:37:46 -0800180 if (get_user(rsv_window_size, (int __user *)arg)) {
181 err = -EFAULT;
182 goto setrsvsz_out;
183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 if (rsv_window_size > EXT3_MAX_RESERVE_BLOCKS)
186 rsv_window_size = EXT3_MAX_RESERVE_BLOCKS;
187
188 /*
189 * need to allocate reservation structure for this inode
190 * before set the window size
191 */
Arjan van de Ven97461512006-03-23 03:00:42 -0800192 mutex_lock(&ei->truncate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (!ei->i_block_alloc_info)
194 ext3_init_block_alloc_info(inode);
195
196 if (ei->i_block_alloc_info){
197 struct ext3_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
198 rsv->rsv_goal_size = rsv_window_size;
199 }
Arjan van de Ven97461512006-03-23 03:00:42 -0800200 mutex_unlock(&ei->truncate_mutex);
Dave Hansen42a74f22008-02-15 14:37:46 -0800201setrsvsz_out:
Al Viro2a79f172011-12-09 08:06:57 -0500202 mnt_drop_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800203 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205 case EXT3_IOC_GROUP_EXTEND: {
Mingming Cao43d23f92006-06-25 05:48:07 -0700206 ext3_fsblk_t n_blocks_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 struct super_block *sb = inode->i_sb;
Hidehiro Kawai2d7c8202008-10-22 14:15:01 -0700208 int err, err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 if (!capable(CAP_SYS_RESOURCE))
211 return -EPERM;
212
Al Viroa561be72011-11-23 11:57:51 -0500213 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800214 if (err)
215 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Dave Hansen42a74f22008-02-15 14:37:46 -0800217 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
218 err = -EFAULT;
219 goto group_extend_out;
220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 err = ext3_group_extend(sb, EXT3_SB(sb)->s_es, n_blocks_count);
222 journal_lock_updates(EXT3_SB(sb)->s_journal);
Hidehiro Kawai2d7c8202008-10-22 14:15:01 -0700223 err2 = journal_flush(EXT3_SB(sb)->s_journal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 journal_unlock_updates(EXT3_SB(sb)->s_journal);
Hidehiro Kawai2d7c8202008-10-22 14:15:01 -0700225 if (err == 0)
226 err = err2;
Dave Hansen42a74f22008-02-15 14:37:46 -0800227group_extend_out:
Al Viro2a79f172011-12-09 08:06:57 -0500228 mnt_drop_write_file(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return err;
230 }
231 case EXT3_IOC_GROUP_ADD: {
232 struct ext3_new_group_data input;
233 struct super_block *sb = inode->i_sb;
Hidehiro Kawai2d7c8202008-10-22 14:15:01 -0700234 int err, err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 if (!capable(CAP_SYS_RESOURCE))
237 return -EPERM;
238
Al Viroa561be72011-11-23 11:57:51 -0500239 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800240 if (err)
241 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 if (copy_from_user(&input, (struct ext3_new_group_input __user *)arg,
Dave Hansen42a74f22008-02-15 14:37:46 -0800244 sizeof(input))) {
245 err = -EFAULT;
246 goto group_add_out;
247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 err = ext3_group_add(sb, &input);
250 journal_lock_updates(EXT3_SB(sb)->s_journal);
Hidehiro Kawai2d7c8202008-10-22 14:15:01 -0700251 err2 = journal_flush(EXT3_SB(sb)->s_journal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 journal_unlock_updates(EXT3_SB(sb)->s_journal);
Hidehiro Kawai2d7c8202008-10-22 14:15:01 -0700253 if (err == 0)
254 err = err2;
Dave Hansen42a74f22008-02-15 14:37:46 -0800255group_add_out:
Al Viro2a79f172011-12-09 08:06:57 -0500256 mnt_drop_write_file(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return err;
258 }
Lukas Czerner9c527492010-11-22 12:29:18 +0100259 case FITRIM: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Lukas Czerner9c527492010-11-22 12:29:18 +0100261 struct super_block *sb = inode->i_sb;
262 struct fstrim_range range;
263 int ret = 0;
264
265 if (!capable(CAP_SYS_ADMIN))
266 return -EPERM;
267
H Hartley Sweeten81fe8c62011-06-10 14:59:05 -0700268 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
Lukas Czerner9c527492010-11-22 12:29:18 +0100269 sizeof(range)))
270 return -EFAULT;
271
272 ret = ext3_trim_fs(sb, &range);
273 if (ret < 0)
274 return ret;
275
H Hartley Sweeten81fe8c62011-06-10 14:59:05 -0700276 if (copy_to_user((struct fstrim_range __user *)arg, &range,
Lukas Czerner9c527492010-11-22 12:29:18 +0100277 sizeof(range)))
278 return -EFAULT;
279
280 return 0;
281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 default:
284 return -ENOTTY;
285 }
286}
David Howells52a700c2006-08-29 19:06:23 +0100287
288#ifdef CONFIG_COMPAT
289long ext3_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
290{
David Howells52a700c2006-08-29 19:06:23 +0100291 /* These are just misnamed, they actually get/put from/to user an int */
292 switch (cmd) {
293 case EXT3_IOC32_GETFLAGS:
294 cmd = EXT3_IOC_GETFLAGS;
295 break;
296 case EXT3_IOC32_SETFLAGS:
297 cmd = EXT3_IOC_SETFLAGS;
298 break;
299 case EXT3_IOC32_GETVERSION:
300 cmd = EXT3_IOC_GETVERSION;
301 break;
302 case EXT3_IOC32_SETVERSION:
303 cmd = EXT3_IOC_SETVERSION;
304 break;
305 case EXT3_IOC32_GROUP_EXTEND:
306 cmd = EXT3_IOC_GROUP_EXTEND;
307 break;
308 case EXT3_IOC32_GETVERSION_OLD:
309 cmd = EXT3_IOC_GETVERSION_OLD;
310 break;
311 case EXT3_IOC32_SETVERSION_OLD:
312 cmd = EXT3_IOC_SETVERSION_OLD;
313 break;
314#ifdef CONFIG_JBD_DEBUG
315 case EXT3_IOC32_WAIT_FOR_READONLY:
316 cmd = EXT3_IOC_WAIT_FOR_READONLY;
317 break;
318#endif
319 case EXT3_IOC32_GETRSVSZ:
320 cmd = EXT3_IOC_GETRSVSZ;
321 break;
322 case EXT3_IOC32_SETRSVSZ:
323 cmd = EXT3_IOC_SETRSVSZ;
324 break;
325 case EXT3_IOC_GROUP_ADD:
326 break;
327 default:
328 return -ENOIOCTLCMD;
329 }
Cyrus Massoumi039fd8c2009-04-02 16:57:12 -0700330 return ext3_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
David Howells52a700c2006-08-29 19:06:23 +0100331}
332#endif