perf bpf: Move perf_event_output() from stdio.h to bpf.h
[linux/fpc-iii.git] / fs / ext4 / ioctl.c
blob0edee31913d1fcccdc3b326aabeeb403fadcca75
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * linux/fs/ext4/ioctl.c
5 * Copyright (C) 1993, 1994, 1995
6 * Remy Card (card@masi.ibp.fr)
7 * Laboratoire MASI - Institut Blaise Pascal
8 * Universite Pierre et Marie Curie (Paris VI)
9 */
11 #include <linux/fs.h>
12 #include <linux/capability.h>
13 #include <linux/time.h>
14 #include <linux/compat.h>
15 #include <linux/mount.h>
16 #include <linux/file.h>
17 #include <linux/quotaops.h>
18 #include <linux/random.h>
19 #include <linux/uuid.h>
20 #include <linux/uaccess.h>
21 #include <linux/delay.h>
22 #include <linux/iversion.h>
23 #include "ext4_jbd2.h"
24 #include "ext4.h"
25 #include <linux/fsmap.h>
26 #include "fsmap.h"
27 #include <trace/events/ext4.h>
29 /**
30 * Swap memory between @a and @b for @len bytes.
32 * @a: pointer to first memory area
33 * @b: pointer to second memory area
34 * @len: number of bytes to swap
37 static void memswap(void *a, void *b, size_t len)
39 unsigned char *ap, *bp;
41 ap = (unsigned char *)a;
42 bp = (unsigned char *)b;
43 while (len-- > 0) {
44 swap(*ap, *bp);
45 ap++;
46 bp++;
50 /**
51 * Swap i_data and associated attributes between @inode1 and @inode2.
52 * This function is used for the primary swap between inode1 and inode2
53 * and also to revert this primary swap in case of errors.
55 * Therefore you have to make sure, that calling this method twice
56 * will revert all changes.
58 * @inode1: pointer to first inode
59 * @inode2: pointer to second inode
61 static void swap_inode_data(struct inode *inode1, struct inode *inode2)
63 loff_t isize;
64 struct ext4_inode_info *ei1;
65 struct ext4_inode_info *ei2;
67 ei1 = EXT4_I(inode1);
68 ei2 = EXT4_I(inode2);
70 swap(inode1->i_version, inode2->i_version);
71 swap(inode1->i_blocks, inode2->i_blocks);
72 swap(inode1->i_bytes, inode2->i_bytes);
73 swap(inode1->i_atime, inode2->i_atime);
74 swap(inode1->i_mtime, inode2->i_mtime);
76 memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data));
77 swap(ei1->i_flags, ei2->i_flags);
78 swap(ei1->i_disksize, ei2->i_disksize);
79 ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS);
80 ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS);
82 isize = i_size_read(inode1);
83 i_size_write(inode1, i_size_read(inode2));
84 i_size_write(inode2, isize);
87 static void reset_inode_seed(struct inode *inode)
89 struct ext4_inode_info *ei = EXT4_I(inode);
90 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
91 __le32 inum = cpu_to_le32(inode->i_ino);
92 __le32 gen = cpu_to_le32(inode->i_generation);
93 __u32 csum;
95 if (!ext4_has_metadata_csum(inode->i_sb))
96 return;
98 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, sizeof(inum));
99 ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, sizeof(gen));
103 * Swap the information from the given @inode and the inode
104 * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
105 * important fields of the inodes.
107 * @sb: the super block of the filesystem
108 * @inode: the inode to swap with EXT4_BOOT_LOADER_INO
111 static long swap_inode_boot_loader(struct super_block *sb,
112 struct inode *inode)
114 handle_t *handle;
115 int err;
116 struct inode *inode_bl;
117 struct ext4_inode_info *ei_bl;
119 if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode) ||
120 IS_SWAPFILE(inode) || IS_ENCRYPTED(inode) ||
121 ext4_has_inline_data(inode))
122 return -EINVAL;
124 if (IS_RDONLY(inode) || IS_APPEND(inode) || IS_IMMUTABLE(inode) ||
125 !inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN))
126 return -EPERM;
128 inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO);
129 if (IS_ERR(inode_bl))
130 return PTR_ERR(inode_bl);
131 ei_bl = EXT4_I(inode_bl);
133 filemap_flush(inode->i_mapping);
134 filemap_flush(inode_bl->i_mapping);
136 /* Protect orig inodes against a truncate and make sure,
137 * that only 1 swap_inode_boot_loader is running. */
138 lock_two_nondirectories(inode, inode_bl);
140 /* Wait for all existing dio workers */
141 inode_dio_wait(inode);
142 inode_dio_wait(inode_bl);
144 truncate_inode_pages(&inode->i_data, 0);
145 truncate_inode_pages(&inode_bl->i_data, 0);
147 handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
148 if (IS_ERR(handle)) {
149 err = -EINVAL;
150 goto journal_err_out;
153 /* Protect extent tree against block allocations via delalloc */
154 ext4_double_down_write_data_sem(inode, inode_bl);
156 if (inode_bl->i_nlink == 0) {
157 /* this inode has never been used as a BOOT_LOADER */
158 set_nlink(inode_bl, 1);
159 i_uid_write(inode_bl, 0);
160 i_gid_write(inode_bl, 0);
161 inode_bl->i_flags = 0;
162 ei_bl->i_flags = 0;
163 inode_set_iversion(inode_bl, 1);
164 i_size_write(inode_bl, 0);
165 inode_bl->i_mode = S_IFREG;
166 if (ext4_has_feature_extents(sb)) {
167 ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
168 ext4_ext_tree_init(handle, inode_bl);
169 } else
170 memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
173 swap_inode_data(inode, inode_bl);
175 inode->i_ctime = inode_bl->i_ctime = current_time(inode);
177 inode->i_generation = prandom_u32();
178 inode_bl->i_generation = prandom_u32();
179 reset_inode_seed(inode);
180 reset_inode_seed(inode_bl);
182 ext4_discard_preallocations(inode);
184 err = ext4_mark_inode_dirty(handle, inode);
185 if (err < 0) {
186 ext4_warning(inode->i_sb,
187 "couldn't mark inode #%lu dirty (err %d)",
188 inode->i_ino, err);
189 /* Revert all changes: */
190 swap_inode_data(inode, inode_bl);
191 ext4_mark_inode_dirty(handle, inode);
192 } else {
193 err = ext4_mark_inode_dirty(handle, inode_bl);
194 if (err < 0) {
195 ext4_warning(inode_bl->i_sb,
196 "couldn't mark inode #%lu dirty (err %d)",
197 inode_bl->i_ino, err);
198 /* Revert all changes: */
199 swap_inode_data(inode, inode_bl);
200 ext4_mark_inode_dirty(handle, inode);
201 ext4_mark_inode_dirty(handle, inode_bl);
204 ext4_journal_stop(handle);
205 ext4_double_up_write_data_sem(inode, inode_bl);
207 journal_err_out:
208 unlock_two_nondirectories(inode, inode_bl);
209 iput(inode_bl);
210 return err;
213 #ifdef CONFIG_EXT4_FS_ENCRYPTION
214 static int uuid_is_zero(__u8 u[16])
216 int i;
218 for (i = 0; i < 16; i++)
219 if (u[i])
220 return 0;
221 return 1;
223 #endif
225 static int ext4_ioctl_setflags(struct inode *inode,
226 unsigned int flags)
228 struct ext4_inode_info *ei = EXT4_I(inode);
229 handle_t *handle = NULL;
230 int err = -EPERM, migrate = 0;
231 struct ext4_iloc iloc;
232 unsigned int oldflags, mask, i;
233 unsigned int jflag;
235 /* Is it quota file? Do not allow user to mess with it */
236 if (ext4_is_quota_file(inode))
237 goto flags_out;
239 oldflags = ei->i_flags;
241 /* The JOURNAL_DATA flag is modifiable only by root */
242 jflag = flags & EXT4_JOURNAL_DATA_FL;
245 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
246 * the relevant capability.
248 * This test looks nicer. Thanks to Pauline Middelink
250 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
251 if (!capable(CAP_LINUX_IMMUTABLE))
252 goto flags_out;
256 * The JOURNAL_DATA flag can only be changed by
257 * the relevant capability.
259 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
260 if (!capable(CAP_SYS_RESOURCE))
261 goto flags_out;
263 if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
264 migrate = 1;
266 if (flags & EXT4_EOFBLOCKS_FL) {
267 /* we don't support adding EOFBLOCKS flag */
268 if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
269 err = -EOPNOTSUPP;
270 goto flags_out;
272 } else if (oldflags & EXT4_EOFBLOCKS_FL) {
273 err = ext4_truncate(inode);
274 if (err)
275 goto flags_out;
278 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
279 if (IS_ERR(handle)) {
280 err = PTR_ERR(handle);
281 goto flags_out;
283 if (IS_SYNC(inode))
284 ext4_handle_sync(handle);
285 err = ext4_reserve_inode_write(handle, inode, &iloc);
286 if (err)
287 goto flags_err;
289 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
290 if (!(mask & EXT4_FL_USER_MODIFIABLE))
291 continue;
292 /* These flags get special treatment later */
293 if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL)
294 continue;
295 if (mask & flags)
296 ext4_set_inode_flag(inode, i);
297 else
298 ext4_clear_inode_flag(inode, i);
301 ext4_set_inode_flags(inode);
302 inode->i_ctime = current_time(inode);
304 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
305 flags_err:
306 ext4_journal_stop(handle);
307 if (err)
308 goto flags_out;
310 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
312 * Changes to the journaling mode can cause unsafe changes to
313 * S_DAX if we are using the DAX mount option.
315 if (test_opt(inode->i_sb, DAX)) {
316 err = -EBUSY;
317 goto flags_out;
320 err = ext4_change_inode_journal_flag(inode, jflag);
321 if (err)
322 goto flags_out;
324 if (migrate) {
325 if (flags & EXT4_EXTENTS_FL)
326 err = ext4_ext_migrate(inode);
327 else
328 err = ext4_ind_migrate(inode);
331 flags_out:
332 return err;
335 #ifdef CONFIG_QUOTA
336 static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
338 struct inode *inode = file_inode(filp);
339 struct super_block *sb = inode->i_sb;
340 struct ext4_inode_info *ei = EXT4_I(inode);
341 int err, rc;
342 handle_t *handle;
343 kprojid_t kprojid;
344 struct ext4_iloc iloc;
345 struct ext4_inode *raw_inode;
346 struct dquot *transfer_to[MAXQUOTAS] = { };
348 if (!ext4_has_feature_project(sb)) {
349 if (projid != EXT4_DEF_PROJID)
350 return -EOPNOTSUPP;
351 else
352 return 0;
355 if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
356 return -EOPNOTSUPP;
358 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
360 if (projid_eq(kprojid, EXT4_I(inode)->i_projid))
361 return 0;
363 err = -EPERM;
364 /* Is it quota file? Do not allow user to mess with it */
365 if (ext4_is_quota_file(inode))
366 return err;
368 err = ext4_get_inode_loc(inode, &iloc);
369 if (err)
370 return err;
372 raw_inode = ext4_raw_inode(&iloc);
373 if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
374 err = ext4_expand_extra_isize(inode,
375 EXT4_SB(sb)->s_want_extra_isize,
376 &iloc);
377 if (err)
378 return err;
379 } else {
380 brelse(iloc.bh);
383 err = dquot_initialize(inode);
384 if (err)
385 return err;
387 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
388 EXT4_QUOTA_INIT_BLOCKS(sb) +
389 EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
390 if (IS_ERR(handle))
391 return PTR_ERR(handle);
393 err = ext4_reserve_inode_write(handle, inode, &iloc);
394 if (err)
395 goto out_stop;
397 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
398 if (!IS_ERR(transfer_to[PRJQUOTA])) {
400 /* __dquot_transfer() calls back ext4_get_inode_usage() which
401 * counts xattr inode references.
403 down_read(&EXT4_I(inode)->xattr_sem);
404 err = __dquot_transfer(inode, transfer_to);
405 up_read(&EXT4_I(inode)->xattr_sem);
406 dqput(transfer_to[PRJQUOTA]);
407 if (err)
408 goto out_dirty;
411 EXT4_I(inode)->i_projid = kprojid;
412 inode->i_ctime = current_time(inode);
413 out_dirty:
414 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
415 if (!err)
416 err = rc;
417 out_stop:
418 ext4_journal_stop(handle);
419 return err;
421 #else
422 static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
424 if (projid != EXT4_DEF_PROJID)
425 return -EOPNOTSUPP;
426 return 0;
428 #endif
430 /* Transfer internal flags to xflags */
431 static inline __u32 ext4_iflags_to_xflags(unsigned long iflags)
433 __u32 xflags = 0;
435 if (iflags & EXT4_SYNC_FL)
436 xflags |= FS_XFLAG_SYNC;
437 if (iflags & EXT4_IMMUTABLE_FL)
438 xflags |= FS_XFLAG_IMMUTABLE;
439 if (iflags & EXT4_APPEND_FL)
440 xflags |= FS_XFLAG_APPEND;
441 if (iflags & EXT4_NODUMP_FL)
442 xflags |= FS_XFLAG_NODUMP;
443 if (iflags & EXT4_NOATIME_FL)
444 xflags |= FS_XFLAG_NOATIME;
445 if (iflags & EXT4_PROJINHERIT_FL)
446 xflags |= FS_XFLAG_PROJINHERIT;
447 return xflags;
450 #define EXT4_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
451 FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
452 FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
454 /* Transfer xflags flags to internal */
455 static inline unsigned long ext4_xflags_to_iflags(__u32 xflags)
457 unsigned long iflags = 0;
459 if (xflags & FS_XFLAG_SYNC)
460 iflags |= EXT4_SYNC_FL;
461 if (xflags & FS_XFLAG_IMMUTABLE)
462 iflags |= EXT4_IMMUTABLE_FL;
463 if (xflags & FS_XFLAG_APPEND)
464 iflags |= EXT4_APPEND_FL;
465 if (xflags & FS_XFLAG_NODUMP)
466 iflags |= EXT4_NODUMP_FL;
467 if (xflags & FS_XFLAG_NOATIME)
468 iflags |= EXT4_NOATIME_FL;
469 if (xflags & FS_XFLAG_PROJINHERIT)
470 iflags |= EXT4_PROJINHERIT_FL;
472 return iflags;
475 static int ext4_shutdown(struct super_block *sb, unsigned long arg)
477 struct ext4_sb_info *sbi = EXT4_SB(sb);
478 __u32 flags;
480 if (!capable(CAP_SYS_ADMIN))
481 return -EPERM;
483 if (get_user(flags, (__u32 __user *)arg))
484 return -EFAULT;
486 if (flags > EXT4_GOING_FLAGS_NOLOGFLUSH)
487 return -EINVAL;
489 if (ext4_forced_shutdown(sbi))
490 return 0;
492 ext4_msg(sb, KERN_ALERT, "shut down requested (%d)", flags);
493 trace_ext4_shutdown(sb, flags);
495 switch (flags) {
496 case EXT4_GOING_FLAGS_DEFAULT:
497 freeze_bdev(sb->s_bdev);
498 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
499 thaw_bdev(sb->s_bdev, sb);
500 break;
501 case EXT4_GOING_FLAGS_LOGFLUSH:
502 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
503 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) {
504 (void) ext4_force_commit(sb);
505 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
507 break;
508 case EXT4_GOING_FLAGS_NOLOGFLUSH:
509 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
510 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal))
511 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
512 break;
513 default:
514 return -EINVAL;
516 clear_opt(sb, DISCARD);
517 return 0;
520 struct getfsmap_info {
521 struct super_block *gi_sb;
522 struct fsmap_head __user *gi_data;
523 unsigned int gi_idx;
524 __u32 gi_last_flags;
527 static int ext4_getfsmap_format(struct ext4_fsmap *xfm, void *priv)
529 struct getfsmap_info *info = priv;
530 struct fsmap fm;
532 trace_ext4_getfsmap_mapping(info->gi_sb, xfm);
534 info->gi_last_flags = xfm->fmr_flags;
535 ext4_fsmap_from_internal(info->gi_sb, &fm, xfm);
536 if (copy_to_user(&info->gi_data->fmh_recs[info->gi_idx++], &fm,
537 sizeof(struct fsmap)))
538 return -EFAULT;
540 return 0;
543 static int ext4_ioc_getfsmap(struct super_block *sb,
544 struct fsmap_head __user *arg)
546 struct getfsmap_info info = {0};
547 struct ext4_fsmap_head xhead = {0};
548 struct fsmap_head head;
549 bool aborted = false;
550 int error;
552 if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
553 return -EFAULT;
554 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
555 memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
556 sizeof(head.fmh_keys[0].fmr_reserved)) ||
557 memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
558 sizeof(head.fmh_keys[1].fmr_reserved)))
559 return -EINVAL;
561 * ext4 doesn't report file extents at all, so the only valid
562 * file offsets are the magic ones (all zeroes or all ones).
564 if (head.fmh_keys[0].fmr_offset ||
565 (head.fmh_keys[1].fmr_offset != 0 &&
566 head.fmh_keys[1].fmr_offset != -1ULL))
567 return -EINVAL;
569 xhead.fmh_iflags = head.fmh_iflags;
570 xhead.fmh_count = head.fmh_count;
571 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[0], &head.fmh_keys[0]);
572 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[1], &head.fmh_keys[1]);
574 trace_ext4_getfsmap_low_key(sb, &xhead.fmh_keys[0]);
575 trace_ext4_getfsmap_high_key(sb, &xhead.fmh_keys[1]);
577 info.gi_sb = sb;
578 info.gi_data = arg;
579 error = ext4_getfsmap(sb, &xhead, ext4_getfsmap_format, &info);
580 if (error == EXT4_QUERY_RANGE_ABORT) {
581 error = 0;
582 aborted = true;
583 } else if (error)
584 return error;
586 /* If we didn't abort, set the "last" flag in the last fmx */
587 if (!aborted && info.gi_idx) {
588 info.gi_last_flags |= FMR_OF_LAST;
589 if (copy_to_user(&info.gi_data->fmh_recs[info.gi_idx - 1].fmr_flags,
590 &info.gi_last_flags,
591 sizeof(info.gi_last_flags)))
592 return -EFAULT;
595 /* copy back header */
596 head.fmh_entries = xhead.fmh_entries;
597 head.fmh_oflags = xhead.fmh_oflags;
598 if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
599 return -EFAULT;
601 return 0;
604 static long ext4_ioctl_group_add(struct file *file,
605 struct ext4_new_group_data *input)
607 struct super_block *sb = file_inode(file)->i_sb;
608 int err, err2=0;
610 err = ext4_resize_begin(sb);
611 if (err)
612 return err;
614 if (ext4_has_feature_bigalloc(sb)) {
615 ext4_msg(sb, KERN_ERR,
616 "Online resizing not supported with bigalloc");
617 err = -EOPNOTSUPP;
618 goto group_add_out;
621 err = mnt_want_write_file(file);
622 if (err)
623 goto group_add_out;
625 err = ext4_group_add(sb, input);
626 if (EXT4_SB(sb)->s_journal) {
627 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
628 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
629 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
631 if (err == 0)
632 err = err2;
633 mnt_drop_write_file(file);
634 if (!err && ext4_has_group_desc_csum(sb) &&
635 test_opt(sb, INIT_INODE_TABLE))
636 err = ext4_register_li_request(sb, input->group);
637 group_add_out:
638 ext4_resize_end(sb);
639 return err;
642 static int ext4_ioctl_check_project(struct inode *inode, struct fsxattr *fa)
645 * Project Quota ID state is only allowed to change from within the init
646 * namespace. Enforce that restriction only if we are trying to change
647 * the quota ID state. Everything else is allowed in user namespaces.
649 if (current_user_ns() == &init_user_ns)
650 return 0;
652 if (__kprojid_val(EXT4_I(inode)->i_projid) != fa->fsx_projid)
653 return -EINVAL;
655 if (ext4_test_inode_flag(inode, EXT4_INODE_PROJINHERIT)) {
656 if (!(fa->fsx_xflags & FS_XFLAG_PROJINHERIT))
657 return -EINVAL;
658 } else {
659 if (fa->fsx_xflags & FS_XFLAG_PROJINHERIT)
660 return -EINVAL;
663 return 0;
666 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
668 struct inode *inode = file_inode(filp);
669 struct super_block *sb = inode->i_sb;
670 struct ext4_inode_info *ei = EXT4_I(inode);
671 unsigned int flags;
673 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
675 switch (cmd) {
676 case FS_IOC_GETFSMAP:
677 return ext4_ioc_getfsmap(sb, (void __user *)arg);
678 case EXT4_IOC_GETFLAGS:
679 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
680 return put_user(flags, (int __user *) arg);
681 case EXT4_IOC_SETFLAGS: {
682 int err;
684 if (!inode_owner_or_capable(inode))
685 return -EACCES;
687 if (get_user(flags, (int __user *) arg))
688 return -EFAULT;
690 if (flags & ~EXT4_FL_USER_VISIBLE)
691 return -EOPNOTSUPP;
693 * chattr(1) grabs flags via GETFLAGS, modifies the result and
694 * passes that to SETFLAGS. So we cannot easily make SETFLAGS
695 * more restrictive than just silently masking off visible but
696 * not settable flags as we always did.
698 flags &= EXT4_FL_USER_MODIFIABLE;
699 if (ext4_mask_flags(inode->i_mode, flags) != flags)
700 return -EOPNOTSUPP;
702 err = mnt_want_write_file(filp);
703 if (err)
704 return err;
706 inode_lock(inode);
707 err = ext4_ioctl_setflags(inode, flags);
708 inode_unlock(inode);
709 mnt_drop_write_file(filp);
710 return err;
712 case EXT4_IOC_GETVERSION:
713 case EXT4_IOC_GETVERSION_OLD:
714 return put_user(inode->i_generation, (int __user *) arg);
715 case EXT4_IOC_SETVERSION:
716 case EXT4_IOC_SETVERSION_OLD: {
717 handle_t *handle;
718 struct ext4_iloc iloc;
719 __u32 generation;
720 int err;
722 if (!inode_owner_or_capable(inode))
723 return -EPERM;
725 if (ext4_has_metadata_csum(inode->i_sb)) {
726 ext4_warning(sb, "Setting inode version is not "
727 "supported with metadata_csum enabled.");
728 return -ENOTTY;
731 err = mnt_want_write_file(filp);
732 if (err)
733 return err;
734 if (get_user(generation, (int __user *) arg)) {
735 err = -EFAULT;
736 goto setversion_out;
739 inode_lock(inode);
740 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
741 if (IS_ERR(handle)) {
742 err = PTR_ERR(handle);
743 goto unlock_out;
745 err = ext4_reserve_inode_write(handle, inode, &iloc);
746 if (err == 0) {
747 inode->i_ctime = current_time(inode);
748 inode->i_generation = generation;
749 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
751 ext4_journal_stop(handle);
753 unlock_out:
754 inode_unlock(inode);
755 setversion_out:
756 mnt_drop_write_file(filp);
757 return err;
759 case EXT4_IOC_GROUP_EXTEND: {
760 ext4_fsblk_t n_blocks_count;
761 int err, err2=0;
763 err = ext4_resize_begin(sb);
764 if (err)
765 return err;
767 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
768 err = -EFAULT;
769 goto group_extend_out;
772 if (ext4_has_feature_bigalloc(sb)) {
773 ext4_msg(sb, KERN_ERR,
774 "Online resizing not supported with bigalloc");
775 err = -EOPNOTSUPP;
776 goto group_extend_out;
779 err = mnt_want_write_file(filp);
780 if (err)
781 goto group_extend_out;
783 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
784 if (EXT4_SB(sb)->s_journal) {
785 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
786 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
787 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
789 if (err == 0)
790 err = err2;
791 mnt_drop_write_file(filp);
792 group_extend_out:
793 ext4_resize_end(sb);
794 return err;
797 case EXT4_IOC_MOVE_EXT: {
798 struct move_extent me;
799 struct fd donor;
800 int err;
802 if (!(filp->f_mode & FMODE_READ) ||
803 !(filp->f_mode & FMODE_WRITE))
804 return -EBADF;
806 if (copy_from_user(&me,
807 (struct move_extent __user *)arg, sizeof(me)))
808 return -EFAULT;
809 me.moved_len = 0;
811 donor = fdget(me.donor_fd);
812 if (!donor.file)
813 return -EBADF;
815 if (!(donor.file->f_mode & FMODE_WRITE)) {
816 err = -EBADF;
817 goto mext_out;
820 if (ext4_has_feature_bigalloc(sb)) {
821 ext4_msg(sb, KERN_ERR,
822 "Online defrag not supported with bigalloc");
823 err = -EOPNOTSUPP;
824 goto mext_out;
825 } else if (IS_DAX(inode)) {
826 ext4_msg(sb, KERN_ERR,
827 "Online defrag not supported with DAX");
828 err = -EOPNOTSUPP;
829 goto mext_out;
832 err = mnt_want_write_file(filp);
833 if (err)
834 goto mext_out;
836 err = ext4_move_extents(filp, donor.file, me.orig_start,
837 me.donor_start, me.len, &me.moved_len);
838 mnt_drop_write_file(filp);
840 if (copy_to_user((struct move_extent __user *)arg,
841 &me, sizeof(me)))
842 err = -EFAULT;
843 mext_out:
844 fdput(donor);
845 return err;
848 case EXT4_IOC_GROUP_ADD: {
849 struct ext4_new_group_data input;
851 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
852 sizeof(input)))
853 return -EFAULT;
855 return ext4_ioctl_group_add(filp, &input);
858 case EXT4_IOC_MIGRATE:
860 int err;
861 if (!inode_owner_or_capable(inode))
862 return -EACCES;
864 err = mnt_want_write_file(filp);
865 if (err)
866 return err;
868 * inode_mutex prevent write and truncate on the file.
869 * Read still goes through. We take i_data_sem in
870 * ext4_ext_swap_inode_data before we switch the
871 * inode format to prevent read.
873 inode_lock((inode));
874 err = ext4_ext_migrate(inode);
875 inode_unlock((inode));
876 mnt_drop_write_file(filp);
877 return err;
880 case EXT4_IOC_ALLOC_DA_BLKS:
882 int err;
883 if (!inode_owner_or_capable(inode))
884 return -EACCES;
886 err = mnt_want_write_file(filp);
887 if (err)
888 return err;
889 err = ext4_alloc_da_blocks(inode);
890 mnt_drop_write_file(filp);
891 return err;
894 case EXT4_IOC_SWAP_BOOT:
896 int err;
897 if (!(filp->f_mode & FMODE_WRITE))
898 return -EBADF;
899 err = mnt_want_write_file(filp);
900 if (err)
901 return err;
902 err = swap_inode_boot_loader(sb, inode);
903 mnt_drop_write_file(filp);
904 return err;
907 case EXT4_IOC_RESIZE_FS: {
908 ext4_fsblk_t n_blocks_count;
909 int err = 0, err2 = 0;
910 ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
912 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
913 sizeof(__u64))) {
914 return -EFAULT;
917 err = ext4_resize_begin(sb);
918 if (err)
919 return err;
921 err = mnt_want_write_file(filp);
922 if (err)
923 goto resizefs_out;
925 err = ext4_resize_fs(sb, n_blocks_count);
926 if (EXT4_SB(sb)->s_journal) {
927 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
928 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
929 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
931 if (err == 0)
932 err = err2;
933 mnt_drop_write_file(filp);
934 if (!err && (o_group > EXT4_SB(sb)->s_groups_count) &&
935 ext4_has_group_desc_csum(sb) &&
936 test_opt(sb, INIT_INODE_TABLE))
937 err = ext4_register_li_request(sb, o_group);
939 resizefs_out:
940 ext4_resize_end(sb);
941 return err;
944 case FITRIM:
946 struct request_queue *q = bdev_get_queue(sb->s_bdev);
947 struct fstrim_range range;
948 int ret = 0;
950 if (!capable(CAP_SYS_ADMIN))
951 return -EPERM;
953 if (!blk_queue_discard(q))
954 return -EOPNOTSUPP;
956 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
957 sizeof(range)))
958 return -EFAULT;
960 range.minlen = max((unsigned int)range.minlen,
961 q->limits.discard_granularity);
962 ret = ext4_trim_fs(sb, &range);
963 if (ret < 0)
964 return ret;
966 if (copy_to_user((struct fstrim_range __user *)arg, &range,
967 sizeof(range)))
968 return -EFAULT;
970 return 0;
972 case EXT4_IOC_PRECACHE_EXTENTS:
973 return ext4_ext_precache(inode);
975 case EXT4_IOC_SET_ENCRYPTION_POLICY:
976 if (!ext4_has_feature_encrypt(sb))
977 return -EOPNOTSUPP;
978 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
980 case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
981 #ifdef CONFIG_EXT4_FS_ENCRYPTION
982 int err, err2;
983 struct ext4_sb_info *sbi = EXT4_SB(sb);
984 handle_t *handle;
986 if (!ext4_has_feature_encrypt(sb))
987 return -EOPNOTSUPP;
988 if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
989 err = mnt_want_write_file(filp);
990 if (err)
991 return err;
992 handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
993 if (IS_ERR(handle)) {
994 err = PTR_ERR(handle);
995 goto pwsalt_err_exit;
997 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
998 if (err)
999 goto pwsalt_err_journal;
1000 generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
1001 err = ext4_handle_dirty_metadata(handle, NULL,
1002 sbi->s_sbh);
1003 pwsalt_err_journal:
1004 err2 = ext4_journal_stop(handle);
1005 if (err2 && !err)
1006 err = err2;
1007 pwsalt_err_exit:
1008 mnt_drop_write_file(filp);
1009 if (err)
1010 return err;
1012 if (copy_to_user((void __user *) arg,
1013 sbi->s_es->s_encrypt_pw_salt, 16))
1014 return -EFAULT;
1015 return 0;
1016 #else
1017 return -EOPNOTSUPP;
1018 #endif
1020 case EXT4_IOC_GET_ENCRYPTION_POLICY:
1021 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
1023 case EXT4_IOC_FSGETXATTR:
1025 struct fsxattr fa;
1027 memset(&fa, 0, sizeof(struct fsxattr));
1028 fa.fsx_xflags = ext4_iflags_to_xflags(ei->i_flags & EXT4_FL_USER_VISIBLE);
1030 if (ext4_has_feature_project(inode->i_sb)) {
1031 fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
1032 EXT4_I(inode)->i_projid);
1035 if (copy_to_user((struct fsxattr __user *)arg,
1036 &fa, sizeof(fa)))
1037 return -EFAULT;
1038 return 0;
1040 case EXT4_IOC_FSSETXATTR:
1042 struct fsxattr fa;
1043 int err;
1045 if (copy_from_user(&fa, (struct fsxattr __user *)arg,
1046 sizeof(fa)))
1047 return -EFAULT;
1049 /* Make sure caller has proper permission */
1050 if (!inode_owner_or_capable(inode))
1051 return -EACCES;
1053 if (fa.fsx_xflags & ~EXT4_SUPPORTED_FS_XFLAGS)
1054 return -EOPNOTSUPP;
1056 flags = ext4_xflags_to_iflags(fa.fsx_xflags);
1057 if (ext4_mask_flags(inode->i_mode, flags) != flags)
1058 return -EOPNOTSUPP;
1060 err = mnt_want_write_file(filp);
1061 if (err)
1062 return err;
1064 inode_lock(inode);
1065 err = ext4_ioctl_check_project(inode, &fa);
1066 if (err)
1067 goto out;
1068 flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) |
1069 (flags & EXT4_FL_XFLAG_VISIBLE);
1070 err = ext4_ioctl_setflags(inode, flags);
1071 if (err)
1072 goto out;
1073 err = ext4_ioctl_setproject(filp, fa.fsx_projid);
1074 out:
1075 inode_unlock(inode);
1076 mnt_drop_write_file(filp);
1077 return err;
1079 case EXT4_IOC_SHUTDOWN:
1080 return ext4_shutdown(sb, arg);
1081 default:
1082 return -ENOTTY;
1086 #ifdef CONFIG_COMPAT
1087 long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1089 /* These are just misnamed, they actually get/put from/to user an int */
1090 switch (cmd) {
1091 case EXT4_IOC32_GETFLAGS:
1092 cmd = EXT4_IOC_GETFLAGS;
1093 break;
1094 case EXT4_IOC32_SETFLAGS:
1095 cmd = EXT4_IOC_SETFLAGS;
1096 break;
1097 case EXT4_IOC32_GETVERSION:
1098 cmd = EXT4_IOC_GETVERSION;
1099 break;
1100 case EXT4_IOC32_SETVERSION:
1101 cmd = EXT4_IOC_SETVERSION;
1102 break;
1103 case EXT4_IOC32_GROUP_EXTEND:
1104 cmd = EXT4_IOC_GROUP_EXTEND;
1105 break;
1106 case EXT4_IOC32_GETVERSION_OLD:
1107 cmd = EXT4_IOC_GETVERSION_OLD;
1108 break;
1109 case EXT4_IOC32_SETVERSION_OLD:
1110 cmd = EXT4_IOC_SETVERSION_OLD;
1111 break;
1112 case EXT4_IOC32_GETRSVSZ:
1113 cmd = EXT4_IOC_GETRSVSZ;
1114 break;
1115 case EXT4_IOC32_SETRSVSZ:
1116 cmd = EXT4_IOC_SETRSVSZ;
1117 break;
1118 case EXT4_IOC32_GROUP_ADD: {
1119 struct compat_ext4_new_group_input __user *uinput;
1120 struct ext4_new_group_data input;
1121 int err;
1123 uinput = compat_ptr(arg);
1124 err = get_user(input.group, &uinput->group);
1125 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
1126 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
1127 err |= get_user(input.inode_table, &uinput->inode_table);
1128 err |= get_user(input.blocks_count, &uinput->blocks_count);
1129 err |= get_user(input.reserved_blocks,
1130 &uinput->reserved_blocks);
1131 if (err)
1132 return -EFAULT;
1133 return ext4_ioctl_group_add(file, &input);
1135 case EXT4_IOC_MOVE_EXT:
1136 case EXT4_IOC_RESIZE_FS:
1137 case EXT4_IOC_PRECACHE_EXTENTS:
1138 case EXT4_IOC_SET_ENCRYPTION_POLICY:
1139 case EXT4_IOC_GET_ENCRYPTION_PWSALT:
1140 case EXT4_IOC_GET_ENCRYPTION_POLICY:
1141 case EXT4_IOC_SHUTDOWN:
1142 case FS_IOC_GETFSMAP:
1143 break;
1144 default:
1145 return -ENOIOCTLCMD;
1147 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
1149 #endif