blob: 677a5c27dc6977b7999e5c87a563f756fbb8ed58 [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
Dave Hansen42a74f22008-02-15 14:37:46 -080010#include <linux/mount.h>
David Howells52a700c2006-08-29 19:06:23 +010011#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <asm/uaccess.h>
Al Viro4613ad12012-03-29 22:30:07 -040013#include "ext3.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Cyrus Massoumi039fd8c2009-04-02 16:57:12 -070015long ext3_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016{
Cyrus Massoumi039fd8c2009-04-02 16:57:12 -070017 struct inode *inode = filp->f_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 struct ext3_inode_info *ei = EXT3_I(inode);
19 unsigned int flags;
20 unsigned short rsv_window_size;
21
22 ext3_debug ("cmd = %u, arg = %lu\n", cmd, arg);
23
24 switch (cmd) {
25 case EXT3_IOC_GETFLAGS:
Jan Kara28be5ab2007-05-08 00:30:33 -070026 ext3_get_inode_flags(ei);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 flags = ei->i_flags & EXT3_FL_USER_VISIBLE;
28 return put_user(flags, (int __user *) arg);
29 case EXT3_IOC_SETFLAGS: {
30 handle_t *handle = NULL;
31 int err;
32 struct ext3_iloc iloc;
33 unsigned int oldflags;
34 unsigned int jflag;
35
Serge E. Hallyn2e149672011-03-23 16:43:26 -070036 if (!inode_owner_or_capable(inode))
Cyrus Massoumi039fd8c2009-04-02 16:57:12 -070037 return -EACCES;
38
39 if (get_user(flags, (int __user *) arg))
40 return -EFAULT;
41
Al Viroa561be72011-11-23 11:57:51 -050042 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -080043 if (err)
44 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Duane Griffin04143e22009-01-07 18:07:26 -080046 flags = ext3_mask_flags(inode->i_mode, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Al Viroa090d912006-04-26 07:32:40 +010048 mutex_lock(&inode->i_mutex);
Cyrus Massoumi039fd8c2009-04-02 16:57:12 -070049
Jan Karae47776a2007-11-14 16:58:56 -080050 /* Is it quota file? Do not allow user to mess with it */
Cyrus Massoumi039fd8c2009-04-02 16:57:12 -070051 err = -EPERM;
52 if (IS_NOQUOTA(inode))
Dave Hansen42a74f22008-02-15 14:37:46 -080053 goto flags_out;
Cyrus Massoumi039fd8c2009-04-02 16:57:12 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 oldflags = ei->i_flags;
56
57 /* The JOURNAL_DATA flag is modifiable only by root */
58 jflag = flags & EXT3_JOURNAL_DATA_FL;
59
60 /*
61 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
62 * the relevant capability.
63 *
64 * This test looks nicer. Thanks to Pauline Middelink
65 */
66 if ((flags ^ oldflags) & (EXT3_APPEND_FL | EXT3_IMMUTABLE_FL)) {
Cyrus Massoumi039fd8c2009-04-02 16:57:12 -070067 if (!capable(CAP_LINUX_IMMUTABLE))
Dave Hansen42a74f22008-02-15 14:37:46 -080068 goto flags_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 }
70
71 /*
72 * The JOURNAL_DATA flag can only be changed by
73 * the relevant capability.
74 */
75 if ((jflag ^ oldflags) & (EXT3_JOURNAL_DATA_FL)) {
Cyrus Massoumi039fd8c2009-04-02 16:57:12 -070076 if (!capable(CAP_SYS_RESOURCE))
Dave Hansen42a74f22008-02-15 14:37:46 -080077 goto flags_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 handle = ext3_journal_start(inode, 1);
Al Viroa090d912006-04-26 07:32:40 +010081 if (IS_ERR(handle)) {
Dave Hansen42a74f22008-02-15 14:37:46 -080082 err = PTR_ERR(handle);
83 goto flags_out;
Al Viroa090d912006-04-26 07:32:40 +010084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 if (IS_SYNC(inode))
86 handle->h_sync = 1;
87 err = ext3_reserve_inode_write(handle, inode, &iloc);
88 if (err)
89 goto flags_err;
90
91 flags = flags & EXT3_FL_USER_MODIFIABLE;
92 flags |= oldflags & ~EXT3_FL_USER_MODIFIABLE;
93 ei->i_flags = flags;
94
95 ext3_set_inode_flags(inode);
96 inode->i_ctime = CURRENT_TIME_SEC;
97
98 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
99flags_err:
100 ext3_journal_stop(handle);
Cyrus Massoumi039fd8c2009-04-02 16:57:12 -0700101 if (err)
102 goto flags_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 if ((jflag ^ oldflags) & (EXT3_JOURNAL_DATA_FL))
105 err = ext3_change_inode_journal_flag(inode, jflag);
Dave Hansen42a74f22008-02-15 14:37:46 -0800106flags_out:
Cyrus Massoumi039fd8c2009-04-02 16:57:12 -0700107 mutex_unlock(&inode->i_mutex);
Al Viro2a79f172011-12-09 08:06:57 -0500108 mnt_drop_write_file(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return err;
110 }
111 case EXT3_IOC_GETVERSION:
112 case EXT3_IOC_GETVERSION_OLD:
113 return put_user(inode->i_generation, (int __user *) arg);
114 case EXT3_IOC_SETVERSION:
115 case EXT3_IOC_SETVERSION_OLD: {
116 handle_t *handle;
117 struct ext3_iloc iloc;
118 __u32 generation;
119 int err;
120
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700121 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return -EPERM;
Cyrus Massoumi039fd8c2009-04-02 16:57:12 -0700123
Al Viroa561be72011-11-23 11:57:51 -0500124 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800125 if (err)
126 return err;
127 if (get_user(generation, (int __user *) arg)) {
128 err = -EFAULT;
129 goto setversion_out;
130 }
Cyrus Massoumi039fd8c2009-04-02 16:57:12 -0700131
Djalal Harouni6c2155b2012-01-03 02:31:52 +0100132 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 handle = ext3_journal_start(inode, 1);
Dave Hansen42a74f22008-02-15 14:37:46 -0800134 if (IS_ERR(handle)) {
135 err = PTR_ERR(handle);
Djalal Harouni6c2155b2012-01-03 02:31:52 +0100136 goto unlock_out;
Dave Hansen42a74f22008-02-15 14:37:46 -0800137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 err = ext3_reserve_inode_write(handle, inode, &iloc);
139 if (err == 0) {
140 inode->i_ctime = CURRENT_TIME_SEC;
141 inode->i_generation = generation;
142 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
143 }
144 ext3_journal_stop(handle);
Djalal Harouni6c2155b2012-01-03 02:31:52 +0100145
146unlock_out:
147 mutex_unlock(&inode->i_mutex);
Dave Hansen42a74f22008-02-15 14:37:46 -0800148setversion_out:
Al Viro2a79f172011-12-09 08:06:57 -0500149 mnt_drop_write_file(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return err;
151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 case EXT3_IOC_GETRSVSZ:
153 if (test_opt(inode->i_sb, RESERVATION)
154 && S_ISREG(inode->i_mode)
155 && ei->i_block_alloc_info) {
156 rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
157 return put_user(rsv_window_size, (int __user *)arg);
158 }
159 return -ENOTTY;
160 case EXT3_IOC_SETRSVSZ: {
Dave Hansen42a74f22008-02-15 14:37:46 -0800161 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
164 return -ENOTTY;
165
Al Viroa561be72011-11-23 11:57:51 -0500166 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800167 if (err)
168 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700170 if (!inode_owner_or_capable(inode)) {
Dave Hansen42a74f22008-02-15 14:37:46 -0800171 err = -EACCES;
172 goto setrsvsz_out;
173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Dave Hansen42a74f22008-02-15 14:37:46 -0800175 if (get_user(rsv_window_size, (int __user *)arg)) {
176 err = -EFAULT;
177 goto setrsvsz_out;
178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 if (rsv_window_size > EXT3_MAX_RESERVE_BLOCKS)
181 rsv_window_size = EXT3_MAX_RESERVE_BLOCKS;
182
183 /*
184 * need to allocate reservation structure for this inode
185 * before set the window size
186 */
Arjan van de Ven97461512006-03-23 03:00:42 -0800187 mutex_lock(&ei->truncate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (!ei->i_block_alloc_info)
189 ext3_init_block_alloc_info(inode);
190
191 if (ei->i_block_alloc_info){
192 struct ext3_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
193 rsv->rsv_goal_size = rsv_window_size;
194 }
Arjan van de Ven97461512006-03-23 03:00:42 -0800195 mutex_unlock(&ei->truncate_mutex);
Dave Hansen42a74f22008-02-15 14:37:46 -0800196setrsvsz_out:
Al Viro2a79f172011-12-09 08:06:57 -0500197 mnt_drop_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800198 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
200 case EXT3_IOC_GROUP_EXTEND: {
Mingming Cao43d23f92006-06-25 05:48:07 -0700201 ext3_fsblk_t n_blocks_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 struct super_block *sb = inode->i_sb;
Hidehiro Kawai2d7c8202008-10-22 14:15:01 -0700203 int err, err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 if (!capable(CAP_SYS_RESOURCE))
206 return -EPERM;
207
Al Viroa561be72011-11-23 11:57:51 -0500208 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800209 if (err)
210 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Dave Hansen42a74f22008-02-15 14:37:46 -0800212 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
213 err = -EFAULT;
214 goto group_extend_out;
215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 err = ext3_group_extend(sb, EXT3_SB(sb)->s_es, n_blocks_count);
217 journal_lock_updates(EXT3_SB(sb)->s_journal);
Hidehiro Kawai2d7c8202008-10-22 14:15:01 -0700218 err2 = journal_flush(EXT3_SB(sb)->s_journal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 journal_unlock_updates(EXT3_SB(sb)->s_journal);
Hidehiro Kawai2d7c8202008-10-22 14:15:01 -0700220 if (err == 0)
221 err = err2;
Dave Hansen42a74f22008-02-15 14:37:46 -0800222group_extend_out:
Al Viro2a79f172011-12-09 08:06:57 -0500223 mnt_drop_write_file(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return err;
225 }
226 case EXT3_IOC_GROUP_ADD: {
227 struct ext3_new_group_data input;
228 struct super_block *sb = inode->i_sb;
Hidehiro Kawai2d7c8202008-10-22 14:15:01 -0700229 int err, err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 if (!capable(CAP_SYS_RESOURCE))
232 return -EPERM;
233
Al Viroa561be72011-11-23 11:57:51 -0500234 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800235 if (err)
236 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 if (copy_from_user(&input, (struct ext3_new_group_input __user *)arg,
Dave Hansen42a74f22008-02-15 14:37:46 -0800239 sizeof(input))) {
240 err = -EFAULT;
241 goto group_add_out;
242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 err = ext3_group_add(sb, &input);
245 journal_lock_updates(EXT3_SB(sb)->s_journal);
Hidehiro Kawai2d7c8202008-10-22 14:15:01 -0700246 err2 = journal_flush(EXT3_SB(sb)->s_journal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 journal_unlock_updates(EXT3_SB(sb)->s_journal);
Hidehiro Kawai2d7c8202008-10-22 14:15:01 -0700248 if (err == 0)
249 err = err2;
Dave Hansen42a74f22008-02-15 14:37:46 -0800250group_add_out:
Al Viro2a79f172011-12-09 08:06:57 -0500251 mnt_drop_write_file(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return err;
253 }
Lukas Czerner9c527492010-11-22 12:29:18 +0100254 case FITRIM: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Lukas Czerner9c527492010-11-22 12:29:18 +0100256 struct super_block *sb = inode->i_sb;
257 struct fstrim_range range;
258 int ret = 0;
259
260 if (!capable(CAP_SYS_ADMIN))
261 return -EPERM;
262
H Hartley Sweeten81fe8c62011-06-10 14:59:05 -0700263 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
Lukas Czerner9c527492010-11-22 12:29:18 +0100264 sizeof(range)))
265 return -EFAULT;
266
267 ret = ext3_trim_fs(sb, &range);
268 if (ret < 0)
269 return ret;
270
H Hartley Sweeten81fe8c62011-06-10 14:59:05 -0700271 if (copy_to_user((struct fstrim_range __user *)arg, &range,
Lukas Czerner9c527492010-11-22 12:29:18 +0100272 sizeof(range)))
273 return -EFAULT;
274
275 return 0;
276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 default:
279 return -ENOTTY;
280 }
281}
David Howells52a700c2006-08-29 19:06:23 +0100282
283#ifdef CONFIG_COMPAT
284long ext3_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
285{
David Howells52a700c2006-08-29 19:06:23 +0100286 /* These are just misnamed, they actually get/put from/to user an int */
287 switch (cmd) {
288 case EXT3_IOC32_GETFLAGS:
289 cmd = EXT3_IOC_GETFLAGS;
290 break;
291 case EXT3_IOC32_SETFLAGS:
292 cmd = EXT3_IOC_SETFLAGS;
293 break;
294 case EXT3_IOC32_GETVERSION:
295 cmd = EXT3_IOC_GETVERSION;
296 break;
297 case EXT3_IOC32_SETVERSION:
298 cmd = EXT3_IOC_SETVERSION;
299 break;
300 case EXT3_IOC32_GROUP_EXTEND:
301 cmd = EXT3_IOC_GROUP_EXTEND;
302 break;
303 case EXT3_IOC32_GETVERSION_OLD:
304 cmd = EXT3_IOC_GETVERSION_OLD;
305 break;
306 case EXT3_IOC32_SETVERSION_OLD:
307 cmd = EXT3_IOC_SETVERSION_OLD;
308 break;
309#ifdef CONFIG_JBD_DEBUG
310 case EXT3_IOC32_WAIT_FOR_READONLY:
311 cmd = EXT3_IOC_WAIT_FOR_READONLY;
312 break;
313#endif
314 case EXT3_IOC32_GETRSVSZ:
315 cmd = EXT3_IOC_GETRSVSZ;
316 break;
317 case EXT3_IOC32_SETRSVSZ:
318 cmd = EXT3_IOC_SETRSVSZ;
319 break;
320 case EXT3_IOC_GROUP_ADD:
321 break;
322 default:
323 return -ENOIOCTLCMD;
324 }
Cyrus Massoumi039fd8c2009-04-02 16:57:12 -0700325 return ext3_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
David Howells52a700c2006-08-29 19:06:23 +0100326}
327#endif