]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/ext4/ioctl.c
powerpc/nohash: Fix use of mmu_has_feature() in setup_initial_memory_limit()
[mirror_ubuntu-artful-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>
ac27a0ec 11#include <linux/capability.h>
ac27a0ec
DK
12#include <linux/time.h>
13#include <linux/compat.h>
42a74f20 14#include <linux/mount.h>
748de673 15#include <linux/file.h>
9b7365fc 16#include <linux/quotaops.h>
8da4b8c4 17#include <linux/uuid.h>
7c0f6ba6 18#include <linux/uaccess.h>
783d9485 19#include <linux/delay.h>
3dcf5451
CH
20#include "ext4_jbd2.h"
21#include "ext4.h"
ac27a0ec 22
393d1d1d
DTB
23/**
24 * Swap memory between @a and @b for @len bytes.
25 *
26 * @a: pointer to first memory area
27 * @b: pointer to second memory area
28 * @len: number of bytes to swap
29 *
30 */
31static void memswap(void *a, void *b, size_t len)
32{
33 unsigned char *ap, *bp;
393d1d1d
DTB
34
35 ap = (unsigned char *)a;
36 bp = (unsigned char *)b;
37 while (len-- > 0) {
4b7e2db5 38 swap(*ap, *bp);
393d1d1d
DTB
39 ap++;
40 bp++;
41 }
42}
43
44/**
45 * Swap i_data and associated attributes between @inode1 and @inode2.
46 * This function is used for the primary swap between inode1 and inode2
47 * and also to revert this primary swap in case of errors.
48 *
49 * Therefore you have to make sure, that calling this method twice
50 * will revert all changes.
51 *
52 * @inode1: pointer to first inode
53 * @inode2: pointer to second inode
54 */
55static void swap_inode_data(struct inode *inode1, struct inode *inode2)
56{
57 loff_t isize;
58 struct ext4_inode_info *ei1;
59 struct ext4_inode_info *ei2;
60
61 ei1 = EXT4_I(inode1);
62 ei2 = EXT4_I(inode2);
63
64 memswap(&inode1->i_flags, &inode2->i_flags, sizeof(inode1->i_flags));
65 memswap(&inode1->i_version, &inode2->i_version,
66 sizeof(inode1->i_version));
67 memswap(&inode1->i_blocks, &inode2->i_blocks,
68 sizeof(inode1->i_blocks));
69 memswap(&inode1->i_bytes, &inode2->i_bytes, sizeof(inode1->i_bytes));
70 memswap(&inode1->i_atime, &inode2->i_atime, sizeof(inode1->i_atime));
71 memswap(&inode1->i_mtime, &inode2->i_mtime, sizeof(inode1->i_mtime));
72
73 memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data));
74 memswap(&ei1->i_flags, &ei2->i_flags, sizeof(ei1->i_flags));
75 memswap(&ei1->i_disksize, &ei2->i_disksize, sizeof(ei1->i_disksize));
cde2d7a7
TT
76 ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS);
77 ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS);
393d1d1d
DTB
78
79 isize = i_size_read(inode1);
80 i_size_write(inode1, i_size_read(inode2));
81 i_size_write(inode2, isize);
82}
83
84/**
85 * Swap the information from the given @inode and the inode
86 * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
87 * important fields of the inodes.
88 *
89 * @sb: the super block of the filesystem
90 * @inode: the inode to swap with EXT4_BOOT_LOADER_INO
91 *
92 */
93static long swap_inode_boot_loader(struct super_block *sb,
94 struct inode *inode)
95{
96 handle_t *handle;
97 int err;
98 struct inode *inode_bl;
393d1d1d 99 struct ext4_inode_info *ei_bl;
d7092ae2 100 struct ext4_sb_info *sbi = EXT4_SB(sb);
393d1d1d 101
d8558a29
TT
102 if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode))
103 return -EINVAL;
393d1d1d 104
d8558a29
TT
105 if (!inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN))
106 return -EPERM;
393d1d1d 107
393d1d1d 108 inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO);
d8558a29
TT
109 if (IS_ERR(inode_bl))
110 return PTR_ERR(inode_bl);
393d1d1d
DTB
111 ei_bl = EXT4_I(inode_bl);
112
113 filemap_flush(inode->i_mapping);
114 filemap_flush(inode_bl->i_mapping);
115
116 /* Protect orig inodes against a truncate and make sure,
117 * that only 1 swap_inode_boot_loader is running. */
375e289e 118 lock_two_nondirectories(inode, inode_bl);
393d1d1d
DTB
119
120 truncate_inode_pages(&inode->i_data, 0);
121 truncate_inode_pages(&inode_bl->i_data, 0);
122
123 /* Wait for all existing dio workers */
124 ext4_inode_block_unlocked_dio(inode);
125 ext4_inode_block_unlocked_dio(inode_bl);
126 inode_dio_wait(inode);
127 inode_dio_wait(inode_bl);
128
129 handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
130 if (IS_ERR(handle)) {
131 err = -EINVAL;
30d29b11 132 goto journal_err_out;
393d1d1d
DTB
133 }
134
135 /* Protect extent tree against block allocations via delalloc */
136 ext4_double_down_write_data_sem(inode, inode_bl);
137
138 if (inode_bl->i_nlink == 0) {
139 /* this inode has never been used as a BOOT_LOADER */
140 set_nlink(inode_bl, 1);
141 i_uid_write(inode_bl, 0);
142 i_gid_write(inode_bl, 0);
143 inode_bl->i_flags = 0;
144 ei_bl->i_flags = 0;
145 inode_bl->i_version = 1;
146 i_size_write(inode_bl, 0);
147 inode_bl->i_mode = S_IFREG;
e2b911c5 148 if (ext4_has_feature_extents(sb)) {
393d1d1d
DTB
149 ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
150 ext4_ext_tree_init(handle, inode_bl);
151 } else
152 memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
153 }
154
155 swap_inode_data(inode, inode_bl);
156
eeca7ea1 157 inode->i_ctime = inode_bl->i_ctime = current_time(inode);
393d1d1d
DTB
158
159 spin_lock(&sbi->s_next_gen_lock);
160 inode->i_generation = sbi->s_next_generation++;
161 inode_bl->i_generation = sbi->s_next_generation++;
162 spin_unlock(&sbi->s_next_gen_lock);
163
164 ext4_discard_preallocations(inode);
165
166 err = ext4_mark_inode_dirty(handle, inode);
167 if (err < 0) {
168 ext4_warning(inode->i_sb,
169 "couldn't mark inode #%lu dirty (err %d)",
170 inode->i_ino, err);
171 /* Revert all changes: */
172 swap_inode_data(inode, inode_bl);
173 } else {
174 err = ext4_mark_inode_dirty(handle, inode_bl);
175 if (err < 0) {
176 ext4_warning(inode_bl->i_sb,
177 "couldn't mark inode #%lu dirty (err %d)",
178 inode_bl->i_ino, err);
179 /* Revert all changes: */
180 swap_inode_data(inode, inode_bl);
181 ext4_mark_inode_dirty(handle, inode);
182 }
183 }
393d1d1d 184 ext4_journal_stop(handle);
393d1d1d
DTB
185 ext4_double_up_write_data_sem(inode, inode_bl);
186
30d29b11 187journal_err_out:
393d1d1d
DTB
188 ext4_inode_resume_unlocked_dio(inode);
189 ext4_inode_resume_unlocked_dio(inode_bl);
375e289e 190 unlock_two_nondirectories(inode, inode_bl);
393d1d1d 191 iput(inode_bl);
393d1d1d
DTB
192 return err;
193}
194
ba679017 195#ifdef CONFIG_EXT4_FS_ENCRYPTION
9bd8212f
MH
196static int uuid_is_zero(__u8 u[16])
197{
198 int i;
199
200 for (i = 0; i < 16; i++)
201 if (u[i])
202 return 0;
203 return 1;
204}
ba679017 205#endif
9bd8212f 206
9b7365fc
LX
207static int ext4_ioctl_setflags(struct inode *inode,
208 unsigned int flags)
209{
210 struct ext4_inode_info *ei = EXT4_I(inode);
211 handle_t *handle = NULL;
fdde368e 212 int err = -EPERM, migrate = 0;
9b7365fc
LX
213 struct ext4_iloc iloc;
214 unsigned int oldflags, mask, i;
215 unsigned int jflag;
216
217 /* Is it quota file? Do not allow user to mess with it */
218 if (IS_NOQUOTA(inode))
219 goto flags_out;
220
221 oldflags = ei->i_flags;
222
223 /* The JOURNAL_DATA flag is modifiable only by root */
224 jflag = flags & EXT4_JOURNAL_DATA_FL;
225
226 /*
227 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
228 * the relevant capability.
229 *
230 * This test looks nicer. Thanks to Pauline Middelink
231 */
232 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
233 if (!capable(CAP_LINUX_IMMUTABLE))
234 goto flags_out;
235 }
236
237 /*
238 * The JOURNAL_DATA flag can only be changed by
239 * the relevant capability.
240 */
241 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
242 if (!capable(CAP_SYS_RESOURCE))
243 goto flags_out;
244 }
245 if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
246 migrate = 1;
247
248 if (flags & EXT4_EOFBLOCKS_FL) {
249 /* we don't support adding EOFBLOCKS flag */
250 if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
251 err = -EOPNOTSUPP;
252 goto flags_out;
253 }
2c98eb5e
TT
254 } else if (oldflags & EXT4_EOFBLOCKS_FL) {
255 err = ext4_truncate(inode);
256 if (err)
257 goto flags_out;
258 }
9b7365fc
LX
259
260 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
261 if (IS_ERR(handle)) {
262 err = PTR_ERR(handle);
263 goto flags_out;
264 }
265 if (IS_SYNC(inode))
266 ext4_handle_sync(handle);
267 err = ext4_reserve_inode_write(handle, inode, &iloc);
268 if (err)
269 goto flags_err;
270
271 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
272 if (!(mask & EXT4_FL_USER_MODIFIABLE))
273 continue;
f8011d93
JK
274 /* These flags get special treatment later */
275 if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL)
276 continue;
9b7365fc
LX
277 if (mask & flags)
278 ext4_set_inode_flag(inode, i);
279 else
280 ext4_clear_inode_flag(inode, i);
281 }
282
283 ext4_set_inode_flags(inode);
eeca7ea1 284 inode->i_ctime = current_time(inode);
9b7365fc
LX
285
286 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
287flags_err:
288 ext4_journal_stop(handle);
289 if (err)
290 goto flags_out;
291
292 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
293 err = ext4_change_inode_journal_flag(inode, jflag);
294 if (err)
295 goto flags_out;
296 if (migrate) {
297 if (flags & EXT4_EXTENTS_FL)
298 err = ext4_ext_migrate(inode);
299 else
300 err = ext4_ind_migrate(inode);
301 }
302
303flags_out:
304 return err;
305}
306
307#ifdef CONFIG_QUOTA
308static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
309{
310 struct inode *inode = file_inode(filp);
311 struct super_block *sb = inode->i_sb;
312 struct ext4_inode_info *ei = EXT4_I(inode);
313 int err, rc;
314 handle_t *handle;
315 kprojid_t kprojid;
316 struct ext4_iloc iloc;
317 struct ext4_inode *raw_inode;
079788d0 318 struct dquot *transfer_to[MAXQUOTAS] = { };
9b7365fc 319
0b7b7779 320 if (!ext4_has_feature_project(sb)) {
9b7365fc
LX
321 if (projid != EXT4_DEF_PROJID)
322 return -EOPNOTSUPP;
323 else
324 return 0;
325 }
326
327 if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
328 return -EOPNOTSUPP;
329
330 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
331
332 if (projid_eq(kprojid, EXT4_I(inode)->i_projid))
333 return 0;
334
335 err = mnt_want_write_file(filp);
336 if (err)
337 return err;
338
339 err = -EPERM;
5955102c 340 inode_lock(inode);
9b7365fc
LX
341 /* Is it quota file? Do not allow user to mess with it */
342 if (IS_NOQUOTA(inode))
343 goto out_unlock;
344
345 err = ext4_get_inode_loc(inode, &iloc);
346 if (err)
347 goto out_unlock;
348
349 raw_inode = ext4_raw_inode(&iloc);
350 if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
351 err = -EOVERFLOW;
352 brelse(iloc.bh);
353 goto out_unlock;
354 }
355 brelse(iloc.bh);
356
357 dquot_initialize(inode);
358
359 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
360 EXT4_QUOTA_INIT_BLOCKS(sb) +
361 EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
362 if (IS_ERR(handle)) {
363 err = PTR_ERR(handle);
364 goto out_unlock;
365 }
366
367 err = ext4_reserve_inode_write(handle, inode, &iloc);
368 if (err)
369 goto out_stop;
370
079788d0
WS
371 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
372 if (!IS_ERR(transfer_to[PRJQUOTA])) {
373 err = __dquot_transfer(inode, transfer_to);
374 dqput(transfer_to[PRJQUOTA]);
375 if (err)
376 goto out_dirty;
9b7365fc 377 }
079788d0 378
9b7365fc 379 EXT4_I(inode)->i_projid = kprojid;
eeca7ea1 380 inode->i_ctime = current_time(inode);
9b7365fc
LX
381out_dirty:
382 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
383 if (!err)
384 err = rc;
385out_stop:
386 ext4_journal_stop(handle);
387out_unlock:
5955102c 388 inode_unlock(inode);
9b7365fc
LX
389 mnt_drop_write_file(filp);
390 return err;
391}
392#else
393static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
394{
395 if (projid != EXT4_DEF_PROJID)
396 return -EOPNOTSUPP;
397 return 0;
398}
399#endif
400
401/* Transfer internal flags to xflags */
402static inline __u32 ext4_iflags_to_xflags(unsigned long iflags)
403{
404 __u32 xflags = 0;
405
406 if (iflags & EXT4_SYNC_FL)
407 xflags |= FS_XFLAG_SYNC;
408 if (iflags & EXT4_IMMUTABLE_FL)
409 xflags |= FS_XFLAG_IMMUTABLE;
410 if (iflags & EXT4_APPEND_FL)
411 xflags |= FS_XFLAG_APPEND;
412 if (iflags & EXT4_NODUMP_FL)
413 xflags |= FS_XFLAG_NODUMP;
414 if (iflags & EXT4_NOATIME_FL)
415 xflags |= FS_XFLAG_NOATIME;
416 if (iflags & EXT4_PROJINHERIT_FL)
417 xflags |= FS_XFLAG_PROJINHERIT;
418 return xflags;
419}
420
d14e7683
JK
421#define EXT4_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
422 FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
423 FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
424
9b7365fc
LX
425/* Transfer xflags flags to internal */
426static inline unsigned long ext4_xflags_to_iflags(__u32 xflags)
427{
428 unsigned long iflags = 0;
429
430 if (xflags & FS_XFLAG_SYNC)
431 iflags |= EXT4_SYNC_FL;
432 if (xflags & FS_XFLAG_IMMUTABLE)
433 iflags |= EXT4_IMMUTABLE_FL;
434 if (xflags & FS_XFLAG_APPEND)
435 iflags |= EXT4_APPEND_FL;
436 if (xflags & FS_XFLAG_NODUMP)
437 iflags |= EXT4_NODUMP_FL;
438 if (xflags & FS_XFLAG_NOATIME)
439 iflags |= EXT4_NOATIME_FL;
440 if (xflags & FS_XFLAG_PROJINHERIT)
441 iflags |= EXT4_PROJINHERIT_FL;
442
443 return iflags;
444}
445
e9be2ac7 446int ext4_shutdown(struct super_block *sb, unsigned long arg)
783d9485
TT
447{
448 struct ext4_sb_info *sbi = EXT4_SB(sb);
449 __u32 flags;
450
451 if (!capable(CAP_SYS_ADMIN))
452 return -EPERM;
453
454 if (get_user(flags, (__u32 __user *)arg))
455 return -EFAULT;
456
457 if (flags > EXT4_GOING_FLAGS_NOLOGFLUSH)
458 return -EINVAL;
459
460 if (ext4_forced_shutdown(sbi))
461 return 0;
462
463 ext4_msg(sb, KERN_ALERT, "shut down requested (%d)", flags);
464
465 switch (flags) {
466 case EXT4_GOING_FLAGS_DEFAULT:
467 freeze_bdev(sb->s_bdev);
468 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
469 thaw_bdev(sb->s_bdev, sb);
470 break;
471 case EXT4_GOING_FLAGS_LOGFLUSH:
472 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
473 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) {
474 (void) ext4_force_commit(sb);
475 jbd2_journal_abort(sbi->s_journal, 0);
476 }
477 break;
478 case EXT4_GOING_FLAGS_NOLOGFLUSH:
479 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
480 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) {
481 msleep(100);
482 jbd2_journal_abort(sbi->s_journal, 0);
483 }
484 break;
485 default:
486 return -EINVAL;
487 }
488 clear_opt(sb, DISCARD);
489 return 0;
490}
491
5cdd7b2d 492long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
ac27a0ec 493{
496ad9aa 494 struct inode *inode = file_inode(filp);
bab08ab9 495 struct super_block *sb = inode->i_sb;
617ba13b 496 struct ext4_inode_info *ei = EXT4_I(inode);
ac27a0ec 497 unsigned int flags;
ac27a0ec 498
af5bc92d 499 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
ac27a0ec
DK
500
501 switch (cmd) {
617ba13b 502 case EXT4_IOC_GETFLAGS:
ff9ddf7e 503 ext4_get_inode_flags(ei);
617ba13b 504 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
ac27a0ec 505 return put_user(flags, (int __user *) arg);
617ba13b 506 case EXT4_IOC_SETFLAGS: {
9b7365fc 507 int err;
ac27a0ec 508
2e149670 509 if (!inode_owner_or_capable(inode))
ac27a0ec
DK
510 return -EACCES;
511
512 if (get_user(flags, (int __user *) arg))
513 return -EFAULT;
514
d14e7683
JK
515 if (flags & ~EXT4_FL_USER_VISIBLE)
516 return -EOPNOTSUPP;
517 /*
518 * chattr(1) grabs flags via GETFLAGS, modifies the result and
519 * passes that to SETFLAGS. So we cannot easily make SETFLAGS
520 * more restrictive than just silently masking off visible but
521 * not settable flags as we always did.
522 */
523 flags &= EXT4_FL_USER_MODIFIABLE;
524 if (ext4_mask_flags(inode->i_mode, flags) != flags)
525 return -EOPNOTSUPP;
526
a561be71 527 err = mnt_want_write_file(filp);
42a74f20
DH
528 if (err)
529 return err;
530
5955102c 531 inode_lock(inode);
9b7365fc 532 err = ext4_ioctl_setflags(inode, flags);
5955102c 533 inode_unlock(inode);
2a79f17e 534 mnt_drop_write_file(filp);
ac27a0ec
DK
535 return err;
536 }
617ba13b
MC
537 case EXT4_IOC_GETVERSION:
538 case EXT4_IOC_GETVERSION_OLD:
ac27a0ec 539 return put_user(inode->i_generation, (int __user *) arg);
617ba13b
MC
540 case EXT4_IOC_SETVERSION:
541 case EXT4_IOC_SETVERSION_OLD: {
ac27a0ec 542 handle_t *handle;
617ba13b 543 struct ext4_iloc iloc;
ac27a0ec
DK
544 __u32 generation;
545 int err;
546
2e149670 547 if (!inode_owner_or_capable(inode))
ac27a0ec 548 return -EPERM;
42a74f20 549
9aa5d32b 550 if (ext4_has_metadata_csum(inode->i_sb)) {
814525f4
DW
551 ext4_warning(sb, "Setting inode version is not "
552 "supported with metadata_csum enabled.");
553 return -ENOTTY;
554 }
555
a561be71 556 err = mnt_want_write_file(filp);
42a74f20
DH
557 if (err)
558 return err;
559 if (get_user(generation, (int __user *) arg)) {
560 err = -EFAULT;
561 goto setversion_out;
562 }
ac27a0ec 563
5955102c 564 inode_lock(inode);
9924a92a 565 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
42a74f20
DH
566 if (IS_ERR(handle)) {
567 err = PTR_ERR(handle);
6c2155b9 568 goto unlock_out;
42a74f20 569 }
617ba13b 570 err = ext4_reserve_inode_write(handle, inode, &iloc);
ac27a0ec 571 if (err == 0) {
eeca7ea1 572 inode->i_ctime = current_time(inode);
ac27a0ec 573 inode->i_generation = generation;
617ba13b 574 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
ac27a0ec 575 }
617ba13b 576 ext4_journal_stop(handle);
6c2155b9
DH
577
578unlock_out:
5955102c 579 inode_unlock(inode);
42a74f20 580setversion_out:
2a79f17e 581 mnt_drop_write_file(filp);
ac27a0ec
DK
582 return err;
583 }
617ba13b
MC
584 case EXT4_IOC_GROUP_EXTEND: {
585 ext4_fsblk_t n_blocks_count;
ac046f1d 586 int err, err2=0;
ac27a0ec 587
8f82f840
YY
588 err = ext4_resize_begin(sb);
589 if (err)
590 return err;
ac27a0ec 591
014a1770
DH
592 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
593 err = -EFAULT;
594 goto group_extend_out;
595 }
ac27a0ec 596
e2b911c5 597 if (ext4_has_feature_bigalloc(sb)) {
bab08ab9
TT
598 ext4_msg(sb, KERN_ERR,
599 "Online resizing not supported with bigalloc");
014a1770
DH
600 err = -EOPNOTSUPP;
601 goto group_extend_out;
bab08ab9
TT
602 }
603
a561be71 604 err = mnt_want_write_file(filp);
42a74f20 605 if (err)
014a1770 606 goto group_extend_out;
42a74f20 607
617ba13b 608 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
ac046f1d
PT
609 if (EXT4_SB(sb)->s_journal) {
610 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
611 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
612 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
613 }
7ffe1ea8
HK
614 if (err == 0)
615 err = err2;
2a79f17e 616 mnt_drop_write_file(filp);
014a1770 617group_extend_out:
8f82f840 618 ext4_resize_end(sb);
ac27a0ec
DK
619 return err;
620 }
748de673
AF
621
622 case EXT4_IOC_MOVE_EXT: {
623 struct move_extent me;
2903ff01
AV
624 struct fd donor;
625 int err;
748de673 626
4a58579b
AF
627 if (!(filp->f_mode & FMODE_READ) ||
628 !(filp->f_mode & FMODE_WRITE))
629 return -EBADF;
630
748de673
AF
631 if (copy_from_user(&me,
632 (struct move_extent __user *)arg, sizeof(me)))
633 return -EFAULT;
4a58579b 634 me.moved_len = 0;
748de673 635
2903ff01
AV
636 donor = fdget(me.donor_fd);
637 if (!donor.file)
748de673
AF
638 return -EBADF;
639
2903ff01 640 if (!(donor.file->f_mode & FMODE_WRITE)) {
4a58579b
AF
641 err = -EBADF;
642 goto mext_out;
748de673
AF
643 }
644
e2b911c5 645 if (ext4_has_feature_bigalloc(sb)) {
bab08ab9
TT
646 ext4_msg(sb, KERN_ERR,
647 "Online defrag not supported with bigalloc");
399c9b86
AV
648 err = -EOPNOTSUPP;
649 goto mext_out;
73f34a5e
RZ
650 } else if (IS_DAX(inode)) {
651 ext4_msg(sb, KERN_ERR,
652 "Online defrag not supported with DAX");
653 err = -EOPNOTSUPP;
654 goto mext_out;
bab08ab9
TT
655 }
656
a561be71 657 err = mnt_want_write_file(filp);
4a58579b
AF
658 if (err)
659 goto mext_out;
660
2903ff01 661 err = ext4_move_extents(filp, donor.file, me.orig_start,
748de673 662 me.donor_start, me.len, &me.moved_len);
2a79f17e 663 mnt_drop_write_file(filp);
748de673 664
60e6679e 665 if (copy_to_user((struct move_extent __user *)arg,
c437b273 666 &me, sizeof(me)))
4a58579b
AF
667 err = -EFAULT;
668mext_out:
2903ff01 669 fdput(donor);
748de673
AF
670 return err;
671 }
672
617ba13b
MC
673 case EXT4_IOC_GROUP_ADD: {
674 struct ext4_new_group_data input;
ac046f1d 675 int err, err2=0;
ac27a0ec 676
8f82f840
YY
677 err = ext4_resize_begin(sb);
678 if (err)
679 return err;
ac27a0ec 680
617ba13b 681 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
014a1770
DH
682 sizeof(input))) {
683 err = -EFAULT;
684 goto group_add_out;
685 }
ac27a0ec 686
e2b911c5 687 if (ext4_has_feature_bigalloc(sb)) {
bab08ab9
TT
688 ext4_msg(sb, KERN_ERR,
689 "Online resizing not supported with bigalloc");
014a1770
DH
690 err = -EOPNOTSUPP;
691 goto group_add_out;
bab08ab9
TT
692 }
693
a561be71 694 err = mnt_want_write_file(filp);
42a74f20 695 if (err)
014a1770 696 goto group_add_out;
42a74f20 697
617ba13b 698 err = ext4_group_add(sb, &input);
ac046f1d
PT
699 if (EXT4_SB(sb)->s_journal) {
700 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
701 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
702 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
703 }
7ffe1ea8
HK
704 if (err == 0)
705 err = err2;
2a79f17e 706 mnt_drop_write_file(filp);
7f511862
TT
707 if (!err && ext4_has_group_desc_csum(sb) &&
708 test_opt(sb, INIT_INODE_TABLE))
709 err = ext4_register_li_request(sb, input.group);
014a1770 710group_add_out:
8f82f840 711 ext4_resize_end(sb);
ac27a0ec
DK
712 return err;
713 }
714
c14c6fd5 715 case EXT4_IOC_MIGRATE:
2a43a878
AK
716 {
717 int err;
2e149670 718 if (!inode_owner_or_capable(inode))
2a43a878
AK
719 return -EACCES;
720
a561be71 721 err = mnt_want_write_file(filp);
2a43a878
AK
722 if (err)
723 return err;
724 /*
725 * inode_mutex prevent write and truncate on the file.
726 * Read still goes through. We take i_data_sem in
727 * ext4_ext_swap_inode_data before we switch the
728 * inode format to prevent read.
729 */
5955102c 730 inode_lock((inode));
2a43a878 731 err = ext4_ext_migrate(inode);
5955102c 732 inode_unlock((inode));
2a79f17e 733 mnt_drop_write_file(filp);
2a43a878
AK
734 return err;
735 }
c14c6fd5 736
ccd2506b
TT
737 case EXT4_IOC_ALLOC_DA_BLKS:
738 {
739 int err;
2e149670 740 if (!inode_owner_or_capable(inode))
ccd2506b
TT
741 return -EACCES;
742
a561be71 743 err = mnt_want_write_file(filp);
ccd2506b
TT
744 if (err)
745 return err;
746 err = ext4_alloc_da_blocks(inode);
2a79f17e 747 mnt_drop_write_file(filp);
ccd2506b
TT
748 return err;
749 }
750
393d1d1d 751 case EXT4_IOC_SWAP_BOOT:
3e67cfad
DM
752 {
753 int err;
393d1d1d
DTB
754 if (!(filp->f_mode & FMODE_WRITE))
755 return -EBADF;
3e67cfad
DM
756 err = mnt_want_write_file(filp);
757 if (err)
758 return err;
759 err = swap_inode_boot_loader(sb, inode);
760 mnt_drop_write_file(filp);
761 return err;
762 }
393d1d1d 763
19c5246d
YY
764 case EXT4_IOC_RESIZE_FS: {
765 ext4_fsblk_t n_blocks_count;
19c5246d 766 int err = 0, err2 = 0;
7f511862 767 ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
19c5246d 768
e2b911c5 769 if (ext4_has_feature_bigalloc(sb)) {
19c5246d
YY
770 ext4_msg(sb, KERN_ERR,
771 "Online resizing not (yet) supported with bigalloc");
772 return -EOPNOTSUPP;
773 }
774
19c5246d
YY
775 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
776 sizeof(__u64))) {
777 return -EFAULT;
778 }
779
19c5246d
YY
780 err = ext4_resize_begin(sb);
781 if (err)
782 return err;
783
8cae6f71 784 err = mnt_want_write_file(filp);
19c5246d
YY
785 if (err)
786 goto resizefs_out;
787
788 err = ext4_resize_fs(sb, n_blocks_count);
789 if (EXT4_SB(sb)->s_journal) {
790 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
791 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
792 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
793 }
794 if (err == 0)
795 err = err2;
8cae6f71 796 mnt_drop_write_file(filp);
7f511862
TT
797 if (!err && (o_group > EXT4_SB(sb)->s_groups_count) &&
798 ext4_has_group_desc_csum(sb) &&
799 test_opt(sb, INIT_INODE_TABLE))
800 err = ext4_register_li_request(sb, o_group);
801
19c5246d
YY
802resizefs_out:
803 ext4_resize_end(sb);
804 return err;
805 }
806
e681c047
LC
807 case FITRIM:
808 {
41431792 809 struct request_queue *q = bdev_get_queue(sb->s_bdev);
e681c047
LC
810 struct fstrim_range range;
811 int ret = 0;
812
813 if (!capable(CAP_SYS_ADMIN))
814 return -EPERM;
815
41431792
LC
816 if (!blk_queue_discard(q))
817 return -EOPNOTSUPP;
818
e6705f7c 819 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
e681c047
LC
820 sizeof(range)))
821 return -EFAULT;
822
5c2ed62f
LC
823 range.minlen = max((unsigned int)range.minlen,
824 q->limits.discard_granularity);
e681c047
LC
825 ret = ext4_trim_fs(sb, &range);
826 if (ret < 0)
827 return ret;
828
e6705f7c 829 if (copy_to_user((struct fstrim_range __user *)arg, &range,
e681c047
LC
830 sizeof(range)))
831 return -EFAULT;
832
833 return 0;
834 }
7869a4a6
TT
835 case EXT4_IOC_PRECACHE_EXTENTS:
836 return ext4_ext_precache(inode);
9bd8212f 837
db717d8e 838 case EXT4_IOC_SET_ENCRYPTION_POLICY:
9a200d07
RW
839 if (!ext4_has_feature_encrypt(sb))
840 return -EOPNOTSUPP;
db717d8e 841 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
9a200d07 842
9bd8212f 843 case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
ba679017 844#ifdef CONFIG_EXT4_FS_ENCRYPTION
9bd8212f
MH
845 int err, err2;
846 struct ext4_sb_info *sbi = EXT4_SB(sb);
847 handle_t *handle;
848
35997d1c 849 if (!ext4_has_feature_encrypt(sb))
9bd8212f
MH
850 return -EOPNOTSUPP;
851 if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
852 err = mnt_want_write_file(filp);
853 if (err)
854 return err;
855 handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
856 if (IS_ERR(handle)) {
857 err = PTR_ERR(handle);
858 goto pwsalt_err_exit;
859 }
860 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
861 if (err)
862 goto pwsalt_err_journal;
863 generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
864 err = ext4_handle_dirty_metadata(handle, NULL,
865 sbi->s_sbh);
866 pwsalt_err_journal:
867 err2 = ext4_journal_stop(handle);
868 if (err2 && !err)
869 err = err2;
870 pwsalt_err_exit:
871 mnt_drop_write_file(filp);
872 if (err)
873 return err;
874 }
b4ab9e29
FF
875 if (copy_to_user((void __user *) arg,
876 sbi->s_es->s_encrypt_pw_salt, 16))
9bd8212f
MH
877 return -EFAULT;
878 return 0;
ba679017
EB
879#else
880 return -EOPNOTSUPP;
881#endif
9bd8212f 882 }
db717d8e
EB
883 case EXT4_IOC_GET_ENCRYPTION_POLICY:
884 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
9bd8212f 885
9b7365fc
LX
886 case EXT4_IOC_FSGETXATTR:
887 {
888 struct fsxattr fa;
889
890 memset(&fa, 0, sizeof(struct fsxattr));
891 ext4_get_inode_flags(ei);
892 fa.fsx_xflags = ext4_iflags_to_xflags(ei->i_flags & EXT4_FL_USER_VISIBLE);
893
0b7b7779 894 if (ext4_has_feature_project(inode->i_sb)) {
9b7365fc
LX
895 fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
896 EXT4_I(inode)->i_projid);
897 }
898
899 if (copy_to_user((struct fsxattr __user *)arg,
900 &fa, sizeof(fa)))
901 return -EFAULT;
902 return 0;
903 }
904 case EXT4_IOC_FSSETXATTR:
905 {
906 struct fsxattr fa;
907 int err;
908
909 if (copy_from_user(&fa, (struct fsxattr __user *)arg,
910 sizeof(fa)))
911 return -EFAULT;
912
913 /* Make sure caller has proper permission */
914 if (!inode_owner_or_capable(inode))
915 return -EACCES;
916
d14e7683
JK
917 if (fa.fsx_xflags & ~EXT4_SUPPORTED_FS_XFLAGS)
918 return -EOPNOTSUPP;
919
920 flags = ext4_xflags_to_iflags(fa.fsx_xflags);
921 if (ext4_mask_flags(inode->i_mode, flags) != flags)
922 return -EOPNOTSUPP;
923
9b7365fc
LX
924 err = mnt_want_write_file(filp);
925 if (err)
926 return err;
927
5955102c 928 inode_lock(inode);
9b7365fc
LX
929 flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) |
930 (flags & EXT4_FL_XFLAG_VISIBLE);
931 err = ext4_ioctl_setflags(inode, flags);
5955102c 932 inode_unlock(inode);
9b7365fc
LX
933 mnt_drop_write_file(filp);
934 if (err)
935 return err;
936
937 err = ext4_ioctl_setproject(filp, fa.fsx_projid);
938 if (err)
939 return err;
940
941 return 0;
942 }
e9be2ac7
TT
943 case EXT4_IOC_SHUTDOWN:
944 return ext4_shutdown(sb, arg);
ac27a0ec
DK
945 default:
946 return -ENOTTY;
947 }
948}
949
950#ifdef CONFIG_COMPAT
617ba13b 951long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
ac27a0ec 952{
ac27a0ec
DK
953 /* These are just misnamed, they actually get/put from/to user an int */
954 switch (cmd) {
617ba13b
MC
955 case EXT4_IOC32_GETFLAGS:
956 cmd = EXT4_IOC_GETFLAGS;
ac27a0ec 957 break;
617ba13b
MC
958 case EXT4_IOC32_SETFLAGS:
959 cmd = EXT4_IOC_SETFLAGS;
ac27a0ec 960 break;
617ba13b
MC
961 case EXT4_IOC32_GETVERSION:
962 cmd = EXT4_IOC_GETVERSION;
ac27a0ec 963 break;
617ba13b
MC
964 case EXT4_IOC32_SETVERSION:
965 cmd = EXT4_IOC_SETVERSION;
ac27a0ec 966 break;
617ba13b
MC
967 case EXT4_IOC32_GROUP_EXTEND:
968 cmd = EXT4_IOC_GROUP_EXTEND;
ac27a0ec 969 break;
617ba13b
MC
970 case EXT4_IOC32_GETVERSION_OLD:
971 cmd = EXT4_IOC_GETVERSION_OLD;
ac27a0ec 972 break;
617ba13b
MC
973 case EXT4_IOC32_SETVERSION_OLD:
974 cmd = EXT4_IOC_SETVERSION_OLD;
ac27a0ec 975 break;
617ba13b
MC
976 case EXT4_IOC32_GETRSVSZ:
977 cmd = EXT4_IOC_GETRSVSZ;
ac27a0ec 978 break;
617ba13b
MC
979 case EXT4_IOC32_SETRSVSZ:
980 cmd = EXT4_IOC_SETRSVSZ;
ac27a0ec 981 break;
4d92dc0f
BH
982 case EXT4_IOC32_GROUP_ADD: {
983 struct compat_ext4_new_group_input __user *uinput;
984 struct ext4_new_group_input input;
985 mm_segment_t old_fs;
986 int err;
987
988 uinput = compat_ptr(arg);
989 err = get_user(input.group, &uinput->group);
990 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
991 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
992 err |= get_user(input.inode_table, &uinput->inode_table);
993 err |= get_user(input.blocks_count, &uinput->blocks_count);
994 err |= get_user(input.reserved_blocks,
995 &uinput->reserved_blocks);
996 if (err)
997 return -EFAULT;
998 old_fs = get_fs();
999 set_fs(KERNEL_DS);
1000 err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
1001 (unsigned long) &input);
1002 set_fs(old_fs);
1003 return err;
1004 }
b684b2ee 1005 case EXT4_IOC_MOVE_EXT:
19c5246d 1006 case EXT4_IOC_RESIZE_FS:
7869a4a6 1007 case EXT4_IOC_PRECACHE_EXTENTS:
9bd8212f
MH
1008 case EXT4_IOC_SET_ENCRYPTION_POLICY:
1009 case EXT4_IOC_GET_ENCRYPTION_PWSALT:
1010 case EXT4_IOC_GET_ENCRYPTION_POLICY:
e9be2ac7 1011 case EXT4_IOC_SHUTDOWN:
b684b2ee 1012 break;
ac27a0ec
DK
1013 default:
1014 return -ENOIOCTLCMD;
1015 }
5cdd7b2d 1016 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
ac27a0ec
DK
1017}
1018#endif