Commit | Line | Data |
---|---|---|
ac27a0ec | 1 | /* |
617ba13b | 2 | * linux/fs/ext4/ioctl.c |
ac27a0ec DK |
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> | |
dab291af | 11 | #include <linux/jbd2.h> |
ac27a0ec | 12 | #include <linux/capability.h> |
617ba13b | 13 | #include <linux/ext4_fs.h> |
dab291af | 14 | #include <linux/ext4_jbd2.h> |
ac27a0ec DK |
15 | #include <linux/time.h> |
16 | #include <linux/compat.h> | |
17 | #include <linux/smp_lock.h> | |
42a74f20 | 18 | #include <linux/mount.h> |
ac27a0ec DK |
19 | #include <asm/uaccess.h> |
20 | ||
617ba13b | 21 | int ext4_ioctl (struct inode * inode, struct file * filp, unsigned int cmd, |
ac27a0ec DK |
22 | unsigned long arg) |
23 | { | |
617ba13b | 24 | struct ext4_inode_info *ei = EXT4_I(inode); |
ac27a0ec DK |
25 | unsigned int flags; |
26 | unsigned short rsv_window_size; | |
27 | ||
617ba13b | 28 | ext4_debug ("cmd = %u, arg = %lu\n", cmd, arg); |
ac27a0ec DK |
29 | |
30 | switch (cmd) { | |
617ba13b | 31 | case EXT4_IOC_GETFLAGS: |
ff9ddf7e | 32 | ext4_get_inode_flags(ei); |
617ba13b | 33 | flags = ei->i_flags & EXT4_FL_USER_VISIBLE; |
ac27a0ec | 34 | return put_user(flags, (int __user *) arg); |
617ba13b | 35 | case EXT4_IOC_SETFLAGS: { |
ac27a0ec DK |
36 | handle_t *handle = NULL; |
37 | int err; | |
617ba13b | 38 | struct ext4_iloc iloc; |
ac27a0ec DK |
39 | unsigned int oldflags; |
40 | unsigned int jflag; | |
41 | ||
3bd858ab | 42 | if (!is_owner_or_cap(inode)) |
ac27a0ec DK |
43 | return -EACCES; |
44 | ||
45 | if (get_user(flags, (int __user *) arg)) | |
46 | return -EFAULT; | |
47 | ||
42a74f20 DH |
48 | err = mnt_want_write(filp->f_path.mnt); |
49 | if (err) | |
50 | return err; | |
51 | ||
ac27a0ec | 52 | if (!S_ISDIR(inode->i_mode)) |
617ba13b | 53 | flags &= ~EXT4_DIRSYNC_FL; |
ac27a0ec | 54 | |
42a74f20 | 55 | err = -EPERM; |
ac27a0ec | 56 | mutex_lock(&inode->i_mutex); |
e47776a0 | 57 | /* Is it quota file? Do not allow user to mess with it */ |
42a74f20 DH |
58 | if (IS_NOQUOTA(inode)) |
59 | goto flags_out; | |
60 | ||
ac27a0ec DK |
61 | oldflags = ei->i_flags; |
62 | ||
63 | /* The JOURNAL_DATA flag is modifiable only by root */ | |
617ba13b | 64 | jflag = flags & EXT4_JOURNAL_DATA_FL; |
ac27a0ec DK |
65 | |
66 | /* | |
67 | * The IMMUTABLE and APPEND_ONLY flags can only be changed by | |
68 | * the relevant capability. | |
69 | * | |
70 | * This test looks nicer. Thanks to Pauline Middelink | |
71 | */ | |
617ba13b | 72 | if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) { |
42a74f20 DH |
73 | if (!capable(CAP_LINUX_IMMUTABLE)) |
74 | goto flags_out; | |
ac27a0ec DK |
75 | } |
76 | ||
77 | /* | |
78 | * The JOURNAL_DATA flag can only be changed by | |
79 | * the relevant capability. | |
80 | */ | |
617ba13b | 81 | if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) { |
42a74f20 DH |
82 | if (!capable(CAP_SYS_RESOURCE)) |
83 | goto flags_out; | |
ac27a0ec DK |
84 | } |
85 | ||
617ba13b | 86 | handle = ext4_journal_start(inode, 1); |
ac27a0ec | 87 | if (IS_ERR(handle)) { |
42a74f20 DH |
88 | err = PTR_ERR(handle); |
89 | goto flags_out; | |
ac27a0ec DK |
90 | } |
91 | if (IS_SYNC(inode)) | |
92 | handle->h_sync = 1; | |
617ba13b | 93 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
ac27a0ec DK |
94 | if (err) |
95 | goto flags_err; | |
96 | ||
617ba13b MC |
97 | flags = flags & EXT4_FL_USER_MODIFIABLE; |
98 | flags |= oldflags & ~EXT4_FL_USER_MODIFIABLE; | |
ac27a0ec DK |
99 | ei->i_flags = flags; |
100 | ||
617ba13b | 101 | ext4_set_inode_flags(inode); |
ef7f3835 | 102 | inode->i_ctime = ext4_current_time(inode); |
ac27a0ec | 103 | |
617ba13b | 104 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); |
ac27a0ec | 105 | flags_err: |
617ba13b | 106 | ext4_journal_stop(handle); |
42a74f20 DH |
107 | if (err) |
108 | goto flags_out; | |
ac27a0ec | 109 | |
617ba13b MC |
110 | if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) |
111 | err = ext4_change_inode_journal_flag(inode, jflag); | |
42a74f20 | 112 | flags_out: |
ac27a0ec | 113 | mutex_unlock(&inode->i_mutex); |
42a74f20 | 114 | mnt_drop_write(filp->f_path.mnt); |
ac27a0ec DK |
115 | return err; |
116 | } | |
617ba13b MC |
117 | case EXT4_IOC_GETVERSION: |
118 | case EXT4_IOC_GETVERSION_OLD: | |
ac27a0ec | 119 | return put_user(inode->i_generation, (int __user *) arg); |
617ba13b MC |
120 | case EXT4_IOC_SETVERSION: |
121 | case EXT4_IOC_SETVERSION_OLD: { | |
ac27a0ec | 122 | handle_t *handle; |
617ba13b | 123 | struct ext4_iloc iloc; |
ac27a0ec DK |
124 | __u32 generation; |
125 | int err; | |
126 | ||
3bd858ab | 127 | if (!is_owner_or_cap(inode)) |
ac27a0ec | 128 | return -EPERM; |
42a74f20 DH |
129 | |
130 | err = mnt_want_write(filp->f_path.mnt); | |
131 | if (err) | |
132 | return err; | |
133 | if (get_user(generation, (int __user *) arg)) { | |
134 | err = -EFAULT; | |
135 | goto setversion_out; | |
136 | } | |
ac27a0ec | 137 | |
617ba13b | 138 | handle = ext4_journal_start(inode, 1); |
42a74f20 DH |
139 | if (IS_ERR(handle)) { |
140 | err = PTR_ERR(handle); | |
141 | goto setversion_out; | |
142 | } | |
617ba13b | 143 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
ac27a0ec | 144 | if (err == 0) { |
ef7f3835 | 145 | inode->i_ctime = ext4_current_time(inode); |
ac27a0ec | 146 | inode->i_generation = generation; |
617ba13b | 147 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); |
ac27a0ec | 148 | } |
617ba13b | 149 | ext4_journal_stop(handle); |
42a74f20 DH |
150 | setversion_out: |
151 | mnt_drop_write(filp->f_path.mnt); | |
ac27a0ec DK |
152 | return err; |
153 | } | |
e23291b9 | 154 | #ifdef CONFIG_JBD2_DEBUG |
617ba13b | 155 | case EXT4_IOC_WAIT_FOR_READONLY: |
ac27a0ec DK |
156 | /* |
157 | * This is racy - by the time we're woken up and running, | |
158 | * the superblock could be released. And the module could | |
159 | * have been unloaded. So sue me. | |
160 | * | |
161 | * Returns 1 if it slept, else zero. | |
162 | */ | |
163 | { | |
164 | struct super_block *sb = inode->i_sb; | |
165 | DECLARE_WAITQUEUE(wait, current); | |
166 | int ret = 0; | |
167 | ||
168 | set_current_state(TASK_INTERRUPTIBLE); | |
617ba13b MC |
169 | add_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait); |
170 | if (timer_pending(&EXT4_SB(sb)->turn_ro_timer)) { | |
ac27a0ec DK |
171 | schedule(); |
172 | ret = 1; | |
173 | } | |
617ba13b | 174 | remove_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait); |
ac27a0ec DK |
175 | return ret; |
176 | } | |
177 | #endif | |
617ba13b | 178 | case EXT4_IOC_GETRSVSZ: |
ac27a0ec DK |
179 | if (test_opt(inode->i_sb, RESERVATION) |
180 | && S_ISREG(inode->i_mode) | |
181 | && ei->i_block_alloc_info) { | |
182 | rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size; | |
183 | return put_user(rsv_window_size, (int __user *)arg); | |
184 | } | |
185 | return -ENOTTY; | |
617ba13b | 186 | case EXT4_IOC_SETRSVSZ: { |
42a74f20 | 187 | int err; |
ac27a0ec DK |
188 | |
189 | if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode)) | |
190 | return -ENOTTY; | |
191 | ||
3bd858ab | 192 | if (!is_owner_or_cap(inode)) |
ac27a0ec DK |
193 | return -EACCES; |
194 | ||
195 | if (get_user(rsv_window_size, (int __user *)arg)) | |
196 | return -EFAULT; | |
197 | ||
42a74f20 DH |
198 | err = mnt_want_write(filp->f_path.mnt); |
199 | if (err) | |
200 | return err; | |
201 | ||
617ba13b MC |
202 | if (rsv_window_size > EXT4_MAX_RESERVE_BLOCKS) |
203 | rsv_window_size = EXT4_MAX_RESERVE_BLOCKS; | |
ac27a0ec DK |
204 | |
205 | /* | |
206 | * need to allocate reservation structure for this inode | |
207 | * before set the window size | |
208 | */ | |
0e855ac8 | 209 | down_write(&ei->i_data_sem); |
ac27a0ec | 210 | if (!ei->i_block_alloc_info) |
617ba13b | 211 | ext4_init_block_alloc_info(inode); |
ac27a0ec DK |
212 | |
213 | if (ei->i_block_alloc_info){ | |
617ba13b | 214 | struct ext4_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node; |
ac27a0ec DK |
215 | rsv->rsv_goal_size = rsv_window_size; |
216 | } | |
0e855ac8 | 217 | up_write(&ei->i_data_sem); |
42a74f20 | 218 | mnt_drop_write(filp->f_path.mnt); |
ac27a0ec DK |
219 | return 0; |
220 | } | |
617ba13b MC |
221 | case EXT4_IOC_GROUP_EXTEND: { |
222 | ext4_fsblk_t n_blocks_count; | |
ac27a0ec DK |
223 | struct super_block *sb = inode->i_sb; |
224 | int err; | |
225 | ||
226 | if (!capable(CAP_SYS_RESOURCE)) | |
227 | return -EPERM; | |
228 | ||
ac27a0ec DK |
229 | if (get_user(n_blocks_count, (__u32 __user *)arg)) |
230 | return -EFAULT; | |
231 | ||
42a74f20 DH |
232 | err = mnt_want_write(filp->f_path.mnt); |
233 | if (err) | |
234 | return err; | |
235 | ||
617ba13b | 236 | err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count); |
dab291af MC |
237 | jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); |
238 | jbd2_journal_flush(EXT4_SB(sb)->s_journal); | |
239 | jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); | |
42a74f20 | 240 | mnt_drop_write(filp->f_path.mnt); |
ac27a0ec DK |
241 | |
242 | return err; | |
243 | } | |
617ba13b MC |
244 | case EXT4_IOC_GROUP_ADD: { |
245 | struct ext4_new_group_data input; | |
ac27a0ec DK |
246 | struct super_block *sb = inode->i_sb; |
247 | int err; | |
248 | ||
249 | if (!capable(CAP_SYS_RESOURCE)) | |
250 | return -EPERM; | |
251 | ||
617ba13b | 252 | if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg, |
ac27a0ec DK |
253 | sizeof(input))) |
254 | return -EFAULT; | |
255 | ||
42a74f20 DH |
256 | err = mnt_want_write(filp->f_path.mnt); |
257 | if (err) | |
258 | return err; | |
259 | ||
617ba13b | 260 | err = ext4_group_add(sb, &input); |
dab291af MC |
261 | jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); |
262 | jbd2_journal_flush(EXT4_SB(sb)->s_journal); | |
263 | jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); | |
42a74f20 | 264 | mnt_drop_write(filp->f_path.mnt); |
ac27a0ec DK |
265 | |
266 | return err; | |
267 | } | |
268 | ||
c14c6fd5 AK |
269 | case EXT4_IOC_MIGRATE: |
270 | return ext4_ext_migrate(inode, filp, cmd, arg); | |
271 | ||
ac27a0ec DK |
272 | default: |
273 | return -ENOTTY; | |
274 | } | |
275 | } | |
276 | ||
277 | #ifdef CONFIG_COMPAT | |
617ba13b | 278 | long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
ac27a0ec | 279 | { |
9d549890 | 280 | struct inode *inode = file->f_path.dentry->d_inode; |
ac27a0ec DK |
281 | int ret; |
282 | ||
283 | /* These are just misnamed, they actually get/put from/to user an int */ | |
284 | switch (cmd) { | |
617ba13b MC |
285 | case EXT4_IOC32_GETFLAGS: |
286 | cmd = EXT4_IOC_GETFLAGS; | |
ac27a0ec | 287 | break; |
617ba13b MC |
288 | case EXT4_IOC32_SETFLAGS: |
289 | cmd = EXT4_IOC_SETFLAGS; | |
ac27a0ec | 290 | break; |
617ba13b MC |
291 | case EXT4_IOC32_GETVERSION: |
292 | cmd = EXT4_IOC_GETVERSION; | |
ac27a0ec | 293 | break; |
617ba13b MC |
294 | case EXT4_IOC32_SETVERSION: |
295 | cmd = EXT4_IOC_SETVERSION; | |
ac27a0ec | 296 | break; |
617ba13b MC |
297 | case EXT4_IOC32_GROUP_EXTEND: |
298 | cmd = EXT4_IOC_GROUP_EXTEND; | |
ac27a0ec | 299 | break; |
617ba13b MC |
300 | case EXT4_IOC32_GETVERSION_OLD: |
301 | cmd = EXT4_IOC_GETVERSION_OLD; | |
ac27a0ec | 302 | break; |
617ba13b MC |
303 | case EXT4_IOC32_SETVERSION_OLD: |
304 | cmd = EXT4_IOC_SETVERSION_OLD; | |
ac27a0ec | 305 | break; |
e23291b9 | 306 | #ifdef CONFIG_JBD2_DEBUG |
617ba13b MC |
307 | case EXT4_IOC32_WAIT_FOR_READONLY: |
308 | cmd = EXT4_IOC_WAIT_FOR_READONLY; | |
ac27a0ec DK |
309 | break; |
310 | #endif | |
617ba13b MC |
311 | case EXT4_IOC32_GETRSVSZ: |
312 | cmd = EXT4_IOC_GETRSVSZ; | |
ac27a0ec | 313 | break; |
617ba13b MC |
314 | case EXT4_IOC32_SETRSVSZ: |
315 | cmd = EXT4_IOC_SETRSVSZ; | |
ac27a0ec | 316 | break; |
617ba13b | 317 | case EXT4_IOC_GROUP_ADD: |
ac27a0ec DK |
318 | break; |
319 | default: | |
320 | return -ENOIOCTLCMD; | |
321 | } | |
322 | lock_kernel(); | |
617ba13b | 323 | ret = ext4_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg)); |
ac27a0ec DK |
324 | unlock_kernel(); |
325 | return ret; | |
326 | } | |
327 | #endif |