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