xref: /linux/fs/ext4/ioctl.c (revision 3ce095c16263630dde46d6051854073edaacf3d7)
1 /*
2  * linux/fs/ext4/ioctl.c
3  *
4  * Copyright (C) 1993, 1994, 1995
5  * Remy Card (card@masi.ibp.fr)
6  * Laboratoire MASI - Institut Blaise Pascal
7  * Universite Pierre et Marie Curie (Paris VI)
8  */
9 
10 #include <linux/fs.h>
11 #include <linux/capability.h>
12 #include <linux/time.h>
13 #include <linux/compat.h>
14 #include <linux/mount.h>
15 #include <linux/file.h>
16 #include <linux/random.h>
17 #include <asm/uaccess.h>
18 #include "ext4_jbd2.h"
19 #include "ext4.h"
20 
21 #define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1)
22 
23 /**
24  * Swap memory between @a and @b for @len bytes.
25  *
26  * @a:          pointer to first memory area
27  * @b:          pointer to second memory area
28  * @len:        number of bytes to swap
29  *
30  */
31 static void memswap(void *a, void *b, size_t len)
32 {
33 	unsigned char *ap, *bp;
34 	unsigned char tmp;
35 
36 	ap = (unsigned char *)a;
37 	bp = (unsigned char *)b;
38 	while (len-- > 0) {
39 		tmp = *ap;
40 		*ap = *bp;
41 		*bp = tmp;
42 		ap++;
43 		bp++;
44 	}
45 }
46 
47 /**
48  * Swap i_data and associated attributes between @inode1 and @inode2.
49  * This function is used for the primary swap between inode1 and inode2
50  * and also to revert this primary swap in case of errors.
51  *
52  * Therefore you have to make sure, that calling this method twice
53  * will revert all changes.
54  *
55  * @inode1:     pointer to first inode
56  * @inode2:     pointer to second inode
57  */
58 static void swap_inode_data(struct inode *inode1, struct inode *inode2)
59 {
60 	loff_t isize;
61 	struct ext4_inode_info *ei1;
62 	struct ext4_inode_info *ei2;
63 
64 	ei1 = EXT4_I(inode1);
65 	ei2 = EXT4_I(inode2);
66 
67 	memswap(&inode1->i_flags, &inode2->i_flags, sizeof(inode1->i_flags));
68 	memswap(&inode1->i_version, &inode2->i_version,
69 		  sizeof(inode1->i_version));
70 	memswap(&inode1->i_blocks, &inode2->i_blocks,
71 		  sizeof(inode1->i_blocks));
72 	memswap(&inode1->i_bytes, &inode2->i_bytes, sizeof(inode1->i_bytes));
73 	memswap(&inode1->i_atime, &inode2->i_atime, sizeof(inode1->i_atime));
74 	memswap(&inode1->i_mtime, &inode2->i_mtime, sizeof(inode1->i_mtime));
75 
76 	memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data));
77 	memswap(&ei1->i_flags, &ei2->i_flags, sizeof(ei1->i_flags));
78 	memswap(&ei1->i_disksize, &ei2->i_disksize, sizeof(ei1->i_disksize));
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 	struct ext4_sb_info *sbi = EXT4_SB(sb);
104 
105 	if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode))
106 		return -EINVAL;
107 
108 	if (!inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN))
109 		return -EPERM;
110 
111 	inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO);
112 	if (IS_ERR(inode_bl))
113 		return PTR_ERR(inode_bl);
114 	ei_bl = EXT4_I(inode_bl);
115 
116 	filemap_flush(inode->i_mapping);
117 	filemap_flush(inode_bl->i_mapping);
118 
119 	/* Protect orig inodes against a truncate and make sure,
120 	 * that only 1 swap_inode_boot_loader is running. */
121 	lock_two_nondirectories(inode, inode_bl);
122 
123 	truncate_inode_pages(&inode->i_data, 0);
124 	truncate_inode_pages(&inode_bl->i_data, 0);
125 
126 	/* Wait for all existing dio workers */
127 	ext4_inode_block_unlocked_dio(inode);
128 	ext4_inode_block_unlocked_dio(inode_bl);
129 	inode_dio_wait(inode);
130 	inode_dio_wait(inode_bl);
131 
132 	handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
133 	if (IS_ERR(handle)) {
134 		err = -EINVAL;
135 		goto journal_err_out;
136 	}
137 
138 	/* Protect extent tree against block allocations via delalloc */
139 	ext4_double_down_write_data_sem(inode, inode_bl);
140 
141 	if (inode_bl->i_nlink == 0) {
142 		/* this inode has never been used as a BOOT_LOADER */
143 		set_nlink(inode_bl, 1);
144 		i_uid_write(inode_bl, 0);
145 		i_gid_write(inode_bl, 0);
146 		inode_bl->i_flags = 0;
147 		ei_bl->i_flags = 0;
148 		inode_bl->i_version = 1;
149 		i_size_write(inode_bl, 0);
150 		inode_bl->i_mode = S_IFREG;
151 		if (EXT4_HAS_INCOMPAT_FEATURE(sb,
152 					      EXT4_FEATURE_INCOMPAT_EXTENTS)) {
153 			ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
154 			ext4_ext_tree_init(handle, inode_bl);
155 		} else
156 			memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
157 	}
158 
159 	swap_inode_data(inode, inode_bl);
160 
161 	inode->i_ctime = inode_bl->i_ctime = ext4_current_time(inode);
162 
163 	spin_lock(&sbi->s_next_gen_lock);
164 	inode->i_generation = sbi->s_next_generation++;
165 	inode_bl->i_generation = sbi->s_next_generation++;
166 	spin_unlock(&sbi->s_next_gen_lock);
167 
168 	ext4_discard_preallocations(inode);
169 
170 	err = ext4_mark_inode_dirty(handle, inode);
171 	if (err < 0) {
172 		ext4_warning(inode->i_sb,
173 			"couldn't mark inode #%lu dirty (err %d)",
174 			inode->i_ino, err);
175 		/* Revert all changes: */
176 		swap_inode_data(inode, inode_bl);
177 	} else {
178 		err = ext4_mark_inode_dirty(handle, inode_bl);
179 		if (err < 0) {
180 			ext4_warning(inode_bl->i_sb,
181 				"couldn't mark inode #%lu dirty (err %d)",
182 				inode_bl->i_ino, err);
183 			/* Revert all changes: */
184 			swap_inode_data(inode, inode_bl);
185 			ext4_mark_inode_dirty(handle, inode);
186 		}
187 	}
188 	ext4_journal_stop(handle);
189 	ext4_double_up_write_data_sem(inode, inode_bl);
190 
191 journal_err_out:
192 	ext4_inode_resume_unlocked_dio(inode);
193 	ext4_inode_resume_unlocked_dio(inode_bl);
194 	unlock_two_nondirectories(inode, inode_bl);
195 	iput(inode_bl);
196 	return err;
197 }
198 
199 static int uuid_is_zero(__u8 u[16])
200 {
201 	int	i;
202 
203 	for (i = 0; i < 16; i++)
204 		if (u[i])
205 			return 0;
206 	return 1;
207 }
208 
209 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
210 {
211 	struct inode *inode = file_inode(filp);
212 	struct super_block *sb = inode->i_sb;
213 	struct ext4_inode_info *ei = EXT4_I(inode);
214 	unsigned int flags;
215 
216 	ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
217 
218 	switch (cmd) {
219 	case EXT4_IOC_GETFLAGS:
220 		ext4_get_inode_flags(ei);
221 		flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
222 		return put_user(flags, (int __user *) arg);
223 	case EXT4_IOC_SETFLAGS: {
224 		handle_t *handle = NULL;
225 		int err, migrate = 0;
226 		struct ext4_iloc iloc;
227 		unsigned int oldflags, mask, i;
228 		unsigned int jflag;
229 
230 		if (!inode_owner_or_capable(inode))
231 			return -EACCES;
232 
233 		if (get_user(flags, (int __user *) arg))
234 			return -EFAULT;
235 
236 		err = mnt_want_write_file(filp);
237 		if (err)
238 			return err;
239 
240 		flags = ext4_mask_flags(inode->i_mode, flags);
241 
242 		err = -EPERM;
243 		mutex_lock(&inode->i_mutex);
244 		/* Is it quota file? Do not allow user to mess with it */
245 		if (IS_NOQUOTA(inode))
246 			goto flags_out;
247 
248 		oldflags = ei->i_flags;
249 
250 		/* The JOURNAL_DATA flag is modifiable only by root */
251 		jflag = flags & EXT4_JOURNAL_DATA_FL;
252 
253 		/*
254 		 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
255 		 * the relevant capability.
256 		 *
257 		 * This test looks nicer. Thanks to Pauline Middelink
258 		 */
259 		if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
260 			if (!capable(CAP_LINUX_IMMUTABLE))
261 				goto flags_out;
262 		}
263 
264 		/*
265 		 * The JOURNAL_DATA flag can only be changed by
266 		 * the relevant capability.
267 		 */
268 		if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
269 			if (!capable(CAP_SYS_RESOURCE))
270 				goto flags_out;
271 		}
272 		if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
273 			migrate = 1;
274 
275 		if (flags & EXT4_EOFBLOCKS_FL) {
276 			/* we don't support adding EOFBLOCKS flag */
277 			if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
278 				err = -EOPNOTSUPP;
279 				goto flags_out;
280 			}
281 		} else if (oldflags & EXT4_EOFBLOCKS_FL)
282 			ext4_truncate(inode);
283 
284 		handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
285 		if (IS_ERR(handle)) {
286 			err = PTR_ERR(handle);
287 			goto flags_out;
288 		}
289 		if (IS_SYNC(inode))
290 			ext4_handle_sync(handle);
291 		err = ext4_reserve_inode_write(handle, inode, &iloc);
292 		if (err)
293 			goto flags_err;
294 
295 		for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
296 			if (!(mask & EXT4_FL_USER_MODIFIABLE))
297 				continue;
298 			if (mask & flags)
299 				ext4_set_inode_flag(inode, i);
300 			else
301 				ext4_clear_inode_flag(inode, i);
302 		}
303 
304 		ext4_set_inode_flags(inode);
305 		inode->i_ctime = ext4_current_time(inode);
306 
307 		err = ext4_mark_iloc_dirty(handle, inode, &iloc);
308 flags_err:
309 		ext4_journal_stop(handle);
310 		if (err)
311 			goto flags_out;
312 
313 		if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
314 			err = ext4_change_inode_journal_flag(inode, jflag);
315 		if (err)
316 			goto flags_out;
317 		if (migrate) {
318 			if (flags & EXT4_EXTENTS_FL)
319 				err = ext4_ext_migrate(inode);
320 			else
321 				err = ext4_ind_migrate(inode);
322 		}
323 
324 flags_out:
325 		mutex_unlock(&inode->i_mutex);
326 		mnt_drop_write_file(filp);
327 		return err;
328 	}
329 	case EXT4_IOC_GETVERSION:
330 	case EXT4_IOC_GETVERSION_OLD:
331 		return put_user(inode->i_generation, (int __user *) arg);
332 	case EXT4_IOC_SETVERSION:
333 	case EXT4_IOC_SETVERSION_OLD: {
334 		handle_t *handle;
335 		struct ext4_iloc iloc;
336 		__u32 generation;
337 		int err;
338 
339 		if (!inode_owner_or_capable(inode))
340 			return -EPERM;
341 
342 		if (ext4_has_metadata_csum(inode->i_sb)) {
343 			ext4_warning(sb, "Setting inode version is not "
344 				     "supported with metadata_csum enabled.");
345 			return -ENOTTY;
346 		}
347 
348 		err = mnt_want_write_file(filp);
349 		if (err)
350 			return err;
351 		if (get_user(generation, (int __user *) arg)) {
352 			err = -EFAULT;
353 			goto setversion_out;
354 		}
355 
356 		mutex_lock(&inode->i_mutex);
357 		handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
358 		if (IS_ERR(handle)) {
359 			err = PTR_ERR(handle);
360 			goto unlock_out;
361 		}
362 		err = ext4_reserve_inode_write(handle, inode, &iloc);
363 		if (err == 0) {
364 			inode->i_ctime = ext4_current_time(inode);
365 			inode->i_generation = generation;
366 			err = ext4_mark_iloc_dirty(handle, inode, &iloc);
367 		}
368 		ext4_journal_stop(handle);
369 
370 unlock_out:
371 		mutex_unlock(&inode->i_mutex);
372 setversion_out:
373 		mnt_drop_write_file(filp);
374 		return err;
375 	}
376 	case EXT4_IOC_GROUP_EXTEND: {
377 		ext4_fsblk_t n_blocks_count;
378 		int err, err2=0;
379 
380 		err = ext4_resize_begin(sb);
381 		if (err)
382 			return err;
383 
384 		if (get_user(n_blocks_count, (__u32 __user *)arg)) {
385 			err = -EFAULT;
386 			goto group_extend_out;
387 		}
388 
389 		if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
390 			       EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
391 			ext4_msg(sb, KERN_ERR,
392 				 "Online resizing not supported with bigalloc");
393 			err = -EOPNOTSUPP;
394 			goto group_extend_out;
395 		}
396 
397 		err = mnt_want_write_file(filp);
398 		if (err)
399 			goto group_extend_out;
400 
401 		err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
402 		if (EXT4_SB(sb)->s_journal) {
403 			jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
404 			err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
405 			jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
406 		}
407 		if (err == 0)
408 			err = err2;
409 		mnt_drop_write_file(filp);
410 group_extend_out:
411 		ext4_resize_end(sb);
412 		return err;
413 	}
414 
415 	case EXT4_IOC_MOVE_EXT: {
416 		struct move_extent me;
417 		struct fd donor;
418 		int err;
419 
420 		if (!(filp->f_mode & FMODE_READ) ||
421 		    !(filp->f_mode & FMODE_WRITE))
422 			return -EBADF;
423 
424 		if (copy_from_user(&me,
425 			(struct move_extent __user *)arg, sizeof(me)))
426 			return -EFAULT;
427 		me.moved_len = 0;
428 
429 		donor = fdget(me.donor_fd);
430 		if (!donor.file)
431 			return -EBADF;
432 
433 		if (!(donor.file->f_mode & FMODE_WRITE)) {
434 			err = -EBADF;
435 			goto mext_out;
436 		}
437 
438 		if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
439 			       EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
440 			ext4_msg(sb, KERN_ERR,
441 				 "Online defrag not supported with bigalloc");
442 			err = -EOPNOTSUPP;
443 			goto mext_out;
444 		}
445 
446 		err = mnt_want_write_file(filp);
447 		if (err)
448 			goto mext_out;
449 
450 		err = ext4_move_extents(filp, donor.file, me.orig_start,
451 					me.donor_start, me.len, &me.moved_len);
452 		mnt_drop_write_file(filp);
453 
454 		if (copy_to_user((struct move_extent __user *)arg,
455 				 &me, sizeof(me)))
456 			err = -EFAULT;
457 mext_out:
458 		fdput(donor);
459 		return err;
460 	}
461 
462 	case EXT4_IOC_GROUP_ADD: {
463 		struct ext4_new_group_data input;
464 		int err, err2=0;
465 
466 		err = ext4_resize_begin(sb);
467 		if (err)
468 			return err;
469 
470 		if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
471 				sizeof(input))) {
472 			err = -EFAULT;
473 			goto group_add_out;
474 		}
475 
476 		if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
477 			       EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
478 			ext4_msg(sb, KERN_ERR,
479 				 "Online resizing not supported with bigalloc");
480 			err = -EOPNOTSUPP;
481 			goto group_add_out;
482 		}
483 
484 		err = mnt_want_write_file(filp);
485 		if (err)
486 			goto group_add_out;
487 
488 		err = ext4_group_add(sb, &input);
489 		if (EXT4_SB(sb)->s_journal) {
490 			jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
491 			err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
492 			jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
493 		}
494 		if (err == 0)
495 			err = err2;
496 		mnt_drop_write_file(filp);
497 		if (!err && ext4_has_group_desc_csum(sb) &&
498 		    test_opt(sb, INIT_INODE_TABLE))
499 			err = ext4_register_li_request(sb, input.group);
500 group_add_out:
501 		ext4_resize_end(sb);
502 		return err;
503 	}
504 
505 	case EXT4_IOC_MIGRATE:
506 	{
507 		int err;
508 		if (!inode_owner_or_capable(inode))
509 			return -EACCES;
510 
511 		err = mnt_want_write_file(filp);
512 		if (err)
513 			return err;
514 		/*
515 		 * inode_mutex prevent write and truncate on the file.
516 		 * Read still goes through. We take i_data_sem in
517 		 * ext4_ext_swap_inode_data before we switch the
518 		 * inode format to prevent read.
519 		 */
520 		mutex_lock(&(inode->i_mutex));
521 		err = ext4_ext_migrate(inode);
522 		mutex_unlock(&(inode->i_mutex));
523 		mnt_drop_write_file(filp);
524 		return err;
525 	}
526 
527 	case EXT4_IOC_ALLOC_DA_BLKS:
528 	{
529 		int err;
530 		if (!inode_owner_or_capable(inode))
531 			return -EACCES;
532 
533 		err = mnt_want_write_file(filp);
534 		if (err)
535 			return err;
536 		err = ext4_alloc_da_blocks(inode);
537 		mnt_drop_write_file(filp);
538 		return err;
539 	}
540 
541 	case EXT4_IOC_SWAP_BOOT:
542 	{
543 		int err;
544 		if (!(filp->f_mode & FMODE_WRITE))
545 			return -EBADF;
546 		err = mnt_want_write_file(filp);
547 		if (err)
548 			return err;
549 		err = swap_inode_boot_loader(sb, inode);
550 		mnt_drop_write_file(filp);
551 		return err;
552 	}
553 
554 	case EXT4_IOC_RESIZE_FS: {
555 		ext4_fsblk_t n_blocks_count;
556 		int err = 0, err2 = 0;
557 		ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
558 
559 		if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
560 			       EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
561 			ext4_msg(sb, KERN_ERR,
562 				 "Online resizing not (yet) supported with bigalloc");
563 			return -EOPNOTSUPP;
564 		}
565 
566 		if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
567 				   sizeof(__u64))) {
568 			return -EFAULT;
569 		}
570 
571 		err = ext4_resize_begin(sb);
572 		if (err)
573 			return err;
574 
575 		err = mnt_want_write_file(filp);
576 		if (err)
577 			goto resizefs_out;
578 
579 		err = ext4_resize_fs(sb, n_blocks_count);
580 		if (EXT4_SB(sb)->s_journal) {
581 			jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
582 			err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
583 			jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
584 		}
585 		if (err == 0)
586 			err = err2;
587 		mnt_drop_write_file(filp);
588 		if (!err && (o_group > EXT4_SB(sb)->s_groups_count) &&
589 		    ext4_has_group_desc_csum(sb) &&
590 		    test_opt(sb, INIT_INODE_TABLE))
591 			err = ext4_register_li_request(sb, o_group);
592 
593 resizefs_out:
594 		ext4_resize_end(sb);
595 		return err;
596 	}
597 
598 	case FITRIM:
599 	{
600 		struct request_queue *q = bdev_get_queue(sb->s_bdev);
601 		struct fstrim_range range;
602 		int ret = 0;
603 
604 		if (!capable(CAP_SYS_ADMIN))
605 			return -EPERM;
606 
607 		if (!blk_queue_discard(q))
608 			return -EOPNOTSUPP;
609 
610 		if (copy_from_user(&range, (struct fstrim_range __user *)arg,
611 		    sizeof(range)))
612 			return -EFAULT;
613 
614 		range.minlen = max((unsigned int)range.minlen,
615 				   q->limits.discard_granularity);
616 		ret = ext4_trim_fs(sb, &range);
617 		if (ret < 0)
618 			return ret;
619 
620 		if (copy_to_user((struct fstrim_range __user *)arg, &range,
621 		    sizeof(range)))
622 			return -EFAULT;
623 
624 		return 0;
625 	}
626 	case EXT4_IOC_PRECACHE_EXTENTS:
627 		return ext4_ext_precache(inode);
628 	case EXT4_IOC_SET_ENCRYPTION_POLICY: {
629 #ifdef CONFIG_EXT4_FS_ENCRYPTION
630 		struct ext4_encryption_policy policy;
631 		int err = 0;
632 
633 		if (copy_from_user(&policy,
634 				   (struct ext4_encryption_policy __user *)arg,
635 				   sizeof(policy))) {
636 			err = -EFAULT;
637 			goto encryption_policy_out;
638 		}
639 
640 		err = ext4_process_policy(&policy, inode);
641 encryption_policy_out:
642 		return err;
643 #else
644 		return -EOPNOTSUPP;
645 #endif
646 	}
647 	case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
648 		int err, err2;
649 		struct ext4_sb_info *sbi = EXT4_SB(sb);
650 		handle_t *handle;
651 
652 		if (!ext4_sb_has_crypto(sb))
653 			return -EOPNOTSUPP;
654 		if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
655 			err = mnt_want_write_file(filp);
656 			if (err)
657 				return err;
658 			handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
659 			if (IS_ERR(handle)) {
660 				err = PTR_ERR(handle);
661 				goto pwsalt_err_exit;
662 			}
663 			err = ext4_journal_get_write_access(handle, sbi->s_sbh);
664 			if (err)
665 				goto pwsalt_err_journal;
666 			generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
667 			err = ext4_handle_dirty_metadata(handle, NULL,
668 							 sbi->s_sbh);
669 		pwsalt_err_journal:
670 			err2 = ext4_journal_stop(handle);
671 			if (err2 && !err)
672 				err = err2;
673 		pwsalt_err_exit:
674 			mnt_drop_write_file(filp);
675 			if (err)
676 				return err;
677 		}
678 		if (copy_to_user((void *) arg, sbi->s_es->s_encrypt_pw_salt,
679 				 16))
680 			return -EFAULT;
681 		return 0;
682 	}
683 	case EXT4_IOC_GET_ENCRYPTION_POLICY: {
684 #ifdef CONFIG_EXT4_FS_ENCRYPTION
685 		struct ext4_encryption_policy policy;
686 		int err = 0;
687 
688 		if (!ext4_encrypted_inode(inode))
689 			return -ENOENT;
690 		err = ext4_get_policy(inode, &policy);
691 		if (err)
692 			return err;
693 		if (copy_to_user((void *)arg, &policy, sizeof(policy)))
694 			return -EFAULT;
695 		return 0;
696 #else
697 		return -EOPNOTSUPP;
698 #endif
699 	}
700 	default:
701 		return -ENOTTY;
702 	}
703 }
704 
705 #ifdef CONFIG_COMPAT
706 long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
707 {
708 	/* These are just misnamed, they actually get/put from/to user an int */
709 	switch (cmd) {
710 	case EXT4_IOC32_GETFLAGS:
711 		cmd = EXT4_IOC_GETFLAGS;
712 		break;
713 	case EXT4_IOC32_SETFLAGS:
714 		cmd = EXT4_IOC_SETFLAGS;
715 		break;
716 	case EXT4_IOC32_GETVERSION:
717 		cmd = EXT4_IOC_GETVERSION;
718 		break;
719 	case EXT4_IOC32_SETVERSION:
720 		cmd = EXT4_IOC_SETVERSION;
721 		break;
722 	case EXT4_IOC32_GROUP_EXTEND:
723 		cmd = EXT4_IOC_GROUP_EXTEND;
724 		break;
725 	case EXT4_IOC32_GETVERSION_OLD:
726 		cmd = EXT4_IOC_GETVERSION_OLD;
727 		break;
728 	case EXT4_IOC32_SETVERSION_OLD:
729 		cmd = EXT4_IOC_SETVERSION_OLD;
730 		break;
731 	case EXT4_IOC32_GETRSVSZ:
732 		cmd = EXT4_IOC_GETRSVSZ;
733 		break;
734 	case EXT4_IOC32_SETRSVSZ:
735 		cmd = EXT4_IOC_SETRSVSZ;
736 		break;
737 	case EXT4_IOC32_GROUP_ADD: {
738 		struct compat_ext4_new_group_input __user *uinput;
739 		struct ext4_new_group_input input;
740 		mm_segment_t old_fs;
741 		int err;
742 
743 		uinput = compat_ptr(arg);
744 		err = get_user(input.group, &uinput->group);
745 		err |= get_user(input.block_bitmap, &uinput->block_bitmap);
746 		err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
747 		err |= get_user(input.inode_table, &uinput->inode_table);
748 		err |= get_user(input.blocks_count, &uinput->blocks_count);
749 		err |= get_user(input.reserved_blocks,
750 				&uinput->reserved_blocks);
751 		if (err)
752 			return -EFAULT;
753 		old_fs = get_fs();
754 		set_fs(KERNEL_DS);
755 		err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
756 				 (unsigned long) &input);
757 		set_fs(old_fs);
758 		return err;
759 	}
760 	case EXT4_IOC_MOVE_EXT:
761 	case FITRIM:
762 	case EXT4_IOC_RESIZE_FS:
763 	case EXT4_IOC_PRECACHE_EXTENTS:
764 	case EXT4_IOC_SET_ENCRYPTION_POLICY:
765 	case EXT4_IOC_GET_ENCRYPTION_PWSALT:
766 	case EXT4_IOC_GET_ENCRYPTION_POLICY:
767 		break;
768 	default:
769 		return -ENOIOCTLCMD;
770 	}
771 	return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
772 }
773 #endif
774