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