blob: 3a6b012d120cedae637169ee747ec43b36b25e47 [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>
15#include <linux/time.h>
16#include <asm/uaccess.h>
17
18
19int ext3_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
20 unsigned long arg)
21{
22 struct ext3_inode_info *ei = EXT3_I(inode);
23 unsigned int flags;
24 unsigned short rsv_window_size;
25
26 ext3_debug ("cmd = %u, arg = %lu\n", cmd, arg);
27
28 switch (cmd) {
29 case EXT3_IOC_GETFLAGS:
30 flags = ei->i_flags & EXT3_FL_USER_VISIBLE;
31 return put_user(flags, (int __user *) arg);
32 case EXT3_IOC_SETFLAGS: {
33 handle_t *handle = NULL;
34 int err;
35 struct ext3_iloc iloc;
36 unsigned int oldflags;
37 unsigned int jflag;
38
39 if (IS_RDONLY(inode))
40 return -EROFS;
41
42 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
43 return -EACCES;
44
45 if (get_user(flags, (int __user *) arg))
46 return -EFAULT;
47
48 if (!S_ISDIR(inode->i_mode))
49 flags &= ~EXT3_DIRSYNC_FL;
50
Al Viroa090d912006-04-26 07:32:40 +010051 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 oldflags = ei->i_flags;
53
54 /* The JOURNAL_DATA flag is modifiable only by root */
55 jflag = flags & EXT3_JOURNAL_DATA_FL;
56
57 /*
58 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
59 * the relevant capability.
60 *
61 * This test looks nicer. Thanks to Pauline Middelink
62 */
63 if ((flags ^ oldflags) & (EXT3_APPEND_FL | EXT3_IMMUTABLE_FL)) {
Al Viroa090d912006-04-26 07:32:40 +010064 if (!capable(CAP_LINUX_IMMUTABLE)) {
65 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return -EPERM;
Al Viroa090d912006-04-26 07:32:40 +010067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 }
69
70 /*
71 * The JOURNAL_DATA flag can only be changed by
72 * the relevant capability.
73 */
74 if ((jflag ^ oldflags) & (EXT3_JOURNAL_DATA_FL)) {
Al Viroa090d912006-04-26 07:32:40 +010075 if (!capable(CAP_SYS_RESOURCE)) {
76 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return -EPERM;
Al Viroa090d912006-04-26 07:32:40 +010078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 }
80
81
82 handle = ext3_journal_start(inode, 1);
Al Viroa090d912006-04-26 07:32:40 +010083 if (IS_ERR(handle)) {
84 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return PTR_ERR(handle);
Al Viroa090d912006-04-26 07:32:40 +010086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (IS_SYNC(inode))
88 handle->h_sync = 1;
89 err = ext3_reserve_inode_write(handle, inode, &iloc);
90 if (err)
91 goto flags_err;
92
93 flags = flags & EXT3_FL_USER_MODIFIABLE;
94 flags |= oldflags & ~EXT3_FL_USER_MODIFIABLE;
95 ei->i_flags = flags;
96
97 ext3_set_inode_flags(inode);
98 inode->i_ctime = CURRENT_TIME_SEC;
99
100 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
101flags_err:
102 ext3_journal_stop(handle);
Al Viroa090d912006-04-26 07:32:40 +0100103 if (err) {
104 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return err;
Al Viroa090d912006-04-26 07:32:40 +0100106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 if ((jflag ^ oldflags) & (EXT3_JOURNAL_DATA_FL))
109 err = ext3_change_inode_journal_flag(inode, jflag);
Al Viroa090d912006-04-26 07:32:40 +0100110 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return err;
112 }
113 case EXT3_IOC_GETVERSION:
114 case EXT3_IOC_GETVERSION_OLD:
115 return put_user(inode->i_generation, (int __user *) arg);
116 case EXT3_IOC_SETVERSION:
117 case EXT3_IOC_SETVERSION_OLD: {
118 handle_t *handle;
119 struct ext3_iloc iloc;
120 __u32 generation;
121 int err;
122
123 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
124 return -EPERM;
125 if (IS_RDONLY(inode))
126 return -EROFS;
127 if (get_user(generation, (int __user *) arg))
128 return -EFAULT;
129
130 handle = ext3_journal_start(inode, 1);
131 if (IS_ERR(handle))
132 return PTR_ERR(handle);
133 err = ext3_reserve_inode_write(handle, inode, &iloc);
134 if (err == 0) {
135 inode->i_ctime = CURRENT_TIME_SEC;
136 inode->i_generation = generation;
137 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
138 }
139 ext3_journal_stop(handle);
140 return err;
141 }
142#ifdef CONFIG_JBD_DEBUG
143 case EXT3_IOC_WAIT_FOR_READONLY:
144 /*
145 * This is racy - by the time we're woken up and running,
146 * the superblock could be released. And the module could
147 * have been unloaded. So sue me.
148 *
149 * Returns 1 if it slept, else zero.
150 */
151 {
152 struct super_block *sb = inode->i_sb;
153 DECLARE_WAITQUEUE(wait, current);
154 int ret = 0;
155
156 set_current_state(TASK_INTERRUPTIBLE);
157 add_wait_queue(&EXT3_SB(sb)->ro_wait_queue, &wait);
158 if (timer_pending(&EXT3_SB(sb)->turn_ro_timer)) {
159 schedule();
160 ret = 1;
161 }
162 remove_wait_queue(&EXT3_SB(sb)->ro_wait_queue, &wait);
163 return ret;
164 }
165#endif
166 case EXT3_IOC_GETRSVSZ:
167 if (test_opt(inode->i_sb, RESERVATION)
168 && S_ISREG(inode->i_mode)
169 && ei->i_block_alloc_info) {
170 rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
171 return put_user(rsv_window_size, (int __user *)arg);
172 }
173 return -ENOTTY;
174 case EXT3_IOC_SETRSVSZ: {
175
176 if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
177 return -ENOTTY;
178
179 if (IS_RDONLY(inode))
180 return -EROFS;
181
182 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
183 return -EACCES;
184
185 if (get_user(rsv_window_size, (int __user *)arg))
186 return -EFAULT;
187
188 if (rsv_window_size > EXT3_MAX_RESERVE_BLOCKS)
189 rsv_window_size = EXT3_MAX_RESERVE_BLOCKS;
190
191 /*
192 * need to allocate reservation structure for this inode
193 * before set the window size
194 */
Arjan van de Ven97461512006-03-23 03:00:42 -0800195 mutex_lock(&ei->truncate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (!ei->i_block_alloc_info)
197 ext3_init_block_alloc_info(inode);
198
199 if (ei->i_block_alloc_info){
200 struct ext3_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
201 rsv->rsv_goal_size = rsv_window_size;
202 }
Arjan van de Ven97461512006-03-23 03:00:42 -0800203 mutex_unlock(&ei->truncate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return 0;
205 }
206 case EXT3_IOC_GROUP_EXTEND: {
Mingming Cao43d23f92006-06-25 05:48:07 -0700207 ext3_fsblk_t n_blocks_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 struct super_block *sb = inode->i_sb;
209 int err;
210
211 if (!capable(CAP_SYS_RESOURCE))
212 return -EPERM;
213
214 if (IS_RDONLY(inode))
215 return -EROFS;
216
217 if (get_user(n_blocks_count, (__u32 __user *)arg))
218 return -EFAULT;
219
220 err = ext3_group_extend(sb, EXT3_SB(sb)->s_es, n_blocks_count);
221 journal_lock_updates(EXT3_SB(sb)->s_journal);
222 journal_flush(EXT3_SB(sb)->s_journal);
223 journal_unlock_updates(EXT3_SB(sb)->s_journal);
224
225 return err;
226 }
227 case EXT3_IOC_GROUP_ADD: {
228 struct ext3_new_group_data input;
229 struct super_block *sb = inode->i_sb;
230 int err;
231
232 if (!capable(CAP_SYS_RESOURCE))
233 return -EPERM;
234
235 if (IS_RDONLY(inode))
236 return -EROFS;
237
238 if (copy_from_user(&input, (struct ext3_new_group_input __user *)arg,
239 sizeof(input)))
240 return -EFAULT;
241
242 err = ext3_group_add(sb, &input);
243 journal_lock_updates(EXT3_SB(sb)->s_journal);
244 journal_flush(EXT3_SB(sb)->s_journal);
245 journal_unlock_updates(EXT3_SB(sb)->s_journal);
246
247 return err;
248 }
249
250
251 default:
252 return -ENOTTY;
253 }
254}