]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/ext4/ioctl.c
ext4: add a new function which adds a flex group to a fs
[mirror_ubuntu-zesty-kernel.git] / fs / ext4 / ioctl.c
CommitLineData
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>
ac27a0ec
DK
13#include <linux/time.h>
14#include <linux/compat.h>
42a74f20 15#include <linux/mount.h>
748de673 16#include <linux/file.h>
ac27a0ec 17#include <asm/uaccess.h>
3dcf5451
CH
18#include "ext4_jbd2.h"
19#include "ext4.h"
ac27a0ec 20
5cdd7b2d 21long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
ac27a0ec 22{
5cdd7b2d 23 struct inode *inode = filp->f_dentry->d_inode;
bab08ab9 24 struct super_block *sb = inode->i_sb;
617ba13b 25 struct ext4_inode_info *ei = EXT4_I(inode);
ac27a0ec 26 unsigned int flags;
ac27a0ec 27
af5bc92d 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 36 handle_t *handle = NULL;
4db46fc2 37 int err, migrate = 0;
617ba13b 38 struct ext4_iloc iloc;
ac27a0ec
DK
39 unsigned int oldflags;
40 unsigned int jflag;
41
2e149670 42 if (!inode_owner_or_capable(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
2dc6b0d4 52 flags = ext4_mask_flags(inode->i_mode, flags);
ac27a0ec 53
42a74f20 54 err = -EPERM;
ac27a0ec 55 mutex_lock(&inode->i_mutex);
e47776a0 56 /* Is it quota file? Do not allow user to mess with it */
42a74f20
DH
57 if (IS_NOQUOTA(inode))
58 goto flags_out;
59
ac27a0ec
DK
60 oldflags = ei->i_flags;
61
62 /* The JOURNAL_DATA flag is modifiable only by root */
617ba13b 63 jflag = flags & EXT4_JOURNAL_DATA_FL;
ac27a0ec
DK
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 */
617ba13b 71 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
42a74f20
DH
72 if (!capable(CAP_LINUX_IMMUTABLE))
73 goto flags_out;
ac27a0ec
DK
74 }
75
76 /*
77 * The JOURNAL_DATA flag can only be changed by
78 * the relevant capability.
79 */
617ba13b 80 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
42a74f20
DH
81 if (!capable(CAP_SYS_RESOURCE))
82 goto flags_out;
ac27a0ec 83 }
4db46fc2
AK
84 if (oldflags & EXT4_EXTENTS_FL) {
85 /* We don't support clearning extent flags */
86 if (!(flags & EXT4_EXTENTS_FL)) {
87 err = -EOPNOTSUPP;
88 goto flags_out;
89 }
90 } else if (flags & EXT4_EXTENTS_FL) {
91 /* migrate the file */
92 migrate = 1;
93 flags &= ~EXT4_EXTENTS_FL;
94 }
ac27a0ec 95
c8d46e41
JZ
96 if (flags & EXT4_EOFBLOCKS_FL) {
97 /* we don't support adding EOFBLOCKS flag */
98 if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
99 err = -EOPNOTSUPP;
100 goto flags_out;
101 }
102 } else if (oldflags & EXT4_EOFBLOCKS_FL)
103 ext4_truncate(inode);
104
617ba13b 105 handle = ext4_journal_start(inode, 1);
ac27a0ec 106 if (IS_ERR(handle)) {
42a74f20
DH
107 err = PTR_ERR(handle);
108 goto flags_out;
ac27a0ec
DK
109 }
110 if (IS_SYNC(inode))
0390131b 111 ext4_handle_sync(handle);
617ba13b 112 err = ext4_reserve_inode_write(handle, inode, &iloc);
ac27a0ec
DK
113 if (err)
114 goto flags_err;
115
617ba13b
MC
116 flags = flags & EXT4_FL_USER_MODIFIABLE;
117 flags |= oldflags & ~EXT4_FL_USER_MODIFIABLE;
ac27a0ec
DK
118 ei->i_flags = flags;
119
617ba13b 120 ext4_set_inode_flags(inode);
ef7f3835 121 inode->i_ctime = ext4_current_time(inode);
ac27a0ec 122
617ba13b 123 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
ac27a0ec 124flags_err:
617ba13b 125 ext4_journal_stop(handle);
42a74f20
DH
126 if (err)
127 goto flags_out;
ac27a0ec 128
617ba13b
MC
129 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
130 err = ext4_change_inode_journal_flag(inode, jflag);
4db46fc2
AK
131 if (err)
132 goto flags_out;
133 if (migrate)
134 err = ext4_ext_migrate(inode);
42a74f20 135flags_out:
ac27a0ec 136 mutex_unlock(&inode->i_mutex);
42a74f20 137 mnt_drop_write(filp->f_path.mnt);
ac27a0ec
DK
138 return err;
139 }
617ba13b
MC
140 case EXT4_IOC_GETVERSION:
141 case EXT4_IOC_GETVERSION_OLD:
ac27a0ec 142 return put_user(inode->i_generation, (int __user *) arg);
617ba13b
MC
143 case EXT4_IOC_SETVERSION:
144 case EXT4_IOC_SETVERSION_OLD: {
ac27a0ec 145 handle_t *handle;
617ba13b 146 struct ext4_iloc iloc;
ac27a0ec
DK
147 __u32 generation;
148 int err;
149
2e149670 150 if (!inode_owner_or_capable(inode))
ac27a0ec 151 return -EPERM;
42a74f20
DH
152
153 err = mnt_want_write(filp->f_path.mnt);
154 if (err)
155 return err;
156 if (get_user(generation, (int __user *) arg)) {
157 err = -EFAULT;
158 goto setversion_out;
159 }
ac27a0ec 160
617ba13b 161 handle = ext4_journal_start(inode, 1);
42a74f20
DH
162 if (IS_ERR(handle)) {
163 err = PTR_ERR(handle);
164 goto setversion_out;
165 }
617ba13b 166 err = ext4_reserve_inode_write(handle, inode, &iloc);
ac27a0ec 167 if (err == 0) {
ef7f3835 168 inode->i_ctime = ext4_current_time(inode);
ac27a0ec 169 inode->i_generation = generation;
617ba13b 170 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
ac27a0ec 171 }
617ba13b 172 ext4_journal_stop(handle);
42a74f20
DH
173setversion_out:
174 mnt_drop_write(filp->f_path.mnt);
ac27a0ec
DK
175 return err;
176 }
617ba13b
MC
177 case EXT4_IOC_GROUP_EXTEND: {
178 ext4_fsblk_t n_blocks_count;
ac046f1d 179 int err, err2=0;
ac27a0ec 180
8f82f840
YY
181 err = ext4_resize_begin(sb);
182 if (err)
183 return err;
ac27a0ec 184
ac27a0ec
DK
185 if (get_user(n_blocks_count, (__u32 __user *)arg))
186 return -EFAULT;
187
bab08ab9
TT
188 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
189 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
190 ext4_msg(sb, KERN_ERR,
191 "Online resizing not supported with bigalloc");
192 return -EOPNOTSUPP;
193 }
194
42a74f20
DH
195 err = mnt_want_write(filp->f_path.mnt);
196 if (err)
197 return err;
198
617ba13b 199 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
ac046f1d
PT
200 if (EXT4_SB(sb)->s_journal) {
201 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
202 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
203 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
204 }
7ffe1ea8
HK
205 if (err == 0)
206 err = err2;
42a74f20 207 mnt_drop_write(filp->f_path.mnt);
8f82f840 208 ext4_resize_end(sb);
ac27a0ec
DK
209
210 return err;
211 }
748de673
AF
212
213 case EXT4_IOC_MOVE_EXT: {
214 struct move_extent me;
215 struct file *donor_filp;
216 int err;
217
4a58579b
AF
218 if (!(filp->f_mode & FMODE_READ) ||
219 !(filp->f_mode & FMODE_WRITE))
220 return -EBADF;
221
748de673
AF
222 if (copy_from_user(&me,
223 (struct move_extent __user *)arg, sizeof(me)))
224 return -EFAULT;
4a58579b 225 me.moved_len = 0;
748de673
AF
226
227 donor_filp = fget(me.donor_fd);
228 if (!donor_filp)
229 return -EBADF;
230
4a58579b
AF
231 if (!(donor_filp->f_mode & FMODE_WRITE)) {
232 err = -EBADF;
233 goto mext_out;
748de673
AF
234 }
235
bab08ab9
TT
236 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
237 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
238 ext4_msg(sb, KERN_ERR,
239 "Online defrag not supported with bigalloc");
240 return -EOPNOTSUPP;
241 }
242
4a58579b
AF
243 err = mnt_want_write(filp->f_path.mnt);
244 if (err)
245 goto mext_out;
246
748de673
AF
247 err = ext4_move_extents(filp, donor_filp, me.orig_start,
248 me.donor_start, me.len, &me.moved_len);
4a58579b 249 mnt_drop_write(filp->f_path.mnt);
748de673 250
60e6679e 251 if (copy_to_user((struct move_extent __user *)arg,
c437b273 252 &me, sizeof(me)))
4a58579b
AF
253 err = -EFAULT;
254mext_out:
255 fput(donor_filp);
748de673
AF
256 return err;
257 }
258
617ba13b
MC
259 case EXT4_IOC_GROUP_ADD: {
260 struct ext4_new_group_data input;
ac046f1d 261 int err, err2=0;
ac27a0ec 262
8f82f840
YY
263 err = ext4_resize_begin(sb);
264 if (err)
265 return err;
ac27a0ec 266
617ba13b 267 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
ac27a0ec
DK
268 sizeof(input)))
269 return -EFAULT;
270
bab08ab9
TT
271 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
272 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
273 ext4_msg(sb, KERN_ERR,
274 "Online resizing not supported with bigalloc");
275 return -EOPNOTSUPP;
276 }
277
42a74f20
DH
278 err = mnt_want_write(filp->f_path.mnt);
279 if (err)
280 return err;
281
617ba13b 282 err = ext4_group_add(sb, &input);
ac046f1d
PT
283 if (EXT4_SB(sb)->s_journal) {
284 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
285 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
286 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
287 }
7ffe1ea8
HK
288 if (err == 0)
289 err = err2;
42a74f20 290 mnt_drop_write(filp->f_path.mnt);
8f82f840 291 ext4_resize_end(sb);
ac27a0ec
DK
292
293 return err;
294 }
295
c14c6fd5 296 case EXT4_IOC_MIGRATE:
2a43a878
AK
297 {
298 int err;
2e149670 299 if (!inode_owner_or_capable(inode))
2a43a878
AK
300 return -EACCES;
301
302 err = mnt_want_write(filp->f_path.mnt);
303 if (err)
304 return err;
305 /*
306 * inode_mutex prevent write and truncate on the file.
307 * Read still goes through. We take i_data_sem in
308 * ext4_ext_swap_inode_data before we switch the
309 * inode format to prevent read.
310 */
311 mutex_lock(&(inode->i_mutex));
312 err = ext4_ext_migrate(inode);
313 mutex_unlock(&(inode->i_mutex));
314 mnt_drop_write(filp->f_path.mnt);
315 return err;
316 }
c14c6fd5 317
ccd2506b
TT
318 case EXT4_IOC_ALLOC_DA_BLKS:
319 {
320 int err;
2e149670 321 if (!inode_owner_or_capable(inode))
ccd2506b
TT
322 return -EACCES;
323
324 err = mnt_want_write(filp->f_path.mnt);
325 if (err)
326 return err;
327 err = ext4_alloc_da_blocks(inode);
328 mnt_drop_write(filp->f_path.mnt);
329 return err;
330 }
331
e681c047
LC
332 case FITRIM:
333 {
41431792 334 struct request_queue *q = bdev_get_queue(sb->s_bdev);
e681c047
LC
335 struct fstrim_range range;
336 int ret = 0;
337
338 if (!capable(CAP_SYS_ADMIN))
339 return -EPERM;
340
41431792
LC
341 if (!blk_queue_discard(q))
342 return -EOPNOTSUPP;
343
bab08ab9
TT
344 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
345 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
346 ext4_msg(sb, KERN_ERR,
347 "FITRIM not supported with bigalloc");
348 return -EOPNOTSUPP;
349 }
350
e6705f7c 351 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
e681c047
LC
352 sizeof(range)))
353 return -EFAULT;
354
5c2ed62f
LC
355 range.minlen = max((unsigned int)range.minlen,
356 q->limits.discard_granularity);
e681c047
LC
357 ret = ext4_trim_fs(sb, &range);
358 if (ret < 0)
359 return ret;
360
e6705f7c 361 if (copy_to_user((struct fstrim_range __user *)arg, &range,
e681c047
LC
362 sizeof(range)))
363 return -EFAULT;
364
365 return 0;
366 }
367
ac27a0ec
DK
368 default:
369 return -ENOTTY;
370 }
371}
372
373#ifdef CONFIG_COMPAT
617ba13b 374long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
ac27a0ec 375{
ac27a0ec
DK
376 /* These are just misnamed, they actually get/put from/to user an int */
377 switch (cmd) {
617ba13b
MC
378 case EXT4_IOC32_GETFLAGS:
379 cmd = EXT4_IOC_GETFLAGS;
ac27a0ec 380 break;
617ba13b
MC
381 case EXT4_IOC32_SETFLAGS:
382 cmd = EXT4_IOC_SETFLAGS;
ac27a0ec 383 break;
617ba13b
MC
384 case EXT4_IOC32_GETVERSION:
385 cmd = EXT4_IOC_GETVERSION;
ac27a0ec 386 break;
617ba13b
MC
387 case EXT4_IOC32_SETVERSION:
388 cmd = EXT4_IOC_SETVERSION;
ac27a0ec 389 break;
617ba13b
MC
390 case EXT4_IOC32_GROUP_EXTEND:
391 cmd = EXT4_IOC_GROUP_EXTEND;
ac27a0ec 392 break;
617ba13b
MC
393 case EXT4_IOC32_GETVERSION_OLD:
394 cmd = EXT4_IOC_GETVERSION_OLD;
ac27a0ec 395 break;
617ba13b
MC
396 case EXT4_IOC32_SETVERSION_OLD:
397 cmd = EXT4_IOC_SETVERSION_OLD;
ac27a0ec 398 break;
617ba13b
MC
399 case EXT4_IOC32_GETRSVSZ:
400 cmd = EXT4_IOC_GETRSVSZ;
ac27a0ec 401 break;
617ba13b
MC
402 case EXT4_IOC32_SETRSVSZ:
403 cmd = EXT4_IOC_SETRSVSZ;
ac27a0ec 404 break;
4d92dc0f
BH
405 case EXT4_IOC32_GROUP_ADD: {
406 struct compat_ext4_new_group_input __user *uinput;
407 struct ext4_new_group_input input;
408 mm_segment_t old_fs;
409 int err;
410
411 uinput = compat_ptr(arg);
412 err = get_user(input.group, &uinput->group);
413 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
414 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
415 err |= get_user(input.inode_table, &uinput->inode_table);
416 err |= get_user(input.blocks_count, &uinput->blocks_count);
417 err |= get_user(input.reserved_blocks,
418 &uinput->reserved_blocks);
419 if (err)
420 return -EFAULT;
421 old_fs = get_fs();
422 set_fs(KERNEL_DS);
423 err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
424 (unsigned long) &input);
425 set_fs(old_fs);
426 return err;
427 }
b684b2ee 428 case EXT4_IOC_MOVE_EXT:
a56e69c2 429 case FITRIM:
b684b2ee 430 break;
ac27a0ec
DK
431 default:
432 return -ENOIOCTLCMD;
433 }
5cdd7b2d 434 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
ac27a0ec
DK
435}
436#endif