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