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