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