xref: /openbmc/linux/fs/ext4/ioctl.c (revision 21cb47be6fb9ece7e6ee63f6780986faa384a77c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * linux/fs/ext4/ioctl.c
4  *
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  */
10 
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>
28 
29 /**
30  * Swap memory between @a and @b for @len bytes.
31  *
32  * @a:          pointer to first memory area
33  * @b:          pointer to second memory area
34  * @len:        number of bytes to swap
35  *
36  */
37 static void memswap(void *a, void *b, size_t len)
38 {
39 	unsigned char *ap, *bp;
40 
41 	ap = (unsigned char *)a;
42 	bp = (unsigned char *)b;
43 	while (len-- > 0) {
44 		swap(*ap, *bp);
45 		ap++;
46 		bp++;
47 	}
48 }
49 
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.
54  *
55  * Therefore you have to make sure, that calling this method twice
56  * will revert all changes.
57  *
58  * @inode1:     pointer to first inode
59  * @inode2:     pointer to second inode
60  */
61 static void swap_inode_data(struct inode *inode1, struct inode *inode2)
62 {
63 	loff_t isize;
64 	struct ext4_inode_info *ei1;
65 	struct ext4_inode_info *ei2;
66 	unsigned long tmp;
67 
68 	ei1 = EXT4_I(inode1);
69 	ei2 = EXT4_I(inode2);
70 
71 	swap(inode1->i_version, inode2->i_version);
72 	swap(inode1->i_atime, inode2->i_atime);
73 	swap(inode1->i_mtime, inode2->i_mtime);
74 
75 	memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data));
76 	tmp = ei1->i_flags & EXT4_FL_SHOULD_SWAP;
77 	ei1->i_flags = (ei2->i_flags & EXT4_FL_SHOULD_SWAP) |
78 		(ei1->i_flags & ~EXT4_FL_SHOULD_SWAP);
79 	ei2->i_flags = tmp | (ei2->i_flags & ~EXT4_FL_SHOULD_SWAP);
80 	swap(ei1->i_disksize, ei2->i_disksize);
81 	ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS);
82 	ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS);
83 
84 	isize = i_size_read(inode1);
85 	i_size_write(inode1, i_size_read(inode2));
86 	i_size_write(inode2, isize);
87 }
88 
89 void ext4_reset_inode_seed(struct inode *inode)
90 {
91 	struct ext4_inode_info *ei = EXT4_I(inode);
92 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
93 	__le32 inum = cpu_to_le32(inode->i_ino);
94 	__le32 gen = cpu_to_le32(inode->i_generation);
95 	__u32 csum;
96 
97 	if (!ext4_has_metadata_csum(inode->i_sb))
98 		return;
99 
100 	csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, sizeof(inum));
101 	ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, sizeof(gen));
102 }
103 
104 /**
105  * Swap the information from the given @inode and the inode
106  * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
107  * important fields of the inodes.
108  *
109  * @sb:         the super block of the filesystem
110  * @inode:      the inode to swap with EXT4_BOOT_LOADER_INO
111  *
112  */
113 static long swap_inode_boot_loader(struct super_block *sb,
114 				struct inode *inode)
115 {
116 	handle_t *handle;
117 	int err;
118 	struct inode *inode_bl;
119 	struct ext4_inode_info *ei_bl;
120 	qsize_t size, size_bl, diff;
121 	blkcnt_t blocks;
122 	unsigned short bytes;
123 
124 	inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO, EXT4_IGET_SPECIAL);
125 	if (IS_ERR(inode_bl))
126 		return PTR_ERR(inode_bl);
127 	ei_bl = EXT4_I(inode_bl);
128 
129 	/* Protect orig inodes against a truncate and make sure,
130 	 * that only 1 swap_inode_boot_loader is running. */
131 	lock_two_nondirectories(inode, inode_bl);
132 
133 	if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode) ||
134 	    IS_SWAPFILE(inode) || IS_ENCRYPTED(inode) ||
135 	    (EXT4_I(inode)->i_flags & EXT4_JOURNAL_DATA_FL) ||
136 	    ext4_has_inline_data(inode)) {
137 		err = -EINVAL;
138 		goto journal_err_out;
139 	}
140 
141 	if (IS_RDONLY(inode) || IS_APPEND(inode) || IS_IMMUTABLE(inode) ||
142 	    !inode_owner_or_capable(&init_user_ns, inode) ||
143 	    !capable(CAP_SYS_ADMIN)) {
144 		err = -EPERM;
145 		goto journal_err_out;
146 	}
147 
148 	down_write(&EXT4_I(inode)->i_mmap_sem);
149 	err = filemap_write_and_wait(inode->i_mapping);
150 	if (err)
151 		goto err_out;
152 
153 	err = filemap_write_and_wait(inode_bl->i_mapping);
154 	if (err)
155 		goto err_out;
156 
157 	/* Wait for all existing dio workers */
158 	inode_dio_wait(inode);
159 	inode_dio_wait(inode_bl);
160 
161 	truncate_inode_pages(&inode->i_data, 0);
162 	truncate_inode_pages(&inode_bl->i_data, 0);
163 
164 	handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
165 	if (IS_ERR(handle)) {
166 		err = -EINVAL;
167 		goto err_out;
168 	}
169 	ext4_fc_start_ineligible(sb, EXT4_FC_REASON_SWAP_BOOT);
170 
171 	/* Protect extent tree against block allocations via delalloc */
172 	ext4_double_down_write_data_sem(inode, inode_bl);
173 
174 	if (inode_bl->i_nlink == 0) {
175 		/* this inode has never been used as a BOOT_LOADER */
176 		set_nlink(inode_bl, 1);
177 		i_uid_write(inode_bl, 0);
178 		i_gid_write(inode_bl, 0);
179 		inode_bl->i_flags = 0;
180 		ei_bl->i_flags = 0;
181 		inode_set_iversion(inode_bl, 1);
182 		i_size_write(inode_bl, 0);
183 		inode_bl->i_mode = S_IFREG;
184 		if (ext4_has_feature_extents(sb)) {
185 			ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
186 			ext4_ext_tree_init(handle, inode_bl);
187 		} else
188 			memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
189 	}
190 
191 	err = dquot_initialize(inode);
192 	if (err)
193 		goto err_out1;
194 
195 	size = (qsize_t)(inode->i_blocks) * (1 << 9) + inode->i_bytes;
196 	size_bl = (qsize_t)(inode_bl->i_blocks) * (1 << 9) + inode_bl->i_bytes;
197 	diff = size - size_bl;
198 	swap_inode_data(inode, inode_bl);
199 
200 	inode->i_ctime = inode_bl->i_ctime = current_time(inode);
201 
202 	inode->i_generation = prandom_u32();
203 	inode_bl->i_generation = prandom_u32();
204 	ext4_reset_inode_seed(inode);
205 	ext4_reset_inode_seed(inode_bl);
206 
207 	ext4_discard_preallocations(inode, 0);
208 
209 	err = ext4_mark_inode_dirty(handle, inode);
210 	if (err < 0) {
211 		/* No need to update quota information. */
212 		ext4_warning(inode->i_sb,
213 			"couldn't mark inode #%lu dirty (err %d)",
214 			inode->i_ino, err);
215 		/* Revert all changes: */
216 		swap_inode_data(inode, inode_bl);
217 		ext4_mark_inode_dirty(handle, inode);
218 		goto err_out1;
219 	}
220 
221 	blocks = inode_bl->i_blocks;
222 	bytes = inode_bl->i_bytes;
223 	inode_bl->i_blocks = inode->i_blocks;
224 	inode_bl->i_bytes = inode->i_bytes;
225 	err = ext4_mark_inode_dirty(handle, inode_bl);
226 	if (err < 0) {
227 		/* No need to update quota information. */
228 		ext4_warning(inode_bl->i_sb,
229 			"couldn't mark inode #%lu dirty (err %d)",
230 			inode_bl->i_ino, err);
231 		goto revert;
232 	}
233 
234 	/* Bootloader inode should not be counted into quota information. */
235 	if (diff > 0)
236 		dquot_free_space(inode, diff);
237 	else
238 		err = dquot_alloc_space(inode, -1 * diff);
239 
240 	if (err < 0) {
241 revert:
242 		/* Revert all changes: */
243 		inode_bl->i_blocks = blocks;
244 		inode_bl->i_bytes = bytes;
245 		swap_inode_data(inode, inode_bl);
246 		ext4_mark_inode_dirty(handle, inode);
247 		ext4_mark_inode_dirty(handle, inode_bl);
248 	}
249 
250 err_out1:
251 	ext4_journal_stop(handle);
252 	ext4_fc_stop_ineligible(sb);
253 	ext4_double_up_write_data_sem(inode, inode_bl);
254 
255 err_out:
256 	up_write(&EXT4_I(inode)->i_mmap_sem);
257 journal_err_out:
258 	unlock_two_nondirectories(inode, inode_bl);
259 	iput(inode_bl);
260 	return err;
261 }
262 
263 #ifdef CONFIG_FS_ENCRYPTION
264 static int uuid_is_zero(__u8 u[16])
265 {
266 	int	i;
267 
268 	for (i = 0; i < 16; i++)
269 		if (u[i])
270 			return 0;
271 	return 1;
272 }
273 #endif
274 
275 /*
276  * If immutable is set and we are not clearing it, we're not allowed to change
277  * anything else in the inode.  Don't error out if we're only trying to set
278  * immutable on an immutable file.
279  */
280 static int ext4_ioctl_check_immutable(struct inode *inode, __u32 new_projid,
281 				      unsigned int flags)
282 {
283 	struct ext4_inode_info *ei = EXT4_I(inode);
284 	unsigned int oldflags = ei->i_flags;
285 
286 	if (!(oldflags & EXT4_IMMUTABLE_FL) || !(flags & EXT4_IMMUTABLE_FL))
287 		return 0;
288 
289 	if ((oldflags & ~EXT4_IMMUTABLE_FL) != (flags & ~EXT4_IMMUTABLE_FL))
290 		return -EPERM;
291 	if (ext4_has_feature_project(inode->i_sb) &&
292 	    __kprojid_val(ei->i_projid) != new_projid)
293 		return -EPERM;
294 
295 	return 0;
296 }
297 
298 static void ext4_dax_dontcache(struct inode *inode, unsigned int flags)
299 {
300 	struct ext4_inode_info *ei = EXT4_I(inode);
301 
302 	if (S_ISDIR(inode->i_mode))
303 		return;
304 
305 	if (test_opt2(inode->i_sb, DAX_NEVER) ||
306 	    test_opt(inode->i_sb, DAX_ALWAYS))
307 		return;
308 
309 	if ((ei->i_flags ^ flags) & EXT4_DAX_FL)
310 		d_mark_dontcache(inode);
311 }
312 
313 static bool dax_compatible(struct inode *inode, unsigned int oldflags,
314 			   unsigned int flags)
315 {
316 	if (flags & EXT4_DAX_FL) {
317 		if ((oldflags & EXT4_DAX_MUT_EXCL) ||
318 		     ext4_test_inode_state(inode,
319 					  EXT4_STATE_VERITY_IN_PROGRESS)) {
320 			return false;
321 		}
322 	}
323 
324 	if ((flags & EXT4_DAX_MUT_EXCL) && (oldflags & EXT4_DAX_FL))
325 			return false;
326 
327 	return true;
328 }
329 
330 static int ext4_ioctl_setflags(struct inode *inode,
331 			       unsigned int flags)
332 {
333 	struct ext4_inode_info *ei = EXT4_I(inode);
334 	handle_t *handle = NULL;
335 	int err = -EPERM, migrate = 0;
336 	struct ext4_iloc iloc;
337 	unsigned int oldflags, mask, i;
338 	struct super_block *sb = inode->i_sb;
339 
340 	/* Is it quota file? Do not allow user to mess with it */
341 	if (ext4_is_quota_file(inode))
342 		goto flags_out;
343 
344 	oldflags = ei->i_flags;
345 
346 	err = vfs_ioc_setflags_prepare(inode, oldflags, flags);
347 	if (err)
348 		goto flags_out;
349 
350 	/*
351 	 * The JOURNAL_DATA flag can only be changed by
352 	 * the relevant capability.
353 	 */
354 	if ((flags ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
355 		if (!capable(CAP_SYS_RESOURCE))
356 			goto flags_out;
357 	}
358 
359 	if (!dax_compatible(inode, oldflags, flags)) {
360 		err = -EOPNOTSUPP;
361 		goto flags_out;
362 	}
363 
364 	if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
365 		migrate = 1;
366 
367 	if ((flags ^ oldflags) & EXT4_CASEFOLD_FL) {
368 		if (!ext4_has_feature_casefold(sb)) {
369 			err = -EOPNOTSUPP;
370 			goto flags_out;
371 		}
372 
373 		if (!S_ISDIR(inode->i_mode)) {
374 			err = -ENOTDIR;
375 			goto flags_out;
376 		}
377 
378 		if (!ext4_empty_dir(inode)) {
379 			err = -ENOTEMPTY;
380 			goto flags_out;
381 		}
382 	}
383 
384 	/*
385 	 * Wait for all pending directio and then flush all the dirty pages
386 	 * for this file.  The flush marks all the pages readonly, so any
387 	 * subsequent attempt to write to the file (particularly mmap pages)
388 	 * will come through the filesystem and fail.
389 	 */
390 	if (S_ISREG(inode->i_mode) && !IS_IMMUTABLE(inode) &&
391 	    (flags & EXT4_IMMUTABLE_FL)) {
392 		inode_dio_wait(inode);
393 		err = filemap_write_and_wait(inode->i_mapping);
394 		if (err)
395 			goto flags_out;
396 	}
397 
398 	handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
399 	if (IS_ERR(handle)) {
400 		err = PTR_ERR(handle);
401 		goto flags_out;
402 	}
403 	if (IS_SYNC(inode))
404 		ext4_handle_sync(handle);
405 	err = ext4_reserve_inode_write(handle, inode, &iloc);
406 	if (err)
407 		goto flags_err;
408 
409 	ext4_dax_dontcache(inode, flags);
410 
411 	for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
412 		if (!(mask & EXT4_FL_USER_MODIFIABLE))
413 			continue;
414 		/* These flags get special treatment later */
415 		if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL)
416 			continue;
417 		if (mask & flags)
418 			ext4_set_inode_flag(inode, i);
419 		else
420 			ext4_clear_inode_flag(inode, i);
421 	}
422 
423 	ext4_set_inode_flags(inode, false);
424 
425 	inode->i_ctime = current_time(inode);
426 
427 	err = ext4_mark_iloc_dirty(handle, inode, &iloc);
428 flags_err:
429 	ext4_journal_stop(handle);
430 	if (err)
431 		goto flags_out;
432 
433 	if ((flags ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
434 		/*
435 		 * Changes to the journaling mode can cause unsafe changes to
436 		 * S_DAX if the inode is DAX
437 		 */
438 		if (IS_DAX(inode)) {
439 			err = -EBUSY;
440 			goto flags_out;
441 		}
442 
443 		err = ext4_change_inode_journal_flag(inode,
444 						     flags & EXT4_JOURNAL_DATA_FL);
445 		if (err)
446 			goto flags_out;
447 	}
448 	if (migrate) {
449 		if (flags & EXT4_EXTENTS_FL)
450 			err = ext4_ext_migrate(inode);
451 		else
452 			err = ext4_ind_migrate(inode);
453 	}
454 
455 flags_out:
456 	return err;
457 }
458 
459 #ifdef CONFIG_QUOTA
460 static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
461 {
462 	struct inode *inode = file_inode(filp);
463 	struct super_block *sb = inode->i_sb;
464 	struct ext4_inode_info *ei = EXT4_I(inode);
465 	int err, rc;
466 	handle_t *handle;
467 	kprojid_t kprojid;
468 	struct ext4_iloc iloc;
469 	struct ext4_inode *raw_inode;
470 	struct dquot *transfer_to[MAXQUOTAS] = { };
471 
472 	if (!ext4_has_feature_project(sb)) {
473 		if (projid != EXT4_DEF_PROJID)
474 			return -EOPNOTSUPP;
475 		else
476 			return 0;
477 	}
478 
479 	if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
480 		return -EOPNOTSUPP;
481 
482 	kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
483 
484 	if (projid_eq(kprojid, EXT4_I(inode)->i_projid))
485 		return 0;
486 
487 	err = -EPERM;
488 	/* Is it quota file? Do not allow user to mess with it */
489 	if (ext4_is_quota_file(inode))
490 		return err;
491 
492 	err = ext4_get_inode_loc(inode, &iloc);
493 	if (err)
494 		return err;
495 
496 	raw_inode = ext4_raw_inode(&iloc);
497 	if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
498 		err = ext4_expand_extra_isize(inode,
499 					      EXT4_SB(sb)->s_want_extra_isize,
500 					      &iloc);
501 		if (err)
502 			return err;
503 	} else {
504 		brelse(iloc.bh);
505 	}
506 
507 	err = dquot_initialize(inode);
508 	if (err)
509 		return err;
510 
511 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
512 		EXT4_QUOTA_INIT_BLOCKS(sb) +
513 		EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
514 	if (IS_ERR(handle))
515 		return PTR_ERR(handle);
516 
517 	err = ext4_reserve_inode_write(handle, inode, &iloc);
518 	if (err)
519 		goto out_stop;
520 
521 	transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
522 	if (!IS_ERR(transfer_to[PRJQUOTA])) {
523 
524 		/* __dquot_transfer() calls back ext4_get_inode_usage() which
525 		 * counts xattr inode references.
526 		 */
527 		down_read(&EXT4_I(inode)->xattr_sem);
528 		err = __dquot_transfer(inode, transfer_to);
529 		up_read(&EXT4_I(inode)->xattr_sem);
530 		dqput(transfer_to[PRJQUOTA]);
531 		if (err)
532 			goto out_dirty;
533 	}
534 
535 	EXT4_I(inode)->i_projid = kprojid;
536 	inode->i_ctime = current_time(inode);
537 out_dirty:
538 	rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
539 	if (!err)
540 		err = rc;
541 out_stop:
542 	ext4_journal_stop(handle);
543 	return err;
544 }
545 #else
546 static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
547 {
548 	if (projid != EXT4_DEF_PROJID)
549 		return -EOPNOTSUPP;
550 	return 0;
551 }
552 #endif
553 
554 /* Transfer internal flags to xflags */
555 static inline __u32 ext4_iflags_to_xflags(unsigned long iflags)
556 {
557 	__u32 xflags = 0;
558 
559 	if (iflags & EXT4_SYNC_FL)
560 		xflags |= FS_XFLAG_SYNC;
561 	if (iflags & EXT4_IMMUTABLE_FL)
562 		xflags |= FS_XFLAG_IMMUTABLE;
563 	if (iflags & EXT4_APPEND_FL)
564 		xflags |= FS_XFLAG_APPEND;
565 	if (iflags & EXT4_NODUMP_FL)
566 		xflags |= FS_XFLAG_NODUMP;
567 	if (iflags & EXT4_NOATIME_FL)
568 		xflags |= FS_XFLAG_NOATIME;
569 	if (iflags & EXT4_PROJINHERIT_FL)
570 		xflags |= FS_XFLAG_PROJINHERIT;
571 	if (iflags & EXT4_DAX_FL)
572 		xflags |= FS_XFLAG_DAX;
573 	return xflags;
574 }
575 
576 #define EXT4_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
577 				  FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
578 				  FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT | \
579 				  FS_XFLAG_DAX)
580 
581 /* Transfer xflags flags to internal */
582 static inline unsigned long ext4_xflags_to_iflags(__u32 xflags)
583 {
584 	unsigned long iflags = 0;
585 
586 	if (xflags & FS_XFLAG_SYNC)
587 		iflags |= EXT4_SYNC_FL;
588 	if (xflags & FS_XFLAG_IMMUTABLE)
589 		iflags |= EXT4_IMMUTABLE_FL;
590 	if (xflags & FS_XFLAG_APPEND)
591 		iflags |= EXT4_APPEND_FL;
592 	if (xflags & FS_XFLAG_NODUMP)
593 		iflags |= EXT4_NODUMP_FL;
594 	if (xflags & FS_XFLAG_NOATIME)
595 		iflags |= EXT4_NOATIME_FL;
596 	if (xflags & FS_XFLAG_PROJINHERIT)
597 		iflags |= EXT4_PROJINHERIT_FL;
598 	if (xflags & FS_XFLAG_DAX)
599 		iflags |= EXT4_DAX_FL;
600 
601 	return iflags;
602 }
603 
604 static int ext4_shutdown(struct super_block *sb, unsigned long arg)
605 {
606 	struct ext4_sb_info *sbi = EXT4_SB(sb);
607 	__u32 flags;
608 
609 	if (!capable(CAP_SYS_ADMIN))
610 		return -EPERM;
611 
612 	if (get_user(flags, (__u32 __user *)arg))
613 		return -EFAULT;
614 
615 	if (flags > EXT4_GOING_FLAGS_NOLOGFLUSH)
616 		return -EINVAL;
617 
618 	if (ext4_forced_shutdown(sbi))
619 		return 0;
620 
621 	ext4_msg(sb, KERN_ALERT, "shut down requested (%d)", flags);
622 	trace_ext4_shutdown(sb, flags);
623 
624 	switch (flags) {
625 	case EXT4_GOING_FLAGS_DEFAULT:
626 		freeze_bdev(sb->s_bdev);
627 		set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
628 		thaw_bdev(sb->s_bdev);
629 		break;
630 	case EXT4_GOING_FLAGS_LOGFLUSH:
631 		set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
632 		if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) {
633 			(void) ext4_force_commit(sb);
634 			jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
635 		}
636 		break;
637 	case EXT4_GOING_FLAGS_NOLOGFLUSH:
638 		set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
639 		if (sbi->s_journal && !is_journal_aborted(sbi->s_journal))
640 			jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
641 		break;
642 	default:
643 		return -EINVAL;
644 	}
645 	clear_opt(sb, DISCARD);
646 	return 0;
647 }
648 
649 struct getfsmap_info {
650 	struct super_block	*gi_sb;
651 	struct fsmap_head __user *gi_data;
652 	unsigned int		gi_idx;
653 	__u32			gi_last_flags;
654 };
655 
656 static int ext4_getfsmap_format(struct ext4_fsmap *xfm, void *priv)
657 {
658 	struct getfsmap_info *info = priv;
659 	struct fsmap fm;
660 
661 	trace_ext4_getfsmap_mapping(info->gi_sb, xfm);
662 
663 	info->gi_last_flags = xfm->fmr_flags;
664 	ext4_fsmap_from_internal(info->gi_sb, &fm, xfm);
665 	if (copy_to_user(&info->gi_data->fmh_recs[info->gi_idx++], &fm,
666 			sizeof(struct fsmap)))
667 		return -EFAULT;
668 
669 	return 0;
670 }
671 
672 static int ext4_ioc_getfsmap(struct super_block *sb,
673 			     struct fsmap_head __user *arg)
674 {
675 	struct getfsmap_info info = { NULL };
676 	struct ext4_fsmap_head xhead = {0};
677 	struct fsmap_head head;
678 	bool aborted = false;
679 	int error;
680 
681 	if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
682 		return -EFAULT;
683 	if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
684 	    memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
685 		       sizeof(head.fmh_keys[0].fmr_reserved)) ||
686 	    memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
687 		       sizeof(head.fmh_keys[1].fmr_reserved)))
688 		return -EINVAL;
689 	/*
690 	 * ext4 doesn't report file extents at all, so the only valid
691 	 * file offsets are the magic ones (all zeroes or all ones).
692 	 */
693 	if (head.fmh_keys[0].fmr_offset ||
694 	    (head.fmh_keys[1].fmr_offset != 0 &&
695 	     head.fmh_keys[1].fmr_offset != -1ULL))
696 		return -EINVAL;
697 
698 	xhead.fmh_iflags = head.fmh_iflags;
699 	xhead.fmh_count = head.fmh_count;
700 	ext4_fsmap_to_internal(sb, &xhead.fmh_keys[0], &head.fmh_keys[0]);
701 	ext4_fsmap_to_internal(sb, &xhead.fmh_keys[1], &head.fmh_keys[1]);
702 
703 	trace_ext4_getfsmap_low_key(sb, &xhead.fmh_keys[0]);
704 	trace_ext4_getfsmap_high_key(sb, &xhead.fmh_keys[1]);
705 
706 	info.gi_sb = sb;
707 	info.gi_data = arg;
708 	error = ext4_getfsmap(sb, &xhead, ext4_getfsmap_format, &info);
709 	if (error == EXT4_QUERY_RANGE_ABORT) {
710 		error = 0;
711 		aborted = true;
712 	} else if (error)
713 		return error;
714 
715 	/* If we didn't abort, set the "last" flag in the last fmx */
716 	if (!aborted && info.gi_idx) {
717 		info.gi_last_flags |= FMR_OF_LAST;
718 		if (copy_to_user(&info.gi_data->fmh_recs[info.gi_idx - 1].fmr_flags,
719 				 &info.gi_last_flags,
720 				 sizeof(info.gi_last_flags)))
721 			return -EFAULT;
722 	}
723 
724 	/* copy back header */
725 	head.fmh_entries = xhead.fmh_entries;
726 	head.fmh_oflags = xhead.fmh_oflags;
727 	if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
728 		return -EFAULT;
729 
730 	return 0;
731 }
732 
733 static long ext4_ioctl_group_add(struct file *file,
734 				 struct ext4_new_group_data *input)
735 {
736 	struct super_block *sb = file_inode(file)->i_sb;
737 	int err, err2=0;
738 
739 	err = ext4_resize_begin(sb);
740 	if (err)
741 		return err;
742 
743 	if (ext4_has_feature_bigalloc(sb)) {
744 		ext4_msg(sb, KERN_ERR,
745 			 "Online resizing not supported with bigalloc");
746 		err = -EOPNOTSUPP;
747 		goto group_add_out;
748 	}
749 
750 	err = mnt_want_write_file(file);
751 	if (err)
752 		goto group_add_out;
753 
754 	err = ext4_group_add(sb, input);
755 	if (EXT4_SB(sb)->s_journal) {
756 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
757 		err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
758 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
759 	}
760 	if (err == 0)
761 		err = err2;
762 	mnt_drop_write_file(file);
763 	if (!err && ext4_has_group_desc_csum(sb) &&
764 	    test_opt(sb, INIT_INODE_TABLE))
765 		err = ext4_register_li_request(sb, input->group);
766 group_add_out:
767 	ext4_resize_end(sb);
768 	return err;
769 }
770 
771 static void ext4_fill_fsxattr(struct inode *inode, struct fsxattr *fa)
772 {
773 	struct ext4_inode_info *ei = EXT4_I(inode);
774 
775 	simple_fill_fsxattr(fa, ext4_iflags_to_xflags(ei->i_flags &
776 						      EXT4_FL_USER_VISIBLE));
777 
778 	if (ext4_has_feature_project(inode->i_sb))
779 		fa->fsx_projid = from_kprojid(&init_user_ns, ei->i_projid);
780 }
781 
782 /* So that the fiemap access checks can't overflow on 32 bit machines. */
783 #define FIEMAP_MAX_EXTENTS	(UINT_MAX / sizeof(struct fiemap_extent))
784 
785 static int ext4_ioctl_get_es_cache(struct file *filp, unsigned long arg)
786 {
787 	struct fiemap fiemap;
788 	struct fiemap __user *ufiemap = (struct fiemap __user *) arg;
789 	struct fiemap_extent_info fieinfo = { 0, };
790 	struct inode *inode = file_inode(filp);
791 	int error;
792 
793 	if (copy_from_user(&fiemap, ufiemap, sizeof(fiemap)))
794 		return -EFAULT;
795 
796 	if (fiemap.fm_extent_count > FIEMAP_MAX_EXTENTS)
797 		return -EINVAL;
798 
799 	fieinfo.fi_flags = fiemap.fm_flags;
800 	fieinfo.fi_extents_max = fiemap.fm_extent_count;
801 	fieinfo.fi_extents_start = ufiemap->fm_extents;
802 
803 	error = ext4_get_es_cache(inode, &fieinfo, fiemap.fm_start,
804 			fiemap.fm_length);
805 	fiemap.fm_flags = fieinfo.fi_flags;
806 	fiemap.fm_mapped_extents = fieinfo.fi_extents_mapped;
807 	if (copy_to_user(ufiemap, &fiemap, sizeof(fiemap)))
808 		error = -EFAULT;
809 
810 	return error;
811 }
812 
813 static long __ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
814 {
815 	struct inode *inode = file_inode(filp);
816 	struct super_block *sb = inode->i_sb;
817 	struct ext4_inode_info *ei = EXT4_I(inode);
818 	unsigned int flags;
819 
820 	ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
821 
822 	switch (cmd) {
823 	case FS_IOC_GETFSMAP:
824 		return ext4_ioc_getfsmap(sb, (void __user *)arg);
825 	case FS_IOC_GETFLAGS:
826 		flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
827 		if (S_ISREG(inode->i_mode))
828 			flags &= ~EXT4_PROJINHERIT_FL;
829 		return put_user(flags, (int __user *) arg);
830 	case FS_IOC_SETFLAGS: {
831 		int err;
832 
833 		if (!inode_owner_or_capable(&init_user_ns, inode))
834 			return -EACCES;
835 
836 		if (get_user(flags, (int __user *) arg))
837 			return -EFAULT;
838 
839 		if (flags & ~EXT4_FL_USER_VISIBLE)
840 			return -EOPNOTSUPP;
841 		/*
842 		 * chattr(1) grabs flags via GETFLAGS, modifies the result and
843 		 * passes that to SETFLAGS. So we cannot easily make SETFLAGS
844 		 * more restrictive than just silently masking off visible but
845 		 * not settable flags as we always did.
846 		 */
847 		flags &= EXT4_FL_USER_MODIFIABLE;
848 		if (ext4_mask_flags(inode->i_mode, flags) != flags)
849 			return -EOPNOTSUPP;
850 
851 		err = mnt_want_write_file(filp);
852 		if (err)
853 			return err;
854 
855 		inode_lock(inode);
856 		err = ext4_ioctl_check_immutable(inode,
857 				from_kprojid(&init_user_ns, ei->i_projid),
858 				flags);
859 		if (!err)
860 			err = ext4_ioctl_setflags(inode, flags);
861 		inode_unlock(inode);
862 		mnt_drop_write_file(filp);
863 		return err;
864 	}
865 	case EXT4_IOC_GETVERSION:
866 	case EXT4_IOC_GETVERSION_OLD:
867 		return put_user(inode->i_generation, (int __user *) arg);
868 	case EXT4_IOC_SETVERSION:
869 	case EXT4_IOC_SETVERSION_OLD: {
870 		handle_t *handle;
871 		struct ext4_iloc iloc;
872 		__u32 generation;
873 		int err;
874 
875 		if (!inode_owner_or_capable(&init_user_ns, inode))
876 			return -EPERM;
877 
878 		if (ext4_has_metadata_csum(inode->i_sb)) {
879 			ext4_warning(sb, "Setting inode version is not "
880 				     "supported with metadata_csum enabled.");
881 			return -ENOTTY;
882 		}
883 
884 		err = mnt_want_write_file(filp);
885 		if (err)
886 			return err;
887 		if (get_user(generation, (int __user *) arg)) {
888 			err = -EFAULT;
889 			goto setversion_out;
890 		}
891 
892 		inode_lock(inode);
893 		handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
894 		if (IS_ERR(handle)) {
895 			err = PTR_ERR(handle);
896 			goto unlock_out;
897 		}
898 		err = ext4_reserve_inode_write(handle, inode, &iloc);
899 		if (err == 0) {
900 			inode->i_ctime = current_time(inode);
901 			inode->i_generation = generation;
902 			err = ext4_mark_iloc_dirty(handle, inode, &iloc);
903 		}
904 		ext4_journal_stop(handle);
905 
906 unlock_out:
907 		inode_unlock(inode);
908 setversion_out:
909 		mnt_drop_write_file(filp);
910 		return err;
911 	}
912 	case EXT4_IOC_GROUP_EXTEND: {
913 		ext4_fsblk_t n_blocks_count;
914 		int err, err2=0;
915 
916 		err = ext4_resize_begin(sb);
917 		if (err)
918 			return err;
919 
920 		if (get_user(n_blocks_count, (__u32 __user *)arg)) {
921 			err = -EFAULT;
922 			goto group_extend_out;
923 		}
924 
925 		if (ext4_has_feature_bigalloc(sb)) {
926 			ext4_msg(sb, KERN_ERR,
927 				 "Online resizing not supported with bigalloc");
928 			err = -EOPNOTSUPP;
929 			goto group_extend_out;
930 		}
931 
932 		err = mnt_want_write_file(filp);
933 		if (err)
934 			goto group_extend_out;
935 
936 		err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
937 		if (EXT4_SB(sb)->s_journal) {
938 			jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
939 			err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
940 			jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
941 		}
942 		if (err == 0)
943 			err = err2;
944 		mnt_drop_write_file(filp);
945 group_extend_out:
946 		ext4_resize_end(sb);
947 		return err;
948 	}
949 
950 	case EXT4_IOC_MOVE_EXT: {
951 		struct move_extent me;
952 		struct fd donor;
953 		int err;
954 
955 		if (!(filp->f_mode & FMODE_READ) ||
956 		    !(filp->f_mode & FMODE_WRITE))
957 			return -EBADF;
958 
959 		if (copy_from_user(&me,
960 			(struct move_extent __user *)arg, sizeof(me)))
961 			return -EFAULT;
962 		me.moved_len = 0;
963 
964 		donor = fdget(me.donor_fd);
965 		if (!donor.file)
966 			return -EBADF;
967 
968 		if (!(donor.file->f_mode & FMODE_WRITE)) {
969 			err = -EBADF;
970 			goto mext_out;
971 		}
972 
973 		if (ext4_has_feature_bigalloc(sb)) {
974 			ext4_msg(sb, KERN_ERR,
975 				 "Online defrag not supported with bigalloc");
976 			err = -EOPNOTSUPP;
977 			goto mext_out;
978 		} else if (IS_DAX(inode)) {
979 			ext4_msg(sb, KERN_ERR,
980 				 "Online defrag not supported with DAX");
981 			err = -EOPNOTSUPP;
982 			goto mext_out;
983 		}
984 
985 		err = mnt_want_write_file(filp);
986 		if (err)
987 			goto mext_out;
988 
989 		err = ext4_move_extents(filp, donor.file, me.orig_start,
990 					me.donor_start, me.len, &me.moved_len);
991 		mnt_drop_write_file(filp);
992 
993 		if (copy_to_user((struct move_extent __user *)arg,
994 				 &me, sizeof(me)))
995 			err = -EFAULT;
996 mext_out:
997 		fdput(donor);
998 		return err;
999 	}
1000 
1001 	case EXT4_IOC_GROUP_ADD: {
1002 		struct ext4_new_group_data input;
1003 
1004 		if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
1005 				sizeof(input)))
1006 			return -EFAULT;
1007 
1008 		return ext4_ioctl_group_add(filp, &input);
1009 	}
1010 
1011 	case EXT4_IOC_MIGRATE:
1012 	{
1013 		int err;
1014 		if (!inode_owner_or_capable(&init_user_ns, inode))
1015 			return -EACCES;
1016 
1017 		err = mnt_want_write_file(filp);
1018 		if (err)
1019 			return err;
1020 		/*
1021 		 * inode_mutex prevent write and truncate on the file.
1022 		 * Read still goes through. We take i_data_sem in
1023 		 * ext4_ext_swap_inode_data before we switch the
1024 		 * inode format to prevent read.
1025 		 */
1026 		inode_lock((inode));
1027 		err = ext4_ext_migrate(inode);
1028 		inode_unlock((inode));
1029 		mnt_drop_write_file(filp);
1030 		return err;
1031 	}
1032 
1033 	case EXT4_IOC_ALLOC_DA_BLKS:
1034 	{
1035 		int err;
1036 		if (!inode_owner_or_capable(&init_user_ns, inode))
1037 			return -EACCES;
1038 
1039 		err = mnt_want_write_file(filp);
1040 		if (err)
1041 			return err;
1042 		err = ext4_alloc_da_blocks(inode);
1043 		mnt_drop_write_file(filp);
1044 		return err;
1045 	}
1046 
1047 	case EXT4_IOC_SWAP_BOOT:
1048 	{
1049 		int err;
1050 		if (!(filp->f_mode & FMODE_WRITE))
1051 			return -EBADF;
1052 		err = mnt_want_write_file(filp);
1053 		if (err)
1054 			return err;
1055 		err = swap_inode_boot_loader(sb, inode);
1056 		mnt_drop_write_file(filp);
1057 		return err;
1058 	}
1059 
1060 	case EXT4_IOC_RESIZE_FS: {
1061 		ext4_fsblk_t n_blocks_count;
1062 		int err = 0, err2 = 0;
1063 		ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
1064 
1065 		if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
1066 				   sizeof(__u64))) {
1067 			return -EFAULT;
1068 		}
1069 
1070 		err = ext4_resize_begin(sb);
1071 		if (err)
1072 			return err;
1073 
1074 		err = mnt_want_write_file(filp);
1075 		if (err)
1076 			goto resizefs_out;
1077 
1078 		err = ext4_resize_fs(sb, n_blocks_count);
1079 		if (EXT4_SB(sb)->s_journal) {
1080 			ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_RESIZE);
1081 			jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
1082 			err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
1083 			jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
1084 		}
1085 		if (err == 0)
1086 			err = err2;
1087 		mnt_drop_write_file(filp);
1088 		if (!err && (o_group < EXT4_SB(sb)->s_groups_count) &&
1089 		    ext4_has_group_desc_csum(sb) &&
1090 		    test_opt(sb, INIT_INODE_TABLE))
1091 			err = ext4_register_li_request(sb, o_group);
1092 
1093 resizefs_out:
1094 		ext4_resize_end(sb);
1095 		return err;
1096 	}
1097 
1098 	case FITRIM:
1099 	{
1100 		struct request_queue *q = bdev_get_queue(sb->s_bdev);
1101 		struct fstrim_range range;
1102 		int ret = 0;
1103 
1104 		if (!capable(CAP_SYS_ADMIN))
1105 			return -EPERM;
1106 
1107 		if (!blk_queue_discard(q))
1108 			return -EOPNOTSUPP;
1109 
1110 		/*
1111 		 * We haven't replayed the journal, so we cannot use our
1112 		 * block-bitmap-guided storage zapping commands.
1113 		 */
1114 		if (test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb))
1115 			return -EROFS;
1116 
1117 		if (copy_from_user(&range, (struct fstrim_range __user *)arg,
1118 		    sizeof(range)))
1119 			return -EFAULT;
1120 
1121 		range.minlen = max((unsigned int)range.minlen,
1122 				   q->limits.discard_granularity);
1123 		ret = ext4_trim_fs(sb, &range);
1124 		if (ret < 0)
1125 			return ret;
1126 
1127 		if (copy_to_user((struct fstrim_range __user *)arg, &range,
1128 		    sizeof(range)))
1129 			return -EFAULT;
1130 
1131 		return 0;
1132 	}
1133 	case EXT4_IOC_PRECACHE_EXTENTS:
1134 		return ext4_ext_precache(inode);
1135 
1136 	case FS_IOC_SET_ENCRYPTION_POLICY:
1137 		if (!ext4_has_feature_encrypt(sb))
1138 			return -EOPNOTSUPP;
1139 		return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
1140 
1141 	case FS_IOC_GET_ENCRYPTION_PWSALT: {
1142 #ifdef CONFIG_FS_ENCRYPTION
1143 		int err, err2;
1144 		struct ext4_sb_info *sbi = EXT4_SB(sb);
1145 		handle_t *handle;
1146 
1147 		if (!ext4_has_feature_encrypt(sb))
1148 			return -EOPNOTSUPP;
1149 		if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
1150 			err = mnt_want_write_file(filp);
1151 			if (err)
1152 				return err;
1153 			handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
1154 			if (IS_ERR(handle)) {
1155 				err = PTR_ERR(handle);
1156 				goto pwsalt_err_exit;
1157 			}
1158 			err = ext4_journal_get_write_access(handle, sbi->s_sbh);
1159 			if (err)
1160 				goto pwsalt_err_journal;
1161 			lock_buffer(sbi->s_sbh);
1162 			generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
1163 			ext4_superblock_csum_set(sb);
1164 			unlock_buffer(sbi->s_sbh);
1165 			err = ext4_handle_dirty_metadata(handle, NULL,
1166 							 sbi->s_sbh);
1167 		pwsalt_err_journal:
1168 			err2 = ext4_journal_stop(handle);
1169 			if (err2 && !err)
1170 				err = err2;
1171 		pwsalt_err_exit:
1172 			mnt_drop_write_file(filp);
1173 			if (err)
1174 				return err;
1175 		}
1176 		if (copy_to_user((void __user *) arg,
1177 				 sbi->s_es->s_encrypt_pw_salt, 16))
1178 			return -EFAULT;
1179 		return 0;
1180 #else
1181 		return -EOPNOTSUPP;
1182 #endif
1183 	}
1184 	case FS_IOC_GET_ENCRYPTION_POLICY:
1185 		if (!ext4_has_feature_encrypt(sb))
1186 			return -EOPNOTSUPP;
1187 		return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
1188 
1189 	case FS_IOC_GET_ENCRYPTION_POLICY_EX:
1190 		if (!ext4_has_feature_encrypt(sb))
1191 			return -EOPNOTSUPP;
1192 		return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
1193 
1194 	case FS_IOC_ADD_ENCRYPTION_KEY:
1195 		if (!ext4_has_feature_encrypt(sb))
1196 			return -EOPNOTSUPP;
1197 		return fscrypt_ioctl_add_key(filp, (void __user *)arg);
1198 
1199 	case FS_IOC_REMOVE_ENCRYPTION_KEY:
1200 		if (!ext4_has_feature_encrypt(sb))
1201 			return -EOPNOTSUPP;
1202 		return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
1203 
1204 	case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
1205 		if (!ext4_has_feature_encrypt(sb))
1206 			return -EOPNOTSUPP;
1207 		return fscrypt_ioctl_remove_key_all_users(filp,
1208 							  (void __user *)arg);
1209 	case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
1210 		if (!ext4_has_feature_encrypt(sb))
1211 			return -EOPNOTSUPP;
1212 		return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
1213 
1214 	case FS_IOC_GET_ENCRYPTION_NONCE:
1215 		if (!ext4_has_feature_encrypt(sb))
1216 			return -EOPNOTSUPP;
1217 		return fscrypt_ioctl_get_nonce(filp, (void __user *)arg);
1218 
1219 	case EXT4_IOC_CLEAR_ES_CACHE:
1220 	{
1221 		if (!inode_owner_or_capable(&init_user_ns, inode))
1222 			return -EACCES;
1223 		ext4_clear_inode_es(inode);
1224 		return 0;
1225 	}
1226 
1227 	case EXT4_IOC_GETSTATE:
1228 	{
1229 		__u32	state = 0;
1230 
1231 		if (ext4_test_inode_state(inode, EXT4_STATE_EXT_PRECACHED))
1232 			state |= EXT4_STATE_FLAG_EXT_PRECACHED;
1233 		if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
1234 			state |= EXT4_STATE_FLAG_NEW;
1235 		if (ext4_test_inode_state(inode, EXT4_STATE_NEWENTRY))
1236 			state |= EXT4_STATE_FLAG_NEWENTRY;
1237 		if (ext4_test_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE))
1238 			state |= EXT4_STATE_FLAG_DA_ALLOC_CLOSE;
1239 
1240 		return put_user(state, (__u32 __user *) arg);
1241 	}
1242 
1243 	case EXT4_IOC_GET_ES_CACHE:
1244 		return ext4_ioctl_get_es_cache(filp, arg);
1245 
1246 	case FS_IOC_FSGETXATTR:
1247 	{
1248 		struct fsxattr fa;
1249 
1250 		ext4_fill_fsxattr(inode, &fa);
1251 
1252 		if (copy_to_user((struct fsxattr __user *)arg,
1253 				 &fa, sizeof(fa)))
1254 			return -EFAULT;
1255 		return 0;
1256 	}
1257 	case FS_IOC_FSSETXATTR:
1258 	{
1259 		struct fsxattr fa, old_fa;
1260 		int err;
1261 
1262 		if (copy_from_user(&fa, (struct fsxattr __user *)arg,
1263 				   sizeof(fa)))
1264 			return -EFAULT;
1265 
1266 		/* Make sure caller has proper permission */
1267 		if (!inode_owner_or_capable(&init_user_ns, inode))
1268 			return -EACCES;
1269 
1270 		if (fa.fsx_xflags & ~EXT4_SUPPORTED_FS_XFLAGS)
1271 			return -EOPNOTSUPP;
1272 
1273 		flags = ext4_xflags_to_iflags(fa.fsx_xflags);
1274 		if (ext4_mask_flags(inode->i_mode, flags) != flags)
1275 			return -EOPNOTSUPP;
1276 
1277 		err = mnt_want_write_file(filp);
1278 		if (err)
1279 			return err;
1280 
1281 		inode_lock(inode);
1282 		ext4_fill_fsxattr(inode, &old_fa);
1283 		err = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa);
1284 		if (err)
1285 			goto out;
1286 		flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) |
1287 			 (flags & EXT4_FL_XFLAG_VISIBLE);
1288 		err = ext4_ioctl_check_immutable(inode, fa.fsx_projid, flags);
1289 		if (err)
1290 			goto out;
1291 		err = ext4_ioctl_setflags(inode, flags);
1292 		if (err)
1293 			goto out;
1294 		err = ext4_ioctl_setproject(filp, fa.fsx_projid);
1295 out:
1296 		inode_unlock(inode);
1297 		mnt_drop_write_file(filp);
1298 		return err;
1299 	}
1300 	case EXT4_IOC_SHUTDOWN:
1301 		return ext4_shutdown(sb, arg);
1302 
1303 	case FS_IOC_ENABLE_VERITY:
1304 		if (!ext4_has_feature_verity(sb))
1305 			return -EOPNOTSUPP;
1306 		return fsverity_ioctl_enable(filp, (const void __user *)arg);
1307 
1308 	case FS_IOC_MEASURE_VERITY:
1309 		if (!ext4_has_feature_verity(sb))
1310 			return -EOPNOTSUPP;
1311 		return fsverity_ioctl_measure(filp, (void __user *)arg);
1312 
1313 	default:
1314 		return -ENOTTY;
1315 	}
1316 }
1317 
1318 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1319 {
1320 	long ret;
1321 
1322 	ext4_fc_start_update(file_inode(filp));
1323 	ret = __ext4_ioctl(filp, cmd, arg);
1324 	ext4_fc_stop_update(file_inode(filp));
1325 
1326 	return ret;
1327 }
1328 
1329 #ifdef CONFIG_COMPAT
1330 long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1331 {
1332 	/* These are just misnamed, they actually get/put from/to user an int */
1333 	switch (cmd) {
1334 	case FS_IOC32_GETFLAGS:
1335 		cmd = FS_IOC_GETFLAGS;
1336 		break;
1337 	case FS_IOC32_SETFLAGS:
1338 		cmd = FS_IOC_SETFLAGS;
1339 		break;
1340 	case EXT4_IOC32_GETVERSION:
1341 		cmd = EXT4_IOC_GETVERSION;
1342 		break;
1343 	case EXT4_IOC32_SETVERSION:
1344 		cmd = EXT4_IOC_SETVERSION;
1345 		break;
1346 	case EXT4_IOC32_GROUP_EXTEND:
1347 		cmd = EXT4_IOC_GROUP_EXTEND;
1348 		break;
1349 	case EXT4_IOC32_GETVERSION_OLD:
1350 		cmd = EXT4_IOC_GETVERSION_OLD;
1351 		break;
1352 	case EXT4_IOC32_SETVERSION_OLD:
1353 		cmd = EXT4_IOC_SETVERSION_OLD;
1354 		break;
1355 	case EXT4_IOC32_GETRSVSZ:
1356 		cmd = EXT4_IOC_GETRSVSZ;
1357 		break;
1358 	case EXT4_IOC32_SETRSVSZ:
1359 		cmd = EXT4_IOC_SETRSVSZ;
1360 		break;
1361 	case EXT4_IOC32_GROUP_ADD: {
1362 		struct compat_ext4_new_group_input __user *uinput;
1363 		struct ext4_new_group_data input;
1364 		int err;
1365 
1366 		uinput = compat_ptr(arg);
1367 		err = get_user(input.group, &uinput->group);
1368 		err |= get_user(input.block_bitmap, &uinput->block_bitmap);
1369 		err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
1370 		err |= get_user(input.inode_table, &uinput->inode_table);
1371 		err |= get_user(input.blocks_count, &uinput->blocks_count);
1372 		err |= get_user(input.reserved_blocks,
1373 				&uinput->reserved_blocks);
1374 		if (err)
1375 			return -EFAULT;
1376 		return ext4_ioctl_group_add(file, &input);
1377 	}
1378 	case EXT4_IOC_MOVE_EXT:
1379 	case EXT4_IOC_RESIZE_FS:
1380 	case FITRIM:
1381 	case EXT4_IOC_PRECACHE_EXTENTS:
1382 	case FS_IOC_SET_ENCRYPTION_POLICY:
1383 	case FS_IOC_GET_ENCRYPTION_PWSALT:
1384 	case FS_IOC_GET_ENCRYPTION_POLICY:
1385 	case FS_IOC_GET_ENCRYPTION_POLICY_EX:
1386 	case FS_IOC_ADD_ENCRYPTION_KEY:
1387 	case FS_IOC_REMOVE_ENCRYPTION_KEY:
1388 	case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
1389 	case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
1390 	case FS_IOC_GET_ENCRYPTION_NONCE:
1391 	case EXT4_IOC_SHUTDOWN:
1392 	case FS_IOC_GETFSMAP:
1393 	case FS_IOC_ENABLE_VERITY:
1394 	case FS_IOC_MEASURE_VERITY:
1395 	case EXT4_IOC_CLEAR_ES_CACHE:
1396 	case EXT4_IOC_GETSTATE:
1397 	case EXT4_IOC_GET_ES_CACHE:
1398 	case FS_IOC_FSGETXATTR:
1399 	case FS_IOC_FSSETXATTR:
1400 		break;
1401 	default:
1402 		return -ENOIOCTLCMD;
1403 	}
1404 	return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
1405 }
1406 #endif
1407