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